From e9fd0fba401a6ed2d5cbde5905e2c64cbae21f51 Mon Sep 17 00:00:00 2001 From: Mike Black W9MDB Date: Sun, 9 Jul 2023 16:51:33 -0500 Subject: [PATCH] send_morse now uses a fifo queue and can send up to 1024 chars in one message it relies on the rig backend returning an error when the rig's CW queue is full --- include/hamlib/rig.h | 12 ++++++++++++ src/fifo.c | 16 +++++++++++++--- src/fifo.h | 10 +--------- src/rig.c | 38 ++++++++++++++++++++++++-------------- 4 files changed, 50 insertions(+), 26 deletions(-) diff --git a/include/hamlib/rig.h b/include/hamlib/rig.h index 20eb6466a..fd3fc2326 100644 --- a/include/hamlib/rig.h +++ b/include/hamlib/rig.h @@ -128,6 +128,17 @@ __BEGIN_DECLS +// FIFO currently used for send_morse queue +#define HAMLIB_FIFO_SIZE 1024 + +typedef struct +{ + char data[HAMLIB_FIFO_SIZE]; + int head; + int tail; +} FIFO; + + /** * \brief size of cookie request buffer * Minimum size of cookie buffer to pass to rig_cookie @@ -2682,6 +2693,7 @@ struct rig_state { int spectrum_attenuator[HAMLIB_MAXDBLSTSIZ]; /*!< Spectrum attenuator list in dB, 0 terminated */ volatile int morse_data_handler_thread_run; void *morse_data_handler_priv_data; + FIFO *fifo; }; /** diff --git a/src/fifo.c b/src/fifo.c index 3272188e0..6a87906d0 100644 --- a/src/fifo.c +++ b/src/fifo.c @@ -9,6 +9,11 @@ void initFIFO(FIFO *fifo) fifo->tail = 0; } +void resetFIFO(FIFO *fifo) +{ + fifo->head = fifo->tail; +} + // returns RIG_OK if added // return -RIG error if overflow int push(FIFO *fifo, const char *msg) @@ -18,9 +23,12 @@ int push(FIFO *fifo, const char *msg) for (int i = 0; i < len; ++i) { fifo->data[fifo->tail] = msg[i]; - fifo->tail = (fifo->tail + 1) % FIFO_SIZE; + rig_debug(RIG_DEBUG_VERBOSE, "%s: push %c (%d,%d)\n", __func__, msg[i], + fifo->head, fifo->tail); - if (fifo->tail == fifo->head) { return -RIG_EDOM; } + if (fifo->tail + 1 == fifo->head) { return -RIG_EDOM; } + + fifo->tail = (fifo->tail + 1) % HAMLIB_FIFO_SIZE; } return RIG_OK; @@ -31,7 +39,9 @@ char pop(FIFO *fifo) if (fifo->tail == fifo->head) { return -1; } char c = fifo->data[fifo->head]; - fifo->head = (fifo->head + 1) % FIFO_SIZE; + rig_debug(RIG_DEBUG_VERBOSE, "%s: pop %c (%d,%d)\n", __func__, c, fifo->head, + fifo->tail); + fifo->head = (fifo->head + 1) % HAMLIB_FIFO_SIZE; return c; } diff --git a/src/fifo.h b/src/fifo.h index 0eeaedb99..679950c59 100644 --- a/src/fifo.h +++ b/src/fifo.h @@ -1,12 +1,4 @@ -#define FIFO_SIZE 1024 - -typedef struct -{ - char data[FIFO_SIZE]; - int head; - int tail; -} FIFO; - void initFIFO(FIFO *fifo); +void resetFIFO(FIFO *fifo); int push(FIFO *fifo, const char *msg); char pop(FIFO *fifo); diff --git a/src/rig.c b/src/rig.c index c8c54c320..e47712d63 100644 --- a/src/rig.c +++ b/src/rig.c @@ -6818,7 +6818,7 @@ int HAMLIB_API rig_recv_dtmf(RIG *rig, vfo_t vfo, char *digits, int *length) int HAMLIB_API rig_send_morse(RIG *rig, vfo_t vfo, const char *msg) { const struct rig_caps *caps; - int retcode, rc2; + int retcode=RIG_EINTERNAL, rc2; vfo_t curr_vfo; ENTERFUNC; @@ -6843,10 +6843,13 @@ int HAMLIB_API rig_send_morse(RIG *rig, vfo_t vfo, const char *msg) if (vfo == RIG_VFO_CURR || vfo == rig->state.current_vfo) { +#if 0 LOCK(1); retcode = caps->send_morse(rig, vfo, msg); LOCK(0); - RETURNFUNC(retcode); +#endif + push(rig->state.fifo, msg); + RETURNFUNC(RIG_OK); } if (!caps->set_vfo) @@ -7980,28 +7983,35 @@ void *morse_data_handler(void *arg) RIG *rig = args->rig; struct rig_state *rs = &rig->state; int result; - FIFO fifo; rig_debug(RIG_DEBUG_VERBOSE, "%s: Starting morse data handler thread\n", __func__); - initFIFO(&fifo); + if (rig->state.fifo == NULL) rig->state.fifo = calloc(1,sizeof(FIFO)); + initFIFO(rig->state.fifo); while (rs->morse_data_handler_thread_run) { - char c[2]; - c[1] = 0; - while((c[0]=pop(&fifo)!=-1)) + char c[11]; // up to 10 chars to be sent + memset(c,0,sizeof(c)); + int n=0; + for(n=0;nstate.fifo))!=-1;++n); + + //while((c[0]=pop(rig->state.fifo))!=-1) + if (n > 0) { - result = rig_send_morse(rig, RIG_VFO_CURR, c); - if (result != RIG_OK) + rig_debug(RIG_DEBUG_ERR, "%s: c=%d\n", __func__, c[0]); + do { - rig_debug(RIG_DEBUG_ERR, "%s: error: %s\n", __func__, rigerror(result)); - push(&fifo, c); - hl_usleep(20*1000); - } + result = rig->caps->send_morse(rig, RIG_VFO_CURR, c); + if (result != RIG_OK) + { + rig_debug(RIG_DEBUG_ERR, "%s: error: %s\n", __func__, rigerror(result)); + hl_usleep(100*1000); + } + } while (result != RIG_OK); } - hl_usleep(20*1000); } + free(rig->state.fifo); pthread_exit(NULL); return NULL; }