2021-01-31 17:12:01 +00:00
|
|
|
#include "StoreForwardPlugin.h"
|
|
|
|
#include "MeshService.h"
|
|
|
|
#include "NodeDB.h"
|
|
|
|
#include "RTC.h"
|
|
|
|
#include "Router.h"
|
|
|
|
#include "configuration.h"
|
2021-02-21 07:53:53 +00:00
|
|
|
#include "mesh-pb-constants.h"
|
2021-01-31 17:12:01 +00:00
|
|
|
#include <Arduino.h>
|
2021-02-17 01:42:46 +00:00
|
|
|
#include <map>
|
2021-01-31 17:12:01 +00:00
|
|
|
|
|
|
|
StoreForwardPlugin *storeForwardPlugin;
|
|
|
|
StoreForwardPluginRadio *storeForwardPluginRadio;
|
2021-02-14 16:44:49 +00:00
|
|
|
|
2021-02-14 21:31:11 +00:00
|
|
|
StoreForwardPlugin::StoreForwardPlugin() : concurrency::OSThread("StoreForwardPlugin") {}
|
2021-01-31 17:12:01 +00:00
|
|
|
|
|
|
|
int32_t StoreForwardPlugin::runOnce()
|
|
|
|
{
|
2021-02-15 04:13:52 +00:00
|
|
|
|
2021-01-31 17:12:01 +00:00
|
|
|
#ifndef NO_ESP32
|
|
|
|
|
2021-02-07 07:29:18 +00:00
|
|
|
/*
|
|
|
|
Uncomment the preferences below if you want to use the plugin
|
|
|
|
without having to configure it from the PythonAPI or WebUI.
|
|
|
|
*/
|
|
|
|
|
2021-02-21 07:53:53 +00:00
|
|
|
radioConfig.preferences.store_forward_plugin_enabled = 1;
|
|
|
|
radioConfig.preferences.is_router = 1;
|
2021-02-15 04:13:52 +00:00
|
|
|
|
|
|
|
if (radioConfig.preferences.store_forward_plugin_enabled) {
|
2021-01-31 17:12:01 +00:00
|
|
|
|
|
|
|
if (firstTime) {
|
|
|
|
|
2021-02-15 04:13:52 +00:00
|
|
|
/*
|
|
|
|
*/
|
2021-01-31 17:12:01 +00:00
|
|
|
|
2021-02-15 04:13:52 +00:00
|
|
|
if (radioConfig.preferences.is_router) {
|
|
|
|
DEBUG_MSG("Initializing Store & Forward Plugin - Enabled\n");
|
|
|
|
// Router
|
|
|
|
if (ESP.getPsramSize()) {
|
|
|
|
if (ESP.getFreePsram() >= 1024 * 1024) {
|
|
|
|
// Do the startup here
|
|
|
|
storeForwardPluginRadio = new StoreForwardPluginRadio();
|
2021-01-31 17:12:01 +00:00
|
|
|
|
2021-02-15 04:13:52 +00:00
|
|
|
firstTime = 0;
|
2021-01-31 17:12:01 +00:00
|
|
|
|
2021-02-21 07:53:53 +00:00
|
|
|
return (10 * 1000);
|
|
|
|
|
2021-02-15 04:13:52 +00:00
|
|
|
} else {
|
|
|
|
DEBUG_MSG("Device has less than 1M of PSRAM free. Aborting startup.\n");
|
|
|
|
DEBUG_MSG("Store & Forward Plugin - Aborting Startup.\n");
|
|
|
|
|
|
|
|
return (INT32_MAX);
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
DEBUG_MSG("Device doesn't have PSRAM.\n");
|
|
|
|
DEBUG_MSG("Store & Forward Plugin - Aborting Startup.\n");
|
2021-02-14 22:01:08 +00:00
|
|
|
|
2021-02-15 04:13:52 +00:00
|
|
|
return (INT32_MAX);
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
DEBUG_MSG("Initializing Store & Forward Plugin - Enabled but is_router is not turned on.\n");
|
|
|
|
DEBUG_MSG(
|
|
|
|
"Initializing Store & Forward Plugin - If you want to use this plugin, you must also turn on is_router.\n");
|
|
|
|
// Non-Router
|
2021-02-17 01:42:46 +00:00
|
|
|
|
|
|
|
return (30 * 1000);
|
2021-02-15 04:13:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2021-02-17 01:42:46 +00:00
|
|
|
// What do we do if it's not our first time?
|
|
|
|
|
|
|
|
// Maybe some cleanup functions?
|
2021-02-21 07:53:53 +00:00
|
|
|
this->sawNodeReport();
|
|
|
|
return (10 * 1000);
|
2021-01-31 17:12:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2021-02-15 04:13:52 +00:00
|
|
|
DEBUG_MSG("Store & Forward Plugin - Disabled\n");
|
2021-01-31 17:12:01 +00:00
|
|
|
|
|
|
|
return (INT32_MAX);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2021-02-15 04:13:52 +00:00
|
|
|
return (INT32_MAX);
|
2021-01-31 17:12:01 +00:00
|
|
|
}
|
|
|
|
|
2021-02-17 01:42:46 +00:00
|
|
|
// We saw a node.
|
|
|
|
uint32_t StoreForwardPlugin::sawNode(uint32_t node)
|
|
|
|
{
|
|
|
|
|
|
|
|
/*
|
|
|
|
TODO: Move receivedRecord into the PSRAM
|
|
|
|
|
|
|
|
TODO: Gracefully handle the case where we run out of records.
|
|
|
|
Maybe replace the oldest record that hasn't been seen in a while and assume they won't be back.
|
|
|
|
|
|
|
|
TODO: Implment this as a std::map for quicker lookups (maybe it doesn't matter?).
|
|
|
|
*/
|
|
|
|
|
2021-02-21 07:53:53 +00:00
|
|
|
DEBUG_MSG("looking for node - %u\n", node);
|
2021-02-17 01:42:46 +00:00
|
|
|
for (int i = 0; i < 50; i++) {
|
2021-02-21 07:53:53 +00:00
|
|
|
//DEBUG_MSG("Iterating through the seen nodes - %u %u %u\n", i, receivedRecord[i][0], receivedRecord[i][1]);
|
2021-02-17 01:42:46 +00:00
|
|
|
// First time seeing that node.
|
|
|
|
if (receivedRecord[i][0] == 0) {
|
2021-02-21 07:53:53 +00:00
|
|
|
//DEBUG_MSG("New node! Woohoo! Win!\n");
|
2021-02-17 01:42:46 +00:00
|
|
|
receivedRecord[i][0] = node;
|
|
|
|
receivedRecord[i][1] = millis();
|
|
|
|
|
|
|
|
return receivedRecord[i][1];
|
|
|
|
}
|
|
|
|
|
|
|
|
// We've seen this node before.
|
|
|
|
if (receivedRecord[i][0] == node) {
|
2021-02-21 07:53:53 +00:00
|
|
|
//DEBUG_MSG("We've seen this node before\n");
|
2021-02-17 01:42:46 +00:00
|
|
|
uint32_t lastSaw = receivedRecord[i][1];
|
|
|
|
receivedRecord[i][1] = millis();
|
|
|
|
return lastSaw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-02-21 07:53:53 +00:00
|
|
|
// We saw a node.
|
|
|
|
void StoreForwardPlugin::sawNodeReport()
|
|
|
|
{
|
|
|
|
|
|
|
|
/*
|
|
|
|
TODO: Move receivedRecord into the PSRAM
|
|
|
|
|
|
|
|
TODO: Gracefully handle the case where we run out of records.
|
|
|
|
Maybe replace the oldest record that hasn't been seen in a while and assume they won't be back.
|
|
|
|
|
|
|
|
TODO: Implment this as a std::map for quicker lookups (maybe it doesn't matter?).
|
|
|
|
*/
|
|
|
|
|
|
|
|
DEBUG_MSG("Iterating through the seen nodes ...\n");
|
|
|
|
for (int i = 0; i < 50; i++) {
|
|
|
|
if (receivedRecord[i][1]) {
|
|
|
|
DEBUG_MSG("... record-%u node-%u secAgo-%u\n", i, receivedRecord[i][0], (millis() - receivedRecord[i][1]) / 1000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-31 17:12:01 +00:00
|
|
|
MeshPacket *StoreForwardPluginRadio::allocReply()
|
|
|
|
{
|
|
|
|
|
|
|
|
auto reply = allocDataPacket(); // Allocate a packet for sending
|
|
|
|
|
|
|
|
return reply;
|
|
|
|
}
|
2021-02-15 04:13:52 +00:00
|
|
|
|
2021-01-31 17:12:01 +00:00
|
|
|
void StoreForwardPluginRadio::sendPayload(NodeNum dest, bool wantReplies)
|
|
|
|
{
|
|
|
|
MeshPacket *p = allocReply();
|
|
|
|
p->to = dest;
|
|
|
|
p->decoded.want_response = wantReplies;
|
|
|
|
|
|
|
|
service.sendToMesh(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool StoreForwardPluginRadio::handleReceived(const MeshPacket &mp)
|
|
|
|
{
|
|
|
|
#ifndef NO_ESP32
|
2021-02-15 04:13:52 +00:00
|
|
|
if (radioConfig.preferences.store_forward_plugin_enabled) {
|
2021-02-21 07:53:53 +00:00
|
|
|
auto &p = mp;
|
2021-01-31 17:12:01 +00:00
|
|
|
|
2021-02-15 04:13:52 +00:00
|
|
|
if (mp.from != nodeDB.getNodeNum()) {
|
2021-02-21 07:53:53 +00:00
|
|
|
// DEBUG_MSG("Store & Forward Plugin -- Print Start ---------- ---------- ---------- ---------- ----------\n\n\n");
|
|
|
|
printPacket("----- PACKET FROM RADIO -----", &mp);
|
2021-02-17 01:42:46 +00:00
|
|
|
uint32_t sawTime = storeForwardPlugin->sawNode(mp.from);
|
2021-02-21 07:53:53 +00:00
|
|
|
DEBUG_MSG("We last saw this node (%u), %u sec ago\n", mp.from, (millis() - sawTime) / 1000);
|
|
|
|
|
|
|
|
if (mp.decoded.data.portnum == PortNum_UNKNOWN_APP) {
|
|
|
|
DEBUG_MSG("Packet came from - PortNum_UNKNOWN_APP\n");
|
|
|
|
} else if (mp.decoded.data.portnum == PortNum_TEXT_MESSAGE_APP) {
|
|
|
|
DEBUG_MSG("Packet came from - PortNum_TEXT_MESSAGE_APP\n");
|
|
|
|
} else if (mp.decoded.data.portnum == PortNum_REMOTE_HARDWARE_APP) {
|
|
|
|
DEBUG_MSG("Packet came from - PortNum_REMOTE_HARDWARE_APP\n");
|
|
|
|
} else if (mp.decoded.data.portnum == PortNum_POSITION_APP) {
|
|
|
|
DEBUG_MSG("Packet came from - PortNum_POSITION_APP\n");
|
|
|
|
} else if (mp.decoded.data.portnum == PortNum_NODEINFO_APP) {
|
|
|
|
DEBUG_MSG("Packet came from - PortNum_NODEINFO_APP\n");
|
|
|
|
} else if (mp.decoded.data.portnum == PortNum_REPLY_APP) {
|
|
|
|
DEBUG_MSG("Packet came from - PortNum_REPLY_APP\n");
|
|
|
|
} else if (mp.decoded.data.portnum == PortNum_IP_TUNNEL_APP) {
|
|
|
|
DEBUG_MSG("Packet came from - PortNum_IP_TUNNEL_APP\n");
|
|
|
|
} else if (mp.decoded.data.portnum == PortNum_SERIAL_APP) {
|
|
|
|
DEBUG_MSG("Packet came from - PortNum_SERIAL_APP\n");
|
|
|
|
} else if (mp.decoded.data.portnum == PortNum_STORE_FORWARD_APP) {
|
|
|
|
DEBUG_MSG("Packet came from - PortNum_STORE_FORWARD_APP\n");
|
|
|
|
} else if (mp.decoded.data.portnum == PortNum_RANGE_TEST_APP) {
|
|
|
|
DEBUG_MSG("Packet came from - PortNum_RANGE_TEST_APP\n");
|
|
|
|
} else if (mp.decoded.data.portnum == PortNum_PRIVATE_APP) {
|
|
|
|
DEBUG_MSG("Packet came from - PortNum_PRIVATE_APP\n");
|
|
|
|
} else if (mp.decoded.data.portnum == PortNum_RANGE_TEST_APP) {
|
|
|
|
DEBUG_MSG("Packet came from - PortNum_RANGE_TEST_APP\n");
|
|
|
|
} else if (mp.decoded.data.portnum == PortNum_ATAK_FORWARDER) {
|
|
|
|
DEBUG_MSG("Packet came from - PortNum_ATAK_FORWARDER\n");
|
|
|
|
} else {
|
|
|
|
DEBUG_MSG("Packet came from an unknown port %u\n", mp.decoded.data.portnum);
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint8_t bytes[MAX_RHPACKETLEN];
|
|
|
|
size_t numbytes = pb_encode_to_bytes(bytes, sizeof(bytes), SubPacket_fields, &p.decoded);
|
|
|
|
assert(numbytes <= MAX_RHPACKETLEN);
|
|
|
|
|
|
|
|
DEBUG_MSG("MP numbytes %u\n", numbytes);
|
|
|
|
|
|
|
|
// Serialization is in Router.cpp line 180
|
2021-02-15 04:13:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
DEBUG_MSG("Store & Forward Plugin - Disabled\n");
|
|
|
|
}
|
2021-01-31 17:12:01 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return true; // Let others look at this message also if they want
|
|
|
|
}
|