]> sigrok.org Git - libsigrok.git/blobdiff - src/hardware/brymen-dmm/protocol.c
dmm/bm85x: introduce DMM packet parser for Brymen BM850(a/s)
[libsigrok.git] / src / hardware / brymen-dmm / protocol.c
index 881881cf026a1366b2993157e464c52c2e550265..3dd8dd3137a3074ea2bbb397963f09cb1e991874 100644 (file)
@@ -17,6 +17,7 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include <config.h>
 #include "protocol.h"
 
 static void handle_packet(const uint8_t *buf, struct sr_dev_inst *sdi)
@@ -25,24 +26,30 @@ static void handle_packet(const uint8_t *buf, struct sr_dev_inst *sdi)
        struct dev_context *devc;
        struct sr_datafeed_packet packet;
        struct sr_datafeed_analog analog;
+       struct sr_analog_encoding encoding;
+       struct sr_analog_meaning meaning;
+       struct sr_analog_spec spec;
 
        devc = sdi->priv;
 
+       /* TODO: Use proper 'digits' value for this device (and its modes). */
+       sr_analog_init(&analog, &encoding, &meaning, &spec, 2);
+
        analog.num_samples = 1;
-       analog.mq = -1;
+       analog.meaning->mq = 0;
 
        if (brymen_parse(buf, &floatval, &analog, NULL) != SR_OK)
                return;
        analog.data = &floatval;
 
-       analog.channels = sdi->channels;
+       analog.meaning->channels = sdi->channels;
 
-       if (analog.mq != -1) {
+       if (analog.meaning->mq != 0) {
                /* Got a measurement. */
                packet.type = SR_DF_ANALOG;
                packet.payload = &analog;
-               sr_session_send(devc->cb_data, &packet);
-               devc->num_samples++;
+               sr_session_send(sdi, &packet);
+               sr_sw_limits_update_samples_read(&devc->sw_limits, 1);
        }
 }
 
@@ -113,7 +120,6 @@ SR_PRIV int brymen_dmm_receive_data(int fd, int revents, void *cb_data)
        struct dev_context *devc;
        struct sr_serial_dev_inst *serial;
        int ret;
-       int64_t time;
 
        (void)fd;
 
@@ -136,20 +142,8 @@ SR_PRIV int brymen_dmm_receive_data(int fd, int revents, void *cb_data)
                }
        }
 
-       if (devc->limit_samples && devc->num_samples >= devc->limit_samples) {
-               sr_info("Requested number of samples reached, stopping.");
-               sdi->driver->dev_acquisition_stop(sdi, cb_data);
-               return TRUE;
-       }
-
-       if (devc->limit_msec) {
-               time = (g_get_monotonic_time() - devc->starttime) / 1000;
-               if (time > (int64_t)devc->limit_msec) {
-                       sr_info("Requested time limit reached, stopping.");
-                       sdi->driver->dev_acquisition_stop(sdi, cb_data);
-                       return TRUE;
-               }
-       }
+       if (sr_sw_limits_check(&devc->sw_limits))
+               sr_dev_acquisition_stop(sdi);
 
        return TRUE;
 }
@@ -179,7 +173,9 @@ SR_PRIV int brymen_stream_detect(struct sr_serial_dev_inst *serial,
 {
        int64_t start, time, byte_delay_us;
        size_t ibuf, i, maxlen;
-       int status, len, packet_len, stream_len;
+       ssize_t len, stream_len;
+       int packet_len;
+       int status;
 
        maxlen = *buflen;
 
@@ -187,7 +183,7 @@ SR_PRIV int brymen_stream_detect(struct sr_serial_dev_inst *serial,
               "ms, baudrate = %d).", serial->port, timeout_ms, baudrate);
 
        /* Assume 8n1 transmission. That is 10 bits for every byte. */
-       byte_delay_us = 10 * (1000000 / baudrate);
+       byte_delay_us = 10 * ((1000 * 1000) / baudrate);
        start = g_get_monotonic_time();
 
        packet_len = i = ibuf = len = 0;
@@ -195,7 +191,7 @@ SR_PRIV int brymen_stream_detect(struct sr_serial_dev_inst *serial,
                len = serial_read_nonblocking(serial, &buf[ibuf], maxlen - ibuf);
                if (len > 0) {
                        ibuf += len;
-                       sr_spew("Read %d bytes.", len);
+                       sr_spew("Read %zd bytes.", len);
                }
 
                time = g_get_monotonic_time() - start;
@@ -206,7 +202,7 @@ SR_PRIV int brymen_stream_detect(struct sr_serial_dev_inst *serial,
                        /* How large of a packet are we expecting? */
                        packet_len = stream_len;
                        status = get_packet_size(&buf[i], &packet_len);
-                       switch(status) {
+                       switch (status) {
                        case PACKET_HEADER_OK:
                                /* We know how much data we need to wait for. */
                                break;
@@ -247,14 +243,14 @@ SR_PRIV int brymen_stream_detect(struct sr_serial_dev_inst *serial,
 
                if (time >= (int64_t)timeout_ms) {
                        /* Timeout */
-                       sr_dbg("Detection timed out after %dms.", time);
+                       sr_dbg("Detection timed out after %" PRIi64 "ms.", time);
                        break;
                }
                g_usleep(byte_delay_us);
        }
 
        *buflen = ibuf;
-       sr_err("Didn't find a valid packet (read %d bytes).", ibuf);
+       sr_err("Didn't find a valid packet (read %zu bytes).", ibuf);
 
        return SR_ERR;
 }