]> sigrok.org Git - libsigrok.git/blobdiff - hardware/mic-985xx/api.c
Enforce open device before config_set()/dev_acquisition_start()
[libsigrok.git] / hardware / mic-985xx / api.c
index 2920ca8240512da7e757224f50a429a96b77f98a..4c093148750de14b6598e61466c1de2d21ce33d8 100644 (file)
 
 #include "protocol.h"
 
-static const int hwopts[] = {
+static const int32_t hwopts[] = {
        SR_CONF_CONN,
        SR_CONF_SERIALCOMM,
-       0,
 };
 
-static const int hwcaps[] = {
+static const int32_t hwcaps[] = {
        SR_CONF_THERMOMETER,
        SR_CONF_HYGROMETER,
        SR_CONF_LIMIT_SAMPLES,
        SR_CONF_LIMIT_MSEC,
        SR_CONF_CONTINUOUS,
-       0,
 };
 
+SR_PRIV struct sr_dev_driver mic_98581_driver_info;
 SR_PRIV struct sr_dev_driver mic_98583_driver_info;
 
 SR_PRIV const struct mic_dev_info mic_devs[] = {
        {
-               "MIC", "98583", "38400/8n2", 32000, TRUE, TRUE,
+               "MIC", "98581", "38400/8n2", 32000, TRUE, FALSE, 6,
+               packet_valid_temp,
+               &mic_98581_driver_info, receive_data_MIC_98581,
+       },
+       {
+               "MIC", "98583", "38400/8n2", 32000, TRUE, TRUE, 10,
+               packet_valid_temp_hum,
                &mic_98583_driver_info, receive_data_MIC_98583,
        },
 };
@@ -49,6 +54,7 @@ static int clear_instances(int idx)
        struct sr_dev_inst *sdi;
        struct drv_context *drvc;
        struct dev_context *devc;
+       struct sr_serial_dev_inst *serial;
        GSList *l;
        struct sr_dev_driver *di;
 
@@ -62,7 +68,8 @@ static int clear_instances(int idx)
                        continue;
                if (!(devc = sdi->priv))
                        continue;
-               sr_serial_dev_inst_free(devc->serial);
+               serial = sdi->conn;
+               sr_serial_dev_inst_free(serial);
                sr_dev_inst_free(sdi);
        }
 
@@ -113,7 +120,8 @@ static GSList *scan(const char *conn, const char *serialcomm, int idx)
                goto scan_cleanup;
        }
 
-       devc->serial = serial;
+       sdi->inst_type = SR_INST_SERIAL;
+       sdi->conn = serial;
 
        sdi->priv = devc;
        sdi->driver = mic_devs[idx].di;
@@ -122,9 +130,11 @@ static GSList *scan(const char *conn, const char *serialcomm, int idx)
                goto scan_cleanup;
        sdi->probes = g_slist_append(sdi->probes, probe);
 
-       if (!(probe = sr_probe_new(1, SR_PROBE_ANALOG, TRUE, "Humidity")))
-               goto scan_cleanup;
-       sdi->probes = g_slist_append(sdi->probes, probe);
+       if (mic_devs[idx].has_humidity) {
+               if (!(probe = sr_probe_new(1, SR_PROBE_ANALOG, TRUE, "Humidity")))
+                       goto scan_cleanup;
+               sdi->probes = g_slist_append(sdi->probes, probe);
+       }
 
        drvc->instances = g_slist_append(drvc->instances, sdi);
        devices = g_slist_append(devices, sdi);
@@ -146,10 +156,10 @@ static GSList *hw_scan(GSList *options, int idx)
                src = l->data;
                switch (src->key) {
                case SR_CONF_CONN:
-                       conn = src->value;
+                       conn = g_variant_get_string(src->data, NULL);
                        break;
                case SR_CONF_SERIALCOMM:
-                       serialcomm = src->value;
+                       serialcomm  = g_variant_get_string(src->data, NULL);
                        break;
                }
        }
