From a9af3baea0c927a5131d332688f0c41338469e09 Mon Sep 17 00:00:00 2001 From: nicandris Date: Sat, 19 Sep 2020 13:43:14 +0200 Subject: [PATCH 1/5] Added SetOption112 - Use friendly name in zigbee topic (use with SetOption89) --- tasmota/settings.h | 2 +- tasmota/xdrv_23_zigbee_2_devices.ino | 12 +++++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/tasmota/settings.h b/tasmota/settings.h index 0ce6b9f57..83f6a1a17 100644 --- a/tasmota/settings.h +++ b/tasmota/settings.h @@ -131,7 +131,7 @@ typedef union { // Restricted by MISRA-C Rule 18.4 bu uint32_t alexa_gen_1 : 1; // bit 27 (v8.4.0.3) - SetOption109 - Alexa gen1 mode - if you only have Echo Dot 2nd gen devices uint32_t zb_disable_autobind : 1; // bit 28 (v8.5.0.1) - SetOption110 - disable Zigbee auto-config when pairing new devices uint32_t buzzer_freq_mode : 1; // bit 29 (v8.5.0.1) - SetOption111 - Use frequency output for buzzer pin instead of on/off signal - uint32_t spare30 : 1; // bit 30 + uint32_t zb_topic_fname : 1; // bit 30 (v8.5.0.1) - SetOption112 - Use friendly name in zigbee topic (use with SetOption89) uint32_t spare31 : 1; // bit 31 }; } SysBitfield4; diff --git a/tasmota/xdrv_23_zigbee_2_devices.ino b/tasmota/xdrv_23_zigbee_2_devices.ino index 82000b2e1..a5a3ab2f9 100644 --- a/tasmota/xdrv_23_zigbee_2_devices.ino +++ b/tasmota/xdrv_23_zigbee_2_devices.ino @@ -896,9 +896,15 @@ void Z_Devices::jsonPublishFlush(uint16_t shortaddr) { attr_list.reset(); // clear the attributes if (Settings.flag4.zigbee_distinct_topics) { - char subtopic[16]; - snprintf_P(subtopic, sizeof(subtopic), PSTR("%04X/" D_RSLT_SENSOR), shortaddr); - MqttPublishPrefixTopic_P(TELE, subtopic, Settings.flag.mqtt_sensor_retain); + if (Settings.flag4.zb_topic_fname && fname) { + char frtopic[13]; + snprintf_P(frtopic, sizeof(frtopic) + strlen(fname), PSTR("tele/%s/" D_RSLT_SENSOR), fname); + MqttPublish(frtopic, Settings.flag.mqtt_sensor_retain); + } else { + char subtopic[16]; + snprintf_P(subtopic, sizeof(subtopic), PSTR("%04X/" D_RSLT_SENSOR), shortaddr); + MqttPublishPrefixTopic_P(TELE, subtopic, Settings.flag.mqtt_sensor_retain); + } } else { MqttPublishPrefixTopic_P(TELE, PSTR(D_RSLT_SENSOR), Settings.flag.mqtt_sensor_retain); } From 7adab74ed57c9bc847f16b82652325b7a775c3a6 Mon Sep 17 00:00:00 2001 From: nicandris Date: Sat, 19 Sep 2020 14:02:15 +0200 Subject: [PATCH 2/5] Fix possible buffer overflow --- tasmota/xdrv_23_zigbee_2_devices.ino | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tasmota/xdrv_23_zigbee_2_devices.ino b/tasmota/xdrv_23_zigbee_2_devices.ino index a5a3ab2f9..26667786a 100644 --- a/tasmota/xdrv_23_zigbee_2_devices.ino +++ b/tasmota/xdrv_23_zigbee_2_devices.ino @@ -897,8 +897,8 @@ void Z_Devices::jsonPublishFlush(uint16_t shortaddr) { if (Settings.flag4.zigbee_distinct_topics) { if (Settings.flag4.zb_topic_fname && fname) { - char frtopic[13]; - snprintf_P(frtopic, sizeof(frtopic) + strlen(fname), PSTR("tele/%s/" D_RSLT_SENSOR), fname); + char frtopic[13 + strlen(fname)]; + snprintf_P(frtopic, sizeof(frtopic), PSTR("tele/%s/" D_RSLT_SENSOR), fname); MqttPublish(frtopic, Settings.flag.mqtt_sensor_retain); } else { char subtopic[16]; From 0fce283532fefd1860f438ac4ac16c4e2dfef416 Mon Sep 17 00:00:00 2001 From: nicandris Date: Sat, 19 Sep 2020 14:34:16 +0200 Subject: [PATCH 3/5] included prefix3 in topic --- tasmota/xdrv_23_zigbee_2_devices.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tasmota/xdrv_23_zigbee_2_devices.ino b/tasmota/xdrv_23_zigbee_2_devices.ino index 26667786a..6951f626b 100644 --- a/tasmota/xdrv_23_zigbee_2_devices.ino +++ b/tasmota/xdrv_23_zigbee_2_devices.ino @@ -898,7 +898,7 @@ void Z_Devices::jsonPublishFlush(uint16_t shortaddr) { if (Settings.flag4.zigbee_distinct_topics) { if (Settings.flag4.zb_topic_fname && fname) { char frtopic[13 + strlen(fname)]; - snprintf_P(frtopic, sizeof(frtopic), PSTR("tele/%s/" D_RSLT_SENSOR), fname); + snprintf_P(frtopic, sizeof(frtopic), PSTR("%s/%s/" D_RSLT_SENSOR), SettingsText(SET_MQTTPREFIX3), fname); MqttPublish(frtopic, Settings.flag.mqtt_sensor_retain); } else { char subtopic[16]; From 03da44b2b4ea346b845f15e6516fdcc2b7981038 Mon Sep 17 00:00:00 2001 From: nicandris Date: Sat, 19 Sep 2020 15:20:17 +0200 Subject: [PATCH 4/5] Clean up of friendly name before setting it to topic --- tasmota/xdrv_23_zigbee_2_devices.ino | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/tasmota/xdrv_23_zigbee_2_devices.ino b/tasmota/xdrv_23_zigbee_2_devices.ino index 6951f626b..f8467afd9 100644 --- a/tasmota/xdrv_23_zigbee_2_devices.ino +++ b/tasmota/xdrv_23_zigbee_2_devices.ino @@ -897,8 +897,13 @@ void Z_Devices::jsonPublishFlush(uint16_t shortaddr) { if (Settings.flag4.zigbee_distinct_topics) { if (Settings.flag4.zb_topic_fname && fname) { - char frtopic[13 + strlen(fname)]; - snprintf_P(frtopic, sizeof(frtopic), PSTR("%s/%s/" D_RSLT_SENSOR), SettingsText(SET_MQTTPREFIX3), fname); + //Clean special characters and check size of friendly name + char stemp[TOPSZ]; + strlcpy(stemp, (!strlen(fname)) ? MQTT_TOPIC : fname, sizeof(stemp)); + MakeValidMqtt(0, stemp); + //Create topic with Prefix3 and cleaned up friendly name + char frtopic[13 + strlen(stemp)]; + snprintf_P(frtopic, sizeof(frtopic), PSTR("%s/%s/" D_RSLT_SENSOR), SettingsText(SET_MQTTPREFIX3), stemp); MqttPublish(frtopic, Settings.flag.mqtt_sensor_retain); } else { char subtopic[16]; From 8a3f3b271a079ac071f0bba2f9a29502f3364d6e Mon Sep 17 00:00:00 2001 From: nicandris Date: Sat, 19 Sep 2020 16:02:19 +0200 Subject: [PATCH 5/5] use TOPSZ for size of topic --- tasmota/xdrv_23_zigbee_2_devices.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tasmota/xdrv_23_zigbee_2_devices.ino b/tasmota/xdrv_23_zigbee_2_devices.ino index f8467afd9..fa7775d03 100644 --- a/tasmota/xdrv_23_zigbee_2_devices.ino +++ b/tasmota/xdrv_23_zigbee_2_devices.ino @@ -902,7 +902,7 @@ void Z_Devices::jsonPublishFlush(uint16_t shortaddr) { strlcpy(stemp, (!strlen(fname)) ? MQTT_TOPIC : fname, sizeof(stemp)); MakeValidMqtt(0, stemp); //Create topic with Prefix3 and cleaned up friendly name - char frtopic[13 + strlen(stemp)]; + char frtopic[TOPSZ]; snprintf_P(frtopic, sizeof(frtopic), PSTR("%s/%s/" D_RSLT_SENSOR), SettingsText(SET_MQTTPREFIX3), stemp); MqttPublish(frtopic, Settings.flag.mqtt_sensor_retain); } else {