]>
Commit | Line | Data |
---|---|---|
43e5747a | 1 | /* |
630293b4 | 2 | * This file is part of the sigrok-cli project. |
43e5747a UH |
3 | * |
4 | * Copyright (C) 2011 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 <stdio.h> | |
21 | #include <stdlib.h> | |
22 | #include <stdint.h> | |
23 | #include <string.h> | |
24 | #include <glib.h> | |
662a1e27 | 25 | #include "sigrok-cli.h" |
43e5747a | 26 | |
d75c8529 BV |
27 | extern struct sr_context *sr_ctx; |
28 | ||
029d73fe | 29 | struct sr_channel *find_channel(GSList *channellist, const char *channelname) |
43e5747a | 30 | { |
029d73fe | 31 | struct sr_channel *ch; |
497f5362 | 32 | GSList *l; |
43e5747a | 33 | |
029d73fe UH |
34 | ch = NULL; |
35 | for (l = channellist; l; l = l->next) { | |
36 | ch = l->data; | |
37 | if (!strcmp(ch->name, channelname)) | |
497f5362 | 38 | break; |
c2c4a0de | 39 | } |
029d73fe | 40 | ch = l ? l->data : NULL; |
497f5362 | 41 | |
029d73fe | 42 | return ch; |
497f5362 BV |
43 | } |
44 | ||
029d73fe | 45 | GSList *parse_channelstring(struct sr_dev_inst *sdi, const char *channelstring) |
497f5362 | 46 | { |
029d73fe | 47 | struct sr_channel *ch; |
c6fa2b2e | 48 | GSList *channellist, *channels; |
497f5362 BV |
49 | int ret, n, b, e, i; |
50 | char **tokens, **range, **names, *eptr, str[8]; | |
43e5747a | 51 | |
c6fa2b2e UH |
52 | channels = sr_dev_inst_channels_get(sdi); |
53 | ||
029d73fe UH |
54 | if (!channelstring || !channelstring[0]) |
55 | /* Use all channels by default. */ | |
c6fa2b2e | 56 | return g_slist_copy(channels); |
497f5362 BV |
57 | |
58 | ret = SR_OK; | |
59 | range = NULL; | |
66149c20 | 60 | names = NULL; |
029d73fe UH |
61 | channellist = NULL; |
62 | tokens = g_strsplit(channelstring, ",", 0); | |
43e5747a | 63 | for (i = 0; tokens[i]; i++) { |
497f5362 | 64 | if (tokens[i][0] == '\0') { |
029d73fe | 65 | g_critical("Invalid empty channel."); |
497f5362 BV |
66 | ret = SR_ERR; |
67 | break; | |
68 | } | |
43e5747a | 69 | if (strchr(tokens[i], '-')) { |
f0f54487 UH |
70 | /* |
71 | * A range of channels in the form a-b. This will only work | |
029d73fe UH |
72 | * if the channels are named as numbers -- so every channel |
73 | * in the range must exist as a channel name string in the | |
f0f54487 UH |
74 | * device. |
75 | */ | |
43e5747a UH |
76 | range = g_strsplit(tokens[i], "-", 2); |
77 | if (!range[0] || !range[1] || range[2]) { | |
78 | /* Need exactly two arguments. */ | |
029d73fe | 79 | g_critical("Invalid channel syntax '%s'.", tokens[i]); |
497f5362 | 80 | ret = SR_ERR; |
8ec20386 | 81 | goto range_fail; |
43e5747a UH |
82 | } |
83 | ||
497f5362 BV |
84 | b = strtol(range[0], &eptr, 10); |
85 | if (eptr == range[0] || *eptr != '\0') { | |
029d73fe | 86 | g_critical("Invalid channel '%s'.", range[0]); |
497f5362 | 87 | ret = SR_ERR; |
8ec20386 | 88 | goto range_fail; |
497f5362 | 89 | } |
43e5747a | 90 | e = strtol(range[1], NULL, 10); |
497f5362 | 91 | if (eptr == range[1] || *eptr != '\0') { |
029d73fe | 92 | g_critical("Invalid channel '%s'.", range[1]); |
497f5362 | 93 | ret = SR_ERR; |
8ec20386 | 94 | goto range_fail; |
497f5362 BV |
95 | } |
96 | if (b < 0 || b >= e) { | |
029d73fe | 97 | g_critical("Invalid channel range '%s'.", tokens[i]); |
497f5362 | 98 | ret = SR_ERR; |
8ec20386 | 99 | goto range_fail; |
43e5747a UH |
100 | } |
101 | ||
102 | while (b <= e) { | |
497f5362 BV |
103 | n = snprintf(str, 8, "%d", b); |
104 | if (n < 0 || n > 8) { | |
029d73fe | 105 | g_critical("Invalid channel '%d'.", b); |
497f5362 BV |
106 | ret = SR_ERR; |
107 | break; | |
108 | } | |
c6fa2b2e | 109 | ch = find_channel(channels, str); |
029d73fe UH |
110 | if (!ch) { |
111 | g_critical("unknown channel '%d'.", b); | |
497f5362 BV |
112 | ret = SR_ERR; |
113 | break; | |
114 | } | |
029d73fe | 115 | channellist = g_slist_append(channellist, ch); |
43e5747a UH |
116 | b++; |
117 | } | |
8ec20386 DJ |
118 | range_fail: |
119 | if (range) | |
120 | g_strfreev(range); | |
121 | ||
497f5362 BV |
122 | if (ret != SR_OK) |
123 | break; | |
43e5747a | 124 | } else { |
497f5362 BV |
125 | names = g_strsplit(tokens[i], "=", 2); |
126 | if (!names[0] || (names[1] && names[2])) { | |
127 | /* Need one or two arguments. */ | |
029d73fe | 128 | g_critical("Invalid channel '%s'.", tokens[i]); |
6df458b7 | 129 | g_strfreev(names); |
497f5362 | 130 | ret = SR_ERR; |
43e5747a UH |
131 | break; |
132 | } | |
133 | ||
c6fa2b2e | 134 | ch = find_channel(channels, names[0]); |
029d73fe UH |
135 | if (!ch) { |
136 | g_critical("unknown channel '%s'.", names[0]); | |
6df458b7 | 137 | g_strfreev(names); |
497f5362 BV |
138 | ret = SR_ERR; |
139 | break; | |
140 | } | |
141 | if (names[1]) { | |
029d73fe UH |
142 | /* Rename channel. */ |
143 | g_free(ch->name); | |
144 | ch->name = g_strdup(names[1]); | |
43e5747a | 145 | } |
029d73fe | 146 | channellist = g_slist_append(channellist, ch); |
8ec20386 | 147 | |
6df458b7 | 148 | g_strfreev(names); |
43e5747a UH |
149 | } |
150 | } | |
66149c20 | 151 | |
497f5362 | 152 | if (ret != SR_OK) { |
029d73fe UH |
153 | g_slist_free(channellist); |
154 | channellist = NULL; | |
43e5747a UH |
155 | } |
156 | ||
157 | g_strfreev(tokens); | |
43e5747a | 158 | |
029d73fe | 159 | return channellist; |
43e5747a UH |
160 | } |
161 | ||
6b27bde4 BV |
162 | int parse_trigger_match(char c) |
163 | { | |
164 | int match; | |
165 | ||
166 | if (c == '0') | |
167 | match = SR_TRIGGER_ZERO; | |
168 | else if (c == '1') | |
169 | match = SR_TRIGGER_ONE; | |
170 | else if (c == 'r') | |
171 | match = SR_TRIGGER_RISING; | |
172 | else if (c == 'f') | |
173 | match = SR_TRIGGER_FALLING; | |
174 | else if (c == 'e') | |
175 | match = SR_TRIGGER_EDGE; | |
176 | else if (c == 'o') | |
177 | match = SR_TRIGGER_OVER; | |
178 | else if (c == 'u') | |
179 | match = SR_TRIGGER_UNDER; | |
180 | else | |
181 | match = 0; | |
182 | ||
183 | return match; | |
184 | } | |
185 | ||
4bf77ec6 BV |
186 | int parse_triggerstring(const struct sr_dev_inst *sdi, const char *s, |
187 | struct sr_trigger **trigger) | |
6b27bde4 BV |
188 | { |
189 | struct sr_channel *ch; | |
6b27bde4 BV |
190 | struct sr_trigger_stage *stage; |
191 | GVariant *gvar; | |
c6fa2b2e | 192 | GSList *l, *channels; |
6b27bde4 BV |
193 | gsize num_matches; |
194 | gboolean found_match, error; | |
195 | const int32_t *matches; | |
196 | int32_t match; | |
197 | unsigned int j; | |
198 | int t, i; | |
199 | char **tokens, *sep; | |
c6fa2b2e UH |
200 | struct sr_dev_driver *driver; |
201 | ||
202 | driver = sr_dev_inst_driver_get(sdi); | |
203 | channels = sr_dev_inst_channels_get(sdi); | |
6b27bde4 | 204 | |
24bd9719 | 205 | if (maybe_config_list(driver, sdi, NULL, SR_CONF_TRIGGER_MATCH, |
6b27bde4 BV |
206 | &gvar) != SR_OK) { |
207 | g_critical("Device doesn't support any triggers."); | |
208 | return FALSE; | |
209 | } | |
210 | matches = g_variant_get_fixed_array(gvar, &num_matches, sizeof(int32_t)); | |
211 | ||
4bf77ec6 | 212 | *trigger = sr_trigger_new(NULL); |
6b27bde4 BV |
213 | error = FALSE; |
214 | tokens = g_strsplit(s, ",", -1); | |
215 | for (i = 0; tokens[i]; i++) { | |
216 | if (!(sep = strchr(tokens[i], '='))) { | |
217 | g_critical("Invalid trigger '%s'.", tokens[i]); | |
218 | error = TRUE; | |
219 | break; | |
220 | } | |
221 | *sep++ = 0; | |
222 | ch = NULL; | |
c6fa2b2e | 223 | for (l = channels; l; l = l->next) { |
6b27bde4 BV |
224 | ch = l->data; |
225 | if (ch->enabled && !strcmp(ch->name, tokens[i])) | |
226 | break; | |
227 | ch = NULL; | |
228 | } | |
229 | if (!ch) { | |
230 | g_critical("Invalid channel '%s'.", tokens[i]); | |
231 | error = TRUE; | |
232 | break; | |
233 | } | |
234 | for (t = 0; sep[t]; t++) { | |
235 | if (!(match = parse_trigger_match(sep[t]))) { | |
236 | g_critical("Invalid trigger match '%c'.", sep[t]); | |
237 | error = TRUE; | |
238 | break; | |
239 | } | |
240 | found_match = FALSE; | |
241 | for (j = 0; j < num_matches; j++) { | |
242 | if (matches[j] == match) { | |
243 | found_match = TRUE; | |
244 | break; | |
245 | } | |
246 | } | |
247 | if (!found_match) { | |
248 | g_critical("Trigger match '%c' not supported by device.", sep[t]); | |
249 | error = TRUE; | |
250 | break; | |
251 | } | |
252 | /* Make sure this ends up in the right stage, creating | |
253 | * them as needed. */ | |
4bf77ec6 BV |
254 | while (!(stage = g_slist_nth_data((*trigger)->stages, t))) |
255 | sr_trigger_stage_add(*trigger); | |
6b27bde4 BV |
256 | if (sr_trigger_match_add(stage, ch, match, 0) != SR_OK) { |
257 | error = TRUE; | |
258 | break; | |
259 | } | |
260 | } | |
261 | } | |
262 | g_strfreev(tokens); | |
263 | g_variant_unref(gvar); | |
264 | ||
265 | if (error) | |
4bf77ec6 | 266 | sr_trigger_free(*trigger); |
6b27bde4 BV |
267 | |
268 | return !error; | |
269 | } | |
270 | ||
63bb454c | 271 | GHashTable *parse_generic_arg(const char *arg, gboolean sep_first) |
43e5747a UH |
272 | { |
273 | GHashTable *hash; | |
274 | int i; | |
275 | char **elements, *e; | |
276 | ||
277 | if (!arg || !arg[0]) | |
278 | return NULL; | |
279 | ||
63bb454c BV |
280 | i = 0; |
281 | hash = g_hash_table_new_full(g_str_hash, g_str_equal, | |
282 | g_free, g_free); | |
43e5747a | 283 | elements = g_strsplit(arg, ":", 0); |
63bb454c BV |
284 | if (sep_first) |
285 | g_hash_table_insert(hash, g_strdup("sigrok_key"), | |
286 | g_strdup(elements[i++])); | |
287 | for (; elements[i]; i++) { | |
43e5747a UH |
288 | e = strchr(elements[i], '='); |
289 | if (!e) | |
290 | g_hash_table_insert(hash, g_strdup(elements[i]), NULL); | |
291 | else { | |
292 | *e++ = '\0'; | |
293 | g_hash_table_insert(hash, g_strdup(elements[i]), g_strdup(e)); | |
294 | } | |
295 | } | |
296 | g_strfreev(elements); | |
297 | ||
298 | return hash; | |
299 | } | |
300 | ||
87e24fed BV |
301 | GHashTable *generic_arg_to_opt(const struct sr_option **opts, GHashTable *genargs) |
302 | { | |
303 | GHashTable *hash; | |
304 | GVariant *gvar; | |
305 | int i; | |
306 | char *s; | |
41ef1ae4 | 307 | gboolean b; |
87e24fed BV |
308 | |
309 | hash = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, | |
310 | (GDestroyNotify)g_variant_unref); | |
311 | for (i = 0; opts[i]; i++) { | |
312 | if (!(s = g_hash_table_lookup(genargs, opts[i]->id))) | |
313 | continue; | |
314 | if (g_variant_is_of_type(opts[i]->def, G_VARIANT_TYPE_UINT32)) { | |
315 | gvar = g_variant_new_uint32(strtoul(s, NULL, 10)); | |
316 | g_hash_table_insert(hash, g_strdup(opts[i]->id), | |
317 | g_variant_ref_sink(gvar)); | |
318 | } else if (g_variant_is_of_type(opts[i]->def, G_VARIANT_TYPE_INT32)) { | |
319 | gvar = g_variant_new_int32(strtoul(s, NULL, 10)); | |
320 | g_hash_table_insert(hash, g_strdup(opts[i]->id), | |
321 | g_variant_ref_sink(gvar)); | |
902e368e BV |
322 | } else if (g_variant_is_of_type(opts[i]->def, G_VARIANT_TYPE_UINT64)) { |
323 | gvar = g_variant_new_uint64(strtoul(s, NULL, 10)); | |
324 | g_hash_table_insert(hash, g_strdup(opts[i]->id), | |
325 | g_variant_ref_sink(gvar)); | |
87e24fed BV |
326 | } else if (g_variant_is_of_type(opts[i]->def, G_VARIANT_TYPE_DOUBLE)) { |
327 | gvar = g_variant_new_double(strtod(s, NULL)); | |
328 | g_hash_table_insert(hash, g_strdup(opts[i]->id), | |
329 | g_variant_ref_sink(gvar)); | |
330 | } else if (g_variant_is_of_type(opts[i]->def, G_VARIANT_TYPE_STRING)) { | |
331 | gvar = g_variant_new_string(s); | |
332 | g_hash_table_insert(hash, g_strdup(opts[i]->id), | |
333 | g_variant_ref_sink(gvar)); | |
41ef1ae4 JS |
334 | } else if (g_variant_is_of_type(opts[i]->def, G_VARIANT_TYPE_BOOLEAN)) { |
335 | b = TRUE; | |
336 | if (0 == strcmp(s, "false") || 0 == strcmp(s, "no")) { | |
337 | b = FALSE; | |
338 | } else if (!(0 == strcmp(s, "true") || 0 == strcmp(s, "yes"))) { | |
339 | g_critical("Unable to convert '%s' to boolean!", s); | |
340 | } | |
341 | ||
342 | gvar = g_variant_new_boolean(b); | |
343 | g_hash_table_insert(hash, g_strdup(opts[i]->id), | |
344 | g_variant_ref_sink(gvar)); | |
87e24fed BV |
345 | } else { |
346 | g_critical("Don't know GVariant type for option '%s'!", opts[i]->id); | |
347 | } | |
348 | } | |
349 | ||
350 | return hash; | |
351 | } | |
352 | ||
2be182e6 | 353 | static char *strcanon(const char *str) |
432de709 BV |
354 | { |
355 | int p0, p1; | |
356 | char *s; | |
357 | ||
358 | /* Returns newly allocated string. */ | |
359 | s = g_ascii_strdown(str, -1); | |
360 | for (p0 = p1 = 0; str[p0]; p0++) { | |
361 | if ((s[p0] >= 'a' && s[p0] <= 'z') | |
362 | || (s[p0] >= '0' && s[p0] <= '9')) | |
363 | s[p1++] = s[p0]; | |
364 | } | |
365 | s[p1] = '\0'; | |
366 | ||
367 | return s; | |
368 | } | |
369 | ||
d2ee5eea | 370 | int canon_cmp(const char *str1, const char *str2) |
432de709 BV |
371 | { |
372 | int ret; | |
373 | char *s1, *s2; | |
374 | ||
375 | s1 = strcanon(str1); | |
376 | s2 = strcanon(str2); | |
377 | ret = g_ascii_strcasecmp(s1, s2); | |
378 | g_free(s2); | |
379 | g_free(s1); | |
380 | ||
381 | return ret; | |
382 | } | |
d75c8529 BV |
383 | |
384 | /* Convert driver options hash to GSList of struct sr_config. */ | |
385 | static GSList *hash_to_hwopt(GHashTable *hash) | |
386 | { | |
387 | struct sr_config *src; | |
388 | GList *gl, *keys; | |
389 | GSList *opts; | |
390 | char *key; | |
391 | ||
392 | keys = g_hash_table_get_keys(hash); | |
393 | opts = NULL; | |
394 | for (gl = keys; gl; gl = gl->next) { | |
395 | key = gl->data; | |
396 | src = g_malloc(sizeof(struct sr_config)); | |
397 | if (opt_to_gvar(key, g_hash_table_lookup(hash, key), src) != 0) | |
398 | return NULL; | |
399 | opts = g_slist_append(opts, src); | |
400 | } | |
401 | g_list_free(keys); | |
402 | ||
403 | return opts; | |
404 | } | |
405 | ||
406 | int parse_driver(char *arg, struct sr_dev_driver **driver, GSList **drvopts) | |
407 | { | |
408 | struct sr_dev_driver **drivers; | |
409 | GHashTable *drvargs; | |
410 | int i; | |
411 | char *drvname; | |
412 | ||
413 | drvargs = parse_generic_arg(arg, TRUE); | |
414 | ||
415 | drvname = g_strdup(g_hash_table_lookup(drvargs, "sigrok_key")); | |
416 | g_hash_table_remove(drvargs, "sigrok_key"); | |
417 | *driver = NULL; | |
418 | drivers = sr_driver_list(); | |
419 | for (i = 0; drivers[i]; i++) { | |
420 | if (strcmp(drivers[i]->name, drvname)) | |
421 | continue; | |
422 | *driver = drivers[i]; | |
423 | } | |
424 | if (!*driver) { | |
425 | g_critical("Driver %s not found.", drvname); | |
426 | g_hash_table_destroy(drvargs); | |
427 | g_free(drvname); | |
428 | return FALSE; | |
429 | } | |
430 | g_free(drvname); | |
431 | if (sr_driver_init(sr_ctx, *driver) != SR_OK) { | |
432 | g_critical("Failed to initialize driver."); | |
433 | g_hash_table_destroy(drvargs); | |
434 | return FALSE; | |
435 | } | |
436 | ||
437 | if (drvopts) { | |
438 | *drvopts = NULL; | |
439 | if (g_hash_table_size(drvargs) > 0) { | |
440 | if (!(*drvopts = hash_to_hwopt(drvargs))) { | |
441 | /* Unknown options, already logged. */ | |
442 | g_hash_table_destroy(drvargs); | |
443 | return FALSE; | |
444 | } | |
445 | } | |
446 | } | |
447 | ||
448 | g_hash_table_destroy(drvargs); | |
449 | ||
450 | return TRUE; | |
451 | } |