From 5caa22b79a5532b2848a8e6689e0be2b36a94fde Mon Sep 17 00:00:00 2001 From: George Baltz N3GB Date: Mon, 3 Mar 2025 15:49:12 -0500 Subject: [PATCH 1/3] Add locking to rig_stop_morse and rig_wait_morse --- src/rig.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/rig.c b/src/rig.c index bddb74d07..dffbcbcbc 100644 --- a/src/rig.c +++ b/src/rig.c @@ -7545,14 +7545,18 @@ int HAMLIB_API rig_stop_morse(RIG *rig, vfo_t vfo) resetFIFO(rs->fifo_morse); // clear out the CW queue + LOCK(1); if (vfo == RIG_VFO_CURR || vfo == rs->current_vfo) { - RETURNFUNC(caps->stop_morse(rig, vfo)); + retcode = caps->stop_morse(rig, vfo); + LOCK(0); + RETURNFUNC(retcode); } if (!caps->set_vfo) { + LOCK(0); RETURNFUNC(-RIG_ENAVAIL); } @@ -7562,6 +7566,7 @@ int HAMLIB_API rig_stop_morse(RIG *rig, vfo_t vfo) if (retcode != RIG_OK) { + LOCK(0); RETURNFUNC(retcode); } @@ -7576,6 +7581,7 @@ int HAMLIB_API rig_stop_morse(RIG *rig, vfo_t vfo) retcode = rc2; } + LOCK(0); RETURNFUNC(retcode); } @@ -7645,14 +7651,18 @@ int HAMLIB_API rig_wait_morse(RIG *rig, vfo_t vfo) caps = rig->caps; + LOCK(1); if (vfo == RIG_VFO_CURR || vfo == STATE(rig)->current_vfo) { - RETURNFUNC(wait_morse_ptt(rig, vfo)); + retcode = wait_morse_ptt(rig, vfo); + LOCK(0); + RETURNFUNC(retcode); } if (!caps->set_vfo) { + LOCK(0); RETURNFUNC(-RIG_ENAVAIL); } @@ -7662,6 +7672,7 @@ int HAMLIB_API rig_wait_morse(RIG *rig, vfo_t vfo) if (retcode != RIG_OK) { + LOCK(0); RETURNFUNC(retcode); } @@ -7676,6 +7687,7 @@ int HAMLIB_API rig_wait_morse(RIG *rig, vfo_t vfo) retcode = rc2; } + LOCK(0); RETURNFUNC(retcode); } From 9b2904c0b676dc4ed57bd09e300b6a1e81b930c8 Mon Sep 17 00:00:00 2001 From: George Baltz N3GB Date: Tue, 4 Mar 2025 16:58:43 -0500 Subject: [PATCH 2/3] Fix timeout in kenwood_stop_morse Add "KY0" to exceptions. --- rigs/kenwood/kenwood.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/rigs/kenwood/kenwood.c b/rigs/kenwood/kenwood.c index d2e444a45..83a93270c 100644 --- a/rigs/kenwood/kenwood.c +++ b/rigs/kenwood/kenwood.c @@ -392,6 +392,7 @@ transaction_write: skip |= strncmp(cmdstr, "RD", 2) == 0; skip |= strncmp(cmdstr, "KYW", 3) == 0; skip |= strncmp(cmdstr, "KY ", 3) == 0; + skip |= strncmp(cmdstr, "KY0", 3) == 0; skip |= strncmp(cmdstr, "KY2", 3) == 0; skip |= strncmp(cmdstr, "PS1", 3) == 0; skip |= strncmp(cmdstr, "PS0", 3) == 0; @@ -400,7 +401,8 @@ transaction_write: if (skip) { // most command we give them a little time -- but not KY - if (strncmp(cmdstr, "KY ", 3) != 0 && strncmp(cmdstr, "KY2", 3) != 0) + if (strncmp(cmdstr, "KY", 2) != 0 || (cmdstr[2] != ' ' && cmdstr[2] != '0' + && cmdstr[2] != '2')) { hl_usleep(200 * 1000); // give little settle time for these commands } From a3e23e79cd97b4c48ea56cc2776b68d86db61a08 Mon Sep 17 00:00:00 2001 From: George Baltz N3GB Date: Tue, 4 Mar 2025 21:32:34 -0500 Subject: [PATCH 3/3] Minor cleanups/fixes Return status from rig_send_morse Remove duplicate include and some dead code Avoid future NULL dereference in error cases --- src/rig.c | 45 ++++++++++++++++++++------------------------- 1 file changed, 20 insertions(+), 25 deletions(-) diff --git a/src/rig.c b/src/rig.c index dffbcbcbc..0e5aa74e7 100644 --- a/src/rig.c +++ b/src/rig.c @@ -61,9 +61,8 @@ #include #include #include -#include "mutex.h" -#include +#include "mutex.h" #include "serial.h" #include "parallel.h" #include "network.h" @@ -957,9 +956,7 @@ int HAMLIB_API rig_open(RIG *rig) { struct rig_caps *caps; struct rig_state *rs; - hamlib_port_t *rp = RIGPORT(rig); - hamlib_port_t *pttp = PTTPORT(rig); - hamlib_port_t *dcdp = DCDPORT(rig); + hamlib_port_t *rp, *pttp, *dcdp; int status = RIG_OK; value_t parm_value; //unsigned int net1, net2, net3, net4, net5, net6, net7, net8, port; @@ -975,6 +972,9 @@ int HAMLIB_API rig_open(RIG *rig) caps = rig->caps; rs = STATE(rig); + rp = RIGPORT(rig); + pttp = PTTPORT(rig); + dcdp = DCDPORT(rig); rp->rig = rig; rs->rigport_deprecated.rig = rig; @@ -1685,9 +1685,7 @@ int HAMLIB_API rig_open(RIG *rig) int HAMLIB_API rig_close(RIG *rig) { const struct rig_caps *caps; - hamlib_port_t *rp = RIGPORT(rig); - hamlib_port_t *pttp = PTTPORT(rig); - hamlib_port_t *dcdp = DCDPORT(rig); + hamlib_port_t *rp, *pttp, *dcdp; struct rig_state *rs; if (!rig || !rig->caps) @@ -1701,6 +1699,9 @@ int HAMLIB_API rig_close(RIG *rig) caps = rig->caps; rs = STATE(rig); + rp = RIGPORT(rig); + pttp = PTTPORT(rig); + dcdp = DCDPORT(rig); if (!rs->comm_state) { @@ -7476,9 +7477,10 @@ int HAMLIB_API rig_send_morse(RIG *rig, vfo_t vfo, const char *msg) LOCK(1); retcode = caps->send_morse(rig, vfo, msg); LOCK(0); +#else + retcode = push(rs->fifo_morse, msg); #endif - push(rs->fifo_morse, msg); - RETURNFUNC(RIG_OK); + RETURNFUNC(retcode); } if (!caps->set_vfo) @@ -8404,20 +8406,6 @@ void rig_lock(RIG *rig, int lock) struct rig_state *rs = STATE(rig); -#if 0 - if (rs->multicast == NULL) - { - rig_debug(RIG_DEBUG_BUG, "%s: locking skipped, lock = %d\n", __func__, lock); - return; - } // not initialized yet - - if (!rs->multicast->mutex_initialized) - { - rs->multicast->mutex = initializer; - rs->multicast->mutex_initialized = 1; - } -#endif - if (lock) { pthread_mutex_lock(&rs->api_mutex); @@ -9058,7 +9046,7 @@ int morse_data_handler_set_keyspd(RIG *rig, int keyspd) * Get the address of a structure without relying on changeable * internal data organization. * - * \retval The address of the enumed structure + * \retval The address of the enumed structure, NULL if error * * Note: This is meant for use by the HAMLIB_???PORT macros mostly. Only * compatibility with them is supported. @@ -9067,6 +9055,13 @@ int morse_data_handler_set_keyspd(RIG *rig, int keyspd) */ HAMLIB_EXPORT(void *) rig_data_pointer(RIG *rig, rig_ptrx_t idx) { + + if (!rig) + { + rig_debug(RIG_DEBUG_ERR, "%s: missing rig\n", __func__); + return NULL; + } + switch (idx) { case RIG_PTRX_RIGPORT: