From 8d14e97dfaf52372303c99b037204ecf3d49f6ed Mon Sep 17 00:00:00 2001 From: geeksville Date: Sat, 6 Jun 2020 08:07:21 -0700 Subject: [PATCH 1/3] oops - we were not saving radio state --- src/mesh/NodeDB.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/mesh/NodeDB.cpp b/src/mesh/NodeDB.cpp index 0fee4b7b..46560de4 100644 --- a/src/mesh/NodeDB.cpp +++ b/src/mesh/NodeDB.cpp @@ -114,7 +114,6 @@ void NodeDB::init() devicestate.has_my_node = true; devicestate.has_radio = true; devicestate.has_owner = true; - devicestate.has_radio = false; devicestate.radio.has_channel_settings = true; devicestate.radio.has_preferences = true; devicestate.node_db_count = 0; @@ -137,7 +136,7 @@ void NodeDB::init() memcpy(owner.macaddr, ourMacAddr, sizeof(owner.macaddr)); sprintf(owner.long_name, "Unknown %02x%02x", ourMacAddr[4], ourMacAddr[5]); - // Crummy guess at our nodenum + // Crummy guess at our nodenum (but we will check against the nodedb to avoid conflicts) pickNewNodeNum(); sprintf(owner.short_name, "?%02X", myNodeInfo.my_node_num & 0xff); From 9ea65c6793fba9356f746fff419c9366299f8664 Mon Sep 17 00:00:00 2001 From: geeksville Date: Sat, 6 Jun 2020 08:30:01 -0700 Subject: [PATCH 2/3] Fix #153 - details below Somehow nodenum was getting reset to zero (and saved to flash - which is bad because it makes the failure permanent). So I've changed nodenum selection to occur after we load the saved preferences (and we try to keep nodenum stable in that case). I'm puzzled as to how it ever got set to zero (unless there *shudder* is some errant pointer that clobbered it). But next week I'm turning 4 byte nodenums back on, which will make this moot - because they will always be based on macaddr and the current process where nodes haggle with the mesh to pick a unique one-byte nodenum will be gone. --- src/mesh/NodeDB.cpp | 45 ++++++++++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/src/mesh/NodeDB.cpp b/src/mesh/NodeDB.cpp index 46560de4..334e178f 100644 --- a/src/mesh/NodeDB.cpp +++ b/src/mesh/NodeDB.cpp @@ -136,16 +136,8 @@ void NodeDB::init() memcpy(owner.macaddr, ourMacAddr, sizeof(owner.macaddr)); sprintf(owner.long_name, "Unknown %02x%02x", ourMacAddr[4], ourMacAddr[5]); - // Crummy guess at our nodenum (but we will check against the nodedb to avoid conflicts) - pickNewNodeNum(); - sprintf(owner.short_name, "?%02X", myNodeInfo.my_node_num & 0xff); - // Include our owner in the node db under our nodenum - NodeInfo *info = getOrCreateNode(getNodeNum()); - info->user = owner; - info->has_user = true; - if (!FSBegin()) // FIXME - do this in main? { DEBUG_MSG("ERROR filesystem mount Failed\n"); @@ -156,6 +148,15 @@ void NodeDB::init() loadFromDisk(); // saveToDisk(); + // 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(); + + // Include our owner in the node db under our nodenum + NodeInfo *info = getOrCreateNode(getNodeNum()); + info->user = owner; + info->has_user = true; + // We set these _after_ loading from disk - because they come from the build and are more trusted than // what is stored in flash strncpy(myNodeInfo.region, optstr(HW_VERSION), sizeof(myNodeInfo.region)); @@ -175,9 +176,12 @@ void NodeDB::init() */ void NodeDB::pickNewNodeNum() { - // Pick an initial nodenum based on the macaddr - NodeNum r = sizeof(NodeNum) == 1 ? ourMacAddr[5] - : ((ourMacAddr[2] << 24) | (ourMacAddr[3] << 16) | (ourMacAddr[4] << 8) | ourMacAddr[5]); + NodeNum r = myNodeInfo.my_node_num; + + // If we don't have a nodenum at app - pick an initial nodenum based on the macaddr + if (r == 0) + r = sizeof(NodeNum) == 1 ? ourMacAddr[5] + : ((ourMacAddr[2] << 24) | (ourMacAddr[3] << 16) | (ourMacAddr[4] << 8) | ourMacAddr[5]); if (r == NODENUM_BROADCAST || r < NUM_RESERVED) r = NUM_RESERVED; // don't pick a reserved node number @@ -246,15 +250,18 @@ void NodeDB::saveToDisk() if (!pb_encode(&stream, DeviceState_fields, &devicestate)) { DEBUG_MSG("Error: can't write protobuf %s\n", PB_GET_ERROR(&stream)); // FIXME - report failure to phone + + f.close(); + } else { + // Success - replace the old file + f.close(); + + // brief window of risk here ;-) + if (!FS.remove(preffile)) + DEBUG_MSG("Warning: Can't remove old pref file\n"); + if (!FS.rename(preftmp, preffile)) + DEBUG_MSG("Error: can't rename new pref file\n"); } - - f.close(); - - // brief window of risk here ;-) - if (!FS.remove(preffile)) - DEBUG_MSG("Warning: Can't remove old pref file\n"); - if (!FS.rename(preftmp, preffile)) - DEBUG_MSG("Error: can't rename new pref file\n"); } else { DEBUG_MSG("ERROR: can't write prefs\n"); // FIXME report to app } From 6a3853ef35040ac107351dd553923c6367468041 Mon Sep 17 00:00:00 2001 From: geeksville Date: Sat, 6 Jun 2020 08:33:20 -0700 Subject: [PATCH 3/3] 0.6.8 --- bin/version.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/version.sh b/bin/version.sh index a13f8e90..4b399693 100644 --- a/bin/version.sh +++ b/bin/version.sh @@ -1,3 +1,3 @@ -export VERSION=0.6.7 \ No newline at end of file +export VERSION=0.6.8 \ No newline at end of file