radioshack-dmm: Improve serial detection
For device detection, use serial_stream_detect() instead of serial_readline() + custom logic. Signed-off-by: Alexandru Gagniuc <mr.nuke.me@gmail.com>
This commit is contained in:
parent
551c3d8ce3
commit
54be7c005e
|
@ -91,6 +91,12 @@ static int hw_init(void)
|
||||||
return SR_OK;
|
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)
|
static GSList *rs_22_812_scan(const char *conn, const char *serialcomm)
|
||||||
{
|
{
|
||||||
struct sr_dev_inst *sdi;
|
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_probe *probe;
|
||||||
struct sr_serial_dev_inst *serial;
|
struct sr_serial_dev_inst *serial;
|
||||||
GSList *devices;
|
GSList *devices;
|
||||||
int retry;
|
int ret;
|
||||||
size_t len, i, good_packets, dropped;
|
size_t len, dropped;
|
||||||
char buf[128], *b;
|
uint8_t buf[128];
|
||||||
const struct rs_22_812_packet *rs_packet;
|
|
||||||
|
|
||||||
if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
|
if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
|
||||||
return NULL;
|
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);
|
sr_info("Probing port '%s' readonly.", conn);
|
||||||
|
|
||||||
drvc = di->priv;
|
drvc = di->priv;
|
||||||
b = buf;
|
|
||||||
retry = 0;
|
|
||||||
devices = NULL;
|
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
|
* periodically, so the best we can do is check if the packets match
|
||||||
* the expected format.
|
* the expected format.
|
||||||
*/
|
*/
|
||||||
while (!devices && retry < 3) {
|
serial_flush(serial);
|
||||||
good_packets = 0;
|
|
||||||
retry++;
|
|
||||||
serial_flush(serial);
|
|
||||||
|
|
||||||
/* Let's get a bit of data and see if we can find a packet. */
|
/* Let's get a bit of data and see if we can find a packet. */
|
||||||
len = sizeof(buf);
|
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Treat our buffer as stream, and find any valid packets. */
|
/* 500ms gives us a window of two packets */
|
||||||
for (i = 0; i < len - RS_22_812_PACKET_SIZE + 1;) {
|
ret = serial_stream_detect(serial, buf, &len, RS_22_812_PACKET_SIZE,
|
||||||
rs_packet = (void *)(&buf[i]);
|
packet_valid_wrap, 500, 4800);
|
||||||
if (!rs_22_812_packet_valid(rs_packet)) {
|
if (ret != SR_OK)
|
||||||
i++;
|
goto scan_cleanup;
|
||||||
continue;
|
|
||||||
}
|
|
||||||
good_packets++;
|
|
||||||
i += RS_22_812_PACKET_SIZE;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* If we dropped more than two packets, something is wrong. */
|
/* If we dropped more than two packets, something is wrong. */
|
||||||
dropped = len - (good_packets * RS_22_812_PACKET_SIZE);
|
dropped = len - RS_22_812_PACKET_SIZE;
|
||||||
if (dropped > 2 * RS_22_812_PACKET_SIZE)
|
if (dropped > 2 * RS_22_812_PACKET_SIZE)
|
||||||
continue;
|
goto scan_cleanup;
|
||||||
|
|
||||||
/* Let's see if we have anything good. */
|
sr_info("Found RadioShack 22-812 on port '%s'.", conn);
|
||||||
if (good_packets == 0)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
sr_info("Found RadioShack 22-812 on port '%s'.", conn);
|
if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "RadioShack",
|
||||||
|
"22-812", "")))
|
||||||
|
goto scan_cleanup;
|
||||||
|
|
||||||
if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "RadioShack",
|
if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
|
||||||
"22-812", "")))
|
sr_err("Device context malloc failed.");
|
||||||
return NULL;
|
goto scan_cleanup;
|
||||||
|
|
||||||
if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
|
|
||||||
sr_err("Device context malloc failed.");
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
devc->serial = serial;
|
||||||
|
|
||||||
|
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);
|
serial_close(serial);
|
||||||
|
|
||||||
return devices;
|
return devices;
|
||||||
|
|
Loading…
Reference in New Issue