Removed kenwood_cmd function and replaced uses of it

The function kenwood_cmd doesn't add value  and it uses a reply buffer
that is  not required.  Also  the use of  a reply buffer  disables the
verification  of commands  that  have no  reply feature.   Substituted
kenwood_simple_cmd calls  that do  the job  correctly without  a reply
buffer.

Added delay to busy retry loop when sending commands to rig.

Implemented retries on wrong length repies from rig also with a delay.
Hamlib-3.0
Bill Somerville 2014-12-24 13:02:25 +00:00
rodzic a2f24678ca
commit 64d080af2e
6 zmienionych plików z 76 dodań i 76 usunięć

Wyświetl plik

@ -205,6 +205,7 @@ int kenwood_transaction(RIG *rig, const char *cmdstr, int cmd_len,
int len; int len;
int retry_read = 0; int retry_read = 0;
int reply_expected = data && *datasize > 0; int reply_expected = data && *datasize > 0;
size_t length = *datasize;
static char const verify[] = "AI;"; /* command we can always send static char const verify[] = "AI;"; /* command we can always send
to any rig even when the rig to any rig even when the rig
is busy */ is busy */
@ -235,6 +236,8 @@ int kenwood_transaction(RIG *rig, const char *cmdstr, int cmd_len,
len++; len++;
} }
*datasize = length; /* reset as may be a retry */
serial_flush(&rs->rigport); serial_flush(&rs->rigport);
retval = write_block(&rs->rigport, cmd, len); retval = write_block(&rs->rigport, cmd, len);
@ -256,7 +259,7 @@ int kenwood_transaction(RIG *rig, const char *cmdstr, int cmd_len,
retval = read_string(&rs->rigport, priv->info, reply_expected ? *datasize : strlen (verify) + 2, cmdtrm, strlen(cmdtrm)); retval = read_string(&rs->rigport, priv->info, reply_expected ? *datasize : strlen (verify) + 2, cmdtrm, strlen(cmdtrm));
if (retval < 0) { if (retval < 0) {
if (retry_read++ < rig->state.rigport.retry) if (retry_read++ < rig->caps->retry)
goto transaction_write; goto transaction_write;
goto transaction_quit; goto transaction_quit;
} }
@ -264,7 +267,7 @@ int kenwood_transaction(RIG *rig, const char *cmdstr, int cmd_len,
/* Check that command termination is correct */ /* Check that command termination is correct */
if (strchr(cmdtrm, priv->info[strlen(priv->info)-1])==NULL) { if (strchr(cmdtrm, priv->info[strlen(priv->info)-1])==NULL) {
rig_debug(RIG_DEBUG_ERR, "%s: Command is not correctly terminated '%s'\n", __func__, priv->info); rig_debug(RIG_DEBUG_ERR, "%s: Command is not correctly terminated '%s'\n", __func__, priv->info);
if (retry_read++ < rig->state.rigport.retry) if (retry_read++ < rig->caps->retry)
goto transaction_write; goto transaction_write;
retval = -RIG_EPROTO; retval = -RIG_EPROTO;
goto transaction_quit; goto transaction_quit;
@ -280,22 +283,26 @@ int kenwood_transaction(RIG *rig, const char *cmdstr, int cmd_len,
case 'O': case 'O':
/* Too many characters sent without a carriage return */ /* Too many characters sent without a carriage return */
rig_debug(RIG_DEBUG_VERBOSE, "%s: Overflow for '%s'\n", __func__, cmdstr); rig_debug(RIG_DEBUG_VERBOSE, "%s: Overflow for '%s'\n", __func__, cmdstr);
if (retry_read++ < rig->state.rigport.retry) if (retry_read++ < rig->caps->retry)
goto transaction_write; goto transaction_write;
retval = -RIG_EPROTO; retval = -RIG_EPROTO;
goto transaction_quit; goto transaction_quit;
case 'E': case 'E':
/* Communication error */ /* Communication error */
rig_debug(RIG_DEBUG_VERBOSE, "%s: Communication error for '%s'\n", __func__, cmdstr); rig_debug(RIG_DEBUG_VERBOSE, "%s: Communication error for '%s'\n", __func__, cmdstr);
if (retry_read++ < rig->state.rigport.retry) if (retry_read++ < rig->caps->retry)
goto transaction_write; goto transaction_write;
retval = -RIG_EIO; retval = -RIG_EIO;
goto transaction_quit; goto transaction_quit;
case '?': case '?':
/* Command not understood by rig */ /* Command not understood by rig or rig busy */
rig_debug(RIG_DEBUG_ERR, "%s: Unknown command '%s'\n", __func__, cmdstr); rig_debug(RIG_DEBUG_ERR, "%s: Unknown command or rig busy '%s'\n", __func__, cmdstr);
if (retry_read++ < rig->state.rigport.retry) if (retry_read++ < rig->caps->retry)
{
rig_debug(RIG_DEBUG_ERR, "%s: Retrying shortly\n", __func__);
usleep (rig->caps->timeout * 1000);
goto transaction_write; goto transaction_write;
}
retval = -RIG_ERJCTED; retval = -RIG_ERJCTED;
goto transaction_quit; goto transaction_quit;
} }
@ -317,15 +324,13 @@ int kenwood_transaction(RIG *rig, const char *cmdstr, int cmd_len,
rig_debug(RIG_DEBUG_ERR, "%s: wrong reply %c%c for command %c%c\n", rig_debug(RIG_DEBUG_ERR, "%s: wrong reply %c%c for command %c%c\n",
__func__, priv->info[0], priv->info[1], cmdstr[0], cmdstr[1]); __func__, priv->info[0], priv->info[1], cmdstr[0], cmdstr[1]);
if (retry_read++ < rig->state.rigport.retry) if (retry_read++ < rig->caps->retry)
goto transaction_write; goto transaction_write;
retval = -RIG_EPROTO; retval = -RIG_EPROTO;
goto transaction_quit; goto transaction_quit;
} }
if (reply_expected)
{
/* always give back a null terminated string without /* always give back a null terminated string without
* the command terminator. * the command terminator.
*/ */
@ -368,26 +373,35 @@ int kenwood_safe_transaction(RIG *rig, const char *cmd, char *buf,
{ {
rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__); rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);
if (!rig || !buf) if (!rig || !cmd)
return -RIG_EINVAL; return -RIG_EINVAL;
int err; int err;
int retry = 0;
if (expected == 0) if (expected == 0)
buf_size = 0; buf_size = 0;
err = kenwood_transaction(rig, cmd, strlen(cmd), buf, &buf_size); do
if (err != RIG_OK) {
size_t length = buf_size;
err = kenwood_transaction(rig, cmd, strlen(cmd), buf, &length);
if (err != RIG_OK) /* return immediately on error as any
retries handled at lower level */
return err; return err;
if (buf_size != expected) { if (length != expected) /* worth retrying as some rigs
occasionally send short results */
{
rig_debug(RIG_DEBUG_ERR, "%s: wrong answer; len for cmd %s: " rig_debug(RIG_DEBUG_ERR, "%s: wrong answer; len for cmd %s: "
"expected = %d, got %d\n", "expected = %d, got %d\n",
__func__, cmd, expected, buf_size); __func__, cmd, expected, length);
return -RIG_EPROTO; err = -RIG_EPROTO;
usleep (rig->caps->timeout * 1000);
} }
} while (err != RIG_OK && ++retry < rig->caps->retry);
return RIG_OK; return err;
} }
rmode_t kenwood2rmode(unsigned char mode, const rmode_t mode_table[]) rmode_t kenwood2rmode(unsigned char mode, const rmode_t mode_table[])

Wyświetl plik

@ -199,27 +199,14 @@ extern const struct rig_caps f6k_caps;
/* use when not interested in the answer, but want to check its len */ /* use when not interested in the answer, but want to check its len */
static int inline kenwood_simple_transaction(RIG *rig, const char *cmd, size_t expected) static int inline kenwood_simple_transaction(RIG *rig, const char *cmd, size_t expected)
{ {
char buf[20]; struct kenwood_priv_data *priv = rig->state.priv;
return kenwood_safe_transaction(rig, cmd, buf, sizeof(buf), expected); return kenwood_safe_transaction(rig, cmd, priv->info, KENWOOD_MAX_BUF_LEN, expected);
} }
/* no answer needed at all */ /* no answer needed at all */
static int inline kenwood_simple_cmd(RIG *rig, const char *cmd) static int inline kenwood_simple_cmd(RIG *rig, const char *cmd)
{ {
char buf[20]; return kenwood_safe_transaction(rig, cmd, NULL, 0, 0);
return kenwood_safe_transaction(rig, cmd, buf, sizeof(buf), 0);
}
/* answer is the same as the command */
static int inline kenwood_cmd(RIG *rig, const char *cmd)
{
char buf[20];
int lenz = strlen(cmd)+1;
if (lenz > sizeof(buf))
return -RIG_ENOMEM;
else
return kenwood_safe_transaction(rig, cmd, buf, sizeof(buf), lenz);
} }
#endif /* _KENWOOD_H */ #endif /* _KENWOOD_H */

Wyświetl plik

@ -197,7 +197,7 @@ th_set_freq(RIG *rig, vfo_t vfo, freq_t freq)
freq_sent = freq_sent >= MHz(470) ? (round(freq_sent/10000)*10000) : freq_sent; freq_sent = freq_sent >= MHz(470) ? (round(freq_sent/10000)*10000) : freq_sent;
sprintf(buf, "FQ %011"PRIll",%X", (int64_t) freq_sent, step); sprintf(buf, "FQ %011"PRIll",%X", (int64_t) freq_sent, step);
return kenwood_cmd(rig, buf); return kenwood_simple_cmd(rig, buf);
} }
/* /*
@ -270,7 +270,7 @@ th_set_mode(RIG *rig, vfo_t vfo, rmode_t mode, pbwidth_t width)
sprintf(mdbuf, "MD %c", kmode); sprintf(mdbuf, "MD %c", kmode);
retval = kenwood_cmd(rig, mdbuf); retval = kenwood_simple_cmd(rig, mdbuf);
if (retval != RIG_OK) if (retval != RIG_OK)
return retval; return retval;
@ -403,7 +403,7 @@ th_set_vfo(RIG *rig, vfo_t vfo)
return kenwood_wrong_vfo(__func__, vfo); return kenwood_wrong_vfo(__func__, vfo);
} }
return kenwood_cmd(rig, cmd); return kenwood_simple_cmd(rig, cmd);
} }
int int
@ -548,7 +548,7 @@ int tm_set_vfo_bc2 (RIG *rig, vfo_t vfo)
} }
sprintf(vfobuf, "VMC %d,%d", vfonum, vfomode); sprintf(vfobuf, "VMC %d,%d", vfonum, vfomode);
retval = kenwood_cmd(rig, vfobuf); retval = kenwood_simple_cmd(rig, vfobuf);
if (retval != RIG_OK) if (retval != RIG_OK)
return retval; return retval;
@ -556,7 +556,7 @@ int tm_set_vfo_bc2 (RIG *rig, vfo_t vfo)
return RIG_OK; return RIG_OK;
sprintf(vfobuf, "BC %d,%d", vfonum, txvfonum); sprintf(vfobuf, "BC %d,%d", vfonum, txvfonum);
retval = kenwood_cmd(rig, vfobuf); retval = kenwood_simple_cmd(rig, vfobuf);
if (retval != RIG_OK) if (retval != RIG_OK)
return retval; return retval;
@ -599,12 +599,12 @@ int th_set_split_vfo (RIG *rig, vfo_t vfo, split_t split, vfo_t txvfo)
/* Set VFO mode. To be done for TX vfo also? */ /* Set VFO mode. To be done for TX vfo also? */
sprintf(vfobuf, "VMC %d,0", vfonum); sprintf(vfobuf, "VMC %d,0", vfonum);
retval = kenwood_cmd(rig, vfobuf); retval = kenwood_simple_cmd(rig, vfobuf);
if (retval != RIG_OK) if (retval != RIG_OK)
return retval; return retval;
sprintf(vfobuf, "BC %d,%d", vfonum, txvfonum); sprintf(vfobuf, "BC %d,%d", vfonum, txvfonum);
retval = kenwood_cmd(rig, vfobuf); retval = kenwood_simple_cmd(rig, vfobuf);
if (retval != RIG_OK) if (retval != RIG_OK)
return retval; return retval;
@ -652,7 +652,7 @@ int th_get_split_vfo (RIG *rig, vfo_t vfo, split_t *split, vfo_t *txvfo)
int int
th_set_trn(RIG *rig, int trn) th_set_trn(RIG *rig, int trn)
{ {
return kenwood_cmd(rig, (trn == RIG_TRN_RIG) ? "AI 1" : "AI 0"); return kenwood_simple_cmd(rig, (trn == RIG_TRN_RIG) ? "AI 1" : "AI 0");
} }
/* /*
@ -747,7 +747,7 @@ th_get_func(RIG *rig, vfo_t vfo, setting_t func, int *status)
static int th_tburst(RIG *rig, vfo_t vfo, int status) static int th_tburst(RIG *rig, vfo_t vfo, int status)
{ {
return kenwood_cmd(rig, (status == 1) ? "TT" : "RX"); return kenwood_simple_cmd(rig, (status == 1) ? "TT" : "RX");
} }
/* /*
@ -766,7 +766,7 @@ static int th_set_kenwood_func(RIG *rig, const char *cmd, int status)
buf[BUFSZ-1] = '\0'; buf[BUFSZ-1] = '\0';
strncat(buf, status ? " 1" : " 0", BUFSZ-1); strncat(buf, status ? " 1" : " 0", BUFSZ-1);
return kenwood_cmd(rig, buf); return kenwood_simple_cmd(rig, buf);
} }
@ -901,11 +901,11 @@ th_set_parm(RIG *rig, setting_t parm, value_t val)
case RIG_PARM_APO: case RIG_PARM_APO:
if (val.i > 30) if (val.i > 30)
return kenwood_cmd(rig, "APO 2"); return kenwood_simple_cmd(rig, "APO 2");
else if (val.i > 0) else if (val.i > 0)
return kenwood_cmd(rig, "APO 1"); return kenwood_simple_cmd(rig, "APO 1");
else else
return kenwood_cmd(rig, "APO 0"); return kenwood_simple_cmd(rig, "APO 0");
default: default:
rig_debug(RIG_DEBUG_ERR, "%s: Unsupported parm %#x\n", __func__, parm); rig_debug(RIG_DEBUG_ERR, "%s: Unsupported parm %#x\n", __func__, parm);
@ -1089,29 +1089,29 @@ int th_set_level (RIG *rig, vfo_t vfo, setting_t level, value_t val)
(int)(val.f*(rig->caps->level_gran[LVL_RFPOWER].max.i - rig->caps->level_gran[LVL_RFPOWER].min.i)) (int)(val.f*(rig->caps->level_gran[LVL_RFPOWER].max.i - rig->caps->level_gran[LVL_RFPOWER].min.i))
+ rig->caps->level_gran[LVL_RFPOWER].min.i); + rig->caps->level_gran[LVL_RFPOWER].min.i);
return kenwood_cmd(rig, buf); return kenwood_simple_cmd(rig, buf);
case RIG_LEVEL_SQL : case RIG_LEVEL_SQL :
sprintf(buf, "SQ %c,%02x", vch, sprintf(buf, "SQ %c,%02x", vch,
(int)(val.f*(rig->caps->level_gran[LVL_SQL].max.i-rig->caps->level_gran[LVL_SQL].min.i)) (int)(val.f*(rig->caps->level_gran[LVL_SQL].max.i-rig->caps->level_gran[LVL_SQL].min.i))
+ rig->caps->level_gran[LVL_SQL].min.i); + rig->caps->level_gran[LVL_SQL].min.i);
return kenwood_cmd(rig, buf); return kenwood_simple_cmd(rig, buf);
case RIG_LEVEL_AF : case RIG_LEVEL_AF :
sprintf(buf, "AG %c,%02x", vch, (int)(val.f * 32.0)); sprintf(buf, "AG %c,%02x", vch, (int)(val.f * 32.0));
return kenwood_cmd(rig, buf); return kenwood_simple_cmd(rig, buf);
case RIG_LEVEL_ATT : case RIG_LEVEL_ATT :
sprintf(buf, "ATT %c", val.i ? '1' : '0'); sprintf(buf, "ATT %c", val.i ? '1' : '0');
return kenwood_cmd(rig, buf); return kenwood_simple_cmd(rig, buf);
case RIG_LEVEL_BALANCE : case RIG_LEVEL_BALANCE :
sprintf(buf, "BAL %c", '4' - (int) (val.f * ('4'-'0'))); sprintf(buf, "BAL %c", '4' - (int) (val.f * ('4'-'0')));
return kenwood_cmd(rig, buf); return kenwood_simple_cmd(rig, buf);
case RIG_LEVEL_VOXGAIN: case RIG_LEVEL_VOXGAIN:
sprintf(buf, "VXG %d", (int) (val.f * 9)); sprintf(buf, "VXG %d", (int) (val.f * 9));
return kenwood_cmd(rig, buf); return kenwood_simple_cmd(rig, buf);
case RIG_LEVEL_VOXDELAY: /* "VXD" */ case RIG_LEVEL_VOXDELAY: /* "VXD" */
return -RIG_ENIMPL; return -RIG_ENIMPL;
@ -1501,7 +1501,7 @@ th_set_ptt(RIG *rig, vfo_t vfo, ptt_t ptt)
{ {
rig_debug(RIG_DEBUG_TRACE, "%s: called\n", __func__); rig_debug(RIG_DEBUG_TRACE, "%s: called\n", __func__);
return kenwood_cmd(rig, (ptt == RIG_PTT_ON) ? "TX" : "RX"); return kenwood_simple_cmd(rig, (ptt == RIG_PTT_ON) ? "TX" : "RX");
} }
int th_get_dcd(RIG *rig, vfo_t vfo, dcd_t *dcd) int th_get_dcd(RIG *rig, vfo_t vfo, dcd_t *dcd)
@ -1558,13 +1558,13 @@ int th_vfo_op(RIG *rig, vfo_t vfo, vfo_op_t op)
switch (op) { switch (op) {
case RIG_OP_UP: case RIG_OP_UP:
return kenwood_cmd(rig, "UP"); return kenwood_simple_cmd(rig, "UP");
case RIG_OP_DOWN: case RIG_OP_DOWN:
return kenwood_cmd(rig, "DW"); return kenwood_simple_cmd(rig, "DW");
case RIG_OP_TO_VFO: case RIG_OP_TO_VFO:
return kenwood_cmd(rig, "MSH"); return kenwood_simple_cmd(rig, "MSH");
default: default:
return -RIG_EINVAL; return -RIG_EINVAL;
@ -2037,7 +2037,7 @@ int th_set_ant(RIG * rig, vfo_t vfo, ant_t ant)
return -RIG_EINVAL; return -RIG_EINVAL;
} }
return kenwood_cmd(rig, cmd); return kenwood_simple_cmd(rig, cmd);
} }

