Add a timeout parameter to blocking serial calls.

Set this new parameter to 0 (no timeout) at every call site. This is
consistent with previous behaviour, so cannot cause any regressions.

Waiting forever for a serial operation is clearly always wrong. Without
specific knowledge of each device and driver however, I can't choose
appropriate timeouts for each call. The maintainers of these drivers
will need to do so, and also add appropriate handling of timeouts.

When this commit is merged, a bug should be entered for each driver
that is touched by it.
This commit is contained in:
Martin Ling 2014-09-28 13:05:33 +01:00 committed by Bert Vermeulen
parent 7ce59a3133
commit eead278242
21 changed files with 53 additions and 49 deletions

View File

@ -116,7 +116,7 @@ static GSList *scan(GSList *options)
return NULL;
serial_flush(serial);
if (serial_write_blocking(serial, "*IDN?\r\n", 7) < 7) {
if (serial_write_blocking(serial, "*IDN?\r\n", 7, 0) < 7) {
sr_err("Unable to send identification string.");
return NULL;
}

View File

@ -140,7 +140,7 @@ static int agdmm_send(const struct sr_dev_inst *sdi, const char *cmd)
strcat(buf, "\r\n");
else
strcat(buf, "\n\r\n");
if (serial_write_blocking(serial, buf, strlen(buf)) < (int)strlen(buf)) {
if (serial_write_blocking(serial, buf, strlen(buf), 0) < (int)strlen(buf)) {
sr_err("Failed to send.");
return SR_ERR;
}

View File

@ -130,7 +130,7 @@ static GSList *scan(GSList *options, int modelid)
memset(packet, 0, PACKET_SIZE);
packet[0] = 0xaa;
packet[1] = 0xaa;
if (serial_write_blocking(serial, packet, PACKET_SIZE) < PACKET_SIZE) {
if (serial_write_blocking(serial, packet, PACKET_SIZE, 0) < PACKET_SIZE) {
sr_err("Unable to write while probing for hardware.");
return NULL;
}

View File

@ -86,7 +86,7 @@ SR_PRIV void send_packet(const struct sr_dev_inst *sdi, uint8_t *packet)
struct sr_serial_dev_inst *serial;
serial = sdi->conn;
if (serial_write_blocking(serial, packet, PACKET_SIZE) < PACKET_SIZE)
if (serial_write_blocking(serial, packet, PACKET_SIZE, 0) < PACKET_SIZE)
sr_dbg("Failed to send packet.");
dump_packet("sent", packet);
}

View File

@ -78,7 +78,7 @@ static int bm_send_command(uint8_t command, uint8_t arg1, uint8_t arg2,
/* TODO: How to compute the checksum? Hardware seems to ignore it. */
/* Request reading. */
written = serial_write_blocking(serial, &cmdout, sizeof(cmdout));
written = serial_write_blocking(serial, &cmdout, sizeof(cmdout), 0);
if (written != sizeof(cmdout))
return SR_ERR;

View File

@ -31,7 +31,7 @@ static int center_send(struct sr_serial_dev_inst *serial, const char *cmd)
{
int ret;
if ((ret = serial_write_blocking(serial, cmd, strlen(cmd))) < 0) {
if ((ret = serial_write_blocking(serial, cmd, strlen(cmd), 0)) < 0) {
sr_err("Error sending '%s' command: %d.", cmd, ret);
return SR_ERR;
}

View File

@ -207,7 +207,7 @@ SR_PRIV int colead_slm_receive_data(int fd, int revents, void *cb_data)
* we don't want it. */
return TRUE;
/* Got 0x10, "measurement ready". */
if (serial_write_blocking(serial, "\x20", 1) < 1)
if (serial_write_blocking(serial, "\x20", 1, 0) < 1)
sr_err("unable to send command");
else {
devc->state = COMMAND_SENT;

View File

@ -48,7 +48,7 @@ SR_PRIV int send_msg1(const struct sr_dev_inst *sdi, char cmd, int param)
sr_spew("send_msg1(): %c%c%c%c\\r", buf[0], buf[1], buf[2], buf[3]);
if (serial_write_blocking(serial, buf, sizeof(buf)) < (int)sizeof(buf)) {
if (serial_write_blocking(serial, buf, sizeof(buf), 0) < (int)sizeof(buf)) {
sr_err("Write error for cmd=%c", cmd);
return SR_ERR;
}

View File

@ -90,7 +90,7 @@ static GSList *fluke_scan(const char *conn, const char *serialcomm)
while (!devices && retry < 3) {
retry++;
serial_flush(serial);
if (serial_write_blocking(serial, "ID\r", 3) < 0) {
if (serial_write_blocking(serial, "ID\r", 3, 0) < 0) {
sr_err("Unable to send ID string");
continue;
}
@ -286,7 +286,7 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
serial_source_add(sdi->session, serial, G_IO_IN, 50,
fluke_receive_data, (void *)sdi);
if (serial_write_blocking(serial, "QM\r", 3) < 0) {
if (serial_write_blocking(serial, "QM\r", 3, 0) < 0) {
sr_err("Unable to send QM.");
return SR_ERR;
}

View File

@ -456,7 +456,7 @@ static void handle_line(const struct sr_dev_inst *sdi)
/* Slip the request in now, before the main
* timer loop asks for metadata again. */
n = sprintf(cmd, "QM %d\r", devc->meas_type);
if (serial_write_blocking(serial, cmd, n) < 0)
if (serial_write_blocking(serial, cmd, n, 0) < 0)
sr_err("Unable to send QM (measurement).");
}
} else {
@ -525,7 +525,7 @@ SR_PRIV int fluke_receive_data(int fd, int revents, void *cb_data)
* out-of-sync or temporary disconnect issues. */
if ((devc->expect_response == FALSE && elapsed > devc->profile->poll_period)
|| elapsed > devc->profile->timeout) {
if (serial_write_blocking(serial, "QM\r", 3) < 0)
if (serial_write_blocking(serial, "QM\r", 3, 0) < 0)
sr_err("Unable to send QM.");
devc->cmd_sent_at = now;
devc->expect_response = TRUE;

View File

@ -1307,7 +1307,7 @@ int req_meas14(const struct sr_dev_inst *sdi)
devc->cmd_idx = 0;
create_cmd_14(devc->addr, 8, params, msg);
devc->req_sent_at = g_get_monotonic_time();
if (serial_write_blocking(serial, msg, sizeof(msg)) < (int)sizeof(msg)) {
if (serial_write_blocking(serial, msg, sizeof(msg), 0) < (int)sizeof(msg)) {
return SR_ERR;
}
@ -1336,13 +1336,13 @@ int req_stat14(const struct sr_dev_inst *sdi, gboolean power_on)
if (power_on) {
sr_info("Write some data and wait 3s to turn on powered off device...");
if (serial_write_blocking(serial, msg, sizeof(msg)) < 0)
if (serial_write_blocking(serial, msg, sizeof(msg), 0) < 0)
return SR_ERR;
g_usleep(1*1000*1000);
if (serial_write_blocking(serial, msg, sizeof(msg)) < 0)
if (serial_write_blocking(serial, msg, sizeof(msg), 0) < 0)
return SR_ERR;
g_usleep(1*1000*1000);
if (serial_write_blocking(serial, msg, sizeof(msg)) < 0)
if (serial_write_blocking(serial, msg, sizeof(msg), 0) < 0)
return SR_ERR;
g_usleep(1*1000*1000);
serial_flush(serial);
@ -1350,7 +1350,7 @@ int req_stat14(const struct sr_dev_inst *sdi, gboolean power_on)
/* Write message and wait for reply */
devc->req_sent_at = g_get_monotonic_time();
if (serial_write_blocking(serial, msg, sizeof(msg)) < (int)sizeof(msg)) {
if (serial_write_blocking(serial, msg, sizeof(msg), 0) < (int)sizeof(msg)) {
return SR_ERR;
}
@ -1529,7 +1529,7 @@ SR_PRIV int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *s
params[0] = 5;
params[1] = 5;
create_cmd_14(devc->addr, 6, params, msg);
if (serial_write_blocking(sdi->conn, msg, sizeof(msg)) < 0)
if (serial_write_blocking(sdi->conn, msg, sizeof(msg), 0) < 0)
return SR_ERR;
else
g_usleep(2000000); /* Wait to ensure transfer before interface switched off. */

View File

@ -43,7 +43,7 @@ SR_PRIV int hcs_send_cmd(struct sr_serial_dev_inst *serial, const char *cmd, ...
sr_dbg("Sending '%s'.", cmd_esc);
g_free(cmd_esc);
if ((ret = serial_write_blocking(serial, cmdbuf, strlen(cmdbuf))) < 0) {
if ((ret = serial_write_blocking(serial, cmdbuf, strlen(cmdbuf), 0)) < 0) {
sr_err("Error sending command: %d.", ret);
return ret;
}
@ -72,7 +72,7 @@ SR_PRIV int hcs_read_reply(struct sr_serial_dev_inst *serial, int lines, char* b
return SR_ERR_ARG;
while ((l_recv < lines) && (bufpos < (buflen + 1))) {
retc = serial_read_blocking(serial, &buf[bufpos], 1);
retc = serial_read_blocking(serial, &buf[bufpos], 1, 0);
if (retc != 1)
return SR_ERR;
if (buf[bufpos] == '\r')
@ -177,7 +177,7 @@ static int handle_new_data(struct sr_dev_inst *sdi)
devc = sdi->priv;
serial = sdi->conn;
len = serial_read_blocking(serial, devc->buf + devc->buflen, 1);
len = serial_read_blocking(serial, devc->buf + devc->buflen, 1, 0);
if (len < 1)
return SR_ERR;

View File

@ -24,7 +24,7 @@ static int mic_send(struct sr_serial_dev_inst *serial, const char *cmd)
{
int ret;
if ((ret = serial_write_blocking(serial, cmd, strlen(cmd))) < 0) {
if ((ret = serial_write_blocking(serial, cmd, strlen(cmd), 0)) < 0) {
sr_err("Error sending '%s' command: %d.", cmd, ret);
return SR_ERR;
}

View File

@ -146,7 +146,7 @@ SR_PRIV int lps_send_va(struct sr_serial_dev_inst *serial, const char* fmt, va_l
sr_spew("lps_send_va: \"%s\"", buf);
retc = serial_write_blocking(serial, buf, strlen(buf));
retc = serial_write_blocking(serial, buf, strlen(buf), 0);
if (retc < 0)
return SR_ERR;

View File

@ -129,7 +129,7 @@ static GSList *do_scan(struct sr_dev_driver* drv, GSList *options)
nmadmm_requests[NMADMM_REQ_IDN].req_str);
g_usleep(150 * 1000); /* Wait a little to allow serial port to settle. */
for (cnt = 0; cnt < 7; cnt++) {
if (serial_write_blocking(serial, req, strlen(req)) < 0) {
if (serial_write_blocking(serial, req, strlen(req), 0) < 0) {
sr_err("Unable to send identification request.");
return NULL;
}

View File

@ -48,7 +48,7 @@ static int nma_send_req(const struct sr_dev_inst *sdi, int req, char *params)
devc->last_req = req;
devc->last_req_pending = TRUE;
if (serial_write_blocking(serial, buf, len) < 0) {
if (serial_write_blocking(serial, buf, len, 0) < 0) {
sr_err("Unable to send request.");
devc->last_req_pending = FALSE;
return SR_ERR;

View File

@ -161,7 +161,7 @@ static GSList *scan(GSList *options)
if (probefd.revents != G_IO_IN)
return NULL;
if (serial_read_blocking(serial, buf, 4) != 4)
if (serial_read_blocking(serial, buf, 4, 0) != 4)
return NULL;
if (strncmp(buf, "1SLO", 4) && strncmp(buf, "1ALS", 4))
return NULL;

View File

@ -30,7 +30,7 @@ SR_PRIV int send_shortcommand(struct sr_serial_dev_inst *serial,
sr_dbg("Sending cmd 0x%.2x.", command);
buf[0] = command;
if (serial_write_blocking(serial, buf, 1) != 1)
if (serial_write_blocking(serial, buf, 1, 0) != 1)
return SR_ERR;
return SR_OK;
@ -48,7 +48,7 @@ SR_PRIV int send_longcommand(struct sr_serial_dev_inst *serial,
buf[2] = data[1];
buf[3] = data[2];
buf[4] = data[3];
if (serial_write_blocking(serial, buf, 5) != 5)
if (serial_write_blocking(serial, buf, 5, 0) != 5)
return SR_ERR;
return SR_OK;
@ -155,7 +155,7 @@ SR_PRIV struct sr_dev_inst *get_metadata(struct sr_serial_dev_inst *serial)
key = 0xff;
while (key) {
if (serial_read_blocking(serial, &key, 1) != 1)
if (serial_read_blocking(serial, &key, 1, 0) != 1)
break;
if (key == 0x00) {
sr_dbg("Got metadata key 0x00, metadata ends.");
@ -167,7 +167,7 @@ SR_PRIV struct sr_dev_inst *get_metadata(struct sr_serial_dev_inst *serial)
case 0:
/* NULL-terminated string */
tmp_str = g_string_new("");
while (serial_read_blocking(serial, &tmp_c, 1) == 1 && tmp_c != '\0')
while (serial_read_blocking(serial, &tmp_c, 1, 0) == 1 && tmp_c != '\0')
g_string_append_c(tmp_str, tmp_c);
sr_dbg("Got metadata key 0x%.2x value '%s'.",
key, tmp_str->str);
@ -199,7 +199,7 @@ SR_PRIV struct sr_dev_inst *get_metadata(struct sr_serial_dev_inst *serial)
break;
case 1:
/* 32-bit unsigned integer */
if (serial_read_blocking(serial, &tmp_int, 4) != 4)
if (serial_read_blocking(serial, &tmp_int, 4, 0) != 4)
break;
tmp_int = RB32(&tmp_int);
sr_dbg("Got metadata key 0x%.2x value 0x%.8x.",
@ -238,7 +238,7 @@ SR_PRIV struct sr_dev_inst *get_metadata(struct sr_serial_dev_inst *serial)
break;
case 2:
/* 8-bit unsigned integer */
if (serial_read_blocking(serial, &tmp_c, 1) != 1)
if (serial_read_blocking(serial, &tmp_c, 1, 0) != 1)
break;
sr_dbg("Got metadata key 0x%.2x value 0x%.2x.",
key, tmp_c);

View File

@ -133,14 +133,14 @@ int tondaj_sl_814_receive_data(int fd, int revents, void *cb_data)
buf[2] = 0x0d;
sr_spew("Sending init command: %02x %02x %02x.",
buf[0], buf[1], buf[2]);
if ((ret = serial_write_blocking(serial, buf, 3)) < 0) {
if ((ret = serial_write_blocking(serial, buf, 3, 0)) < 0) {
sr_err("Error sending init command: %d.", ret);
return FALSE;
}
devc->state = GET_INIT_REPLY;
} else if (devc->state == GET_INIT_REPLY) {
/* If we just sent the "init" command, get its reply. */
if ((ret = serial_read_blocking(serial, buf, 2)) < 0) {
if ((ret = serial_read_blocking(serial, buf, 2, 0)) < 0) {
sr_err("Error reading init reply: %d.", ret);
return FALSE;
}
@ -159,7 +159,7 @@ int tondaj_sl_814_receive_data(int fd, int revents, void *cb_data)
buf[2] = 0x0d;
sr_spew("Sending data request command: %02x %02x %02x.",
buf[0], buf[1], buf[2]);
if ((ret = serial_write_blocking(serial, buf, 3)) < 0) {
if ((ret = serial_write_blocking(serial, buf, 3, 0)) < 0) {
sr_err("Error sending request command: %d.", ret);
return FALSE;
}

View File

@ -630,11 +630,11 @@ SR_PRIV int serial_open(struct sr_serial_dev_inst *serial, int flags);
SR_PRIV int serial_close(struct sr_serial_dev_inst *serial);
SR_PRIV int serial_flush(struct sr_serial_dev_inst *serial);
SR_PRIV int serial_write_blocking(struct sr_serial_dev_inst *serial,
const void *buf, size_t count);
const void *buf, size_t count, unsigned int timeout_ms);
SR_PRIV int serial_write_nonblocking(struct sr_serial_dev_inst *serial,
const void *buf, size_t count);
SR_PRIV int serial_read_blocking(struct sr_serial_dev_inst *serial, void *buf,
size_t count);
size_t count, unsigned int timeout_ms);
SR_PRIV int serial_read_nonblocking(struct sr_serial_dev_inst *serial, void *buf,
size_t count);
SR_PRIV int serial_set_params(struct sr_serial_dev_inst *serial, int baudrate,

View File

@ -170,7 +170,7 @@ SR_PRIV int serial_flush(struct sr_serial_dev_inst *serial)
}
static int _serial_write(struct sr_serial_dev_inst *serial,
const void *buf, size_t count, int nonblocking)
const void *buf, size_t count, int nonblocking, unsigned int timeout_ms)
{
ssize_t ret;
char *error;
@ -188,7 +188,7 @@ static int _serial_write(struct sr_serial_dev_inst *serial,
if (nonblocking)
ret = sp_nonblocking_write(serial->data, buf, count);
else
ret = sp_blocking_write(serial->data, buf, count, 0);
ret = sp_blocking_write(serial->data, buf, count, timeout_ms);
switch (ret) {
case SP_ERR_ARG:
@ -212,15 +212,17 @@ static int _serial_write(struct sr_serial_dev_inst *serial,
* @param serial Previously initialized serial port structure.
* @param[in] buf Buffer containing the bytes to write.
* @param[in] count Number of bytes to write.
* @param[in] timeout_ms Timeout in ms, or 0 for no timeout.
*
* @retval SR_ERR_ARG Invalid argument.
* @retval SR_ERR Other error.
* @retval other The number of bytes written.
* @retval other The number of bytes written. If this is less than the number
* specified in the call, the timeout was reached.
*/
SR_PRIV int serial_write_blocking(struct sr_serial_dev_inst *serial,
const void *buf, size_t count)
const void *buf, size_t count, unsigned int timeout_ms)
{
return _serial_write(serial, buf, count, 0);
return _serial_write(serial, buf, count, 0, timeout_ms);
}
/**
@ -237,11 +239,11 @@ SR_PRIV int serial_write_blocking(struct sr_serial_dev_inst *serial,
SR_PRIV int serial_write_nonblocking(struct sr_serial_dev_inst *serial,
const void *buf, size_t count)
{
return _serial_write(serial, buf, count, 1);
return _serial_write(serial, buf, count, 1, 0);
}
static int _serial_read(struct sr_serial_dev_inst *serial, void *buf,
size_t count, int nonblocking)
size_t count, int nonblocking, unsigned int timeout_ms)
{
ssize_t ret;
char *error;
@ -259,7 +261,7 @@ static int _serial_read(struct sr_serial_dev_inst *serial, void *buf,
if (nonblocking)
ret = sp_nonblocking_read(serial->data, buf, count);
else
ret = sp_blocking_read(serial->data, buf, count, 0);
ret = sp_blocking_read(serial->data, buf, count, timeout_ms);
switch (ret) {
case SP_ERR_ARG:
@ -284,15 +286,17 @@ static int _serial_read(struct sr_serial_dev_inst *serial, void *buf,
* @param serial Previously initialized serial port structure.
* @param buf Buffer where to store the bytes that are read.
* @param[in] count The number of bytes to read.
* @param[in] timeout_ms Timeout in ms, or 0 for no timeout.
*
* @retval SR_ERR_ARG Invalid argument.
* @retval SR_ERR Other error.
* @retval other The number of bytes read.
* @retval other The number of bytes read. If this is less than the number
* requested, the timeout was reached.
*/
SR_PRIV int serial_read_blocking(struct sr_serial_dev_inst *serial, void *buf,
size_t count)
size_t count, unsigned int timeout_ms)
{
return _serial_read(serial, buf, count, 0);
return _serial_read(serial, buf, count, 0, timeout_ms);
}
/**
@ -310,7 +314,7 @@ SR_PRIV int serial_read_blocking(struct sr_serial_dev_inst *serial, void *buf,
SR_PRIV int serial_read_nonblocking(struct sr_serial_dev_inst *serial, void *buf,
size_t count)
{
return _serial_read(serial, buf, count, 1);
return _serial_read(serial, buf, count, 1, 0);
}
/**