From d4cf8a04d71c9da9bdd704277a616574b4c973a3 Mon Sep 17 00:00:00 2001 From: Mike Black W9MDB Date: Wed, 4 Oct 2023 17:30:02 -0500 Subject: [PATCH] Change iofunc.c to read FLRig more efficiently https://github.com/Hamlib/Hamlib/issues/1394 --- include/hamlib/rig.h | 2 +- rigs/dummy/flrig.c | 6 +++--- src/iofunc.c | 46 ++++++++++++++++++++++++++------------------ src/rig.c | 8 ++++---- 4 files changed, 35 insertions(+), 27 deletions(-) diff --git a/include/hamlib/rig.h b/include/hamlib/rig.h index 949bdd288..6a38c6744 100644 --- a/include/hamlib/rig.h +++ b/include/hamlib/rig.h @@ -3635,7 +3635,7 @@ extern HAMLIB_EXPORT_VAR(char) debugmsgsave3[DEBUGMSGSAVE_SIZE]; // last-2 debu // Measuring elapsed time -- local variable inside function when macro is used #define ELAPSED1 struct timespec __begin; elapsed_ms(&__begin, HAMLIB_ELAPSED_SET); -#define ELAPSED2 rig_debug(RIG_DEBUG_TRACE, "%.*s%d:%s: elapsed=%.0lfms\n", rig->state.depth-1, spaces(), rig->state.depth, __func__, elapsed_ms(&__begin, HAMLIB_ELAPSED_GET)); +#define ELAPSED2 rig_debug(RIG_DEBUG_VERBOSE, "%.*s%d:%s: elapsed=%.0lfms\n", rig->state.depth-1, spaces(), rig->state.depth, __func__, elapsed_ms(&__begin, HAMLIB_ELAPSED_GET)); // use this instead of snprintf for automatic detection of buffer limit #define SNPRINTF(s,n,...) { snprintf(s,n,##__VA_ARGS__);if (strlen(s) > n-1) fprintf(stderr,"****** %s(%d): buffer overflow ******\n", __func__, __LINE__); } diff --git a/rigs/dummy/flrig.c b/rigs/dummy/flrig.c index b7c8538d9..a298f9bfa 100644 --- a/rigs/dummy/flrig.c +++ b/rigs/dummy/flrig.c @@ -422,7 +422,7 @@ static int read_transaction(RIG *rig, char *xml, int xml_len) { int retval; int retry; - char *delims; + //char *delims; char *terminator = ""; struct rig_state *rs = &rig->state; @@ -443,8 +443,8 @@ static int read_transaction(RIG *rig, char *xml, int xml_len) rig_debug(RIG_DEBUG_TRACE, "%s: before read_string\n", __func__); int len = read_string(&rs->rigport, (unsigned char *) tmp_buf, sizeof(tmp_buf), - "", - 16, 0, 1); + "", + 17, 0, 1); rig_debug(RIG_DEBUG_TRACE, "%s: string='%s'\n", __func__, tmp_buf); // if our first response we should see the HTTP header diff --git a/src/iofunc.c b/src/iofunc.c index 447c913d1..68f06c66c 100644 --- a/src/iofunc.c +++ b/src/iofunc.c @@ -1339,7 +1339,7 @@ static int read_string_generic(hamlib_port_t *p, memset(rxbuffer, 0, rxmax); short timeout_retries = p->timeout_retry; - HAMLIB_TRACE2; + //HAMLIB_TRACE2; while (total_count < rxmax - 1) // allow 1 byte for end-of-string { ssize_t rd_count = 0; @@ -1358,7 +1358,7 @@ static int read_string_generic(hamlib_port_t *p, rig_debug(RIG_DEBUG_CACHE, "%s(%d): retrying read timeout %d/%d timeout=%d\n", __func__, __LINE__, p->timeout_retry - timeout_retries, p->timeout_retry, p->timeout); hl_usleep(10 * 1000); - HAMLIB_TRACE2; + //HAMLIB_TRACE2; continue; } @@ -1416,12 +1416,12 @@ static int read_string_generic(hamlib_port_t *p, //rig_debug(RIG_DEBUG_ERR, "xs: avail=%d expected_len=%d, minlen=%d, direct=%d\n", __func__, avail, expected_len, minlen, direct); #endif #endif - HAMLIB_TRACE2; + //HAMLIB_TRACE2; shortcut: rd_count = port_read_generic(p, &rxbuffer[total_count], expected_len == 1 ? 1 : minlen, direct); - HAMLIB_TRACE2; -// rig_debug(RIG_DEBUG_VERBOSE, "%s: read %d bytes tot=%d\n", __func__, (int)rd_count, total_count); + //HAMLIB_TRACE2; +// rig_debug(RIG_DEBUG_VERBOSE, "%s: read %d bytes tot=%d\nrxbuffer=%s\n", __func__, (int)rd_count, total_count, rxbuffer); minlen -= rd_count; if (errno == EAGAIN) @@ -1433,7 +1433,7 @@ static int read_string_generic(hamlib_port_t *p, } while (++i < 10 && errno == EBUSY); // 50ms should be enough - HAMLIB_TRACE2; + //HAMLIB_TRACE2; /* if we get 0 bytes or an error something is wrong */ if (rd_count <= 0) @@ -1449,6 +1449,7 @@ static int read_string_generic(hamlib_port_t *p, return -RIG_EIO; } + //HAMLIB_TRACE2; // check to see if our string startis with \...if so we need more chars if (total_count == 0 && rxbuffer[total_count] == '\\') { rxmax = (rxmax - 1) * 5; } @@ -1456,7 +1457,27 @@ static int read_string_generic(hamlib_port_t *p, if (total_count == rxmax) { break; } + // special read for FLRig +// rig_debug(RIG_DEBUG_TRACE, "%s: stopset=%s\n", __func__, stopset); + if (strcmp(stopset, "") == 0 || timeout_retries <= 0) + { + //HAMLIB_TRACE2; + if (strstr((char*)rxbuffer, stopset)) + { + //HAMLIB_TRACE2; + break; + } + else { + //HAMLIB_TRACE2; + goto shortcut; + } + } + //else + //HAMLIB_TRACE2; + + + //HAMLIB_TRACE2; if (stopset && memchr(stopset, rxbuffer[total_count - 1], stopset_len)) { if (minlen == 1) { minlen = total_count; } @@ -1482,19 +1503,6 @@ static int read_string_generic(hamlib_port_t *p, rig_debug(RIG_DEBUG_VERBOSE, "%s: skipping single ';' chars at beginning of reply\n", __func__); } - // special read for FLRig - if (strcmp(stopset, "/methodResponse>") == 0 || timeout_retries <= 0) - { - if (strstr((char*)rxbuffer, stopset)) - { - HAMLIB_TRACE2; - } - - else { - HAMLIB_TRACE2; - goto shortcut; - } - } /* * Doesn't hurt anyway. But be aware, some binary protocols may have diff --git a/src/rig.c b/src/rig.c index c90c820a6..290781739 100644 --- a/src/rig.c +++ b/src/rig.c @@ -7964,18 +7964,18 @@ static int morse_data_handler_stop(RIG *rig) rs->morse_data_handler_priv_data; // wait until fifo queue is flushed - HAMLIB_TRACE; + //HAMLIB_TRACE; hl_usleep(100*1000); - HAMLIB_TRACE; + //HAMLIB_TRACE; while(peek(rig->state.fifo_morse) >= 0) { HAMLIB_TRACE; rig_debug(RIG_DEBUG_TRACE, "%s: waiting for fifo queue to flush\n", __func__); hl_usleep(100*1000); } - HAMLIB_TRACE; + //HAMLIB_TRACE; hl_usleep(100*1000); - HAMLIB_TRACE; + //HAMLIB_TRACE; if (morse_data_handler_priv != NULL) { if (morse_data_handler_priv->thread_id != 0)