diff --git a/platformio.ini b/platformio.ini index c97a56dcf..9b3bdb79d 100644 --- a/platformio.ini +++ b/platformio.ini @@ -51,6 +51,7 @@ lib_deps = https://github.com/meshtastic/TinyGPSPlus.git#2f0d0528d737000043e949f4c3bdfb623cf0b902 https://github.com/meshtastic/ArduinoThread.git#72921ac222eed6f526ba1682023cee290d9aa1b3 nanopb/Nanopb@^0.4.6 + erriez/ErriezCRC32@^1.0.1 ; Used for the code analysis in PIO Home / Inspect check_tool = cppcheck diff --git a/src/mesh/NodeDB.cpp b/src/mesh/NodeDB.cpp index 2bcf46f17..4fe23f876 100644 --- a/src/mesh/NodeDB.cpp +++ b/src/mesh/NodeDB.cpp @@ -16,6 +16,7 @@ #include "mesh-pb-constants.h" #include #include +#include #ifdef ARCH_ESP32 #include "mesh/http/WiFiAPClient.h" @@ -268,6 +269,12 @@ void NodeDB::init() DEBUG_MSG("Initializing NodeDB\n"); loadFromDisk(); + uint32_t devicestateCRC = crc32Buffer(&devicestate, sizeof(devicestate)); + uint32_t configCRC = crc32Buffer(&config, sizeof(config)); + uint32_t channelFileCRC = crc32Buffer(&channelFile, sizeof(channelFile)); + + int saveWhat = 0; + 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) @@ -305,7 +312,15 @@ void NodeDB::init() 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(SEGMENT_DEVICESTATE | SEGMENT_CONFIG | SEGMENT_CHANNELS); + + 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; + + saveToDisk(saveWhat); } // We reserve a few nodenums for future use @@ -480,11 +495,11 @@ void NodeDB::saveToDisk(int saveWhat) #ifdef FSCom FSCom.mkdir("/prefs"); #endif - if (saveWhat && SEGMENT_DEVICESTATE) { + if (saveWhat & SEGMENT_DEVICESTATE) { saveDeviceStateToDisk(); } - if (saveWhat && SEGMENT_CONFIG) { + if (saveWhat & SEGMENT_CONFIG) { config.has_device = true; config.has_display = true; config.has_lora = true; @@ -495,7 +510,7 @@ void NodeDB::saveToDisk(int saveWhat) saveProto(configFileName, LocalConfig_size, sizeof(config), LocalConfig_fields, &config); } - if (saveWhat && SEGMENT_MODULECONFIG) { + if (saveWhat & SEGMENT_MODULECONFIG) { moduleConfig.has_canned_message = true; moduleConfig.has_external_notification = true; moduleConfig.has_mqtt = true; @@ -506,7 +521,7 @@ void NodeDB::saveToDisk(int saveWhat) saveProto(moduleConfigFileName, LocalModuleConfig_size, sizeof(moduleConfig), LocalModuleConfig_fields, &moduleConfig); } - if (saveWhat && SEGMENT_CHANNELS) { + if (saveWhat & SEGMENT_CHANNELS) { saveChannelsToDisk(); } } else {