]> sigrok.org Git - libsigrok.git/blobdiff - src/hardware/brymen-dmm/parser.c
brymen-dmm: improve text to number conversion robustness, signed OL
[libsigrok.git] / src / hardware / brymen-dmm / parser.c
index e4b12274d41d862b3f08c6b85182545a300e39d5..baab57d9dcf8f03598b0576ce2b3bf9ab741bb03 100644 (file)
@@ -17,6 +17,7 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include <config.h>
 #include "protocol.h"
 
 #define MAX_PACKET_LEN 22
@@ -78,7 +79,8 @@ static int bm_send_command(uint8_t command, uint8_t arg1, uint8_t arg2,
        /* TODO: How to compute the checksum? Hardware seems to ignore it. */
 
        /* Request reading. */
-       written = serial_write(serial, &cmdout, sizeof(cmdout));
+       written = serial_write_blocking(serial, &cmdout, sizeof(cmdout),
+                       serial_timeout(serial, sizeof(cmdout)));
        if (written != sizeof(cmdout))
                return SR_ERR;
 
@@ -128,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;
 }
@@ -202,7 +228,7 @@ SR_PRIV int brymen_parse(const uint8_t *buf, float *floatval,
        hdr = (void *)buf;
        bfunc = (uint8_t *)(buf + sizeof(struct brymen_header));
 
-       analog->mqflags = 0;
+       analog->meaning->mqflags = 0;
 
        /* Give some debug info about the package. */
        asciilen = hdr->len - 4;
@@ -216,43 +242,43 @@ SR_PRIV int brymen_parse(const uint8_t *buf, float *floatval,
                return SR_ERR;
 
        if (flags.is_volt) {
-               analog->mq = SR_MQ_VOLTAGE;
-               analog->unit = SR_UNIT_VOLT;
+               analog->meaning->mq = SR_MQ_VOLTAGE;
+               analog->meaning->unit = SR_UNIT_VOLT;
        }
        if (flags.is_amp) {
-               analog->mq = SR_MQ_CURRENT;
-               analog->unit = SR_UNIT_AMPERE;
+               analog->meaning->mq = SR_MQ_CURRENT;
+               analog->meaning->unit = SR_UNIT_AMPERE;
        }
        if (flags.is_ohm) {
                if (flags.is_beep)
-                       analog->mq = SR_MQ_CONTINUITY;
+                       analog->meaning->mq = SR_MQ_CONTINUITY;
                else
-                       analog->mq = SR_MQ_RESISTANCE;
-               analog->unit = SR_UNIT_OHM;
+                       analog->meaning->mq = SR_MQ_RESISTANCE;
+               analog->meaning->unit = SR_UNIT_OHM;
        }
        if (flags.is_hertz) {
-               analog->mq = SR_MQ_FREQUENCY;
-               analog->unit = SR_UNIT_HERTZ;
+               analog->meaning->mq = SR_MQ_FREQUENCY;
+               analog->meaning->unit = SR_UNIT_HERTZ;
        }
        if (flags.is_duty_cycle) {
-               analog->mq = SR_MQ_DUTY_CYCLE;
-               analog->unit = SR_UNIT_PERCENTAGE;
+               analog->meaning->mq = SR_MQ_DUTY_CYCLE;
+               analog->meaning->unit = SR_UNIT_PERCENTAGE;
        }
        if (flags.is_capacitance) {
-               analog->mq = SR_MQ_CAPACITANCE;
-               analog->unit = SR_UNIT_FARAD;
+               analog->meaning->mq = SR_MQ_CAPACITANCE;
+               analog->meaning->unit = SR_UNIT_FARAD;
        }
        if (flags.is_fahrenheit) {
-               analog->mq = SR_MQ_TEMPERATURE;
-               analog->unit = SR_UNIT_FAHRENHEIT;
+               analog->meaning->mq = SR_MQ_TEMPERATURE;
+               analog->meaning->unit = SR_UNIT_FAHRENHEIT;
        }
        if (flags.is_celsius) {
-               analog->mq = SR_MQ_TEMPERATURE;
-               analog->unit = SR_UNIT_CELSIUS;
+               analog->meaning->mq = SR_MQ_TEMPERATURE;
+               analog->meaning->unit = SR_UNIT_CELSIUS;
        }
        if (flags.is_capacitance) {
-               analog->mq = SR_MQ_CAPACITANCE;
-               analog->unit = SR_UNIT_FARAD;
+               analog->meaning->mq = SR_MQ_CAPACITANCE;
+               analog->meaning->unit = SR_UNIT_FARAD;
        }
 
        /*
@@ -263,8 +289,8 @@ SR_PRIV int brymen_parse(const uint8_t *buf, float *floatval,
         * identify the value as ohm, not dBmW.
         */
        if (flags.is_decibel && !flags.is_ohm) {
-               analog->mq = SR_MQ_POWER;
-               analog->unit = SR_UNIT_DECIBEL_MW;
+               analog->meaning->mq = SR_MQ_POWER;
+               analog->meaning->unit = SR_UNIT_DECIBEL_MW;
                /*
                 * For some reason, dBm measurements are sent by the multimeter
                 * with a value three orders of magnitude smaller than the
@@ -274,15 +300,15 @@ SR_PRIV int brymen_parse(const uint8_t *buf, float *floatval,
        }
 
        if (flags.is_diode)
-               analog->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->mqflags |= SR_MQFLAG_AC;
+               analog->meaning->mqflags |= SR_MQFLAG_AC;
        if (flags.is_dc)
-               analog->mqflags |= SR_MQFLAG_DC;
+               analog->meaning->mqflags |= SR_MQFLAG_DC;
 
        if (flags.is_low_batt)
-               sr_info("Low battery!");
+               sr_warn("Low battery!");
 
        return SR_OK;
 }