From 13d5deddec707a70edc571806ea4d56b0fdcd252 Mon Sep 17 00:00:00 2001 From: Blaz Kristan Date: Sun, 17 Dec 2023 11:34:44 +0100 Subject: [PATCH] Fix buffer overwrite in PIR MQTT --- .../usermod_PIR_sensor_switch.h | 16 ++++++++-------- usermods/Temperature/usermod_temperature.h | 1 - 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/usermods/PIR_sensor_switch/usermod_PIR_sensor_switch.h b/usermods/PIR_sensor_switch/usermod_PIR_sensor_switch.h index 62bf3d9df..4c2aff6c7 100644 --- a/usermods/PIR_sensor_switch/usermod_PIR_sensor_switch.h +++ b/usermods/PIR_sensor_switch/usermod_PIR_sensor_switch.h @@ -96,7 +96,7 @@ private: * switch strip on/off */ void switchStrip(bool switchOn); - void publishMqtt(const char* state); + void publishMqtt(bool switchOn); // Create an MQTT Binary Sensor for Home Assistant Discovery purposes, this includes a pointer to the topic that is published to in the Loop. void publishHomeAssistantAutodiscovery(); @@ -226,6 +226,7 @@ void PIRsensorSwitch::switchStrip(bool switchOn) if (PIRtriggered && switchOn) return; //if already on and triggered before, do nothing PIRtriggered = switchOn; DEBUG_PRINT(F("PIR: strip=")); DEBUG_PRINTLN(switchOn?"on":"off"); + publishMqtt(switchOn); if (switchOn) { if (m_onPreset) { if (currentPlaylist>0 && !offMode) { @@ -269,23 +270,22 @@ void PIRsensorSwitch::switchStrip(bool switchOn) } } -void PIRsensorSwitch::publishMqtt(const char* state) +void PIRsensorSwitch::publishMqtt(bool switchOn) { #ifndef WLED_DISABLE_MQTT //Check if MQTT Connected, otherwise it will crash the 8266 if (WLED_MQTT_CONNECTED) { char buf[128]; sprintf_P(buf, PSTR("%s/motion"), mqttDeviceTopic); //max length: 33 + 7 = 40 - mqtt->publish(buf, 0, false, state); + mqtt->publish(buf, 0, false, switchOn?"on":"off"); // Domoticz formatted message if (idx > 0) { StaticJsonDocument <128> msg; msg[F("idx")] = idx; msg[F("RSSI")] = WiFi.RSSI(); msg[F("command")] = F("switchlight"); - strcpy(buf, state); buf[0] = toupper(buf[0]); - msg[F("switchcmd")] = (const char *)buf; - serializeJson(msg, buf, 127); + msg[F("switchcmd")] = switchOn ? F("On") : F("Off"); + serializeJson(msg, buf, 128); mqtt->publish("domoticz/in", 0, false, buf); } } @@ -337,7 +337,7 @@ bool PIRsensorSwitch::updatePIRsensorState() offTimerStart = 0; if (!m_mqttOnly && (!m_nightTimeOnly || (m_nightTimeOnly && !isDayTime()))) switchStrip(true); else if (NotifyUpdateMode != CALL_MODE_NO_NOTIFY) updateInterfaces(CALL_MODE_WS_SEND); - publishMqtt("on"); + //publishMqtt("on"); } else { // start switch off timer offTimerStart = millis(); @@ -355,7 +355,7 @@ bool PIRsensorSwitch::handleOffTimer() if (enabled == true) { if (!m_mqttOnly && (!m_nightTimeOnly || (m_nightTimeOnly && !isDayTime()) || PIRtriggered)) switchStrip(false); else if (NotifyUpdateMode != CALL_MODE_NO_NOTIFY) updateInterfaces(CALL_MODE_WS_SEND); - publishMqtt("off"); + //publishMqtt("off"); } return true; } diff --git a/usermods/Temperature/usermod_temperature.h b/usermods/Temperature/usermod_temperature.h index 0b9bbbda7..1c2d67dee 100644 --- a/usermods/Temperature/usermod_temperature.h +++ b/usermods/Temperature/usermod_temperature.h @@ -283,7 +283,6 @@ void UsermodTemperature::loop() { msg[F("nvalue")] = 0; msg[F("svalue")] = String(getTemperatureC()); serializeJson(msg, subuf, 127); - DEBUG_PRINTLN(subuf); mqtt->publish("domoticz/in", 0, false, subuf); } } else {