From: Bert Vermeulen Date: Mon, 27 Oct 2014 20:29:17 +0000 (+0100) Subject: Show driver detail even if no device was found. X-Git-Tag: sigrok-cli-0.6.0~54 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=d75c85291a61a6d83386de95cff6ad279e853ebd;p=sigrok-cli.git Show driver detail even if no device was found. --- diff --git a/device.c b/device.c index 55d306b..ea4d073 100644 --- a/device.c +++ b/device.c @@ -24,28 +24,6 @@ extern struct sr_context *sr_ctx; -/* Convert driver options hash to GSList of struct sr_config. */ -static GSList *hash_to_hwopt(GHashTable *hash) -{ - struct sr_config *src; - GList *gl, *keys; - GSList *opts; - char *key; - - keys = g_hash_table_get_keys(hash); - opts = NULL; - for (gl = keys; gl; gl = gl->next) { - key = gl->data; - src = g_malloc(sizeof(struct sr_config)); - if (opt_to_gvar(key, g_hash_table_lookup(hash, key), src) != 0) - return NULL; - opts = g_slist_append(opts, src); - } - g_list_free(keys); - - return opts; -} - static void free_drvopts(struct sr_config *src) { g_variant_unref(src->data); @@ -55,43 +33,12 @@ static void free_drvopts(struct sr_config *src) GSList *device_scan(void) { struct sr_dev_driver **drivers, *driver; - GHashTable *drvargs; GSList *drvopts, *devices, *tmpdevs, *l; int i; - char *drvname; if (opt_drv) { - drvargs = parse_generic_arg(opt_drv, TRUE); - drvname = g_strdup(g_hash_table_lookup(drvargs, "sigrok_key")); - g_hash_table_remove(drvargs, "sigrok_key"); - driver = NULL; - drivers = sr_driver_list(); - for (i = 0; drivers[i]; i++) { - if (strcmp(drivers[i]->name, drvname)) - continue; - driver = drivers[i]; - } - if (!driver) { - g_critical("Driver %s not found.", drvname); - g_hash_table_destroy(drvargs); - g_free(drvname); - return NULL; - } - g_free(drvname); - if (sr_driver_init(sr_ctx, driver) != SR_OK) { - g_critical("Failed to initialize driver."); - g_hash_table_destroy(drvargs); + if (!parse_driver(opt_drv, &driver, &drvopts)) return NULL; - } - drvopts = NULL; - if (g_hash_table_size(drvargs) > 0) { - if (!(drvopts = hash_to_hwopt(drvargs))) { - /* Unknown options, already logged. */ - g_hash_table_destroy(drvargs); - return NULL; - } - } - g_hash_table_destroy(drvargs); devices = sr_driver_scan(driver, drvopts); g_slist_free_full(drvopts, (GDestroyNotify)free_drvopts); } else { diff --git a/parsers.c b/parsers.c index 8d654ee..ad5f379 100644 --- a/parsers.c +++ b/parsers.c @@ -24,6 +24,8 @@ #include #include +extern struct sr_context *sr_ctx; + struct sr_channel *find_channel(GSList *channellist, const char *channelname) { struct sr_channel *ch; @@ -370,3 +372,73 @@ int canon_cmp(const char *str1, const char *str2) return ret; } + +/* Convert driver options hash to GSList of struct sr_config. */ +static GSList *hash_to_hwopt(GHashTable *hash) +{ + struct sr_config *src; + GList *gl, *keys; + GSList *opts; + char *key; + + keys = g_hash_table_get_keys(hash); + opts = NULL; + for (gl = keys; gl; gl = gl->next) { + key = gl->data; + src = g_malloc(sizeof(struct sr_config)); + if (opt_to_gvar(key, g_hash_table_lookup(hash, key), src) != 0) + return NULL; + opts = g_slist_append(opts, src); + } + g_list_free(keys); + + return opts; +} + +int parse_driver(char *arg, struct sr_dev_driver **driver, GSList **drvopts) +{ + struct sr_dev_driver **drivers; + GHashTable *drvargs; + int i; + char *drvname; + + drvargs = parse_generic_arg(arg, TRUE); + + drvname = g_strdup(g_hash_table_lookup(drvargs, "sigrok_key")); + g_hash_table_remove(drvargs, "sigrok_key"); + *driver = NULL; + drivers = sr_driver_list(); + for (i = 0; drivers[i]; i++) { + if (strcmp(drivers[i]->name, drvname)) + continue; + *driver = drivers[i]; + } + if (!*driver) { + g_critical("Driver %s not found.", drvname); + g_hash_table_destroy(drvargs); + g_free(drvname); + return FALSE; + } + g_free(drvname); + if (sr_driver_init(sr_ctx, *driver) != SR_OK) { + g_critical("Failed to initialize driver."); + g_hash_table_destroy(drvargs); + return FALSE; + } + + if (drvopts) { + *drvopts = NULL; + if (g_hash_table_size(drvargs) > 0) { + if (!(*drvopts = hash_to_hwopt(drvargs))) { + /* Unknown options, already logged. */ + g_hash_table_destroy(drvargs); + return FALSE; + } + } + } + + g_hash_table_destroy(drvargs); + + return TRUE; +} + diff --git a/show.c b/show.c index cc5258b..fa2305f 100644 --- a/show.c +++ b/show.c @@ -193,8 +193,47 @@ void show_dev_list(void) } +void show_drv_detail(struct sr_dev_driver *driver) +{ + const struct sr_config_info *srci; + GVariant *gvar_opts; + const uint32_t *opts; + gsize num_elements, i; + + if ((sr_config_list(driver, NULL, NULL, SR_CONF_DEVICE_OPTIONS, + &gvar_opts) == SR_OK)) { + opts = g_variant_get_fixed_array(gvar_opts, &num_elements, + sizeof(uint32_t)); + if (num_elements) { + printf("Driver functions:\n"); + for (i = 0; i < num_elements; i++) { + if (!(srci = sr_config_info_get(opts[i] & SR_CONF_MASK))) + continue; + printf(" %s\n", srci->name); + } + } + g_variant_unref(gvar_opts); + } + + if ((sr_config_list(driver, NULL, NULL, SR_CONF_SCAN_OPTIONS, + &gvar_opts) == SR_OK)) { + opts = g_variant_get_fixed_array(gvar_opts, &num_elements, + sizeof(uint32_t)); + if (num_elements) { + printf("Scan options:\n"); + for (i = 0; i < num_elements; i++) { + if (!(srci = sr_config_info_get(opts[i] & SR_CONF_MASK))) + continue; + printf(" %s\n", srci->id); + } + } + g_variant_unref(gvar_opts); + } +} + void show_dev_detail(void) { + struct sr_dev_driver *driver; struct sr_dev_inst *sdi; const struct sr_config_info *srci; struct sr_channel *ch; @@ -212,6 +251,11 @@ void show_dev_detail(void) char *tmp_str, *s, c; const char **stropts; + if (parse_driver(opt_drv, &driver, NULL)) { + /* A driver was specified, report driver-wide options now. */ + show_drv_detail(driver); + } + if (!(devices = device_scan())) { g_critical("No devices found."); return; @@ -233,19 +277,6 @@ void show_dev_detail(void) return; } - if ((sr_config_list(sdi->driver, NULL, NULL, SR_CONF_SCAN_OPTIONS, - &gvar_opts) == SR_OK)) { - opts = g_variant_get_fixed_array(gvar_opts, &num_elements, - sizeof(int32_t)); - printf("Supported driver options:\n"); - for (i = 0; i < num_elements; i++) { - if (!(srci = sr_config_info_get(opts[i]))) - continue; - printf(" %s\n", srci->id); - } - g_variant_unref(gvar_opts); - } - /* Selected channels and channel group may affect which options are * returned, or which values for them. */ select_channels(sdi); diff --git a/sigrok-cli.h b/sigrok-cli.h index 8d0b624..4ea449f 100644 --- a/sigrok-cli.h +++ b/sigrok-cli.h @@ -78,6 +78,7 @@ int parse_triggerstring(const struct sr_dev_inst *sdi, const char *s, GHashTable *parse_generic_arg(const char *arg, gboolean sep_first); GHashTable *generic_arg_to_opt(const struct sr_option **opts, GHashTable *genargs); int canon_cmp(const char *str1, const char *str2); +int parse_driver(char *arg, struct sr_dev_driver **driver, GSList **drvopts); /* anykey.c */ void add_anykey(struct sr_session *session);