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