Skip to content

Commit

Permalink
It compiles...
Browse files Browse the repository at this point in the history
  • Loading branch information
jp-bennett committed Mar 17, 2024
1 parent e83a7fe commit 6fff34c
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 34 deletions.
58 changes: 38 additions & 20 deletions src/mesh/NodeDB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@
#include "mesh-pb-constants.h"
#include "modules/NeighborInfoModule.h"
#include <ErriezCRC32.h>
#include <algorithm>
#include <iostream>
#include <pb_decode.h>
#include <pb_encode.h>
#include <vector>

#ifdef ARCH_ESP32
#include "mesh/wifi/WiFiAPClient.h"
Expand Down Expand Up @@ -46,6 +49,16 @@ meshtastic_LocalModuleConfig moduleConfig;
meshtastic_ChannelFile channelFile;
meshtastic_OEMStore oemStore;

bool meshtastic_DeviceState_callback(pb_istream_t *istream, pb_ostream_t *ostream, const pb_field_iter_t *field)
{
std::vector<meshtastic_NodeInfoLite> *vec = (std::vector<meshtastic_NodeInfoLite> *)field->pData;

for (auto item : *vec) {
pb_encode_tag_for_field(ostream, field);
pb_encode_submessage(ostream, meshtastic_NodeInfoLite_fields, &item);
}
}

/** The current change # for radio settings. Starts at 0 on boot and any time the radio settings
* might have changed is incremented. Allows others to detect they might now be on a new channel.
*/
Expand All @@ -68,7 +81,12 @@ uint32_t error_address = 0;

static uint8_t ourMacAddr[6];

NodeDB::NodeDB() : meshNodes(devicestate.node_db_lite), numMeshNodes(&devicestate.node_db_lite_count) {}
NodeDB::NodeDB() : meshNodes(devicestate.node_db_lite), numMeshNodes(devicestate.node_db_lite.size())
{
std::cout << "test" << std::endl;
std::cout << MAX_NUM_NODES << std::endl;
meshNodes.reserve(MAX_NUM_NODES);
}

