]> sigrok.org Git - libsigrok.git/commitdiff
serial_readline: Implement using sp_blocking_read.
authorMartin Ling <redacted>
Sun, 21 Sep 2014 18:35:39 +0000 (19:35 +0100)
committerUwe Hermann <redacted>
Wed, 24 Sep 2014 21:18:20 +0000 (23:18 +0200)
src/serial.c

index 4943bb106e54b1f3d87e432ecd6f48549fbc3025..83df6a348064ed1b3292c0c1180b06383721b7f9 100644 (file)
@@ -530,7 +530,7 @@ SR_PRIV int serial_set_paramstr(struct sr_serial_dev_inst *serial,
 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) {
@@ -543,8 +543,8 @@ SR_PRIV int serial_readline(struct sr_serial_dev_inst *serial, char **buf,
                return -1;
        }
 
-       timeout_ms *= 1000;
        start = g_get_monotonic_time();
+       remaining = timeout_ms;
 
        maxlen = *buflen;
        *buflen = len = 0;
@@ -552,7 +552,7 @@ SR_PRIV int serial_readline(struct sr_serial_dev_inst *serial, char **buf,
                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';
@@ -563,7 +563,9 @@ SR_PRIV int serial_readline(struct sr_serial_dev_inst *serial, char **buf,
                                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)