X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=src%2Fhardware%2Fscpi-pps%2Fapi.c;h=7448796298ddb082a777d123ed75b428e303bb0e;hb=e66d1892d0dd16ed166fc8f13493f95de0607362;hp=10ab5f9e8335147bae53ad3b804490f073590893;hpb=c50805d59ce85e00ef263c0a1edad2487bb8a9fe;p=libsigrok.git diff --git a/src/hardware/scpi-pps/api.c b/src/hardware/scpi-pps/api.c index 10ab5f9e..74487962 100644 --- a/src/hardware/scpi-pps/api.c +++ b/src/hardware/scpi-pps/api.c @@ -23,7 +23,8 @@ #include "scpi.h" #include "protocol.h" -SR_PRIV struct sr_dev_driver scpi_pps_driver_info; +static struct sr_dev_driver scpi_pps_driver_info; +static struct sr_dev_driver hp_ib_pps_driver_info; static const uint32_t scanopts[] = { SR_CONF_CONN, @@ -41,12 +42,9 @@ static const struct pps_channel_instance pci[] = { { SR_MQ_FREQUENCY, SCPI_CMD_GET_MEAS_FREQUENCY, "F" }, }; -static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx) -{ - return std_init(sr_ctx, di, LOG_PREFIX); -} - -static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi) +static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi, + int (*get_hw_id)(struct sr_scpi_dev_inst *scpi, + struct sr_scpi_hw_info **scpi_response)) { struct dev_context *devc; struct sr_dev_inst *sdi; @@ -67,7 +65,7 @@ static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi) const char *vendor; char ch_name[16]; - if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) { + if (get_hw_id(scpi, &hw_info) != SR_OK) { sr_info("Couldn't get IDN response."); return NULL; } @@ -171,19 +169,80 @@ static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi) return sdi; } -static GSList *scan(struct sr_dev_driver *di, GSList *options) +static gchar *hpib_get_revision(struct sr_scpi_dev_inst *scpi) +{ + int ret; + gboolean matches; + char *response; + GRegex *version_regex; + + ret = sr_scpi_get_string(scpi, "ROM?", &response); + if (ret != SR_OK && !response) + return NULL; + + /* Example version string: "B01 B01" */ + version_regex = g_regex_new("[A-Z][0-9]{2} [A-Z][0-9]{2}", 0, 0, NULL); + matches = g_regex_match(version_regex, response, 0, NULL); + g_regex_unref(version_regex); + + if (!matches) { + /* Not a valid version string. Ignore it. */ + g_free(response); + response = NULL; + } else { + /* Replace space with dot. */ + response[3] = '.'; + } + + return response; +} + +/* + * This function assumes the response is in the form "HP" + * + * HP made many GPIB (then called HP-IB) instruments before the SCPI command + * set was introduced into the standard. We haven't seen any non-HP instruments + * which respond to the "ID?" query, so assume all are HP for now. + */ +static int hpib_get_hw_id(struct sr_scpi_dev_inst *scpi, + struct sr_scpi_hw_info **scpi_response) +{ + int ret; + char *response; + struct sr_scpi_hw_info *hw_info; + + ret = sr_scpi_get_string(scpi, "ID?", &response); + if ((ret != SR_OK) || !response) + return SR_ERR; + + hw_info = g_malloc0(sizeof(struct sr_scpi_hw_info)); + + *scpi_response = hw_info; + hw_info->model = response; + hw_info->firmware_version = hpib_get_revision(scpi); + hw_info->manufacturer = g_strdup("HP"); + + return SR_OK; +} + +static struct sr_dev_inst *probe_scpi_pps_device(struct sr_scpi_dev_inst *scpi) { - return sr_scpi_scan(di->context, options, probe_device); + return probe_device(scpi, sr_scpi_get_hw_id); } -static GSList *dev_list(const struct sr_dev_driver *di) +static struct sr_dev_inst *probe_hpib_pps_device(struct sr_scpi_dev_inst *scpi) { - return ((struct drv_context *)(di->context))->instances; + return probe_device(scpi, hpib_get_hw_id); } -static int dev_clear(const struct sr_dev_driver *di) +static GSList *scan_scpi_pps(struct sr_dev_driver *di, GSList *options) +{ + return sr_scpi_scan(di->context, options, probe_scpi_pps_device); +} + +static GSList *scan_hpib_pps(struct sr_dev_driver *di, GSList *options) { - return std_dev_clear(di, NULL); + return sr_scpi_scan(di->context, options, probe_hpib_pps_device); } static int dev_open(struct sr_dev_inst *sdi) @@ -192,15 +251,10 @@ static int dev_open(struct sr_dev_inst *sdi) struct sr_scpi_dev_inst *scpi; GVariant *beeper; - if (sdi->status != SR_ST_INACTIVE) - return SR_ERR; - scpi = sdi->conn; if (sr_scpi_open(scpi) < 0) return SR_ERR; - sdi->status = SR_ST_ACTIVE; - devc = sdi->priv; scpi_cmd(sdi, devc->device->commands, SCPI_CMD_REMOTE); devc->beeper_was_set = FALSE; @@ -221,35 +275,28 @@ static int dev_close(struct sr_dev_inst *sdi) struct sr_scpi_dev_inst *scpi; struct dev_context *devc; - if (sdi->status != SR_ST_ACTIVE) - return SR_ERR_DEV_CLOSED; - devc = sdi->priv; scpi = sdi->conn; - if (scpi) { - if (devc->beeper_was_set) - scpi_cmd(sdi, devc->device->commands, SCPI_CMD_BEEPER_ENABLE); - scpi_cmd(sdi, devc->device->commands, SCPI_CMD_LOCAL); - sr_scpi_close(scpi); - sdi->status = SR_ST_INACTIVE; - } - return SR_OK; + if (!scpi) + return SR_ERR_BUG; + + if (devc->beeper_was_set) + scpi_cmd(sdi, devc->device->commands, SCPI_CMD_BEEPER_ENABLE); + scpi_cmd(sdi, devc->device->commands, SCPI_CMD_LOCAL); + + return sr_scpi_close(scpi); } -static void clear_helper(void *priv) +static void clear_helper(struct dev_context *devc) { - struct dev_context *devc; - - devc = priv; g_free(devc->channels); g_free(devc->channel_groups); - g_free(devc); } -static int cleanup(const struct sr_dev_driver *di) +static int dev_clear(const struct sr_dev_driver *di) { - return std_dev_clear(di, clear_helper); + return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper); } static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, @@ -391,9 +438,6 @@ static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sd if (!sdi) return SR_ERR_ARG; - if (sdi->status != SR_ST_ACTIVE) - return SR_ERR_DEV_CLOSED; - if (cg) /* Channel group specified. */ select_channel(sdi, cg->channels->data); @@ -473,32 +517,21 @@ static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst * const struct channel_spec *ch_spec; GVariant *gvar; GVariantBuilder gvb; - int ret, i; + int i; const char *s[16]; - /* Always available, even without sdi. */ - 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; - } - - if (!sdi) - return SR_ERR_ARG; - devc = sdi->priv; + devc = (sdi) ? sdi->priv : NULL; - ret = SR_OK; if (!cg) { /* No channel group: global options. */ switch (key) { + case SR_CONF_SCAN_OPTIONS: case SR_CONF_DEVICE_OPTIONS: - *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, - devc->device->devopts, devc->device->num_devopts, - sizeof(uint32_t)); + return std_opts_config_list(key, data, sdi, cg, + scanopts, ARRAY_SIZE(scanopts), + drvopts, ARRAY_SIZE(drvopts), + (devc) ? devc->device->devopts : NULL, + (devc) ? devc->device->num_devopts : 0); break; case SR_CONF_CHANNEL_CONFIG: /* Not used. */ @@ -572,7 +605,7 @@ static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst * } } - return ret; + return SR_OK; } static int dev_acquisition_start(const struct sr_dev_inst *sdi) @@ -583,16 +616,13 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi) struct pps_channel *pch; int cmd, ret; - if (sdi->status != SR_ST_ACTIVE) - return SR_ERR_DEV_CLOSED; - devc = sdi->priv; scpi = sdi->conn; if ((ret = sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 10, scpi_pps_receive_data, (void *)sdi)) != SR_OK) return ret; - std_session_send_df_header(sdi, LOG_PREFIX); + std_session_send_df_header(sdi); /* Prime the pipe with the first channel's fetch. */ ch = sr_next_enabled_channel(sdi, NULL); @@ -619,9 +649,6 @@ static int dev_acquisition_stop(struct sr_dev_inst *sdi) struct sr_scpi_dev_inst *scpi; float f; - if (sdi->status != SR_ST_ACTIVE) - return SR_ERR_DEV_CLOSED; - scpi = sdi->conn; /* @@ -632,19 +659,38 @@ static int dev_acquisition_stop(struct sr_dev_inst *sdi) sr_scpi_get_float(scpi, NULL, &f); sr_scpi_source_remove(sdi->session, scpi); - std_session_send_df_end(sdi, LOG_PREFIX); + std_session_send_df_end(sdi); return SR_OK; } -SR_PRIV struct sr_dev_driver scpi_pps_driver_info = { +static struct sr_dev_driver scpi_pps_driver_info = { .name = "scpi-pps", .longname = "SCPI PPS", .api_version = 1, - .init = init, - .cleanup = cleanup, - .scan = scan, - .dev_list = dev_list, + .init = std_init, + .cleanup = std_cleanup, + .scan = scan_scpi_pps, + .dev_list = std_dev_list, + .dev_clear = dev_clear, + .config_get = config_get, + .config_set = config_set, + .config_list = config_list, + .dev_open = dev_open, + .dev_close = dev_close, + .dev_acquisition_start = dev_acquisition_start, + .dev_acquisition_stop = dev_acquisition_stop, + .context = NULL, +}; + +static struct sr_dev_driver hp_ib_pps_driver_info = { + .name = "hpib-pps", + .longname = "HP-IB PPS", + .api_version = 1, + .init = std_init, + .cleanup = std_cleanup, + .scan = scan_hpib_pps, + .dev_list = std_dev_list, .dev_clear = dev_clear, .config_get = config_get, .config_set = config_set, @@ -655,3 +701,5 @@ SR_PRIV struct sr_dev_driver scpi_pps_driver_info = { .dev_acquisition_stop = dev_acquisition_stop, .context = NULL, }; +SR_REGISTER_DEV_DRIVER(scpi_pps_driver_info); +SR_REGISTER_DEV_DRIVER(hp_ib_pps_driver_info);