From ad54a1a630e6887204bc57a95075f403fd44aa6c Mon Sep 17 00:00:00 2001 From: Bert Vermeulen Date: Thu, 26 Dec 2013 12:08:34 +0100 Subject: [PATCH] Add support for config keys of type G_VARIANT_TYPE_INT32. --- device.c | 12 ++--- session.c | 145 ++++++++++++++++++++++++++++----------------------- sigrok-cli.h | 1 + 3 files changed, 85 insertions(+), 73 deletions(-) diff --git a/device.c b/device.c index 28184ce..a7dfe62 100644 --- a/device.c +++ b/device.c @@ -28,24 +28,18 @@ extern gchar *opt_probe_group; /* Convert driver options hash to GSList of struct sr_config. */ static GSList *hash_to_hwopt(GHashTable *hash) { - const struct sr_config_info *srci; struct sr_config *src; GList *gl, *keys; GSList *opts; - char *key, *value; + char *key; keys = g_hash_table_get_keys(hash); opts = NULL; for (gl = keys; gl; gl = gl->next) { key = gl->data; - if (!(srci = sr_config_info_name_get(key))) { - g_critical("Unknown option %s", key); + src = g_malloc(sizeof(struct sr_config)); + if (opt_to_gvar(key, g_hash_table_lookup(hash, key), src) != 0) return NULL; - } - src = g_try_malloc(sizeof(struct sr_config)); - src->key = srci->key; - value = g_hash_table_lookup(hash, key); - src->data = g_variant_new_string(value); opts = g_slist_append(opts, src); } g_list_free(keys); diff --git a/session.c b/session.c index 130eed5..a8a647a 100644 --- a/session.c +++ b/session.c @@ -442,83 +442,100 @@ void datafeed_in(const struct sr_dev_inst *sdi, } -int set_dev_options(struct sr_dev_inst *sdi, GHashTable *args) +int opt_to_gvar(char *key, char *value, struct sr_config *src) { const struct sr_config_info *srci; - struct sr_probe_group *pg; - GHashTableIter iter; - gpointer key, value; - int ret; double tmp_double; uint64_t tmp_u64, p, q, low, high; + GVariant *rational[2], *range[2]; gboolean tmp_bool; - GVariant *val, *rational[2], *range[2]; + int ret; - g_hash_table_iter_init(&iter, args); - while (g_hash_table_iter_next(&iter, &key, &value)) { - if (!(srci = sr_config_info_name_get(key))) { - g_critical("Unknown device option '%s'.", (char *) key); - return SR_ERR; - } + if (!(srci = sr_config_info_name_get(key))) { + g_critical("Unknown device option '%s'.", (char *) key); + return -1; + } + src->key = srci->key; - if ((value == NULL) && - (srci->datatype != SR_T_BOOL)) { - g_critical("Option '%s' needs a value.", (char *)key); - return SR_ERR; - } - val = NULL; - switch (srci->datatype) { - case SR_T_UINT64: - ret = sr_parse_sizestring(value, &tmp_u64); - if (ret != SR_OK) - break; - val = g_variant_new_uint64(tmp_u64); - break; - case SR_T_CHAR: - val = g_variant_new_string(value); - break; - case SR_T_BOOL: - if (!value) - tmp_bool = TRUE; - else - tmp_bool = sr_parse_boolstring(value); - val = g_variant_new_boolean(tmp_bool); + if ((value == NULL) && + (srci->datatype != SR_T_BOOL)) { + g_critical("Option '%s' needs a value.", (char *)key); + return -1; + } + + ret = 0; + switch (srci->datatype) { + case SR_T_UINT64: + ret = sr_parse_sizestring(value, &tmp_u64); + if (ret != 0) break; - case SR_T_FLOAT: - tmp_double = strtof(value, NULL); - val = g_variant_new_double(tmp_double); + src->data = g_variant_new_uint64(tmp_u64); + break; + case SR_T_INT32: + ret = sr_parse_sizestring(value, &tmp_u64); + if (ret != 0) break; - case SR_T_RATIONAL_PERIOD: - if ((ret = sr_parse_period(value, &p, &q)) != SR_OK) - break; - rational[0] = g_variant_new_uint64(p); - rational[1] = g_variant_new_uint64(q); - val = g_variant_new_tuple(rational, 2); + src->data = g_variant_new_int32(tmp_u64); + break; + case SR_T_CHAR: + src->data = g_variant_new_string(value); + break; + case SR_T_BOOL: + if (!value) + tmp_bool = TRUE; + else + tmp_bool = sr_parse_boolstring(value); + src->data = g_variant_new_boolean(tmp_bool); + break; + case SR_T_FLOAT: + tmp_double = strtof(value, NULL); + src->data = g_variant_new_double(tmp_double); + break; + case SR_T_RATIONAL_PERIOD: + if ((ret = sr_parse_period(value, &p, &q)) != SR_OK) break; - case SR_T_RATIONAL_VOLT: - if ((ret = sr_parse_voltage(value, &p, &q)) != SR_OK) - break; - rational[0] = g_variant_new_uint64(p); - rational[1] = g_variant_new_uint64(q); - val = g_variant_new_tuple(rational, 2); + rational[0] = g_variant_new_uint64(p); + rational[1] = g_variant_new_uint64(q); + src->data = g_variant_new_tuple(rational, 2); + break; + case SR_T_RATIONAL_VOLT: + if ((ret = sr_parse_voltage(value, &p, &q)) != SR_OK) break; - case SR_T_UINT64_RANGE: - if (sscanf(value, "%"PRIu64"-%"PRIu64, &low, &high) != 2) { - ret = SR_ERR; - break; - } else { - range[0] = g_variant_new_uint64(low); - range[1] = g_variant_new_uint64(high); - val = g_variant_new_tuple(range, 2); - } + rational[0] = g_variant_new_uint64(p); + rational[1] = g_variant_new_uint64(q); + src->data = g_variant_new_tuple(rational, 2); + break; + case SR_T_UINT64_RANGE: + if (sscanf(value, "%"PRIu64"-%"PRIu64, &low, &high) != 2) { + ret = -1; break; - default: - ret = SR_ERR; - } - if (val) { - pg = select_probe_group(sdi); - ret = sr_config_set(sdi, pg, srci->key, val); + } else { + range[0] = g_variant_new_uint64(low); + range[1] = g_variant_new_uint64(high); + src->data = g_variant_new_tuple(range, 2); } + break; + default: + ret = -1; + } + + return ret; +} + +int set_dev_options(struct sr_dev_inst *sdi, GHashTable *args) +{ + struct sr_config src; + struct sr_probe_group *pg; + GHashTableIter iter; + gpointer key, value; + int ret; + + g_hash_table_iter_init(&iter, args); + while (g_hash_table_iter_next(&iter, &key, &value)) { + if ((ret = opt_to_gvar(key, value, &src)) != 0) + return ret; + pg = select_probe_group(sdi); + ret = sr_config_set(sdi, pg, src.key, src.data); if (ret != SR_OK) { g_critical("Failed to set device option '%s'.", (char *)key); return ret; diff --git a/sigrok-cli.h b/sigrok-cli.h index 27ae876..7a05fc4 100644 --- a/sigrok-cli.h +++ b/sigrok-cli.h @@ -46,6 +46,7 @@ struct sr_probe_group *select_probe_group(struct sr_dev_inst *sdi); int setup_output_format(void); void datafeed_in(const struct sr_dev_inst *sdi, const struct sr_datafeed_packet *packet, void *cb_data); +int opt_to_gvar(char *key, char *value, struct sr_config *src); int set_dev_options(struct sr_dev_inst *sdi, GHashTable *args); void run_session(void); -- 2.30.2