]> sigrok.org Git - libsigrok.git/commitdiff
serial.c: Sanitize serial_stream_detect
authorAlexandru Gagniuc <redacted>
Thu, 22 Nov 2012 01:18:21 +0000 (19:18 -0600)
committerAlexandru Gagniuc <redacted>
Thu, 22 Nov 2012 01:18:21 +0000 (19:18 -0600)
Print the timeout in miliseconds, not microseconds.
Only calculate elapsed time once oer loop.

Signed-off-by: Alexandru Gagniuc <redacted>
hardware/common/serial.c

index 76464970bfe123077898d79431cdd5c373e966dc..bdf02163a38ee8ca1145eca15067dafb2263ce33 100644 (file)
@@ -660,8 +660,6 @@ SR_PRIV int serial_stream_detect(struct sr_serial_dev_inst *serial,
                return SR_ERR;
        }
 
-       timeout_ms *= 1000;
-
        /* Assume 8n1 transmission. That is 10 bits for every byte. */
        byte_delay_us = 10 * (1000000 / baudrate);
        start = g_get_monotonic_time();
@@ -676,11 +674,13 @@ SR_PRIV int serial_stream_detect(struct sr_serial_dev_inst *serial,
                } else {
                        /* Error reading byte, but continuing anyway. */
                }
+
+               time = g_get_monotonic_time() - start;
+               time /= 1000;
+
                if ((ibuf - i) >= packet_size) {
                        /* We have at least a packet's worth of data. */
                        if (is_valid(&buf[i])) {
-                               time = g_get_monotonic_time() - start;
-                               time /= 1000;
                                sr_spew("Found valid %d-byte packet after "
                                        "%" PRIu64 "ms.", (ibuf - i), time);
                                *buflen = ibuf;
@@ -692,9 +692,9 @@ SR_PRIV int serial_stream_detect(struct sr_serial_dev_inst *serial,
                        /* Not a valid packet. Continue searching. */
                        i++;
                }
-               if (g_get_monotonic_time() - start > timeout_ms) {
+               if (time >= timeout_ms) {
                        /* Timeout */
-                       sr_dbg("Detection timed out after %dms.", timeout_ms);
+                       sr_dbg("Detection timed out after %dms.", time);
                        break;
                }
                g_usleep(byte_delay_us);