From b7895f70386563e8c67e951dd86e4c5838ec7464 Mon Sep 17 00:00:00 2001 From: GUVWAF Date: Wed, 15 Feb 2023 19:31:09 +0100 Subject: [PATCH] Sanity check for sending NodeInfo Don't send it if we've done so less than 1 min. ago --- src/mesh/MeshModule.cpp | 10 +++++++--- src/mesh/MeshModule.h | 3 +++ src/modules/NodeInfoModule.cpp | 28 ++++++++++++++++++++-------- src/modules/NodeInfoModule.h | 3 +++ 4 files changed, 33 insertions(+), 11 deletions(-) diff --git a/src/mesh/MeshModule.cpp b/src/mesh/MeshModule.cpp index cc54e78c2..d8dbf8a3a 100644 --- a/src/mesh/MeshModule.cpp +++ b/src/mesh/MeshModule.cpp @@ -77,6 +77,8 @@ void MeshModule::callPlugins(const meshtastic_MeshPacket &mp, RxSource src) currentReply = NULL; // No reply yet + bool ignoreRequest = false; // No module asked to ignore the request yet + // Was this message directed to us specifically? Will be false if we are sniffing someone elses packets auto ourNodeNum = nodeDB.getNodeNum(); bool toUs = mp.to == NODENUM_BROADCAST || mp.to == ourNodeNum; @@ -135,7 +137,8 @@ void MeshModule::callPlugins(const meshtastic_MeshPacket &mp, RxSource src) // any other node. if (mp.decoded.want_response && toUs && (getFrom(&mp) != ourNodeNum || mp.to == ourNodeNum) && !currentReply) { pi.sendResponse(mp); - LOG_INFO("Module '%s' sent a response\n", pi.name); + ignoreRequest = ignoreRequest || pi.ignoreRequest; // If at least one module asks it, we may ignore a request + LOG_INFO("Asked module '%s' to send a response\n", pi.name); } else { LOG_DEBUG("Module '%s' considered\n", pi.name); } @@ -162,8 +165,9 @@ void MeshModule::callPlugins(const meshtastic_MeshPacket &mp, RxSource src) printPacket("Sending response", currentReply); service.sendToMesh(currentReply); currentReply = NULL; - } else if (mp.from != ourNodeNum) { - // Note: if the message started with the local node we don't want to send a no response reply + } else if (mp.from != ourNodeNum && !ignoreRequest) { + // Note: if the message started with the local node or a module asked to ignore the request, we don't want to send a + // no response reply // No one wanted to reply to this requst, tell the requster that happened LOG_DEBUG("No one responded, send a nak\n"); diff --git a/src/mesh/MeshModule.h b/src/mesh/MeshModule.h index c6550f3d9..2eee04f5d 100644 --- a/src/mesh/MeshModule.h +++ b/src/mesh/MeshModule.h @@ -94,6 +94,9 @@ class MeshModule * flag */ bool encryptedOk = false; + /* We allow modules to ignore a request without sending an error if they have a specific reason for it. */ + bool ignoreRequest = false; + /** If a bound channel name is set, we will only accept received packets that come in on that channel. * A special exception (FIXME, not sure if this is a good idea) - packets that arrive on the local interface * are allowed on any channel (this lets the local user do anything). diff --git a/src/modules/NodeInfoModule.cpp b/src/modules/NodeInfoModule.cpp index 15661e837..004ad75f0 100644 --- a/src/modules/NodeInfoModule.cpp +++ b/src/modules/NodeInfoModule.cpp @@ -34,20 +34,32 @@ void NodeInfoModule::sendOurNodeInfo(NodeNum dest, bool wantReplies) service.cancelSending(prevPacketId); meshtastic_MeshPacket *p = allocReply(); - p->to = dest; - p->decoded.want_response = wantReplies; - p->priority = meshtastic_MeshPacket_Priority_BACKGROUND; - prevPacketId = p->id; + if (p) { // Check whether we didn't ignore it + p->to = dest; + p->decoded.want_response = wantReplies; + p->priority = meshtastic_MeshPacket_Priority_BACKGROUND; + prevPacketId = p->id; - service.sendToMesh(p); + service.sendToMesh(p); + } } meshtastic_MeshPacket *NodeInfoModule::allocReply() { - meshtastic_User &u = owner; + uint32_t now = millis(); + // If we sent our NodeInfo less than 1 min. ago, don't send it again as it may be still underway. + if (lastSentToMesh && (now - lastSentToMesh) < 60 * 1000) { + LOG_DEBUG("Sending NodeInfo will be ignored since we just sent it.\n"); + ignoreRequest = true; // Mark it as ignored for MeshModule + return NULL; + } else { + ignoreRequest = false; // Don't ignore requests anymore + meshtastic_User &u = owner; - LOG_INFO("sending owner %s/%s/%s\n", u.id, u.long_name, u.short_name); - return allocDataProtobuf(u); + LOG_INFO("sending owner %s/%s/%s\n", u.id, u.long_name, u.short_name); + lastSentToMesh = now; + return allocDataProtobuf(u); + } } NodeInfoModule::NodeInfoModule() diff --git a/src/modules/NodeInfoModule.h b/src/modules/NodeInfoModule.h index 4645b06b4..900d5983d 100644 --- a/src/modules/NodeInfoModule.h +++ b/src/modules/NodeInfoModule.h @@ -35,6 +35,9 @@ class NodeInfoModule : public ProtobufModule, private concurren /** Does our periodic broadcast */ virtual int32_t runOnce() override; + + private: + uint32_t lastSentToMesh = 0; // Last time we sent our NodeInfo to the mesh }; extern NodeInfoModule *nodeInfoModule;