X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=src%2Fhwdriver.c;h=ec0c706e97279ee431d516b9a19d4ba81d9d412d;hb=032da34b786333a1af811235c5cf29855479f0b6;hp=e36990a258404842dd4a9ea25624dc23f86794a4;hpb=584560f142e1b17b9f4ef9069bd3724f2f77e750;p=libsigrok.git diff --git a/src/hwdriver.c b/src/hwdriver.c index e36990a2..ec0c706e 100644 --- a/src/hwdriver.c +++ b/src/hwdriver.c @@ -31,8 +31,6 @@ #define LOG_PREFIX "hwdriver" /** @endcond */ -extern SR_PRIV struct sr_dev_driver *drivers_list[]; - /** * @file * @@ -47,27 +45,42 @@ extern SR_PRIV struct sr_dev_driver *drivers_list[]; * @{ */ +/* Same key order/grouping as in enum sr_configkey (libsigrok.h). */ static struct sr_config_info sr_config_info_data[] = { + /* Device classes */ + {SR_CONF_LOGIC_ANALYZER, SR_T_STRING, NULL, "Logic analyzer", NULL}, + {SR_CONF_OSCILLOSCOPE, SR_T_STRING, NULL, "Oscilloscope", NULL}, + {SR_CONF_MULTIMETER, SR_T_STRING, NULL, "Multimeter", NULL}, + {SR_CONF_DEMO_DEV, SR_T_STRING, NULL, "Demo device", NULL}, + {SR_CONF_SOUNDLEVELMETER, SR_T_STRING, NULL, "Sound level meter", NULL}, + {SR_CONF_THERMOMETER, SR_T_STRING, NULL, "Thermometer", NULL}, + {SR_CONF_HYGROMETER, SR_T_STRING, NULL, "Hygrometer", NULL}, + {SR_CONF_ENERGYMETER, SR_T_STRING, NULL, "Energy meter", NULL}, + {SR_CONF_DEMODULATOR, SR_T_STRING, NULL, "Demodulator", NULL}, + {SR_CONF_POWER_SUPPLY, SR_T_STRING, NULL, "Power supply", NULL}, + {SR_CONF_LCRMETER, SR_T_STRING, NULL, "LCR meter", NULL}, + + /* Driver scan options */ {SR_CONF_CONN, SR_T_STRING, "conn", "Connection", NULL}, {SR_CONF_SERIALCOMM, SR_T_STRING, "serialcomm", "Serial communication", NULL}, + + /* Device (or channel group) configuration */ {SR_CONF_SAMPLERATE, SR_T_UINT64, "samplerate", "Sample rate", NULL}, {SR_CONF_CAPTURE_RATIO, SR_T_UINT64, "captureratio", "Pre-trigger capture ratio", NULL}, {SR_CONF_PATTERN_MODE, SR_T_STRING, "pattern", "Pattern", NULL}, - {SR_CONF_TRIGGER_MATCH, SR_T_INT32, "triggermatch", - "Trigger matches", NULL}, - {SR_CONF_EXTERNAL_CLOCK, SR_T_BOOL, "external_clock", - "External clock mode", NULL}, - {SR_CONF_SWAP, SR_T_BOOL, "swap", - "Swap channel order", NULL}, {SR_CONF_RLE, SR_T_BOOL, "rle", - "Run Length Encoding", NULL}, + "Run length encoding", NULL}, {SR_CONF_TRIGGER_SLOPE, SR_T_STRING, "triggerslope", "Trigger slope", NULL}, + {SR_CONF_AVERAGING, SR_T_BOOL, "averaging", + "Averaging", NULL}, + {SR_CONF_AVG_SAMPLES, SR_T_UINT64, "avg_samples", + "Number of samples to average over", NULL}, {SR_CONF_TRIGGER_SOURCE, SR_T_STRING, "triggersource", "Trigger source", NULL}, {SR_CONF_HORIZ_TRIGGERPOS, SR_T_FLOAT, "horiz_triggerpos", @@ -76,42 +89,50 @@ static struct sr_config_info sr_config_info_data[] = { "Buffer size", NULL}, {SR_CONF_TIMEBASE, SR_T_RATIONAL_PERIOD, "timebase", "Time base", NULL}, - {SR_CONF_FILTER, SR_T_STRING, "filter", - "Filter targets", NULL}, + {SR_CONF_FILTER, SR_T_BOOL, "filter", + "Filter", NULL}, {SR_CONF_VDIV, SR_T_RATIONAL_VOLT, "vdiv", "Volts/div", NULL}, {SR_CONF_COUPLING, SR_T_STRING, "coupling", "Coupling", NULL}, - {SR_CONF_DATALOG, SR_T_BOOL, "datalog", - "Datalog", NULL}, + {SR_CONF_TRIGGER_MATCH, SR_T_INT32, "triggermatch", + "Trigger matches", NULL}, + {SR_CONF_SAMPLE_INTERVAL, SR_T_UINT64, "sample_interval", + "Sample interval", NULL}, + {SR_CONF_NUM_HDIV, SR_T_INT32, "num_hdiv", + "Number of horizontal divisions", NULL}, + {SR_CONF_NUM_VDIV, SR_T_INT32, "num_vdiv", + "Number of vertical divisions", NULL}, {SR_CONF_SPL_WEIGHT_FREQ, SR_T_STRING, "spl_weight_freq", "Sound pressure level frequency weighting", NULL}, {SR_CONF_SPL_WEIGHT_TIME, SR_T_STRING, "spl_weight_time", "Sound pressure level time weighting", NULL}, + {SR_CONF_SPL_MEASUREMENT_RANGE, SR_T_UINT64_RANGE, "spl_meas_range", + "Sound pressure level measurement range", NULL}, {SR_CONF_HOLD_MAX, SR_T_BOOL, "hold_max", "Hold max", NULL}, {SR_CONF_HOLD_MIN, SR_T_BOOL, "hold_min", "Hold min", NULL}, - {SR_CONF_SPL_MEASUREMENT_RANGE, SR_T_UINT64_RANGE, "spl_meas_range", - "Sound pressure level measurement range", NULL}, {SR_CONF_VOLTAGE_THRESHOLD, SR_T_DOUBLE_RANGE, "voltage_threshold", "Voltage threshold", NULL }, - {SR_CONF_POWER_OFF, SR_T_BOOL, "power_off", - "Power off", NULL}, - {SR_CONF_DATA_SOURCE, SR_T_STRING, "data_source", - "Data source", NULL}, + {SR_CONF_EXTERNAL_CLOCK, SR_T_BOOL, "external_clock", + "External clock mode", NULL}, + {SR_CONF_SWAP, SR_T_BOOL, "swap", + "Swap channel order", NULL}, + {SR_CONF_CENTER_FREQUENCY, SR_T_UINT64, "center_frequency", + "Center frequency", NULL}, {SR_CONF_NUM_LOGIC_CHANNELS, SR_T_INT32, "logic_channels", "Number of logic channels", NULL}, {SR_CONF_NUM_ANALOG_CHANNELS, SR_T_INT32, "analog_channels", "Number of analog channels", NULL}, {SR_CONF_OUTPUT_VOLTAGE, SR_T_FLOAT, "output_voltage", "Current output voltage", NULL}, - {SR_CONF_OUTPUT_VOLTAGE_MAX, SR_T_FLOAT, "output_voltage_max", - "Maximum output voltage", NULL}, + {SR_CONF_OUTPUT_VOLTAGE_TARGET, SR_T_FLOAT, "output_voltage_target", + "Output voltage target", NULL}, {SR_CONF_OUTPUT_CURRENT, SR_T_FLOAT, "output_current", "Current output current", NULL}, - {SR_CONF_OUTPUT_CURRENT_MAX, SR_T_FLOAT, "output_current_max", - "Maximum output current", NULL}, + {SR_CONF_OUTPUT_CURRENT_LIMIT, SR_T_FLOAT, "output_current_limit", + "Output current limit", NULL}, {SR_CONF_OUTPUT_ENABLED, SR_T_BOOL, "output_enabled", "Output enabled", NULL}, {SR_CONF_OUTPUT_CHANNEL_CONFIG, SR_T_STRING, "output_channel_config", @@ -128,16 +149,57 @@ static struct sr_config_info sr_config_info_data[] = { "Over-current protection active", NULL}, {SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD, SR_T_FLOAT, "ocp_threshold", "Over-current protection threshold", NULL}, - {SR_CONF_LIMIT_SAMPLES, SR_T_UINT64, "limit_samples", - "Sample limit", NULL}, {SR_CONF_CLOCK_EDGE, SR_T_STRING, "clock_edge", "Clock edge", NULL}, {SR_CONF_AMPLITUDE, SR_T_FLOAT, "amplitude", "Amplitude", NULL}, - {SR_CONF_OVER_TEMPERATURE_PROTECTION, SR_T_BOOL, "otp", - "Over-temperature protection", NULL}, {SR_CONF_OUTPUT_REGULATION, SR_T_STRING, "output_regulation", "Output channel regulation", NULL}, + {SR_CONF_OVER_TEMPERATURE_PROTECTION, SR_T_BOOL, "otp", + "Over-temperature protection", NULL}, + {SR_CONF_OUTPUT_FREQUENCY, SR_T_UINT64, "output_frequency", + "Output frequency", NULL}, + {SR_CONF_MEASURED_QUANTITY, SR_T_STRING, "measured_quantity", + "Measured quantity", NULL}, + {SR_CONF_MEASURED_2ND_QUANTITY, SR_T_STRING, "measured_2nd_quantity", + "Measured secondary quantity", NULL}, + {SR_CONF_EQUIV_CIRCUIT_MODEL, SR_T_STRING, "equiv_circuit_model", + "Equivalent circuit model", NULL}, + + /* Special stuff */ + {SR_CONF_SCAN_OPTIONS, SR_T_STRING, "scan_options", + "Scan options", NULL}, + {SR_CONF_DEVICE_OPTIONS, SR_T_STRING, "device_options", + "Device options", NULL}, + {SR_CONF_SESSIONFILE, SR_T_STRING, "sessionfile", + "Session file", NULL}, + {SR_CONF_CAPTUREFILE, SR_T_STRING, "capturefile", + "Capture file", NULL}, + {SR_CONF_CAPTURE_UNITSIZE, SR_T_UINT64, "capture_unitsize", + "Capture unitsize", NULL}, + {SR_CONF_POWER_OFF, SR_T_BOOL, "power_off", + "Power off", NULL}, + {SR_CONF_DATA_SOURCE, SR_T_STRING, "data_source", + "Data source", NULL}, + {SR_CONF_PROBE_FACTOR, SR_T_UINT64, "probe_factor", + "Probe factor", NULL}, + + /* Acquisition modes, sample limiting */ + {SR_CONF_LIMIT_MSEC, SR_T_UINT64, "limit_time", + "Time limit", NULL}, + {SR_CONF_LIMIT_SAMPLES, SR_T_UINT64, "limit_samples", + "Sample limit", NULL}, + {SR_CONF_LIMIT_FRAMES, SR_T_UINT64, "limit_frames", + "Frame limit", NULL}, + {SR_CONF_CONTINUOUS, SR_T_UINT64, "continuous", + "Continuous sampling", NULL}, + {SR_CONF_DATALOG, SR_T_BOOL, "datalog", + "Datalog", NULL}, + {SR_CONF_DEVICE_MODE, SR_T_STRING, "device_mode", + "Device mode", NULL}, + {SR_CONF_TEST_MODE, SR_T_STRING, "test_mode", + "Test mode", NULL}, + {0, 0, NULL, NULL, NULL}, }; @@ -170,34 +232,45 @@ SR_PRIV int sr_variant_type_check(uint32_t key, GVariant *value) { const struct sr_config_info *info; const GVariantType *type, *expected; + char *expected_string, *type_string; + info = sr_config_info_get(key); if (!info) return SR_OK; + expected = sr_variant_type_get(info->datatype); type = g_variant_get_type(value); - if (!g_variant_type_equal(type, expected)) { - gchar *expected_string = g_variant_type_dup_string(expected); - gchar *type_string = g_variant_type_dup_string(type); + if (!g_variant_type_equal(type, expected) + && !g_variant_type_is_subtype_of(type, expected)) { + expected_string = g_variant_type_dup_string(expected); + type_string = g_variant_type_dup_string(type); sr_err("Wrong variant type for key '%s': expected '%s', got '%s'", info->name, expected_string, type_string); g_free(expected_string); g_free(type_string); return SR_ERR_ARG; } + return SR_OK; } /** * Return the list of supported hardware drivers. * - * @return Pointer to the NULL-terminated list of hardware driver pointers. + * @param[in] ctx Pointer to a libsigrok context struct. Must not be NULL. + * + * @retval NULL The ctx argument was NULL, or there are no supported drivers. + * @retval Other Pointer to the NULL-terminated list of hardware drivers. + * The user should NOT g_free() this list, sr_exit() will do that. * - * @since 0.1.0 + * @since 0.4.0 */ -SR_API struct sr_dev_driver **sr_driver_list(void) +SR_API struct sr_dev_driver **sr_driver_list(const struct sr_context *ctx) { + if (!ctx) + return NULL; - return drivers_list; + return ctx->driver_list; } /** @@ -234,12 +307,56 @@ SR_API int sr_driver_init(struct sr_context *ctx, struct sr_dev_driver *driver) } sr_spew("Initializing driver '%s'.", driver->name); - if ((ret = driver->init(ctx)) < 0) + if ((ret = driver->init(driver, ctx)) < 0) sr_err("Failed to initialize the driver: %d.", ret); return ret; } +static int check_options(struct sr_dev_driver *driver, GSList *options, + uint32_t optlist_key, struct sr_dev_inst *sdi, + struct sr_channel_group *cg) +{ + struct sr_config *src; + const struct sr_config_info *srci; + GVariant *gvar_opts; + GSList *l; + const uint32_t *opts; + gsize num_opts, i; + int ret; + + if (sr_config_list(driver, sdi, cg, optlist_key, &gvar_opts) != SR_OK) { + /* Driver publishes no options for this optlist. */ + return SR_ERR; + } + + ret = SR_OK; + opts = g_variant_get_fixed_array(gvar_opts, &num_opts, sizeof(uint32_t)); + for (l = options; l; l = l->next) { + src = l->data; + for (i = 0; i < num_opts; i++) { + if (opts[i] == src->key) + break; + } + if (i == num_opts) { + if (!(srci = sr_config_info_get(src->key))) + /* Shouldn't happen. */ + sr_err("Invalid option %d.", src->key); + else + sr_err("Invalid option '%s'.", srci->id); + ret = SR_ERR_ARG; + break; + } + if (sr_variant_type_check(src->key, src->data) != SR_OK) { + ret = SR_ERR_ARG; + break; + } + } + g_variant_unref(gvar_opts); + + return ret; +} + /** * Tell a hardware driver to scan for devices. * @@ -268,7 +385,6 @@ SR_API int sr_driver_init(struct sr_context *ctx, struct sr_dev_driver *driver) SR_API GSList *sr_driver_scan(struct sr_dev_driver *driver, GSList *options) { GSList *l; - struct sr_config *src; if (!driver) { sr_err("Invalid driver, can't scan for devices."); @@ -280,13 +396,12 @@ SR_API GSList *sr_driver_scan(struct sr_dev_driver *driver, GSList *options) return NULL; } - for (l = options; l; l = l->next) { - src = l->data; - if (sr_variant_type_check(src->key, src->data) != SR_OK) + if (options) { + if (check_options(driver, options, SR_CONF_SCAN_OPTIONS, NULL, NULL) != SR_OK) return NULL; } - l = driver->scan(options); + l = driver->scan(driver, options); sr_spew("Scan of '%s' found %d devices.", driver->name, g_slist_length(l)); @@ -294,17 +409,26 @@ SR_API GSList *sr_driver_scan(struct sr_dev_driver *driver, GSList *options) return l; } -/** Call driver cleanup function for all drivers. - * @private */ -SR_PRIV void sr_hw_cleanup_all(void) +/** + * Call driver cleanup function for all drivers. + * + * @param[in] ctx Pointer to a libsigrok context struct. Must not be NULL. + * + * @private + */ +SR_PRIV void sr_hw_cleanup_all(const struct sr_context *ctx) { int i; struct sr_dev_driver **drivers; - drivers = sr_driver_list(); + if (!ctx) + return; + + drivers = sr_driver_list(ctx); for (i = 0; drivers[i]; i++) { if (drivers[i]->cleanup) - drivers[i]->cleanup(); + drivers[i]->cleanup(drivers[i]); + drivers[i]->priv = NULL; } } @@ -316,8 +440,7 @@ SR_PRIV struct sr_config *sr_config_new(uint32_t key, GVariant *data) { struct sr_config *src; - if (!(src = g_try_malloc(sizeof(struct sr_config)))) - return NULL; + src = g_malloc0(sizeof(struct sr_config)); src->key = key; src->data = g_variant_ref_sink(data); @@ -340,6 +463,89 @@ SR_PRIV void sr_config_free(struct sr_config *src) } +static void log_key(const struct sr_dev_inst *sdi, + const struct sr_channel_group *cg, uint32_t key, int op, GVariant *data) +{ + const char *opstr; + const struct sr_config_info *srci; + + /* Don't log SR_CONF_DEVICE_OPTIONS, it's verbose and not too useful. */ + if (key == SR_CONF_DEVICE_OPTIONS) + return; + + opstr = op == SR_CONF_GET ? "get" : op == SR_CONF_SET ? "set" : "list"; + srci = sr_config_info_get(key); + + sr_spew("sr_config_%s(): key %d (%s) sdi %p cg %s -> %s", opstr, key, + srci ? srci->id : "NULL", sdi, cg ? cg->name : "NULL", + data ? g_variant_print(data, TRUE) : "NULL"); +} + +static int check_key(const struct sr_dev_driver *driver, + const struct sr_dev_inst *sdi, const struct sr_channel_group *cg, + uint32_t key, int op, GVariant *data) +{ + const struct sr_config_info *srci; + gsize num_opts, i; + GVariant *gvar_opts; + const uint32_t *opts; + uint32_t pub_opt; + char *suffix, *opstr; + + if (sdi && cg) + suffix = " for this device and channel group"; + else if (sdi) + suffix = " for this device"; + else + suffix = ""; + + if (!(srci = sr_config_info_get(key))) { + sr_err("Invalid key %d.", key); + return SR_ERR_ARG; + } + opstr = op == SR_CONF_GET ? "get" : op == SR_CONF_SET ? "set" : "list"; + + switch (key) { + case SR_CONF_LIMIT_MSEC: + case SR_CONF_LIMIT_SAMPLES: + case SR_CONF_SAMPLERATE: + /* Setting any of these to 0 is not useful. */ + if (op != SR_CONF_SET || !data) + break; + if (g_variant_get_uint64(data) == 0) { + sr_err("Cannot set '%s' to 0.", srci->id); + return SR_ERR_ARG; + } + break; + } + + if (sr_config_list(driver, sdi, cg, SR_CONF_DEVICE_OPTIONS, &gvar_opts) != SR_OK) { + /* Driver publishes no options. */ + sr_err("No options available%s.", srci->id, suffix); + return SR_ERR_ARG; + } + opts = g_variant_get_fixed_array(gvar_opts, &num_opts, sizeof(uint32_t)); + pub_opt = 0; + for (i = 0; i < num_opts; i++) { + if ((opts[i] & SR_CONF_MASK) == key) { + pub_opt = opts[i]; + break; + } + } + g_variant_unref(gvar_opts); + if (!pub_opt) { + sr_err("Option '%s' not available%s.", srci->id, suffix); + return SR_ERR_ARG; + } + + if (!(pub_opt & op)) { + sr_err("Option '%s' not available to %s%s.", srci->id, opstr, suffix); + return SR_ERR_ARG; + } + + return SR_OK; +} + /** * Query value of a configuration key at the given driver or device instance. * @@ -377,7 +583,11 @@ SR_API int sr_config_get(const struct sr_dev_driver *driver, if (!driver->config_get) return SR_ERR_ARG; + if (check_key(driver, sdi, cg, key, SR_CONF_GET, NULL) != SR_OK) + return SR_ERR_ARG; + if ((ret = driver->config_get(key, data, sdi, cg)) == SR_OK) { + log_key(sdi, cg, key, SR_CONF_GET, *data); /* Got a floating reference from the driver. Sink it here, * caller will need to unref when done with it. */ g_variant_ref_sink(*data); @@ -417,8 +627,12 @@ SR_API int sr_config_set(const struct sr_dev_inst *sdi, ret = SR_ERR; else if (!sdi->driver->config_set) ret = SR_ERR_ARG; - else if ((ret = sr_variant_type_check(key, data)) == SR_OK) + else if (check_key(sdi->driver, sdi, cg, key, SR_CONF_SET, data) != SR_OK) + return SR_ERR_ARG; + else if ((ret = sr_variant_type_check(key, data)) == SR_OK) { + log_key(sdi, cg, key, SR_CONF_SET, data); ret = sdi->driver->config_set(key, data, sdi, cg); + } g_variant_unref(data); @@ -479,11 +693,17 @@ SR_API int sr_config_list(const struct sr_dev_driver *driver, int ret; if (!driver || !data) - ret = SR_ERR; + return SR_ERR; else if (!driver->config_list) - ret = SR_ERR_ARG; - else if ((ret = driver->config_list(key, data, sdi, cg)) == SR_OK) + return SR_ERR_ARG; + else if (key != SR_CONF_SCAN_OPTIONS && key != SR_CONF_DEVICE_OPTIONS) { + if (check_key(driver, sdi, cg, key, SR_CONF_LIST, NULL) != SR_OK) + return SR_ERR_ARG; + } + if ((ret = driver->config_list(key, data, sdi, cg)) == SR_OK) { + log_key(sdi, cg, key, SR_CONF_LIST, *data); g_variant_ref_sink(*data); + } return ret; } @@ -525,6 +745,8 @@ SR_API const struct sr_config_info *sr_config_info_name_get(const char *optname) int i; for (i = 0; sr_config_info_data[i].key; i++) { + if (!sr_config_info_data[i].id) + continue; if (!strcmp(sr_config_info_data[i].id, optname)) return &sr_config_info_data[i]; }