From c4d2db98d5dd485be475286e244cd6adc920fd42 Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Sun, 19 May 2024 17:40:11 +0700 Subject: [PATCH] enter recovery mode --- .../lib/ESPMegaPRO/ESPMegaRecovery.cpp | 31 +++++++++++++++---- .../lib/ESPMegaPRO/ESPMegaRecovery.hpp | 1 + 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaRecovery.cpp b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaRecovery.cpp index 0c145c4..06c1618 100644 --- a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaRecovery.cpp +++ b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaRecovery.cpp @@ -3,10 +3,11 @@ ESPMegaRecovery::ESPMegaRecovery() { // Initialize all the pointers to null this->fram = nullptr; - this->web_server = nullptr; this->fram_address = 0; this->bootloop_counter = 0; this->recovery_mode = false; + this->web_server = nullptr; + this->iot = nullptr; } void ESPMegaRecovery::begin() { // Retrieve the bootloop counter from the FRAM @@ -18,13 +19,15 @@ void ESPMegaRecovery::begin() { ESP_LOGV("ESPMegaRecovery", "Bootloop counter: %d", this->getBootloopCounter()); // If the bootloop counter is greater than 5, enter recovery mode if(this->getBootloopCounter() > 5) { - ESP_LOGW("ESPMegaRecovery", "Entering recovery mode"); - this->enterRecoveryMode(); + ESP_LOGE("ESPMegaRecovery", "Bootloop detected"); // Reset the bootloop counter to prevent re-entering recovery mode // The device might unintentionally restart multiple times // By resetting the counter, the user can press reset once in recovery mode to exit ESP_LOGD("ESPMegaRecovery", "Resetting bootloop counter"); this->resetBootloopCounter(); + ESP_LOGW("ESPMegaRecovery", "Entering recovery mode"); + this->enterRecoveryMode(); + this->loop(); } } void ESPMegaRecovery::loop() { @@ -32,9 +35,10 @@ void ESPMegaRecovery::loop() { if(this->isRecoveryMode()) { int i = 0; while(true) { - if (i%10 == 0) + if (i%10 == 0) { ESP_LOGV("ESPMegaRecovery", "System is in recovery mode, no tasks will be executed"); ESP_LOGV("ESPMegaRecovery", "Please upload a new firmware to exit recovery mode"); + } // This code will become the new loop delay(1000); i++; @@ -54,10 +58,25 @@ void ESPMegaRecovery::loop() { void ESPMegaRecovery::enterRecoveryMode() { // Set the recovery mode flag this->recovery_mode = true; - // Remove web server binding + // Enabling the IoT module + ESP_LOGI("ESPMegaRecovery", "Enabling the IoT module"); + this->iot = new ESPMegaIoT(); + this->iot->bindFRAM(this->fram); + ETH.begin(); + this->iot->bindEthernetInterface(Ð); + ESP_LOGI("ESPMegaRecovery", "Loading network configuration"); + this->iot->loadNetworkConfig(); + ESP_LOGI("ESPMegaRecovery", "Attempting to connect to the network"); + this->iot->connectNetwork(); + // Start the web server + ESP_LOGI("ESPMegaRecovery", "Starting the web server"); + this->web_server = new ESPMegaWebServer(80, this->iot); + this->web_server->bindFRAM(this->fram); + this->web_server->loadCredentialsFromFRAM(); + ESP_LOGI("ESPMegaRecovery", "Aquiring the web server instance"); AsyncWebServer *server = this->web_server->getServer(); - server->reset(); // Add OTA update and restart endpoint + ESP_LOGI("ESPMegaRecovery", "Adding OTA update and reboot endpoints"); auto bindedOtaRequestHandler = std::bind(&ESPMegaWebServer::otaRequestHandler, this->web_server, std::placeholders::_1); auto bindedOtaUploadHandler = std::bind(&ESPMegaWebServer::otaUploadHandler, this->web_server, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, std::placeholders::_5, std::placeholders::_6); server->on("/ota_update", HTTP_POST, bindedOtaRequestHandler, bindedOtaUploadHandler); diff --git a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaRecovery.hpp b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaRecovery.hpp index 1687017..a8e9662 100644 --- a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaRecovery.hpp +++ b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaRecovery.hpp @@ -29,6 +29,7 @@ private: FRAM* fram; uint32_t fram_address; uint8_t bootloop_counter; + ESPMegaIoT* iot; ESPMegaWebServer* web_server; bool recovery_mode; }; \ No newline at end of file