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 struct sr_channel *find_channel(GSList *channellist, const char *channelname)
29 struct sr_channel *ch;
33 for (l = channellist; l; l = l->next) {
35 if (!strcmp(ch->name, channelname))
38 ch = l ? l->data : NULL;
43 GSList *parse_channelstring(struct sr_dev_inst *sdi, const char *channelstring)
45 struct sr_channel *ch;
48 char **tokens, **range, **names, *eptr, str[8];
50 if (!channelstring || !channelstring[0])
51 /* Use all channels by default. */
52 return g_slist_copy(sdi->channels);
58 tokens = g_strsplit(channelstring, ",", 0);
59 for (i = 0; tokens[i]; i++) {
60 if (tokens[i][0] == '\0') {
61 g_critical("Invalid empty channel.");
65 if (strchr(tokens[i], '-')) {
66 /* A range of channels in the form a-b. This will only work
67 * if the channels are named as numbers -- so every channel
68 * in the range must exist as a channel name string in the
70 range = g_strsplit(tokens[i], "-", 2);
71 if (!range[0] || !range[1] || range[2]) {
72 /* Need exactly two arguments. */
73 g_critical("Invalid channel syntax '%s'.", tokens[i]);
78 b = strtol(range[0], &eptr, 10);
79 if (eptr == range[0] || *eptr != '\0') {
80 g_critical("Invalid channel '%s'.", range[0]);
84 e = strtol(range[1], NULL, 10);
85 if (eptr == range[1] || *eptr != '\0') {
86 g_critical("Invalid channel '%s'.", range[1]);
90 if (b < 0 || b >= e) {
91 g_critical("Invalid channel range '%s'.", tokens[i]);
97 n = snprintf(str, 8, "%d", b);
99 g_critical("Invalid channel '%d'.", b);
103 ch = find_channel(sdi->channels, str);
105 g_critical("unknown channel '%d'.", b);
109 channellist = g_slist_append(channellist, ch);
119 names = g_strsplit(tokens[i], "=", 2);
120 if (!names[0] || (names[1] && names[2])) {
121 /* Need one or two arguments. */
122 g_critical("Invalid channel '%s'.", tokens[i]);
128 ch = find_channel(sdi->channels, names[0]);
130 g_critical("unknown channel '%s'.", names[0]);
136 /* Rename channel. */
138 ch->name = g_strdup(names[1]);
140 channellist = g_slist_append(channellist, ch);
147 g_slist_free(channellist);
156 int parse_trigger_match(char c)
161 match = SR_TRIGGER_ZERO;
163 match = SR_TRIGGER_ONE;
165 match = SR_TRIGGER_RISING;
167 match = SR_TRIGGER_FALLING;
169 match = SR_TRIGGER_EDGE;
171 match = SR_TRIGGER_OVER;
173 match = SR_TRIGGER_UNDER;
180 int parse_triggerstring(const struct sr_dev_inst *sdi, const char *s,
181 struct sr_trigger **trigger)
183 struct sr_channel *ch;
184 struct sr_trigger_stage *stage;
188 gboolean found_match, error;
189 const int32_t *matches;
195 if (sr_config_list(sdi->driver, sdi, NULL, SR_CONF_TRIGGER_MATCH,
197 g_critical("Device doesn't support any triggers.");
200 matches = g_variant_get_fixed_array(gvar, &num_matches, sizeof(int32_t));
202 *trigger = sr_trigger_new(NULL);
204 tokens = g_strsplit(s, ",", -1);
205 for (i = 0; tokens[i]; i++) {
206 if (!(sep = strchr(tokens[i], '='))) {
207 g_critical("Invalid trigger '%s'.", tokens[i]);
213 for (l = sdi->channels; l; l = l->next) {
215 if (ch->enabled && !strcmp(ch->name, tokens[i]))
220 g_critical("Invalid channel '%s'.", tokens[i]);
224 for (t = 0; sep[t]; t++) {
225 if (!(match = parse_trigger_match(sep[t]))) {
226 g_critical("Invalid trigger match '%c'.", sep[t]);
231 for (j = 0; j < num_matches; j++) {
232 if (matches[j] == match) {
238 g_critical("Trigger match '%c' not supported by device.", sep[t]);
242 /* Make sure this ends up in the right stage, creating
244 while (!(stage = g_slist_nth_data((*trigger)->stages, t)))
245 sr_trigger_stage_add(*trigger);
246 if (sr_trigger_match_add(stage, ch, match, 0) != SR_OK) {
253 g_variant_unref(gvar);
256 sr_trigger_free(*trigger);
261 GHashTable *parse_generic_arg(const char *arg, gboolean sep_first)
271 hash = g_hash_table_new_full(g_str_hash, g_str_equal,
273 elements = g_strsplit(arg, ":", 0);
275 g_hash_table_insert(hash, g_strdup("sigrok_key"),
276 g_strdup(elements[i++]));
277 for (; elements[i]; i++) {
278 e = strchr(elements[i], '=');
280 g_hash_table_insert(hash, g_strdup(elements[i]), NULL);
283 g_hash_table_insert(hash, g_strdup(elements[i]), g_strdup(e));
286 g_strfreev(elements);
291 GHashTable *generic_arg_to_opt(const struct sr_option **opts, GHashTable *genargs)
298 hash = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
299 (GDestroyNotify)g_variant_unref);
300 for (i = 0; opts[i]; i++) {
301 if (!(s = g_hash_table_lookup(genargs, opts[i]->id)))
303 if (g_variant_is_of_type(opts[i]->def, G_VARIANT_TYPE_UINT32)) {
304 gvar = g_variant_new_uint32(strtoul(s, NULL, 10));
305 g_hash_table_insert(hash, g_strdup(opts[i]->id),
306 g_variant_ref_sink(gvar));
307 } else if (g_variant_is_of_type(opts[i]->def, G_VARIANT_TYPE_INT32)) {
308 gvar = g_variant_new_int32(strtoul(s, NULL, 10));
309 g_hash_table_insert(hash, g_strdup(opts[i]->id),
310 g_variant_ref_sink(gvar));
311 } else if (g_variant_is_of_type(opts[i]->def, G_VARIANT_TYPE_UINT64)) {
312 gvar = g_variant_new_uint64(strtoul(s, NULL, 10));
313 g_hash_table_insert(hash, g_strdup(opts[i]->id),
314 g_variant_ref_sink(gvar));
315 } else if (g_variant_is_of_type(opts[i]->def, G_VARIANT_TYPE_DOUBLE)) {
316 gvar = g_variant_new_double(strtod(s, NULL));
317 g_hash_table_insert(hash, g_strdup(opts[i]->id),
318 g_variant_ref_sink(gvar));
319 } else if (g_variant_is_of_type(opts[i]->def, G_VARIANT_TYPE_STRING)) {
320 gvar = g_variant_new_string(s);
321 g_hash_table_insert(hash, g_strdup(opts[i]->id),
322 g_variant_ref_sink(gvar));
324 g_critical("Don't know GVariant type for option '%s'!", opts[i]->id);
331 static char *strcanon(const char *str)
336 /* Returns newly allocated string. */
337 s = g_ascii_strdown(str, -1);
338 for (p0 = p1 = 0; str[p0]; p0++) {
339 if ((s[p0] >= 'a' && s[p0] <= 'z')
340 || (s[p0] >= '0' && s[p0] <= '9'))
348 int canon_cmp(const char *str1, const char *str2)
355 ret = g_ascii_strcasecmp(s1, s2);