From a0711bfa715faf6985a232c2caee423d94f7e1d7 Mon Sep 17 00:00:00 2001 From: Mike Black W9MDB Date: Fri, 25 Nov 2022 17:39:35 -0600 Subject: [PATCH] Fix send_raw and add some debug to it https://github.com/Hamlib/Hamlib/issues/1157 --- NEWS | 1 + src/rig.c | 12 +++++++++-- tests/rigctl_parse.c | 49 +++++++++++++++++++++++++++++++++++--------- 3 files changed, 50 insertions(+), 12 deletions(-) diff --git a/NEWS b/NEWS index 234db18b0..f6b4d9a2f 100644 --- a/NEWS +++ b/NEWS @@ -18,6 +18,7 @@ Version 4.6 Version 4.5.1 * 2022-XX-XX + * Added send_raw to rigctl * Fix AGC Level dumpcaps on most rigs * Fix rig_send_raw to return bytes read, fill buffer, and also work with fixed length response/null terminator * Change all Kenwood rigs to 500ms serial timeout diff --git a/src/rig.c b/src/rig.c index d014f0606..5a351f4f1 100644 --- a/src/rig.c +++ b/src/rig.c @@ -7414,11 +7414,15 @@ HAMLIB_EXPORT(int) rig_send_raw(RIG *rig, const unsigned char *send, int nbytes; ENTERFUNC; - if (rig->caps->rig_model == RIG_MODEL_DUMMY || rig->caps->rig_model == RIG_MODEL_NONE) + if (rig->caps->rig_model == RIG_MODEL_DUMMY + || rig->caps->rig_model == RIG_MODEL_NONE) { - rig_debug(RIG_DEBUG_ERR, "%s: not implemented for model %s\n", __func__, rig->caps->model_name); + rig_debug(RIG_DEBUG_ERR, "%s: not implemented for model %s\n", __func__, + rig->caps->model_name); return -RIG_ENAVAIL; } + + rig_debug(RIG_DEBUG_VERBOSE, "%s: writing %d bytes\n", __func__, send_len); int retval = write_block_sync(&rs->rigport, send, send_len); if (retval < 0) @@ -7440,16 +7444,20 @@ HAMLIB_EXPORT(int) rig_send_raw(RIG *rig, const unsigned char *send, if (*term == 0xfd) // then we want an Icom frame { + rig_debug(RIG_DEBUG_VERBOSE, "%s: reading icom frame\n", __func__); retval = read_icom_frame(&rs->rigport, buf, sizeof(buf)); nbytes = retval; } else if (term == NULL) { + rig_debug(RIG_DEBUG_VERBOSE, "%s: reading binary frame\n", __func__); nbytes = read_string_direct(&rs->rigport, buf, reply_len, (const char *)term, 1, 0, 1); } else // we'll assume the provided terminator works { + rig_debug(RIG_DEBUG_VERBOSE, "%s: reading frame terminated by '%s'\n", __func__, + term); nbytes = read_string_direct(&rs->rigport, buf, sizeof(buf), (const char *)term, 1, 0, 1); } diff --git a/tests/rigctl_parse.c b/tests/rigctl_parse.c index 2fa9ab971..f9b9332df 100644 --- a/tests/rigctl_parse.c +++ b/tests/rigctl_parse.c @@ -371,7 +371,7 @@ static struct test_table test_list[] = { 0xa1, "get_separator", ACTION(get_separator), ARG_NOVFO, "Separator" }, { 0xa2, "set_lock_mode", ACTION(set_lock_mode), ARG_IN | ARG_NOVFO, "Locked" }, { 0xa3, "get_lock_mode", ACTION(get_lock_mode), ARG_NOVFO, "Locked" }, - { 0xa4, "send_raw", ACTION(send_raw), ARG_NOVFO | ARG_IN | ARG_OUT, "Send raw command" }, + { 0xa4, "send_raw", ACTION(send_raw), ARG_NOVFO | ARG_IN | ARG_OUT, "Send raw answer" }, { 0x00, "", NULL }, }; @@ -5352,36 +5352,65 @@ static int parse_hex(const char *s, unsigned char *buf, int len) int i; buf[0] = 0; char *s2 = strdup(s); - char *p = strtok(s2,";"); - while(p) + char *p = strtok(s2, ";"); + + while (p) { unsigned int val; - sscanf(p,"0x%x", &val); + sscanf(p, "0x%x", &val); buf[i++] = val; p = strtok(NULL, ";"); } + free(s2); return i; } /* 0xa4 */ declare_proto_rig(send_raw) { - int retval; + int reply_len; unsigned char term[] = ";"; unsigned char buf[100]; unsigned char send[100]; - unsigned char *sendp = (unsigned char*)arg1; + unsigned char *sendp = (unsigned char *)arg1; int arg1_len = strlen(arg1); + int hex_flag = 0; - if (strncmp(arg1, "0x", 2)==0) { - arg1_len = parse_hex(arg1,send,sizeof(send)); + if (strncmp(arg1, "0x", 2) == 0) + { + arg1_len = parse_hex(arg1, send, sizeof(send)); sendp = send; + hex_flag = 1; } + rig_debug(RIG_DEBUG_TRACE, "%s:\n", __func__); - retval = rig_send_raw(rig, (unsigned char*)sendp, arg1_len, buf, sizeof(buf),term); + reply_len = rig_send_raw(rig, (unsigned char *)sendp, arg1_len, buf, + sizeof(buf), term); + if ((interactive && prompt) || (interactive && !prompt && ext_resp)) { fprintf(fout, "%s: ", cmd->arg1); } - return retval; + + if (reply_len == 0) + { + fprintf(fout, "No answer\n"); + } + else if (hex_flag) + { + int i; + + for (i = 0; i < reply_len; ++i) + { + fprintf(fout, "0x%02x ", buf[i]); + } + + fprintf(fout, "\n"); + } + else + { + fprintf(fout, "%s\n", buf); + } + + return RIG_OK; }