blob: ea4d073bf98cfd08156176d77b64cf72fa146ca3 [file] [log] [blame]
Bert Vermeulen2be182e2013-11-17 13:15:38 +01001/*
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
Uwe Hermann20fb52e2013-11-19 11:48:06 +010020#include "sigrok-cli.h"
Bert Vermeulen2be182e2013-11-17 13:15:38 +010021#include "config.h"
22#include <glib.h>
Uwe Hermann55de58a2014-01-28 15:55:24 +010023#include <string.h>
Bert Vermeulen2be182e2013-11-17 13:15:38 +010024
25extern struct sr_context *sr_ctx;
Bert Vermeulen2be182e2013-11-17 13:15:38 +010026
Bert Vermeulen2be182e2013-11-17 13:15:38 +010027static void free_drvopts(struct sr_config *src)
28{
29 g_variant_unref(src->data);
30 g_free(src);
31}
32
33GSList *device_scan(void)
34{
35 struct sr_dev_driver **drivers, *driver;
Bert Vermeulen2be182e2013-11-17 13:15:38 +010036 GSList *drvopts, *devices, *tmpdevs, *l;
37 int i;
Bert Vermeulen2be182e2013-11-17 13:15:38 +010038
39 if (opt_drv) {
Bert Vermeulend75c8522014-10-27 21:29:17 +010040 if (!parse_driver(opt_drv, &driver, &drvopts))
Bert Vermeulen2be182e2013-11-17 13:15:38 +010041 return NULL;
Bert Vermeulen2be182e2013-11-17 13:15:38 +010042 devices = sr_driver_scan(driver, drvopts);
43 g_slist_free_full(drvopts, (GDestroyNotify)free_drvopts);
44 } else {
45 /* No driver specified, let them all scan on their own. */
46 devices = NULL;
47 drivers = sr_driver_list();
48 for (i = 0; drivers[i]; i++) {
49 driver = drivers[i];
50 if (sr_driver_init(sr_ctx, driver) != SR_OK) {
51 g_critical("Failed to initialize driver.");
52 return NULL;
53 }
54 tmpdevs = sr_driver_scan(driver, NULL);
55 for (l = tmpdevs; l; l = l->next)
56 devices = g_slist_append(devices, l->data);
57 g_slist_free(tmpdevs);
58 }
59 }
60
61 return devices;
62}
63
Uwe Hermannca50f4b2014-03-14 21:46:10 +010064struct sr_channel_group *select_channel_group(struct sr_dev_inst *sdi)
Bert Vermeulen2be182e2013-11-17 13:15:38 +010065{
Uwe Hermannca50f4b2014-03-14 21:46:10 +010066 struct sr_channel_group *cg;
Bert Vermeulen2be182e2013-11-17 13:15:38 +010067 GSList *l;
68
Uwe Hermannca50f4b2014-03-14 21:46:10 +010069 if (!opt_channel_group)
Bert Vermeulen2be182e2013-11-17 13:15:38 +010070 return NULL;
71
Uwe Hermannca50f4b2014-03-14 21:46:10 +010072 if (!sdi->channel_groups) {
73 g_critical("This device does not have any channel groups.");
Bert Vermeulen2be182e2013-11-17 13:15:38 +010074 return NULL;
75 }
76
Uwe Hermannca50f4b2014-03-14 21:46:10 +010077 for (l = sdi->channel_groups; l; l = l->next) {
78 cg = l->data;
79 if (!strcasecmp(opt_channel_group, cg->name)) {
80 return cg;
Bert Vermeulen2be182e2013-11-17 13:15:38 +010081 }
82 }
Bert Vermeulenac1e9972014-09-09 19:54:36 +020083 g_critical("Invalid channel group '%s'", opt_channel_group);
Bert Vermeulen2be182e2013-11-17 13:15:38 +010084
85 return NULL;
86}
87