From 76adcbb46ba2c8c57158db37a73a6d75bb4511be Mon Sep 17 00:00:00 2001 From: lewisxhe Date: Tue, 7 May 2024 11:57:49 +0800 Subject: [PATCH 1/9] Enhanced t-echo file system integrity check --- src/FSCommon.cpp | 86 ++++++++++++++++++++++++++++++---- variants/t-echo/platformio.ini | 1 + 2 files changed, 79 insertions(+), 8 deletions(-) diff --git a/src/FSCommon.cpp b/src/FSCommon.cpp index d5ca72142..3d2885654 100644 --- a/src/FSCommon.cpp +++ b/src/FSCommon.cpp @@ -205,6 +205,63 @@ void rmDir(const char *dirname) #endif } +bool fsCheck() +{ +#if defined(ARCH_NRF52) + size_t write_size = 0; + size_t read_size = 0; + char buf[32] = {0}; + while (!Serial); + + Adafruit_LittleFS_Namespace::File file(FSCom); + const char *text = "meshtastic fs test"; + size_t text_length = strlen(text); + const char *filename = "/meshtastic.txt"; + + LOG_DEBUG("Try create file .\n"); + if (file.open(filename, FILE_O_WRITE)) { + write_size = file.write(text); + } else { + LOG_DEBUG("Open file failed .\n");; + goto FORMAT_FS; + } + + if (write_size != text_length) { + LOG_DEBUG("Text bytes do not match .\n"); + file.close(); + goto FORMAT_FS; + } + + file.close(); + + if (!file.open(filename, FILE_O_READ)) { + LOG_DEBUG("Open file failed .\n"); + goto FORMAT_FS; + } + + read_size = file.readBytes(buf, text_length); + if (read_size != text_length) { + LOG_DEBUG("Text bytes do not match .\n"); + file.close(); + goto FORMAT_FS; + } + + if (memcmp(buf, text, text_length) != 0) { + LOG_DEBUG("The written bytes do not match the read bytes .\n"); + file.close(); + goto FORMAT_FS; + } + return true; +FORMAT_FS: + LOG_DEBUG("Format FS ....\n"); + FSCom.format(); + FSCom.begin(); + return false; +#else + return true; +#endif +} + void fsInit() { #ifdef FSCom @@ -219,15 +276,28 @@ void fsInit() * nRF52840 has a certain chance of automatic formatting failure. * Try to create a file after initializing the file system. If the creation fails, * it means that the file system is not working properly. Please format it manually again. + * To check the normality of the file system, you need to disable the LFS_NO_ASSERT assertion. + * Otherwise, the assertion will be entered at the moment of reading or opening, and the FS will not be formatted. * */ - Adafruit_LittleFS_Namespace::File file(FSCom); - const char *filename = "/meshtastic.txt"; - if (!file.open(filename, FILE_O_WRITE)) { - LOG_DEBUG("Format ...."); - FSCom.format(); - FSCom.begin(); - } else { - file.close(); + bool ret = false; + uint8_t retry = 3; + + while (retry--) { + ret = fsCheck(); + if (ret) { + LOG_DEBUG("File system check is OK.\n"); + break; + } + delay(10); + } + + // It may not be possible to reach this step. + // Add a loop here to prevent unpredictable situations from happening. + // Can add a screen to display error status later. + if (!ret) { + while (1) { + Serial.println("The file system is damaged and cannot proceed to the next step.\n"); delay(1000); + } } #else LOG_DEBUG("Filesystem files:\n"); diff --git a/variants/t-echo/platformio.ini b/variants/t-echo/platformio.ini index 9ff60be3f..900c7ffd8 100644 --- a/variants/t-echo/platformio.ini +++ b/variants/t-echo/platformio.ini @@ -17,6 +17,7 @@ build_flags = ${nrf52840_base.build_flags} -Ivariants/t-echo -DEINK_LIMIT_RATE_RESPONSIVE_SEC=1 ; Minimum interval between RESPONSIVE updates -DEINK_LIMIT_GHOSTING_PX=2000 ; (Optional) How much image ghosting is tolerated -DEINK_BACKGROUND_USES_FAST ; (Optional) Use FAST refresh for both BACKGROUND and RESPONSIVE, until a limit is reached. + -DLFS_NO_ASSERT ; Disable LFS assertions build_src_filter = ${nrf52_base.build_src_filter} +<../variants/t-echo> lib_deps = ${nrf52840_base.lib_deps} From b63997b36f56e61ad2eefafaac80e66afd65dd18 Mon Sep 17 00:00:00 2001 From: lewisxhe Date: Tue, 7 May 2024 13:42:00 +0800 Subject: [PATCH 2/9] Remove debug wait --- src/FSCommon.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/FSCommon.cpp b/src/FSCommon.cpp index 3d2885654..28dd877ae 100644 --- a/src/FSCommon.cpp +++ b/src/FSCommon.cpp @@ -211,7 +211,6 @@ bool fsCheck() size_t write_size = 0; size_t read_size = 0; char buf[32] = {0}; - while (!Serial); Adafruit_LittleFS_Namespace::File file(FSCom); const char *text = "meshtastic fs test"; From 81ecd6d926f0dfe4a938ca0106975f326ca9f533 Mon Sep 17 00:00:00 2001 From: lewisxhe Date: Tue, 7 May 2024 14:33:16 +0800 Subject: [PATCH 3/9] trunk fmt --- src/FSCommon.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/FSCommon.cpp b/src/FSCommon.cpp index 28dd877ae..8e3b67fa0 100644 --- a/src/FSCommon.cpp +++ b/src/FSCommon.cpp @@ -208,8 +208,8 @@ void rmDir(const char *dirname) bool fsCheck() { #if defined(ARCH_NRF52) - size_t write_size = 0; - size_t read_size = 0; + size_t write_size = 0; + size_t read_size = 0; char buf[32] = {0}; Adafruit_LittleFS_Namespace::File file(FSCom); @@ -221,7 +221,7 @@ bool fsCheck() if (file.open(filename, FILE_O_WRITE)) { write_size = file.write(text); } else { - LOG_DEBUG("Open file failed .\n");; + LOG_DEBUG("Open file failed .\n"); goto FORMAT_FS; } @@ -295,7 +295,8 @@ void fsInit() // Can add a screen to display error status later. if (!ret) { while (1) { - Serial.println("The file system is damaged and cannot proceed to the next step.\n"); delay(1000); + Serial.println("The file system is damaged and cannot proceed to the next step.\n"); + delay(1000); } } #else From 8886d2df551fd66dfe7886114f82000353ed1e47 Mon Sep 17 00:00:00 2001 From: lewisxhe Date: Tue, 7 May 2024 11:57:49 +0800 Subject: [PATCH 4/9] Enhanced t-echo file system integrity check --- src/FSCommon.cpp | 86 ++++++++++++++++++++++++++++++---- variants/t-echo/platformio.ini | 1 + 2 files changed, 79 insertions(+), 8 deletions(-) diff --git a/src/FSCommon.cpp b/src/FSCommon.cpp index d5ca72142..3d2885654 100644 --- a/src/FSCommon.cpp +++ b/src/FSCommon.cpp @@ -205,6 +205,63 @@ void rmDir(const char *dirname) #endif } +bool fsCheck() +{ +#if defined(ARCH_NRF52) + size_t write_size = 0; + size_t read_size = 0; + char buf[32] = {0}; + while (!Serial); + + Adafruit_LittleFS_Namespace::File file(FSCom); + const char *text = "meshtastic fs test"; + size_t text_length = strlen(text); + const char *filename = "/meshtastic.txt"; + + LOG_DEBUG("Try create file .\n"); + if (file.open(filename, FILE_O_WRITE)) { + write_size = file.write(text); + } else { + LOG_DEBUG("Open file failed .\n");; + goto FORMAT_FS; + } + + if (write_size != text_length) { + LOG_DEBUG("Text bytes do not match .\n"); + file.close(); + goto FORMAT_FS; + } + + file.close(); + + if (!file.open(filename, FILE_O_READ)) { + LOG_DEBUG("Open file failed .\n"); + goto FORMAT_FS; + } + + read_size = file.readBytes(buf, text_length); + if (read_size != text_length) { + LOG_DEBUG("Text bytes do not match .\n"); + file.close(); + goto FORMAT_FS; + } + + if (memcmp(buf, text, text_length) != 0) { + LOG_DEBUG("The written bytes do not match the read bytes .\n"); + file.close(); + goto FORMAT_FS; + } + return true; +FORMAT_FS: + LOG_DEBUG("Format FS ....\n"); + FSCom.format(); + FSCom.begin(); + return false; +#else + return true; +#endif +} + void fsInit() { #ifdef FSCom @@ -219,15 +276,28 @@ void fsInit() * nRF52840 has a certain chance of automatic formatting failure. * Try to create a file after initializing the file system. If the creation fails, * it means that the file system is not working properly. Please format it manually again. + * To check the normality of the file system, you need to disable the LFS_NO_ASSERT assertion. + * Otherwise, the assertion will be entered at the moment of reading or opening, and the FS will not be formatted. * */ - Adafruit_LittleFS_Namespace::File file(FSCom); - const char *filename = "/meshtastic.txt"; - if (!file.open(filename, FILE_O_WRITE)) { - LOG_DEBUG("Format ...."); - FSCom.format(); - FSCom.begin(); - } else { - file.close(); + bool ret = false; + uint8_t retry = 3; + + while (retry--) { + ret = fsCheck(); + if (ret) { + LOG_DEBUG("File system check is OK.\n"); + break; + } + delay(10); + } + + // It may not be possible to reach this step. + // Add a loop here to prevent unpredictable situations from happening. + // Can add a screen to display error status later. + if (!ret) { + while (1) { + Serial.println("The file system is damaged and cannot proceed to the next step.\n"); delay(1000); + } } #else LOG_DEBUG("Filesystem files:\n"); diff --git a/variants/t-echo/platformio.ini b/variants/t-echo/platformio.ini index 9ff60be3f..900c7ffd8 100644 --- a/variants/t-echo/platformio.ini +++ b/variants/t-echo/platformio.ini @@ -17,6 +17,7 @@ build_flags = ${nrf52840_base.build_flags} -Ivariants/t-echo -DEINK_LIMIT_RATE_RESPONSIVE_SEC=1 ; Minimum interval between RESPONSIVE updates -DEINK_LIMIT_GHOSTING_PX=2000 ; (Optional) How much image ghosting is tolerated -DEINK_BACKGROUND_USES_FAST ; (Optional) Use FAST refresh for both BACKGROUND and RESPONSIVE, until a limit is reached. + -DLFS_NO_ASSERT ; Disable LFS assertions build_src_filter = ${nrf52_base.build_src_filter} +<../variants/t-echo> lib_deps = ${nrf52840_base.lib_deps} From 8e7ede16efe2c20ce3ab31dc358944b35468ef8c Mon Sep 17 00:00:00 2001 From: lewisxhe Date: Tue, 7 May 2024 13:42:00 +0800 Subject: [PATCH 5/9] Remove debug wait --- src/FSCommon.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/FSCommon.cpp b/src/FSCommon.cpp index 3d2885654..28dd877ae 100644 --- a/src/FSCommon.cpp +++ b/src/FSCommon.cpp @@ -211,7 +211,6 @@ bool fsCheck() size_t write_size = 0; size_t read_size = 0; char buf[32] = {0}; - while (!Serial); Adafruit_LittleFS_Namespace::File file(FSCom); const char *text = "meshtastic fs test"; From 1d583341e49ba353bf2339c67a21c677dbf3e85c Mon Sep 17 00:00:00 2001 From: lewisxhe Date: Tue, 7 May 2024 14:33:16 +0800 Subject: [PATCH 6/9] trunk fmt --- src/FSCommon.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/FSCommon.cpp b/src/FSCommon.cpp index 28dd877ae..8e3b67fa0 100644 --- a/src/FSCommon.cpp +++ b/src/FSCommon.cpp @@ -208,8 +208,8 @@ void rmDir(const char *dirname) bool fsCheck() { #if defined(ARCH_NRF52) - size_t write_size = 0; - size_t read_size = 0; + size_t write_size = 0; + size_t read_size = 0; char buf[32] = {0}; Adafruit_LittleFS_Namespace::File file(FSCom); @@ -221,7 +221,7 @@ bool fsCheck() if (file.open(filename, FILE_O_WRITE)) { write_size = file.write(text); } else { - LOG_DEBUG("Open file failed .\n");; + LOG_DEBUG("Open file failed .\n"); goto FORMAT_FS; } @@ -295,7 +295,8 @@ void fsInit() // Can add a screen to display error status later. if (!ret) { while (1) { - Serial.println("The file system is damaged and cannot proceed to the next step.\n"); delay(1000); + Serial.println("The file system is damaged and cannot proceed to the next step.\n"); + delay(1000); } } #else From 73ab43c67a2a5aafbdf49eae951ffaf500a74c38 Mon Sep 17 00:00:00 2001 From: lewisxhe Date: Wed, 8 May 2024 08:45:24 +0800 Subject: [PATCH 7/9] Change to LOG_ERROR --- src/FSCommon.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/FSCommon.cpp b/src/FSCommon.cpp index 8e3b67fa0..96aad1a9a 100644 --- a/src/FSCommon.cpp +++ b/src/FSCommon.cpp @@ -295,7 +295,7 @@ void fsInit() // Can add a screen to display error status later. if (!ret) { while (1) { - Serial.println("The file system is damaged and cannot proceed to the next step.\n"); + LOG_ERROR("The file system is damaged and cannot proceed to the next step.\n"); delay(1000); } } From 8c3b9a61395d15f4b88101760f0eb94989e21b14 Mon Sep 17 00:00:00 2001 From: lewisxhe Date: Wed, 8 May 2024 08:46:08 +0800 Subject: [PATCH 8/9] Move LFS_NO_ASSERT to nrf52.ini --- arch/nrf52/nrf52.ini | 1 + variants/t-echo/platformio.ini | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/nrf52/nrf52.ini b/arch/nrf52/nrf52.ini index 0669a31e8..314c73361 100644 --- a/arch/nrf52/nrf52.ini +++ b/arch/nrf52/nrf52.ini @@ -9,6 +9,7 @@ build_flags = -DSERIAL_BUFFER_SIZE=1024 -Wno-unused-variable -Isrc/platform/nrf52 + -DLFS_NO_ASSERT ; Disable LFS assertions , see https://github.com/meshtastic/firmware/pull/3818 build_src_filter = ${arduino_base.build_src_filter} - - - - - - - - - - diff --git a/variants/t-echo/platformio.ini b/variants/t-echo/platformio.ini index 900c7ffd8..9ff60be3f 100644 --- a/variants/t-echo/platformio.ini +++ b/variants/t-echo/platformio.ini @@ -17,7 +17,6 @@ build_flags = ${nrf52840_base.build_flags} -Ivariants/t-echo -DEINK_LIMIT_RATE_RESPONSIVE_SEC=1 ; Minimum interval between RESPONSIVE updates -DEINK_LIMIT_GHOSTING_PX=2000 ; (Optional) How much image ghosting is tolerated -DEINK_BACKGROUND_USES_FAST ; (Optional) Use FAST refresh for both BACKGROUND and RESPONSIVE, until a limit is reached. - -DLFS_NO_ASSERT ; Disable LFS assertions build_src_filter = ${nrf52_base.build_src_filter} +<../variants/t-echo> lib_deps = ${nrf52840_base.lib_deps} From 6c75f2e627a6e67e3dc059797dd99af63a182c92 Mon Sep 17 00:00:00 2001 From: lewisxhe Date: Wed, 8 May 2024 08:51:24 +0800 Subject: [PATCH 9/9] Move LFS_NO_ASSERT to nrf52.ini --- variants/t-echo/platformio.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/variants/t-echo/platformio.ini b/variants/t-echo/platformio.ini index 900c7ffd8..1ba0afe25 100644 --- a/variants/t-echo/platformio.ini +++ b/variants/t-echo/platformio.ini @@ -17,7 +17,7 @@ build_flags = ${nrf52840_base.build_flags} -Ivariants/t-echo -DEINK_LIMIT_RATE_RESPONSIVE_SEC=1 ; Minimum interval between RESPONSIVE updates -DEINK_LIMIT_GHOSTING_PX=2000 ; (Optional) How much image ghosting is tolerated -DEINK_BACKGROUND_USES_FAST ; (Optional) Use FAST refresh for both BACKGROUND and RESPONSIVE, until a limit is reached. - -DLFS_NO_ASSERT ; Disable LFS assertions + build_src_filter = ${nrf52_base.build_src_filter} +<../variants/t-echo> lib_deps = ${nrf52840_base.lib_deps}