SR_PRIV int serial_readline(struct sr_serial_dev_inst *serial, char **buf,
int *buflen, gint64 timeout_ms)
{
- gint64 start;
+ gint64 start, remaining;
int maxlen, len;
if (!serial) {
return -1;
}
- timeout_ms *= 1000;
start = g_get_monotonic_time();
+ remaining = timeout_ms;
maxlen = *buflen;
*buflen = len = 0;
len = maxlen - *buflen - 1;
if (len < 1)
break;
- len = serial_read(serial, *buf + *buflen, 1);
+ len = sp_blocking_read(serial->data, *buf + *buflen, 1, remaining);
if (len > 0) {
*buflen += len;
*(*buf + *buflen) = '\0';
break;
}
}
- if (g_get_monotonic_time() - start > timeout_ms)
+ /* Reduce timeout by time elapsed. */
+ remaining = timeout_ms - ((g_get_monotonic_time() - start) / 1000);
+ if (remaining <= 0)
/* Timeout */
break;
if (len < 1)