From 545cc27ca89066bdeb712a6a73936496a065ed05 Mon Sep 17 00:00:00 2001 From: Marius Bezuidenhout Date: Sat, 4 May 2024 14:42:35 +0200 Subject: [PATCH] Fixed HexToBytes function. Added Publish3 command. (#21329) --- tasmota/tasmota_support/support.ino | 5 ++--- .../tasmota_xdrv_driver/xdrv_02_9_mqtt.ino | 21 ++++++++++++++++++- .../xdrv_05_irremote_full.ino | 2 +- .../xdrv_73_8_lorawan_bridge.ino | 6 +++--- 4 files changed, 26 insertions(+), 8 deletions(-) diff --git a/tasmota/tasmota_support/support.ino b/tasmota/tasmota_support/support.ino index 9d1306a43..cc9011c8a 100755 --- a/tasmota/tasmota_support/support.ino +++ b/tasmota/tasmota_support/support.ino @@ -611,16 +611,15 @@ String HexToString(uint8_t* data, uint32_t length) { // Converts a Hex string (case insensitive) into an array of bytes // Returns the number of bytes in the array, or -1 if an error occured // The `out` buffer must be at least half the size of hex string -int32_t HexToBytes(const char* hex, uint8_t* out, size_t* outLen) { +int32_t HexToBytes(const char* hex, uint8_t* out, size_t outLen) { size_t len = strlen_P(hex); - *outLen = 0; if (len % 2 != 0) { return -1; } size_t outLength = len / 2; - for(size_t i = 0; i < outLength; i++) { + for (size_t i = 0; i < outLength && i < outLen; i++) { char byte[3]; byte[0] = hex[i*2]; byte[1] = hex[i*2 + 1]; diff --git a/tasmota/tasmota_xdrv_driver/xdrv_02_9_mqtt.ino b/tasmota/tasmota_xdrv_driver/xdrv_02_9_mqtt.ino index 681be4104..8dd67aa5a 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_02_9_mqtt.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_02_9_mqtt.ino @@ -742,6 +742,25 @@ void MqttPublish(const char* topic, bool retained) { MqttPublishPayload(topic, ResponseData(), 0, retained); } +void MqttPublish(const char* topic, bool retained, bool binary) { + int32_t binary_length = 0; + char *response_data = ResponseData(); + if (binary) { + // Binary data will be half of the size of a text packet + uint8_t binary_payload[MQTT_MAX_PACKET_SIZE / 2]; + binary_length = HexToBytes(response_data, binary_payload, MQTT_MAX_PACKET_SIZE / 2); + if (binary_length == -1) { + AddLog(LOG_LEVEL_INFO, PSTR(D_LOG_MQTT "Invalid hex: %s"), response_data); + } else { + // Conversion was successful + MqttPublishPayload(topic, (const char *)binary_payload, binary_length, retained); + } + } else { + // Publish default ResponseData string with optional retained + MqttPublishPayload(topic, response_data, 0, retained); + } +} + void MqttPublish(const char* topic) { // Publish default ResponseData string no retained MqttPublish(topic, false); @@ -1550,7 +1569,7 @@ void CmndPublish(void) { } else { ResponseClear(); } - MqttPublish(stemp1, (XdrvMailbox.index == 2)); + MqttPublish(stemp1, (XdrvMailbox.index == 2), (XdrvMailbox.index == 3)); ResponseClear(); } } diff --git a/tasmota/tasmota_xdrv_driver/xdrv_05_irremote_full.ino b/tasmota/tasmota_xdrv_driver/xdrv_05_irremote_full.ino index 8ce870fdb..e0f6da521 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_05_irremote_full.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_05_irremote_full.ino @@ -654,7 +654,7 @@ uint32_t IrRemoteCmndIrSendJson(void) } uint8_t data_bytes[num_bytes]; // allocate on stack since it's small enough - if (HexToBytes(data_hex, data_bytes, &num_bytes) <= 0) { return IE_INVALID_HEXDATA; } + if (HexToBytes(data_hex, data_bytes, num_bytes) <= 0) { return IE_INVALID_HEXDATA; } if (lsb) { reverseBits(data_bytes, bits); diff --git a/tasmota/tasmota_xdrv_driver/xdrv_73_8_lorawan_bridge.ino b/tasmota/tasmota_xdrv_driver/xdrv_73_8_lorawan_bridge.ino index d88d57fa2..797528842 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_73_8_lorawan_bridge.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_73_8_lorawan_bridge.ino @@ -93,7 +93,7 @@ bool LoraWanLoadData(void) { app_key = root.getStr(PSTR(D_JSON_APPKEY), nullptr); if (strlen(app_key)) { size_t out_len = TAS_LORAWAN_AES128_KEY_SIZE; - HexToBytes(app_key, Lora->settings.end_node[n].AppKey, &out_len); + HexToBytes(app_key, Lora->settings.end_node[n].AppKey, out_len); } Lora->settings.end_node[n].DevEUIh = root.getUInt(PSTR(D_JSON_DEVEUI "h"), Lora->settings.end_node[n].DevEUIh); Lora->settings.end_node[n].DevEUIl = root.getUInt(PSTR(D_JSON_DEVEUI "l"), Lora->settings.end_node[n].DevEUIl); @@ -581,8 +581,8 @@ void CmndLoraWanAppKey(void) { if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= TAS_LORAWAN_ENDNODES)) { uint32_t node = XdrvMailbox.index -1; if (32 == XdrvMailbox.data_len) { - size_t out_len = 16; - HexToBytes(XdrvMailbox.data, Lora->settings.end_node[node].AppKey, &out_len); + size_t out_len = TAS_LORAWAN_AES128_KEY_SIZE; + HexToBytes(XdrvMailbox.data, Lora->settings.end_node[node].AppKey, out_len); if (0 == Lora->settings.end_node[node].name.length()) { Lora->settings.end_node[node].name = F("0x0000"); }