From 063c4904ff0cccb8e4f321ff9abc26cce33bd05a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=B6ttgens?= Date: Tue, 4 Oct 2022 17:25:03 +0200 Subject: [PATCH 1/3] only save files when they changed - also clamp app version --- src/mesh/NodeDB.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mesh/NodeDB.cpp b/src/mesh/NodeDB.cpp index 551bfdd2..2bcf46f1 100644 --- a/src/mesh/NodeDB.cpp +++ b/src/mesh/NodeDB.cpp @@ -274,7 +274,7 @@ void NodeDB::init() myNodeInfo.error_address = 0; // likewise - we always want the app requirements to come from the running appload - myNodeInfo.min_app_version = 20300; // format is Mmmss (where M is 1+the numeric major number. i.e. 20120 means 1.1.20 + myNodeInfo.min_app_version = 20341; // format is Mmmss (where M is 1+the numeric major number. i.e. 20120 means 1.1.20 // 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) @@ -305,7 +305,7 @@ 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(); + saveToDisk(SEGMENT_DEVICESTATE | SEGMENT_CONFIG | SEGMENT_CHANNELS); } // We reserve a few nodenums for future use From 54816231e9dab469d26a8ada317a1943dfa74c33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=B6ttgens?= Date: Wed, 5 Oct 2022 08:52:27 +0200 Subject: [PATCH 2/3] Calculate CRC32 of Protobuf and compare before save. --- platformio.ini | 1 + src/mesh/NodeDB.cpp | 25 ++++++++++++++++++++----- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/platformio.ini b/platformio.ini index c97a56dc..9b3bdb79 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 2bcf46f1..4fe23f87 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 { From f3042ddf37ffd1e9e7d27231a3b7b5a1a9dceffd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=B6ttgens?= Date: Wed, 5 Oct 2022 08:56:00 +0200 Subject: [PATCH 3/3] Update NodeDB.cpp --- src/mesh/NodeDB.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mesh/NodeDB.cpp b/src/mesh/NodeDB.cpp index 4fe23f87..fca3af30 100644 --- a/src/mesh/NodeDB.cpp +++ b/src/mesh/NodeDB.cpp @@ -281,7 +281,7 @@ void NodeDB::init() myNodeInfo.error_address = 0; // likewise - we always want the app requirements to come from the running appload - myNodeInfo.min_app_version = 20341; // format is Mmmss (where M is 1+the numeric major number. i.e. 20120 means 1.1.20 + myNodeInfo.min_app_version = 20300; // format is Mmmss (where M is 1+the numeric major number. i.e. 20120 means 1.1.20 // 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)