add restart reason to sysinfo page

This commit is contained in:
technyon
2023-02-05 15:03:09 +01:00
parent d72970e46b
commit b3c5a93666
9 changed files with 104 additions and 17 deletions

View File

@@ -61,6 +61,7 @@ file(GLOB SRCFILES
PreferencesKeys.h PreferencesKeys.h
Gpio.cpp Gpio.cpp
Logger.cpp Logger.cpp
RestartReason.h
# include/RTOS.h # include/RTOS.h
lib/WiFiManager/WiFiManager.cpp lib/WiFiManager/WiFiManager.cpp
lib/WiFiManager/wm_consts_en.h lib/WiFiManager/wm_consts_en.h

View File

@@ -5,6 +5,7 @@
#include "networkDevices/WifiDevice.h" #include "networkDevices/WifiDevice.h"
#include "Logger.h" #include "Logger.h"
#include "Config.h" #include "Config.h"
#include "RestartReason.h"
Network* Network::_inst = nullptr; Network* Network::_inst = nullptr;
@@ -187,7 +188,7 @@ bool Network::update()
{ {
if(_restartOnDisconnect && millis() > 60000) if(_restartOnDisconnect && millis() > 60000)
{ {
ESP.restart(); restartEsp(RestartReason::RestartOnDisconnectWatchdog);
} }
Log->println(F("Network not connected. Trying reconnect.")); Log->println(F("Network not connected. Trying reconnect."));
@@ -199,7 +200,7 @@ bool Network::update()
strcpy(WiFi_fallbackDetect, "wifi_fallback"); strcpy(WiFi_fallbackDetect, "wifi_fallback");
Log->println("Network device has a critical failure, enable fallback to Wifi and reboot."); Log->println("Network device has a critical failure, enable fallback to Wifi and reboot.");
delay(200); delay(200);
ESP.restart(); restartEsp(RestartReason::NetworkDeviceCriticalFailure);
break; break;
case ReconnectStatus::Success: case ReconnectStatus::Success:
memset(WiFi_fallbackDetect, 0, sizeof(WiFi_fallbackDetect)); memset(WiFi_fallbackDetect, 0, sizeof(WiFi_fallbackDetect));
@@ -218,7 +219,7 @@ bool Network::update()
{ {
Log->println("Network timeout has been reached, restarting ..."); Log->println("Network timeout has been reached, restarting ...");
delay(200); delay(200);
ESP.restart(); restartEsp(RestartReason::NetworkTimeoutWatchdog);
} }
bool success = reconnect(); bool success = reconnect();

View File

@@ -5,6 +5,7 @@
#include "PreferencesKeys.h" #include "PreferencesKeys.h"
#include "Pins.h" #include "Pins.h"
#include "Logger.h" #include "Logger.h"
#include "RestartReason.h"
NetworkLock::NetworkLock(Network* network, Preferences* preferences) NetworkLock::NetworkLock(Network* network, Preferences* preferences)
: _network(network), : _network(network),
@@ -81,7 +82,7 @@ void NetworkLock::onMqttDataReceived(const char* topic, byte* payload, const uns
{ {
Log->println(F("Restart requested via MQTT.")); Log->println(F("Restart requested via MQTT."));
delay(200); delay(200);
ESP.restart(); restartEsp(RestartReason::RequestedViaMqtt);
} }
if(processActions && comparePrefixedPath(topic, mqtt_topic_lock_action)) if(processActions && comparePrefixedPath(topic, mqtt_topic_lock_action))

View File

@@ -3,6 +3,7 @@
#include "PreferencesKeys.h" #include "PreferencesKeys.h"
#include "MqttTopics.h" #include "MqttTopics.h"
#include "Logger.h" #include "Logger.h"
#include "RestartReason.h"
#include <NukiOpenerUtils.h> #include <NukiOpenerUtils.h>
NukiOpenerWrapper* nukiOpenerInst; NukiOpenerWrapper* nukiOpenerInst;
@@ -108,7 +109,7 @@ void NukiOpenerWrapper::update()
Log->print((millis() - _nukiOpener.getLastReceivedBeaconTs()) / 1000); Log->print((millis() - _nukiOpener.getLastReceivedBeaconTs()) / 1000);
Log->println(" seconds, restarting device."); Log->println(" seconds, restarting device.");
delay(200); delay(200);
ESP.restart(); restartEsp(RestartReason::BLEBeaconWatchdog);
} }
_nukiOpener.updateConnectionState(); _nukiOpener.updateConnectionState();

View File

@@ -3,6 +3,7 @@
#include "PreferencesKeys.h" #include "PreferencesKeys.h"
#include "MqttTopics.h" #include "MqttTopics.h"
#include "Logger.h" #include "Logger.h"
#include "RestartReason.h"
#include <NukiLockUtils.h> #include <NukiLockUtils.h>
NukiWrapper* nukiInst; NukiWrapper* nukiInst;
@@ -134,7 +135,7 @@ void NukiWrapper::update()
Log->print((millis() - _nukiLock.getLastReceivedBeaconTs()) / 1000); Log->print((millis() - _nukiLock.getLastReceivedBeaconTs()) / 1000);
Log->println(" seconds, restarting device."); Log->println(" seconds, restarting device.");
delay(200); delay(200);
ESP.restart(); restartEsp(RestartReason::BLEBeaconWatchdog);
} }
_nukiLock.updateConnectionState(); _nukiLock.updateConnectionState();

