From 05b02800198a1ef592c0bc6099a6c8cefc239e83 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Thu, 21 Feb 2019 14:31:31 +0100 Subject: [PATCH] 6.4.1.18 - Fix some exceptions and watchdogs 6.4.1.18 20191221 * Fix some exceptions and watchdogs due to lack of stack space - part 1 (#5215) --- sonoff/_changelog.ino | 5 ++++- sonoff/sonoff.ino | 40 ++++++------------------------------ sonoff/sonoff_version.h | 2 +- sonoff/support.ino | 36 ++++++++++++++++++++++++++++++++ sonoff/xdrv_01_webserver.ino | 2 ++ 5 files changed, 49 insertions(+), 36 deletions(-) diff --git a/sonoff/_changelog.ino b/sonoff/_changelog.ino index 0549b2576..0d4fef299 100644 --- a/sonoff/_changelog.ino +++ b/sonoff/_changelog.ino @@ -1,4 +1,7 @@ -/* 6.4.1.17 20190214 +/* 6.4.1.18 20191221 + * Fix some exceptions and watchdogs due to lack of stack space - part 1 (#5215) + * + * 6.4.1.17 20190214 * Change template update by removing possibility to add user module config keeping template as defined (#5222) * Fix regression from 6.4.1.16 where GPIO9 and GPIO10 connected devices did not work (#5197) * Fix GUI wifi password acception starting with asteriks (*) (#5231, #5242) diff --git a/sonoff/sonoff.ino b/sonoff/sonoff.ino index 55d218456..39f0d7e0b 100755 --- a/sonoff/sonoff.ino +++ b/sonoff/sonoff.ino @@ -963,41 +963,12 @@ void MqttDataHandler(char* topic, uint8_t* data, unsigned int data_len) } } } - else if (data_len > 9) { // Workaround exception if empty JSON like {} - Needs checks - StaticJsonBuffer<350> jb; // 331 from https://arduinojson.org/v5/assistant/ - JsonObject& obj = jb.parseObject(dataBuf); - if (!obj.success()) { + else if (data_len > 9) { // Workaround exception if empty JSON like {} - Needs checks + if (JsonTemplate(dataBuf)) { // Free 336 bytes StaticJsonBuffer stack space by moving code to function + if (USER_MODULE == Settings.module) { restart_flag = 2; } + } else { snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_SVALUE, command, D_JSON_INVALID_JSON); error = true; - } else { - // All parameters are optional allowing for partial changes - const char* name = obj[D_JSON_NAME]; - if (name != nullptr) { - strlcpy(Settings.user_template.name, name, sizeof(Settings.user_template.name)); - } - if (obj[D_JSON_GPIO].success()) { - for (uint8_t i = 0; i < sizeof(mycfgio); i++) { - Settings.user_template.gp.io[i] = obj[D_JSON_GPIO][i] | 0; - } - } - if (obj[D_JSON_FLAG].success()) { - uint8_t flag = obj[D_JSON_FLAG] | 0; - memcpy(&Settings.user_template.flag, &flag, sizeof(gpio_flag)); - } - if (obj[D_JSON_BASE].success()) { - uint8_t base = obj[D_JSON_BASE]; - if ((0 == base) || (base >= MAXMODULE)) { base = 17; } else { base--; } - Settings.user_template_base = base; // Default WEMOS - } - - // Validate GPIO -// for (uint8_t i = 0; i < sizeof(mycfgio); i++) { - // For now do not allow non-user configurable GPIO -// if ((Settings.user_template.gp.io[i] > GPIO_FIX_START) && (Settings.user_template.gp.io[i] < GPIO_USER)) { -// Settings.user_template.gp.io[i] = GPIO_NONE; -// }; -// } - if (USER_MODULE == Settings.module) { restart_flag = 2; } } } if (!error) { TemplateJson(); } @@ -2223,7 +2194,8 @@ void ArduinoOTAInit(void) void SerialInput(void) { while (Serial.available()) { - yield(); +// yield(); + delay(0); serial_in_byte = Serial.read(); /*-------------------------------------------------------------------------------------------*\ diff --git a/sonoff/sonoff_version.h b/sonoff/sonoff_version.h index 3816029fd..bba08f9de 100644 --- a/sonoff/sonoff_version.h +++ b/sonoff/sonoff_version.h @@ -20,7 +20,7 @@ #ifndef _SONOFF_VERSION_H_ #define _SONOFF_VERSION_H_ -#define VERSION 0x06040111 +#define VERSION 0x06040112 #define D_PROGRAMNAME "Sonoff-Tasmota" #define D_AUTHOR "Theo Arends" diff --git a/sonoff/support.ino b/sonoff/support.ino index d37185e3f..113968974 100644 --- a/sonoff/support.ino +++ b/sonoff/support.ino @@ -845,6 +845,42 @@ bool GetUsedInModule(uint8_t val, uint8_t *arr) return false; } +bool JsonTemplate(const char* dataBuf) +{ + StaticJsonBuffer<350> jb; // 331 from https://arduinojson.org/v5/assistant/ + JsonObject& obj = jb.parseObject(dataBuf); + if (!obj.success()) { return false; } + + // All parameters are optional allowing for partial changes + const char* name = obj[D_JSON_NAME]; + if (name != nullptr) { + strlcpy(Settings.user_template.name, name, sizeof(Settings.user_template.name)); + } + if (obj[D_JSON_GPIO].success()) { + for (uint8_t i = 0; i < sizeof(mycfgio); i++) { + Settings.user_template.gp.io[i] = obj[D_JSON_GPIO][i] | 0; + } + } + if (obj[D_JSON_FLAG].success()) { + uint8_t flag = obj[D_JSON_FLAG] | 0; + memcpy(&Settings.user_template.flag, &flag, sizeof(gpio_flag)); + } + if (obj[D_JSON_BASE].success()) { + uint8_t base = obj[D_JSON_BASE]; + if ((0 == base) || (base >= MAXMODULE)) { base = 17; } else { base--; } + Settings.user_template_base = base; // Default WEMOS + } + + // Validate GPIO +// for (uint8_t i = 0; i < sizeof(mycfgio); i++) { + // For now do not allow non-user configurable GPIO +// if ((Settings.user_template.gp.io[i] > GPIO_FIX_START) && (Settings.user_template.gp.io[i] < GPIO_USER)) { +// Settings.user_template.gp.io[i] = GPIO_NONE; +// }; +// } + return true; +} + void TemplateJson() { snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_JSON_NAME "\":\"%s\",\"" D_JSON_GPIO "\":["), Settings.user_template.name); diff --git a/sonoff/xdrv_01_webserver.ino b/sonoff/xdrv_01_webserver.ino index 2bdf55248..e8aab33db 100644 --- a/sonoff/xdrv_01_webserver.ino +++ b/sonoff/xdrv_01_webserver.ino @@ -1968,6 +1968,7 @@ void HandleHttpCommand(void) if (JSON) { // Is it a JSON message (and not only [15:26:08 MQT: stat/wemos5/POWER = O]) if (message.length() > 1) { message += F(","); } size_t JSONlen = len - (JSON - tmp); + if (JSONlen > sizeof(mqtt_data)) { JSONlen = sizeof(mqtt_data); } strlcpy(mqtt_data, JSON +1, JSONlen -2); message += mqtt_data; } @@ -2047,6 +2048,7 @@ void HandleAjaxConsoleRefresh(void) } else { cflg = true; } + if (len > sizeof(mqtt_data) -2) { len = sizeof(mqtt_data); } strlcpy(mqtt_data, tmp, len); message += mqtt_data; // mqtt_data used as scratch space }