]> sigrok.org Git - libsigrok.git/blobdiff - src/hardware/hp-3457a/api.c
hp-3457a: only probe when conn= was specified, to not break SCPI devices
[libsigrok.git] / src / hardware / hp-3457a / api.c
index b152be9236427aafa61f02fcfc051c18d8128852..4cfc2d758b54e79625609d408ef0f506d028a5b7 100644 (file)
@@ -31,13 +31,13 @@ static const uint32_t drvopts[] = {
 };
 
 static const uint32_t devopts[] = {
-       SR_CONF_CONTINUOUS | SR_CONF_SET,
+       SR_CONF_CONTINUOUS,
        SR_CONF_LIMIT_SAMPLES | SR_CONF_SET,
        SR_CONF_MEASURED_QUANTITY | SR_CONF_SET,
        SR_CONF_ADC_POWERLINE_CYCLES | SR_CONF_SET | SR_CONF_GET,
 };
 
-SR_PRIV struct sr_dev_driver hp_3457a_driver_info;
+static struct sr_dev_driver hp_3457a_driver_info;
 
 static int create_front_channel(struct sr_dev_inst *sdi, int chan_idx)
 {
@@ -127,6 +127,14 @@ static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi)
        struct sr_dev_inst *sdi;
        struct dev_context *devc;
 
+       /*
+        * The device cannot get identified by means of SCPI queries.
+        * Neither shall non-SCPI requests get emitted before reliable
+        * identification of the device. Assume that we only get here
+        * when user specs led us to believe it's safe to communicate
+        * to the expected kind of device.
+        */
+
        /*
         * This command ensures we receive an EOI after every response, so that
         * we don't wait the entire timeout after the response is received.
@@ -138,13 +146,15 @@ static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi)
        if ((ret != SR_OK) || !response)
                return NULL;
 
-       if (strcmp(response, "HP3457A"))
+       if (strcmp(response, "HP3457A") != 0) {
+               g_free(response);
                return NULL;
+       }
 
        g_free(response);
 
-       devc = g_malloc0(sizeof(struct dev_context));
-       sdi = g_malloc0(sizeof(struct sr_dev_inst));
+       devc = g_malloc0(sizeof(*devc));
+       sdi = g_malloc0(sizeof(*sdi));
        sdi->vendor = g_strdup("Hewlett-Packard");
        sdi->model = g_strdup("3457A");
        sdi->version = get_revision(scpi);
@@ -166,24 +176,17 @@ static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi)
        return sdi;
 }
 
-static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
-{
-       return std_init(sr_ctx, di, LOG_PREFIX);
-}
-
 static GSList *scan(struct sr_dev_driver *di, GSList *options)
 {
-       return sr_scpi_scan(di->context, options, probe_device);
-}
+       const char *conn;
 
-static GSList *dev_list(const struct sr_dev_driver *di)
-{
-       return ((struct drv_context *)(di->context))->instances;
-}
+       /* Only scan for a device when conn= was specified. */
+       conn = NULL;
+       (void)sr_serial_extract_options(options, &conn, NULL);
+       if (!conn)
+               return NULL;
 
-static int dev_clear(const struct sr_dev_driver *di)
-{
-       return std_dev_clear(di, NULL);
+       return sr_scpi_scan(di->context, options, probe_device);
 }
 
 /*
@@ -220,8 +223,6 @@ static int dev_open(struct sr_dev_inst *sdi)
        sr_scpi_send(scpi, "TRIG HOLD");
        sr_scpi_get_float(scpi, "NPLC?", &devc->nplc);
 
-       sdi->status = SR_ST_ACTIVE;
-
        return SR_OK;
 }
 
@@ -229,9 +230,6 @@ static int dev_close(struct sr_dev_inst *sdi)
 {
        struct sr_scpi_dev_inst *scpi = sdi->conn;
 
-       if (sdi->status != SR_ST_ACTIVE)
-               return SR_ERR_DEV_CLOSED;
-
        /* Disable scan-advance (preserve relay life). */
        sr_scpi_send(scpi, "SADV HOLD");
        /* Switch back to auto-triggering. */
@@ -239,27 +237,18 @@ static int dev_close(struct sr_dev_inst *sdi)
 
        sr_scpi_close(scpi);
 
-       sdi->status = SR_ST_INACTIVE;
-
        return SR_OK;
 }
 
