From d18aa2e7cbcf39f0e302928c55aaae4d9330956c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=B6ttgens?= Date: Wed, 15 Jun 2022 16:52:04 +0200 Subject: [PATCH 1/3] add file version to local savefiles --- src/mesh/NodeDB.cpp | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/src/mesh/NodeDB.cpp b/src/mesh/NodeDB.cpp index a9ee9df1e..d4d5218ec 100644 --- a/src/mesh/NodeDB.cpp +++ b/src/mesh/NodeDB.cpp @@ -48,7 +48,7 @@ DeviceState versions used to be defined in the .proto file but really only this #define here. */ -#define DEVICESTATE_CUR_VER 11 +#define DEVICESTATE_CUR_VER 13 #define DEVICESTATE_MIN_VER DEVICESTATE_CUR_VER // FIXME - move this somewhere else @@ -146,6 +146,7 @@ bool NodeDB::resetRadioConfig() void NodeDB::installDefaultConfig() { memset(&config, 0, sizeof(LocalConfig)); + config.version = DEVICESTATE_CUR_VER; config.has_device = true; config.has_display = true; config.has_lora = true; @@ -165,6 +166,7 @@ void NodeDB::installDefaultConfig() void NodeDB::installDefaultModuleConfig() { memset(&moduleConfig, 0, sizeof(ModuleConfig)); + moduleConfig.version = DEVICESTATE_CUR_VER; moduleConfig.has_canned_message = true; moduleConfig.has_external_notification = true; moduleConfig.has_mqtt = true; @@ -189,6 +191,7 @@ void NodeDB::installDefaultModuleConfig() void NodeDB::installDefaultChannels() { memset(&channelFile, 0, sizeof(ChannelFile)); + channelFile.version = DEVICESTATE_CUR_VER; } void NodeDB::installDefaultDeviceState() @@ -347,20 +350,41 @@ void NodeDB::loadFromDisk() DEBUG_MSG("Warn: devicestate %d is old, discarding\n", devicestate.version); installDefaultDeviceState(); } else { - DEBUG_MSG("Loaded saved preferences version %d\n", devicestate.version); + DEBUG_MSG("Loaded saved devicestate version %d\n", devicestate.version); } } if (!loadProto(configfile, LocalConfig_size, sizeof(LocalConfig), LocalConfig_fields, &config)) { installDefaultConfig(); // Our in RAM copy might now be corrupt + } else { + if (config.version < DEVICESTATE_MIN_VER) { + DEBUG_MSG("Warn: config %d is old, discarding\n", config.version); + installDefaultConfig(); + } else { + DEBUG_MSG("Loaded saved config version %d\n", config.version); + } } if (!loadProto(moduleConfigfile, LocalModuleConfig_size, sizeof(LocalModuleConfig), LocalModuleConfig_fields, &moduleConfig)) { installDefaultModuleConfig(); // Our in RAM copy might now be corrupt + } else { + if (moduleConfig.version < DEVICESTATE_MIN_VER) { + DEBUG_MSG("Warn: moduleConfig %d is old, discarding\n", moduleConfig.version); + installDefaultModuleConfig(); + } else { + DEBUG_MSG("Loaded saved moduleConfig version %d\n", moduleConfig.version); + } } if (!loadProto(channelfile, ChannelFile_size, sizeof(ChannelFile), ChannelFile_fields, &channelFile)) { installDefaultChannels(); // Our in RAM copy might now be corrupt + } else { + if (channelFile.version < DEVICESTATE_MIN_VER) { + DEBUG_MSG("Warn: channelFile %d is old, discarding\n", channelFile.version); + installDefaultChannels(); + } else { + DEBUG_MSG("Loaded saved channelFile version %d\n", channelFile.version); + } } } From b1274799610ddf81e036ed11392d0158839b12b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=B6ttgens?= Date: Wed, 15 Jun 2022 17:09:42 +0200 Subject: [PATCH 2/3] - Refactored factory reset a bit to not installDefaultDeviceState twice on ESP32 - clear BLE bonds on settings version increase --- src/mesh/NodeDB.cpp | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/src/mesh/NodeDB.cpp b/src/mesh/NodeDB.cpp index d4d5218ec..c4ab8cad0 100644 --- a/src/mesh/NodeDB.cpp +++ b/src/mesh/NodeDB.cpp @@ -90,18 +90,17 @@ bool NodeDB::resetRadioConfig() // radioConfig.has_preferences = true; if (config.device.factory_reset) { DEBUG_MSG("Performing factory reset!\n"); - installDefaultDeviceState(); -#ifndef NO_ESP32 - // This will erase what's in NVS including ssl keys, persistant variables and ble pairing - nvs_flash_erase(); -#endif -#ifdef NRF52_SERIES // first, remove the "/prefs" (this removes most prefs) FSCom.rmdir_r("/prefs"); // second, install default state (this will deal with the duplicate mac address issue) installDefaultDeviceState(); // third, write to disk saveToDisk(); +#ifndef NO_ESP32 + // This will erase what's in NVS including ssl keys, persistant variables and ble pairing + nvs_flash_erase(); +#endif +#ifdef NRF52_SERIES Bluefruit.begin(); DEBUG_MSG("Clearing bluetooth bonds!\n"); bond_print_list(BLE_GAP_ROLE_PERIPH); @@ -176,18 +175,6 @@ void NodeDB::installDefaultModuleConfig() moduleConfig.has_telemetry = true; } -// void NodeDB::installDefaultRadioConfig() -// { -// memset(&radioConfig, 0, sizeof(radioConfig)); -// radioConfig.has_preferences = true; -// resetRadioConfig(); - -// // for backward compat, default position flags are BAT+ALT+MSL (0x23 = 35) -// config.position.position_flags = -// (Config_PositionConfig_PositionFlags_POS_BATTERY | Config_PositionConfig_PositionFlags_POS_ALTITUDE | -// Config_PositionConfig_PositionFlags_POS_ALT_MSL); -// } - void NodeDB::installDefaultChannels() { memset(&channelFile, 0, sizeof(ChannelFile)); @@ -349,6 +336,18 @@ void NodeDB::loadFromDisk() if (devicestate.version < DEVICESTATE_MIN_VER) { DEBUG_MSG("Warn: devicestate %d is old, discarding\n", devicestate.version); installDefaultDeviceState(); +#ifndef NO_ESP32 + // This will erase what's in NVS including ssl keys, persistant variables and ble pairing + nvs_flash_erase(); +#endif +#ifdef NRF52_SERIES + Bluefruit.begin(); + DEBUG_MSG("Clearing bluetooth bonds!\n"); + bond_print_list(BLE_GAP_ROLE_PERIPH); + bond_print_list(BLE_GAP_ROLE_CENTRAL); + Bluefruit.Periph.clearBonds(); + Bluefruit.Central.clearBonds(); +#endif } else { DEBUG_MSG("Loaded saved devicestate version %d\n", devicestate.version); } From 125f76d9845f736099d08727171cbb3a4a2fa409 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=B6ttgens?= Date: Wed, 15 Jun 2022 17:52:37 +0200 Subject: [PATCH 3/3] Don't use rmdir_r but roll our own version. --- src/FSCommon.cpp | 28 ++++++++++++++++++++++++++++ src/FSCommon.h | 4 +++- src/mesh/NodeDB.cpp | 2 +- 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/FSCommon.cpp b/src/FSCommon.cpp index 3a2f8a92e..5293c4015 100644 --- a/src/FSCommon.cpp +++ b/src/FSCommon.cpp @@ -28,6 +28,34 @@ void listDir(const char * dirname, uint8_t levels) #endif } +void rmDir(const char * dirname) +#ifdef FSCom +{ + File root = FSCom.open(dirname); + if(!root){ + return; + } + if(!root.isDirectory()){ + return; + } + + File file = root.openNextFile(); + while(file){ + if(file.isDirectory() && !String(file.name()).endsWith(".")) { + file.close(); + rmDir(file.name()); + FSCom.rmdir(file.name()); + } else { + file.close(); + FSCom.remove(file.name()); + } + file.close(); + file = root.openNextFile(); + } + file.close(); +#endif +} + void fsInit() { #ifdef FSCom diff --git a/src/FSCommon.h b/src/FSCommon.h index be515b946..26d4f9f88 100644 --- a/src/FSCommon.h +++ b/src/FSCommon.h @@ -26,4 +26,6 @@ using namespace Adafruit_LittleFS_Namespace; #endif -void fsInit(); \ No newline at end of file +void fsInit(); +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 c4ab8cad0..f70ffa1dd 100644 --- a/src/mesh/NodeDB.cpp +++ b/src/mesh/NodeDB.cpp @@ -91,7 +91,7 @@ bool NodeDB::resetRadioConfig() if (config.device.factory_reset) { DEBUG_MSG("Performing factory reset!\n"); // first, remove the "/prefs" (this removes most prefs) - FSCom.rmdir_r("/prefs"); + rmDir("/prefs"); // second, install default state (this will deal with the duplicate mac address issue) installDefaultDeviceState(); // third, write to disk