fluke-dmm: fix discovery

This commit is contained in:
Bert Vermeulen 2012-09-16 21:07:17 +02:00
parent 5c51e09868
commit fb480d578e
1 changed files with 64 additions and 58 deletions

View File

@ -91,29 +91,22 @@ static int serial_readline(int fd, char **buf, int *buflen, uint64_t timeout_ms)
len = maxlen - *buflen - 1; len = maxlen - *buflen - 1;
if (len < 1) if (len < 1)
break; break;
len = serial_read(fd, *buf + *buflen, len); len = serial_read(fd, *buf + *buflen, 1);
if (len > 0) { if (len > 0) {
*buflen += len; *buflen += len;
*(*buf + *buflen) = '\0'; *(*buf + *buflen) = '\0';
if (*buflen > 0 && *(*buf + *buflen - 1) == '\r') if (*buflen > 0 && *(*buf + *buflen - 1) == '\n') {
/* End of line */ /* Strip LF and terminate. */
*(*buf + --*buflen) = '\0';
break; break;
} }
}
if (g_get_monotonic_time() - start > timeout_ms) if (g_get_monotonic_time() - start > timeout_ms)
/* Timeout */ /* Timeout */
break; break;
g_usleep(2000); g_usleep(2000);
} }
sr_dbg("fluke-dmm: received %d: '%s'", *buflen, *buf);
/* Strip CRLF */
while (*buflen) {
if (*(*buf + *buflen - 1) == '\r' || *(*buf + *buflen - 1) == '\n')
*(*buf + --*buflen) = '\0';
else
break;
}
if (*buflen)
sr_dbg("fluke-dmm: received '%s'", *buf);
return SR_OK; return SR_OK;
} }
@ -126,9 +119,9 @@ static GSList *hw_scan(GSList *options)
struct sr_hwopt *opt; struct sr_hwopt *opt;
struct sr_probe *probe; struct sr_probe *probe;
GSList *l, *devices; GSList *l, *devices;
int len, fd, i; int retry, len, fd, i, s;
const char *conn, *serialcomm; const char *conn, *serialcomm;
char *buf, **tokens; char buf[128], *b, **tokens;
drvc = di->priv; drvc = di->priv;
drvc->instances = NULL; drvc->instances = NULL;
@ -166,32 +159,45 @@ static GSList *hw_scan(GSList *options)
return NULL; return NULL;
} }
b = buf;
retry = 0;
/* We'll try the discovery sequence three times in case the device
* is not in an idle state when we send ID. */
while (!devices && retry < 3) {
retry++;
serial_flush(fd); serial_flush(fd);
if (serial_write(fd, "ID\r", 3) == -1) { if (serial_write(fd, "ID\r", 3) == -1) {
sr_err("fluke-dmm: unable to send identification string: %s", sr_err("fluke-dmm: unable to send ID string: %s",
strerror(errno)); strerror(errno));
return NULL; continue;
} }
/* Response is first a CMD_ACK byte (ASCII '0' for OK,
* or '1' to signify an error. */
len = 128; len = 128;
buf = g_try_malloc(len); serial_readline(fd, &b, &len, 150);
serial_readline(fd, &buf, &len, 150); if (len != 1)
if (!len) continue;
return NULL; if (buf[0] != '0') {
sr_dbg("fluke-dmm: got ID response %.2x", buf[0]);
if (len < 2 || buf[0] != '0' || buf[1] != '\r') { continue;
sr_err("fluke-dmm: invalid response to identification string");
return NULL;
} }
tokens = g_strsplit(buf + 2, ",", 3); /* If CMD_ACK was OK, ID string follows. */
len = 128;
serial_readline(fd, &b, &len, 150);
if (len < 10)
continue;
tokens = g_strsplit(buf, ",", 3);
if (!strncmp("FLUKE", tokens[0], 5) if (!strncmp("FLUKE", tokens[0], 5)
&& tokens[1] && tokens[2]) { && tokens[1] && tokens[2]) {
for (i = 0; supported_flukedmm[i].model; i++) { for (i = 0; supported_flukedmm[i].model; i++) {
if (strcmp(supported_flukedmm[i].modelname, tokens[0])) if (strcmp(supported_flukedmm[i].modelname, tokens[0] + 6))
continue; continue;
/* Skip leading spaces in version number. */
for (s = 0; tokens[1][s] == ' '; s++);
if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "Fluke", if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "Fluke",
tokens[0] + 6, tokens[1]))) tokens[0] + 6, tokens[1] + s)))
return NULL; return NULL;
if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) { if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
sr_dbg("fluke-dmm: failed to malloc devc"); sr_dbg("fluke-dmm: failed to malloc devc");
@ -210,7 +216,7 @@ static GSList *hw_scan(GSList *options)
} }
} }
g_strfreev(tokens); g_strfreev(tokens);
g_free(buf); }
serial_close(fd); serial_close(fd);