From: Bert Vermeulen Date: Fri, 14 Nov 2014 19:24:43 +0000 (+0100) Subject: Refactor scan options check. X-Git-Tag: libsigrok-0.4.0~770 X-Git-Url: https://sigrok.org/gitweb/?a=commitdiff_plain;ds=sidebyside;h=adfba7368ac297bba20b0afbc7d7322309508b30;hp=b8721d7cf0ee95c51fb6a3d357f371b314b5aeab;p=libsigrok.git Refactor scan options check. --- diff --git a/src/hwdriver.c b/src/hwdriver.c index 604206e3..24a52db3 100644 --- a/src/hwdriver.c +++ b/src/hwdriver.c @@ -277,6 +277,50 @@ SR_API int sr_driver_init(struct sr_context *ctx, struct sr_dev_driver *driver) return ret; } +static int check_options(struct sr_dev_driver *driver, GSList *options, + uint32_t optlist_key, struct sr_dev_inst *sdi, + struct sr_channel_group *cg) +{ + struct sr_config *src; + const struct sr_config_info *srci; + GVariant *gvar_opts; + GSList *l; + const uint32_t *opts; + gsize num_opts, i; + int ret; + + if (sr_config_list(driver, sdi, cg, optlist_key, &gvar_opts) != SR_OK) { + /* Driver publishes no options for this optlist. */ + return SR_ERR; + } + + ret = SR_OK; + opts = g_variant_get_fixed_array(gvar_opts, &num_opts, sizeof(uint32_t)); + for (l = options; l; l = l->next) { + src = l->data; + for (i = 0; i < num_opts; i++) { + if (opts[i] == src->key) + break; + } + if (i == num_opts) { + if (!(srci = sr_config_info_get(src->key))) + /* Shouldn't happen. */ + sr_err("Invalid option %d.", src->key); + else + sr_err("Invalid option '%s'.", srci->id); + ret = SR_ERR_ARG; + break; + } + if (sr_variant_type_check(src->key, src->data) != SR_OK) { + ret = SR_ERR_ARG; + break; + } + } + g_variant_unref(gvar_opts); + + return ret; +} + /** * Tell a hardware driver to scan for devices. * @@ -304,13 +348,7 @@ SR_API int sr_driver_init(struct sr_context *ctx, struct sr_dev_driver *driver) */ SR_API GSList *sr_driver_scan(struct sr_dev_driver *driver, GSList *options) { - struct sr_config *src; - const struct sr_config_info *srci; GSList *l; - GVariant *gvar_opts; - const uint32_t *opts; - gsize num_opts, i; - int ret; if (!driver) { sr_err("Invalid driver, can't scan for devices."); @@ -322,33 +360,10 @@ SR_API GSList *sr_driver_scan(struct sr_dev_driver *driver, GSList *options) return NULL; } - ret = sr_config_list(driver, NULL, NULL, SR_CONF_SCAN_OPTIONS, &gvar_opts); - if (ret != SR_OK && options) { - /* Driver publishes no scan options but some were given. */ - sr_err("Driver does not support scan options."); - return NULL; - } - opts = g_variant_get_fixed_array(gvar_opts, &num_opts, sizeof(uint32_t)); - for (l = options; l; l = l->next) { - src = l->data; - for (i = 0; i < num_opts; i++) { - if (opts[i] == src->key) - break; - } - if (i == num_opts) { - if (!(srci = sr_config_info_get(src->key))) - sr_err("Driver does not support scan option %d.", src->key); - else - sr_err("Driver does not support scan option '%s'.", srci->id); - g_variant_unref(gvar_opts); - return NULL; - } - if (sr_variant_type_check(src->key, src->data) != SR_OK) { - g_variant_unref(gvar_opts); + if (options) { + if (check_options(driver, options, SR_CONF_SCAN_OPTIONS, NULL, NULL) != SR_OK) return NULL; - } } - g_variant_unref(gvar_opts); l = driver->scan(options);