Support multiple protocol decoder stacks
[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 <string.h>
23 #include "sigrok-cli.h"
24
25 static void free_drvopts(struct sr_config *src)
26 {
27         g_variant_unref(src->data);
28         g_free(src);
29 }
30
31 GSList *device_scan(void)
32 {
33         struct sr_dev_driver **drivers, *driver;
34         GSList *drvopts, *devices, *tmpdevs, *l;
35         int i;
36
37         if (opt_drv) {
38                 if (!parse_driver(opt_drv, &driver, &drvopts))
39                         return NULL;
40                 devices = sr_driver_scan(driver, drvopts);
41                 g_slist_free_full(drvopts, (GDestroyNotify)free_drvopts);
42         } else {
43                 /* No driver specified, let them all scan on their own. */
44                 devices = NULL;
45                 drivers = sr_driver_list(sr_ctx);
46                 for (i = 0; drivers[i]; i++) {
47                         driver = drivers[i];
48                         if (sr_driver_init(sr_ctx, driver) != SR_OK) {
49                                 g_critical("Failed to initialize driver.");
50                                 return NULL;
51                         }
52                         tmpdevs = sr_driver_scan(driver, NULL);
53                         for (l = tmpdevs; l; l = l->next)
54                                 devices = g_slist_append(devices, l->data);
55                         g_slist_free(tmpdevs);
56                 }
57         }
58
59         return devices;
60 }
61
62 struct sr_channel_group *select_channel_group(struct sr_dev_inst *sdi)
63 {
64         struct sr_channel_group *cg;
65         GSList *l, *channel_groups;
66
67         if (!opt_channel_group)
68                 return NULL;
69
70         channel_groups = sr_dev_inst_channel_groups_get(sdi);
71
72         if (!channel_groups) {
73                 g_critical("This device does not have any channel groups.");
74                 return NULL;
75         }
76
77         for (l = channel_groups; l; l = l->next) {
78                 cg = l->data;
79                 if (!g_ascii_strcasecmp(opt_channel_group, cg->name)) {
80                         return cg;
81                 }
82         }
83         g_critical("Invalid channel group '%s'", opt_channel_group);
84
85         return NULL;
86 }