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;
}
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;
}
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;
}
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);
}
/* 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;
{
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;
}
* 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;
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;
}
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;
}
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;
}
/* 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 {
* 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;
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;
}
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);
/* 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;
}
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. */
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;
}
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')
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;
{
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;
}
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;
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;
}
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;
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;
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;
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;
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.");
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);
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.",
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);
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;
}
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;
}
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,
}
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;
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:
* @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);
}
/**
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;
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:
* @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);
}
/**
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);
}
/**