Wyświetl plik

@ -943,7 +943,7 @@ int tmd710_set_vfo (RIG *rig, vfo_t vfo)
} }
snprintf(vfobuf,9, "VM %d,%d", vfonum, vfomode); snprintf(vfobuf,9, "VM %d,%d", vfonum, vfomode);
retval = kenwood_cmd(rig, vfobuf); retval = kenwood_simple_cmd(rig, vfobuf);
if (retval != RIG_OK) if (retval != RIG_OK)
return retval; return retval;
@ -951,7 +951,7 @@ int tmd710_set_vfo (RIG *rig, vfo_t vfo)
return RIG_OK; return RIG_OK;
snprintf(vfobuf, 15, "BC %d,%d", vfonum, txvfonum); snprintf(vfobuf, 15, "BC %d,%d", vfonum, txvfonum);
retval = kenwood_cmd(rig, vfobuf); retval = kenwood_simple_cmd(rig, vfobuf);
if (retval != RIG_OK) if (retval != RIG_OK)
return retval; return retval;

Wyświetl plik

@ -30,8 +30,6 @@
#include <hamlib/rig.h> #include <hamlib/rig.h>
#include "kenwood.h" #include "kenwood.h"
#include "ic10.h"
#define TS570_ALL_MODES (RIG_MODE_AM|RIG_MODE_CW|RIG_MODE_SSB|RIG_MODE_FM|RIG_MODE_RTTY) #define TS570_ALL_MODES (RIG_MODE_AM|RIG_MODE_CW|RIG_MODE_SSB|RIG_MODE_FM|RIG_MODE_RTTY)
#define TS570_OTHER_TX_MODES (RIG_MODE_CW|RIG_MODE_SSB|RIG_MODE_FM|RIG_MODE_RTTY) #define TS570_OTHER_TX_MODES (RIG_MODE_CW|RIG_MODE_SSB|RIG_MODE_FM|RIG_MODE_RTTY)
@ -710,8 +708,8 @@ const struct rig_caps ts570s_caps = {
.serial_handshake = RIG_HANDSHAKE_NONE, .serial_handshake = RIG_HANDSHAKE_NONE,
.write_delay = 0, .write_delay = 0,
.post_write_delay = 30, .post_write_delay = 30,
.timeout = 1000, .timeout = 400,
.retry = 3, .retry = 5,
.has_get_func = TS570_FUNC_ALL, .has_get_func = TS570_FUNC_ALL,
.has_set_func = TS570_FUNC_ALL, .has_set_func = TS570_FUNC_ALL,
@ -825,7 +823,7 @@ const struct rig_caps ts570s_caps = {
.rig_init = kenwood_init, .rig_init = kenwood_init,
.rig_cleanup = kenwood_cleanup, .rig_cleanup = kenwood_cleanup,
.set_freq = kenwood_set_freq, .set_freq = kenwood_set_freq,
.get_freq = ic10_get_freq, .get_freq = kenwood_get_freq_if,
.set_rit = ts570_set_rit, .set_rit = ts570_set_rit,
.get_rit = kenwood_get_rit, .get_rit = kenwood_get_rit,
.set_xit = ts570_set_xit, .set_xit = ts570_set_xit,
@ -889,8 +887,8 @@ const struct rig_caps ts570d_caps = {
.serial_handshake = RIG_HANDSHAKE_NONE, .serial_handshake = RIG_HANDSHAKE_NONE,
.write_delay = 0, .write_delay = 0,
.post_write_delay = 30, .post_write_delay = 30,
.timeout = 1000, .timeout = 400,
.retry = 3, .retry = 5,
.has_get_func = TS570_FUNC_ALL, .has_get_func = TS570_FUNC_ALL,
.has_set_func = TS570_FUNC_ALL, .has_set_func = TS570_FUNC_ALL,
@ -1009,7 +1007,7 @@ const struct rig_caps ts570d_caps = {
.rig_init = kenwood_init, .rig_init = kenwood_init,
.rig_cleanup = kenwood_cleanup, .rig_cleanup = kenwood_cleanup,
.set_freq = kenwood_set_freq, .set_freq = kenwood_set_freq,
.get_freq = ic10_get_freq, .get_freq = kenwood_get_freq_if,
.set_rit = ts570_set_rit, .set_rit = ts570_set_rit,
.get_rit = kenwood_get_rit, .get_rit = kenwood_get_rit,
.set_xit = ts570_set_xit, .set_xit = ts570_set_xit,

Wyświetl plik

@ -532,6 +532,7 @@ const struct rig_caps ts870s_caps = {
.priv = (void *)&ts870s_priv_caps, .priv = (void *)&ts870s_priv_caps,
.rig_init = kenwood_init, .rig_init = kenwood_init,
.rig_open = kenwood_open,
.rig_cleanup = kenwood_cleanup, .rig_cleanup = kenwood_cleanup,
.set_freq = kenwood_set_freq, .set_freq = kenwood_set_freq,
.get_freq = kenwood_get_freq, .get_freq = kenwood_get_freq,