From 3f0ff452324f9e5b94f857003c89c23ba9cc6744 Mon Sep 17 00:00:00 2001 From: Ben Meadors Date: Sat, 2 Jul 2022 09:09:41 -0500 Subject: [PATCH] Node db cleanup and debug prints (#1543) * Node db cleanup and debug prints * File name cleanup --- src/mesh/NodeDB.cpp | 86 +++++++++++++++++++------------------ src/mesh/NodeDB.h | 2 + src/mesh/RadioInterface.cpp | 5 +-- 3 files changed, 49 insertions(+), 44 deletions(-) diff --git a/src/mesh/NodeDB.cpp b/src/mesh/NodeDB.cpp index a6e61734..f790f84b 100644 --- a/src/mesh/NodeDB.cpp +++ b/src/mesh/NodeDB.cpp @@ -89,26 +89,7 @@ bool NodeDB::resetRadioConfig() // radioConfig.has_preferences = true; if (config.device.factory_reset) { - DEBUG_MSG("Performing factory reset!\n"); - // first, remove the "/prefs" (this removes most prefs) - rmDir("/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); - bond_print_list(BLE_GAP_ROLE_CENTRAL); - Bluefruit.Periph.clearBonds(); - Bluefruit.Central.clearBonds(); -#endif - didFactoryReset = true; + didFactoryReset = factoryReset(); } if (channelFile.channels_count != MAX_NUM_CHANNELS) { @@ -142,8 +123,33 @@ bool NodeDB::resetRadioConfig() return didFactoryReset; } +bool NodeDB::factoryReset() +{ + DEBUG_MSG("Performing factory reset!\n"); + // first, remove the "/prefs" (this removes most prefs) + rmDir("/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); + bond_print_list(BLE_GAP_ROLE_CENTRAL); + Bluefruit.Periph.clearBonds(); + Bluefruit.Central.clearBonds(); +#endif + return true; +} + void NodeDB::installDefaultConfig() { + DEBUG_MSG("Installing default LocalConfig\n"); memset(&config, 0, sizeof(LocalConfig)); config.version = DEVICESTATE_CUR_VER; config.has_device = true; @@ -164,25 +170,28 @@ void NodeDB::installDefaultConfig() void NodeDB::installDefaultModuleConfig() { + DEBUG_MSG("Installing default ModuleConfig\n"); memset(&moduleConfig, 0, sizeof(ModuleConfig)); moduleConfig.version = DEVICESTATE_CUR_VER; - 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; + moduleConfig.has_external_notification = true; + moduleConfig.has_canned_message = true; } void NodeDB::installDefaultChannels() { + DEBUG_MSG("Installing default ChannelFile\n"); memset(&channelFile, 0, sizeof(ChannelFile)); channelFile.version = DEVICESTATE_CUR_VER; } void NodeDB::installDefaultDeviceState() { + DEBUG_MSG("Installing default DeviceState\n"); memset(&devicestate, 0, sizeof(DeviceState)); *numNodes = 0; // Forget node DB @@ -209,23 +218,18 @@ void NodeDB::installDefaultDeviceState() sprintf(owner.id, "!%08x", getNodeNum()); // Default node ID now based on nodenum memcpy(owner.macaddr, ourMacAddr, sizeof(owner.macaddr)); - - installDefaultChannels(); - installDefaultConfig(); } void NodeDB::init() { - installDefaultDeviceState(); - + DEBUG_MSG("Initializing NodeDB\n"); // saveToDisk(); loadFromDisk(); // saveToDisk(); myNodeInfo.max_channels = MAX_NUM_CHANNELS; // tell others the max # of channels we can understand - myNodeInfo.error_code = - CriticalErrorCode_None; // For the error code, only show values from this boot (discard value from flash) + myNodeInfo.error_code = CriticalErrorCode_None; // For the error code, only show values from this boot (discard value from flash) myNodeInfo.error_address = 0; // likewise - we always want the app requirements to come from the running appload @@ -290,10 +294,10 @@ void NodeDB::pickNewNodeNum() myNodeInfo.my_node_num = r; } -static const char *preffile = "/prefs/db.proto"; -static const char *configfile = "/prefs/config.proto"; -static const char *moduleConfigfile = "/prefs/module.proto"; -static const char *channelfile = "/prefs/channels.proto"; +static const char *prefFileName = "/prefs/db.proto"; +static const char *configFileName = "/prefs/config.proto"; +static const char *moduleConfigFileName = "/prefs/module.proto"; +static const char *channelFileName = "/prefs/channels.proto"; /** Load a protobuf from a file, return true for success */ bool loadProto(const char *filename, size_t protoSize, size_t objSize, const pb_msgdesc_t *fields, void *dest_struct) @@ -330,7 +334,7 @@ bool loadProto(const char *filename, size_t protoSize, size_t objSize, const pb_ void NodeDB::loadFromDisk() { // static DeviceState scratch; We no longer read into a tempbuf because this structure is 15KB of valuable RAM - if (!loadProto(preffile, DeviceState_size, sizeof(devicestate), DeviceState_fields, &devicestate)) { + if (!loadProto(prefFileName, DeviceState_size, sizeof(devicestate), DeviceState_fields, &devicestate)) { installDefaultDeviceState(); // Our in RAM copy might now be corrupt } else { if (devicestate.version < DEVICESTATE_MIN_VER) { @@ -353,7 +357,7 @@ void NodeDB::loadFromDisk() } } - if (!loadProto(configfile, LocalConfig_size, sizeof(LocalConfig), LocalConfig_fields, &config)) { + if (!loadProto(configFileName, LocalConfig_size, sizeof(LocalConfig), LocalConfig_fields, &config)) { installDefaultConfig(); // Our in RAM copy might now be corrupt } else { if (config.version < DEVICESTATE_MIN_VER) { @@ -364,7 +368,7 @@ void NodeDB::loadFromDisk() } } - if (!loadProto(moduleConfigfile, LocalModuleConfig_size, sizeof(LocalModuleConfig), LocalModuleConfig_fields, &moduleConfig)) { + if (!loadProto(moduleConfigFileName, LocalModuleConfig_size, sizeof(LocalModuleConfig), LocalModuleConfig_fields, &moduleConfig)) { installDefaultModuleConfig(); // Our in RAM copy might now be corrupt } else { if (moduleConfig.version < DEVICESTATE_MIN_VER) { @@ -375,7 +379,7 @@ void NodeDB::loadFromDisk() } } - if (!loadProto(channelfile, ChannelFile_size, sizeof(ChannelFile), ChannelFile_fields, &channelFile)) { + if (!loadProto(channelFileName, ChannelFile_size, sizeof(ChannelFile), ChannelFile_fields, &channelFile)) { installDefaultChannels(); // Our in RAM copy might now be corrupt } else { if (channelFile.version < DEVICESTATE_MIN_VER) { @@ -428,7 +432,7 @@ void NodeDB::saveChannelsToDisk() #ifdef FSCom FSCom.mkdir("/prefs"); #endif - saveProto(channelfile, ChannelFile_size, sizeof(ChannelFile), ChannelFile_fields, &channelFile); + saveProto(channelFileName, ChannelFile_size, sizeof(ChannelFile), ChannelFile_fields, &channelFile); } } @@ -438,7 +442,7 @@ void NodeDB::saveToDisk() #ifdef FSCom FSCom.mkdir("/prefs"); #endif - saveProto(preffile, DeviceState_size, sizeof(devicestate), DeviceState_fields, &devicestate); + saveProto(prefFileName, DeviceState_size, sizeof(devicestate), DeviceState_fields, &devicestate); // save all config segments config.has_device = true; @@ -447,7 +451,7 @@ void NodeDB::saveToDisk() config.has_position = true; config.has_power = true; config.has_wifi = true; - saveProto(configfile, LocalConfig_size, sizeof(LocalConfig), LocalConfig_fields, &config); + saveProto(configFileName, LocalConfig_size, sizeof(LocalConfig), LocalConfig_fields, &config); moduleConfig.has_canned_message = true; moduleConfig.has_external_notification = true; @@ -456,7 +460,7 @@ void NodeDB::saveToDisk() moduleConfig.has_serial = true; moduleConfig.has_store_forward = true; moduleConfig.has_telemetry = true; - saveProto(moduleConfigfile, LocalModuleConfig_size, sizeof(LocalModuleConfig), LocalModuleConfig_fields, &moduleConfig); + saveProto(moduleConfigFileName, LocalModuleConfig_size, sizeof(LocalModuleConfig), LocalModuleConfig_fields, &moduleConfig); saveChannelsToDisk(); diff --git a/src/mesh/NodeDB.h b/src/mesh/NodeDB.h index 2cd3a833..0a2a5bdb 100644 --- a/src/mesh/NodeDB.h +++ b/src/mesh/NodeDB.h @@ -119,6 +119,8 @@ class NodeDB newStatus.notifyObservers(&status); } + bool factoryReset(); + /// read our db from flash void loadFromDisk(); diff --git a/src/mesh/RadioInterface.cpp b/src/mesh/RadioInterface.cpp index 09a0e3a9..05c60035 100644 --- a/src/mesh/RadioInterface.cpp +++ b/src/mesh/RadioInterface.cpp @@ -419,9 +419,8 @@ void RadioInterface::applyModemConfig() saveChannelNum(channel_num); saveFreq(freq); - DEBUG_MSG("Set radio: name=%s, config=%u, ch=%d, power=%d\n", channelName, loraConfig.modem_preset, channel_num, power); - DEBUG_MSG("Radio myRegion->freqStart / myRegion->freqEnd: %f -> %f (%f mhz)\n", myRegion->freqStart, myRegion->freqEnd, - myRegion->freqEnd - myRegion->freqStart); + DEBUG_MSG("Set radio: region=%, name=%s, config=%u, ch=%d, power=%d\n", myRegion->code, channelName, loraConfig.modem_preset, channel_num, power); + DEBUG_MSG("Radio myRegion->freqStart / myRegion->freqEnd: %f -> %f (%f mhz)\n", myRegion->freqStart, myRegion->freqEnd, myRegion->freqEnd - myRegion->freqStart); DEBUG_MSG("Radio myRegion->numChannels: %d\n", numChannels); DEBUG_MSG("Radio channel_num: %d\n", channel_num); DEBUG_MSG("Radio frequency: %f\n", getFreq());