|
|
|
|
@@ -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,17 @@ 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
|
|
|
|
|
@@ -259,8 +262,8 @@ void NodeDB::init()
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
resetRadioConfig(); // If bogus settings got saved, then fix them
|
|
|
|
|
|
|
|
|
|
DEBUG_MSG("region=%d, NODENUM=0x%x, dbsize=%d\n", config.lora.region, myNodeInfo.my_node_num, *numNodes);
|
|
|
|
|
saveToDisk();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// We reserve a few nodenums for future use
|
|
|
|
|
@@ -290,10 +293,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 +333,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 +356,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 +367,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 +378,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 +431,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 +441,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 +450,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,10 +459,9 @@ 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();
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
DEBUG_MSG("***** DEVELOPMENT MODE - DO NOT RELEASE - not saving to flash *****\n");
|
|
|
|
|
}
|
|
|
|
|
|