Node db cleanup and debug prints (#1543)

* Node db cleanup and debug prints

* File name cleanup
This commit is contained in:
Ben Meadors 2022-07-02 09:09:41 -05:00 committed by GitHub
parent f8ee1ac4f9
commit 3f0ff45232
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 44 deletions

View File

@ -89,26 +89,7 @@ bool NodeDB::resetRadioConfig()
// radioConfig.has_preferences = true; // radioConfig.has_preferences = true;
if (config.device.factory_reset) { if (config.device.factory_reset) {
DEBUG_MSG("Performing factory reset!\n"); didFactoryReset = factoryReset();
// 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;
} }
if (channelFile.channels_count != MAX_NUM_CHANNELS) { if (channelFile.channels_count != MAX_NUM_CHANNELS) {
@ -142,8 +123,33 @@ bool NodeDB::resetRadioConfig()
return didFactoryReset; 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() void NodeDB::installDefaultConfig()
{ {
DEBUG_MSG("Installing default LocalConfig\n");
memset(&config, 0, sizeof(LocalConfig)); memset(&config, 0, sizeof(LocalConfig));
config.version = DEVICESTATE_CUR_VER; config.version = DEVICESTATE_CUR_VER;
config.has_device = true; config.has_device = true;
@ -164,25 +170,28 @@ void NodeDB::installDefaultConfig()
void NodeDB::installDefaultModuleConfig() void NodeDB::installDefaultModuleConfig()
{ {
DEBUG_MSG("Installing default ModuleConfig\n");
memset(&moduleConfig, 0, sizeof(ModuleConfig)); memset(&moduleConfig, 0, sizeof(ModuleConfig));
moduleConfig.version = DEVICESTATE_CUR_VER; moduleConfig.version = DEVICESTATE_CUR_VER;
moduleConfig.has_canned_message = true;
moduleConfig.has_external_notification = true;
moduleConfig.has_mqtt = true; moduleConfig.has_mqtt = true;
moduleConfig.has_range_test = true; moduleConfig.has_range_test = true;
moduleConfig.has_serial = true; moduleConfig.has_serial = true;
moduleConfig.has_store_forward = true; moduleConfig.has_store_forward = true;
moduleConfig.has_telemetry = true; moduleConfig.has_telemetry = true;
moduleConfig.has_external_notification = true;
moduleConfig.has_canned_message = true;
} }
void NodeDB::installDefaultChannels() void NodeDB::installDefaultChannels()
{ {
DEBUG_MSG("Installing default ChannelFile\n");
memset(&channelFile, 0, sizeof(ChannelFile)); memset(&channelFile, 0, sizeof(ChannelFile));
channelFile.version = DEVICESTATE_CUR_VER; channelFile.version = DEVICESTATE_CUR_VER;
} }
void NodeDB::installDefaultDeviceState() void NodeDB::installDefaultDeviceState()
{ {
DEBUG_MSG("Installing default DeviceState\n");
memset(&devicestate, 0, sizeof(DeviceState)); memset(&devicestate, 0, sizeof(DeviceState));
*numNodes = 0; // Forget node DB *numNodes = 0; // Forget node DB
@ -209,23 +218,18 @@ void NodeDB::installDefaultDeviceState()
sprintf(owner.id, "!%08x", getNodeNum()); // Default node ID now based on nodenum sprintf(owner.id, "!%08x", getNodeNum()); // Default node ID now based on nodenum
memcpy(owner.macaddr, ourMacAddr, sizeof(owner.macaddr)); memcpy(owner.macaddr, ourMacAddr, sizeof(owner.macaddr));
installDefaultChannels();
installDefaultConfig();
} }
void NodeDB::init() void NodeDB::init()
{ {
installDefaultDeviceState(); DEBUG_MSG("Initializing NodeDB\n");
// saveToDisk(); // saveToDisk();
loadFromDisk(); loadFromDisk();
// saveToDisk(); // saveToDisk();
myNodeInfo.max_channels = MAX_NUM_CHANNELS; // tell others the max # of channels we can understand myNodeInfo.max_channels = MAX_NUM_CHANNELS; // tell others the max # of channels we can understand
myNodeInfo.error_code = myNodeInfo.error_code = CriticalErrorCode_None; // For the error code, only show values from this boot (discard value from flash)
CriticalErrorCode_None; // For the error code, only show values from this boot (discard value from flash)
myNodeInfo.error_address = 0; myNodeInfo.error_address = 0;
// likewise - we always want the app requirements to come from the running appload // 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; myNodeInfo.my_node_num = r;
} }
static const char *preffile = "/prefs/db.proto"; static const char *prefFileName = "/prefs/db.proto";
static const char *configfile = "/prefs/config.proto"; static const char *configFileName = "/prefs/config.proto";
static const char *moduleConfigfile = "/prefs/module.proto"; static const char *moduleConfigFileName = "/prefs/module.proto";
static const char *channelfile = "/prefs/channels.proto"; static const char *channelFileName = "/prefs/channels.proto";
/** Load a protobuf from a file, return true for success */ /** 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) 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() void NodeDB::loadFromDisk()
{ {
// static DeviceState scratch; We no longer read into a tempbuf because this structure is 15KB of valuable RAM // 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 installDefaultDeviceState(); // Our in RAM copy might now be corrupt
} else { } else {
if (devicestate.version < DEVICESTATE_MIN_VER) { 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 installDefaultConfig(); // Our in RAM copy might now be corrupt
} else { } else {
if (config.version < DEVICESTATE_MIN_VER) { 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 installDefaultModuleConfig(); // Our in RAM copy might now be corrupt
} else { } else {
if (moduleConfig.version < DEVICESTATE_MIN_VER) { 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 installDefaultChannels(); // Our in RAM copy might now be corrupt
} else { } else {
if (channelFile.version < DEVICESTATE_MIN_VER) { if (channelFile.version < DEVICESTATE_MIN_VER) {
@ -428,7 +432,7 @@ void NodeDB::saveChannelsToDisk()
#ifdef FSCom #ifdef FSCom
FSCom.mkdir("/prefs"); FSCom.mkdir("/prefs");
#endif #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 #ifdef FSCom
FSCom.mkdir("/prefs"); FSCom.mkdir("/prefs");
#endif #endif
saveProto(preffile, DeviceState_size, sizeof(devicestate), DeviceState_fields, &devicestate); saveProto(prefFileName, DeviceState_size, sizeof(devicestate), DeviceState_fields, &devicestate);
// save all config segments // save all config segments
config.has_device = true; config.has_device = true;
@ -447,7 +451,7 @@ void NodeDB::saveToDisk()
config.has_position = true; config.has_position = true;
config.has_power = true; config.has_power = true;
config.has_wifi = 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_canned_message = true;
moduleConfig.has_external_notification = true; moduleConfig.has_external_notification = true;
@ -456,7 +460,7 @@ void NodeDB::saveToDisk()
moduleConfig.has_serial = true; moduleConfig.has_serial = true;
moduleConfig.has_store_forward = true; moduleConfig.has_store_forward = true;
moduleConfig.has_telemetry = true; moduleConfig.has_telemetry = true;
saveProto(moduleConfigfile, LocalModuleConfig_size, sizeof(LocalModuleConfig), LocalModuleConfig_fields, &moduleConfig); saveProto(moduleConfigFileName, LocalModuleConfig_size, sizeof(LocalModuleConfig), LocalModuleConfig_fields, &moduleConfig);
saveChannelsToDisk(); saveChannelsToDisk();

View File

@ -119,6 +119,8 @@ class NodeDB
newStatus.notifyObservers(&status); newStatus.notifyObservers(&status);
} }
bool factoryReset();
/// read our db from flash /// read our db from flash
void loadFromDisk(); void loadFromDisk();

View File

@ -419,9 +419,8 @@ void RadioInterface::applyModemConfig()
saveChannelNum(channel_num); saveChannelNum(channel_num);
saveFreq(freq); saveFreq(freq);
DEBUG_MSG("Set radio: name=%s, config=%u, ch=%d, power=%d\n", channelName, loraConfig.modem_preset, channel_num, power); 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, DEBUG_MSG("Radio myRegion->freqStart / myRegion->freqEnd: %f -> %f (%f mhz)\n", myRegion->freqStart, myRegion->freqEnd, myRegion->freqEnd - myRegion->freqStart);
myRegion->freqEnd - myRegion->freqStart);
DEBUG_MSG("Radio myRegion->numChannels: %d\n", numChannels); DEBUG_MSG("Radio myRegion->numChannels: %d\n", numChannels);
DEBUG_MSG("Radio channel_num: %d\n", channel_num); DEBUG_MSG("Radio channel_num: %d\n", channel_num);
DEBUG_MSG("Radio frequency: %f\n", getFreq()); DEBUG_MSG("Radio frequency: %f\n", getFreq());