X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=src%2Fhardware%2Fbrymen-dmm%2Fparser.c;h=baab57d9dcf8f03598b0576ce2b3bf9ab741bb03;hb=56213aa0270fc8046dfcc380c137e766be41fd9c;hp=a5e8333a6a004e3f3eda0d489a2bd0abc2695378;hpb=1653c4309ab4d5756a2af7520d7d13a63f5e3619;p=libsigrok.git diff --git a/src/hardware/brymen-dmm/parser.c b/src/hardware/brymen-dmm/parser.c index a5e8333a..baab57d9 100644 --- a/src/hardware/brymen-dmm/parser.c +++ b/src/hardware/brymen-dmm/parser.c @@ -130,43 +130,67 @@ SR_PRIV gboolean brymen_packet_is_valid(const uint8_t *buf) int i; uint8_t chksum = 0; uint8_t *payload; - + payload = (uint8_t *)(buf + sizeof(struct brymen_header)); hdr = (void *)buf; tail = (void *)(payload + hdr->len); - + for (i = 0; i< hdr->len; i++) chksum ^= payload[i]; - + if (tail->checksum != chksum) { sr_dbg("Packet has invalid checksum 0x%.2x. Expected 0x%.2x.", chksum, tail->checksum); return FALSE; } - + return TRUE; } -static int parse_value(const char *strbuf, int len, float *floatval) +static int parse_value(const char *txt, size_t len, float *floatval) { - int s, d; - char str[32]; + const char *txt_end; + char c, buf[32], *dst; + int ret; - if (strstr(strbuf, "OL")) { - sr_dbg("Overlimit."); - *floatval = INFINITY; - return SR_OK; + /* + * The input text is not NUL terminated, the checksum follows + * the value text field. Spaces may interfere with the text to + * number conversion, especially with exponent parsing. Copy the + * input data to a terminated text buffer and strip spaces in the + * process, before running ASCIIZ string operations. + */ + if (len >= sizeof(buf)) { + sr_err("Insufficient text conversion buffer size."); + return SR_ERR_BUG; } + txt_end = txt + len; + dst = &buf[0]; + while (txt < txt_end && *txt) { + c = *txt++; + if (c == ' ') + continue; + *dst++ = c; + } + *dst = '\0'; - memset(str, 0, sizeof(str)); - /* Spaces may interfere with parsing the exponent. Strip them. */ - for (s = 0, d = 0; s < len; s++) { - if (strbuf[s] != ' ') - str[d++] = strbuf[s]; + /* Check for overflow, or get the number value. */ + if (strstr(buf, "+OL")) { + *floatval = +INFINITY; + return SR_OK; } - if (sr_atof_ascii(str, floatval) != SR_OK) - return SR_ERR; + if (strstr(buf, "-OL")) { + *floatval = -INFINITY; + return SR_OK; + } + if (strstr(buf, "OL")) { + *floatval = INFINITY; + return SR_OK; + } + ret = sr_atof_ascii(buf, floatval); + if (ret != SR_OK) + return ret; return SR_OK; } @@ -276,7 +300,7 @@ SR_PRIV int brymen_parse(const uint8_t *buf, float *floatval, } if (flags.is_diode) - analog->meaning->mqflags |= SR_MQFLAG_DIODE; + analog->meaning->mqflags |= SR_MQFLAG_DIODE | SR_MQFLAG_DC; /* We can have both AC+DC in a single measurement. */ if (flags.is_ac) analog->meaning->mqflags |= SR_MQFLAG_AC; @@ -284,7 +308,7 @@ SR_PRIV int brymen_parse(const uint8_t *buf, float *floatval, analog->meaning->mqflags |= SR_MQFLAG_DC; if (flags.is_low_batt) - sr_info("Low battery!"); + sr_warn("Low battery!"); return SR_OK; }