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/>.
20 #include "sigrok-cli.h"
27 extern struct sr_context *sr_ctx;
29 struct sr_channel *find_channel(GSList *channellist, const char *channelname)
31 struct sr_channel *ch;
35 for (l = channellist; l; l = l->next) {
37 if (!strcmp(ch->name, channelname))
40 ch = l ? l->data : NULL;
45 GSList *parse_channelstring(struct sr_dev_inst *sdi, const char *channelstring)
47 struct sr_channel *ch;
50 char **tokens, **range, **names, *eptr, str[8];
52 if (!channelstring || !channelstring[0])
53 /* Use all channels by default. */
54 return g_slist_copy(sdi->channels);
60 tokens = g_strsplit(channelstring, ",", 0);
61 for (i = 0; tokens[i]; i++) {
62 if (tokens[i][0] == '\0') {
63 g_critical("Invalid empty channel.");
67 if (strchr(tokens[i], '-')) {
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
72 range = g_strsplit(tokens[i], "-", 2);
73 if (!range[0] || !range[1] || range[2]) {
74 /* Need exactly two arguments. */
75 g_critical("Invalid channel syntax '%s'.", tokens[i]);
80 b = strtol(range[0], &eptr, 10);
81 if (eptr == range[0] || *eptr != '\0') {
82 g_critical("Invalid channel '%s'.", range[0]);
86 e = strtol(range[1], NULL, 10);
87 if (eptr == range[1] || *eptr != '\0') {
88 g_critical("Invalid channel '%s'.", range[1]);
92 if (b < 0 || b >= e) {
93 g_critical("Invalid channel range '%s'.", tokens[i]);
99 n = snprintf(str, 8, "%d", b);
100 if (n < 0 || n > 8) {
101 g_critical("Invalid channel '%d'.", b);
105 ch = find_channel(sdi->channels, str);
107 g_critical("unknown channel '%d'.", b);
111 channellist = g_slist_append(channellist, ch);
121 names = g_strsplit(tokens[i], "=", 2);
122 if (!names[0] || (names[1] && names[2])) {
123 /* Need one or two arguments. */
124 g_critical("Invalid channel '%s'.", tokens[i]);
130 ch = find_channel(sdi->channels, names[0]);
132 g_critical("unknown channel '%s'.", names[0]);
138 /* Rename channel. */
140 ch->name = g_strdup(names[1]);
142 channellist = g_slist_append(channellist, ch);
149 g_slist_free(channellist);
158 int parse_trigger_match(char c)
163 match = SR_TRIGGER_ZERO;
165 match = SR_TRIGGER_ONE;
167 match = SR_TRIGGER_RISING;
169 match = SR_TRIGGER_FALLING;
171 match = SR_TRIGGER_EDGE;
173 match = SR_TRIGGER_OVER;
175 match = SR_TRIGGER_UNDER;
182 int parse_triggerstring(const struct sr_dev_inst *sdi, const char *s,
183 struct sr_trigger **trigger)
185 struct sr_channel *ch;
186 struct sr_trigger_stage *stage;
190 gboolean found_match, error;
191 const int32_t *matches;
197 if (sr_config_list(sdi->driver, sdi, NULL, SR_CONF_TRIGGER_MATCH,
199 g_critical("Device doesn't support any triggers.");
202 matches = g_variant_get_fixed_array(gvar, &num_matches, sizeof(int32_t));
204 *trigger = sr_trigger_new(NULL);
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]);
215 for (l = sdi->channels; l; l = l->next) {
217 if (ch->enabled && !strcmp(ch->name, tokens[i]))
222 g_critical("Invalid channel '%s'.", tokens[i]);
226 for (t = 0; sep[t]; t++) {
227 if (!(match = parse_trigger_match(sep[t]))) {
228 g_critical("Invalid trigger match '%c'.", sep[t]);
233 for (j = 0; j < num_matches; j++) {
234 if (matches[j] == match) {
240 g_critical("Trigger match '%c' not supported by device.", sep[t]);
244 /* Make sure this ends up in the right stage, creating
246 while (!(stage = g_slist_nth_data((*trigger)->stages, t)))
247 sr_trigger_stage_add(*trigger);
248 if (sr_trigger_match_add(stage, ch, match, 0) != SR_OK) {
255 g_variant_unref(gvar);
258 sr_trigger_free(*trigger);
263 GHashTable *parse_generic_arg(const char *arg, gboolean sep_first)
273 hash = g_hash_table_new_full(g_str_hash, g_str_equal,
275 elements = g_strsplit(arg, ":", 0);
277 g_hash_table_insert(hash, g_strdup("sigrok_key"),
278 g_strdup(elements[i++]));
279 for (; elements[i]; i++) {
280 e = strchr(elements[i], '=');
282 g_hash_table_insert(hash, g_strdup(elements[i]), NULL);
285 g_hash_table_insert(hash, g_strdup(elements[i]), g_strdup(e));
288 g_strfreev(elements);
293 GHashTable *generic_arg_to_opt(const struct sr_option **opts, GHashTable *genargs)
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)))
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));
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));
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));
326 } else if (g_variant_is_of_type(opts[i]->def, G_VARIANT_TYPE_BOOLEAN)) {
328 if (0 == strcmp(s, "false") || 0 == strcmp(s, "no")) {
330 } else if (!(0 == strcmp(s, "true") || 0 == strcmp(s, "yes"))) {
331 g_critical("Unable to convert '%s' to boolean!", s);
334 gvar = g_variant_new_boolean(b);
335 g_hash_table_insert(hash, g_strdup(opts[i]->id),
336 g_variant_ref_sink(gvar));
338 g_critical("Don't know GVariant type for option '%s'!", opts[i]->id);
345 static char *strcanon(const char *str)
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'))
362 int canon_cmp(const char *str1, const char *str2)
369 ret = g_ascii_strcasecmp(s1, s2);
376 /* Convert driver options hash to GSList of struct sr_config. */
377 static GSList *hash_to_hwopt(GHashTable *hash)
379 struct sr_config *src;
384 keys = g_hash_table_get_keys(hash);
386 for (gl = keys; gl; gl = gl->next) {
388 src = g_malloc(sizeof(struct sr_config));
389 if (opt_to_gvar(key, g_hash_table_lookup(hash, key), src) != 0)
391 opts = g_slist_append(opts, src);
398 int parse_driver(char *arg, struct sr_dev_driver **driver, GSList **drvopts)
400 struct sr_dev_driver **drivers;
405 drvargs = parse_generic_arg(arg, TRUE);
407 drvname = g_strdup(g_hash_table_lookup(drvargs, "sigrok_key"));
408 g_hash_table_remove(drvargs, "sigrok_key");
410 drivers = sr_driver_list();
411 for (i = 0; drivers[i]; i++) {
412 if (strcmp(drivers[i]->name, drvname))
414 *driver = drivers[i];
417 g_critical("Driver %s not found.", drvname);
418 g_hash_table_destroy(drvargs);
423 if (sr_driver_init(sr_ctx, *driver) != SR_OK) {
424 g_critical("Failed to initialize driver.");
425 g_hash_table_destroy(drvargs);
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);
440 g_hash_table_destroy(drvargs);