firmware/src/NodeDB.cpp

238 lines
6.7 KiB
C++
Raw Normal View History

2020-02-03 17:13:19 +00:00
#include <Arduino.h>
#include <assert.h>
2020-02-07 17:36:15 +00:00
#include "FS.h"
#include "SPIFFS.h"
2020-02-03 17:13:19 +00:00
#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-06 16:18:20 +00:00
#include "GPS.h"
2020-02-03 17:13:19 +00:00
NodeDB nodeDB;
2020-02-04 17:00:17 +00:00
2020-02-07 17:36:15 +00:00
// we have plenty of ram so statically alloc this tempbuf (for now)
DeviceState devicestate;
MyNodeInfo &myNodeInfo = devicestate.my_node;
RadioConfig &radioConfig = devicestate.radio;
#define FS SPIFFS
2020-02-04 17:00:17 +00:00
/**
*
* Normally userids are unique and start with +country code to look like Signal phone numbers.
* But there are some special ids used when we haven't yet been configured by a user. In that case
* we use !macaddr (no colons).
*/
2020-02-07 17:36:15 +00:00
User &owner = devicestate.owner;
2020-02-04 17:00:17 +00:00
static uint8_t ourMacAddr[6];
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()
{
2020-02-04 17:00:17 +00:00
esp_efuse_mac_get_default(ourMacAddr);
2020-02-03 17:13:19 +00:00
// FIXME not the right way to guess node numes
2020-02-04 17:00:17 +00:00
uint8_t r = ourMacAddr[5];
2020-02-06 18:58:19 +00:00
assert(r != 0xff && r != 0); // It better not be the broadcast address or zero
2020-02-03 17:13:19 +00:00
return r;
}
2020-02-07 17:36:15 +00:00
NodeDB::NodeDB() : nodes(devicestate.node_db), numNodes(&devicestate.node_db_count)
2020-02-04 17:00:17 +00:00
{
}
2020-02-07 17:36:15 +00:00
void NodeDB::init()
{
// Crummy guess at our nodenum
2020-02-04 21:47:42 +00:00
myNodeInfo.my_node_num = getDesiredNodeNum();
2020-02-04 17:00:17 +00:00
2020-02-07 17:36:15 +00:00
// init our devicestate with valid flags so protobuf writing/reading will work
devicestate.has_my_node = true;
devicestate.has_radio = true;
devicestate.has_owner = true;
devicestate.node_db_count = 0;
devicestate.receive_queue_count = 0;
2020-02-04 17:00:17 +00:00
// Init our blank owner info to reasonable defaults
sprintf(owner.id, "!%02x%02x%02x%02x%02x%02x", ourMacAddr[0],
ourMacAddr[1], ourMacAddr[2], ourMacAddr[3], ourMacAddr[4], ourMacAddr[5]);
memcpy(owner.macaddr, ourMacAddr, sizeof(owner.macaddr));
2020-02-06 18:58:19 +00:00
sprintf(owner.long_name, "Unknown %02x%02x", ourMacAddr[4], ourMacAddr[5]);
sprintf(owner.short_name, "?%02X", ourMacAddr[5]);
2020-02-04 17:00:17 +00:00
// FIXME, read owner info from flash
// Include our owner in the node db under our nodenum
2020-02-04 21:47:42 +00:00
NodeInfo *info = getOrCreateNode(getNodeNum());
2020-02-04 17:00:17 +00:00
info->user = owner;
info->has_user = true;
2020-02-06 16:18:20 +00:00
info->last_seen = 0; // haven't heard a real message yet
2020-02-07 17:36:15 +00:00
if (!FS.begin(true)) // FIXME - do this in main?
{
DEBUG_MSG("ERROR SPIFFS Mount Failed\n");
// FIXME - report failure to phone
}
// saveToDisk();
loadFromDisk();
DEBUG_MSG("NODENUM=0x%x, dbsize=%d\n", myNodeInfo.my_node_num, *numNodes);
}
const char *preffile = "/db.proto";
const char *preftmp = "/db.proto.tmp";
void NodeDB::loadFromDisk()
{
static DeviceState scratch;
2020-02-07 17:36:15 +00:00
File f = FS.open(preffile);
if (f)
{
DEBUG_MSG("Loading saved preferences\n");
pb_istream_t stream = {&readcb, &f, DeviceState_size};
scratch = DeviceState_init_zero;
if (!pb_decode(&stream, DeviceState_fields, &scratch))
2020-02-07 17:36:15 +00:00
{
DEBUG_MSG("Error: can't decode protobuf %s\n", PB_GET_ERROR(&stream));
// FIXME - report failure to phone
}
else {
if(scratch.version < DeviceState_Version_Minimum)
DEBUG_MSG("Warn: devicestate is old, discarding\n");
else
devicestate = scratch;
}
2020-02-07 17:36:15 +00:00
f.close();
}
else
{
DEBUG_MSG("No saved preferences found\n");
}
}
void NodeDB::saveToDisk()
{
File f = FS.open(preftmp, "w");
if (f)
{
DEBUG_MSG("Writing preferences\n");
pb_ostream_t stream = {&writecb, &f, DeviceState_size, 0};
devicestate.version = DeviceState_Version_Current;
2020-02-07 17:36:15 +00:00
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();
// brief window of risk here ;-)
FS.remove(preffile);
FS.rename(preftmp, preffile);
}
else
{
DEBUG_MSG("ERROR: can't write prefs\n"); // FIXME report to app
}
2020-02-04 17:00:17 +00:00
}
2020-02-04 05:03:20 +00:00
const NodeInfo *NodeDB::readNextInfo()
{
2020-02-07 17:36:15 +00:00
if (readPointer < *numNodes)
2020-02-04 05:03:20 +00:00
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 0x%x for variant %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
{
2020-02-07 17:36:15 +00:00
int oldNumNodes = *numNodes;
2020-02-03 17:13:19 +00:00
NodeInfo *info = getOrCreateNode(mp.from);
2020-02-07 17:36:15 +00:00
if (oldNumNodes != *numNodes)
2020-02-03 17:13:19 +00:00
updateGUI = true; // we just created a nodeinfo
2020-02-06 16:18:20 +00:00
info->last_seen = gps.getTime();
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:
2020-02-07 17:36:15 +00:00
{
DEBUG_MSG("old user %s/%s/%s\n", info->user.id, info->user.long_name, info->user.short_name);
bool changed = memcmp(&info->user, &p.variant.user, sizeof(info->user)); // Both of these blocks start as filled with zero so I think this is okay
2020-02-03 17:13:19 +00:00
info->user = p.variant.user;
2020-02-07 17:36:15 +00:00
DEBUG_MSG("updating changed=%d user %s/%s/%s\n", changed, info->user.id, info->user.long_name, info->user.short_name);
2020-02-03 17:13:19 +00:00
info->has_user = true;
updateGUIforNode = info;
2020-02-07 17:36:15 +00:00
if (changed)
{
// We just created a user for the first time, store our DB
saveToDisk();
}
2020-02-03 17:13:19 +00:00
break;
2020-02-07 17:36:15 +00:00
}
2020-02-03 17:13:19 +00:00
default:
break; // Ignore other packet types
}
}
}
}
/// Find a node in our DB, return null for missing
NodeInfo *NodeDB::getNode(NodeNum n)
{
2020-02-07 17:36:15 +00:00
for (int i = 0; i < *numNodes; i++)
2020-02-03 17:13:19 +00:00
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);
2020-02-04 17:00:17 +00:00
if (!info)
2020-02-03 17:13:19 +00:00
{
// add the node
2020-02-07 17:36:15 +00:00
assert(*numNodes < MAX_NUM_NODES);
info = &nodes[(*numNodes)++];
2020-02-03 17:13:19 +00:00
// everything is missing except the nodenum
memset(info, 0, sizeof(*info));
info->num = n;
}
return info;
}