From 095cc226321b0aa8be050d6f37e1e02278ca8dee Mon Sep 17 00:00:00 2001 From: peret2000 <19463568+peret2000@users.noreply.github.com> Date: Sun, 3 Jun 2018 17:28:12 +0200 Subject: [PATCH 1/3] Declare _mqttTopicCopy in BootNormal.hpp --- src/Homie/Boot/BootNormal.hpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Homie/Boot/BootNormal.hpp b/src/Homie/Boot/BootNormal.hpp index 8d119715..3391c1b4 100644 --- a/src/Homie/Boot/BootNormal.hpp +++ b/src/Homie/Boot/BootNormal.hpp @@ -86,6 +86,7 @@ class BootNormal : public Boot { std::unique_ptr _mqttPayloadBuffer; std::unique_ptr _mqttTopicLevels; uint8_t _mqttTopicLevelsCount; + std::unique_ptr _mqttTopicCopy; void _wifiConnect(); void _onWifiGotIp(const WiFiEventStationModeGotIP& event); From 9e72036ff2534f21640902461a111ed7b86195ee Mon Sep 17 00:00:00 2001 From: peret2000 <19463568+peret2000@users.noreply.github.com> Date: Sun, 3 Jun 2018 17:36:30 +0200 Subject: [PATCH 2/3] Using _mqttTopicCopy to manage subscribed topics --- src/Homie/Boot/BootNormal.cpp | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/Homie/Boot/BootNormal.cpp b/src/Homie/Boot/BootNormal.cpp index 6176bd17..c7db23b0 100644 --- a/src/Homie/Boot/BootNormal.cpp +++ b/src/Homie/Boot/BootNormal.cpp @@ -20,8 +20,9 @@ BootNormal::BootNormal() , _mqttWillTopic(nullptr) , _mqttPayloadBuffer(nullptr) , _mqttTopicLevels(nullptr) - , _mqttTopicLevelsCount(0) { - strlcpy(_fwChecksum, ESP.getSketchMD5().c_str(), sizeof(_fwChecksum)); + , _mqttTopicLevelsCount(0) + , _mqttTopicCopy(nullptr) + {strlcpy(_fwChecksum, ESP.getSketchMD5().c_str(), sizeof(_fwChecksum)); _fwChecksum[sizeof(_fwChecksum) - 1] = '\0'; } @@ -519,23 +520,28 @@ void BootNormal::_onMqttDisconnected(AsyncMqttClientDisconnectReason reason) { void BootNormal::_onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) { if (total == 0) return; // no empty message possible + size_t topicLength = strlen(topic); + _mqttTopicCopy = std::unique_ptr(new char[topicLength+1]); + memcpy(_mqttTopicCopy.get(), topic, topicLength); + _mqttTopicCopy.get()[topicLength] = '\0'; + // split topic on each "/" if (index == 0) { - __splitTopic(topic); + __splitTopic(_mqttTopicCopy.get()); } // 1. Handle OTA firmware (not copied to payload buffer) - if (__handleOTAUpdates(topic, payload, properties, len, index, total)) + if (__handleOTAUpdates(_mqttTopicCopy.get(), payload, properties, len, index, total)) return; // 2. Fill Payload Buffer - if (__fillPayloadBuffer(topic, payload, properties, len, index, total)) + if (__fillPayloadBuffer(_mqttTopicCopy.get(), payload, properties, len, index, total)) return; /* Arrived here, the payload is complete */ // 3. handle broadcasts - if (__handleBroadcasts(topic, payload, properties, len, index, total)) + if (__handleBroadcasts(_mqttTopicCopy.get(), payload, properties, len, index, total)) return; // 4.all following messages are only for this deviceId @@ -543,15 +549,15 @@ void BootNormal::_onMqttMessage(char* topic, char* payload, AsyncMqttClientMessa return; // 5. handle reset - if (__handleResets(topic, payload, properties, len, index, total)) + if (__handleResets(_mqttTopicCopy.get(), payload, properties, len, index, total)) return; // 6. handle config set - if (__handleConfig(topic, payload, properties, len, index, total)) + if (__handleConfig(_mqttTopicCopy.get(), payload, properties, len, index, total)) return; // 7. here, we're sure we have a node property - if (__handleNodeProperty(topic, payload, properties, len, index, total)) + if (__handleNodeProperty(_mqttTopicCopy.get(), payload, properties, len, index, total)) return; } From 2bf8e12a3b621e86101a60c9be224e2dc7beae65 Mon Sep 17 00:00:00 2001 From: peret2000 <19463568+peret2000@users.noreply.github.com> Date: Mon, 4 Jun 2018 08:21:53 +0200 Subject: [PATCH 3/3] Removed leading spaces --- src/Homie/Boot/BootNormal.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Homie/Boot/BootNormal.cpp b/src/Homie/Boot/BootNormal.cpp index c7db23b0..38910654 100644 --- a/src/Homie/Boot/BootNormal.cpp +++ b/src/Homie/Boot/BootNormal.cpp @@ -524,7 +524,7 @@ void BootNormal::_onMqttMessage(char* topic, char* payload, AsyncMqttClientMessa _mqttTopicCopy = std::unique_ptr(new char[topicLength+1]); memcpy(_mqttTopicCopy.get(), topic, topicLength); _mqttTopicCopy.get()[topicLength] = '\0'; - + // split topic on each "/" if (index == 0) { __splitTopic(_mqttTopicCopy.get());