Add support for config keys of type G_VARIANT_TYPE_INT32.
authorBert Vermeulen <bert@biot.com>
Thu, 26 Dec 2013 11:08:34 +0000 (12:08 +0100)
committerBert Vermeulen <bert@biot.com>
Sun, 29 Dec 2013 11:51:47 +0000 (12:51 +0100)
device.c
session.c
sigrok-cli.h

index 28184ce1e5451e732b410fa1ffc9eaefd1a0c456..a7dfe62d57166a08eb40aae5fc81e8f43f6d5eba 100644 (file)
--- 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);
index 130eed5a751cb9fe16f1e4f0d737fbec829fef2c..a8a647a35079c9a0ad5223319df77b24377523e9 100644 (file)
--- 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;
index 27ae8762663ce879122bf4567cae0f6f8a1b100a..7a05fc48302a4d5adbd75c48523843deba41a226 100644 (file)
@@ -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);