Refactor main source into separate files
[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 "config.h"
21 #include <glib.h>
22 #include <libsigrok/libsigrok.h>
23 #include "sigrok-cli.h"
24
25 extern struct sr_context *sr_ctx;
26 extern gchar *opt_drv;
27 extern gchar *opt_probe_group;
28
29 /* Convert driver options hash to GSList of struct sr_config. */
30 static GSList *hash_to_hwopt(GHashTable *hash)
31 {
32         const struct sr_config_info *srci;
33         struct sr_config *src;
34         GList *gl, *keys;
35         GSList *opts;
36         char *key, *value;
37
38         keys = g_hash_table_get_keys(hash);
39         opts = NULL;
40         for (gl = keys; gl; gl = gl->next) {
41                 key = gl->data;
42                 if (!(srci = sr_config_info_name_get(key))) {
43                         g_critical("Unknown option %s", key);
44                         return NULL;
45                 }
46                 src = g_try_malloc(sizeof(struct sr_config));
47                 src->key = srci->key;
48                 value = g_hash_table_lookup(hash, key);
49                 src->data = g_variant_new_string(value);
50                 opts = g_slist_append(opts, src);
51         }
52         g_list_free(keys);
53
54         return opts;
55 }
56
57 static void free_drvopts(struct sr_config *src)
58 {
59         g_variant_unref(src->data);
60         g_free(src);
61 }
62
63 GSList *device_scan(void)
64 {
65         struct sr_dev_driver **drivers, *driver;
66         GHashTable *drvargs;
67         GSList *drvopts, *devices, *tmpdevs, *l;
68         int i;
69         char *drvname;
70
71         if (opt_drv) {
72                 drvargs = parse_generic_arg(opt_drv, TRUE);
73                 drvname = g_strdup(g_hash_table_lookup(drvargs, "sigrok_key"));
74                 g_hash_table_remove(drvargs, "sigrok_key");
75                 driver = NULL;
76                 drivers = sr_driver_list();
77                 for (i = 0; drivers[i]; i++) {
78                         if (strcmp(drivers[i]->name, drvname))
79                                 continue;
80                         driver = drivers[i];
81                 }
82                 if (!driver) {
83                         g_critical("Driver %s not found.", drvname);
84                         g_hash_table_destroy(drvargs);
85                         g_free(drvname);
86                         return NULL;
87                 }
88                 g_free(drvname);
89                 if (sr_driver_init(sr_ctx, driver) != SR_OK) {
90                         g_critical("Failed to initialize driver.");
91                         g_hash_table_destroy(drvargs);
92                         return NULL;
93                 }
94                 drvopts = NULL;
95                 if (g_hash_table_size(drvargs) > 0) {
96                         if (!(drvopts = hash_to_hwopt(drvargs))) {
97                                 /* Unknown options, already logged. */
98                                 g_hash_table_destroy(drvargs);
99                                 return NULL;
100                         }
101                 }
102                 g_hash_table_destroy(drvargs);
103                 devices = sr_driver_scan(driver, drvopts);
104                 g_slist_free_full(drvopts, (GDestroyNotify)free_drvopts);
105         } else {
106                 /* No driver specified, let them all scan on their own. */
107                 devices = NULL;
108                 drivers = sr_driver_list();
109                 for (i = 0; drivers[i]; i++) {
110                         driver = drivers[i];
111                         if (sr_driver_init(sr_ctx, driver) != SR_OK) {
112                                 g_critical("Failed to initialize driver.");
113                                 return NULL;
114                         }
115                         tmpdevs = sr_driver_scan(driver, NULL);
116                         for (l = tmpdevs; l; l = l->next)
117                                 devices = g_slist_append(devices, l->data);
118                         g_slist_free(tmpdevs);
119                 }
120         }
121
122         return devices;
123 }
124
125 struct sr_probe_group *select_probe_group(struct sr_dev_inst *sdi)
126 {
127         struct sr_probe_group *pg;
128         GSList *l;
129
130         if (!opt_probe_group)
131                 return NULL;
132
133         if (!sdi->probe_groups) {
134                 g_critical("This device does not have any probe groups.");
135                 return NULL;
136         }
137
138         for (l = sdi->probe_groups; l; l = l->next) {
139                 pg = l->data;
140                 if (!strcasecmp(opt_probe_group, pg->name)) {
141                         return pg;
142                 }
143         }
144
145         return NULL;
146 }
147