diff --git a/bin/regen-protos.sh b/bin/regen-protos.sh index ad771ab452..b3395de30c 100755 --- a/bin/regen-protos.sh +++ b/bin/regen-protos.sh @@ -10,7 +10,9 @@ echo "prebuilt binaries for your computer into nanopb-0.4.7" cd protobufs ../nanopb-0.4.7/generator-bin/protoc --nanopb_out=-v:../src/mesh/generated/ -I=../protobufs meshtastic/*.proto --experimental_allow_proto3_optional -# cd ../src/mesh/generated/meshtastic +cd ../src/mesh/generated/meshtastic +rename .c .cpp *.c +cd ../../../.. # sed -i 's/#include "meshtastic/#include "./g' -- * # sed -i 's/meshtastic_//g' -- * diff --git a/src/mesh/NodeDB.cpp b/src/mesh/NodeDB.cpp index 8fb03055ce..47a46476fc 100644 --- a/src/mesh/NodeDB.cpp +++ b/src/mesh/NodeDB.cpp @@ -51,12 +51,22 @@ meshtastic_OEMStore oemStore; bool meshtastic_DeviceState_callback(pb_istream_t *istream, pb_ostream_t *ostream, const pb_field_iter_t *field) { - std::vector *vec = (std::vector *)field->pData; + if (ostream) { + std::vector *vec = (std::vector *)field->pData; + for (auto item : *vec) { + if (!pb_encode_tag_for_field(ostream, field)) + return false; + pb_encode_submessage(ostream, meshtastic_NodeInfoLite_fields, &item); + } + } + if (istream) { + meshtastic_NodeInfoLite node; // this gets good data + std::vector *vec = (std::vector *)field->pData; - for (auto item : *vec) { - pb_encode_tag_for_field(ostream, field); - pb_encode_submessage(ostream, meshtastic_NodeInfoLite_fields, &item); + if (istream->bytes_left && pb_decode(istream, meshtastic_NodeInfoLite_fields, &node)) + vec->push_back(node); } + return true; } /** The current change # for radio settings. Starts at 0 on boot and any time the radio settings @@ -81,14 +91,7 @@ uint32_t error_address = 0; static uint8_t ourMacAddr[6]; -NodeDB::NodeDB() : meshNodes(devicestate.node_db_lite) -{ - std::cout << "test " << numMeshNodes << std::endl; - std::cout << MAX_NUM_NODES << std::endl; - meshNodes.reserve(MAX_NUM_NODES); - std::cout << "vector size " << meshNodes.size() << std::endl; - numMeshNodes = devicestate.node_db_lite_count; -} +NodeDB::NodeDB() {} /** * Most (but not always) of the time we want to treat packets 'from' the local phone (where from == 0), as if they originated on @@ -390,12 +393,14 @@ void NodeDB::removeNodeByNum(uint nodeNum) { int newPos = 0, removed = 0; for (int i = 0; i < numMeshNodes; i++) { - if (meshNodes[i].num != nodeNum) - meshNodes[newPos++] = meshNodes[i]; + if (meshNodes->at(i).num != nodeNum) + meshNodes->at(newPos++) = meshNodes->at(i); else removed++; } numMeshNodes -= removed; + std::fill(devicestate.node_db_lite.begin() + numMeshNodes, devicestate.node_db_lite.begin() + numMeshNodes + 1, + meshtastic_NodeInfoLite()); LOG_DEBUG("NodeDB::removeNodeByNum purged %d entries. Saving changes...\n", removed); saveDeviceStateToDisk(); } @@ -404,12 +409,14 @@ void NodeDB::cleanupMeshDB() { int newPos = 0, removed = 0; for (int i = 0; i < numMeshNodes; i++) { - if (meshNodes[i].has_user) - meshNodes[newPos++] = meshNodes[i]; + if (meshNodes->at(i).has_user) + meshNodes->at(newPos++) = meshNodes->at(i); else removed++; } numMeshNodes -= removed; + std::fill(devicestate.node_db_lite.begin() + numMeshNodes, devicestate.node_db_lite.begin() + numMeshNodes + removed, + meshtastic_NodeInfoLite()); LOG_DEBUG("cleanupMeshDB purged %d entries\n", removed); } @@ -419,6 +426,7 @@ void NodeDB::installDefaultDeviceState() // memset(&devicestate, 0, sizeof(meshtastic_DeviceState)); numMeshNodes = 0; + meshNodes = &devicestate.node_db_lite; // init our devicestate with valid flags so protobuf writing/reading will work devicestate.has_my_node = true; @@ -565,17 +573,21 @@ bool NodeDB::loadProto(const char *filename, size_t protoSize, size_t objSize, c void NodeDB::loadFromDisk() { // static DeviceState scratch; We no longer read into a tempbuf because this structure is 15KB of valuable RAM - if (!loadProto(prefFileName, sizeof(meshtastic_DeviceState), sizeof(meshtastic_DeviceState), &meshtastic_DeviceState_msg, - &devicestate)) { + if (!loadProto(prefFileName, sizeof(meshtastic_DeviceState) + MAX_NUM_NODES * sizeof(meshtastic_NodeInfo), + sizeof(meshtastic_DeviceState), &meshtastic_DeviceState_msg, &devicestate)) { installDefaultDeviceState(); // Our in RAM copy might now be corrupt } else { if (devicestate.version < DEVICESTATE_MIN_VER) { LOG_WARN("Devicestate %d is old, discarding\n", devicestate.version); factoryReset(); } else { - LOG_INFO("Loaded saved devicestate version %d\n", devicestate.version); + LOG_INFO("Loaded saved devicestate version %d, with nodecount: %d\n", devicestate.version, + devicestate.node_db_lite.size()); + meshNodes = &devicestate.node_db_lite; + numMeshNodes = devicestate.node_db_lite.size(); } } + meshNodes->resize(MAX_NUM_NODES); if (!loadProto(configFileName, meshtastic_LocalConfig_size, sizeof(meshtastic_LocalConfig), &meshtastic_LocalConfig_msg, &config)) { @@ -728,7 +740,7 @@ void NodeDB::saveToDisk(int saveWhat) const meshtastic_NodeInfoLite *NodeDB::readNextMeshNode(uint32_t &readIndex) { if (readIndex < numMeshNodes) - return &meshNodes[readIndex++]; + return &meshNodes->at(readIndex++); else return NULL; } @@ -764,9 +776,9 @@ size_t NodeDB::getNumOnlineMeshNodes(bool localOnly) // FIXME this implementation is kinda expensive for (int i = 0; i < numMeshNodes; i++) { - if (localOnly && meshNodes[i].via_mqtt) + if (localOnly && meshNodes->at(i).via_mqtt) continue; - if (sinceLastSeen(&meshNodes[i]) < NUM_ONLINE_SECS) + if (sinceLastSeen(&meshNodes->at(i)) < NUM_ONLINE_SECS) numseen++; } @@ -915,8 +927,8 @@ uint8_t NodeDB::getMeshNodeChannel(NodeNum n) meshtastic_NodeInfoLite *NodeDB::getMeshNode(NodeNum n) { for (int i = 0; i < numMeshNodes; i++) - if (meshNodes[i].num == n) - return &meshNodes[i]; + if (meshNodes->at(i).num == n) + return &meshNodes->at(i); return NULL; } @@ -935,19 +947,19 @@ meshtastic_NodeInfoLite *NodeDB::getOrCreateMeshNode(NodeNum n) uint32_t oldest = UINT32_MAX; int oldestIndex = -1; for (int i = 1; i < numMeshNodes; i++) { - if (meshNodes[i].last_heard < oldest) { - oldest = meshNodes[i].last_heard; + if (meshNodes->at(i).last_heard < oldest) { + oldest = meshNodes->at(i).last_heard; oldestIndex = i; } } // Shove the remaining nodes down the chain for (int i = oldestIndex; i < numMeshNodes - 1; i++) { - meshNodes[i] = meshNodes[i + 1]; + meshNodes->at(i) = meshNodes->at(i + 1); } (numMeshNodes)--; } // add the node at the end - lite = &meshNodes[(numMeshNodes)++]; + lite = &meshNodes->at((numMeshNodes)++); // everything is missing except the nodenum memset(lite, 0, sizeof(*lite)); diff --git a/src/mesh/NodeDB.h b/src/mesh/NodeDB.h index 362b28618b..a2e83fb5cf 100644 --- a/src/mesh/NodeDB.h +++ b/src/mesh/NodeDB.h @@ -46,13 +46,13 @@ class NodeDB // Eventually use a smarter datastructure // HashMap nodes; // Note: these two references just point into our static array we serialize to/from disk - std::vector meshNodes; - pb_size_t numMeshNodes; public: + std::vector *meshNodes; bool updateGUI = false; // we think the gui should definitely be redrawn, screen will clear this once handled meshtastic_NodeInfoLite *updateGUIforNode = NULL; // if currently showing this node, we think you should update the GUI Observable newStatus; + pb_size_t numMeshNodes; /// don't do mesh based algorithm for node id assignment (initially) /// instead just store in flash - possibly even in the initial alpha release do this hack @@ -128,7 +128,7 @@ class NodeDB meshtastic_NodeInfoLite *getMeshNodeByIndex(size_t x) { assert(x < numMeshNodes); - return &meshNodes[x]; + return &meshNodes->at(x); } meshtastic_NodeInfoLite *getMeshNode(NodeNum n); diff --git a/src/mesh/generated/meshtastic/deviceonly.pb.h b/src/mesh/generated/meshtastic/deviceonly.pb.h index 27acbea67a..9ba9edac37 100644 --- a/src/mesh/generated/meshtastic/deviceonly.pb.h +++ b/src/mesh/generated/meshtastic/deviceonly.pb.h @@ -156,7 +156,6 @@ typedef struct _meshtastic_DeviceState { meshtastic_NodeRemoteHardwarePin node_remote_hardware_pins[12]; /* New lite version of NodeDB to decrease memory footprint */ std::vector node_db_lite; - uint32_t node_db_lite_count; } meshtastic_DeviceState; @@ -179,13 +178,13 @@ extern "C" { /* Initializer values for message structs */ -#define meshtastic_DeviceState_init_default {false, meshtastic_MyNodeInfo_init_default, false, meshtastic_User_init_default, 0, {meshtastic_MeshPacket_init_default}, false, meshtastic_MeshPacket_init_default, 0, 0, 0, false, meshtastic_MeshPacket_init_default, 0, {meshtastic_NodeRemoteHardwarePin_init_default, meshtastic_NodeRemoteHardwarePin_init_default, meshtastic_NodeRemoteHardwarePin_init_default, meshtastic_NodeRemoteHardwarePin_init_default, meshtastic_NodeRemoteHardwarePin_init_default, meshtastic_NodeRemoteHardwarePin_init_default, meshtastic_NodeRemoteHardwarePin_init_default, meshtastic_NodeRemoteHardwarePin_init_default, meshtastic_NodeRemoteHardwarePin_init_default, meshtastic_NodeRemoteHardwarePin_init_default, meshtastic_NodeRemoteHardwarePin_init_default, meshtastic_NodeRemoteHardwarePin_init_default}, {{NULL}, NULL}, 0} +#define meshtastic_DeviceState_init_default {false, meshtastic_MyNodeInfo_init_default, false, meshtastic_User_init_default, 0, {meshtastic_MeshPacket_init_default}, false, meshtastic_MeshPacket_init_default, 0, 0, 0, false, meshtastic_MeshPacket_init_default, 0, {meshtastic_NodeRemoteHardwarePin_init_default, meshtastic_NodeRemoteHardwarePin_init_default, meshtastic_NodeRemoteHardwarePin_init_default, meshtastic_NodeRemoteHardwarePin_init_default, meshtastic_NodeRemoteHardwarePin_init_default, meshtastic_NodeRemoteHardwarePin_init_default, meshtastic_NodeRemoteHardwarePin_init_default, meshtastic_NodeRemoteHardwarePin_init_default, meshtastic_NodeRemoteHardwarePin_init_default, meshtastic_NodeRemoteHardwarePin_init_default, meshtastic_NodeRemoteHardwarePin_init_default, meshtastic_NodeRemoteHardwarePin_init_default}, {{NULL}, NULL}} #define meshtastic_NodeInfoLite_init_default {0, false, meshtastic_User_init_default, false, meshtastic_PositionLite_init_default, 0, 0, false, meshtastic_DeviceMetrics_init_default, 0, 0, 0} #define meshtastic_PositionLite_init_default {0, 0, 0, 0, _meshtastic_Position_LocSource_MIN} #define meshtastic_ChannelFile_init_default {0, {meshtastic_Channel_init_default, meshtastic_Channel_init_default, meshtastic_Channel_init_default, meshtastic_Channel_init_default, meshtastic_Channel_init_default, meshtastic_Channel_init_default, meshtastic_Channel_init_default, meshtastic_Channel_init_default}, 0} #define meshtastic_OEMStore_init_default {0, 0, {0, {0}}, _meshtastic_ScreenFonts_MIN, "", {0, {0}}, false, meshtastic_LocalConfig_init_default, false, meshtastic_LocalModuleConfig_init_default} #define meshtastic_NodeRemoteHardwarePin_init_default {0, false, meshtastic_RemoteHardwarePin_init_default} -#define meshtastic_DeviceState_init_zero {false, meshtastic_MyNodeInfo_init_zero, false, meshtastic_User_init_zero, 0, {meshtastic_MeshPacket_init_zero}, false, meshtastic_MeshPacket_init_zero, 0, 0, 0, false, meshtastic_MeshPacket_init_zero, 0, {meshtastic_NodeRemoteHardwarePin_init_zero, meshtastic_NodeRemoteHardwarePin_init_zero, meshtastic_NodeRemoteHardwarePin_init_zero, meshtastic_NodeRemoteHardwarePin_init_zero, meshtastic_NodeRemoteHardwarePin_init_zero, meshtastic_NodeRemoteHardwarePin_init_zero, meshtastic_NodeRemoteHardwarePin_init_zero, meshtastic_NodeRemoteHardwarePin_init_zero, meshtastic_NodeRemoteHardwarePin_init_zero, meshtastic_NodeRemoteHardwarePin_init_zero, meshtastic_NodeRemoteHardwarePin_init_zero, meshtastic_NodeRemoteHardwarePin_init_zero}, {{NULL}, NULL}, 0} +#define meshtastic_DeviceState_init_zero {false, meshtastic_MyNodeInfo_init_zero, false, meshtastic_User_init_zero, 0, {meshtastic_MeshPacket_init_zero}, false, meshtastic_MeshPacket_init_zero, 0, 0, 0, false, meshtastic_MeshPacket_init_zero, 0, {meshtastic_NodeRemoteHardwarePin_init_zero, meshtastic_NodeRemoteHardwarePin_init_zero, meshtastic_NodeRemoteHardwarePin_init_zero, meshtastic_NodeRemoteHardwarePin_init_zero, meshtastic_NodeRemoteHardwarePin_init_zero, meshtastic_NodeRemoteHardwarePin_init_zero, meshtastic_NodeRemoteHardwarePin_init_zero, meshtastic_NodeRemoteHardwarePin_init_zero, meshtastic_NodeRemoteHardwarePin_init_zero, meshtastic_NodeRemoteHardwarePin_init_zero, meshtastic_NodeRemoteHardwarePin_init_zero, meshtastic_NodeRemoteHardwarePin_init_zero}, {{NULL}, NULL}} #define meshtastic_NodeInfoLite_init_zero {0, false, meshtastic_User_init_zero, false, meshtastic_PositionLite_init_zero, 0, 0, false, meshtastic_DeviceMetrics_init_zero, 0, 0, 0} #define meshtastic_PositionLite_init_zero {0, 0, 0, 0, _meshtastic_Position_LocSource_MIN} #define meshtastic_ChannelFile_init_zero {0, {meshtastic_Channel_init_zero, meshtastic_Channel_init_zero, meshtastic_Channel_init_zero, meshtastic_Channel_init_zero, meshtastic_Channel_init_zero, meshtastic_Channel_init_zero, meshtastic_Channel_init_zero, meshtastic_Channel_init_zero}, 0} @@ -229,7 +228,6 @@ extern "C" { #define meshtastic_DeviceState_rx_waypoint_tag 12 #define meshtastic_DeviceState_node_remote_hardware_pins_tag 13 #define meshtastic_DeviceState_node_db_lite_tag 14 -#define meshtastic_DeviceState_node_db_lite_count_tag 15 /* Struct field encoding specification for nanopb */ #define meshtastic_DeviceState_FIELDLIST(X, a) \ @@ -242,8 +240,7 @@ X(a, STATIC, SINGULAR, BOOL, no_save, 9) \ X(a, STATIC, SINGULAR, BOOL, did_gps_reset, 11) \ X(a, STATIC, OPTIONAL, MESSAGE, rx_waypoint, 12) \ X(a, STATIC, REPEATED, MESSAGE, node_remote_hardware_pins, 13) \ -X(a, CALLBACK, REPEATED, MESSAGE, node_db_lite, 14) \ -X(a, STATIC, SINGULAR, UINT32, node_db_lite_count, 15) +X(a, CALLBACK, REPEATED, MESSAGE, node_db_lite, 14) extern bool meshtastic_DeviceState_callback(pb_istream_t *istream, pb_ostream_t *ostream, const pb_field_t *field); #define meshtastic_DeviceState_CALLBACK meshtastic_DeviceState_callback #define meshtastic_DeviceState_DEFAULT NULL diff --git a/src/platform/portduino/PortduinoGlue.cpp b/src/platform/portduino/PortduinoGlue.cpp index 7f299ce38a..b255c0ce1a 100644 --- a/src/platform/portduino/PortduinoGlue.cpp +++ b/src/platform/portduino/PortduinoGlue.cpp @@ -199,9 +199,8 @@ void portduinoSetup() settingsMap[webserverport] = (yamlConfig["Webserver"]["Port"]).as(-1); settingsStrings[webserverrootpath] = (yamlConfig["Webserver"]["RootPath"]).as(""); } - // if (yamlConfig["General"]) { - settingsMap[maxnodes] = (yamlConfig["General"]["MaxNodes"]).as(150); - //} + + settingsMap[maxnodes] = (yamlConfig["General"]["MaxNodes"]).as(200); } catch (YAML::Exception e) { std::cout << "*** Exception " << e.what() << std::endl;