From a7ef9e9c084e8301c0e42ec7e7b1877848997e2c Mon Sep 17 00:00:00 2001 From: Jorropo Date: Wed, 30 Apr 2025 12:52:42 +0200 Subject: [PATCH] udp-multicast: remove the thread from the multicast thread API (#6685) * udp-multicast: remove the thread from the multicast thread API The whole API is parallel & asynchronous we don't need to start a thread ourself, the implementation probably does when we call start listening already. * Take copilot advice and call it a handler --------- Co-authored-by: Ben Meadors --- src/main.cpp | 8 ++++---- src/main.h | 4 ++-- src/mesh/Router.cpp | 4 ++-- .../{UdpMulticastThread.h => UdpMulticastHandler.h} | 13 ++----------- src/mesh/wifi/WiFiAPClient.cpp | 4 ++-- 5 files changed, 12 insertions(+), 21 deletions(-) rename src/mesh/udp/{UdpMulticastThread.h => UdpMulticastHandler.h} (85%) diff --git a/src/main.cpp b/src/main.cpp index eb93a70d1..9ef944e65 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -124,8 +124,8 @@ extern void tftSetup(void); #endif #ifdef HAS_UDP_MULTICAST -#include "mesh/udp/UdpMulticastThread.h" -UdpMulticastThread *udpThread = nullptr; +#include "mesh/udp/UdpMulticastHandler.h" +UdpMulticastHandler *udpHandler = nullptr; #endif #if defined(TCXO_OPTIONAL) @@ -918,12 +918,12 @@ void setup() #ifdef HAS_UDP_MULTICAST LOG_DEBUG("Start multicast thread"); - udpThread = new UdpMulticastThread(); + udpHandler = new UdpMulticastHandler(); #ifdef ARCH_PORTDUINO // FIXME: portduino does not ever call onNetworkConnected so call it here because I don't know what happen if I call // onNetworkConnected there if (config.network.enabled_protocols & meshtastic_Config_NetworkConfig_ProtocolFlags_UDP_BROADCAST) { - udpThread->start(); + udpHandler->start(); } #endif #endif diff --git a/src/main.h b/src/main.h index 3b71cfeea..c3807cfd5 100644 --- a/src/main.h +++ b/src/main.h @@ -51,8 +51,8 @@ extern AudioThread *audioThread; #endif #ifdef HAS_UDP_MULTICAST -#include "mesh/udp/UdpMulticastThread.h" -extern UdpMulticastThread *udpThread; +#include "mesh/udp/UdpMulticastHandler.h" +extern UdpMulticastHandler *udpHandler; #endif // Global Screen singleton. diff --git a/src/mesh/Router.cpp b/src/mesh/Router.cpp index 2cc3007a2..fef29388e 100644 --- a/src/mesh/Router.cpp +++ b/src/mesh/Router.cpp @@ -293,8 +293,8 @@ ErrorCode Router::send(meshtastic_MeshPacket *p) } #if HAS_UDP_MULTICAST - if (udpThread && config.network.enabled_protocols & meshtastic_Config_NetworkConfig_ProtocolFlags_UDP_BROADCAST) { - udpThread->onSend(const_cast(p)); + if (udpHandler && config.network.enabled_protocols & meshtastic_Config_NetworkConfig_ProtocolFlags_UDP_BROADCAST) { + udpHandler->onSend(const_cast(p)); } #endif diff --git a/src/mesh/udp/UdpMulticastThread.h b/src/mesh/udp/UdpMulticastHandler.h similarity index 85% rename from src/mesh/udp/UdpMulticastThread.h rename to src/mesh/udp/UdpMulticastHandler.h index 88824dc4d..39bd61021 100644 --- a/src/mesh/udp/UdpMulticastThread.h +++ b/src/mesh/udp/UdpMulticastHandler.h @@ -13,12 +13,11 @@ #endif // HAS_ETHERNET #define UDP_MULTICAST_DEFAUL_PORT 4403 // Default port for UDP multicast is same as TCP api server -#define UDP_MULTICAST_THREAD_INTERVAL_MS 15000 -class UdpMulticastThread : public concurrency::OSThread +class UdpMulticastHandler final { public: - UdpMulticastThread() : OSThread("UdpMulticast") { udpIpAddress = IPAddress(224, 0, 0, 69); } + UdpMulticastHandler() { udpIpAddress = IPAddress(224, 0, 0, 69); } void start() { @@ -71,14 +70,6 @@ class UdpMulticastThread : public concurrency::OSThread return true; } - protected: - int32_t runOnce() override - { - canSleep = true; - // TODO: Implement nodeinfo broadcast - return UDP_MULTICAST_THREAD_INTERVAL_MS; - } - private: IPAddress udpIpAddress; AsyncUDP udp; diff --git a/src/mesh/wifi/WiFiAPClient.cpp b/src/mesh/wifi/WiFiAPClient.cpp index 4d0b74f7c..789f8ac44 100644 --- a/src/mesh/wifi/WiFiAPClient.cpp +++ b/src/mesh/wifi/WiFiAPClient.cpp @@ -133,8 +133,8 @@ static void onNetworkConnected() } #if HAS_UDP_MULTICAST - if (udpThread && config.network.enabled_protocols & meshtastic_Config_NetworkConfig_ProtocolFlags_UDP_BROADCAST) { - udpThread->start(); + if (udpHandler && config.network.enabled_protocols & meshtastic_Config_NetworkConfig_ProtocolFlags_UDP_BROADCAST) { + udpHandler->start(); } #endif }