Fix wrong base topic if gateway_name is changed (#1242)
* Rectify and match parameters sizes. Increase mqtt_topic_max_size from 100/50 to 150/75 bytes * Faster and safer compare in cmpToMainTopic()
This commit is contained in:
2 files changed
+23
-19
No files matched your search
+2
-2
@@ -144,7 +144,7 @@ const byte mac[] = {0xDE, 0xED, 0xBA, 0xFE, 0x54, 0x95}; //W5100 ethernet shield
|
||||
//MQTT Parameters definition
|
||||
#if defined(ESP8266) || defined(ESP32) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1280__)
|
||||
# define parameters_size 60
|
||||
# define mqtt_topic_max_size 100
|
||||
# define mqtt_topic_max_size 150
|
||||
# ifndef mqtt_max_packet_size
|
||||
# ifdef MQTT_HTTPS_FW_UPDATE
|
||||
# define mqtt_max_packet_size 2560
|
||||
@@ -154,7 +154,7 @@ const byte mac[] = {0xDE, 0xED, 0xBA, 0xFE, 0x54, 0x95}; //W5100 ethernet shield
|
||||
# endif
|
||||
#else
|
||||
# define parameters_size 30
|
||||
# define mqtt_topic_max_size 50
|
||||
# define mqtt_topic_max_size 75
|
||||
# define mqtt_max_packet_size 128
|
||||
#endif
|
||||
|
||||
|
||||
+21
-17
@@ -163,12 +163,12 @@ void setupTLS(bool self_signed = false, uint8_t index = 0);
|
||||
//adding this to bypass the problem of the arduino builder issue 50
|
||||
void callback(char* topic, byte* payload, unsigned int length);
|
||||
|
||||
char mqtt_user[parameters_size] = MQTT_USER; // not compulsory only if your broker needs authentication
|
||||
char mqtt_pass[parameters_size] = MQTT_PASS; // not compulsory only if your broker needs authentication
|
||||
char mqtt_server[parameters_size] = MQTT_SERVER;
|
||||
char mqtt_user[parameters_size + 1] = MQTT_USER; // not compulsory only if your broker needs authentication
|
||||
char mqtt_pass[parameters_size + 1] = MQTT_PASS; // not compulsory only if your broker needs authentication
|
||||
char mqtt_server[parameters_size + 1] = MQTT_SERVER;
|
||||
char mqtt_port[6] = MQTT_PORT;
|
||||
char mqtt_topic[mqtt_topic_max_size] = Base_Topic;
|
||||
char gateway_name[parameters_size] = Gateway_Name;
|
||||
char mqtt_topic[parameters_size + 1] = Base_Topic;
|
||||
char gateway_name[parameters_size + 1] = Gateway_Name;
|
||||
#ifdef USE_MAC_AS_GATEWAY_NAME
|
||||
# undef WifiManager_ssid
|
||||
# undef ota_hostname
|
||||
@@ -490,15 +490,19 @@ void pubMQTT(String topic, unsigned long payload) {
|
||||
}
|
||||
|
||||
bool cmpToMainTopic(const char* topicOri, const char* toAdd) {
|
||||
char topic[mqtt_topic_max_size];
|
||||
strcpy(topic, mqtt_topic);
|
||||
strcat(topic, gateway_name);
|
||||
strcat(topic, toAdd);
|
||||
if (strstr(topicOri, topic) != NULL) {
|
||||
return true;
|
||||
} else {
|
||||
// Is string "<mqtt_topic><gateway_name><toAdd>" equal to "<topicOri>"?
|
||||
// Compare first part with first chunk
|
||||
if (strncmp(topicOri, mqtt_topic, strlen(mqtt_topic)) != 0)
|
||||
return false;
|
||||
}
|
||||
// Move pointer of sizeof chunk
|
||||
topicOri += strlen(mqtt_topic);
|
||||
// And so on...
|
||||
if (strncmp(topicOri, gateway_name, strlen(gateway_name)) != 0)
|
||||
return false;
|
||||
topicOri += strlen(gateway_name);
|
||||
if (strncmp(topicOri, toAdd, strlen(toAdd)) != 0)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
void connectMQTT() {
|
||||
@@ -1152,11 +1156,11 @@ void setup_wifimanager(bool reset_settings) {
|
||||
WiFiManagerParameter custom_mqtt_server("server", "mqtt server", mqtt_server, parameters_size);
|
||||
WiFiManagerParameter custom_mqtt_port("port", "mqtt port", mqtt_port, 6);
|
||||
WiFiManagerParameter custom_mqtt_user("user", "mqtt user", mqtt_user, parameters_size);
|
||||
WiFiManagerParameter custom_mqtt_pass("pass", "mqtt pass", mqtt_pass, parameters_size * 2);
|
||||
WiFiManagerParameter custom_mqtt_pass("pass", "mqtt pass", mqtt_pass, parameters_size);
|
||||
WiFiManagerParameter custom_mqtt_topic("topic", "mqtt base topic", mqtt_topic, mqtt_topic_max_size);
|
||||
WiFiManagerParameter custom_mqtt_secure("secure", "mqtt secure", "1", 1, mqtt_secure ? "type=\"checkbox\" checked" : "type=\"checkbox\"");
|
||||
WiFiManagerParameter custom_mqtt_cert("cert", "mqtt broker cert", mqtt_cert.c_str(), 2048);
|
||||
WiFiManagerParameter custom_gateway_name("name", "gateway name", gateway_name, parameters_size * 2);
|
||||
WiFiManagerParameter custom_gateway_name("name", "gateway name", gateway_name, parameters_size);
|
||||
# endif
|
||||
//WiFiManager
|
||||
//Local intialization. Once its business is done, there is no need to keep it around
|
||||
@@ -2069,10 +2073,10 @@ void MQTTtoSYS(char* topicOri, JsonObject& SYSdata) { // json object decoding
|
||||
# endif
|
||||
if (SYSdata.containsKey("mqtt_topic") || SYSdata.containsKey("gateway_name")) {
|
||||
if (SYSdata.containsKey("mqtt_topic")) {
|
||||
strncpy(mqtt_topic, SYSdata["mqtt_topic"], mqtt_topic_max_size);
|
||||
strncpy(mqtt_topic, SYSdata["mqtt_topic"], parameters_size);
|
||||
}
|
||||
if (SYSdata.containsKey("gateway_name")) {
|
||||
strncpy(gateway_name, SYSdata["gateway_name"], parameters_size * 2);
|
||||
strncpy(gateway_name, SYSdata["gateway_name"], parameters_size);
|
||||
}
|
||||
# ifndef ESPWifiManualSetup
|
||||
saveMqttConfig();
|
||||
|
||||
Reference in new issue
Block a user