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