]> sigrok.org Git - libsigrok.git/blobdiff - src/hardware/teleinfo/protocol.c
drivers: Add and use STD_CONFIG_LIST().
[libsigrok.git] / src / hardware / teleinfo / protocol.c
index a610173d1aaaa20421d2716bf44b3e7eaf8f42cd..d42da22b5621466418e3ae66f4f3df0e58757bfd 100644 (file)
@@ -17,6 +17,7 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include <config.h>
 #include <stdlib.h>
 #include <string.h>
 #include <glib.h>
@@ -55,35 +56,36 @@ static struct sr_channel *teleinfo_find_channel(struct sr_dev_inst *sdi,
 }
 
 static void teleinfo_send_value(struct sr_dev_inst *sdi, const char *channel_name,
-                                float value, int mq, int unit)
+                                float value, enum sr_mq mq, enum sr_unit unit)
 {
-       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;
        struct sr_channel *ch;
 
-       devc = sdi->priv;
        ch = teleinfo_find_channel(sdi, channel_name);
 
        if (!ch || !ch->enabled)
                return;
 
-       memset(&analog, 0, sizeof(struct sr_datafeed_analog));
-       analog.channels = g_slist_append(analog.channels, ch);
+       /* Note: digits/spec_digits is actually really 0 for this device! */
+       sr_analog_init(&analog, &encoding, &meaning, &spec, 0);
+       analog.meaning->channels = g_slist_append(analog.meaning->channels, ch);
        analog.num_samples = 1;
-       analog.mq = mq;
-       analog.unit = unit;
+       analog.meaning->mq = mq;
+       analog.meaning->unit = unit;
        analog.data = &value;
 
        packet.type = SR_DF_ANALOG;
        packet.payload = &analog;
-       sr_session_send(devc->session_cb_data, &packet);
-       g_slist_free(analog.channels);
+       sr_session_send(sdi, &packet);
+       g_slist_free(analog.meaning->channels);
 }
 
-static void teleinfo_handle_mesurement(struct sr_dev_inst *sdi,
-                                       const char *label, const char *data,
-                                       char *optarif)
+static void teleinfo_handle_measurement(struct sr_dev_inst *sdi,
+               const char *label, const char *data, char *optarif)
 {
        struct dev_context *devc;
        int v = atoi(data);
@@ -95,7 +97,7 @@ static void teleinfo_handle_mesurement(struct sr_dev_inst *sdi,
        }
 
        if (!strcmp(label, "ADCO")) {
-               devc->num_samples++;
+               sr_sw_limits_update_samples_read(&devc->sw_limits, 1);
        } else if (!strcmp(label, "BASE")) {
                teleinfo_send_value(sdi, "BASE", v, SR_MQ_POWER, SR_UNIT_WATT_HOUR);
        } else if (!strcmp(label, "HCHP")) {
@@ -135,7 +137,7 @@ static gboolean teleinfo_parse_group(struct sr_dev_inst *sdi,
                return FALSE;
        if (!teleinfo_control_check(label, data, control))
                return FALSE;
-       teleinfo_handle_mesurement(sdi, label, data, optarif);
+       teleinfo_handle_measurement(sdi, label, data, optarif);
        return TRUE;
 }
 
@@ -186,7 +188,6 @@ SR_PRIV int teleinfo_receive_data(int fd, int revents, void *cb_data)
        struct sr_serial_dev_inst *serial;
        const uint8_t *ptr, *next_ptr, *end_ptr;
        int len;
-       int64_t time;
 
        (void)fd;
 
@@ -196,7 +197,7 @@ SR_PRIV int teleinfo_receive_data(int fd, int revents, void *cb_data)
 
        /* Try to get as much data as the buffer can hold. */
        len = TELEINFO_BUF_SIZE - devc->buf_len;
-       len = serial_read(serial, devc->buf + devc->buf_len, len);
+       len = serial_read_nonblocking(serial, devc->buf + devc->buf_len, len);
        if (len < 1) {
                sr_err("Serial port read error: %d.", len);
                return FALSE;
@@ -219,19 +220,8 @@ SR_PRIV int teleinfo_receive_data(int fd, int revents, void *cb_data)
                return FALSE;
        }
 
-       if (devc->limit_samples && devc->num_samples >= devc->limit_samples) {
-               sr_info("Requested number of samples reached.");
-               sdi->driver->dev_acquisition_stop(sdi, devc->session_cb_data);
-               return TRUE;
-       }
+       if (sr_sw_limits_check(&devc->sw_limits))
+               sr_dev_acquisition_stop(sdi);
 
-       if (devc->limit_msec) {
-               time = (g_get_monotonic_time() - devc->start_time) / 1000;
-               if (time > (int64_t)devc->limit_msec) {
-                       sr_info("Requested time limit reached.");
-                       sdi->driver->dev_acquisition_stop(sdi, devc->session_cb_data);
-                       return TRUE;
-               }
-       }
        return TRUE;
 }