]> sigrok.org Git - sigrok-cli.git/commitdiff
Show driver detail even if no device was found.
authorBert Vermeulen <redacted>
Mon, 27 Oct 2014 20:29:17 +0000 (21:29 +0100)
committerBert Vermeulen <redacted>
Mon, 27 Oct 2014 20:29:17 +0000 (21:29 +0100)
device.c
parsers.c
show.c
sigrok-cli.h

index 55d306b323699c3ba8a5948a3b1a679eb7b54101..ea4d073bf98cfd08156176d77b64cf72fa146ca3 100644 (file)
--- a/device.c
+++ b/device.c
 
 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 {
index 8d654eec5f3804c1ce80958a006610e8ec7def0d..ad5f37925f7d0e156e9158e0865f813c2de6d70d 100644 (file)
--- a/parsers.c
+++ b/parsers.c
@@ -24,6 +24,8 @@
 #include <string.h>
 #include <glib.h>
 
+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 cc5258b6777074a4d7d2154e5e04a02f67678c2e..fa2305f0f98b675c86e744f86b062d978f5b0652 100644 (file)
--- 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);
index 8d0b624b786e8cd03595683eed0111f9065816ab..4ea449f69be1318742d641e045a1b38799d196a1 100644 (file)
@@ -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);