+
+/* Convert driver options hash to GSList of struct sr_hwopt. */
+static GSList *hash_to_hwopt(GHashTable *hash)
+{
+ const struct sr_hwcap_option *hwo;
+ struct sr_hwopt *hwopt;
+ GList *gl, *keys;
+ GSList *opts;
+ char *key, *value;
+
+ keys = g_hash_table_get_keys(hash);
+ opts = NULL;
+ for (gl = keys; gl; gl = gl->next) {
+ key = gl->data;
+ if (!(hwo = sr_drvopt_name_get(key))) {
+ g_critical("Unknown option %s", key);
+ return NULL;
+ }
+ hwopt = g_try_malloc(sizeof(struct sr_hwopt));
+ hwopt->hwopt = hwo->hwcap;
+ value = g_hash_table_lookup(hash, key);
+ hwopt->value = g_strdup(value);
+ opts = g_slist_append(opts, hwopt);
+ }
+ g_list_free(keys);
+
+ return opts;
+}
+
+static void free_hwopt(struct sr_hwopt *hwopt)
+{
+ g_free((void *)hwopt->value);
+ g_free(hwopt);
+}
+
+static 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);
+ return NULL;
+ }
+ g_free(drvname);
+ if (sr_driver_init(sr_ctx, driver) != SR_OK) {
+ g_critical("Failed to initialize driver.");
+ return NULL;
+ }
+ drvopts = NULL;
+ if (g_hash_table_size(drvargs) > 0)
+ if (!(drvopts = hash_to_hwopt(drvargs)))
+ /* Unknown options, already logged. */
+ return NULL;
+ devices = sr_driver_scan(driver, drvopts);
+ g_slist_free_full(drvopts, (GDestroyNotify)free_hwopt);
+ } else {
+ /* No driver specified, let them all scan on their own. */
+ devices = NULL;
+ drivers = sr_driver_list();
+ for (i = 0; drivers[i]; i++) {
+ driver = drivers[i];
+ if (sr_driver_init(sr_ctx, driver) != SR_OK) {
+ g_critical("Failed to initialize driver.");
+ return NULL;
+ }
+ tmpdevs = sr_driver_scan(driver, NULL);
+ for (l = tmpdevs; l; l = l->next)
+ devices = g_slist_append(devices, l->data);
+ g_slist_free(tmpdevs);
+ }
+ }
+
+ return devices;
+}
+