From eb3ffc19224d395a0f90863cd3cc55565026485f Mon Sep 17 00:00:00 2001 From: Ben Meadors Date: Tue, 25 Feb 2025 07:32:17 -0600 Subject: [PATCH] Updates and fix lame legacy code paths --- protobufs | 2 +- src/mesh/Default.h | 3 ++ src/mesh/MeshService.cpp | 8 ++--- src/mesh/MeshService.h | 2 +- src/mesh/NodeDB.cpp | 68 +++++++++++++++++++++++-------------- src/mesh/NodeDB.h | 14 ++++---- src/modules/AdminModule.cpp | 3 +- 7 files changed, 59 insertions(+), 41 deletions(-) diff --git a/protobufs b/protobufs index 47ff85bb9..79298fcdf 160000 --- a/protobufs +++ b/protobufs @@ -1 +1 @@ -Subproject commit 47ff85bb9c2ab63fd0232f9a0eb6e27896944ef6 +Subproject commit 79298fcdf04c1e680f5abd875233394f44ddaf7a diff --git a/src/mesh/Default.h b/src/mesh/Default.h index d39886d1c..9e6efcb09 100644 --- a/src/mesh/Default.h +++ b/src/mesh/Default.h @@ -7,6 +7,9 @@ #define THIRTY_SECONDS_MS 30 * 1000 #define FIVE_SECONDS_MS 5 * 1000 +// Backup after the first 5 minutes +#define AUTOMATIC_BACKUP_MS 5 * 60 * 1000 + #define min_default_telemetry_interval_secs 30 * 60 #define default_gps_update_interval IF_ROUTER(ONE_DAY, 2 * 60) #define default_telemetry_broadcast_interval_secs IF_ROUTER(ONE_DAY / 2, 60 * 60) diff --git a/src/mesh/MeshService.cpp b/src/mesh/MeshService.cpp index 0ef21d4ca..0b74fdf0c 100644 --- a/src/mesh/MeshService.cpp +++ b/src/mesh/MeshService.cpp @@ -117,17 +117,13 @@ void MeshService::loop() } /// The radioConfig object just changed, call this to force the hw to change to the new settings -bool MeshService::reloadConfig(int saveWhat) +void MeshService::reloadConfig(int saveWhat) { - // If we can successfully set this radio to these settings, save them to disk - // This will also update the region as needed - bool didReset = nodeDB->resetRadioConfig(); // Don't let the phone send us fatally bad settings + 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(saveWhat); - - return didReset; } /// The owner User record just got updated, update our node DB and broadcast the info into the mesh diff --git a/src/mesh/MeshService.h b/src/mesh/MeshService.h index 42f701d5c..e2e430c03 100644 --- a/src/mesh/MeshService.h +++ b/src/mesh/MeshService.h @@ -118,7 +118,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(int saveWhat = SEGMENT_CONFIG | SEGMENT_MODULECONFIG | SEGMENT_DEVICESTATE | SEGMENT_CHANNELS); + void 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(bool shouldSave = true); diff --git a/src/mesh/NodeDB.cpp b/src/mesh/NodeDB.cpp index c38900c9d..4a2468e49 100644 --- a/src/mesh/NodeDB.cpp +++ b/src/mesh/NodeDB.cpp @@ -400,34 +400,17 @@ bool isBroadcast(uint32_t dest) return dest == NODENUM_BROADCAST || dest == NODENUM_BROADCAST_NO_LORA; } -bool NodeDB::resetRadioConfig(bool factory_reset) +void NodeDB::resetRadioConfig() { - bool didFactoryReset = false; - radioGeneration++; - - if (factory_reset) { - didFactoryReset = factoryReset(); - } - if (channelFile.channels_count != MAX_NUM_CHANNELS) { LOG_INFO("Set default channel and radio preferences!"); channels.initDefaults(); } - channels.onConfigChanged(); - // Update the global myRegion initRegion(); - - if (didFactoryReset) { - LOG_INFO("Reboot due to factory reset"); - screen->startAlert("Rebooting..."); - rebootAtMsec = millis() + (5 * 1000); - } - - return didFactoryReset; } bool NodeDB::factoryReset(bool eraseBleBonds) @@ -1032,6 +1015,9 @@ void NodeDB::loadFromDisk() if (devicestate.version < DEVICESTATE_MIN_VER) { LOG_WARN("Devicestate %d is old, discard", devicestate.version); installDefaultDeviceState(); + } else if (state != LoadFileResult::LOAD_SUCCESS) { + // If we have an owner in the backup, restore it + restorePreferences(meshtastic_AdminMessage_BackupLocation_FLASH, SEGMENT_DEVICESTATE); } else { LOG_INFO("Loaded saved devicestate version %d", devicestate.version); } @@ -1040,16 +1026,18 @@ void NodeDB::loadFromDisk() &config); if (state != LoadFileResult::LOAD_SUCCESS) { installDefaultConfig(); // Our in RAM copy might now be corrupt + restorePreferences(meshtastic_AdminMessage_BackupLocation_FLASH, SEGMENT_CONFIG); } else { if (config.version < DEVICESTATE_MIN_VER) { LOG_WARN("config %d is old, discard", config.version); installDefaultConfig(true); + restorePreferences(meshtastic_AdminMessage_BackupLocation_FLASH, SEGMENT_CONFIG); } else { LOG_INFO("Loaded saved config version %d", config.version); } } if (backupSecurity.private_key.size > 0) { - LOG_DEBUG("Restoring backup of security config"); + LOG_DEBUG("Restoring security config"); config.security = backupSecurity; saveToDisk(SEGMENT_CONFIG); } @@ -1109,6 +1097,7 @@ void NodeDB::loadFromDisk() &meshtastic_LocalModuleConfig_msg, &moduleConfig); if (state != LoadFileResult::LOAD_SUCCESS) { installDefaultModuleConfig(); // Our in RAM copy might now be corrupt + restorePreferences(meshtastic_AdminMessage_BackupLocation_FLASH, SEGMENT_MODULECONFIG); } else { if (moduleConfig.version < DEVICESTATE_MIN_VER) { LOG_WARN("moduleConfig %d is old, discard", moduleConfig.version); @@ -1122,6 +1111,7 @@ void NodeDB::loadFromDisk() &channelFile); if (state != LoadFileResult::LOAD_SUCCESS) { installDefaultChannels(); // Our in RAM copy might now be corrupt + restorePreferences(meshtastic_AdminMessage_BackupLocation_FLASH, SEGMENT_CHANNELS); } else { if (channelFile.version < DEVICESTATE_MIN_VER) { LOG_WARN("channelFile %d is old, discard", channelFile.version); @@ -1597,9 +1587,24 @@ UserLicenseStatus NodeDB::getLicenseStatus(uint32_t nodeNum) return info->user.is_licensed ? UserLicenseStatus::Licensed : UserLicenseStatus::NotLicensed; } +// bool NodeDB::shouldAutoPerformBackup() +// { +// // Already backed up +// if (lastBackupAttempt > 0) +// return false; +// // Not enough time has passed +// if (millis() - lastBackupAttempt < AUTOMATIC_BACKUP_MS) { +// return false; +// } + +// // If a LoRa region is set, assume the device is setup +// return config.lora.region != meshtastic_Config_LoRaConfig_RegionCode_UNSET; +// } + bool NodeDB::backupPreferences(meshtastic_AdminMessage_BackupLocation location) { bool success = false; + lastBackupAttempt = millis(); #ifdef FSCom if (location == meshtastic_AdminMessage_BackupLocation_FLASH) { meshtastic_BackupPreferences backup = meshtastic_BackupPreferences_init_zero; @@ -1634,7 +1639,7 @@ bool NodeDB::backupPreferences(meshtastic_AdminMessage_BackupLocation location) return success; } -bool NodeDB::restorePreferences(meshtastic_AdminMessage_BackupLocation location) +bool NodeDB::restorePreferences(meshtastic_AdminMessage_BackupLocation location, int restoreWhat) { bool success = false; #ifdef FSCom @@ -1651,11 +1656,24 @@ bool NodeDB::restorePreferences(meshtastic_AdminMessage_BackupLocation location) success = loadProto(backupFileName, meshtastic_BackupPreferences_size, sizeof(meshtastic_BackupPreferences), &meshtastic_BackupPreferences_msg, &backup); if (success) { - config = backup.config; - moduleConfig = backup.module_config; - channelFile = backup.channels; - owner = backup.owner; - success = saveToDisk(SEGMENT_DEVICESTATE | SEGMENT_CONFIG | SEGMENT_MODULECONFIG | SEGMENT_CHANNELS); + if (restoreWhat & SEGMENT_CONFIG) { + config = backup.config; + LOG_DEBUG("Restored config"); + } + if (restoreWhat & SEGMENT_MODULECONFIG) { + moduleConfig = backup.module_config; + LOG_DEBUG("Restored module config"); + } + if (restoreWhat & SEGMENT_DEVICESTATE) { + devicestate.owner = backup.owner; + LOG_DEBUG("Restored device state"); + } + if (restoreWhat & SEGMENT_CHANNELS) { + channelFile = backup.channels; + LOG_DEBUG("Restored channels"); + } + + success = saveToDisk(restoreWhat); if (!success) { LOG_ERROR("Failed to save restored preferences to flash"); } else { diff --git a/src/mesh/NodeDB.h b/src/mesh/NodeDB.h index 5bf0ba1fc..0e047ccc8 100644 --- a/src/mesh/NodeDB.h +++ b/src/mesh/NodeDB.h @@ -97,12 +97,9 @@ class NodeDB SEGMENT_NODEDATABASE); /** Reinit radio config if needed, because either: - * a) sometimes a buggy android app might send us bogus settings or - * b) the client set factory_reset - * - * @return true if the config was completely reset, in that case, we should send it back to the client + * sometimes a buggy android app might send us bogus settings */ - bool resetRadioConfig(bool factory_reset = false); + void resetRadioConfig(); /// given a subpacket sniffed from the network, update our DB state /// we updateGUI and updateGUIforNode if we think our this change is big enough for a redraw @@ -201,11 +198,14 @@ class NodeDB bool hasValidPosition(const meshtastic_NodeInfoLite *n); + // bool shouldAutoPerformBackup(); bool backupPreferences(meshtastic_AdminMessage_BackupLocation location); - bool restorePreferences(meshtastic_AdminMessage_BackupLocation location); + bool restorePreferences(meshtastic_AdminMessage_BackupLocation location, + int restoreWhat = SEGMENT_CONFIG | SEGMENT_MODULECONFIG | SEGMENT_DEVICESTATE | SEGMENT_CHANNELS); private: - uint32_t lastNodeDbSave = 0; // when we last saved our db to flash + uint32_t lastNodeDbSave = 0; // when we last saved our db to flash + uint32_t lastBackupAttempt = 0; // when we last tried a backup automatically or manually /// Find a node in our DB, create an empty NodeInfoLite if missing meshtastic_NodeInfoLite *getOrCreateMeshNode(NodeNum n); diff --git a/src/modules/AdminModule.cpp b/src/modules/AdminModule.cpp index b3a409877..7a90885f3 100644 --- a/src/modules/AdminModule.cpp +++ b/src/modules/AdminModule.cpp @@ -384,7 +384,8 @@ bool AdminModule::handleReceivedProtobuf(const meshtastic_MeshPacket &mp, meshta } case meshtastic_AdminMessage_restore_preferences_tag: { LOG_INFO("Client requesting to restore preferences"); - if (nodeDB->restorePreferences(r->backup_preferences)) { + if (nodeDB->restorePreferences(r->backup_preferences, + SEGMENT_DEVICESTATE | SEGMENT_CONFIG | SEGMENT_MODULECONFIG | SEGMENT_CHANNELS)) { myReply = allocErrorResponse(meshtastic_Routing_Error_NONE, &mp); disableBluetooth(); } else {