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