MQTT null termination fix.

Nigtttime detection for PIR fix.
This commit is contained in:
Blaz Kristan 2021-05-11 16:20:43 +02:00
parent 05b86a71fd
commit 2b7f2d4744
3 changed files with 18 additions and 14 deletions

View File

@ -108,20 +108,15 @@ private:
uint8_t hr = hour(localTime);
uint8_t mi = minute(localTime);
DEBUG_PRINTF("--Time: %02d:%02d\n", (int)hr, (int)mi);
if (sunrise && sunset) {
DEBUG_PRINTLN(F("--Sunrise & sunset detected."));
if (hour(sunrise)>hr && hour(sunset)<hr) {
if (hour(sunrise)<hr && hour(sunset)>hr) {
isDayTime = true;
DEBUG_PRINTLN(F("--It is daytime."));
} else {
if (hour(sunrise)==hr && minute(sunrise)<mi) {
isDayTime = true;
DEBUG_PRINTLN(F("--It is daytime."));
}
if (hour(sunset)==hr && minute(sunset)>mi) {
isDayTime = true;
DEBUG_PRINTLN(F("--It is daytime."));
}
}
}

View File

@ -64,6 +64,13 @@ void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties
}
DEBUG_PRINTLN(payload);
// payload is not always null terminated
char *payload0 = new char[len+1];
if (payload0==nullptr) return; // out of memory
strncpy(payload0,payload,len);
payload0[len] = '\0';
DEBUG_PRINTLN(payload0);
size_t topicPrefixLen = strlen(mqttDeviceTopic);
if (strncmp(topic, mqttDeviceTopic, topicPrefixLen) == 0) {
topic += topicPrefixLen;
@ -73,7 +80,8 @@ void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties
topic += topicPrefixLen;
} else {
// Non-Wled Topic used here. Probably a usermod subscribed to this topic.
usermods.onMqttMessage(topic, payload);
usermods.onMqttMessage(topic, payload0);
delete[] payload0;
return;
}
}
@ -81,25 +89,26 @@ void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties
//Prefix is stripped from the topic at this point
if (strcmp_P(topic, PSTR("/col")) == 0) {
colorFromDecOrHexString(col, (char*)payload);
colorFromDecOrHexString(col, payload0);
colorUpdated(NOTIFIER_CALL_MODE_DIRECT_CHANGE);
} else if (strcmp_P(topic, PSTR("/api")) == 0) {
if (payload[0] == '{') { //JSON API
if (payload0[0] == '{') { //JSON API
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
deserializeJson(doc, payload);
deserializeJson(doc, payload0);
deserializeState(doc.as<JsonObject>());
} else { //HTTP API
String apireq = "win&";
apireq += (char*)payload;
apireq += payload0;
handleSet(nullptr, apireq);
}
} else if (strlen(topic) != 0) {
// non standard topic, check with usermods
usermods.onMqttMessage(topic, payload);
usermods.onMqttMessage(topic, payload0);
} else {
// topmost topic (just wled/MAC)
parseMQTTBriPayload(payload);
parseMQTTBriPayload(payload0);
}
delete[] payload0;
}

View File

@ -8,7 +8,7 @@
*/
// version code in format yymmddb (b = daily build)
#define VERSION 2105111
#define VERSION 2105112
//uncomment this if you have a "my_config.h" file you'd like to use
//#define WLED_USE_MY_CONFIG