]> sigrok.org Git - sigrok-cli.git/blame - device.c
doc: update IRC reference to Libera.Chat
[sigrok-cli.git] / device.c
CommitLineData
2be182e6
BV
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
d486cbdd 20#include <config.h>
2be182e6 21#include <glib.h>
55de58a2 22#include <string.h>
662a1e27 23#include "sigrok-cli.h"
2be182e6 24
2be182e6
BV
25static void free_drvopts(struct sr_config *src)
26{
27 g_variant_unref(src->data);
28 g_free(src);
29}
30
31GSList *device_scan(void)
32{
33 struct sr_dev_driver **drivers, *driver;
2be182e6
BV
34 GSList *drvopts, *devices, *tmpdevs, *l;
35 int i;
2be182e6
BV
36
37 if (opt_drv) {
b20ea789 38 /* Caller specified driver. Use it. Only this one. */
d75c8529 39 if (!parse_driver(opt_drv, &driver, &drvopts))
2be182e6 40 return NULL;
2be182e6
BV
41 devices = sr_driver_scan(driver, drvopts);
42 g_slist_free_full(drvopts, (GDestroyNotify)free_drvopts);
b20ea789
GS
43 } else if (opt_dont_scan) {
44 /* No -d choice, and -D "don't scan" requested. Do nothing. */
45 devices = NULL;
2be182e6 46 } else {
b20ea789 47 /* No driver specified. Scan all available drivers. */
2be182e6 48 devices = NULL;
59e421bb 49 drivers = sr_driver_list(sr_ctx);
2be182e6
BV
50 for (i = 0; drivers[i]; i++) {
51 driver = drivers[i];
52 if (sr_driver_init(sr_ctx, driver) != SR_OK) {
53 g_critical("Failed to initialize driver.");
54 return NULL;
55 }
56 tmpdevs = sr_driver_scan(driver, NULL);
57 for (l = tmpdevs; l; l = l->next)
58 devices = g_slist_append(devices, l->data);
59 g_slist_free(tmpdevs);
60 }
61 }
62
63 return devices;
64}
65
9a6e8ec4
GS
66/**
67 * Lookup a channel group from its name.
68 *
6c94f0c1
GS
69 * Uses the caller specified channel group name, or a previously stored
70 * option value as a fallback. Returns a reference to the channel group
71 * when the lookup succeeded, or #NULL after lookup failure, as well as
72 * #NULL for the global channel group (the device).
73 *
74 * Accepts either #NULL pointer, or an empty string, or the "global"
75 * literal to address the global channel group (the device). Emits an
76 * error message when the lookup failed while a name was specified.
9a6e8ec4
GS
77 *
78 * @param[in] sdi Device instance.
6c94f0c1 79 * @param[in] cg_name Caller provided channel group name.
9a6e8ec4
GS
80 *
81 * @returns The channel group, or #NULL for failed lookup.
82 */
6c94f0c1
GS
83struct sr_channel_group *lookup_channel_group(struct sr_dev_inst *sdi,
84 const char *cg_name)
2be182e6 85{
ca50f4b3 86 struct sr_channel_group *cg;
c6fa2b2e 87 GSList *l, *channel_groups;
2be182e6 88
6c94f0c1
GS
89 if (!cg_name)
90 cg_name = opt_channel_group;
91 if (cg_name && g_ascii_strcasecmp(cg_name, "global") == 0)
92 cg_name = NULL;
93 if (!cg_name || !*cg_name)
2be182e6
BV
94 return NULL;
95
c6fa2b2e 96 channel_groups = sr_dev_inst_channel_groups_get(sdi);
c6fa2b2e 97 if (!channel_groups) {
ca50f4b3 98 g_critical("This device does not have any channel groups.");
2be182e6
BV
99 return NULL;
100 }
101
c6fa2b2e 102 for (l = channel_groups; l; l = l->next) {
ca50f4b3 103 cg = l->data;
6c94f0c1 104 if (g_ascii_strcasecmp(cg_name, cg->name) != 0)
9a6e8ec4
GS
105 continue;
106 return cg;
2be182e6 107 }
6c94f0c1 108 g_critical("Invalid channel group '%s'", cg_name);
2be182e6
BV
109
110 return NULL;
111}