From 6d73e2a36fd4bc09948f58cf7b4a95902d78a045 Mon Sep 17 00:00:00 2001 From: PabasaraDilshan Date: Fri, 22 Mar 2024 16:34:31 +0530 Subject: [PATCH 1/3] feature: azure iot hub direct method support --- .../tasmota_xdrv_driver/xdrv_02_9_mqtt.ino | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tasmota/tasmota_xdrv_driver/xdrv_02_9_mqtt.ino b/tasmota/tasmota_xdrv_driver/xdrv_02_9_mqtt.ino index d0b2d2d4ca9c..febf3b214f8c 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_02_9_mqtt.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_02_9_mqtt.ino @@ -476,6 +476,7 @@ void MqttSubscribeLib(const char *topic) { String realTopicString = "devices/" + String(SettingsText(SET_MQTT_CLIENT)); realTopicString += "/messages/devicebound/#"; MqttClient.subscribe(realTopicString.c_str()); + MqttClient.subscribe("$iothub/methods/POST/#"); SettingsUpdateText(SET_MQTT_FULLTOPIC, SettingsText(SET_MQTT_CLIENT)); SettingsUpdateText(SET_MQTT_TOPIC, SettingsText(SET_MQTT_CLIENT)); #else @@ -577,6 +578,25 @@ void MqttDataHandler(char* mqtt_topic, uint8_t* mqtt_data, unsigned int data_len char topic[TOPSZ]; #ifdef USE_MQTT_AZURE_IOT + #ifdef USE_AZURE_DIRECT_METHOD + String fullTopicString = String(mqtt_topic); + int startOfMethod = fullTopicString.indexOf("methods/POST"); + int endofMethod = fullTopicString.indexOf("/?$rid"); + String req_id = fullTopicString.substring(endofMethod + 7); + if (startOfMethod == -1){ + AddLog(LOG_LEVEL_ERROR, PSTR(D_LOG_MQTT "Azure IoT Hub message without a method.")); + return; + } + String newMethod = fullTopicString.substring(startOfMethod + 12,endofMethod); + strlcpy(topic, newMethod.c_str(), sizeof(topic)); + mqtt_data[data_len] = 0; + JsonParser mqtt_json_data((char*) mqtt_data); + JsonParserObject message_object = mqtt_json_data.getRootObject(); + String mqtt_data_str= message_object.getStr("payload",""); + data_len = mqtt_data_str.length(); + strncpy(reinterpret_cast(mqtt_data),mqtt_data_str.c_str(),data_len); + + #else // for Azure, we read the topic from the property of the message String fullTopicString = String(mqtt_topic); String toppicUpper = fullTopicString; @@ -593,6 +613,7 @@ void MqttDataHandler(char* mqtt_topic, uint8_t* mqtt_data, unsigned int data_len return; } strlcpy(topic, newTopic.c_str(), sizeof(topic)); + #endif #else strlcpy(topic, mqtt_topic, sizeof(topic)); #endif // USE_MQTT_AZURE_IOT @@ -624,6 +645,11 @@ void MqttDataHandler(char* mqtt_topic, uint8_t* mqtt_data, unsigned int data_len if (Mqtt.disable_logging) { TasmotaGlobal.masterlog_level = LOG_LEVEL_NONE; // Enable logging } + #ifdef USE_AZURE_DIRECT_METHOD // Send response for the direct method + String response_topic = "$iothub/methods/res/200/?$rid=" + req_id; + String payload = "{\"status\": \"success\"}"; + MqttClient.publish(response_topic.c_str(),payload.c_str()); + #endif } /*********************************************************************************************/ From 0ed966f742576f1194506a663d822b0b2124410b Mon Sep 17 00:00:00 2001 From: Pabasara Perera <57918516+PabasaraDilshan@users.noreply.github.com> Date: Fri, 22 Mar 2024 16:39:45 +0530 Subject: [PATCH 2/3] null terminator --- tasmota/tasmota_xdrv_driver/xdrv_02_9_mqtt.ino | 1 + 1 file changed, 1 insertion(+) diff --git a/tasmota/tasmota_xdrv_driver/xdrv_02_9_mqtt.ino b/tasmota/tasmota_xdrv_driver/xdrv_02_9_mqtt.ino index febf3b214f8c..5329372c6237 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_02_9_mqtt.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_02_9_mqtt.ino @@ -595,6 +595,7 @@ void MqttDataHandler(char* mqtt_topic, uint8_t* mqtt_data, unsigned int data_len String mqtt_data_str= message_object.getStr("payload",""); data_len = mqtt_data_str.length(); strncpy(reinterpret_cast(mqtt_data),mqtt_data_str.c_str(),data_len); + mqtt_data[data_len] = 0; #else // for Azure, we read the topic from the property of the message From 92833ca592025e6d1cdada8b097da6f33104746e Mon Sep 17 00:00:00 2001 From: s-hadinger <49731213+s-hadinger@users.noreply.github.com> Date: Mon, 25 Mar 2024 21:54:17 +0100 Subject: [PATCH 3/3] Remove vulnerability --- tasmota/tasmota_xdrv_driver/xdrv_02_9_mqtt.ino | 1 - 1 file changed, 1 deletion(-) diff --git a/tasmota/tasmota_xdrv_driver/xdrv_02_9_mqtt.ino b/tasmota/tasmota_xdrv_driver/xdrv_02_9_mqtt.ino index 5329372c6237..29a67390aedd 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_02_9_mqtt.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_02_9_mqtt.ino @@ -593,7 +593,6 @@ void MqttDataHandler(char* mqtt_topic, uint8_t* mqtt_data, unsigned int data_len JsonParser mqtt_json_data((char*) mqtt_data); JsonParserObject message_object = mqtt_json_data.getRootObject(); String mqtt_data_str= message_object.getStr("payload",""); - data_len = mqtt_data_str.length(); strncpy(reinterpret_cast(mqtt_data),mqtt_data_str.c_str(),data_len); mqtt_data[data_len] = 0;