From b5a8efa16b367bd75cb946c545c99c0f9ca9dc71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=B6ttgens?= Date: Wed, 21 Sep 2022 16:47:10 +0200 Subject: [PATCH 1/7] Filesystem fixes for LittleFS --- src/FSCommon.cpp | 16 ++++++++-------- src/FSCommon.h | 1 + src/mesh/NodeDB.cpp | 7 +++---- src/modules/CannedMessageModule.cpp | 2 +- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/FSCommon.cpp b/src/FSCommon.cpp index 5832a2ac..ecda025c 100644 --- a/src/FSCommon.cpp +++ b/src/FSCommon.cpp @@ -9,13 +9,13 @@ bool copyFile(const char* from, const char* to) File f1 = FSCom.open(from, FILE_O_READ); if (!f1){ - DEBUG_MSG("Failed to open file"); + DEBUG_MSG("Failed to open source file %s\n", from); return false; } File f2 = FSCom.open(to, FILE_O_WRITE); if (!f2) { - DEBUG_MSG("Failed to open file"); + DEBUG_MSG("Failed to open destination file %s\n", to); return false; } @@ -56,10 +56,10 @@ void listDir(const char * dirname, uint8_t levels) while(file){ if(file.isDirectory() && !String(file.name()).endsWith(".")) { if(levels){ - listDir(file.name(), levels -1); + listDir(file.path(), levels -1); } } else { - DEBUG_MSG(" %s (%i Bytes)\n", file.name(), file.size()); + DEBUG_MSG(" %s (%i Bytes)\n", file.path(), file.size()); } file.close(); file = root.openNextFile(); @@ -94,16 +94,16 @@ void rmDir(const char * dirname) strcat(dirpath,entry.name()); // append string two to the result. if(entry.isDirectory() && !String(entry.name()).endsWith(".")) { entry.close(); - // DEBUG_MSG("Descend DIR %s\n", dirpath); + DEBUG_MSG("Descend DIR %s\n", dirpath); rmDir(dirpath); } else { entry.close(); - // DEBUG_MSG("Remove FILE %s\n", entry.name()); + DEBUG_MSG("Remove FILE %s\n", entry.name()); FSCom.remove(entry.name()); } } FSCom.rmdir(dirname); - // DEBUG_MSG("Remove DIR %s\n", dirname); + DEBUG_MSG("Remove DIR %s\n", dirname); file.close(); #endif } @@ -117,7 +117,7 @@ void fsInit() assert(0); // FIXME - report failure to phone } - DEBUG_MSG("Filesystem files:\n"); + DEBUG_MSG("Filesystem files (%d/%d total Bytes):\n", FSCom.usedBytes(), FSCom.totalBytes()); listDir("/", 10); #endif } diff --git a/src/FSCommon.h b/src/FSCommon.h index 38ca403b..2daca975 100644 --- a/src/FSCommon.h +++ b/src/FSCommon.h @@ -40,6 +40,7 @@ using namespace Adafruit_LittleFS_Namespace; #endif void fsInit(); +bool copyFile(const char* from, const char* to); bool renameFile(const char* pathFrom, const char* pathTo); void listDir(const char * dirname, uint8_t levels); void rmDir(const char * dirname); diff --git a/src/mesh/NodeDB.cpp b/src/mesh/NodeDB.cpp index 15601749..4e06d263 100644 --- a/src/mesh/NodeDB.cpp +++ b/src/mesh/NodeDB.cpp @@ -431,7 +431,6 @@ bool saveProto(const char *filename, size_t protoSize, size_t objSize, const pb_ } else { okay = true; } - f.close(); // brief window of risk here ;-) @@ -454,7 +453,7 @@ void NodeDB::saveChannelsToDisk() #ifdef FSCom FSCom.mkdir("/prefs"); #endif - saveProto(channelFileName, ChannelFile_size, sizeof(ChannelFile), ChannelFile_fields, &channelFile); + saveProto(channelFileName, ChannelFile_size, sizeof(channelFile), ChannelFile_fields, &channelFile); } } @@ -484,7 +483,7 @@ void NodeDB::saveToDisk() config.has_power = true; config.has_network = true; config.has_bluetooth = true; - saveProto(configFileName, LocalConfig_size, sizeof(LocalConfig), LocalConfig_fields, &config); + saveProto(configFileName, LocalConfig_size, sizeof(config), LocalConfig_fields, &config); moduleConfig.has_canned_message = true; moduleConfig.has_external_notification = true; @@ -493,7 +492,7 @@ void NodeDB::saveToDisk() moduleConfig.has_serial = true; moduleConfig.has_store_forward = true; moduleConfig.has_telemetry = true; - saveProto(moduleConfigFileName, LocalModuleConfig_size, sizeof(LocalModuleConfig), LocalModuleConfig_fields, &moduleConfig); + saveProto(moduleConfigFileName, LocalModuleConfig_size, sizeof(moduleConfig), LocalModuleConfig_fields, &moduleConfig); saveChannelsToDisk(); } else { diff --git a/src/modules/CannedMessageModule.cpp b/src/modules/CannedMessageModule.cpp index 240c2308..41f62062 100644 --- a/src/modules/CannedMessageModule.cpp +++ b/src/modules/CannedMessageModule.cpp @@ -411,7 +411,7 @@ bool CannedMessageModule::saveProtoForModule() FS.mkdir("/prefs"); #endif - okay &= saveProto(cannedMessagesConfigFile, CannedMessageModuleConfig_size, sizeof(CannedMessageModuleConfig), + okay &= saveProto(cannedMessagesConfigFile, CannedMessageModuleConfig_size, sizeof(cannedMessageModuleConfig), CannedMessageModuleConfig_fields, &cannedMessageModuleConfig); return okay; From cf4947d898ce8ba4102dbcd780db91bbaaa1e951 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=B6ttgens?= Date: Wed, 21 Sep 2022 17:05:10 +0200 Subject: [PATCH 2/7] Fix build for non-ESP32 --- src/FSCommon.cpp | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/FSCommon.cpp b/src/FSCommon.cpp index ecda025c..74f6fd4c 100644 --- a/src/FSCommon.cpp +++ b/src/FSCommon.cpp @@ -56,10 +56,18 @@ void listDir(const char * dirname, uint8_t levels) while(file){ if(file.isDirectory() && !String(file.name()).endsWith(".")) { if(levels){ +#ifdef ARCH_ESP32 listDir(file.path(), levels -1); +#else + listDir(file.name(), levels -1); +#endif } } else { +#ifdef ARCH_ESP32 DEBUG_MSG(" %s (%i Bytes)\n", file.path(), file.size()); +#else + DEBUG_MSG(" %s (%i Bytes)\n", file.name(), file.size()); +#endif } file.close(); file = root.openNextFile(); @@ -116,8 +124,11 @@ void fsInit() DEBUG_MSG("ERROR filesystem mount Failed. Formatting...\n"); assert(0); // FIXME - report failure to phone } - +#ifdef ARCH_ESP32 DEBUG_MSG("Filesystem files (%d/%d total Bytes):\n", FSCom.usedBytes(), FSCom.totalBytes()); +#else + DEBUG_MSG("Filesystem files:\n"); +#endif listDir("/", 10); #endif } From af4d11e17b2f9f81c6640ffa3a77b78e3c19de99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=B6ttgens?= Date: Fri, 23 Sep 2022 19:51:08 +0200 Subject: [PATCH 3/7] fix and refactor FSCommon for new ESPIDF --- src/FSCommon.cpp | 84 ++++++++++++++++++++++++++---------------------- src/FSCommon.h | 2 +- 2 files changed, 47 insertions(+), 39 deletions(-) diff --git a/src/FSCommon.cpp b/src/FSCommon.cpp index 74f6fd4c..b85036bf 100644 --- a/src/FSCommon.cpp +++ b/src/FSCommon.cpp @@ -33,17 +33,23 @@ bool copyFile(const char* from, const char* to) bool renameFile(const char* pathFrom, const char* pathTo) { #ifdef FSCom +#ifdef ARCH_ESP32 + // rename was fixed for ESP32 IDF LittleFS in April + return FSCom.rename(pathFrom, pathTo); +#else if (copyFile(pathFrom, pathTo) && FSCom.remove(pathFrom) ) { return true; } else{ return false; } #endif +#endif } -void listDir(const char * dirname, uint8_t levels) +void listDir(const char * dirname, uint8_t levels, boolean del = false) { #ifdef FSCom + char buffer[255]; File root = FSCom.open(dirname, FILE_O_READ); if(!root){ return; @@ -57,62 +63,64 @@ void listDir(const char * dirname, uint8_t levels) if(file.isDirectory() && !String(file.name()).endsWith(".")) { if(levels){ #ifdef ARCH_ESP32 - listDir(file.path(), levels -1); + listDir(file.path(), levels -1, del); + if(del) { + DEBUG_MSG("Removing %s\n", file.path()); + strcpy(buffer, file.path()); + file.close(); + FSCom.rmdir(buffer); + } else { + file.close(); + } #else - listDir(file.name(), levels -1); + listDir(file.name(), levels -1, del); + file.close(); #endif } } else { #ifdef ARCH_ESP32 + if(del) { + DEBUG_MSG("Deleting %s\n", file.path()); + strcpy(buffer, file.path()); + file.close(); + FSCom.remove(buffer); + } else { DEBUG_MSG(" %s (%i Bytes)\n", file.path(), file.size()); + file.close(); + } #else DEBUG_MSG(" %s (%i Bytes)\n", file.name(), file.size()); + file.close(); #endif } - file.close(); file = root.openNextFile(); } - file.close(); +#ifdef ARCH_ESP32 + if(del) { + DEBUG_MSG("Removing %s\n", root.path()); + strcpy(buffer, root.path()); + root.close(); + FSCom.rmdir(buffer); + } else { + root.close(); + } +#else + root.close(); +#endif #endif } void rmDir(const char * dirname) { #ifdef FSCom - File file = FSCom.open(dirname, FILE_O_READ); - if(!file){ - return; - } - if(!file.isDirectory()){ - file.close(); - FSCom.remove(file.name()); - // DEBUG_MSG("Remove FILE %s\n", file.name()); - return; - } +#ifdef ARCH_ESP32 - file.rewindDirectory(); - while (true) { - File entry = file.openNextFile(); - if (!entry) { - break; - } - char dirpath[100]; // array to hold the result. - strcpy(dirpath, dirname); // copy string one into the result. - strcat(dirpath,"/"); // append string two to the result. - strcat(dirpath,entry.name()); // append string two to the result. - if(entry.isDirectory() && !String(entry.name()).endsWith(".")) { - entry.close(); - DEBUG_MSG("Descend DIR %s\n", dirpath); - rmDir(dirpath); - } else { - entry.close(); - DEBUG_MSG("Remove FILE %s\n", entry.name()); - FSCom.remove(entry.name()); - } - } FSCom.rmdir(dirname); - DEBUG_MSG("Remove DIR %s\n", dirname); - file.close(); + listDir(dirname, 10, true); +#else + // nRF52 implementation of LittleFS has a recursive delete function + FSCom.rmdir_r(dirname); +#endif #endif } @@ -125,7 +133,7 @@ void fsInit() assert(0); // FIXME - report failure to phone } #ifdef ARCH_ESP32 - DEBUG_MSG("Filesystem files (%d/%d total Bytes):\n", FSCom.usedBytes(), FSCom.totalBytes()); + DEBUG_MSG("Filesystem files (%d/%d Bytes):\n", FSCom.usedBytes(), FSCom.totalBytes()); #else DEBUG_MSG("Filesystem files:\n"); #endif diff --git a/src/FSCommon.h b/src/FSCommon.h index 2daca975..841fa5ad 100644 --- a/src/FSCommon.h +++ b/src/FSCommon.h @@ -42,5 +42,5 @@ using namespace Adafruit_LittleFS_Namespace; void fsInit(); bool copyFile(const char* from, const char* to); bool renameFile(const char* pathFrom, const char* pathTo); -void listDir(const char * dirname, uint8_t levels); +void listDir(const char * dirname, uint8_t levels, boolean del); void rmDir(const char * dirname); From b4f75ad04202b300eb74a54e813a20aa66a4cb48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=B6ttgens?= Date: Fri, 23 Sep 2022 19:52:07 +0200 Subject: [PATCH 4/7] use exception decoder --- arch/esp32/esp32.ini | 1 + arch/esp32/esp32s3.ini | 1 + 2 files changed, 2 insertions(+) diff --git a/arch/esp32/esp32.ini b/arch/esp32/esp32.ini index 49ff1054..c11daed7 100644 --- a/arch/esp32/esp32.ini +++ b/arch/esp32/esp32.ini @@ -6,6 +6,7 @@ build_src_filter = ${arduino_base.build_src_filter} - - - upload_speed = 921600 debug_init_break = tbreak setup +monitor_filters = esp32_exception_decoder # Remove -DMYNEWT_VAL_BLE_HS_LOG_LVL=LOG_LEVEL_CRITICAL for low level BLE logging. # See library directory for BLE logging possible values: .pio/libdeps/tbeam/NimBLE-Arduino/src/log_common/log_common.h diff --git a/arch/esp32/esp32s3.ini b/arch/esp32/esp32s3.ini index 4e281718..7828ea68 100644 --- a/arch/esp32/esp32s3.ini +++ b/arch/esp32/esp32s3.ini @@ -6,6 +6,7 @@ build_src_filter = upload_speed = 961200 monitor_speed = 115200 debug_init_break = tbreak setup +monitor_filters = esp32_exception_decoder # Remove -DMYNEWT_VAL_BLE_HS_LOG_LVL=LOG_LEVEL_CRITICAL for low level BLE logging. # See library directory for BLE logging possible values: .pio/libdeps/tbeam/NimBLE-Arduino/src/log_common/log_common.h From 4949bda606724906fe91f14bfed55c24dcdc534e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=B6ttgens?= Date: Fri, 23 Sep 2022 19:52:42 +0200 Subject: [PATCH 5/7] Don't delete OEM.proto on factory reset --- src/graphics/Screen.cpp | 2 +- src/modules/CannedMessageModule.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/graphics/Screen.cpp b/src/graphics/Screen.cpp index 23be64e7..14a43545 100644 --- a/src/graphics/Screen.cpp +++ b/src/graphics/Screen.cpp @@ -86,7 +86,7 @@ static char ourId[5]; GeoCoord geoCoord; // OEM Config File -static const char *oemConfigFile = "/prefs/oem.proto"; +static const char *oemConfigFile = "/oem/oem.proto"; OEMStore oemStore; #ifdef SHOW_REDRAWS diff --git a/src/modules/CannedMessageModule.h b/src/modules/CannedMessageModule.h index 33f60dd6..784e40fc 100644 --- a/src/modules/CannedMessageModule.h +++ b/src/modules/CannedMessageModule.h @@ -74,7 +74,7 @@ class CannedMessageModule : int currentMessageIndex = -1; cannedMessageModuleRunState runState = CANNED_MESSAGE_RUN_STATE_INACTIVE; char payload; - int cursor = 0; + unsigned int cursor = 0; String freetext = ""; // Text Buffer for Freetext Editor char messageStore[CANNED_MESSAGE_MODULE_MESSAGES_SIZE+1]; From bc2cddcb11fa606c6bb6fc2302a306d7befb11fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=B6ttgens?= Date: Fri, 23 Sep 2022 20:43:42 +0200 Subject: [PATCH 6/7] Filesystem abstraction would work really well, if it wasn't for crap vendor toolchains. --- src/FSCommon.cpp | 37 +++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/src/FSCommon.cpp b/src/FSCommon.cpp index b85036bf..a659df8e 100644 --- a/src/FSCommon.cpp +++ b/src/FSCommon.cpp @@ -49,7 +49,9 @@ bool renameFile(const char* pathFrom, const char* pathTo) void listDir(const char * dirname, uint8_t levels, boolean del = false) { #ifdef FSCom +#if (defined(ARCH_ESP32) || defined(ARCH_RP2040) || defined(ARCH_PORTDUINO)) char buffer[255]; +#endif File root = FSCom.open(dirname, FILE_O_READ); if(!root){ return; @@ -72,6 +74,16 @@ void listDir(const char * dirname, uint8_t levels, boolean del = false) } else { file.close(); } +#elif (defined(ARCH_RP2040) || defined(ARCH_PORTDUINO)) + listDir(file.name(), levels -1, del); + if(del) { + DEBUG_MSG("Removing %s\n", file.name()); + strcpy(buffer, file.name()); + file.close(); + FSCom.rmdir(buffer); + } else { + file.close(); + } #else listDir(file.name(), levels -1, del); file.close(); @@ -88,6 +100,16 @@ void listDir(const char * dirname, uint8_t levels, boolean del = false) DEBUG_MSG(" %s (%i Bytes)\n", file.path(), file.size()); file.close(); } +#elif (defined(ARCH_RP2040) || defined(ARCH_PORTDUINO)) + if(del) { + DEBUG_MSG("Deleting %s\n", file.name()); + strcpy(buffer, file.name()); + file.close(); + FSCom.remove(buffer); + } else { + DEBUG_MSG(" %s (%i Bytes)\n", file.name(), file.size()); + file.close(); + } #else DEBUG_MSG(" %s (%i Bytes)\n", file.name(), file.size()); file.close(); @@ -104,6 +126,15 @@ void listDir(const char * dirname, uint8_t levels, boolean del = false) } else { root.close(); } +#elif (defined(ARCH_RP2040) || defined(ARCH_PORTDUINO)) + if(del) { + DEBUG_MSG("Removing %s\n", root.name()); + strcpy(buffer, root.name()); + root.close(); + FSCom.rmdir(buffer); + } else { + root.close(); + } #else root.close(); #endif @@ -113,11 +144,9 @@ void listDir(const char * dirname, uint8_t levels, boolean del = false) void rmDir(const char * dirname) { #ifdef FSCom -#ifdef ARCH_ESP32 - - FSCom.rmdir(dirname); +#if (defined(ARCH_ESP32) || defined(ARCH_RP2040) || defined(ARCH_PORTDUINO)) listDir(dirname, 10, true); -#else +#else if defined(ARCH_NRF52) // nRF52 implementation of LittleFS has a recursive delete function FSCom.rmdir_r(dirname); #endif From 664d18cf58f4b35e7b381284a4f3f1622230dcb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=B6ttgens?= Date: Fri, 23 Sep 2022 21:03:53 +0200 Subject: [PATCH 7/7] t'ell that came from? --- src/FSCommon.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/FSCommon.cpp b/src/FSCommon.cpp index a659df8e..a4fa3c3a 100644 --- a/src/FSCommon.cpp +++ b/src/FSCommon.cpp @@ -36,7 +36,7 @@ bool renameFile(const char* pathFrom, const char* pathTo) #ifdef ARCH_ESP32 // rename was fixed for ESP32 IDF LittleFS in April return FSCom.rename(pathFrom, pathTo); -#else +#else if (copyFile(pathFrom, pathTo) && FSCom.remove(pathFrom) ) { return true; } else{ @@ -146,7 +146,7 @@ void rmDir(const char * dirname) #ifdef FSCom #if (defined(ARCH_ESP32) || defined(ARCH_RP2040) || defined(ARCH_PORTDUINO)) listDir(dirname, 10, true); -#else if defined(ARCH_NRF52) +#elif defined(ARCH_NRF52) // nRF52 implementation of LittleFS has a recursive delete function FSCom.rmdir_r(dirname); #endif