2 * This file is part of the sigrok-cli project.
4 * Copyright (C) 2011 Bert Vermeulen <bert@biot.com>
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.
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.
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/>.
26 #include "sigrok-cli.h"
28 struct sr_channel *find_channel(GSList *channellist, const char *channelname)
30 struct sr_channel *ch;
34 for (l = channellist; l; l = l->next) {
36 if (!strcmp(ch->name, channelname))
39 ch = l ? l->data : NULL;
44 GSList *parse_channelstring(struct sr_dev_inst *sdi, const char *channelstring)
46 struct sr_channel *ch;
47 GSList *channellist, *channels;
49 char **tokens, **range, **names, *eptr, str[8];
51 channels = sr_dev_inst_channels_get(sdi);
53 if (!channelstring || !channelstring[0])
54 /* Use all channels by default. */
55 return g_slist_copy(channels);
61 tokens = g_strsplit(channelstring, ",", 0);
62 for (i = 0; tokens[i]; i++) {
63 if (tokens[i][0] == '\0') {
64 g_critical("Invalid empty channel.");
68 if (strchr(tokens[i], '-')) {
70 * A range of channels in the form a-b. This will only work
71 * if the channels are named as numbers -- so every channel
72 * in the range must exist as a channel name string in the
75 range = g_strsplit(tokens[i], "-", 2);
76 if (!range[0] || !range[1] || range[2]) {
77 /* Need exactly two arguments. */
78 g_critical("Invalid channel syntax '%s'.", tokens[i]);
83 b = strtol(range[0], &eptr, 10);
84 if (eptr == range[0] || *eptr != '\0') {
85 g_critical("Invalid channel '%s'.", range[0]);
89 e = strtol(range[1], NULL, 10);
90 if (eptr == range[1] || *eptr != '\0') {
91 g_critical("Invalid channel '%s'.", range[1]);
95 if (b < 0 || b >= e) {
96 g_critical("Invalid channel range '%s'.", tokens[i]);
102 n = snprintf(str, 8, "%d", b);
103 if (n < 0 || n > 8) {
104 g_critical("Invalid channel '%d'.", b);
108 ch = find_channel(channels, str);
110 g_critical("unknown channel '%d'.", b);
114 channellist = g_slist_append(channellist, ch);
124 names = g_strsplit(tokens[i], "=", 2);
125 if (!names[0] || (names[1] && names[2])) {
126 /* Need one or two arguments. */
127 g_critical("Invalid channel '%s'.", tokens[i]);
133 ch = find_channel(channels, names[0]);
135 g_critical("unknown channel '%s'.", names[0]);
141 /* Rename channel. */
143 ch->name = g_strdup(names[1]);
145 channellist = g_slist_append(channellist, ch);
152 g_slist_free(channellist);
161 int parse_trigger_match(char c)
166 match = SR_TRIGGER_ZERO;
168 match = SR_TRIGGER_ONE;
170 match = SR_TRIGGER_RISING;
172 match = SR_TRIGGER_FALLING;
174 match = SR_TRIGGER_EDGE;
176 match = SR_TRIGGER_OVER;
178 match = SR_TRIGGER_UNDER;
185 int parse_triggerstring(const struct sr_dev_inst *sdi, const char *s,
186 struct sr_trigger **trigger)
188 struct sr_channel *ch;
189 struct sr_trigger_stage *stage;
191 GSList *l, *channels;
193 gboolean found_match, error;
194 const int32_t *matches;
199 struct sr_dev_driver *driver;
201 driver = sr_dev_inst_driver_get(sdi);
202 channels = sr_dev_inst_channels_get(sdi);
204 if (maybe_config_list(driver, sdi, NULL, SR_CONF_TRIGGER_MATCH,
206 g_critical("Device doesn't support any triggers.");
209 matches = g_variant_get_fixed_array(gvar, &num_matches, sizeof(int32_t));
211 *trigger = sr_trigger_new(NULL);
213 tokens = g_strsplit(s, ",", -1);
214 for (i = 0; tokens[i]; i++) {
215 if (!(sep = strchr(tokens[i], '='))) {
216 g_critical("Invalid trigger '%s'.", tokens[i]);
222 for (l = channels; l; l = l->next) {
224 if (ch->enabled && !strcmp(ch->name, tokens[i]))
229 g_critical("Invalid channel '%s'.", tokens[i]);
233 for (t = 0; sep[t]; t++) {
234 if (!(match = parse_trigger_match(sep[t]))) {
235 g_critical("Invalid trigger match '%c'.", sep[t]);
240 for (j = 0; j < num_matches; j++) {
241 if (matches[j] == match) {
247 g_critical("Trigger match '%c' not supported by device.", sep[t]);
251 /* Make sure this ends up in the right stage, creating
253 while (!(stage = g_slist_nth_data((*trigger)->stages, t)))
254 sr_trigger_stage_add(*trigger);
255 if (sr_trigger_match_add(stage, ch, match, 0) != SR_OK) {
262 g_variant_unref(gvar);
265 sr_trigger_free(*trigger);
270 GHashTable *parse_generic_arg(const char *arg, gboolean sep_first)
280 hash = g_hash_table_new_full(g_str_hash, g_str_equal,
282 elements = g_strsplit(arg, ":", 0);
284 g_hash_table_insert(hash, g_strdup("sigrok_key"),
285 g_strdup(elements[i++]));
286 for (; elements[i]; i++) {
287 e = strchr(elements[i], '=');
289 g_hash_table_insert(hash, g_strdup(elements[i]), NULL);
292 g_hash_table_insert(hash, g_strdup(elements[i]), g_strdup(e));
295 g_strfreev(elements);
300 GSList *check_unknown_keys(const struct sr_option **avail, GHashTable *used)
307 const char *avail_id, *found_id;
309 /* Collect a list of used but not available keywords. */
311 g_hash_table_iter_init(&iter, used);
312 while (g_hash_table_iter_next(&iter, &key, NULL)) {
315 for (avail_idx = 0; avail[avail_idx] && avail[avail_idx]->id; avail_idx++) {
316 avail_id = avail[avail_idx]->id;
317 if (strcmp(avail_id, used_id) == 0) {
323 unknown = g_slist_append(unknown, g_strdup(used_id));
326 /* Return the list of unknown keywords, or NULL if empty. */
330 gboolean warn_unknown_keys(const struct sr_option **avail, GHashTable *used,
334 gboolean had_unknown;
337 if (!caption || !*caption)
338 caption = "Unknown keyword";
340 unknown = check_unknown_keys(avail, used);
341 had_unknown = unknown != NULL;
342 for (l = unknown; l; l = l->next) {
344 g_warning("%s: %s.", caption, s);
346 g_slist_free_full(unknown, g_free);
351 GHashTable *generic_arg_to_opt(const struct sr_option **opts, GHashTable *genargs)
359 hash = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
360 (GDestroyNotify)g_variant_unref);
361 for (i = 0; opts[i]; i++) {
362 if (!(s = g_hash_table_lookup(genargs, opts[i]->id)))
364 if (g_variant_is_of_type(opts[i]->def, G_VARIANT_TYPE_UINT32)) {
365 gvar = g_variant_new_uint32(strtoul(s, NULL, 10));
366 g_hash_table_insert(hash, g_strdup(opts[i]->id),
367 g_variant_ref_sink(gvar));
368 } else if (g_variant_is_of_type(opts[i]->def, G_VARIANT_TYPE_INT32)) {
369 gvar = g_variant_new_int32(strtoul(s, NULL, 10));
370 g_hash_table_insert(hash, g_strdup(opts[i]->id),
371 g_variant_ref_sink(gvar));
372 } else if (g_variant_is_of_type(opts[i]->def, G_VARIANT_TYPE_UINT64)) {
373 gvar = g_variant_new_uint64(strtoul(s, NULL, 10));
374 g_hash_table_insert(hash, g_strdup(opts[i]->id),
375 g_variant_ref_sink(gvar));
376 } else if (g_variant_is_of_type(opts[i]->def, G_VARIANT_TYPE_DOUBLE)) {
377 gvar = g_variant_new_double(strtod(s, NULL));
378 g_hash_table_insert(hash, g_strdup(opts[i]->id),
379 g_variant_ref_sink(gvar));
380 } else if (g_variant_is_of_type(opts[i]->def, G_VARIANT_TYPE_STRING)) {
381 gvar = g_variant_new_string(s);
382 g_hash_table_insert(hash, g_strdup(opts[i]->id),
383 g_variant_ref_sink(gvar));
384 } else if (g_variant_is_of_type(opts[i]->def, G_VARIANT_TYPE_BOOLEAN)) {
386 if (0 == strcmp(s, "false") || 0 == strcmp(s, "no")) {
388 } else if (!(0 == strcmp(s, "true") || 0 == strcmp(s, "yes"))) {
389 g_critical("Unable to convert '%s' to boolean!", s);
392 gvar = g_variant_new_boolean(b);
393 g_hash_table_insert(hash, g_strdup(opts[i]->id),
394 g_variant_ref_sink(gvar));
396 g_critical("Don't know GVariant type for option '%s'!", opts[i]->id);
403 static char *strcanon(const char *str)
408 /* Returns newly allocated string. */
409 s = g_ascii_strdown(str, -1);
410 for (p0 = p1 = 0; str[p0]; p0++) {
411 if ((s[p0] >= 'a' && s[p0] <= 'z')
412 || (s[p0] >= '0' && s[p0] <= '9'))
420 int canon_cmp(const char *str1, const char *str2)
427 ret = g_ascii_strcasecmp(s1, s2);
434 /* Convert driver options hash to GSList of struct sr_config. */
435 static GSList *hash_to_hwopt(GHashTable *hash)
437 struct sr_config *src;
442 keys = g_hash_table_get_keys(hash);
444 for (gl = keys; gl; gl = gl->next) {
446 src = g_malloc(sizeof(struct sr_config));
447 if (opt_to_gvar(key, g_hash_table_lookup(hash, key), src) != 0)
449 opts = g_slist_append(opts, src);
456 int parse_driver(char *arg, struct sr_dev_driver **driver, GSList **drvopts)
458 struct sr_dev_driver **drivers;
466 drvargs = parse_generic_arg(arg, TRUE);
468 drvname = g_strdup(g_hash_table_lookup(drvargs, "sigrok_key"));
469 g_hash_table_remove(drvargs, "sigrok_key");
471 drivers = sr_driver_list(sr_ctx);
472 for (i = 0; drivers[i]; i++) {
473 if (strcmp(drivers[i]->name, drvname))
475 *driver = drivers[i];
478 g_critical("Driver %s not found.", drvname);
479 g_hash_table_destroy(drvargs);
484 if (sr_driver_init(sr_ctx, *driver) != SR_OK) {
485 g_critical("Failed to initialize driver.");
486 g_hash_table_destroy(drvargs);
492 if (g_hash_table_size(drvargs) > 0) {
493 if (!(*drvopts = hash_to_hwopt(drvargs))) {
494 /* Unknown options, already logged. */
495 g_hash_table_destroy(drvargs);
501 g_hash_table_destroy(drvargs);