From 3597685b2363323360d3b62b7b9cc1bca579f0ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=B6ttgens?= Date: Tue, 4 Oct 2022 14:32:07 +0200 Subject: [PATCH] Only save the changed parts of config to disk. --- src/main.cpp | 2 +- src/mesh/MeshService.cpp | 6 ++--- src/mesh/MeshService.h | 2 +- src/mesh/NodeDB.cpp | 49 +++++++++++++++++++--------------- src/mesh/NodeDB.h | 7 ++++- src/modules/AdminModule.cpp | 4 +-- src/nimble/NimbleBluetooth.cpp | 2 +- 7 files changed, 42 insertions(+), 30 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 3430cb129..d6d48d5d1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -324,7 +324,7 @@ void setup() DEBUG_MSG("GPS FactoryReset requested\n"); if (gps->factoryReset()) { // If we don't succeed try again next time devicestate.did_gps_reset = true; - nodeDB.saveToDisk(); + nodeDB.saveToDisk(SEGMENT_DEVICESTATE); } } diff --git a/src/mesh/MeshService.cpp b/src/mesh/MeshService.cpp index 99411f91d..31d26b625 100644 --- a/src/mesh/MeshService.cpp +++ b/src/mesh/MeshService.cpp @@ -90,7 +90,7 @@ void MeshService::loop() } /// The radioConfig object just changed, call this to force the hw to change to the new settings -bool MeshService::reloadConfig() +bool MeshService::reloadConfig(int saveWhat) { // If we can successfully set this radio to these settings, save them to disk @@ -98,7 +98,7 @@ bool MeshService::reloadConfig() bool didReset = nodeDB.resetRadioConfig(); // Don't let the phone send us fatally bad settings configChanged.notifyObservers(NULL); // This will cause radio hardware to change freqs etc - nodeDB.saveToDisk(); + nodeDB.saveToDisk(saveWhat); return didReset; } @@ -113,7 +113,7 @@ void MeshService::reloadOwner() // update everyone else if (nodeInfoModule) nodeInfoModule->sendOurNodeInfo(); - nodeDB.saveToDisk(); + nodeDB.saveToDisk(SEGMENT_DEVICESTATE); } /** diff --git a/src/mesh/MeshService.h b/src/mesh/MeshService.h index d886a57a9..ffa8c06ec 100644 --- a/src/mesh/MeshService.h +++ b/src/mesh/MeshService.h @@ -66,7 +66,7 @@ class MeshService /** The radioConfig object just changed, call this to force the hw to change to the new settings * @return true if client devices should be sent a new set of radio configs */ - bool reloadConfig(); + bool reloadConfig(int saveWhat=SEGMENT_CONFIG | SEGMENT_MODULECONFIG | SEGMENT_DEVICESTATE | SEGMENT_CHANNELS); /// The owner User record just got updated, update our node DB and broadcast the info into the mesh void reloadOwner(); diff --git a/src/mesh/NodeDB.cpp b/src/mesh/NodeDB.cpp index 22ea77e93..551bfdd2a 100644 --- a/src/mesh/NodeDB.cpp +++ b/src/mesh/NodeDB.cpp @@ -128,7 +128,7 @@ bool NodeDB::factoryReset() // second, install default state (this will deal with the duplicate mac address issue) installDefaultDeviceState(); installDefaultConfig(); - // third, write to disk + // third, write everything to disk saveToDisk(); #ifdef ARCH_ESP32 // This will erase what's in NVS including ssl keys, persistant variables and ble pairing @@ -474,34 +474,41 @@ void NodeDB::saveDeviceStateToDisk() } } -void NodeDB::saveToDisk() +void NodeDB::saveToDisk(int saveWhat) { if (!devicestate.no_save) { #ifdef FSCom FSCom.mkdir("/prefs"); #endif - saveProto(prefFileName, DeviceState_size, sizeof(devicestate), DeviceState_fields, &devicestate); + if (saveWhat && SEGMENT_DEVICESTATE) { + saveDeviceStateToDisk(); + } - // save all config segments - config.has_device = true; - config.has_display = true; - config.has_lora = true; - config.has_position = true; - config.has_power = true; - config.has_network = true; - config.has_bluetooth = true; - saveProto(configFileName, LocalConfig_size, sizeof(config), LocalConfig_fields, &config); + if (saveWhat && SEGMENT_CONFIG) { + config.has_device = true; + config.has_display = true; + config.has_lora = true; + config.has_position = true; + config.has_power = true; + config.has_network = true; + config.has_bluetooth = true; + saveProto(configFileName, LocalConfig_size, sizeof(config), LocalConfig_fields, &config); + } - moduleConfig.has_canned_message = true; - moduleConfig.has_external_notification = true; - moduleConfig.has_mqtt = true; - moduleConfig.has_range_test = true; - moduleConfig.has_serial = true; - moduleConfig.has_store_forward = true; - moduleConfig.has_telemetry = true; - saveProto(moduleConfigFileName, LocalModuleConfig_size, sizeof(moduleConfig), LocalModuleConfig_fields, &moduleConfig); + if (saveWhat && SEGMENT_MODULECONFIG) { + moduleConfig.has_canned_message = true; + moduleConfig.has_external_notification = true; + moduleConfig.has_mqtt = true; + moduleConfig.has_range_test = true; + moduleConfig.has_serial = true; + moduleConfig.has_store_forward = true; + moduleConfig.has_telemetry = true; + saveProto(moduleConfigFileName, LocalModuleConfig_size, sizeof(moduleConfig), LocalModuleConfig_fields, &moduleConfig); + } - saveChannelsToDisk(); + if (saveWhat && SEGMENT_CHANNELS) { + saveChannelsToDisk(); + } } else { DEBUG_MSG("***** DEVELOPMENT MODE - DO NOT RELEASE - not saving to flash *****\n"); } diff --git a/src/mesh/NodeDB.h b/src/mesh/NodeDB.h index 47145fc34..574d6fe54 100644 --- a/src/mesh/NodeDB.h +++ b/src/mesh/NodeDB.h @@ -13,6 +13,11 @@ DeviceState versions used to be defined in the .proto file but really only this #define here. */ +#define SEGMENT_CONFIG 1 +#define SEGMENT_MODULECONFIG 2 +#define SEGMENT_DEVICESTATE 4 +#define SEGMENT_CHANNELS 8 + #define DEVICESTATE_CUR_VER 19 #define DEVICESTATE_MIN_VER DEVICESTATE_CUR_VER @@ -52,7 +57,7 @@ class NodeDB void init(); /// write to flash - void saveToDisk(), saveChannelsToDisk(), saveDeviceStateToDisk(); + void saveToDisk(int saveWhat=SEGMENT_CONFIG | SEGMENT_MODULECONFIG | SEGMENT_DEVICESTATE | SEGMENT_CHANNELS), saveChannelsToDisk(), saveDeviceStateToDisk(); /** Reinit radio config if needed, because either: * a) sometimes a buggy android app might send us bogus settings or diff --git a/src/modules/AdminModule.cpp b/src/modules/AdminModule.cpp index 4dd9d57b5..b2ed44a05 100644 --- a/src/modules/AdminModule.cpp +++ b/src/modules/AdminModule.cpp @@ -241,7 +241,7 @@ void AdminModule::handleSetConfig(const Config &c) break; } - service.reloadConfig(); + service.reloadConfig(SEGMENT_CONFIG); // Reboot 5 seconds after a config that requires rebooting is set if (requiresReboot) { DEBUG_MSG("Rebooting due to config changes\n"); @@ -290,7 +290,7 @@ void AdminModule::handleSetModuleConfig(const ModuleConfig &c) break; } - service.reloadConfig(); + service.reloadConfig(SEGMENT_MODULECONFIG); } void AdminModule::handleSetChannel(const Channel &cc) diff --git a/src/nimble/NimbleBluetooth.cpp b/src/nimble/NimbleBluetooth.cpp index 4bc3fe44a..b2a4e3970 100644 --- a/src/nimble/NimbleBluetooth.cpp +++ b/src/nimble/NimbleBluetooth.cpp @@ -75,7 +75,7 @@ class NimbleBluetoothServerCallback : public NimBLEServerCallbacks if (doublepressed > 0 && (doublepressed + (30 * 1000)) > millis()) { DEBUG_MSG("User has set BLE pairing mode to fixed-pin\n"); config.bluetooth.mode = Config_BluetoothConfig_PairingMode_FIXED_PIN; - nodeDB.saveToDisk(); + nodeDB.saveToDisk(SEGMENT_CONFIG); } else if (config.bluetooth.mode == Config_BluetoothConfig_PairingMode_RANDOM_PIN) { DEBUG_MSG("Using random passkey\n"); // This is the passkey to be entered on peer - we pick a number >100,000 to ensure 6 digits