2020-02-03 17:13:19 +00:00
|
|
|
#pragma once
|
|
|
|
|
2020-08-12 18:04:03 +00:00
|
|
|
#include "Observer.h"
|
2020-02-03 17:13:19 +00:00
|
|
|
#include <Arduino.h>
|
|
|
|
#include <assert.h>
|
2024-03-21 14:06:37 +00:00
|
|
|
#include <vector>
|
2020-02-03 17:13:19 +00:00
|
|
|
|
|
|
|
#include "MeshTypes.h"
|
2020-06-28 04:19:49 +00:00
|
|
|
#include "NodeStatus.h"
|
2020-08-12 18:04:03 +00:00
|
|
|
#include "mesh-pb-constants.h"
|
2023-06-08 13:07:32 +00:00
|
|
|
#include "mesh/generated/meshtastic/mesh.pb.h" // For CriticalErrorCode
|
2020-02-03 17:13:19 +00:00
|
|
|
|
2022-08-08 12:19:04 +00:00
|
|
|
/*
|
|
|
|
DeviceState versions used to be defined in the .proto file but really only this function cares. So changed to a
|
|
|
|
#define here.
|
|
|
|
*/
|
|
|
|
|
2022-10-04 12:32:07 +00:00
|
|
|
#define SEGMENT_CONFIG 1
|
|
|
|
#define SEGMENT_MODULECONFIG 2
|
|
|
|
#define SEGMENT_DEVICESTATE 4
|
|
|
|
#define SEGMENT_CHANNELS 8
|
|
|
|
|
2023-07-27 21:03:57 +00:00
|
|
|
#define DEVICESTATE_CUR_VER 22
|
2022-08-08 12:19:04 +00:00
|
|
|
#define DEVICESTATE_MIN_VER DEVICESTATE_CUR_VER
|
|
|
|
|
2023-01-21 17:22:19 +00:00
|
|
|
extern meshtastic_DeviceState devicestate;
|
|
|
|
extern meshtastic_ChannelFile channelFile;
|
|
|
|
extern meshtastic_MyNodeInfo &myNodeInfo;
|
|
|
|
extern meshtastic_LocalConfig config;
|
|
|
|
extern meshtastic_LocalModuleConfig moduleConfig;
|
|
|
|
extern meshtastic_OEMStore oemStore;
|
|
|
|
extern meshtastic_User &owner;
|
2023-06-17 14:10:09 +00:00
|
|
|
extern meshtastic_Position localPosition;
|
2020-02-04 21:47:42 +00:00
|
|
|
|
2020-02-12 19:52:53 +00:00
|
|
|
/// Given a node, return how many seconds in the past (vs now) that we last heard from it
|
2023-06-17 14:10:09 +00:00
|
|
|
uint32_t sinceLastSeen(const meshtastic_NodeInfoLite *n);
|
2020-02-12 19:52:53 +00:00
|
|
|
|
2023-02-02 23:40:51 +00:00
|
|
|
/// Given a packet, return how many seconds in the past (vs now) it was received
|
|
|
|
uint32_t sinceReceived(const meshtastic_MeshPacket *p);
|
|
|
|
|
2024-04-15 12:22:05 +00:00
|
|
|
enum LoadFileResult {
|
|
|
|
// Successfully opened the file
|
|
|
|
SUCCESS = 1,
|
|
|
|
// File does not exist
|
|
|
|
NOT_FOUND = 2,
|
|
|
|
// Device does not have a filesystem
|
|
|
|
NO_FILESYSTEM = 3,
|
|
|
|
// File exists, but could not decode protobufs
|
|
|
|
DECODE_FAILED = 4,
|
|
|
|
// File exists, but open failed for some reason
|
|
|
|
OTHER_FAILURE = 5
|
|
|
|
};
|
|
|
|
|
2020-02-04 05:03:20 +00:00
|
|
|
class NodeDB
|
|
|
|
{
|
2020-02-03 17:13:19 +00:00
|
|
|
// NodeNum provisionalNodeNum; // if we are trying to find a node num this is our current attempt
|
|
|
|
|
|
|
|
// A NodeInfo for every node we've seen
|
|
|
|
// Eventually use a smarter datastructure
|
|
|
|
// HashMap<NodeNum, NodeInfo> nodes;
|
2020-02-07 17:36:15 +00:00
|
|
|
// Note: these two references just point into our static array we serialize to/from disk
|
2023-06-17 14:10:09 +00:00
|
|
|
|
2020-03-19 02:15:51 +00:00
|
|
|
public:
|
2024-03-21 14:06:37 +00:00
|
|
|
std::vector<meshtastic_NodeInfoLite> *meshNodes;
|
2023-01-21 17:39:58 +00:00
|
|
|
bool updateGUI = false; // we think the gui should definitely be redrawn, screen will clear this once handled
|
2023-06-17 14:10:09 +00:00
|
|
|
meshtastic_NodeInfoLite *updateGUIforNode = NULL; // if currently showing this node, we think you should update the GUI
|
2020-06-29 01:17:52 +00:00
|
|
|
Observable<const meshtastic::NodeStatus *> newStatus;
|
2024-03-21 14:06:37 +00:00
|
|
|
pb_size_t numMeshNodes;
|
2020-02-13 03:58:44 +00:00
|
|
|
|
2023-07-14 21:25:20 +00:00
|
|
|
/// don't do mesh based algorithm for node id assignment (initially)
|
2020-02-03 17:13:19 +00:00
|
|
|
/// instead just store in flash - possibly even in the initial alpha release do this hack
|
|
|
|
NodeDB();
|
|
|
|
|
2020-02-07 17:36:15 +00:00
|
|
|
/// write to flash
|
2023-01-21 13:34:29 +00:00
|
|
|
void saveToDisk(int saveWhat = SEGMENT_CONFIG | SEGMENT_MODULECONFIG | SEGMENT_DEVICESTATE | SEGMENT_CHANNELS),
|
|
|
|
saveChannelsToDisk(), saveDeviceStateToDisk();
|
2020-02-07 17:36:15 +00:00
|
|
|
|
2020-09-19 18:19:42 +00:00
|
|
|
/** Reinit radio config if needed, because either:
|
2020-10-06 01:43:00 +00:00
|
|
|
* a) sometimes a buggy android app might send us bogus settings or
|
2020-09-19 18:19:42 +00:00
|
|
|
* b) the client set factory_reset
|
2020-10-06 01:43:00 +00:00
|
|
|
*
|
2020-09-19 18:19:42 +00:00
|
|
|
* @return true if the config was completely reset, in that case, we should send it back to the client
|
|
|
|
*/
|
2022-09-09 10:51:41 +00:00
|
|
|
bool resetRadioConfig(bool factory_reset = false);
|
2020-03-30 23:05:28 +00:00
|
|
|
|
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
|
2023-01-21 17:22:19 +00:00
|
|
|
void updateFrom(const meshtastic_MeshPacket &p);
|
2020-02-03 17:13:19 +00:00
|
|
|
|
2020-12-03 08:48:44 +00:00
|
|
|
/** Update position info for this node based on received position data
|
|
|
|
*/
|
2023-01-21 17:22:19 +00:00
|
|
|
void updatePosition(uint32_t nodeId, const meshtastic_Position &p, RxSource src = RX_SRC_RADIO);
|
2020-12-03 08:48:44 +00:00
|
|
|
|
2022-03-20 14:55:38 +00:00
|
|
|
/** Update telemetry info for this node based on received metrics
|
|
|
|
*/
|
2023-01-21 17:22:19 +00:00
|
|
|
void updateTelemetry(uint32_t nodeId, const meshtastic_Telemetry &t, RxSource src = RX_SRC_RADIO);
|
2022-03-20 14:55:38 +00:00
|
|
|
|
2024-01-16 02:11:35 +00:00
|
|
|
/** Update user info and channel for this node based on received user data
|
2020-12-05 02:00:46 +00:00
|
|
|
*/
|
2024-01-16 02:11:35 +00:00
|
|
|
bool updateUser(uint32_t nodeId, const meshtastic_User &p, uint8_t channelIndex = 0);
|
2020-12-05 02:00:46 +00:00
|
|
|
|
2020-02-18 00:32:51 +00:00
|
|
|
/// @return our node number
|
2020-02-04 21:47:42 +00:00
|
|
|
NodeNum getNodeNum() { return myNodeInfo.my_node_num; }
|
2020-02-03 17:13:19 +00:00
|
|
|
|
|
|
|
/// if returns false, that means our node should send a DenyNodeNum response. If true, we think the number is okay for use
|
|
|
|
// bool handleWantNodeNum(NodeNum n);
|
|
|
|
|
|
|
|
/* void handleDenyNodeNum(NodeNum FIXME read mesh proto docs, perhaps picking a random node num is not a great idea
|
2020-03-19 02:15:51 +00:00
|
|
|
and instead we should use a special 'im unconfigured node number' and include our desired node number in the wantnum message.
|
|
|
|
the unconfigured node num would only be used while initially joining the mesh so low odds of conflicting (especially if we
|
|
|
|
randomly select from a small number of nodenums which can be used temporarily for this operation). figure out what the lower
|
|
|
|
level mesh sw does if it does conflict? would it be better for people who are replying with denynode num to just broadcast
|
|
|
|
their denial?)
|
2020-02-03 17:13:19 +00:00
|
|
|
*/
|
|
|
|
|
2020-02-08 20:42:54 +00:00
|
|
|
/// pick a provisional nodenum we hope no one is using
|
|
|
|
void pickNewNodeNum();
|
|
|
|
|
2023-03-29 11:51:22 +00:00
|
|
|
// get channel channel index we heard a nodeNum on, defaults to 0 if not found
|
2023-06-17 14:10:09 +00:00
|
|
|
uint8_t getMeshNodeChannel(NodeNum n);
|
2020-02-11 18:51:45 +00:00
|
|
|
|
2024-03-10 13:52:37 +00:00
|
|
|
/* Return the number of nodes we've heard from recently (within the last 2 hrs?)
|
|
|
|
* @param localOnly if true, ignore nodes heard via MQTT
|
|
|
|
*/
|
|
|
|
size_t getNumOnlineMeshNodes(bool localOnly = false);
|
2020-02-11 19:56:48 +00:00
|
|
|
|
2024-05-03 20:49:22 +00:00
|
|
|
void initConfigIntervals(), initModuleConfigIntervals(), resetNodes(), removeNodeByNum(NodeNum nodeNum);
|
2023-01-21 13:34:29 +00:00
|
|
|
|
2022-09-16 16:00:05 +00:00
|
|
|
bool factoryReset();
|
2022-09-10 17:35:36 +00:00
|
|
|
|
2024-04-15 12:22:05 +00:00
|
|
|
LoadFileResult loadProto(const char *filename, size_t protoSize, size_t objSize, const pb_msgdesc_t *fields,
|
|
|
|
void *dest_struct);
|
2022-12-29 15:53:36 +00:00
|
|
|
bool saveProto(const char *filename, size_t protoSize, const pb_msgdesc_t *fields, const void *dest_struct);
|
|
|
|
|
2023-01-28 20:32:57 +00:00
|
|
|
void installRoleDefaults(meshtastic_Config_DeviceConfig_Role role);
|
|
|
|
|
2023-06-17 14:10:09 +00:00
|
|
|
const meshtastic_NodeInfoLite *readNextMeshNode(uint32_t &readIndex);
|
|
|
|
|
|
|
|
meshtastic_NodeInfoLite *getMeshNodeByIndex(size_t x)
|
|
|
|
{
|
2024-03-21 14:06:37 +00:00
|
|
|
assert(x < numMeshNodes);
|
|
|
|
return &meshNodes->at(x);
|
2023-06-17 14:10:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
meshtastic_NodeInfoLite *getMeshNode(NodeNum n);
|
2024-03-21 14:06:37 +00:00
|
|
|
size_t getNumMeshNodes() { return numMeshNodes; }
|
2023-06-17 14:10:09 +00:00
|
|
|
|
2024-03-21 19:43:10 +00:00
|
|
|
void clearLocalPosition();
|
|
|
|
|
2024-03-14 01:27:26 +00:00
|
|
|
void setLocalPosition(meshtastic_Position position, bool timeOnly = false)
|
2023-10-09 23:33:04 +00:00
|
|
|
{
|
2024-03-14 01:27:26 +00:00
|
|
|
if (timeOnly) {
|
2024-04-21 17:36:37 +00:00
|
|
|
LOG_DEBUG("Setting local position time only: time=%u timestamp=%u\n", position.time, position.timestamp);
|
2024-03-14 01:27:26 +00:00
|
|
|
localPosition.time = position.time;
|
2024-04-21 13:45:36 +00:00
|
|
|
localPosition.timestamp = position.timestamp > 0 ? position.timestamp : position.time;
|
2024-03-14 01:27:26 +00:00
|
|
|
return;
|
|
|
|
}
|
2024-04-21 17:36:37 +00:00
|
|
|
LOG_DEBUG("Setting local position: latitude=%i, longitude=%i, time=%u, timestamp=%u\n", position.latitude_i,
|
2024-04-21 13:45:36 +00:00
|
|
|
position.longitude_i, position.time, position.timestamp);
|
2023-10-09 23:33:04 +00:00
|
|
|
localPosition = position;
|
|
|
|
}
|
|
|
|
|
2020-03-19 02:15:51 +00:00
|
|
|
private:
|
2024-04-18 19:20:39 +00:00
|
|
|
uint32_t lastNodeDbSave = 0; // when we last saved our db to flash
|
2023-06-17 14:10:09 +00:00
|
|
|
/// Find a node in our DB, create an empty NodeInfoLite if missing
|
|
|
|
meshtastic_NodeInfoLite *getOrCreateMeshNode(NodeNum n);
|
2020-02-07 17:36:15 +00:00
|
|
|
|
2020-06-28 04:19:49 +00:00
|
|
|
/// Notify observers of changes to the DB
|
2020-08-12 18:04:03 +00:00
|
|
|
void notifyObservers(bool forceUpdate = false)
|
|
|
|
{
|
2020-06-28 04:19:49 +00:00
|
|
|
// Notify observers of the current node state
|
2023-06-17 14:10:09 +00:00
|
|
|
const meshtastic::NodeStatus status = meshtastic::NodeStatus(getNumOnlineMeshNodes(), getNumMeshNodes(), forceUpdate);
|
2020-06-29 01:17:52 +00:00
|
|
|
newStatus.notifyObservers(&status);
|
2020-06-28 04:19:49 +00:00
|
|
|
}
|
|
|
|
|
2020-02-07 17:36:15 +00:00
|
|
|
/// read our db from flash
|
|
|
|
void loadFromDisk();
|
2020-06-16 22:02:11 +00:00
|
|
|
|
2023-09-18 11:16:37 +00:00
|
|
|
/// purge db entries without user info
|
|
|
|
void cleanupMeshDB();
|
|
|
|
|
2020-06-16 22:02:11 +00:00
|
|
|
/// Reinit device state from scratch (not loading from disk)
|
2023-01-21 13:34:29 +00:00
|
|
|
void installDefaultDeviceState(), installDefaultChannels(), installDefaultConfig(), installDefaultModuleConfig();
|
2020-02-03 17:13:19 +00:00
|
|
|
};
|
|
|
|
|
2024-03-21 14:06:37 +00:00
|
|
|
extern NodeDB *nodeDB;
|
2020-08-12 18:04:03 +00:00
|
|
|
|
2020-12-21 03:38:03 +00:00
|
|
|
/*
|
|
|
|
If is_router is set, we use a number of different default values
|
|
|
|
|
2022-06-13 00:56:32 +00:00
|
|
|
# FIXME - after tuning, move these params into the on-device defaults based on is_router and is_power_saving
|
2020-12-21 03:38:03 +00:00
|
|
|
|
|
|
|
# prefs.position_broadcast_secs = FIXME possibly broadcast only once an hr
|
|
|
|
prefs.wait_bluetooth_secs = 1 # Don't stay in bluetooth mode
|
|
|
|
# try to stay in light sleep one full day, then briefly wake and sleep again
|
|
|
|
|
|
|
|
prefs.ls_secs = oneday
|
|
|
|
|
|
|
|
prefs.position_broadcast_secs = 12 hours # send either position or owner every 12hrs
|
2022-05-02 12:00:24 +00:00
|
|
|
|
2020-12-21 03:38:03 +00:00
|
|
|
# get a new GPS position once per day
|
|
|
|
prefs.gps_update_interval = oneday
|
|
|
|
|
2022-06-13 00:56:32 +00:00
|
|
|
prefs.is_power_saving = True
|
2020-12-21 03:38:03 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
// Our delay functions check for this for times that should never expire
|
2021-03-04 03:28:50 +00:00
|
|
|
#define NODE_DELAY_FOREVER 0xffffffff
|
2020-12-21 03:38:03 +00:00
|
|
|
|
2023-05-30 10:26:34 +00:00
|
|
|
/// Sometimes we will have Position objects that only have a time, so check for
|
|
|
|
/// valid lat/lon
|
2023-06-17 14:10:09 +00:00
|
|
|
static inline bool hasValidPosition(const meshtastic_NodeInfoLite *n)
|
|
|
|
{
|
|
|
|
return n->has_position && (n->position.latitude_i != 0 || n->position.longitude_i != 0);
|
|
|
|
}
|
2023-05-30 10:26:34 +00:00
|
|
|
|
2022-05-02 12:00:24 +00:00
|
|
|
/** The current change # for radio settings. Starts at 0 on boot and any time the radio settings
|
2020-12-17 02:53:29 +00:00
|
|
|
* might have changed is incremented. Allows others to detect they might now be on a new channel.
|
|
|
|
*/
|
|
|
|
extern uint32_t radioGeneration;
|
2021-03-05 02:19:27 +00:00
|
|
|
|
2023-06-08 13:07:32 +00:00
|
|
|
extern meshtastic_CriticalErrorCode error_code;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A numeric error address (nonzero if available)
|
|
|
|
*/
|
|
|
|
extern uint32_t error_address;
|
|
|
|
|
2023-01-21 13:34:29 +00:00
|
|
|
#define Module_Config_size \
|
|
|
|
(ModuleConfig_CannedMessageConfig_size + ModuleConfig_ExternalNotificationConfig_size + ModuleConfig_MQTTConfig_size + \
|
|
|
|
ModuleConfig_RangeTestConfig_size + ModuleConfig_SerialConfig_size + ModuleConfig_StoreForwardConfig_size + \
|
2024-03-19 11:22:45 +00:00
|
|
|
ModuleConfig_TelemetryConfig_size + ModuleConfig_size)
|
|
|
|
|
2024-04-18 19:20:39 +00:00
|
|
|
// Please do not remove this comment, it makes trunk and compiler happy at the same time.
|