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
;
48 GSList
*channellist
, *channels
;
50 char **tokens
, **range
, **names
, *eptr
, str
[8];
52 channels
= sr_dev_inst_channels_get(sdi
);
54 if (!channelstring
|| !channelstring
[0])
55 /* Use all channels by default. */
56 return g_slist_copy(channels
);
62 tokens
= g_strsplit(channelstring
, ",", 0);
63 for (i
= 0; tokens
[i
]; i
++) {
64 if (tokens
[i
][0] == '\0') {
65 g_critical("Invalid empty channel.");
69 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
74 range
= g_strsplit(tokens
[i
], "-", 2);
75 if (!range
[0] || !range
[1] || range
[2]) {
76 /* Need exactly two arguments. */
77 g_critical("Invalid channel syntax '%s'.", tokens
[i
]);
82 b
= strtol(range
[0], &eptr
, 10);
83 if (eptr
== range
[0] || *eptr
!= '\0') {
84 g_critical("Invalid channel '%s'.", range
[0]);
88 e
= strtol(range
[1], NULL
, 10);
89 if (eptr
== range
[1] || *eptr
!= '\0') {
90 g_critical("Invalid channel '%s'.", range
[1]);
94 if (b
< 0 || b
>= e
) {
95 g_critical("Invalid channel range '%s'.", tokens
[i
]);
101 n
= snprintf(str
, 8, "%d", b
);
102 if (n
< 0 || n
> 8) {
103 g_critical("Invalid channel '%d'.", b
);
107 ch
= find_channel(channels
, str
);
109 g_critical("unknown channel '%d'.", b
);
113 channellist
= g_slist_append(channellist
, ch
);
123 names
= g_strsplit(tokens
[i
], "=", 2);
124 if (!names
[0] || (names
[1] && names
[2])) {
125 /* Need one or two arguments. */
126 g_critical("Invalid channel '%s'.", tokens
[i
]);
132 ch
= find_channel(channels
, names
[0]);
134 g_critical("unknown channel '%s'.", names
[0]);
140 /* Rename channel. */
142 ch
->name
= g_strdup(names
[1]);
144 channellist
= g_slist_append(channellist
, ch
);
151 g_slist_free(channellist
);
160 int parse_trigger_match(char c
)
165 match
= SR_TRIGGER_ZERO
;
167 match
= SR_TRIGGER_ONE
;
169 match
= SR_TRIGGER_RISING
;
171 match
= SR_TRIGGER_FALLING
;
173 match
= SR_TRIGGER_EDGE
;
175 match
= SR_TRIGGER_OVER
;
177 match
= SR_TRIGGER_UNDER
;
184 int parse_triggerstring(const struct sr_dev_inst
*sdi
, const char *s
,
185 struct sr_trigger
**trigger
)
187 struct sr_channel
*ch
;
188 struct sr_trigger_stage
*stage
;
190 GSList
*l
, *channels
;
192 gboolean found_match
, error
;
193 const int32_t *matches
;
198 struct sr_dev_driver
*driver
;
200 driver
= sr_dev_inst_driver_get(sdi
);
201 channels
= sr_dev_inst_channels_get(sdi
);
203 if (sr_config_list(driver
, sdi
, NULL
, SR_CONF_TRIGGER_MATCH
,
205 g_critical("Device doesn't support any triggers.");
208 matches
= g_variant_get_fixed_array(gvar
, &num_matches
, sizeof(int32_t));
210 *trigger
= sr_trigger_new(NULL
);
212 tokens
= g_strsplit(s
, ",", -1);
213 for (i
= 0; tokens
[i
]; i
++) {
214 if (!(sep
= strchr(tokens
[i
], '='))) {
215 g_critical("Invalid trigger '%s'.", tokens
[i
]);
221 for (l
= channels
; l
; l
= l
->next
) {
223 if (ch
->enabled
&& !strcmp(ch
->name
, tokens
[i
]))
228 g_critical("Invalid channel '%s'.", tokens
[i
]);
232 for (t
= 0; sep
[t
]; t
++) {
233 if (!(match
= parse_trigger_match(sep
[t
]))) {
234 g_critical("Invalid trigger match '%c'.", sep
[t
]);
239 for (j
= 0; j
< num_matches
; j
++) {
240 if (matches
[j
] == match
) {
246 g_critical("Trigger match '%c' not supported by device.", sep
[t
]);
250 /* Make sure this ends up in the right stage, creating
252 while (!(stage
= g_slist_nth_data((*trigger
)->stages
, t
)))
253 sr_trigger_stage_add(*trigger
);
254 if (sr_trigger_match_add(stage
, ch
, match
, 0) != SR_OK
) {
261 g_variant_unref(gvar
);
264 sr_trigger_free(*trigger
);
269 GHashTable
*parse_generic_arg(const char *arg
, gboolean sep_first
)
279 hash
= g_hash_table_new_full(g_str_hash
, g_str_equal
,
281 elements
= g_strsplit(arg
, ":", 0);
283 g_hash_table_insert(hash
, g_strdup("sigrok_key"),
284 g_strdup(elements
[i
++]));
285 for (; elements
[i
]; i
++) {
286 e
= strchr(elements
[i
], '=');
288 g_hash_table_insert(hash
, g_strdup(elements
[i
]), NULL
);
291 g_hash_table_insert(hash
, g_strdup(elements
[i
]), g_strdup(e
));
294 g_strfreev(elements
);
299 GHashTable
*generic_arg_to_opt(const struct sr_option
**opts
, GHashTable
*genargs
)
307 hash
= g_hash_table_new_full(g_str_hash
, g_str_equal
, g_free
,
308 (GDestroyNotify
)g_variant_unref
);
309 for (i
= 0; opts
[i
]; i
++) {
310 if (!(s
= g_hash_table_lookup(genargs
, opts
[i
]->id
)))
312 if (g_variant_is_of_type(opts
[i
]->def
, G_VARIANT_TYPE_UINT32
)) {
313 gvar
= g_variant_new_uint32(strtoul(s
, NULL
, 10));
314 g_hash_table_insert(hash
, g_strdup(opts
[i
]->id
),
315 g_variant_ref_sink(gvar
));
316 } else if (g_variant_is_of_type(opts
[i
]->def
, G_VARIANT_TYPE_INT32
)) {
317 gvar
= g_variant_new_int32(strtoul(s
, NULL
, 10));
318 g_hash_table_insert(hash
, g_strdup(opts
[i
]->id
),
319 g_variant_ref_sink(gvar
));
320 } else if (g_variant_is_of_type(opts
[i
]->def
, G_VARIANT_TYPE_UINT64
)) {
321 gvar
= g_variant_new_uint64(strtoul(s
, NULL
, 10));
322 g_hash_table_insert(hash
, g_strdup(opts
[i
]->id
),
323 g_variant_ref_sink(gvar
));
324 } else if (g_variant_is_of_type(opts
[i
]->def
, G_VARIANT_TYPE_DOUBLE
)) {
325 gvar
= g_variant_new_double(strtod(s
, NULL
));
326 g_hash_table_insert(hash
, g_strdup(opts
[i
]->id
),
327 g_variant_ref_sink(gvar
));
328 } else if (g_variant_is_of_type(opts
[i
]->def
, G_VARIANT_TYPE_STRING
)) {
329 gvar
= g_variant_new_string(s
);
330 g_hash_table_insert(hash
, g_strdup(opts
[i
]->id
),
331 g_variant_ref_sink(gvar
));
332 } else if (g_variant_is_of_type(opts
[i
]->def
, G_VARIANT_TYPE_BOOLEAN
)) {
334 if (0 == strcmp(s
, "false") || 0 == strcmp(s
, "no")) {
336 } else if (!(0 == strcmp(s
, "true") || 0 == strcmp(s
, "yes"))) {
337 g_critical("Unable to convert '%s' to boolean!", s
);
340 gvar
= g_variant_new_boolean(b
);
341 g_hash_table_insert(hash
, g_strdup(opts
[i
]->id
),
342 g_variant_ref_sink(gvar
));
344 g_critical("Don't know GVariant type for option '%s'!", opts
[i
]->id
);
351 static char *strcanon(const char *str
)
356 /* Returns newly allocated string. */
357 s
= g_ascii_strdown(str
, -1);
358 for (p0
= p1
= 0; str
[p0
]; p0
++) {
359 if ((s
[p0
] >= 'a' && s
[p0
] <= 'z')
360 || (s
[p0
] >= '0' && s
[p0
] <= '9'))
368 int canon_cmp(const char *str1
, const char *str2
)
375 ret
= g_ascii_strcasecmp(s1
, s2
);
382 /* Convert driver options hash to GSList of struct sr_config. */
383 static GSList
*hash_to_hwopt(GHashTable
*hash
)
385 struct sr_config
*src
;
390 keys
= g_hash_table_get_keys(hash
);
392 for (gl
= keys
; gl
; gl
= gl
->next
) {
394 src
= g_malloc(sizeof(struct sr_config
));
395 if (opt_to_gvar(key
, g_hash_table_lookup(hash
, key
), src
) != 0)
397 opts
= g_slist_append(opts
, src
);
404 int parse_driver(char *arg
, struct sr_dev_driver
**driver
, GSList
**drvopts
)
406 struct sr_dev_driver
**drivers
;
411 drvargs
= parse_generic_arg(arg
, TRUE
);
413 drvname
= g_strdup(g_hash_table_lookup(drvargs
, "sigrok_key"));
414 g_hash_table_remove(drvargs
, "sigrok_key");
416 drivers
= sr_driver_list();
417 for (i
= 0; drivers
[i
]; i
++) {
418 if (strcmp(drivers
[i
]->name
, drvname
))
420 *driver
= drivers
[i
];
423 g_critical("Driver %s not found.", drvname
);
424 g_hash_table_destroy(drvargs
);
429 if (sr_driver_init(sr_ctx
, *driver
) != SR_OK
) {
430 g_critical("Failed to initialize driver.");
431 g_hash_table_destroy(drvargs
);
437 if (g_hash_table_size(drvargs
) > 0) {
438 if (!(*drvopts
= hash_to_hwopt(drvargs
))) {
439 /* Unknown options, already logged. */
440 g_hash_table_destroy(drvargs
);
446 g_hash_table_destroy(drvargs
);