|
|
|
@@ -5,11 +5,13 @@
|
|
|
|
|
#include "Router.h"
|
|
|
|
|
#include "configuration.h"
|
|
|
|
|
#include "mesh-pb-constants.h"
|
|
|
|
|
#include "plugins/PluginDev.h"
|
|
|
|
|
#include <Arduino.h>
|
|
|
|
|
#include <map>
|
|
|
|
|
|
|
|
|
|
#define STOREFORWARD_MAX_PACKETS 0
|
|
|
|
|
#define STOREFORWARD_SEND_HISTORY_SHORT 600
|
|
|
|
|
#define STOREFORWARD_SEND_HISTORY_PERIOD 10 * 60
|
|
|
|
|
#define STOREFORWARD_SEND_HISTORY_MAX 0
|
|
|
|
|
|
|
|
|
|
StoreForwardPlugin *storeForwardPlugin;
|
|
|
|
|
|
|
|
|
@@ -22,9 +24,8 @@ int32_t StoreForwardPlugin::runOnce()
|
|
|
|
|
|
|
|
|
|
if (radioConfig.preferences.is_router) {
|
|
|
|
|
// Maybe some cleanup functions?
|
|
|
|
|
this->sawNodeReport();
|
|
|
|
|
this->historyReport();
|
|
|
|
|
return (10 * 1000);
|
|
|
|
|
return (60 * 1000);
|
|
|
|
|
} else {
|
|
|
|
|
/*
|
|
|
|
|
* If the plugin is turned on and is_router is not enabled, then we'll send a heartbeat every
|
|
|
|
@@ -64,14 +65,12 @@ void StoreForwardPlugin::populatePSRAM()
|
|
|
|
|
DEBUG_MSG(" Total PSRAM: %d\n", ESP.getPsramSize());
|
|
|
|
|
DEBUG_MSG(" Free PSRAM: %d\n", ESP.getFreePsram());
|
|
|
|
|
|
|
|
|
|
// PacketHistoryStruct *packetHistory = (PacketHistoryStruct *)ps_calloc(STOREFORWARD_MAX_PACKETS,
|
|
|
|
|
// sizeof(PacketHistoryStruct));
|
|
|
|
|
|
|
|
|
|
// Use a maximum of half the available PSRAM unless otherwise specified.
|
|
|
|
|
uint32_t numberOfPackets =
|
|
|
|
|
STOREFORWARD_MAX_PACKETS ? STOREFORWARD_MAX_PACKETS : ((ESP.getPsramSize() / 2) / sizeof(PacketHistoryStruct));
|
|
|
|
|
|
|
|
|
|
this->packetHistory = (PacketHistoryStruct *)ps_calloc(numberOfPackets, sizeof(PacketHistoryStruct));
|
|
|
|
|
// this->packetHistory = (PacketHistoryStruct *)ps_calloc(numberOfPackets, sizeof(PacketHistoryStruct));
|
|
|
|
|
this->packetHistory = static_cast<PacketHistoryStruct *>(ps_calloc(numberOfPackets, sizeof(PacketHistoryStruct)));
|
|
|
|
|
DEBUG_MSG("After PSRAM initilization:\n");
|
|
|
|
|
|
|
|
|
|
DEBUG_MSG(" Total heap: %d\n", ESP.getHeapSize());
|
|
|
|
@@ -83,41 +82,17 @@ void StoreForwardPlugin::populatePSRAM()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// We saw a node.
|
|
|
|
|
uint32_t StoreForwardPlugin::sawNode(uint32_t node)
|
|
|
|
|
void StoreForwardPlugin::sawNode(uint32_t whoWeSaw, uint32_t sawSecAgo)
|
|
|
|
|
{
|
|
|
|
|
if (radioConfig.preferences.is_router) {
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
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("%s (id=0x%08x Fr0x%02x To0x%02x, WantAck%d, HopLim%d", prefix, p->id, p->from & 0xff, p->to & 0xff,
|
|
|
|
|
// p->want_ack, p->hop_limit);
|
|
|
|
|
DEBUG_MSG("looking for node - from-0x%08x\n", node);
|
|
|
|
|
for (int i = 0; i < 50; i++) {
|
|
|
|
|
// DEBUG_MSG("Iterating through the seen nodes - %u %u %u\n", i, receivedRecord[i][0], receivedRecord[i][1]);
|
|
|
|
|
// First time seeing that node.
|
|
|
|
|
if (receivedRecord[i][0] == 0) {
|
|
|
|
|
// DEBUG_MSG("New node! Woohoo! Win!\n");
|
|
|
|
|
receivedRecord[i][0] = node;
|
|
|
|
|
receivedRecord[i][1] = millis();
|
|
|
|
|
|
|
|
|
|
return receivedRecord[i][1];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// We've seen this node before.
|
|
|
|
|
if (receivedRecord[i][0] == node) {
|
|
|
|
|
// DEBUG_MSG("We've seen this node before\n");
|
|
|
|
|
uint32_t lastSaw = receivedRecord[i][1];
|
|
|
|
|
receivedRecord[i][1] = millis();
|
|
|
|
|
return lastSaw;
|
|
|
|
|
// If node has been away for more than 10 minutes, send the node the last 10 minutes of
|
|
|
|
|
// messages
|
|
|
|
|
if (sawSecAgo > STOREFORWARD_SEND_HISTORY_PERIOD) {
|
|
|
|
|
// Node has been away for a while.
|
|
|
|
|
storeForwardPlugin->historySend(STOREFORWARD_SEND_HISTORY_PERIOD, whoWeSaw);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void StoreForwardPlugin::historyReport()
|
|
|
|
@@ -132,8 +107,15 @@ void StoreForwardPlugin::historyReport()
|
|
|
|
|
}
|
|
|
|
|
DEBUG_MSG("StoreForwardPlugin::historyReport runtime - %u ms\n", millis() - startTimer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
void StoreForwardPlugin::historySend(uint32_t msAgo, uint32_t to)
|
|
|
|
|
{
|
|
|
|
|
// Send "Welcome back"
|
|
|
|
|
this->sendPayloadWelcome(to, false);
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < this->packetHistoryCurrent; i++) {
|
|
|
|
|
if (this->packetHistory[i].time) {
|
|
|
|
|
// DEBUG_MSG("... time-%u to-0x%08x\n", this->packetHistory[i].time, this->packetHistory[i].to & 0xffffffff);
|
|
|
|
@@ -159,27 +141,6 @@ void StoreForwardPlugin::historyAdd(const MeshPacket *mp)
|
|
|
|
|
this->packetHistoryCurrent++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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 in receivedRecord...\n");
|
|
|
|
|
for (int i = 0; i < 50; i++) {
|
|
|
|
|
if (receivedRecord[i][1]) {
|
|
|
|
|
DEBUG_MSG("... record-%u from-0x%08x secAgo-%u\n", i, receivedRecord[i][0], (millis() - receivedRecord[i][1]) / 1000);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MeshPacket *StoreForwardPlugin::allocReply()
|
|
|
|
|
{
|
|
|
|
|
auto reply = allocDataPacket(); // Allocate a packet for sending
|
|
|
|
@@ -194,8 +155,7 @@ void StoreForwardPlugin::sendPayload(NodeNum dest, bool wantReplies)
|
|
|
|
|
p->decoded.want_response = wantReplies;
|
|
|
|
|
|
|
|
|
|
p->want_ack = true;
|
|
|
|
|
/*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
static char heartbeatString[20];
|
|
|
|
|
snprintf(heartbeatString, sizeof(heartbeatString), "1");
|
|
|
|
|
|
|
|
|
@@ -205,60 +165,50 @@ void StoreForwardPlugin::sendPayload(NodeNum dest, bool wantReplies)
|
|
|
|
|
service.sendToMesh(p);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void StoreForwardPlugin::sendPayloadWelcome(NodeNum dest, bool wantReplies)
|
|
|
|
|
{
|
|
|
|
|
DEBUG_MSG("*********************************\n");
|
|
|
|
|
DEBUG_MSG("*********************************\n");
|
|
|
|
|
DEBUG_MSG("*********************************\n");
|
|
|
|
|
DEBUG_MSG("Sending S&F Welcome Message\n");
|
|
|
|
|
DEBUG_MSG("*********************************\n");
|
|
|
|
|
DEBUG_MSG("*********************************\n");
|
|
|
|
|
DEBUG_MSG("*********************************\n");
|
|
|
|
|
MeshPacket *p = allocReply();
|
|
|
|
|
p->to = dest;
|
|
|
|
|
p->decoded.want_response = wantReplies;
|
|
|
|
|
|
|
|
|
|
p->want_ack = true;
|
|
|
|
|
|
|
|
|
|
p->decoded.portnum = PortNum_TEXT_MESSAGE_APP;
|
|
|
|
|
|
|
|
|
|
static char heartbeatString[80];
|
|
|
|
|
snprintf(heartbeatString, sizeof(heartbeatString), "Welcome back to the mesh. We have not seen you in x minutes!");
|
|
|
|
|
|
|
|
|
|
p->decoded.payload.size = strlen(heartbeatString); // You must specify how many bytes are in the reply
|
|
|
|
|
memcpy(p->decoded.payload.bytes, heartbeatString, p->decoded.payload.size);
|
|
|
|
|
|
|
|
|
|
service.sendToMesh(p);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool StoreForwardPlugin::handleReceived(const MeshPacket &mp)
|
|
|
|
|
{
|
|
|
|
|
#ifndef NO_ESP32
|
|
|
|
|
if (radioConfig.preferences.store_forward_plugin_enabled) {
|
|
|
|
|
|
|
|
|
|
if (getFrom(&mp) != nodeDB.getNodeNum()) {
|
|
|
|
|
// DEBUG_MSG("Store & Forward Plugin -- Print Start ---------- ---------- ---------- ---------- ----------\n\n\n");
|
|
|
|
|
// DEBUG_MSG("%s (id=0x%08x Fr0x%02x To0x%02x, WantAck%d, HopLim%d", prefix, p->id, p->from & 0xff, p->to & 0xff,
|
|
|
|
|
// p->want_ack, p->hop_limit);
|
|
|
|
|
printPacket("----- PACKET FROM RADIO -----", &mp);
|
|
|
|
|
uint32_t sawTime = storeForwardPlugin->sawNode(getFrom(&mp) & 0xffffffff);
|
|
|
|
|
DEBUG_MSG("We last saw this node (%u), %u sec ago\n", mp.from & 0xffffffff, (millis() - sawTime) / 1000);
|
|
|
|
|
// uint32_t sawTime = storeForwardPlugin->sawNode(getFrom(&mp) & 0xffffffff);
|
|
|
|
|
// DEBUG_MSG("We last saw this node (%u), %u sec ago\n", mp.from & 0xffffffff, (millis() - sawTime) / 1000);
|
|
|
|
|
DEBUG_MSG(" -------------- ");
|
|
|
|
|
if (mp.decoded.portnum == PortNum_UNKNOWN_APP) {
|
|
|
|
|
DEBUG_MSG("Packet came from - PortNum_UNKNOWN_APP\n");
|
|
|
|
|
} else if (mp.decoded.portnum == PortNum_TEXT_MESSAGE_APP) {
|
|
|
|
|
if (mp.decoded.portnum == PortNum_TEXT_MESSAGE_APP) {
|
|
|
|
|
DEBUG_MSG("Packet came from - PortNum_TEXT_MESSAGE_APP\n");
|
|
|
|
|
|
|
|
|
|
storeForwardPlugin->historyAdd(&mp);
|
|
|
|
|
|
|
|
|
|
} else if (mp.decoded.portnum == PortNum_REMOTE_HARDWARE_APP) {
|
|
|
|
|
DEBUG_MSG("Packet came from - PortNum_REMOTE_HARDWARE_APP\n");
|
|
|
|
|
} else if (mp.decoded.portnum == PortNum_POSITION_APP) {
|
|
|
|
|
DEBUG_MSG("Packet came from - PortNum_POSITION_APP\n");
|
|
|
|
|
} else if (mp.decoded.portnum == PortNum_NODEINFO_APP) {
|
|
|
|
|
DEBUG_MSG("Packet came from - PortNum_NODEINFO_APP\n");
|
|
|
|
|
} else if (mp.decoded.portnum == PortNum_ROUTING_APP) {
|
|
|
|
|
DEBUG_MSG("Packet came from - PortNum_ROUTING_APP\n");
|
|
|
|
|
} else if (mp.decoded.portnum == PortNum_ADMIN_APP) {
|
|
|
|
|
DEBUG_MSG("Packet came from - PortNum_ADMIN_APP\n");
|
|
|
|
|
} else if (mp.decoded.portnum == PortNum_REPLY_APP) {
|
|
|
|
|
DEBUG_MSG("Packet came from - PortNum_REPLY_APP\n");
|
|
|
|
|
} else if (mp.decoded.portnum == PortNum_IP_TUNNEL_APP) {
|
|
|
|
|
DEBUG_MSG("Packet came from - PortNum_IP_TUNNEL_APP\n");
|
|
|
|
|
} else if (mp.decoded.portnum == PortNum_SERIAL_APP) {
|
|
|
|
|
DEBUG_MSG("Packet came from - PortNum_SERIAL_APP\n");
|
|
|
|
|
} else if (mp.decoded.portnum == PortNum_STORE_FORWARD_APP) {
|
|
|
|
|
DEBUG_MSG("Packet came from - PortNum_STORE_FORWARD_APP\n");
|
|
|
|
|
} else if (mp.decoded.portnum == PortNum_RANGE_TEST_APP) {
|
|
|
|
|
DEBUG_MSG("Packet came from - PortNum_RANGE_TEST_APP\n");
|
|
|
|
|
} else if (mp.decoded.portnum == PortNum_PRIVATE_APP) {
|
|
|
|
|
DEBUG_MSG("Packet came from - PortNum_PRIVATE_APP\n");
|
|
|
|
|
} else if (mp.decoded.portnum == PortNum_RANGE_TEST_APP) {
|
|
|
|
|
DEBUG_MSG("Packet came from - PortNum_RANGE_TEST_APP\n");
|
|
|
|
|
} else if (mp.decoded.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.portnum);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((millis() - sawTime) > STOREFORWARD_SEND_HISTORY_SHORT) {
|
|
|
|
|
// Node has been away for a while.
|
|
|
|
|
storeForwardPlugin->historySend(sawTime, mp.from);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
@@ -276,13 +226,16 @@ StoreForwardPlugin::StoreForwardPlugin()
|
|
|
|
|
|
|
|
|
|
#ifndef NO_ESP32
|
|
|
|
|
|
|
|
|
|
isPromiscuous = true; // Brown chicken brown cow
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
Uncomment the preferences below if you want to use the plugin
|
|
|
|
|
without having to configure it from the PythonAPI or WebUI.
|
|
|
|
|
|
|
|
|
|
radioConfig.preferences.store_forward_plugin_enabled = 1;
|
|
|
|
|
radioConfig.preferences.is_router = 1;
|
|
|
|
|
*/
|
|
|
|
|
if (StoreForward_Dev) {
|
|
|
|
|
radioConfig.preferences.store_forward_plugin_enabled = 1;
|
|
|
|
|
radioConfig.preferences.is_router = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (radioConfig.preferences.store_forward_plugin_enabled) {
|
|
|
|
|
|
|
|
|
|