diff --git a/src/mesh/NodeDB.cpp b/src/mesh/NodeDB.cpp index 4e87ac773..8a2968758 100644 --- a/src/mesh/NodeDB.cpp +++ b/src/mesh/NodeDB.cpp @@ -110,8 +110,7 @@ void NodeDB::resetRadioConfig() */ } -void NodeDB::init() -{ +void NodeDB::installDefaultDeviceState() { // init our devicestate with valid flags so protobuf writing/reading will work devicestate.has_my_node = true; devicestate.has_radio = true; @@ -140,11 +139,16 @@ void NodeDB::init() // owner.short_name now sprintf(owner.long_name, "Unknown %02x%02x", ourMacAddr[4], ourMacAddr[5]); sprintf(owner.short_name, "?%02X", myNodeInfo.my_node_num & 0xff); +} + +void NodeDB::init() +{ + installDefaultDeviceState(); if (!FSBegin()) // FIXME - do this in main? { DEBUG_MSG("ERROR filesystem mount Failed\n"); - // FIXME - report failure to phone + assert(0); // FIXME - report failure to phone } // saveToDisk(); @@ -210,7 +214,7 @@ const char *preftmp = "/db.proto.tmp"; void NodeDB::loadFromDisk() { #ifdef FS - static DeviceState scratch; + // static DeviceState scratch; We no longer read into a tempbuf because this structure is 15KB of valuable RAM auto f = FS.open(preffile); if (f) { @@ -219,16 +223,18 @@ void NodeDB::loadFromDisk() // DEBUG_MSG("Preload channel name=%s\n", channelSettings.name); - memset(&scratch, 0, sizeof(scratch)); - if (!pb_decode(&stream, DeviceState_fields, &scratch)) { + memset(&devicestate, 0, sizeof(devicestate)); + if (!pb_decode(&stream, DeviceState_fields, &devicestate)) { DEBUG_MSG("Error: can't decode protobuf %s\n", PB_GET_ERROR(&stream)); + installDefaultDeviceState(); // Our in RAM copy might now be corrupt // FIXME - report failure to phone } else { - if (scratch.version < DEVICESTATE_MIN_VER) + if (devicestate.version < DEVICESTATE_MIN_VER) { DEBUG_MSG("Warn: devicestate is old, discarding\n"); + installDefaultDeviceState(); + } else { - DEBUG_MSG("Loaded saved preferences version %d\n", scratch.version); - devicestate = scratch; + DEBUG_MSG("Loaded saved preferences version %d\n", devicestate.version); } // DEBUG_MSG("Postload channel name=%s\n", channelSettings.name); @@ -238,6 +244,9 @@ void NodeDB::loadFromDisk() } else { DEBUG_MSG("No saved preferences found\n"); } + + + #else DEBUG_MSG("ERROR: Filesystem not implemented\n"); #endif diff --git a/src/mesh/NodeDB.h b/src/mesh/NodeDB.h index 0171228a8..611024e22 100644 --- a/src/mesh/NodeDB.h +++ b/src/mesh/NodeDB.h @@ -93,6 +93,9 @@ class NodeDB /// read our db from flash void loadFromDisk(); + + /// Reinit device state from scratch (not loading from disk) + void installDefaultDeviceState(); }; /**