From 8e63ce7a36236be4473b7373cab4ca0f97c326dc Mon Sep 17 00:00:00 2001 From: Mike Black W9MDB Date: Tue, 25 May 2021 09:29:27 -0500 Subject: [PATCH] Add multicast thread -- the intent is that this will always run https://github.com/Hamlib/Hamlib/issues/695 --- src/network.c | 25 +++++++++++++++++++++++++ src/rig.c | 13 +++++++++++++ 2 files changed, 38 insertions(+) diff --git a/src/network.c b/src/network.c index f094d6e54..2b837356b 100644 --- a/src/network.c +++ b/src/network.c @@ -44,6 +44,7 @@ #include #include #include +#include #ifdef HAVE_NETINET_IN_H # include @@ -396,6 +397,23 @@ int network_close(hamlib_port_t *rp) } //! @endcond +volatile int multicast_server_run = 1; +pthread_t multicast_server_threadId; + +//! @cond Doxygen_Suppress +// our multicast server loop +static void *multicast_server(void *arg) +{ + rig_debug(RIG_DEBUG_TRACE, "%s(%d): Starting multicast server\n", __FILE__, __LINE__); + do { + rig_debug(RIG_DEBUG_TRACE, "%s(%d): Multicast server poll\n", __FILE__, __LINE__); + hl_usleep(1000*1000); + } while(multicast_server_run); + rig_debug(RIG_DEBUG_TRACE, "%s(%d): Stopping multicast server\n", __FILE__, __LINE__); + return NULL; +} +//! @endcond + /** * \brief Open multicast server using rig.state data * @@ -430,6 +448,13 @@ int network_multicast_server(RIG *rig, const char *multicast_addr, int default_p rig_debug(RIG_DEBUG_ERR, "%s(%d) unknown MULTICAST item requested=0x%x\n", __FILE__, __LINE__, items); } + int err = pthread_create(&multicast_server_threadId, NULL, multicast_server, NULL); + if (err) + { + rig_debug(RIG_DEBUG_ERR, "%s(%d) pthread_create error %s\n", __FILE__, __LINE__, strerror(errno)); + return -RIG_EINTERNAL; + } + RETURNFUNC(RIG_OK); } diff --git a/src/rig.c b/src/rig.c index b711c285a..7a95882f6 100644 --- a/src/rig.c +++ b/src/rig.c @@ -60,6 +60,8 @@ #include #include #include +#include +#include #include @@ -1096,6 +1098,17 @@ int HAMLIB_API rig_close(RIG *rig) ENTERFUNC; + // terminate the multicast server + extern int multicast_server_run; + multicast_server_run = 0; + extern pthread_t multicast_server_threadId; + int err = pthread_join(multicast_server_threadId, NULL); + if (err) + { + rig_debug(RIG_DEBUG_ERR, "%s(%d): pthread_join error %s\n", __FILE__, __LINE__, strerror(errno)); + // just ignore it + } + if (!rig || !rig->caps) { RETURNFUNC(-RIG_EINVAL);