From 808d2b533db5ca8d1866bed23aaf479622ebaa75 Mon Sep 17 00:00:00 2001 From: Maxim Prokhorov Date: Fri, 29 Nov 2019 00:28:08 +0300 Subject: [PATCH 1/5] ota: arduinoota reboots after end callback --- code/espurna/ota_arduinoota.ino | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/code/espurna/ota_arduinoota.ino b/code/espurna/ota_arduinoota.ino index 78664421..7b6d5e91 100644 --- a/code/espurna/ota_arduinoota.ino +++ b/code/espurna/ota_arduinoota.ino @@ -48,7 +48,10 @@ void _arduinoOtaOnEnd() { #if WEB_SUPPORT wsSend_P(PSTR("{\"action\": \"reload\"}")); #endif - deferredReset(100, CUSTOM_RESET_OTA); + + // Note: ArduinoOTA will reset the board after this callback returns. + customResetReason(CUSTOM_RESET_OTA); + nice_delay(100); } From 655805a404942d62452ea23fb24f98983397d2a7 Mon Sep 17 00:00:00 2001 From: Maxim Prokhorov Date: Fri, 29 Nov 2019 00:32:33 +0300 Subject: [PATCH 2/5] ota: hide progress cb under DEBUG_SUPPORT check --- code/espurna/ota_arduinoota.ino | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/code/espurna/ota_arduinoota.ino b/code/espurna/ota_arduinoota.ino index 7b6d5e91..95011df1 100644 --- a/code/espurna/ota_arduinoota.ino +++ b/code/espurna/ota_arduinoota.ino @@ -63,13 +63,15 @@ void _arduinoOtaOnProgress(unsigned int progress, unsigned int total) { if (wsConnected()) return; #endif - static unsigned int _progOld; + #if DEBUG_SUPPORT + static unsigned int _progOld; - unsigned int _prog = (progress / (total / 100)); - if (_prog != _progOld) { - DEBUG_MSG_P(PSTR("[OTA] Progress: %u%%\r"), _prog); - _progOld = _prog; - } + unsigned int _prog = (progress / (total / 100)); + if (_prog != _progOld) { + DEBUG_MSG_P(PSTR("[OTA] Progress: %u%%\r"), _prog); + _progOld = _prog; + } + #endif } From 03e6443ac692f6b560f46d795c5647af6003a3cb Mon Sep 17 00:00:00 2001 From: Maxim Prokhorov Date: Fri, 29 Nov 2019 00:34:16 +0300 Subject: [PATCH 3/5] system: move prototypes to a separate header --- code/espurna/api.ino | 2 ++ code/espurna/button.ino | 1 + code/espurna/config/prototypes.h | 4 ---- code/espurna/espurna.ino | 11 +++++++---- code/espurna/mqtt.ino | 1 + code/espurna/ota_arduinoota.ino | 2 ++ code/espurna/ota_asynctcp.ino | 2 ++ code/espurna/ota_httpupdate.ino | 1 + code/espurna/system.h | 25 +++++++++++++++++++++++++ code/espurna/system.ino | 6 ++++-- code/espurna/terminal.ino | 1 + code/espurna/web.ino | 1 + code/espurna/ws.ino | 2 ++ 13 files changed, 49 insertions(+), 10 deletions(-) create mode 100644 code/espurna/system.h diff --git a/code/espurna/api.ino b/code/espurna/api.ino index 4271486f..e19a6e04 100644 --- a/code/espurna/api.ino +++ b/code/espurna/api.ino @@ -13,6 +13,8 @@ Copyright (C) 2016-2019 by Xose Pérez #include #include +#include "system.h" + typedef struct { char * key; api_get_callback_f getFn = NULL; diff --git a/code/espurna/button.ino b/code/espurna/button.ino index e23413a1..7716d2f7 100644 --- a/code/espurna/button.ino +++ b/code/espurna/button.ino @@ -15,6 +15,7 @@ Copyright (C) 2016-2019 by Xose Pérez #include #include +#include "system.h" #include "relay.h" #include "light.h" diff --git a/code/espurna/config/prototypes.h b/code/espurna/config/prototypes.h index 8b55670c..0a6a332d 100644 --- a/code/espurna/config/prototypes.h +++ b/code/espurna/config/prototypes.h @@ -38,10 +38,6 @@ extern "C" { #define TCP_MSS (1460) #endif -uint32_t systemResetReason(); -uint8_t systemStabilityCounter(); -void systemStabilityCounter(uint8_t); - // ----------------------------------------------------------------------------- // PROGMEM // ----------------------------------------------------------------------------- diff --git a/code/espurna/espurna.ino b/code/espurna/espurna.ino index c5587383..143d3e4e 100644 --- a/code/espurna/espurna.ino +++ b/code/espurna/espurna.ino @@ -22,14 +22,17 @@ along with this program. If not, see . #include "config/all.h" #include +#include "system.h" #include "utils.h" #include "relay.h" #include "broker.h" #include "tuya.h" #include "libs/HeapStats.h" -std::vector _loop_callbacks; -std::vector _reload_callbacks; +using void_callback_f = void (*)(); + +std::vector _loop_callbacks; +std::vector _reload_callbacks; bool _reload_config = false; unsigned long _loop_delay = 0; @@ -38,11 +41,11 @@ unsigned long _loop_delay = 0; // GENERAL CALLBACKS // ----------------------------------------------------------------------------- -void espurnaRegisterLoop(void (*callback)()) { +void espurnaRegisterLoop(void_callback_f callback) { _loop_callbacks.push_back(callback); } -void espurnaRegisterReload(void (*callback)()) { +void espurnaRegisterReload(void_callback_f callback) { _reload_callbacks.push_back(callback); } diff --git a/code/espurna/mqtt.ino b/code/espurna/mqtt.ino index 37183b3f..d81f765c 100644 --- a/code/espurna/mqtt.ino +++ b/code/espurna/mqtt.ino @@ -18,6 +18,7 @@ Updated secure client support by Niek van der Maas < mail at niekvandermaas dot #include #include +#include "system.h" #include "libs/SecureClientHelpers.h" #if MQTT_LIBRARY == MQTT_LIBRARY_ASYNCMQTTCLIENT diff --git a/code/espurna/ota_arduinoota.ino b/code/espurna/ota_arduinoota.ino index 95011df1..f4e2807e 100644 --- a/code/espurna/ota_arduinoota.ino +++ b/code/espurna/ota_arduinoota.ino @@ -8,6 +8,8 @@ Copyright (C) 2016-2019 by Xose Pérez #if OTA_ARDUINOOTA_SUPPORT +#include "system.h" + // TODO: allocate ArduinoOTAClass on-demand, stop using global instance void _arduinoOtaConfigure() { diff --git a/code/espurna/ota_asynctcp.ino b/code/espurna/ota_asynctcp.ino index a03fa2cb..0e61fe27 100644 --- a/code/espurna/ota_asynctcp.ino +++ b/code/espurna/ota_asynctcp.ino @@ -15,6 +15,8 @@ Copyright (C) 2016-2019 by Xose Pérez #if TERMINAL_SUPPORT || OTA_MQTT_SUPPORT #include + +#include "system.h" #include "libs/URL.h" std::unique_ptr _ota_client = nullptr; diff --git a/code/espurna/ota_httpupdate.ino b/code/espurna/ota_httpupdate.ino index b80113c0..30155f22 100644 --- a/code/espurna/ota_httpupdate.ino +++ b/code/espurna/ota_httpupdate.ino @@ -18,6 +18,7 @@ Copyright (C) 2019 by Maxim Prokhorov #include #include +#include "system.h" #include "libs/URL.h" #include "libs/TypeChecks.h" #include "libs/SecureClientHelpers.h" diff --git a/code/espurna/system.h b/code/espurna/system.h new file mode 100644 index 00000000..c28e840a --- /dev/null +++ b/code/espurna/system.h @@ -0,0 +1,25 @@ +/* + +SYSTEM MODULE + +Copyright (C) 2019 by Xose Pérez + +*/ + +#pragma once + +#include + +uint32_t systemResetReason(); +uint8_t systemStabilityCounter(); +void systemStabilityCounter(uint8_t count); + +void systemCheck(bool stable); +bool systemCheck(); + +uint32_t systemResetReason(); +unsigned char customResetReason(); +void customResetReason(unsigned char reason); + +void deferredReset(unsigned long delay, unsigned char reason); +bool checkNeedsReset(); diff --git a/code/espurna/system.ino b/code/espurna/system.ino index 2f90e5b3..20e88621 100644 --- a/code/espurna/system.ino +++ b/code/espurna/system.ino @@ -9,6 +9,8 @@ Copyright (C) 2019 by Xose Pérez #include #include +#include "system.h" + // ----------------------------------------------------------------------------- bool _system_send_heartbeat = false; @@ -35,10 +37,10 @@ uint8_t systemStabilityCounter() { return data.parts.stability_counter; } -void systemStabilityCounter(uint8_t counter) { +void systemStabilityCounter(uint8_t count) { system_rtcmem_t data; data.value = Rtcmem->sys; - data.parts.stability_counter = counter; + data.parts.stability_counter = count; Rtcmem->sys = data.value; } diff --git a/code/espurna/terminal.ino b/code/espurna/terminal.ino index bc460169..f8853495 100644 --- a/code/espurna/terminal.ino +++ b/code/espurna/terminal.ino @@ -8,6 +8,7 @@ Copyright (C) 2016-2019 by Xose Pérez #if TERMINAL_SUPPORT +#include "system.h" #include "utils.h" #include "libs/EmbedisWrap.h" #include "libs/StreamInjector.h" diff --git a/code/espurna/web.ino b/code/espurna/web.ino index 4fbd0ccc..092755d0 100644 --- a/code/espurna/web.ino +++ b/code/espurna/web.ino @@ -8,6 +8,7 @@ Copyright (C) 2016-2019 by Xose Pérez #if WEB_SUPPORT +#include "system.h" #include "utils.h" #include diff --git a/code/espurna/ws.ino b/code/espurna/ws.ino index 51087464..7b5c89fe 100644 --- a/code/espurna/ws.ino +++ b/code/espurna/ws.ino @@ -13,6 +13,8 @@ Copyright (C) 2016-2019 by Xose Pérez #include #include #include + +#include "system.h" #include "libs/WebSocketIncommingBuffer.h" AsyncWebSocket _ws("/ws"); From 5e0ec8613b056bdab1b94b7ac07b99a52a1365b3 Mon Sep 17 00:00:00 2001 From: Maxim Prokhorov Date: Fri, 29 Nov 2019 01:05:14 +0300 Subject: [PATCH 4/5] system: same name as light for rtcmem struct member --- code/espurna/system.ino | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/code/espurna/system.ino b/code/espurna/system.ino index 20e88621..a2eafd01 100644 --- a/code/espurna/system.ino +++ b/code/espurna/system.ino @@ -27,33 +27,33 @@ union system_rtcmem_t { uint8_t stability_counter; uint8_t reset_reason; uint16_t _reserved_; - } parts; + } packed; uint32_t value; }; uint8_t systemStabilityCounter() { system_rtcmem_t data; data.value = Rtcmem->sys; - return data.parts.stability_counter; + return data.packed.stability_counter; } void systemStabilityCounter(uint8_t count) { system_rtcmem_t data; data.value = Rtcmem->sys; - data.parts.stability_counter = count; + data.packed.stability_counter = count; Rtcmem->sys = data.value; } uint8_t _systemResetReason() { system_rtcmem_t data; data.value = Rtcmem->sys; - return data.parts.reset_reason; + return data.packed.reset_reason; } void _systemResetReason(uint8_t reason) { system_rtcmem_t data; data.value = Rtcmem->sys; - data.parts.reset_reason = reason; + data.packed.reset_reason = reason; Rtcmem->sys = data.value; } From c43efc7deef0f1ccdb489dd6652b2d4bf80f25be Mon Sep 17 00:00:00 2001 From: Maxim Prokhorov Date: Fri, 29 Nov 2019 01:14:50 +0300 Subject: [PATCH 5/5] nofuss: custom reset reason --- code/espurna/nofuss.ino | 1 + 1 file changed, 1 insertion(+) diff --git a/code/espurna/nofuss.ino b/code/espurna/nofuss.ino index 5c6568a4..e6fed563 100644 --- a/code/espurna/nofuss.ino +++ b/code/espurna/nofuss.ino @@ -153,6 +153,7 @@ void nofussSetup() { #endif // TODO: NoFUSS will reset the board after this callback returns. // Maybe this should be optional + customResetReason(CUSTOM_RESET_NOFUSS); nice_delay(100); }