/**
* Most (but not always) of the time we want to treat packets 'from' the local phone (where from == 0), as if they originated on
Expand Down Expand Up @@ -359,8 +377,8 @@ void NodeDB::installDefaultChannels()

void NodeDB::resetNodes()
{
devicestate.node_db_lite_count = 1;
std::fill(&devicestate.node_db_lite[1], &devicestate.node_db_lite[MAX_NUM_NODES - 1], meshtastic_NodeInfoLite());
// devicestate.node_db_lite_count = 1;
std::fill(devicestate.node_db_lite.begin(), devicestate.node_db_lite.end(), meshtastic_NodeInfoLite());
saveDeviceStateToDisk();
if (neighborInfoModule && moduleConfig.neighbor_info.enabled)
neighborInfoModule->resetNeighbors();
Expand All @@ -369,27 +387,27 @@ void NodeDB::resetNodes()
void NodeDB::removeNodeByNum(uint nodeNum)
{
int newPos = 0, removed = 0;
for (int i = 0; i < *numMeshNodes; i++) {
for (int i = 0; i < numMeshNodes; i++) {
if (meshNodes[i].num != nodeNum)
meshNodes[newPos++] = meshNodes[i];
else
removed++;
}
*numMeshNodes -= removed;
numMeshNodes -= removed;
LOG_DEBUG("NodeDB::removeNodeByNum purged %d entries. Saving changes...\n", removed);
saveDeviceStateToDisk();
}

void NodeDB::cleanupMeshDB()
{
int newPos = 0, removed = 0;
for (int i = 0; i < *numMeshNodes; i++) {
for (int i = 0; i < numMeshNodes; i++) {
if (meshNodes[i].has_user)
meshNodes[newPos++] = meshNodes[i];
else
removed++;
}
*numMeshNodes -= removed;
numMeshNodes -= removed;
LOG_DEBUG("cleanupMeshDB purged %d entries\n", removed);
}

Expand All @@ -398,12 +416,12 @@ void NodeDB::installDefaultDeviceState()
LOG_INFO("Installing default DeviceState\n");
memset(&devicestate, 0, sizeof(meshtastic_DeviceState));

*numMeshNodes = 0;
numMeshNodes = 0;

// init our devicestate with valid flags so protobuf writing/reading will work
devicestate.has_my_node = true;
devicestate.has_owner = true;
devicestate.node_db_lite_count = 0;
// devicestate.node_db_lite_count = 0;
devicestate.version = DEVICESTATE_CUR_VER;
devicestate.receive_queue_count = 0; // Not yet implemented FIXME

Expand Down Expand Up @@ -454,7 +472,7 @@ void NodeDB::init()
#endif

resetRadioConfig(); // If bogus settings got saved, then fix them
LOG_DEBUG("region=%d, NODENUM=0x%x, dbsize=%d\n", config.lora.region, myNodeInfo.my_node_num, *numMeshNodes);
LOG_DEBUG("region=%d, NODENUM=0x%x, dbsize=%d\n", config.lora.region, myNodeInfo.my_node_num, numMeshNodes);

if (devicestateCRC != crc32Buffer(&devicestate, sizeof(devicestate)))
saveWhat |= SEGMENT_DEVICESTATE;
Expand Down Expand Up @@ -543,7 +561,7 @@ 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, meshtastic_DeviceState_size, sizeof(meshtastic_DeviceState), &meshtastic_DeviceState_msg,
if (!loadProto(prefFileName, sizeof(meshtastic_DeviceState), sizeof(meshtastic_DeviceState), &meshtastic_DeviceState_msg,
&devicestate)) {
installDefaultDeviceState(); // Our in RAM copy might now be corrupt
} else {
Expand Down Expand Up @@ -658,7 +676,7 @@ void NodeDB::saveDeviceStateToDisk()
#ifdef FSCom
FSCom.mkdir("/prefs");
#endif
saveProto(prefFileName, meshtastic_DeviceState_size, &meshtastic_DeviceState_msg, &devicestate);
saveProto(prefFileName, sizeof(meshtastic_DeviceState), &meshtastic_DeviceState_msg, &devicestate);
}
}

Expand Down Expand Up @@ -704,7 +722,7 @@ void NodeDB::saveToDisk(int saveWhat)

const meshtastic_NodeInfoLite *NodeDB::readNextMeshNode(uint32_t &readIndex)
{
if (readIndex < *numMeshNodes)
if (readIndex < numMeshNodes)
return &meshNodes[readIndex++];
else
return NULL;
Expand Down Expand Up @@ -740,7 +758,7 @@ size_t NodeDB::getNumOnlineMeshNodes(bool localOnly)
size_t numseen = 0;

// FIXME this implementation is kinda expensive
for (int i = 0; i < *numMeshNodes; i++) {
for (int i = 0; i < numMeshNodes; i++) {
if (localOnly && meshNodes[i].via_mqtt)
continue;
if (sinceLastSeen(&meshNodes[i]) < NUM_ONLINE_SECS)
Expand Down Expand Up @@ -891,7 +909,7 @@ uint8_t NodeDB::getMeshNodeChannel(NodeNum n)
/// NOTE: This function might be called from an ISR
meshtastic_NodeInfoLite *NodeDB::getMeshNode(NodeNum n)
{
for (int i = 0; i < *numMeshNodes; i++)
for (int i = 0; i < numMeshNodes; i++)
if (meshNodes[i].num == n)
return &meshNodes[i];

Expand All @@ -904,27 +922,27 @@ meshtastic_NodeInfoLite *NodeDB::getOrCreateMeshNode(NodeNum n)
meshtastic_NodeInfoLite *lite = getMeshNode(n);

if (!lite) {
if ((*numMeshNodes >= MAX_NUM_NODES) || (memGet.getFreeHeap() < meshtastic_NodeInfoLite_size * 3)) {
if ((numMeshNodes >= MAX_NUM_NODES) || (memGet.getFreeHeap() < meshtastic_NodeInfoLite_size * 3)) {
if (screen)
screen->print("Warn: node database full!\nErasing oldest entry\n");
LOG_INFO("Warn: node database full!\nErasing oldest entry\n");
// look for oldest node and erase it
uint32_t oldest = UINT32_MAX;
int oldestIndex = -1;
for (int i = 1; i < *numMeshNodes; i++) {
for (int i = 1; i < numMeshNodes; i++) {
if (meshNodes[i].last_heard < oldest) {
oldest = meshNodes[i].last_heard;
oldestIndex = i;
}
}
// Shove the remaining nodes down the chain
for (int i = oldestIndex; i < *numMeshNodes - 1; i++) {
for (int i = oldestIndex; i < numMeshNodes - 1; i++) {
meshNodes[i] = meshNodes[i + 1];
}
(*numMeshNodes)--;
(numMeshNodes)--;
}
// add the node at the end
lite = &meshNodes[(*numMeshNodes)++];
lite = &meshNodes[(numMeshNodes)++];

// everything is missing except the nodenum
memset(lite, 0, sizeof(*lite));
Expand Down
9 changes: 5 additions & 4 deletions src/mesh/NodeDB.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "Observer.h"
#include <Arduino.h>
#include <assert.h>
#include <vector>

#include "MeshTypes.h"
#include "NodeStatus.h"
Expand Down Expand Up @@ -45,8 +46,8 @@ class NodeDB
// Eventually use a smarter datastructure
// HashMap<NodeNum, NodeInfo> nodes;
// Note: these two references just point into our static array we serialize to/from disk
meshtastic_NodeInfoLite *meshNodes;
pb_size_t *numMeshNodes;
std::vector<meshtastic_NodeInfoLite> meshNodes;
pb_size_t numMeshNodes;

public:
bool updateGUI = false; // we think the gui should definitely be redrawn, screen will clear this once handled
Expand Down Expand Up @@ -126,12 +127,12 @@ class NodeDB

meshtastic_NodeInfoLite *getMeshNodeByIndex(size_t x)
{
assert(x < *numMeshNodes);
assert(x < numMeshNodes);
return &meshNodes[x];
}

meshtastic_NodeInfoLite *getMeshNode(NodeNum n);
size_t getNumMeshNodes() { return *numMeshNodes; }
size_t getNumMeshNodes() { return numMeshNodes; }

void setLocalPosition(meshtastic_Position position, bool timeOnly = false)
{
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#error Regenerate this file with the current version of nanopb generator.
#endif

PB_BIND(meshtastic_DeviceState, meshtastic_DeviceState, 4)
PB_BIND(meshtastic_DeviceState, meshtastic_DeviceState, 2)


PB_BIND(meshtastic_NodeInfoLite, meshtastic_NodeInfoLite, AUTO)
Expand Down
Loading

0 comments on commit 6fff34c

Please sign in to comment.