Skip to content

Commit

Permalink
Fix rig_send_raw to return bytes read, fill buffer, and also work wit…
Browse files Browse the repository at this point in the history
…h fixed length response/null terminator

#1157
  • Loading branch information
mdblack98 committed Nov 22, 2022
1 parent 508eef3 commit edcbd17
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/iofunc.c
Original file line number Diff line number Diff line change
Expand Up @@ -952,12 +952,12 @@ int HAMLIB_API write_block_sync(hamlib_port_t *p, const unsigned char *txbuffer,
size_t count)
{

if (!p->asyncio)
if (p->asyncio)
{
return -RIG_EINTERNAL;
return (int) write(p->fd_sync_write, txbuffer, count);
}

return (int) write(p->fd_sync_write, txbuffer, count);
return (int) write(p->fd, txbuffer, count);
}

int HAMLIB_API write_block_sync_error(hamlib_port_t *p,
Expand Down
24 changes: 21 additions & 3 deletions src/rig.c
Original file line number Diff line number Diff line change
Expand Up @@ -7403,11 +7403,15 @@ extern int read_icom_frame(hamlib_port_t *p, const unsigned char rxbuffer[],
size_t rxbuffer_len);


// Returns # of bytes read
// reply_len should be max bytes expected + 1
// if term is null then will read reply_len bytes exactly and reply will not be null terminated
HAMLIB_EXPORT(int) rig_send_raw(RIG *rig, const unsigned char *send,
int send_len, unsigned char *reply, int reply_len, unsigned char *term)
{
struct rig_state *rs = &rig->state;
unsigned char buf[200];
int nbytes;
ENTERFUNC;
int retval = write_block_sync(&rs->rigport, send, send_len);

Expand All @@ -7431,22 +7435,36 @@ HAMLIB_EXPORT(int) rig_send_raw(RIG *rig, const unsigned char *send,
if (*term == 0xfd) // then we want an Icom frame
{
retval = read_icom_frame(&rs->rigport, buf, sizeof(buf));
nbytes = retval;
}
else if (term == NULL)
{
nbytes = read_string_direct(&rs->rigport, buf, reply_len, (const char *)term,
1, 0, 1);
}
else // we'll assume the provided terminator works
{
retval = read_string_direct(&rs->rigport, buf, sizeof(buf), (const char *)term,
nbytes = read_string_direct(&rs->rigport, buf, sizeof(buf), (const char *)term,
1, 0, 1);
}

if (retval != RIG_OK)
if (retval < RIG_OK)
{
rig_debug(RIG_DEBUG_ERR, "%s: write_block_sync() failed, result=%d\n", __func__,
retval);
RETURNFUNC(retval);
}
if (nbytes >= reply_len)
{
rig_debug(RIG_DEBUG_ERR, "%s: reply_len(%d) less than reply from rig(%d)\n", __func__, reply_len, nbytes);
return -RIG_EINVAL;
}
memcpy(reply,buf,reply_len-1);
}
else
RETURNFUNC(retval);

RETURNFUNC(retval);
RETURNFUNC(nbytes > 0? nbytes:-RIG_EPROTO);
}

HAMLIB_EXPORT(int) rig_set_lock_mode(RIG *rig, int mode)
Expand Down

0 comments on commit edcbd17

Please sign in to comment.