68
RestartReason.h Normal file
View File

@@ -0,0 +1,68 @@
#pragma once
enum class RestartReason
{
RequestedViaMqtt,
BLEBeaconWatchdog,
RestartOnDisconnectWatchdog,
RestartIntervalWatchdog,
NetworkTimeoutWatchdog,
WifiInitFailed,
ReconfigureWifi,
NetworkDeviceCriticalFailure,
ConfigurationUpdated,
RestartTimer,
OTATimeout,
DeviceUnpaired
};
#define RESTART_REASON_VALID_DETECT 0xa00ab00bc00bd00d;
extern int restartReason;
extern uint64_t restartReasonValid;
inline static void restartEsp(RestartReason reason)
{
restartReason = (int)reason;
restartReasonValid = RESTART_REASON_VALID_DETECT;
ESP.restart();
}
inline static String getRestartReasion()
{
uint64_t cmp = RESTART_REASON_VALID_DETECT;
if(restartReasonValid != cmp)
{
return "UnknownNoRestartRegistered";
}
switch((RestartReason)restartReason)
{
case RestartReason::RequestedViaMqtt:
return "RequestedViaMqtt";
case RestartReason::BLEBeaconWatchdog:
return "BLEBeaconWatchdog";
case RestartReason::RestartOnDisconnectWatchdog:
return "RestartOnDisconnectWatchdog";
case RestartReason::RestartIntervalWatchdog:
return "RestartIntervalWatchdog";
case RestartReason::NetworkTimeoutWatchdog:
return "NetworkTimeoutWatchdog";
case RestartReason::WifiInitFailed:
return "WifiInitFailed";
case RestartReason::ReconfigureWifi:
return "ReconfigureWifi";
case RestartReason::NetworkDeviceCriticalFailure:
return "NetworkDeviceCriticalFailure";
case RestartReason::ConfigurationUpdated:
return "ConfigurationUpdated";
case RestartReason::RestartTimer:
return "RestartTimer";
case RestartReason::OTATimeout:
return "OTATimeout";
case RestartReason::DeviceUnpaired:
return "DeviceUnpaired";
default:
return "Unknown: " + restartReason;
}
}

View File

