From 2fb60e23299037456a626ad65d15b89a78393917 Mon Sep 17 00:00:00 2001 From: Bert Vermeulen Date: Mon, 17 Aug 2015 01:30:14 +0200 Subject: [PATCH] Replace sr_config_info with sr_key_info. The tables defined with this struct can now be used for information on items other than config keys. Functions to access these tables have been renamed sr_key_info_[name_]get. These take an extra argument, keytype, which should be set to SR_KEY_CONFIG to get the config key tables. Other key types will be added. --- bindings/cxx/ConfigKey_methods.cpp | 8 ++-- include/libsigrok/libsigrok.h | 16 ++++--- include/libsigrok/proto.h | 4 +- src/hwdriver.c | 76 ++++++++++++++++++++---------- 4 files changed, 67 insertions(+), 37 deletions(-) diff --git a/bindings/cxx/ConfigKey_methods.cpp b/bindings/cxx/ConfigKey_methods.cpp index 9ba82adb..06bdc121 100644 --- a/bindings/cxx/ConfigKey_methods.cpp +++ b/bindings/cxx/ConfigKey_methods.cpp @@ -1,6 +1,6 @@ const DataType *ConfigKey::data_type() const { - const struct sr_config_info *info = sr_config_info_get(_id); + const struct sr_key_info *info = sr_key_info_get(SR_KEY_CONFIG, _id); if (!info) throw Error(SR_ERR_NA); return DataType::get(info->datatype); @@ -8,7 +8,7 @@ const DataType *ConfigKey::data_type() const string ConfigKey::identifier() const { - const struct sr_config_info *info = sr_config_info_get(_id); + const struct sr_key_info *info = sr_key_info_get(SR_KEY_CONFIG, _id); if (!info) throw Error(SR_ERR_NA); return valid_string(info->id); @@ -16,7 +16,7 @@ string ConfigKey::identifier() const string ConfigKey::description() const { - const struct sr_config_info *info = sr_config_info_get(_id); + const struct sr_key_info *info = sr_key_info_get(SR_KEY_CONFIG, _id); if (!info) throw Error(SR_ERR_NA); return valid_string(info->name); @@ -24,7 +24,7 @@ string ConfigKey::description() const const ConfigKey *ConfigKey::get_by_identifier(string identifier) { - const struct sr_config_info *info = sr_config_info_name_get(identifier.c_str()); + const struct sr_key_info *info = sr_key_info_name_get(SR_KEY_CONFIG, identifier.c_str()); if (!info) throw Error(SR_ERR_ARG); return get(info->key); diff --git a/include/libsigrok/libsigrok.h b/include/libsigrok/libsigrok.h index 0e35957f..ccb8602f 100644 --- a/include/libsigrok/libsigrok.h +++ b/include/libsigrok/libsigrok.h @@ -574,15 +574,19 @@ struct sr_config { GVariant *data; }; -/** Information about a config key. */ -struct sr_config_info { - /** Config key like SR_CONF_CONN, etc. */ +enum sr_keytype { + SR_KEY_CONFIG, +}; + +/** Information about a key. */ +struct sr_key_info { + /** Config key like SR_CONF_CONN, MQ value like SR_MQ_VOLTAGE, etc. */ uint32_t key; - /** Data type like SR_T_STRING, etc. */ + /** Data type like SR_T_STRING, etc if applicable. */ int datatype; - /** Id string, e.g. "serialcomm". */ + /** Short, lowercase ID string, e.g. "serialcomm", "voltage". */ char *id; - /** Name, e.g. "Serial communication". */ + /** Full capitalized name, e.g. "Serial communication". */ char *name; /** Verbose description (unused currently). */ char *description; diff --git a/include/libsigrok/proto.h b/include/libsigrok/proto.h index 1aa2ee76..7396c57b 100644 --- a/include/libsigrok/proto.h +++ b/include/libsigrok/proto.h @@ -95,8 +95,8 @@ SR_API int sr_config_list(const struct sr_dev_driver *driver, const struct sr_dev_inst *sdi, const struct sr_channel_group *cg, uint32_t key, GVariant **data); -SR_API const struct sr_config_info *sr_config_info_get(uint32_t key); -SR_API const struct sr_config_info *sr_config_info_name_get(const char *optname); +SR_API const struct sr_key_info *sr_key_info_get(int keytype, uint32_t key); +SR_API const struct sr_key_info *sr_key_info_name_get(int keytype, const char *keyid); /*--- session.c -------------------------------------------------------------*/ diff --git a/src/hwdriver.c b/src/hwdriver.c index 3348b759..8f4bd9ec 100644 --- a/src/hwdriver.c +++ b/src/hwdriver.c @@ -46,7 +46,7 @@ */ /* Same key order/grouping as in enum sr_configkey (libsigrok.h). */ -static struct sr_config_info sr_config_info_data[] = { +static struct sr_key_info sr_key_info_config[] = { /* Device classes */ {SR_CONF_LOGIC_ANALYZER, SR_T_STRING, NULL, "Logic analyzer", NULL}, {SR_CONF_OSCILLOSCOPE, SR_T_STRING, NULL, "Oscilloscope", NULL}, @@ -236,11 +236,11 @@ SR_PRIV const GVariantType *sr_variant_type_get(int datatype) SR_PRIV int sr_variant_type_check(uint32_t key, GVariant *value) { - const struct sr_config_info *info; + const struct sr_key_info *info; const GVariantType *type, *expected; char *expected_string, *type_string; - info = sr_config_info_get(key); + info = sr_key_info_get(SR_KEY_CONFIG, key); if (!info) return SR_OK; @@ -324,7 +324,7 @@ static int check_options(struct sr_dev_driver *driver, GSList *options, struct sr_channel_group *cg) { struct sr_config *src; - const struct sr_config_info *srci; + const struct sr_key_info *srci; GVariant *gvar_opts; GSList *l; const uint32_t *opts; @@ -345,7 +345,7 @@ static int check_options(struct sr_dev_driver *driver, GSList *options, break; } if (i == num_opts) { - if (!(srci = sr_config_info_get(src->key))) + if (!(srci = sr_key_info_get(SR_KEY_CONFIG, src->key))) /* Shouldn't happen. */ sr_err("Invalid option %d.", src->key); else @@ -473,14 +473,14 @@ 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; + const struct sr_key_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); + srci = sr_key_info_get(SR_KEY_CONFIG, 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", @@ -491,7 +491,7 @@ 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; + const struct sr_key_info *srci; gsize num_opts, i; GVariant *gvar_opts; const uint32_t *opts; @@ -505,7 +505,7 @@ static int check_key(const struct sr_dev_driver *driver, else suffix = ""; - if (!(srci = sr_config_info_get(key))) { + if (!(srci = sr_key_info_get(SR_KEY_CONFIG, key))) { sr_err("Invalid key %d.", key); return SR_ERR_ARG; } @@ -714,47 +714,73 @@ SR_API int sr_config_list(const struct sr_dev_driver *driver, return ret; } +static struct sr_key_info *get_keytable(int keytype) +{ + struct sr_key_info *table; + + switch (keytype) { + case SR_KEY_CONFIG: + table = sr_key_info_config; + break; + default: + sr_err("Invalid keytype %d", keytype); + return NULL; + } + + return table; +} + /** - * Get information about a configuration key, by key. + * Get information about a key, by key. * - * @param[in] key The configuration key. + * @param[in] keytype The namespace the key is in. + * @param[in] key The key to find. * - * @return A pointer to a struct sr_config_info, or NULL if the key + * @return A pointer to a struct sr_key_info, or NULL if the key * was not found. * - * @since 0.2.0 + * @since 0.3.0 */ -SR_API const struct sr_config_info *sr_config_info_get(uint32_t key) +SR_API const struct sr_key_info *sr_key_info_get(int keytype, uint32_t key) { + struct sr_key_info *table; int i; - for (i = 0; sr_config_info_data[i].key; i++) { - if (sr_config_info_data[i].key == key) - return &sr_config_info_data[i]; + if (!(table = get_keytable(keytype))) + return NULL; + + for (i = 0; table[i].key; i++) { + if (table[i].key == key) + return &table[i]; } return NULL; } /** - * Get information about a configuration key, by name. + * Get information about a key, by name. * - * @param[in] optname The configuration key. + * @param[in] keytype The namespace the key is in. + * @param[in] keyid The key id string. * - * @return A pointer to a struct sr_config_info, or NULL if the key + * @return A pointer to a struct sr_key_info, or NULL if the key * was not found. * * @since 0.2.0 */ -SR_API const struct sr_config_info *sr_config_info_name_get(const char *optname) +SR_API const struct sr_key_info *sr_key_info_name_get(int keytype, const char *keyid) { + struct sr_key_info *table; int i; - for (i = 0; sr_config_info_data[i].key; i++) { - if (!sr_config_info_data[i].id) + if (!(table = get_keytable(keytype))) + return NULL; + + for (i = 0; table[i].key; i++) { + if (!table[i].id) continue; - if (!strcmp(sr_config_info_data[i].id, optname)) - return &sr_config_info_data[i]; + if (!strcmp(table[i].id, keyid)) + return &table[i]; } return NULL;