From 1eb37dded816c3f749cddea54449e8a051272740 Mon Sep 17 00:00:00 2001 From: Jm Casler Date: Sun, 21 Mar 2021 19:45:35 -0700 Subject: [PATCH] partial work for S&F --- src/mesh/NodeDB.cpp | 31 ++++- src/plugins/esp32/StoreForwardPlugin.cpp | 143 +++++++---------------- src/plugins/esp32/StoreForwardPlugin.h | 5 +- 3 files changed, 70 insertions(+), 109 deletions(-) diff --git a/src/mesh/NodeDB.cpp b/src/mesh/NodeDB.cpp index 35f54fa7..aaa8402d 100644 --- a/src/mesh/NodeDB.cpp +++ b/src/mesh/NodeDB.cpp @@ -23,6 +23,7 @@ #ifndef NO_ESP32 #include "mesh/http/WiFiAPClient.h" +#include "plugins/esp32/StoreForwardPlugin.h" #endif NodeDB nodeDB; @@ -206,12 +207,13 @@ void NodeDB::init() // removed from 1.2 (though we do use old values if found) // We set these _after_ loading from disk - because they come from the build and are more trusted than // what is stored in flash - //if (xstr(HW_VERSION)[0]) + // if (xstr(HW_VERSION)[0]) // strncpy(myNodeInfo.region, optstr(HW_VERSION), sizeof(myNodeInfo.region)); - // else DEBUG_MSG("This build does not specify a HW_VERSION\n"); // Eventually new builds will no longer include this build flag + // else DEBUG_MSG("This build does not specify a HW_VERSION\n"); // Eventually new builds will no longer include this build + // flag // DEBUG_MSG("legacy region %d\n", devicestate.legacyRadio.preferences.region); - if(radioConfig.preferences.region == RegionCode_Unset) + if (radioConfig.preferences.region == RegionCode_Unset) radioConfig.preferences.region = devicestate.legacyRadio.preferences.region; // Check for the old style of region code strings, if found, convert to the new enum. @@ -226,7 +228,7 @@ void NodeDB::init() } strncpy(myNodeInfo.firmware_version, optstr(APP_VERSION), sizeof(myNodeInfo.firmware_version)); - + // hw_model is no longer stored in myNodeInfo (as of 1.2.11) - we now store it as an enum in nodeinfo myNodeInfo.hw_model_deprecated[0] = '\0'; // strncpy(myNodeInfo.hw_model, HW_VENDOR, sizeof(myNodeInfo.hw_model)); @@ -373,7 +375,7 @@ void NodeDB::saveChannelsToDisk() FS.mkdir("/prefs"); #endif saveProto(channelfile, ChannelFile_size, sizeof(ChannelFile), ChannelFile_fields, &channelFile); - } + } } void NodeDB::saveToDisk() @@ -484,16 +486,33 @@ void NodeDB::updateUser(uint32_t nodeId, const User &p) /// we updateGUI and updateGUIforNode if we think our this change is big enough for a redraw void NodeDB::updateFrom(const MeshPacket &mp) { + uint32_t sawSecAgo = 0; + if (mp.which_payloadVariant == MeshPacket_decoded_tag) { DEBUG_MSG("Update DB node 0x%x, rx_time=%u\n", mp.from, mp.rx_time); NodeInfo *info = getOrCreateNode(getFrom(&mp)); - if (mp.rx_time) { // if the packet has a valid timestamp use it to update our last_seen + if (mp.rx_time) { // if the packet has a valid timestamp use it to update our last_seen + + sawSecAgo = sinceLastSeen(info); // Used by S&F + info->has_position = true; // at least the time is valid info->position.time = mp.rx_time; } +// Used by the store & forward plugin. +#ifndef NO_ESP32 + + // Check that both the plugin is + if (radioConfig.preferences.store_forward_plugin_enabled && storeForwardPlugin) { + /* Notify the store and forward plugin that we just saw a node. + */ + storeForwardPlugin.sawNode(getFrom(&mp), sawSecAgo); + } + +#endif + info->snr = mp.rx_snr; // keep the most recent SNR we received for this node. } } diff --git a/src/plugins/esp32/StoreForwardPlugin.cpp b/src/plugins/esp32/StoreForwardPlugin.cpp index e5694caa..fa4ccc41 100644 --- a/src/plugins/esp32/StoreForwardPlugin.cpp +++ b/src/plugins/esp32/StoreForwardPlugin.cpp @@ -9,7 +9,8 @@ #include #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,7 +23,6 @@ int32_t StoreForwardPlugin::runOnce() if (radioConfig.preferences.is_router) { // Maybe some cleanup functions? - this->sawNodeReport(); this->historyReport(); return (10 * 1000); } else { @@ -64,14 +64,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(ps_calloc(numberOfPackets, sizeof(PacketHistoryStruct))); DEBUG_MSG("After PSRAM initilization:\n"); DEBUG_MSG(" Total heap: %d\n", ESP.getHeapSize()); @@ -83,41 +81,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 +106,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 +140,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 +154,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 +164,44 @@ void StoreForwardPlugin::sendPayload(NodeNum dest, bool wantReplies) service.sendToMesh(p); } +void StoreForwardPlugin::sendPayloadWelcome(NodeNum dest, bool wantReplies) +{ + DEBUG_MSG("Sending S&F Welcome Message\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 { @@ -305,7 +248,7 @@ StoreForwardPlugin::StoreForwardPlugin() DEBUG_MSG("Store & Forward Plugin - Aborting Startup.\n"); } - // Client + // Client } else { DEBUG_MSG("Initializing Store & Forward Plugin - Enabled as Client\n"); } diff --git a/src/plugins/esp32/StoreForwardPlugin.h b/src/plugins/esp32/StoreForwardPlugin.h index f8df4892..b444b615 100644 --- a/src/plugins/esp32/StoreForwardPlugin.h +++ b/src/plugins/esp32/StoreForwardPlugin.h @@ -29,8 +29,7 @@ class StoreForwardPlugin : public SinglePortPlugin, private concurrency::OSThrea Update our local reference of when we last saw that node. @return 0 if we have never seen that node before otherwise return the last time we saw the node. */ - uint32_t sawNode(uint32_t); - void sawNodeReport(); + void sawNode(uint32_t whoWeSaw, uint32_t sawSecAgo); void historyAdd(const MeshPacket *mp); void historyReport(); void historySend(uint32_t msAgo, uint32_t to); @@ -40,6 +39,7 @@ class StoreForwardPlugin : public SinglePortPlugin, private concurrency::OSThrea * Send our payload into the mesh */ void sendPayload(NodeNum dest = NODENUM_BROADCAST, bool wantReplies = false); + void sendPayloadWelcome(NodeNum dest = NODENUM_BROADCAST, bool wantReplies = false); virtual MeshPacket *allocReply(); virtual bool wantPortnum(PortNum p) { return true; }; @@ -80,4 +80,3 @@ class StoreForwardPluginRadio : public SinglePortPlugin extern StoreForwardPluginRadio *storeForwardPluginRadio; */ -