firmware/src/NodeDB.cpp

114 lines
2.9 KiB
C++
Raw Normal View History

2020-02-03 17:13:19 +00:00
#include <Arduino.h>
#include <assert.h>
#include <pb_encode.h>
#include <pb_decode.h>
#include "configuration.h"
2020-02-03 17:13:19 +00:00
#include "mesh-pb-constants.h"
#include "NodeDB.h"
2020-02-03 19:53:38 +00:00
MyNodeInfo myNodeInfo = MyNodeInfo_init_zero;
2020-02-03 17:13:19 +00:00
NodeDB nodeDB;
2020-02-03 19:53:38 +00:00
User owner = User_init_zero;
2020-02-03 17:13:19 +00:00
/**
* get our starting (provisional) nodenum from flash. But check first if anyone else is using it, by trying to send a message to it (arping)
*/
static NodeNum getDesiredNodeNum()
{
uint8_t dmac[6];
esp_efuse_mac_get_default(dmac);
// FIXME not the right way to guess node numes
uint8_t r = dmac[5];
assert(r != 0xff); // It better not be the broadcast address
return r;
}
2020-02-04 05:03:20 +00:00
NodeDB::NodeDB() : ourNodeNum(getDesiredNodeNum())
2020-02-03 17:13:19 +00:00
{
}
/// return number msecs since 1970
2020-02-04 05:03:20 +00:00
uint64_t getCurrentTime()
{
2020-02-03 17:13:19 +00:00
return 4403; // FIXME
}
2020-02-04 05:03:20 +00:00
const NodeInfo *NodeDB::readNextInfo()
{
if (readPointer < numNodes)
return &nodes[readPointer++];
else
return NULL;
}
2020-02-03 17:13:19 +00:00
/// given a subpacket sniffed from the network, update our DB state
/// we updateGUI and updateGUIforNode if we think our this change is big enough for a redraw
void NodeDB::updateFrom(const MeshPacket &mp)
{
if (mp.has_payload)
{
const SubPacket &p = mp.payload;
DEBUG_MSG("Update DB node %x for %d\n", mp.from, p.which_variant);
2020-02-03 17:13:19 +00:00
if (p.which_variant != SubPacket_want_node_tag) // we don't create nodeinfo records for someone that is just trying to claim a nodenum
{
int oldNumNodes = numNodes;
NodeInfo *info = getOrCreateNode(mp.from);
if (oldNumNodes != numNodes)
updateGUI = true; // we just created a nodeinfo
info->last_seen.msecs = getCurrentTime();
info->has_last_seen = true;
2020-02-04 05:03:20 +00:00
2020-02-03 17:13:19 +00:00
switch (p.which_variant)
{
case SubPacket_position_tag:
info->position = p.variant.position;
info->has_position = true;
updateGUIforNode = info;
break;
case SubPacket_user_tag:
info->user = p.variant.user;
info->has_user = true;
updateGUIforNode = info;
break;
default:
break; // Ignore other packet types
}
}
}
}
/// Find a node in our DB, return null for missing
NodeInfo *NodeDB::getNode(NodeNum n)
{
for (int i = 0; i < numNodes; i++)
if (nodes[i].num == n)
return &nodes[i];
return NULL;
}
/// Find a node in our DB, create an empty NodeInfo if missing
NodeInfo *NodeDB::getOrCreateNode(NodeNum n)
{
NodeInfo *info = getNode(n);
if (!n)
{
// add the node
assert(numNodes < MAX_NUM_NODES);
info = &nodes[numNodes++];
// everything is missing except the nodenum
memset(info, 0, sizeof(*info));
info->num = n;
}
return info;
}