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.
This commit is contained in:
Bert Vermeulen 2015-08-17 01:30:14 +02:00 committed by Uwe Hermann
parent 0b2b92f6c3
commit 2fb60e2329
4 changed files with 67 additions and 37 deletions

View File

@ -1,6 +1,6 @@
const DataType *ConfigKey::data_type() const 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) if (!info)
throw Error(SR_ERR_NA); throw Error(SR_ERR_NA);
return DataType::get(info->datatype); return DataType::get(info->datatype);
@ -8,7 +8,7 @@ const DataType *ConfigKey::data_type() const
string ConfigKey::identifier() 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) if (!info)
throw Error(SR_ERR_NA); throw Error(SR_ERR_NA);
return valid_string(info->id); return valid_string(info->id);
@ -16,7 +16,7 @@ string ConfigKey::identifier() const
string ConfigKey::description() 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) if (!info)
throw Error(SR_ERR_NA); throw Error(SR_ERR_NA);
return valid_string(info->name); return valid_string(info->name);
@ -24,7 +24,7 @@ string ConfigKey::description() const
const ConfigKey *ConfigKey::get_by_identifier(string identifier) 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) if (!info)
throw Error(SR_ERR_ARG); throw Error(SR_ERR_ARG);
return get(info->key); return get(info->key);

View File

@ -574,15 +574,19 @@ struct sr_config {
GVariant *data; GVariant *data;
}; };
/** Information about a config key. */ enum sr_keytype {
struct sr_config_info { SR_KEY_CONFIG,
/** Config key like SR_CONF_CONN, etc. */ };
/** Information about a key. */
struct sr_key_info {
/** Config key like SR_CONF_CONN, MQ value like SR_MQ_VOLTAGE, etc. */
uint32_t key; uint32_t key;
/** Data type like SR_T_STRING, etc. */ /** Data type like SR_T_STRING, etc if applicable. */
int datatype; int datatype;
/** Id string, e.g. "serialcomm". */ /** Short, lowercase ID string, e.g. "serialcomm", "voltage". */
char *id; char *id;
/** Name, e.g. "Serial communication". */ /** Full capitalized name, e.g. "Serial communication". */
char *name; char *name;
/** Verbose description (unused currently). */ /** Verbose description (unused currently). */
char *description; char *description;

View File

@ -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_dev_inst *sdi,
const struct sr_channel_group *cg, const struct sr_channel_group *cg,
uint32_t key, GVariant **data); uint32_t key, GVariant **data);
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);
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);
/*--- session.c -------------------------------------------------------------*/ /*--- session.c -------------------------------------------------------------*/

View File

@ -46,7 +46,7 @@
*/ */
/* Same key order/grouping as in enum sr_configkey (libsigrok.h). */ /* 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 */ /* Device classes */
{SR_CONF_LOGIC_ANALYZER, SR_T_STRING, NULL, "Logic analyzer", NULL}, {SR_CONF_LOGIC_ANALYZER, SR_T_STRING, NULL, "Logic analyzer", NULL},
{SR_CONF_OSCILLOSCOPE, SR_T_STRING, NULL, "Oscilloscope", 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) 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; const GVariantType *type, *expected;
char *expected_string, *type_string; char *expected_string, *type_string;
info = sr_config_info_get(key); info = sr_key_info_get(SR_KEY_CONFIG, key);
if (!info) if (!info)
return SR_OK; 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_channel_group *cg)
{ {
struct sr_config *src; struct sr_config *src;
const struct sr_config_info *srci; const struct sr_key_info *srci;
GVariant *gvar_opts; GVariant *gvar_opts;
GSList *l; GSList *l;
const uint32_t *opts; const uint32_t *opts;
@ -345,7 +345,7 @@ static int check_options(struct sr_dev_driver *driver, GSList *options,
break; break;
} }
if (i == num_opts) { 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. */ /* Shouldn't happen. */
sr_err("Invalid option %d.", src->key); sr_err("Invalid option %d.", src->key);
else 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 struct sr_channel_group *cg, uint32_t key, int op, GVariant *data)
{ {
const char *opstr; 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. */ /* Don't log SR_CONF_DEVICE_OPTIONS, it's verbose and not too useful. */
if (key == SR_CONF_DEVICE_OPTIONS) if (key == SR_CONF_DEVICE_OPTIONS)
return; return;
opstr = op == SR_CONF_GET ? "get" : op == SR_CONF_SET ? "set" : "list"; 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, sr_spew("sr_config_%s(): key %d (%s) sdi %p cg %s -> %s", opstr, key,
srci ? srci->id : "NULL", sdi, cg ? cg->name : "NULL", 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, const struct sr_dev_inst *sdi, const struct sr_channel_group *cg,
uint32_t key, int op, GVariant *data) uint32_t key, int op, GVariant *data)
{ {
const struct sr_config_info *srci; const struct sr_key_info *srci;
gsize num_opts, i; gsize num_opts, i;
GVariant *gvar_opts; GVariant *gvar_opts;
const uint32_t *opts; const uint32_t *opts;
@ -505,7 +505,7 @@ static int check_key(const struct sr_dev_driver *driver,
else else
suffix = ""; suffix = "";
if (!(srci = sr_config_info_get(key))) { if (!(srci = sr_key_info_get(SR_KEY_CONFIG, key))) {
sr_err("Invalid key %d.", key); sr_err("Invalid key %d.", key);
return SR_ERR_ARG; return SR_ERR_ARG;
} }
@ -714,47 +714,73 @@ SR_API int sr_config_list(const struct sr_dev_driver *driver,
return ret; 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. * 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; int i;
for (i = 0; sr_config_info_data[i].key; i++) { if (!(table = get_keytable(keytype)))
if (sr_config_info_data[i].key == key) return NULL;
return &sr_config_info_data[i];
for (i = 0; table[i].key; i++) {
if (table[i].key == key)
return &table[i];
} }
return NULL; 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. * was not found.
* *
* @since 0.2.0 * @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; int i;
for (i = 0; sr_config_info_data[i].key; i++) { if (!(table = get_keytable(keytype)))
if (!sr_config_info_data[i].id) return NULL;
for (i = 0; table[i].key; i++) {
if (!table[i].id)
continue; continue;
if (!strcmp(sr_config_info_data[i].id, optname)) if (!strcmp(table[i].id, keyid))
return &sr_config_info_data[i]; return &table[i];
} }
return NULL; return NULL;