@@ -174,11 +184,10 @@ static GSList *hw_dev_list(int idx)
 
 static int hw_dev_open(struct sr_dev_inst *sdi)
 {
-       struct dev_context *devc;
-
-       devc = sdi->priv;
+       struct sr_serial_dev_inst *serial;
 
-       if (serial_open(devc->serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
+       serial = sdi->conn;
+       if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
                return SR_ERR;
 
        sdi->status = SR_ST_ACTIVE;
@@ -188,12 +197,11 @@ static int hw_dev_open(struct sr_dev_inst *sdi)
 
 static int hw_dev_close(struct sr_dev_inst *sdi)
 {
-       struct dev_context *devc;
-
-       devc = sdi->priv;
+       struct sr_serial_dev_inst *serial;
 
-       if (devc->serial && devc->serial->fd != -1) {
-               serial_close(devc->serial);
+       serial = sdi->conn;
+       if (serial && serial->fd != -1) {
+               serial_close(serial);
                sdi->status = SR_ST_INACTIVE;
        }
 
@@ -207,47 +215,48 @@ static int hw_cleanup(int idx)
        return SR_OK;
 }
 
-static int config_set(int id, const void *value, const struct sr_dev_inst *sdi)
+static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi)
 {
        struct dev_context *devc;
 
        if (sdi->status != SR_ST_ACTIVE)
-               return SR_ERR;
+               return SR_ERR_DEV_CLOSED;
 
        devc = sdi->priv;
 
        switch (id) {
        case SR_CONF_LIMIT_SAMPLES:
-               devc->limit_samples = *(const uint64_t *)value;
+               devc->limit_samples = g_variant_get_uint64(data);
                sr_dbg("Setting sample limit to %" PRIu64 ".",
                       devc->limit_samples);
                break;
        case SR_CONF_LIMIT_MSEC:
-               devc->limit_msec = *(const uint64_t *)value;
+               devc->limit_msec = g_variant_get_uint64(data);
                sr_dbg("Setting time limit to %" PRIu64 "ms.",
                       devc->limit_msec);
                break;
        default:
-               sr_err("Unknown config: %d.", id);
-               return SR_ERR_ARG;
+               return SR_ERR_NA;
        }
 
        return SR_OK;
 }
 
-static int config_list(int key, const void **data, const struct sr_dev_inst *sdi)
+static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi)
 {
        (void)sdi;
 
        switch (key) {
        case SR_CONF_SCAN_OPTIONS:
-               *data = hwopts;
+               *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
+                               hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
                break;
        case SR_CONF_DEVICE_OPTIONS:
-               *data = hwcaps;
+               *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
+                               hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
                break;
        default:
-               return SR_ERR_ARG;
+               return SR_ERR_NA;
        }
 
        return SR_OK;
@@ -257,11 +266,13 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
                                    void *cb_data, int idx)
 {
        struct dev_context *devc;
+       struct sr_serial_dev_inst *serial;
 
-       devc = sdi->priv;
+       if (sdi->status != SR_ST_ACTIVE)
+               return SR_ERR_DEV_CLOSED;
 
+       devc = sdi->priv;
        devc->cb_data = cb_data;
-
        devc->num_samples = 0;
        devc->starttime = g_get_monotonic_time();
 
@@ -269,7 +280,8 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
        std_session_send_df_header(cb_data, DRIVER_LOG_DOMAIN);
 
        /* Poll every 100ms, or whenever some data comes in. */
-       sr_source_add(devc->serial->fd, G_IO_IN, 100,
+       serial = sdi->conn;
+       sr_source_add(serial->fd, G_IO_IN, 100,
                      mic_devs[idx].receive_data, (void *)sdi);
 
        return SR_OK;
@@ -278,7 +290,7 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
 {
        return std_hw_dev_acquisition_stop_serial(sdi, cb_data, hw_dev_close,
-              ((struct dev_context *)(sdi->priv))->serial, DRIVER_LOG_DOMAIN);
+                                                 sdi->conn, DRIVER_LOG_DOMAIN);
 }
 
 /* Driver-specific API function wrappers */
@@ -323,4 +335,5 @@ SR_PRIV struct sr_dev_driver ID##_driver_info = { \
        .priv = NULL, \
 };
 
+DRV(mic_98581, MIC_98581, "mic-98581", "MIC 98581")
 DRV(mic_98583, MIC_98583, "mic-98583", "MIC 98583")