diff --git a/src/plugins/esp32/StoreForwardPlugin.cpp b/src/plugins/esp32/StoreForwardPlugin.cpp index 28f1c673..1726a0b2 100644 --- a/src/plugins/esp32/StoreForwardPlugin.cpp +++ b/src/plugins/esp32/StoreForwardPlugin.cpp @@ -8,89 +8,36 @@ #include #include -#define STOREFORWARD_MAX_PACKETS 7500 +#define STOREFORWARD_MAX_PACKETS 0 #define STOREFORWARD_SEND_HISTORY_SHORT 600 StoreForwardPlugin *storeForwardPlugin; -StoreForwardPluginRadio *storeForwardPluginRadio; - -StoreForwardPlugin::StoreForwardPlugin() : concurrency::OSThread("StoreForwardPlugin") {} int32_t StoreForwardPlugin::runOnce() { #ifndef NO_ESP32 - /* - Uncomment the preferences below if you want to use the plugin - without having to configure it from the PythonAPI or WebUI. - - attn @mc-hamster I moved this back inside the comment because I don't think it was intended to checkin. It was forcing all - nodes to be running this and turning off is_router. - - radioConfig.preferences.store_forward_plugin_enabled = 1; - radioConfig.preferences.is_router = 0; - */ - if (radioConfig.preferences.store_forward_plugin_enabled) { - if (firstTime) { - - firstTime = 0; - - if (radioConfig.preferences.is_router) { - DEBUG_MSG("Initializing Store & Forward Plugin - Enabled as Router\n"); - // Router - if (ESP.getPsramSize()) { - if (ESP.getFreePsram() >= 2048 * 1024) { - // Do the startup here - storeForwardPluginRadio = new StoreForwardPluginRadio(); - - this->populatePSRAM(); - - // packetHistory[0].bytes; - return (10 * 1000); - - } else { - DEBUG_MSG("Device has less than 2M 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"); - - return (INT32_MAX); - } - - } else { - DEBUG_MSG("Initializing Store & Forward Plugin - Enabled as Client\n"); - return (5 * 1000); - } - + if (radioConfig.preferences.is_router) { + // Maybe some cleanup functions? + this->sawNodeReport(); + this->historyReport(); + return (10 * 1000); } else { + /* + * If the plugin is turned on and is_router is not enabled, then we'll send a heartbeat every + * few minutes. + * + * This behavior is expected to change. It's only here until we come up with something better. + */ - if (radioConfig.preferences.is_router) { - // Maybe some cleanup functions? - this->sawNodeReport(); - this->historyReport(); - return (10 * 1000); - } else { - /* - * If the plugin is turned on and is_router is not enabled, then we'll send a heartbeat every - * few minutes. - */ + DEBUG_MSG("Store & Forward Plugin - Sending heartbeat\n"); - DEBUG_MSG("Store & Forward Plugin - Sending heartbeat\n"); + storeForwardPlugin->sendPayload(); - // storeForwardPluginRadio->sendPayloadHeartbeat(); - if(storeForwardPluginRadio) - storeForwardPluginRadio->sendPayload(); - - return (1 * 60 * 1000); - } + return (4 * 60 * 1000); } } else { @@ -110,24 +57,29 @@ void StoreForwardPlugin::populatePSRAM() https://learn.upesy.com/en/programmation/psram.html#psram-tab */ - DEBUG_MSG("Before PSRAM initilization\n"); + DEBUG_MSG("Before PSRAM initilization:\n"); - DEBUG_MSG("Total heap: %d\n", ESP.getHeapSize()); - DEBUG_MSG("Free heap: %d\n", ESP.getFreeHeap()); - DEBUG_MSG("Total PSRAM: %d\n", ESP.getPsramSize()); - DEBUG_MSG("Free PSRAM: %d\n", ESP.getFreePsram()); + DEBUG_MSG(" Total heap: %d\n", ESP.getHeapSize()); + DEBUG_MSG(" Free heap: %d\n", ESP.getFreeHeap()); + 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)); - this->packetHistory = (PacketHistoryStruct *)ps_calloc(STOREFORWARD_MAX_PACKETS, sizeof(PacketHistoryStruct)); - DEBUG_MSG("After PSRAM initilization\n"); - DEBUG_MSG("Total heap: %d\n", ESP.getHeapSize()); - DEBUG_MSG("Free heap: %d\n", ESP.getFreeHeap()); - DEBUG_MSG("Total PSRAM: %d\n", ESP.getPsramSize()); - DEBUG_MSG("Free PSRAM: %d\n", ESP.getFreePsram()); + // 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)); - DEBUG_MSG("packetHistory Size - %u", sizeof(packetHistory)); + this->packetHistory = (PacketHistoryStruct *)ps_calloc(numberOfPackets, sizeof(PacketHistoryStruct)); + DEBUG_MSG("After PSRAM initilization:\n"); + + DEBUG_MSG(" Total heap: %d\n", ESP.getHeapSize()); + DEBUG_MSG(" Free heap: %d\n", ESP.getFreeHeap()); + DEBUG_MSG(" Total PSRAM: %d\n", ESP.getPsramSize()); + DEBUG_MSG(" Free PSRAM: %d\n", ESP.getFreePsram()); + DEBUG_MSG("Store and Forward Stats:\n"); + DEBUG_MSG(" numberOfPackets - %u\n", numberOfPackets); } // We saw a node. @@ -228,41 +180,32 @@ void StoreForwardPlugin::sawNodeReport() } } -MeshPacket *StoreForwardPluginRadio::allocReply() +MeshPacket *StoreForwardPlugin::allocReply() { auto reply = allocDataPacket(); // Allocate a packet for sending - return reply; // attn @mc-hamster this code was commented out and was causing memory corruption + return reply; } -void StoreForwardPluginRadio::sendPayload(NodeNum dest, bool wantReplies) +void StoreForwardPlugin::sendPayload(NodeNum dest, bool wantReplies) { - /* - MeshPacket *p = this->allocReply(); // attn @mc-hamster, I moved inside the commented block to prevent leaking memory + DEBUG_MSG("Sending S&F Payload\n"); + MeshPacket *p = allocReply(); p->to = dest; p->decoded.want_response = wantReplies; p->want_ack = true; -*/ - // static char heartbeatString[20]; - // snprintf(heartbeatString, sizeof(heartbeatString), "1"); + /* + */ + static char heartbeatString[20]; + snprintf(heartbeatString, sizeof(heartbeatString), "1"); - // p->decoded.data.payload.size = strlen(heartbeatString); // You must specify how many bytes are in the reply - // memcpy(p->decoded.data.payload.bytes, "1", 1); - - // service.sendToMesh(p); -} - -void StoreForwardPluginRadio::sendPayloadHeartbeat(NodeNum dest, bool wantReplies) -{ - DEBUG_MSG("Sending S&F Heartbeat\n"); - MeshPacket *p = this->allocReply(); - p->to = dest; - p->decoded.want_response = wantReplies; + p->decoded.payload.size = strlen(heartbeatString); // You must specify how many bytes are in the reply + memcpy(p->decoded.payload.bytes, "1", 1); service.sendToMesh(p); } -bool StoreForwardPluginRadio::handleReceived(const MeshPacket &mp) +bool StoreForwardPlugin::handleReceived(const MeshPacket &mp) { #ifndef NO_ESP32 if (radioConfig.preferences.store_forward_plugin_enabled) { @@ -274,7 +217,7 @@ bool StoreForwardPluginRadio::handleReceived(const MeshPacket &mp) 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); - + 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) { @@ -322,3 +265,52 @@ bool StoreForwardPluginRadio::handleReceived(const MeshPacket &mp) return true; // Let others look at this message also if they want } + +StoreForwardPlugin::StoreForwardPlugin() + : SinglePortPlugin("StoreForwardPlugin", PortNum_STORE_FORWARD_APP), concurrency::OSThread("StoreForwardPlugin") +{ + +#ifndef NO_ESP32 + + /* + 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 (radioConfig.preferences.store_forward_plugin_enabled) { + if (radioConfig.preferences.is_router) { + DEBUG_MSG("Initializing Store & Forward Plugin - Enabled as Router\n"); + // Router + if (ESP.getPsramSize()) { + if (ESP.getFreePsram() >= 2048 * 1024) { + // Do the startup here + + this->populatePSRAM(); + + // packetHistory[0].bytes; + // return (10 * 1000); + + } else { + DEBUG_MSG("Device has less than 2M 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"); + + // return (INT32_MAX); + } + + } else { + DEBUG_MSG("Initializing Store & Forward Plugin - Enabled as Client\n"); + // return (5 * 1000); + } + } +#endif +} \ No newline at end of file diff --git a/src/plugins/esp32/StoreForwardPlugin.h b/src/plugins/esp32/StoreForwardPlugin.h index d2a2879c..f8df4892 100644 --- a/src/plugins/esp32/StoreForwardPlugin.h +++ b/src/plugins/esp32/StoreForwardPlugin.h @@ -6,7 +6,6 @@ #include #include - struct PacketHistoryStruct { uint32_t time; uint32_t to; @@ -14,7 +13,7 @@ struct PacketHistoryStruct { uint8_t bytes[MAX_RHPACKETLEN]; }; -class StoreForwardPlugin : private concurrency::OSThread +class StoreForwardPlugin : public SinglePortPlugin, private concurrency::OSThread { bool firstTime = 1; @@ -37,41 +36,18 @@ class StoreForwardPlugin : private concurrency::OSThread void historySend(uint32_t msAgo, uint32_t to); void populatePSRAM(); + /** + * Send our payload into the mesh + */ + void sendPayload(NodeNum dest = NODENUM_BROADCAST, bool wantReplies = false); + virtual MeshPacket *allocReply(); + virtual bool wantPortnum(PortNum p) { return true; }; + private: // Nothing here protected: virtual int32_t runOnce(); -}; - -extern StoreForwardPlugin *storeForwardPlugin; - -/* - * Radio interface for StoreForwardPlugin - * - */ -class StoreForwardPluginRadio : public SinglePortPlugin -{ - // uint32_t lastRxID; - - public: - StoreForwardPluginRadio() : SinglePortPlugin("StoreForwardPluginRadio", PortNum_STORE_FORWARD_APP) {} - // StoreForwardPluginRadio() : SinglePortPlugin("StoreForwardPluginRadio", PortNum_TEXT_MESSAGE_APP) {} - - /** - * Send our payload into the mesh - */ - void sendPayload(NodeNum dest = NODENUM_BROADCAST, bool wantReplies = false); - - /** - * Send our payload into the mesh - */ - void sendPayloadHeartbeat(NodeNum dest = NODENUM_BROADCAST, bool wantReplies = false); - - protected: - virtual MeshPacket *allocReply(); - - virtual bool wantPortnum(PortNum p) { return true; }; /** Called to handle a particular incoming message @@ -80,4 +56,28 @@ class StoreForwardPluginRadio : public SinglePortPlugin virtual bool handleReceived(const MeshPacket &mp); }; +extern StoreForwardPlugin *storeForwardPlugin; + +/* + * Radio interface for StoreForwardPlugin + * + */ + +/* +class StoreForwardPluginRadio : public SinglePortPlugin +{ + // uint32_t lastRxID; + + public: + StoreForwardPluginRadio() : SinglePortPlugin("StoreForwardPluginRadio", PortNum_STORE_FORWARD_APP) {} + // StoreForwardPluginRadio() : SinglePortPlugin("StoreForwardPluginRadio", PortNum_TEXT_MESSAGE_APP) {} + + void sendPayloadHeartbeat(NodeNum dest = NODENUM_BROADCAST, bool wantReplies = false); + + protected: + virtual MeshPacket *allocReply2(); +}; + extern StoreForwardPluginRadio *storeForwardPluginRadio; +*/ +