From 185fff0342be5c9a1baee2d0d2f44ec3a64d146e Mon Sep 17 00:00:00 2001 From: Jonathan Bennett Date: Thu, 21 Mar 2024 12:43:36 -0500 Subject: [PATCH] move nodeDB::init code into nodeDB constructor --- src/main.cpp | 2 +- src/mesh/NodeDB.cpp | 119 +++++++++++++++++++++----------------------- src/mesh/NodeDB.h | 3 -- 3 files changed, 58 insertions(+), 66 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index e09af0c9a2..5f746f12a0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -556,7 +556,7 @@ void setup() // We do this as early as possible because this loads preferences from flash // but we need to do this after main cpu init (esp32setup), because we need the random seed set - nodeDB = NodeDB::init(); + nodeDB = new NodeDB; // If we're taking on the repeater role, use flood router and turn off 3V3_S rail because peripherals are not needed if (config.device.role == meshtastic_Config_DeviceConfig_Role_REPEATER) { diff --git a/src/mesh/NodeDB.cpp b/src/mesh/NodeDB.cpp index 37232e6edf..6db8fc50b0 100644 --- a/src/mesh/NodeDB.cpp +++ b/src/mesh/NodeDB.cpp @@ -92,7 +92,63 @@ uint32_t error_address = 0; static uint8_t ourMacAddr[6]; -NodeDB::NodeDB() {} +NodeDB::NodeDB() +{ + LOG_INFO("Initializing NodeDB\n"); + loadFromDisk(); + cleanupMeshDB(); + + uint32_t devicestateCRC = crc32Buffer(&devicestate, sizeof(devicestate)); + uint32_t configCRC = crc32Buffer(&config, sizeof(config)); + uint32_t channelFileCRC = crc32Buffer(&channelFile, sizeof(channelFile)); + + int saveWhat = 0; + + // likewise - we always want the app requirements to come from the running appload + myNodeInfo.min_app_version = 30200; // format is Mmmss (where M is 1+the numeric major number. i.e. 30200 means 2.2.00 + // Note! We do this after loading saved settings, so that if somehow an invalid nodenum was stored in preferences we won't + // keep using that nodenum forever. Crummy guess at our nodenum (but we will check against the nodedb to avoid conflicts) + pickNewNodeNum(); + + // Set our board type so we can share it with others + owner.hw_model = HW_VENDOR; + // Ensure user (nodeinfo) role is set to whatever we're configured to + owner.role = config.device.role; + + // Include our owner in the node db under our nodenum + meshtastic_NodeInfoLite *info = getOrCreateMeshNode(getNodeNum()); + info->user = owner; + info->has_user = true; + +#ifdef ARCH_ESP32 + Preferences preferences; + preferences.begin("meshtastic", false); + myNodeInfo.reboot_count = preferences.getUInt("rebootCounter", 0); + preferences.end(); + LOG_DEBUG("Number of Device Reboots: %d\n", myNodeInfo.reboot_count); +#endif + + resetRadioConfig(); // If bogus settings got saved, then fix them + // nodeDB->LOG_DEBUG("region=%d, NODENUM=0x%x, dbsize=%d\n", config.lora.region, myNodeInfo.my_node_num, numMeshNodes); + + if (devicestateCRC != crc32Buffer(&devicestate, sizeof(devicestate))) + saveWhat |= SEGMENT_DEVICESTATE; + if (configCRC != crc32Buffer(&config, sizeof(config))) + saveWhat |= SEGMENT_CONFIG; + if (channelFileCRC != crc32Buffer(&channelFile, sizeof(channelFile))) + saveWhat |= SEGMENT_CHANNELS; + + if (!devicestate.node_remote_hardware_pins) { + meshtastic_NodeRemoteHardwarePin empty[12] = {meshtastic_RemoteHardwarePin_init_default}; + memcpy(devicestate.node_remote_hardware_pins, empty, sizeof(empty)); + } + + if (config.position.gps_enabled) { + config.position.gps_mode = meshtastic_Config_PositionConfig_GpsMode_ENABLED; + config.position.gps_enabled = 0; + } + saveToDisk(saveWhat); +} /** * Most (but not always) of the time we want to treat packets 'from' the local phone (where from == 0), as if they originated on @@ -439,67 +495,6 @@ void NodeDB::installDefaultDeviceState() memcpy(owner.macaddr, ourMacAddr, sizeof(owner.macaddr)); } -NodeDB *NodeDB::init() -{ - LOG_INFO("Initializing NodeDB\n"); - NodeDB *newnodeDB = new NodeDB; - newnodeDB->loadFromDisk(); - newnodeDB->cleanupMeshDB(); - - uint32_t devicestateCRC = crc32Buffer(&devicestate, sizeof(devicestate)); - uint32_t configCRC = crc32Buffer(&config, sizeof(config)); - uint32_t channelFileCRC = crc32Buffer(&channelFile, sizeof(channelFile)); - - int saveWhat = 0; - - // likewise - we always want the app requirements to come from the running appload - myNodeInfo.min_app_version = 30200; // format is Mmmss (where M is 1+the numeric major number. i.e. 30200 means 2.2.00 - // Note! We do this after loading saved settings, so that if somehow an invalid nodenum was stored in preferences we won't - // keep using that nodenum forever. Crummy guess at our nodenum (but we will check against the nodedb to avoid conflicts) - newnodeDB->pickNewNodeNum(); - - // Set our board type so we can share it with others - owner.hw_model = HW_VENDOR; - // Ensure user (nodeinfo) role is set to whatever we're configured to - owner.role = config.device.role; - - // Include our owner in the node db under our nodenum - meshtastic_NodeInfoLite *info = newnodeDB->getOrCreateMeshNode(newnodeDB->getNodeNum()); - info->user = owner; - info->has_user = true; - -#ifdef ARCH_ESP32 - Preferences preferences; - preferences.begin("meshtastic", false); - myNodeInfo.reboot_count = preferences.getUInt("rebootCounter", 0); - preferences.end(); - LOG_DEBUG("Number of Device Reboots: %d\n", myNodeInfo.reboot_count); -#endif - - newnodeDB->resetRadioConfig(); // If bogus settings got saved, then fix them - // nodeDB->LOG_DEBUG("region=%d, NODENUM=0x%x, dbsize=%d\n", config.lora.region, myNodeInfo.my_node_num, numMeshNodes); - - if (devicestateCRC != crc32Buffer(&devicestate, sizeof(devicestate))) - saveWhat |= SEGMENT_DEVICESTATE; - if (configCRC != crc32Buffer(&config, sizeof(config))) - saveWhat |= SEGMENT_CONFIG; - if (channelFileCRC != crc32Buffer(&channelFile, sizeof(channelFile))) - saveWhat |= SEGMENT_CHANNELS; - - if (!devicestate.node_remote_hardware_pins) { - meshtastic_NodeRemoteHardwarePin empty[12] = {meshtastic_RemoteHardwarePin_init_default}; - memcpy(devicestate.node_remote_hardware_pins, empty, sizeof(empty)); - } - - if (config.position.gps_enabled) { - config.position.gps_mode = meshtastic_Config_PositionConfig_GpsMode_ENABLED; - config.position.gps_enabled = 0; - } - return newnodeDB; - - nodeDB->saveToDisk(saveWhat); -} - // We reserve a few nodenums for future use #define NUM_RESERVED 4 diff --git a/src/mesh/NodeDB.h b/src/mesh/NodeDB.h index ea2019c375..23870db745 100644 --- a/src/mesh/NodeDB.h +++ b/src/mesh/NodeDB.h @@ -58,9 +58,6 @@ class NodeDB /// instead just store in flash - possibly even in the initial alpha release do this hack NodeDB(); - /// Called from service after app start, to do init which can only be done after OS load - static NodeDB *init(); - /// write to flash void saveToDisk(int saveWhat = SEGMENT_CONFIG | SEGMENT_MODULECONFIG | SEGMENT_DEVICESTATE | SEGMENT_CHANNELS), saveChannelsToDisk(), saveDeviceStateToDisk();