ESPMegaPRO-v3-SDK/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaRecovery.cpp

76 lines
2.6 KiB
C++
Raw Normal View History

2024-02-14 09:30:46 +00:00
#include <ESPMegaRecovery.hpp>
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;
}
void ESPMegaRecovery::begin() {
// Retrieve the bootloop counter from the FRAM
if(this->fram != nullptr) {
this->bootloop_counter = this->fram->read8(this->fram_address);
}
// Inclement the bootloop counter
this->inclementBootloopCounter();
// If the bootloop counter is greater than 5, enter recovery mode
if(this->getBootloopCounter() > 5) {
this->enterRecoveryMode();
}
}
void ESPMegaRecovery::loop() {
// If the device is in recovery mode, block all other tasks
if(this->isRecoveryMode()) {
while(true) {
// This code will become the new loop
delay(1000);
}
}
2024-02-14 09:32:49 +00:00
// Watchdog timer
static bool booted = false;
static uint32_t boot_time = millis();
if(!booted) {
if(millis() - boot_time > RECOVERY_WATCHDOG_TIMEOUT * 1000) {
this->resetBootloopCounter();
booted = true;
}
}
2024-02-14 09:30:46 +00:00
}
void ESPMegaRecovery::enterRecoveryMode() {
// Set the recovery mode flag
this->recovery_mode = true;
// Remove web server binding
AsyncWebServer *server = this->web_server->getServer();
server->reset();
// Add OTA update and restart endpoint
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);
server->on("/reboot", HTTP_GET, std::bind(&ESPMegaWebServer::rebootHandler, this->web_server, std::placeholders::_1));
}
void ESPMegaRecovery::bindFRAM(FRAM *fram, uint32_t address) {
this->fram = fram;
this->fram_address = address;
}
uint8_t ESPMegaRecovery::getBootloopCounter() {
return this->bootloop_counter;
}
void ESPMegaRecovery::inclementBootloopCounter() {
this->bootloop_counter++;
if(this->fram != nullptr) {
this->fram->write8(this->fram_address, this->bootloop_counter);
}
}
void ESPMegaRecovery::resetBootloopCounter() {
this->bootloop_counter = 0;
if(this->fram != nullptr) {
this->fram->write8(this->fram_address, this->bootloop_counter);
}
}
bool ESPMegaRecovery::isRecoveryMode() {
return this->recovery_mode;
}