From a4ce4830e867580097f672b8fbffbdc2728213fe Mon Sep 17 00:00:00 2001 From: Matthijs Abma <4146168+abmaonline@users.noreply.github.com> Date: Sat, 23 May 2020 17:34:59 +0200 Subject: [PATCH 1/4] Encode content when not raw or json --- tasmota/support_tasmota.ino | 6 +++++- tasmota/xdrv_08_serial_bridge.ino | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/tasmota/support_tasmota.ino b/tasmota/support_tasmota.ino index 671b4ad9d..6ccd057da 100644 --- a/tasmota/support_tasmota.ino +++ b/tasmota/support_tasmota.ino @@ -1349,7 +1349,11 @@ void SerialInput(void) bool assume_json = (!Settings.flag.mqtt_serial_raw && (serial_in_buffer[0] == '{')); Response_P(PSTR("{\"" D_JSON_SERIALRECEIVED "\":%s%s%s}"), (assume_json) ? "" : "\"", - (Settings.flag.mqtt_serial_raw) ? ToHex_P((unsigned char*)serial_in_buffer, serial_in_byte_counter, hex_char, sizeof(hex_char)) : serial_in_buffer, + (Settings.flag.mqtt_serial_raw) + ? ToHex_P((unsigned char*)serial_in_buffer, serial_in_byte_counter, hex_char, sizeof(hex_char)) + : (assume_json) + ? serial_in_buffer + : EscapeJSONString(serial_in_buffer).c_str(), (assume_json) ? "" : "\""); MqttPublishPrefixTopic_P(RESULT_OR_TELE, PSTR(D_JSON_SERIALRECEIVED)); XdrvRulesProcess(); diff --git a/tasmota/xdrv_08_serial_bridge.ino b/tasmota/xdrv_08_serial_bridge.ino index a713b5cfb..7aa6397dd 100644 --- a/tasmota/xdrv_08_serial_bridge.ino +++ b/tasmota/xdrv_08_serial_bridge.ino @@ -80,7 +80,11 @@ void SerialBridgeInput(void) bool assume_json = (!serial_bridge_raw && (serial_bridge_buffer[0] == '{')); Response_P(PSTR("{\"" D_JSON_SSERIALRECEIVED "\":%s%s%s}"), (assume_json) ? "" : "\"", - (serial_bridge_raw) ? ToHex_P((unsigned char*)serial_bridge_buffer, serial_bridge_in_byte_counter, hex_char, sizeof(hex_char)) : serial_bridge_buffer, + (serial_bridge_raw) + ? ToHex_P((unsigned char*)serial_bridge_buffer, serial_bridge_in_byte_counter, hex_char, sizeof(hex_char)) + : (assume_json) + ? serial_bridge_buffer + : EscapeJSONString(serial_bridge_buffer).c_str(), (assume_json) ? "" : "\""); MqttPublishPrefixTopic_P(RESULT_OR_TELE, PSTR(D_JSON_SSERIALRECEIVED)); XdrvRulesProcess(); From 7210934774818200f14f37eb0a1c06e7361705c1 Mon Sep 17 00:00:00 2001 From: Matthijs Abma <4146168+abmaonline@users.noreply.github.com> Date: Sun, 24 May 2020 17:15:06 +0200 Subject: [PATCH 2/4] Cleanup code and only allocate buffer when needed --- tasmota/support_tasmota.ino | 25 ++++++++++++++++--------- tasmota/xdrv_08_serial_bridge.ino | 25 ++++++++++++++++--------- 2 files changed, 32 insertions(+), 18 deletions(-) diff --git a/tasmota/support_tasmota.ino b/tasmota/support_tasmota.ino index 6ccd057da..e0a45ce77 100644 --- a/tasmota/support_tasmota.ino +++ b/tasmota/support_tasmota.ino @@ -1345,16 +1345,23 @@ void SerialInput(void) if (Settings.flag.mqtt_serial && serial_in_byte_counter && (millis() > (serial_polling_window + SERIAL_POLLING))) { // CMND_SERIALSEND and CMND_SERIALLOG serial_in_buffer[serial_in_byte_counter] = 0; // Serial data completed - char hex_char[(serial_in_byte_counter * 2) + 2]; bool assume_json = (!Settings.flag.mqtt_serial_raw && (serial_in_buffer[0] == '{')); - Response_P(PSTR("{\"" D_JSON_SERIALRECEIVED "\":%s%s%s}"), - (assume_json) ? "" : "\"", - (Settings.flag.mqtt_serial_raw) - ? ToHex_P((unsigned char*)serial_in_buffer, serial_in_byte_counter, hex_char, sizeof(hex_char)) - : (assume_json) - ? serial_in_buffer - : EscapeJSONString(serial_in_buffer).c_str(), - (assume_json) ? "" : "\""); + + Response_P(PSTR("{\"" D_JSON_SERIALRECEIVED "\":")); + if (assume_json) { + ResponseAppend_P(serial_in_buffer); + } else { + ResponseAppend_P(PSTR("\"")); + if (Settings.flag.mqtt_serial_raw) { + char hex_char[(serial_in_byte_counter * 2) + 2]; + ResponseAppend_P(ToHex_P((unsigned char*)serial_in_buffer, serial_in_byte_counter, hex_char, sizeof(hex_char))); + } else { + ResponseAppend_P(EscapeJSONString(serial_in_buffer).c_str()); + } + ResponseAppend_P(PSTR("\"")); + } + ResponseAppend_P(PSTR("}")); + MqttPublishPrefixTopic_P(RESULT_OR_TELE, PSTR(D_JSON_SERIALRECEIVED)); XdrvRulesProcess(); serial_in_byte_counter = 0; diff --git a/tasmota/xdrv_08_serial_bridge.ino b/tasmota/xdrv_08_serial_bridge.ino index 7aa6397dd..e50d4dd33 100644 --- a/tasmota/xdrv_08_serial_bridge.ino +++ b/tasmota/xdrv_08_serial_bridge.ino @@ -76,16 +76,23 @@ void SerialBridgeInput(void) if (serial_bridge_in_byte_counter && (millis() > (serial_bridge_polling_window + SERIAL_POLLING))) { serial_bridge_buffer[serial_bridge_in_byte_counter] = 0; // Serial data completed - char hex_char[(serial_bridge_in_byte_counter * 2) + 2]; bool assume_json = (!serial_bridge_raw && (serial_bridge_buffer[0] == '{')); - Response_P(PSTR("{\"" D_JSON_SSERIALRECEIVED "\":%s%s%s}"), - (assume_json) ? "" : "\"", - (serial_bridge_raw) - ? ToHex_P((unsigned char*)serial_bridge_buffer, serial_bridge_in_byte_counter, hex_char, sizeof(hex_char)) - : (assume_json) - ? serial_bridge_buffer - : EscapeJSONString(serial_bridge_buffer).c_str(), - (assume_json) ? "" : "\""); + + Response_P(PSTR("{\"" D_JSON_SSERIALRECEIVED "\":")); + if (assume_json) { + ResponseAppend_P(serial_bridge_buffer); + } else { + ResponseAppend_P(PSTR("\"")); + if (serial_bridge_raw) { + char hex_char[(serial_bridge_in_byte_counter * 2) + 2]; + ResponseAppend_P(ToHex_P((unsigned char*)serial_bridge_buffer, serial_bridge_in_byte_counter, hex_char, sizeof(hex_char))); + } else { + ResponseAppend_P(EscapeJSONString(serial_bridge_buffer).c_str()); + } + ResponseAppend_P(PSTR("\"")); + } + ResponseAppend_P(PSTR("}")); + MqttPublishPrefixTopic_P(RESULT_OR_TELE, PSTR(D_JSON_SSERIALRECEIVED)); XdrvRulesProcess(); serial_bridge_in_byte_counter = 0; From 721c2c8f7fe0a7ae88fd4211d56d58bbdab71961 Mon Sep 17 00:00:00 2001 From: Matthijs Abma <4146168+abmaonline@users.noreply.github.com> Date: Sun, 24 May 2020 17:23:26 +0200 Subject: [PATCH 3/4] Reduce input buffer so hex/json encoded content always fits --- tasmota/tasmota.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tasmota/tasmota.h b/tasmota/tasmota.h index 7434ff28e..942d718d2 100644 --- a/tasmota/tasmota.h +++ b/tasmota/tasmota.h @@ -124,12 +124,12 @@ const uint16_t SYSLOG_TIMER = 600; // Seconds to restore syslog_level const uint16_t SERIALLOG_TIMER = 600; // Seconds to disable SerialLog const uint8_t OTA_ATTEMPTS = 5; // Number of times to try fetching the new firmware -const uint16_t INPUT_BUFFER_SIZE = 520; // Max number of characters in serial command buffer +const uint16_t INPUT_BUFFER_SIZE = 510; // Max number of characters in serial command buffer: floor((MIN_MESSZ - len({"SerialReceived":""})) / 2) + 1 const uint16_t FLOATSZ = 16; // Max number of characters in float result from dtostrfd (max 32) const uint16_t CMDSZ = 24; // Max number of characters in command const uint16_t TOPSZ = 151; // Max number of characters in topic string const uint16_t LOGSZ = 700; // Max number of characters in log -const uint16_t MIN_MESSZ = 1040; // Min number of characters in MQTT message (1000 - TOPSZ - 9 header bytes) +const uint16_t MIN_MESSZ = 1040; // Min number of characters in MQTT message (1200 - TOPSZ - 9 header bytes) const uint8_t SENSOR_MAX_MISS = 5; // Max number of missed sensor reads before deciding it's offline From 7bfbe95d0fd5e9f6c9a70932a8e6feca71b6f881 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Tue, 26 May 2020 12:15:20 +0200 Subject: [PATCH 4/4] Update tasmota.h --- tasmota/tasmota.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tasmota/tasmota.h b/tasmota/tasmota.h index 942d718d2..fdace042c 100644 --- a/tasmota/tasmota.h +++ b/tasmota/tasmota.h @@ -124,7 +124,7 @@ const uint16_t SYSLOG_TIMER = 600; // Seconds to restore syslog_level const uint16_t SERIALLOG_TIMER = 600; // Seconds to disable SerialLog const uint8_t OTA_ATTEMPTS = 5; // Number of times to try fetching the new firmware -const uint16_t INPUT_BUFFER_SIZE = 510; // Max number of characters in serial command buffer: floor((MIN_MESSZ - len({"SerialReceived":""})) / 2) + 1 +const uint16_t INPUT_BUFFER_SIZE = 520; // Max number of characters in serial command buffer const uint16_t FLOATSZ = 16; // Max number of characters in float result from dtostrfd (max 32) const uint16_t CMDSZ = 24; // Max number of characters in command const uint16_t TOPSZ = 151; // Max number of characters in topic string