]> sigrok.org Git - sigrok-cli.git/commitdiff
Always check config key capabilities before using them.
authorBert Vermeulen <redacted>
Tue, 18 Nov 2014 15:24:52 +0000 (16:24 +0100)
committerBert Vermeulen <redacted>
Tue, 18 Nov 2014 15:24:52 +0000 (16:24 +0100)
main.c
parsers.c
session.c
show.c
sigrok-cli.h

diff --git a/main.c b/main.c
index 81dc4ad21055a11bfc6f199d1dff732e388eb620..1b17c526e1a31fca5546a083b7a0e052a8fb3a4b 100644 (file)
--- a/main.c
+++ b/main.c
@@ -73,6 +73,61 @@ int select_channels(struct sr_dev_inst *sdi)
        return SR_OK;
 }
 
+gboolean config_key_has_cap(struct sr_dev_driver *driver,
+               const struct sr_dev_inst *sdi, struct sr_channel_group *cg,
+               uint32_t key, uint32_t capability)
+{
+       GVariant *gvar_opts;
+       const uint32_t *opts;
+       gsize num_opts, i;
+
+       if (sr_config_list(driver, sdi, cg, SR_CONF_DEVICE_OPTIONS,
+                       &gvar_opts) != SR_OK)
+               return FALSE;
+
+       opts = g_variant_get_fixed_array(gvar_opts, &num_opts, sizeof(uint32_t));
+       for (i = 0; i < num_opts; i++) {
+               if ((opts[i] & SR_CONF_MASK) == key) {
+                       if ((opts[i] & capability) == capability)
+                               return TRUE;
+                       else
+                               return FALSE;
+               }
+       }
+
+       return FALSE;
+}
+
+int maybe_config_get(struct sr_dev_driver *driver,
+               const struct sr_dev_inst *sdi, struct sr_channel_group *cg,
+               uint32_t key, GVariant **gvar)
+{
+       if (config_key_has_cap(driver, sdi, cg, key, SR_CONF_GET))
+               return sr_config_get(driver, sdi, cg, key, gvar);
+
+       return SR_ERR_NA;
+}
+
+int maybe_config_set(struct sr_dev_driver *driver,
+               const struct sr_dev_inst *sdi, struct sr_channel_group *cg,
+               uint32_t key, GVariant *gvar)
+{
+       if (config_key_has_cap(driver, sdi, cg, key, SR_CONF_SET))
+               return sr_config_set(sdi, cg, key, gvar);
+
+       return SR_ERR_NA;
+}
+
+int maybe_config_list(struct sr_dev_driver *driver,
+               const struct sr_dev_inst *sdi, struct sr_channel_group *cg,
+               uint32_t key, GVariant **gvar)
+{
+       if (config_key_has_cap(driver, sdi, cg, key, SR_CONF_LIST))
+               return sr_config_list(driver, sdi, cg, key, gvar);
+
+       return SR_ERR_NA;
+}
+
 static void get_option(void)
 {
        struct sr_dev_inst *sdi;
@@ -107,7 +162,7 @@ static void get_option(void)
                set_dev_options(sdi, devargs);
        else devargs = NULL;
 
-       if ((ret = sr_config_get(driver, sdi, cg, ci->key, &gvar)) != SR_OK)
+       if ((ret = maybe_config_get(driver, sdi, cg, ci->key, &gvar)) != SR_OK)
                g_critical("Failed to get '%s': %s", opt_get, sr_strerror(ret));
        s = g_variant_print(gvar, FALSE);
        printf("%s\n", s);
index ee992712bd126fb261fea9991e9e529dba260fb0..35545e02e7d59d3080fc36b98486d02491fd08a2 100644 (file)
--- a/parsers.c
+++ b/parsers.c
@@ -200,7 +200,7 @@ int parse_triggerstring(const struct sr_dev_inst *sdi, const char *s,
        driver = sr_dev_inst_driver_get(sdi);
        channels = sr_dev_inst_channels_get(sdi);
 
-       if (sr_config_list(driver, sdi, NULL, SR_CONF_TRIGGER_MATCH,
+       if (maybe_config_list(driver, sdi, NULL, SR_CONF_TRIGGER_MATCH,
                        &gvar) != SR_OK) {
                g_critical("Device doesn't support any triggers.");
                return FALSE;
index fb9ebb6d4def3d4a384b8847f60b9bcf510408f9..0011445df9ebd2dd2281cb2610b21b9cbeca79e3 100644 (file)
--- a/session.c
+++ b/session.c
@@ -45,13 +45,14 @@ static int set_limit_time(const struct sr_dev_inst *sdi)
                return SR_ERR;
        }
 
-       if (sr_dev_has_option(sdi, SR_CONF_LIMIT_MSEC)) {
+       if (config_key_has_cap(driver, sdi, NULL, SR_CONF_LIMIT_MSEC, SR_CONF_SET)) {
                gvar = g_variant_new_uint64(time_msec);
                if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_MSEC, gvar) != SR_OK) {
                        g_critical("Failed to configure time limit.");
                        return SR_ERR;
                }
-       } else if (sr_dev_has_option(sdi, SR_CONF_SAMPLERATE)) {
+       } else if (config_key_has_cap(driver, sdi, NULL, SR_CONF_SAMPLERATE,
+                       SR_CONF_GET | SR_CONF_SET)) {
                /* Convert to samples based on the samplerate.  */
                sr_config_get(driver, sdi, NULL, SR_CONF_SAMPLERATE, &gvar);
                samplerate = g_variant_get_uint64(gvar);
@@ -159,7 +160,7 @@ void datafeed_in(const struct sr_dev_inst *sdi,
 
                rcvd_samples_logic = rcvd_samples_analog = 0;
 
-               if (sr_config_get(driver, sdi, NULL, SR_CONF_SAMPLERATE,
+               if (maybe_config_get(driver, sdi, NULL, SR_CONF_SAMPLERATE,
                                &gvar) == SR_OK) {
                        samplerate = g_variant_get_uint64(gvar);
                        g_variant_unref(gvar);
@@ -422,8 +423,8 @@ int set_dev_options(struct sr_dev_inst *sdi, GHashTable *args)
                if ((ret = opt_to_gvar(key, value, &src)) != 0)
                        return ret;
                cg = select_channel_group(sdi);
-               ret = sr_config_set(sdi, cg, src.key, src.data);
-               if (ret != SR_OK) {
+               if ((ret = maybe_config_set(sr_dev_inst_driver_get(sdi), sdi, cg,
+                               src.key, src.data)) != SR_OK) {
                        g_critical("Failed to set device option '%s'.", (char *)key);
                        return ret;
                }
@@ -459,7 +460,7 @@ void run_session(void)
                driver = sr_dev_inst_driver_get(sdi);
 
                if (sr_config_list(driver, sdi, NULL, SR_CONF_DEVICE_OPTIONS, &gvar) != SR_OK) {
-                       g_critical("Failed to query sr_config_list(SR_CONF_DEVICE_OPTIONS).");
+                       g_critical("Failed to query list device options.");
                        return;
                }
 
@@ -553,8 +554,8 @@ void run_session(void)
                        sr_session_destroy(session);
                        return;
                }
-               if (sr_config_list(driver, sdi, NULL,
-                               SR_CONF_LIMIT_SAMPLES, &gvar) == SR_OK) {
+               if (maybe_config_list(driver, sdi, NULL, SR_CONF_LIMIT_SAMPLES,
+                               &gvar) == SR_OK) {
                        /* The device has no compression, or compression is turned
                         * off, and publishes its sample memory size. */
                        g_variant_get(gvar, "(tt)", &min_samples, &max_samples);
@@ -569,7 +570,7 @@ void run_session(void)
                        }
                }
                gvar = g_variant_new_uint64(limit_samples);
-               if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_SAMPLES, gvar) != SR_OK) {
+               if (maybe_config_set(sr_dev_inst_driver_get(sdi), sdi, NULL, SR_CONF_LIMIT_SAMPLES, gvar) != SR_OK) {
                        g_critical("Failed to configure sample limit.");
                        sr_session_destroy(session);
                        return;
@@ -583,7 +584,7 @@ void run_session(void)
                        return;
                }
                gvar = g_variant_new_uint64(limit_frames);
-               if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_FRAMES, gvar) != SR_OK) {
+               if (maybe_config_set(sr_dev_inst_driver_get(sdi), sdi, NULL, SR_CONF_LIMIT_FRAMES, gvar) != SR_OK) {
                        g_critical("Failed to configure frame limit.");
                        sr_session_destroy(session);
                        return;
diff --git a/show.c b/show.c
index ca6cb1188444a213126f0f7bd83345a742c31d86..8ee99da108141c90574d32c491ca096b3be4cd25 100644 (file)
--- a/show.c
+++ b/show.c
@@ -135,33 +135,6 @@ static gint sort_channels(gconstpointer a, gconstpointer b)
        return pa->index - pb->index;
 }
 
-static gboolean config_key_has_cap(const struct sr_dev_inst *sdi,
-          struct sr_channel_group *cg, uint32_t key, uint32_t capability)
-{
-       struct sr_dev_driver *driver;
-       GVariant *gvar_opts;
-       const uint32_t *opts;
-       gsize num_opts, i;
-
-       driver = sr_dev_inst_driver_get(sdi);
-       if (sr_config_list(driver, sdi, cg, SR_CONF_DEVICE_OPTIONS,
-                       &gvar_opts) != SR_OK)
-               return FALSE;
-
-       opts = g_variant_get_fixed_array(gvar_opts, &num_opts, sizeof(uint32_t));
-       for (i = 0; i < num_opts; i++) {
-               if ((opts[i] & SR_CONF_MASK) == key) {
-                       if (opts[i] & capability)
-                               return TRUE;
-                       else
-                               return FALSE;
-               }
-       }
-
-       return FALSE;
-}
-
-
 static void print_dev_line(const struct sr_dev_inst *sdi)
 {
        struct sr_channel *ch;
@@ -179,8 +152,7 @@ static void print_dev_line(const struct sr_dev_inst *sdi)
 
        s = g_string_sized_new(128);
        g_string_assign(s, driver->name);
-       if (config_key_has_cap(sdi, NULL, SR_CONF_CONN, SR_CONF_GET)
-                       && sr_config_get(driver, sdi, NULL, SR_CONF_CONN, &gvar) == SR_OK) {
+       if (maybe_config_get(driver, sdi, NULL, SR_CONF_CONN, &gvar) == SR_OK) {
                g_string_append(s, ":conn=");
                g_string_append(s, g_variant_get_string(gvar, NULL));
                g_variant_unref(gvar);
@@ -355,7 +327,7 @@ void show_dev_detail(void)
                        continue;
 
                if (key == SR_CONF_TRIGGER_MATCH) {
-                       if (sr_config_list(driver, sdi, channel_group, key,
+                       if (maybe_config_list(driver, sdi, channel_group, key,
                                        &gvar_list) != SR_OK) {
                                printf("\n");
                                continue;
@@ -396,7 +368,8 @@ void show_dev_detail(void)
                        printf("\n");
                        g_variant_unref(gvar_list);
 
-               } else if (key == SR_CONF_LIMIT_SAMPLES) {
+               } else if (key == SR_CONF_LIMIT_SAMPLES
+                               && config_key_has_cap(driver, sdi, cg, key, SR_CONF_LIST)) {
                        /* If implemented in config_list(), this denotes the
                         * maximum number of samples a device can send. This
                         * really applies only to logic analyzers, and then
@@ -404,17 +377,16 @@ void show_dev_detail(void)
                         * have it turned off by default. The values returned
                         * are the low/high limits. */
                        if (sr_config_list(driver, sdi, channel_group, key,
-                                       &gvar) != SR_OK) {
-                               continue;
+                                       &gvar) == SR_OK) {
+                               g_variant_get(gvar, "(tt)", &low, &high);
+                               g_variant_unref(gvar);
+                               printf("    Maximum number of samples: %"PRIu64"\n", high);
                        }
-                       g_variant_get(gvar, "(tt)", &low, &high);
-                       g_variant_unref(gvar);
-                       printf("    Maximum number of samples: %"PRIu64"\n", high);
 
                } else if (key == SR_CONF_SAMPLERATE) {
                        /* Supported samplerates */
                        printf("    %s", srci->id);
-                       if (sr_config_list(driver, sdi, channel_group, SR_CONF_SAMPLERATE,
+                       if (maybe_config_list(driver, sdi, channel_group, SR_CONF_SAMPLERATE,
                                        &gvar_dict) != SR_OK) {
                                printf("\n");
                                continue;
@@ -455,16 +427,16 @@ void show_dev_detail(void)
                        g_variant_unref(gvar_dict);
 
                } else if (srci->datatype == SR_T_UINT64) {
-                       printf("    %s", srci->id);
+                       printf("    %s", srci->id);
                        gvar = NULL;
-                       if (sr_config_get(driver, sdi, channel_group, key,
+                       if (maybe_config_get(driver, sdi, channel_group, key,
                                        &gvar) == SR_OK) {
                                tmp_uint64 = g_variant_get_uint64(gvar);
                                g_variant_unref(gvar);
                        } else
                                tmp_uint64 = 0;
-                       if (sr_config_list(driver, sdi, channel_group,
-                                       SR_CONF_BUFFERSIZE, &gvar_list) != SR_OK) {
+                       if (maybe_config_list(driver, sdi, channel_group,
+                                       key, &gvar_list) != SR_OK) {
                                if (gvar) {
                                        /* Can't list it, but we have a value to show. */
                                        printf("%"PRIu64" (current)", tmp_uint64);
@@ -485,14 +457,14 @@ void show_dev_detail(void)
 
                } else if (srci->datatype == SR_T_STRING) {
                        printf("    %s: ", srci->id);
-                       if (sr_config_get(driver, sdi, channel_group, key,
+                       if (maybe_config_get(driver, sdi, channel_group, key,
                                        &gvar) == SR_OK) {
                                tmp_str = g_strdup(g_variant_get_string(gvar, NULL));
                                g_variant_unref(gvar);
                        } else
                                tmp_str = NULL;
 
-                       if (sr_config_list(driver, sdi, channel_group, key,
+                       if (maybe_config_list(driver, sdi, channel_group, key,
                                        &gvar) != SR_OK) {
                                if (tmp_str) {
                                        /* Can't list it, but we have a value to show. */
@@ -518,13 +490,13 @@ void show_dev_detail(void)
 
                } else if (srci->datatype == SR_T_UINT64_RANGE) {
                        printf("    %s: ", srci->id);
-                       if (sr_config_list(driver, sdi, channel_group, key,
+                       if (maybe_config_list(driver, sdi, channel_group, key,
                                        &gvar_list) != SR_OK) {
                                printf("\n");
                                continue;
                        }
 
-                       if (sr_config_get(driver, sdi, channel_group, key, &gvar) == SR_OK) {
+                       if (maybe_config_get(driver, sdi, channel_group, key, &gvar) == SR_OK) {
                                g_variant_get(gvar, "(tt)", &cur_low, &cur_high);
                                g_variant_unref(gvar);
                        } else {
@@ -548,7 +520,7 @@ void show_dev_detail(void)
 
                } else if (srci->datatype == SR_T_BOOL) {
                        printf("    %s: ", srci->id);
-                       if (sr_config_get(driver, sdi, channel_group, key,
+                       if (maybe_config_get(driver, sdi, channel_group, key,
                                        &gvar) == SR_OK) {
                                if (g_variant_get_boolean(gvar))
                                        printf("on (current), off\n");
@@ -560,13 +532,13 @@ void show_dev_detail(void)
 
                } else if (srci->datatype == SR_T_DOUBLE_RANGE) {
                        printf("    %s: ", srci->id);
-                       if (sr_config_list(driver, sdi, channel_group, key,
+                       if (maybe_config_list(driver, sdi, channel_group, key,
                                        &gvar_list) != SR_OK) {
                                printf("\n");
                                continue;
                        }
 
-                       if (sr_config_get(driver, sdi, channel_group, key, &gvar) == SR_OK) {
+                       if (maybe_config_get(driver, sdi, channel_group, key, &gvar) == SR_OK) {
                                g_variant_get(gvar, "(dd)", &dcur_low, &dcur_high);
                                g_variant_unref(gvar);
                        } else {
@@ -590,7 +562,7 @@ void show_dev_detail(void)
 
                } else if (srci->datatype == SR_T_FLOAT) {
                        printf("    %s: ", srci->id);
-                       if (sr_config_get(driver, sdi, channel_group, key,
+                       if (maybe_config_get(driver, sdi, channel_group, key,
                                        &gvar) == SR_OK) {
                                printf("%f\n", g_variant_get_double(gvar));
                                g_variant_unref(gvar);
@@ -600,14 +572,14 @@ void show_dev_detail(void)
                } else if (srci->datatype == SR_T_RATIONAL_PERIOD
                                || srci->datatype == SR_T_RATIONAL_VOLT) {
                        printf("    %s", srci->id);
-                       if (sr_config_get(driver, sdi, channel_group, key,
+                       if (maybe_config_get(driver, sdi, channel_group, key,
                                        &gvar) == SR_OK) {
                                g_variant_get(gvar, "(tt)", &cur_p, &cur_q);
                                g_variant_unref(gvar);
                        } else
                                cur_p = cur_q = 0;
 
-                       if (sr_config_list(driver, sdi, channel_group,
+                       if (maybe_config_list(driver, sdi, channel_group,
                                        key, &gvar_list) != SR_OK) {
                                printf("\n");
                                continue;
index 4ea449f69be1318742d641e045a1b38799d196a1..c07d6cb9ee494742460e0c335701a7f6aea377b7 100644 (file)
 
 /* main.c */
 int select_channels(struct sr_dev_inst *sdi);
+gboolean config_key_has_cap(struct sr_dev_driver *driver,
+               const struct sr_dev_inst *sdi, struct sr_channel_group *cg,
+               uint32_t key, uint32_t capability);
+int maybe_config_get(struct sr_dev_driver *driver,
+               const struct sr_dev_inst *sdi, struct sr_channel_group *cg,
+               uint32_t key, GVariant **gvar);
+int maybe_config_set(struct sr_dev_driver *driver,
+               const struct sr_dev_inst *sdi, struct sr_channel_group *cg,
+               uint32_t key, GVariant *gvar);
+int maybe_config_list(struct sr_dev_driver *driver,
+               const struct sr_dev_inst *sdi, struct sr_channel_group *cg,
+               uint32_t key, GVariant **gvar);
 
 /* show.c */
 void show_version(void);