@@ -4,6 +4,7 @@
#include "hardware/WifiEthServer.h" #include "hardware/WifiEthServer.h"
#include "Logger.h" #include "Logger.h"
#include "Config.h" #include "Config.h"
#include "RestartReason.h"
#include <esp_task_wdt.h> #include <esp_task_wdt.h>
WebCfgServer::WebCfgServer(NukiWrapper* nuki, NukiOpenerWrapper* nukiOpener, Network* network, EthServer* ethServer, Preferences* preferences, bool allowRestartToPortal) WebCfgServer::WebCfgServer(NukiWrapper* nuki, NukiOpenerWrapper* nukiOpener, Network* network, EthServer* ethServer, Preferences* preferences, bool allowRestartToPortal)
@@ -119,8 +120,8 @@ void WebCfgServer::initialize()
return _server.requestAuthentication(); return _server.requestAuthentication();
} }
String message = ""; String message = "";
bool restartEsp = processArgs(message); bool restart = processArgs(message);
if(restartEsp) if(restart)
{ {
String response = ""; String response = "";
buildConfirmHtml(response, message); buildConfirmHtml(response, message);
@@ -128,7 +129,7 @@ void WebCfgServer::initialize()
Log->println(F("Restarting")); Log->println(F("Restarting"));
waitAndProcess(true, 1000); waitAndProcess(true, 1000);
ESP.restart(); restartEsp(RestartReason::ConfigurationUpdated);
} }
else else
{ {
@@ -176,7 +177,7 @@ void WebCfgServer::initialize()
Log->println(F("Restarting")); Log->println(F("Restarting"));
waitAndProcess(true, 1000); waitAndProcess(true, 1000);
ESP.restart(); restartEsp(RestartReason::ConfigurationUpdated);
}); });
_server.on("/heapoff", [&]() { _server.on("/heapoff", [&]() {
_preferences->putBool(preference_publish_heap, false); _preferences->putBool(preference_publish_heap, false);
@@ -187,7 +188,7 @@ void WebCfgServer::initialize()
Log->println(F("Restarting")); Log->println(F("Restarting"));
waitAndProcess(true, 1000); waitAndProcess(true, 1000);
ESP.restart(); restartEsp(RestartReason::ConfigurationUpdated);
}); });
_server.begin(); _server.begin();
@@ -475,7 +476,7 @@ void WebCfgServer::update()
{ {
Log->println(F("OTA time out, restarting")); Log->println(F("OTA time out, restarting"));
delay(200); delay(200);
ESP.restart(); restartEsp(RestartReason::OTATimeout);
} }
if(!_enabled) return; if(!_enabled) return;
@@ -763,6 +764,14 @@ void WebCfgServer::buildInfoHtml(String &response)
response.concat(esp_get_free_heap_size()); response.concat(esp_get_free_heap_size());
response.concat("\n"); response.concat("\n");
response.concat("Restart reason FW: ");
response.concat(getRestartReasion());
response.concat("\n");
// response.concat("Restart reason ESP: ");
// response.concat(getRestartReasion());
// response.concat("\n");
response.concat("</pre> </BODY></HTML>"); response.concat("</pre> </BODY></HTML>");
} }
@@ -801,7 +810,7 @@ void WebCfgServer::processUnpair(bool opener)
_nukiOpener->unpair(); _nukiOpener->unpair();
} }
waitAndProcess(false, 1000); waitAndProcess(false, 1000);
ESP.restart(); restartEsp(RestartReason::DeviceUnpaired);
} }
void WebCfgServer::buildHtmlHeader(String &response) void WebCfgServer::buildHtmlHeader(String &response)

View File

@@ -12,6 +12,7 @@
#include "Gpio.h" #include "Gpio.h"
#include "Logger.h" #include "Logger.h"
#include "Config.h" #include "Config.h"
#include "RestartReason.h"
Network* network = nullptr; Network* network = nullptr;
NetworkLock* networkLock = nullptr; NetworkLock* networkLock = nullptr;
@@ -28,6 +29,9 @@ bool lockEnabled = false;
bool openerEnabled = false; bool openerEnabled = false;
unsigned long restartTs = (2^32) - 5 * 60000; unsigned long restartTs = (2^32) - 5 * 60000;
RTC_NOINIT_ATTR int restartReason;
RTC_NOINIT_ATTR uint64_t restartReasonValid;
void networkTask(void *pvParameters) void networkTask(void *pvParameters)
{ {
while(true) while(true)
@@ -92,7 +96,7 @@ void checkMillisTask(void *pvParameters)
{ {
Log->println(F("Restart timer expired, restarting device.")); Log->println(F("Restart timer expired, restarting device."));
delay(200); delay(200);
ESP.restart(); restartEsp(RestartReason::RestartTimer);
} }
} }
} }

View File

@@ -4,6 +4,7 @@
#include "../Logger.h" #include "../Logger.h"
#include "../MqttTopics.h" #include "../MqttTopics.h"
#include "espMqttClient.h" #include "espMqttClient.h"
#include "../RestartReason.h"
RTC_NOINIT_ATTR char WiFiDevice_reconfdetect[17]; RTC_NOINIT_ATTR char WiFiDevice_reconfdetect[17];
@@ -80,7 +81,7 @@ void WifiDevice::initialize()
if(!res) { if(!res) {
Log->println(F("Failed to connect. Wait for ESP restart.")); Log->println(F("Failed to connect. Wait for ESP restart."));
delay(1000); delay(1000);
ESP.restart(); restartEsp(RestartReason::WifiInitFailed);
} }
else { else {
Log->print(F("WiFi connected: ")); Log->print(F("WiFi connected: "));
@@ -103,7 +104,7 @@ void WifiDevice::reconfigure()
{ {
strcpy(WiFiDevice_reconfdetect, "reconfigure_wifi"); strcpy(WiFiDevice_reconfdetect, "reconfigure_wifi");
delay(200); delay(200);
ESP.restart(); restartEsp(RestartReason::ReconfigureWifi);
} }
void WifiDevice::printError() void WifiDevice::printError()
@@ -143,7 +144,7 @@ void WifiDevice::onDisconnected()
{ {
if(millis() > 60000) if(millis() > 60000)
{ {
ESP.restart(); restartEsp(RestartReason::RestartOnDisconnectWatchdog);
} }
} }