Skip to content

Commit

Permalink
Azure iothub direct method support (#21013)
Browse files Browse the repository at this point in the history
* feature: azure iot hub direct method support

* null terminator

* Remove vulnerability

---------

Co-authored-by: s-hadinger <49731213+s-hadinger@users.noreply.github.com>
  • Loading branch information
PabasaraDilshan and s-hadinger authored Mar 25, 2024
1 parent 10465ce commit 2fff690
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions tasmota/tasmota_xdrv_driver/xdrv_02_9_mqtt.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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","");
strncpy(reinterpret_cast<char*>(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
String fullTopicString = String(mqtt_topic);
String toppicUpper = fullTopicString;
Expand All @@ -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
Expand Down Expand Up @@ -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
}

/*********************************************************************************************/
Expand Down

0 comments on commit 2fff690

Please sign in to comment.