X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=src%2Fhwdriver.c;h=c2ce6e6e2db6526b5e8f507abb186c4437075b3c;hb=db6fa867a4bec0a1dc0742281df3ab7bd76787d2;hp=914fc337cf79e3474b42d7d851e4e18660e00c7e;hpb=cff7d8d60d1ae12aa38bc97c41506cc9067fe980;p=libsigrok.git diff --git a/src/hwdriver.c b/src/hwdriver.c index 914fc337..c2ce6e6e 100644 --- a/src/hwdriver.c +++ b/src/hwdriver.c @@ -48,10 +48,36 @@ extern SR_PRIV struct sr_dev_driver *drivers_list[]; */ static struct sr_config_info sr_config_info_data[] = { + /* Device types */ + {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}, + + /* Sample limiting */ + {SR_CONF_LIMIT_SAMPLES, SR_T_UINT64, "limit_samples", + "Sample limit", NULL}, + {SR_CONF_LIMIT_MSEC, SR_T_UINT64, "limit_time", + "Time limit", NULL}, + {SR_CONF_LIMIT_FRAMES, SR_T_UINT64, "limit_frames", + "Frame limit", NULL}, + {SR_CONF_CONTINUOUS, SR_T_UINT64, "continuous", + "Continuous sampling", NULL}, + + /* Scan options */ {SR_CONF_CONN, SR_T_STRING, "conn", "Connection", NULL}, {SR_CONF_SERIALCOMM, SR_T_STRING, "serialcomm", "Serial communication", NULL}, + + /* Device/channel group options */ {SR_CONF_SAMPLERATE, SR_T_UINT64, "samplerate", "Sample rate", NULL}, {SR_CONF_CAPTURE_RATIO, SR_T_UINT64, "captureratio", @@ -76,8 +102,8 @@ 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", @@ -106,29 +132,98 @@ static struct sr_config_info sr_config_info_data[] = { "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, SR_T_STRING, "output_channel", + {SR_CONF_OUTPUT_CHANNEL_CONFIG, SR_T_STRING, "output_channel_config", "Output channel modes", NULL}, - {SR_CONF_OVER_VOLTAGE_PROTECTION, SR_T_BOOL, "ovp", - "Over-voltage protection", NULL}, - {SR_CONF_OVER_CURRENT_PROTECTION, SR_T_BOOL, "ocp", - "Over-current protection", NULL}, - {SR_CONF_LIMIT_SAMPLES, SR_T_UINT64, "limit_samples", - "Sample limit", NULL}, + {SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED, SR_T_BOOL, "ovp_enabled", + "Over-voltage protection enabled", NULL}, + {SR_CONF_OVER_VOLTAGE_PROTECTION_ACTIVE, SR_T_BOOL, "ovp_active", + "Over-voltage protection active", NULL}, + {SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD, SR_T_FLOAT, "ovp_threshold", + "Over-voltage protection threshold", NULL}, + {SR_CONF_OVER_CURRENT_PROTECTION_ENABLED, SR_T_BOOL, "ocp_enabled", + "Over-current protection enabled", NULL}, + {SR_CONF_OVER_CURRENT_PROTECTION_ACTIVE, SR_T_BOOL, "ocp_active", + "Over-current protection active", NULL}, + {SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD, SR_T_FLOAT, "ocp_threshold", + "Over-current protection threshold", 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_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}, {0, 0, NULL, NULL, NULL}, }; +SR_PRIV const GVariantType *sr_variant_type_get(int datatype) +{ + switch (datatype) { + case SR_T_INT32: + return G_VARIANT_TYPE_INT32; + case SR_T_UINT64: + return G_VARIANT_TYPE_UINT64; + case SR_T_STRING: + return G_VARIANT_TYPE_STRING; + case SR_T_BOOL: + return G_VARIANT_TYPE_BOOLEAN; + case SR_T_FLOAT: + return G_VARIANT_TYPE_DOUBLE; + case SR_T_RATIONAL_PERIOD: + case SR_T_RATIONAL_VOLT: + case SR_T_UINT64_RANGE: + case SR_T_DOUBLE_RANGE: + return G_VARIANT_TYPE_TUPLE; + case SR_T_KEYVALUE: + return G_VARIANT_TYPE_DICTIONARY; + default: + return NULL; + } +} + +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) + && !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. * @@ -182,6 +277,50 @@ SR_API int sr_driver_init(struct sr_context *ctx, struct sr_dev_driver *driver) 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. * @@ -221,6 +360,11 @@ SR_API GSList *sr_driver_scan(struct sr_dev_driver *driver, GSList *options) return NULL; } + if (options) { + if (check_options(driver, options, SR_CONF_SCAN_OPTIONS, NULL, NULL) != SR_OK) + return NULL; + } + l = driver->scan(options); sr_spew("Scan of '%s' found %d devices.", driver->name, @@ -247,7 +391,7 @@ SR_PRIV void sr_hw_cleanup_all(void) * A floating reference can be passed in for data. * @private */ -SR_PRIV struct sr_config *sr_config_new(int key, GVariant *data) +SR_PRIV struct sr_config *sr_config_new(uint32_t key, GVariant *data) { struct sr_config *src; @@ -275,6 +419,59 @@ SR_PRIV void sr_config_free(struct sr_config *src) } +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) +{ + 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"; + sr_spew("sr_config_%s(): key %d (%s) sdi %p cg %s", opstr, key, + srci->id, sdi, cg ? cg->name : "NULL"); + + 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. * @@ -302,7 +499,7 @@ SR_PRIV void sr_config_free(struct sr_config *src) SR_API int sr_config_get(const struct sr_dev_driver *driver, const struct sr_dev_inst *sdi, const struct sr_channel_group *cg, - int key, GVariant **data) + uint32_t key, GVariant **data) { int ret; @@ -312,6 +509,9 @@ 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) != SR_OK) + return SR_ERR_ARG; + if ((ret = driver->config_get(key, data, sdi, cg)) == SR_OK) { /* Got a floating reference from the driver. Sink it here, * caller will need to unref when done with it. */ @@ -342,7 +542,7 @@ SR_API int sr_config_get(const struct sr_dev_driver *driver, */ SR_API int sr_config_set(const struct sr_dev_inst *sdi, const struct sr_channel_group *cg, - int key, GVariant *data) + uint32_t key, GVariant *data) { int ret; @@ -352,7 +552,9 @@ 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 + else if (check_key(sdi->driver, sdi, cg, key, SR_CONF_SET) != SR_OK) + return SR_ERR_ARG; + else if ((ret = sr_variant_type_check(key, data)) == SR_OK) ret = sdi->driver->config_set(key, data, sdi, cg); g_variant_unref(data); @@ -409,7 +611,7 @@ SR_API int sr_config_commit(const struct sr_dev_inst *sdi) SR_API int sr_config_list(const struct sr_dev_driver *driver, const struct sr_dev_inst *sdi, const struct sr_channel_group *cg, - int key, GVariant **data) + uint32_t key, GVariant **data) { int ret; @@ -417,7 +619,11 @@ SR_API int sr_config_list(const struct sr_dev_driver *driver, ret = SR_ERR; else if (!driver->config_list) ret = SR_ERR_ARG; - else if ((ret = driver->config_list(key, data, sdi, cg)) == SR_OK) + else if (key != SR_CONF_SCAN_OPTIONS && key != SR_CONF_DEVICE_OPTIONS) { + if (check_key(driver, sdi, cg, key, SR_CONF_LIST) != SR_OK) + return SR_ERR_ARG; + } + if ((ret = driver->config_list(key, data, sdi, cg)) == SR_OK) g_variant_ref_sink(*data); return ret; @@ -433,7 +639,7 @@ SR_API int sr_config_list(const struct sr_dev_driver *driver, * * @since 0.2.0 */ -SR_API const struct sr_config_info *sr_config_info_get(int key) +SR_API const struct sr_config_info *sr_config_info_get(uint32_t key) { int i; @@ -460,6 +666,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]; }