-static int cleanup(const struct sr_dev_driver *di)
-{
-       return dev_clear(di);
-}
-
 static int config_get(uint32_t key, GVariant **data,
        const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
 {
-       int ret;
        struct dev_context *devc;
 
        (void)cg;
 
        devc = sdi->priv;
 
-       ret = SR_OK;
        switch (key) {
        case SR_CONF_ADC_POWERLINE_CYCLES:
                *data = g_variant_new_double(devc->nplc);
@@ -268,14 +257,12 @@ static int config_get(uint32_t key, GVariant **data,
                return SR_ERR_NA;
        }
 
-       return ret;
+       return SR_OK;
 }
 
 static int config_set(uint32_t key, GVariant *data,
-                     const struct sr_dev_inst *sdi,
-                     const struct sr_channel_group *cg)
+       const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
 {
-       int ret;
        enum sr_mq mq;
        enum sr_mqflag mq_flags;
        struct dev_context *devc;
@@ -283,12 +270,8 @@ static int config_set(uint32_t key, GVariant *data,
 
        (void)cg;
 
-       if (sdi->status != SR_ST_ACTIVE)
-               return SR_ERR_DEV_CLOSED;
-
        devc = sdi->priv;
 
-       ret = SR_OK;
        switch (key) {
        case SR_CONF_LIMIT_SAMPLES:
                devc->limit_samples = g_variant_get_uint64(data);
@@ -298,54 +281,26 @@ static int config_set(uint32_t key, GVariant *data,
                mq = g_variant_get_uint32(tuple_child);
                tuple_child = g_variant_get_child_value(data, 1);
                mq_flags = g_variant_get_uint64(tuple_child);
-               ret = hp_3457a_set_mq(sdi, mq, mq_flags);
                g_variant_unref(tuple_child);
-               break;
+               return hp_3457a_set_mq(sdi, mq, mq_flags);
        case SR_CONF_ADC_POWERLINE_CYCLES:
-               ret = hp_3457a_set_nplc(sdi, g_variant_get_double(data));
-               break;
+               return hp_3457a_set_nplc(sdi, g_variant_get_double(data));
        default:
-               ret = SR_ERR_NA;
+               return SR_ERR_NA;
        }
 
-       return ret;
+       return SR_OK;
 }
 
 static int config_list(uint32_t key, GVariant **data,
        const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
 {
-       int ret;
-
-       if (key == SR_CONF_SCAN_OPTIONS) {
-               *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
-                       scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
-               return SR_OK;
-       } else if ((key == SR_CONF_DEVICE_OPTIONS) && !sdi) {
-               *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
-                       drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
-               return SR_OK;
-       } else if ((key == SR_CONF_DEVICE_OPTIONS) && !cg) {
-               *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
-                       devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
-               return SR_OK;
-       }
-
-       /* From here on, we're only concerned with channel group config. */
-       if (!cg)
-               return SR_ERR_NA;
-
        /*
         * TODO: Implement channel group configuration when adding support for
         * plug-in cards.
         */
 
-       ret = SR_OK;
-       switch (key) {
-       default:
-               ret = SR_ERR_NA;
-       }
-
-       return ret;
+       return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
 }
 
 static void create_channel_index_list(GSList *channels, GArray **arr)
@@ -387,9 +342,6 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi)
        GArray *ch_list;
        GSList *channels;
 
-       if (sdi->status != SR_ST_ACTIVE)
-               return SR_ERR_DEV_CLOSED;
-
        scpi = sdi->conn;
        devc = sdi->priv;
 
@@ -398,7 +350,7 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi)
        if (ret != SR_OK)
                return ret;
 
-       std_session_send_df_header(sdi, LOG_PREFIX);
+       std_session_send_df_header(sdi);
 
        front_selected = rear_selected = FALSE;
        devc->active_channels = NULL;
@@ -425,8 +377,13 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi)
                return SR_ERR_ARG;
        }
 
-       devc->current_channel = devc->active_channels->data;
        devc->num_active_channels = g_slist_length(devc->active_channels);
+       if (!devc->num_active_channels) {
+               sr_err("Need at least one active channel!");
+               g_slist_free(devc->active_channels);
+               return SR_ERR_ARG;
+       }
+       devc->current_channel = devc->active_channels->data;
 
        hp_3457a_select_input(sdi, front_selected ? CONN_FRONT : CONN_REAR);
 
@@ -457,15 +414,15 @@ static int dev_acquisition_stop(struct sr_dev_inst *sdi)
        return SR_OK;
 }
 
-SR_PRIV struct sr_dev_driver hp_3457a_driver_info = {
+static struct sr_dev_driver hp_3457a_driver_info = {
        .name = "hp-3457a",
        .longname = "HP 3457A",
        .api_version = 1,
-       .init = init,
-       .cleanup = cleanup,
+       .init = std_init,
+       .cleanup = std_cleanup,
        .scan = scan,
-       .dev_list = dev_list,
-       .dev_clear = dev_clear,
+       .dev_list = std_dev_list,
+       .dev_clear = std_dev_clear,
        .config_get = config_get,
        .config_set = config_set,
        .config_list = config_list,
@@ -475,3 +432,4 @@ SR_PRIV struct sr_dev_driver hp_3457a_driver_info = {
        .dev_acquisition_stop = dev_acquisition_stop,
        .context = NULL,
 };
+SR_REGISTER_DEV_DRIVER(hp_3457a_driver_info);