]> sigrok.org Git - sigrok-cli.git/blob - device.c
Add support for config keys of type G_VARIANT_TYPE_INT32.
[sigrok-cli.git] / device.c
1 /*
2  * This file is part of the sigrok-cli project.
3  *
4  * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include "sigrok-cli.h"
21 #include "config.h"
22 #include <glib.h>
23
24 extern struct sr_context *sr_ctx;
25 extern gchar *opt_drv;
26 extern gchar *opt_probe_group;
27
28 /* Convert driver options hash to GSList of struct sr_config. */
29 static GSList *hash_to_hwopt(GHashTable *hash)
30 {
31         struct sr_config *src;
32         GList *gl, *keys;
33         GSList *opts;
34         char *key;
35
36         keys = g_hash_table_get_keys(hash);
37         opts = NULL;
38         for (gl = keys; gl; gl = gl->next) {
39                 key = gl->data;
40                 src = g_malloc(sizeof(struct sr_config));
41                 if (opt_to_gvar(key, g_hash_table_lookup(hash, key), src) != 0)
42                         return NULL;
43                 opts = g_slist_append(opts, src);
44         }
45         g_list_free(keys);
46
47         return opts;
48 }
49
50 static void free_drvopts(struct sr_config *src)
51 {
52         g_variant_unref(src->data);
53         g_free(src);
54 }
55
56 GSList *device_scan(void)
57 {
58         struct sr_dev_driver **drivers, *driver;
59         GHashTable *drvargs;
60         GSList *drvopts, *devices, *tmpdevs, *l;
61         int i;
62         char *drvname;
63
64         if (opt_drv) {
65                 drvargs = parse_generic_arg(opt_drv, TRUE);
66                 drvname = g_strdup(g_hash_table_lookup(drvargs, "sigrok_key"));
67                 g_hash_table_remove(drvargs, "sigrok_key");
68                 driver = NULL;
69                 drivers = sr_driver_list();
70                 for (i = 0; drivers[i]; i++) {
71                         if (strcmp(drivers[i]->name, drvname))
72                                 continue;
73                         driver = drivers[i];
74                 }
75                 if (!driver) {
76                         g_critical("Driver %s not found.", drvname);
77                         g_hash_table_destroy(drvargs);
78                         g_free(drvname);
79                         return NULL;
80                 }
81                 g_free(drvname);
82                 if (sr_driver_init(sr_ctx, driver) != SR_OK) {
83                         g_critical("Failed to initialize driver.");
84                         g_hash_table_destroy(drvargs);
85                         return NULL;
86                 }
87                 drvopts = NULL;
88                 if (g_hash_table_size(drvargs) > 0) {
89                         if (!(drvopts = hash_to_hwopt(drvargs))) {
90                                 /* Unknown options, already logged. */
91                                 g_hash_table_destroy(drvargs);
92                                 return NULL;
93                         }
94                 }
95                 g_hash_table_destroy(drvargs);
96                 devices = sr_driver_scan(driver, drvopts);
97                 g_slist_free_full(drvopts, (GDestroyNotify)free_drvopts);
98         } else {
99                 /* No driver specified, let them all scan on their own. */
100                 devices = NULL;
101                 drivers = sr_driver_list();
102                 for (i = 0; drivers[i]; i++) {
103                         driver = drivers[i];
104                         if (sr_driver_init(sr_ctx, driver) != SR_OK) {
105                                 g_critical("Failed to initialize driver.");
106                                 return NULL;
107                         }
108                         tmpdevs = sr_driver_scan(driver, NULL);
109                         for (l = tmpdevs; l; l = l->next)
110                                 devices = g_slist_append(devices, l->data);
111                         g_slist_free(tmpdevs);
112                 }
113         }
114
115         return devices;
116 }
117
118 struct sr_probe_group *select_probe_group(struct sr_dev_inst *sdi)
119 {
120         struct sr_probe_group *pg;
121         GSList *l;
122
123         if (!opt_probe_group)
124                 return NULL;
125
126         if (!sdi->probe_groups) {
127                 g_critical("This device does not have any probe groups.");
128                 return NULL;
129         }
130
131         for (l = sdi->probe_groups; l; l = l->next) {
132                 pg = l->data;
133                 if (!strcasecmp(opt_probe_group, pg->name)) {
134                         return pg;
135                 }
136         }
137
138         return NULL;
139 }
140