From 5b9e628eaaf2e2df918501d3eb8c435c9f26e8b4 Mon Sep 17 00:00:00 2001 From: proddy Date: Thu, 4 Mar 2021 21:40:03 +0100 Subject: [PATCH] fix factory reset using LITTLEFS --- lib/framework/FactoryResetService.cpp | 46 +++++++++++++++------------ src/system.cpp | 37 ++++++++------------- src/test/test.cpp | 1 + src/test/test.h | 2 -- 4 files changed, 40 insertions(+), 46 deletions(-) diff --git a/lib/framework/FactoryResetService.cpp b/lib/framework/FactoryResetService.cpp index 974220702..e97fa7367 100644 --- a/lib/framework/FactoryResetService.cpp +++ b/lib/framework/FactoryResetService.cpp @@ -2,16 +2,14 @@ using namespace std::placeholders; -FactoryResetService::FactoryResetService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager) : fs(fs) { - server->on(FACTORY_RESET_SERVICE_PATH, - HTTP_POST, - securityManager->wrapRequest(std::bind(&FactoryResetService::handleRequest, this, _1), - AuthenticationPredicates::IS_ADMIN)); +FactoryResetService::FactoryResetService(AsyncWebServer * server, FS * fs, SecurityManager * securityManager) + : fs(fs) { + server->on(FACTORY_RESET_SERVICE_PATH, HTTP_POST, securityManager->wrapRequest(std::bind(&FactoryResetService::handleRequest, this, _1), AuthenticationPredicates::IS_ADMIN)); } -void FactoryResetService::handleRequest(AsyncWebServerRequest* request) { - request->onDisconnect(std::bind(&FactoryResetService::factoryReset, this)); - request->send(200); +void FactoryResetService::handleRequest(AsyncWebServerRequest * request) { + request->onDisconnect(std::bind(&FactoryResetService::factoryReset, this)); + request->send(200); } /** @@ -19,19 +17,25 @@ void FactoryResetService::handleRequest(AsyncWebServerRequest* request) { */ void FactoryResetService::factoryReset() { #ifdef ESP32 - File root = fs->open(FS_CONFIG_DIRECTORY); - File file; - while (file = root.openNextFile()) { - fs->remove(file.name()); - } + /* + * Based on LITTLEFS. Modified by proddy + * Could be replaced with fs.rmdir(FS_CONFIG_DIRECTORY) in IDF 4.2 + */ + File root = fs->open(FS_CONFIG_DIRECTORY); + File file; + while (file = root.openNextFile()) { + char * pathStr = strdup(file.name()); + file.close(); + fs->remove(pathStr); + } #elif defined(ESP8266) - Dir configDirectory = fs->openDir(FS_CONFIG_DIRECTORY); - while (configDirectory.next()) { - String path = FS_CONFIG_DIRECTORY; - path.concat("/"); - path.concat(configDirectory.fileName()); - fs->remove(path); - } + Dir configDirectory = fs->openDir(FS_CONFIG_DIRECTORY); + while (configDirectory.next()) { + String path = FS_CONFIG_DIRECTORY; + path.concat("/"); + path.concat(configDirectory.fileName()); + fs->remove(path); + } #endif - RestartService::restartNow(); + RestartService::restartNow(); } diff --git a/src/system.cpp b/src/system.cpp index 14cd78e1a..baf6db3ad 100644 --- a/src/system.cpp +++ b/src/system.cpp @@ -236,24 +236,15 @@ void System::button_OnLongPress(PButton & b) { // button indefinite press void System::button_OnVLongPress(PButton & b) { LOG_DEBUG(F("Button pressed - very long press")); +#ifndef EMSESP_STANDALONE LOG_WARNING(F("Performing factory reset...")); EMSESP::console_.loop(); -#ifndef EMSESP_STANDALONE - // remove all files under config - File root = LITTLEFS.open(FS_CONFIG_DIRECTORY); - File file; - Serial.printf("Removing files: ", file.name()); - while (file = root.openNextFile()) { - Serial.printf("%s ", file.name()); - LITTLEFS.remove(file.name()); - } - Serial.println(); +#ifdef EMSESP_TEST + Test::listDir(LITTLEFS, FS_CONFIG_DIRECTORY, 3); +#endif - // restart - WiFi.disconnect(true); - delay(500); - ESP.restart(); + EMSESP::esp8266React.factoryReset(); #endif } @@ -265,16 +256,16 @@ void System::button_init(bool refresh) { // Allow 0 for Boot-button on NodeMCU-32s? // if (pbutton_gpio_) { - if (!myPButton_.init(pbutton_gpio_, HIGH)) { - LOG_INFO(F("External multi-functional button not detected")); - } else { - LOG_INFO(F("External multi-functional button enabled")); - } + if (!myPButton_.init(pbutton_gpio_, HIGH)) { + LOG_INFO(F("External multi-functional button not detected")); + } else { + LOG_INFO(F("External multi-functional button enabled")); + } - myPButton_.onClick(BUTTON_Debounce, button_OnClick); - myPButton_.onDblClick(BUTTON_DblClickDelay, button_OnDblClick); - myPButton_.onLongPress(BUTTON_LongPressDelay, button_OnLongPress); - myPButton_.onVLongPress(BUTTON_VLongPressDelay, button_OnVLongPress); + myPButton_.onClick(BUTTON_Debounce, button_OnClick); + myPButton_.onDblClick(BUTTON_DblClickDelay, button_OnDblClick); + myPButton_.onLongPress(BUTTON_LongPressDelay, button_OnLongPress); + myPButton_.onVLongPress(BUTTON_VLongPressDelay, button_OnVLongPress); // } } diff --git a/src/test/test.cpp b/src/test/test.cpp index 083d8001d..4475c2cc6 100644 --- a/src/test/test.cpp +++ b/src/test/test.cpp @@ -978,6 +978,7 @@ void Test::add_device(uint8_t device_id, uint8_t product_id) { #ifndef EMSESP_STANDALONE void Test::listDir(fs::FS & fs, const char * dirname, uint8_t levels) { + Serial.println(); Serial.printf("Listing directory: %s\r\n", dirname); File root = fs.open(dirname); diff --git a/src/test/test.h b/src/test/test.h index f2a7f12b6..a7c2d4f4f 100644 --- a/src/test/test.h +++ b/src/test/test.h @@ -44,8 +44,6 @@ class Test { static void uart_telegram_withCRC(const char * rx_data); static void add_device(uint8_t device_id, uint8_t product_id); static void debug(uuid::console::Shell & shell, const std::string & command); - - private: #ifndef EMSESP_STANDALONE static void listDir(fs::FS & fs, const char * dirname, uint8_t levels); #endif