]> sigrok.org Git - libsigrok.git/commitdiff
radioshack-dmm: Improve serial detection
authorAlexandru Gagniuc <redacted>
Thu, 22 Nov 2012 01:19:58 +0000 (19:19 -0600)
committerAlexandru Gagniuc <redacted>
Thu, 22 Nov 2012 01:19:58 +0000 (19:19 -0600)
For device detection, use serial_stream_detect() instead of
serial_readline() + custom logic.

Signed-off-by: Alexandru Gagniuc <redacted>
hardware/radioshack-dmm/api.c

index c32ffbe6e1d62ed55440045e4060fc543800ebe4..c51c6f08689c519dbf1d5859e3e9d907e4dac504 100644 (file)
@@ -91,6 +91,12 @@ static int hw_init(void)
        return SR_OK;
 }
 
+
+static gboolean packet_valid_wrap(const uint8_t *buf)
+{
+       return rs_22_812_packet_valid((void*)buf);
+}
+
 static GSList *rs_22_812_scan(const char *conn, const char *serialcomm)
 {
        struct sr_dev_inst *sdi;
@@ -99,10 +105,9 @@ static GSList *rs_22_812_scan(const char *conn, const char *serialcomm)
        struct sr_probe *probe;
        struct sr_serial_dev_inst *serial;
        GSList *devices;
-       int retry;
-       size_t len, i, good_packets, dropped;
-       char buf[128], *b;
-       const struct rs_22_812_packet *rs_packet;
+       int ret;
+       size_t len, dropped;
+       uint8_t buf[128];
 
        if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
                return NULL;
@@ -113,8 +118,6 @@ static GSList *rs_22_812_scan(const char *conn, const char *serialcomm)
        sr_info("Probing port '%s' readonly.", conn);
 
        drvc = di->priv;
-       b = buf;
-       retry = 0;
        devices = NULL;
 
        /*
@@ -122,61 +125,44 @@ static GSList *rs_22_812_scan(const char *conn, const char *serialcomm)
         * periodically, so the best we can do is check if the packets match
         * the expected format.
         */
-       while (!devices && retry < 3) {
-               good_packets = 0;
-               retry++;
-               serial_flush(serial);
-
-               /* Let's get a bit of data and see if we can find a packet. */
-               len = sizeof(buf);
-               serial_readline(serial, &b, &len, 250);
-               if ((len == 0) || (len < RS_22_812_PACKET_SIZE)) {
-                       /* Not enough data received, is the DMM connected? */
-                       continue;
-               }
+       serial_flush(serial);
 
-               /* Treat our buffer as stream, and find any valid packets. */
-               for (i = 0; i < len - RS_22_812_PACKET_SIZE + 1;) {
-                       rs_packet = (void *)(&buf[i]);
-                       if (!rs_22_812_packet_valid(rs_packet)) {
-                               i++;
-                               continue;
-                       }
-                       good_packets++;
-                       i += RS_22_812_PACKET_SIZE;
-               }
+       /* Let's get a bit of data and see if we can find a packet. */
+       len = sizeof(buf);
 
-               /* If we dropped more than two packets, something is wrong. */
-               dropped = len - (good_packets * RS_22_812_PACKET_SIZE);
-               if (dropped > 2 * RS_22_812_PACKET_SIZE)
-                       continue;
+       /* 500ms gives us a window of two packets */
+       ret = serial_stream_detect(serial, buf, &len, RS_22_812_PACKET_SIZE,
+                                  packet_valid_wrap, 500, 4800);
+       if (ret != SR_OK)
+               goto scan_cleanup;
 
-               /* Let's see if we have anything good. */
-               if (good_packets == 0)
-                       continue;
+       /* If we dropped more than two packets, something is wrong. */
+       dropped = len - RS_22_812_PACKET_SIZE;
+       if (dropped > 2 * RS_22_812_PACKET_SIZE)
+               goto scan_cleanup;
 
-               sr_info("Found RadioShack 22-812 on port '%s'.", conn);
+       sr_info("Found RadioShack 22-812 on port '%s'.", conn);
 
-               if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "RadioShack",
-                                           "22-812", "")))
-                       return NULL;
+       if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "RadioShack",
+                                   "22-812", "")))
+               goto scan_cleanup;
 
-               if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
-                       sr_err("Device context malloc failed.");
-                       return NULL;
-               }
+       if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
+               sr_err("Device context malloc failed.");
+               goto scan_cleanup;
+       }
 
-               devc->serial = serial;
+       devc->serial = serial;
 
-               sdi->priv = devc;
-               sdi->driver = di;
-               if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
-                       return NULL;
-               sdi->probes = g_slist_append(sdi->probes, probe);
-               drvc->instances = g_slist_append(drvc->instances, sdi);
-               devices = g_slist_append(devices, sdi);
-               break;
-       }
+       sdi->priv = devc;
+       sdi->driver = di;
+       if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
+               goto scan_cleanup;
+       sdi->probes = g_slist_append(sdi->probes, probe);
+       drvc->instances = g_slist_append(drvc->instances, sdi);
+       devices = g_slist_append(devices, sdi);
+
+scan_cleanup:
        serial_close(serial);
 
        return devices;