]> sigrok.org Git - libsigrok.git/blobdiff - src/hardware/baylibre-acme/api.c
baylibre-acme: Close sysfs files after stopping the acquisition.
[libsigrok.git] / src / hardware / baylibre-acme / api.c
index f1da9b507668a7bdad5c8eaa63ef58c9ef389b60..75f2c0446e4f76a94d95e5abe4809b93c7997214 100644 (file)
@@ -28,10 +28,19 @@ static const uint32_t devopts[] = {
        SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
 };
 
-static const uint32_t devopts_cg[] = {
-       SR_CONF_PROBE_FACTOR | SR_CONF_GET | SR_CONF_SET,
-       SR_CONF_POWER_OFF | SR_CONF_GET | SR_CONF_SET,
-};
+/*
+ * Currently there are two channel-group/probe options for ACME:
+ *   - SR_CONF_PROBE_FACTOR - allows to modify current shunt resistance
+ *     calibration
+ *   - SR_CONF_POWER_OFF - allows to remotely cut-off/restore power to
+ *     measured devices
+ *
+ * They are not static - we have to check each probe's capabilities in
+ * config_list().
+ */
+#define MAX_DEVOPTS_CG         2
+#define HAS_PROBE_FACTOR       (SR_CONF_PROBE_FACTOR | SR_CONF_GET | SR_CONF_SET)
+#define HAS_POWER_OFF          (SR_CONF_POWER_OFF | SR_CONF_GET | SR_CONF_SET)
 
 #define MAX_SAMPLE_RATE 500 /* In Hz */
 
@@ -115,7 +124,7 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options)
         * Let's assume there's no ACME device present if no probe
         * has been registered.
         */
-       if (sdi->channel_groups == NULL)
+       if (!sdi->channel_groups)
                goto err_out;
 
        devices = g_slist_append(devices, sdi);
@@ -228,12 +237,10 @@ static int config_set(uint32_t key, GVariant *data,
        case SR_CONF_LIMIT_SAMPLES:
                devc->limit_samples = g_variant_get_uint64(data);
                devc->limit_msec = 0;
-               sr_dbg("Setting sample limit to %" PRIu64, devc->limit_samples);
                break;
        case SR_CONF_LIMIT_MSEC:
                devc->limit_msec = g_variant_get_uint64(data) * 1000;
                devc->limit_samples = 0;
-               sr_dbg("Setting time limit to %" PRIu64"ms", devc->limit_msec);
                break;
        case SR_CONF_SAMPLERATE:
                samplerate = g_variant_get_uint64(data);
@@ -243,7 +250,7 @@ static int config_set(uint32_t key, GVariant *data,
                        break;
                }
                devc->samplerate = samplerate;
-               sr_dbg("Setting samplerate to %" PRIu64, devc->samplerate);
+               bl_acme_maybe_set_update_interval(sdi, samplerate);
                break;
        case SR_CONF_PROBE_FACTOR:
                if (!cg)
@@ -266,9 +273,10 @@ static int config_list(uint32_t key, GVariant **data,
                       const struct sr_dev_inst *sdi,
                       const struct sr_channel_group *cg)
 {
+       uint32_t devopts_cg[MAX_DEVOPTS_CG];
        GVariant *gvar;
        GVariantBuilder gvb;
-       int ret;
+       int ret, num_devopts_cg = 0;
 
        (void)sdi;
        (void)cg;
@@ -294,8 +302,13 @@ static int config_list(uint32_t key, GVariant **data,
        } else {
                switch (key) {
                case SR_CONF_DEVICE_OPTIONS:
+                       if (bl_acme_get_probe_type(cg) == PROBE_ENRG)
+                               devopts_cg[num_devopts_cg++] = HAS_PROBE_FACTOR;
+                       if (bl_acme_probe_has_pws(cg))
+                               devopts_cg[num_devopts_cg++] = HAS_POWER_OFF;
+
                        *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
-                               devopts_cg, ARRAY_SIZE(devopts_cg), sizeof(uint32_t));
+                               devopts_cg, num_devopts_cg, sizeof(uint32_t));
                        break;
                default:
                        return SR_ERR_NA;
@@ -305,6 +318,34 @@ static int config_list(uint32_t key, GVariant **data,
        return ret;
 }
 
+static void dev_acquisition_close(const struct sr_dev_inst *sdi)
+{
+       GSList *chl;
+       struct sr_channel *ch;
+
+       for (chl = sdi->channels; chl; chl = chl->next) {
+               ch = chl->data;
+               bl_acme_close_channel(ch);
+       }
+}
+
+static int dev_acquisition_open(const struct sr_dev_inst *sdi)
+{
+       GSList *chl;
+       struct sr_channel *ch;
+
+       for (chl = sdi->channels; chl; chl = chl->next) {
+               ch = chl->data;
+               if (bl_acme_open_channel(ch)) {
+                       sr_err("Error opening channel %s", ch->name);
+                       dev_acquisition_close(sdi);
+                       return SR_ERR;
+               }
+       }
+
+       return 0;
+}
+
 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
 {
        struct dev_context *devc;
@@ -314,6 +355,9 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
        if (sdi->status != SR_ST_ACTIVE)
                return SR_ERR_DEV_CLOSED;
 
+       if (dev_acquisition_open(sdi))
+               return SR_ERR;
+
        devc = sdi->priv;
        devc->samples_read = 0;
 
@@ -349,6 +393,7 @@ static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
        if (sdi->status != SR_ST_ACTIVE)
                return SR_ERR_DEV_CLOSED;
 
+       dev_acquisition_close(sdi);
        sr_session_source_remove_channel(sdi->session, devc->channel);
        g_io_channel_shutdown(devc->channel, FALSE, NULL);
        g_io_channel_unref(devc->channel);