make select() watch for errors

git-svn-id: https://hamlib.svn.sourceforge.net/svnroot/hamlib/trunk@2427 7ae35d74-ebe9-4afe-98af-79ac388436b8
Hamlib-1.2.8
Stéphane Fillod, F8CFE 2008-10-27 22:20:21 +00:00
rodzic 2bdd2c910b
commit 638628e18d
1 zmienionych plików z 67 dodań i 52 usunięć

Wyświetl plik

@ -2,7 +2,7 @@
* Hamlib Interface - generic file based io functions * Hamlib Interface - generic file based io functions
* Copyright (c) 2000-2008 by Stephane Fillod and Frank Singleton * Copyright (c) 2000-2008 by Stephane Fillod and Frank Singleton
* *
* $Id: iofunc.c,v 1.18 2008-10-26 10:47:33 fillods Exp $ * $Id: iofunc.c,v 1.19 2008-10-27 22:20:21 fillods Exp $
* *
* This library is free software; you can redistribute it and/or modify * This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as * it under the terms of the GNU Library General Public License as
@ -170,13 +170,11 @@ int HAMLIB_API write_block(hamlib_port_t *p, const char *txbuffer, size_t count)
int HAMLIB_API read_block(hamlib_port_t *p, char *rxbuffer, size_t count) int HAMLIB_API read_block(hamlib_port_t *p, char *rxbuffer, size_t count)
{ {
fd_set rfds; fd_set rfds, efds;
struct timeval tv, tv_timeout; struct timeval tv, tv_timeout;
int rd_count, total_count = 0; int rd_count, total_count = 0;
int retval; int retval;
FD_ZERO(&rfds);
FD_SET(p->fd, &rfds);
/* /*
* Wait up to timeout ms. * Wait up to timeout ms.
@ -185,34 +183,43 @@ int HAMLIB_API read_block(hamlib_port_t *p, char *rxbuffer, size_t count)
tv_timeout.tv_usec = (p->timeout%1000)*1000; tv_timeout.tv_usec = (p->timeout%1000)*1000;
while (count > 0) { while (count > 0) {
tv = tv_timeout; /* select may have updated it */ tv = tv_timeout; /* select may have updated it */
retval = select(p->fd+1, &rfds, NULL, NULL, &tv); FD_ZERO(&rfds);
if (retval == 0) { FD_SET(p->fd, &rfds);
dump_hex((unsigned char *) rxbuffer, total_count); efds = rfds;
rig_debug(RIG_DEBUG_WARN, "read_block: timedout after %d chars\n",
total_count);
return -RIG_ETIMEOUT;
}
if (retval < 0) {
dump_hex((unsigned char *) rxbuffer, total_count);
rig_debug(RIG_DEBUG_ERR,"read_block: select error after %d chars: "
"%s\n", total_count, strerror(errno));
return -RIG_EIO;
}
/* retval = select(p->fd+1, &rfds, NULL, &efds, &tv);
* grab bytes from the rig if (retval == 0) {
* The file descriptor must have been set up non blocking. dump_hex((unsigned char *) rxbuffer, total_count);
*/ rig_debug(RIG_DEBUG_WARN, "read_block: timedout after %d chars\n",
rd_count = read(p->fd, rxbuffer+total_count, count); total_count);
if (rd_count < 0) { return -RIG_ETIMEOUT;
rig_debug(RIG_DEBUG_ERR, "read_block: read failed - %s\n", }
strerror(errno)); if (retval < 0) {
return -RIG_EIO; dump_hex((unsigned char *) rxbuffer, total_count);
} rig_debug(RIG_DEBUG_ERR,"read_block: select error after %d chars: "
total_count += rd_count; "%s\n", total_count, strerror(errno));
count -= rd_count; return -RIG_EIO;
}
if (FD_ISSET(p->fd, &efds)) {
rig_debug(RIG_DEBUG_ERR, "%s: fd error after %d chars\n",
__FUNCTION__, total_count);
return -RIG_EIO;
}
/*
* grab bytes from the rig
* The file descriptor must have been set up non blocking.
*/
rd_count = read(p->fd, rxbuffer+total_count, count);
if (rd_count < 0) {
rig_debug(RIG_DEBUG_ERR, "read_block: read failed - %s\n",
strerror(errno));
return -RIG_EIO;
}
total_count += rd_count;
count -= rd_count;
} }
rig_debug(RIG_DEBUG_TRACE,"RX %d bytes\n",total_count); rig_debug(RIG_DEBUG_TRACE,"RX %d bytes\n",total_count);
@ -249,14 +256,11 @@ int HAMLIB_API read_block(hamlib_port_t *p, char *rxbuffer, size_t count)
int HAMLIB_API read_string(hamlib_port_t *p, char *rxbuffer, size_t rxmax, const char *stopset, int HAMLIB_API read_string(hamlib_port_t *p, char *rxbuffer, size_t rxmax, const char *stopset,
int stopset_len) int stopset_len)
{ {
fd_set rfds; fd_set rfds, efds;
struct timeval tv, tv_timeout; struct timeval tv, tv_timeout;
int rd_count, total_count = 0; int rd_count, total_count = 0;
int retval; int retval;
FD_ZERO(&rfds);
FD_SET(p->fd, &rfds);
/* /*
* Wait up to timeout ms. * Wait up to timeout ms.
*/ */
@ -264,32 +268,43 @@ int HAMLIB_API read_string(hamlib_port_t *p, char *rxbuffer, size_t rxmax, const
tv_timeout.tv_usec = (p->timeout%1000)*1000; tv_timeout.tv_usec = (p->timeout%1000)*1000;
while (total_count < rxmax-1) { while (total_count < rxmax-1) {
tv = tv_timeout; /* select may have updated it */ tv = tv_timeout; /* select may have updated it */
retval = select(p->fd+1, &rfds, NULL, NULL, &tv); FD_ZERO(&rfds);
FD_SET(p->fd, &rfds);
efds = rfds;
retval = select(p->fd+1, &rfds, NULL, &efds, &tv);
if (retval == 0) /* Timed out */ if (retval == 0) /* Timed out */
break; break;
if (retval < 0) { if (retval < 0) {
dump_hex((unsigned char *) rxbuffer, total_count); dump_hex((unsigned char *) rxbuffer, total_count);
rig_debug(RIG_DEBUG_ERR, "%s: select error after %d chars: %s\n", rig_debug(RIG_DEBUG_ERR, "%s: select error after %d chars: %s\n",
__FUNCTION__, total_count, strerror(errno)); __FUNCTION__, total_count, strerror(errno));
return -RIG_EIO; return -RIG_EIO;
} }
/*
* read 1 character from the rig, (check if in stop set) if (FD_ISSET(p->fd, &efds)) {
* The file descriptor must have been set up non blocking. rig_debug(RIG_DEBUG_ERR, "%s: fd error after %d chars\n",
*/ __FUNCTION__, total_count);
return -RIG_EIO;
}
/*
* read 1 character from the rig, (check if in stop set)
* The file descriptor must have been set up non blocking.
*/
rd_count = read(p->fd, &rxbuffer[total_count], 1); rd_count = read(p->fd, &rxbuffer[total_count], 1);
if (rd_count < 0) { if (rd_count < 0) {
dump_hex((unsigned char *) rxbuffer, total_count); dump_hex((unsigned char *) rxbuffer, total_count);
rig_debug(RIG_DEBUG_ERR, "%s: read failed - %s\n",__FUNCTION__, rig_debug(RIG_DEBUG_ERR, "%s: read failed - %s\n",__FUNCTION__,
strerror(errno)); strerror(errno));
return -RIG_EIO; return -RIG_EIO;
} }
++total_count; ++total_count;
if (stopset && memchr(stopset, rxbuffer[total_count-1], stopset_len)) if (stopset && memchr(stopset, rxbuffer[total_count-1], stopset_len))
break; break;
} }
/* /*
* Doesn't hurt anyway. But be aware, some binary protocols may have * Doesn't hurt anyway. But be aware, some binary protocols may have