]> sigrok.org Git - sigrok-cli.git/blame - decode.c
List supported devices, modules, decoders via --list-supported.
[sigrok-cli.git] / decode.c
CommitLineData
2be182e6
BV
1/*
2 * This file is part of the sigrok-cli project.
3 *
4 * Copyright (C) 2013 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
d486cbdd 20#include <config.h>
f0f54487
UH
21#include <stdlib.h>
22#include <string.h>
2be182e6 23#include <glib.h>
662a1e27 24#include "sigrok-cli.h"
2be182e6
BV
25
26#ifdef HAVE_SRD
27static GHashTable *pd_ann_visible = NULL;
28static GHashTable *pd_meta_visible = NULL;
29static GHashTable *pd_binary_visible = NULL;
3dfbfbc8 30static GHashTable *pd_channel_maps = NULL;
2be182e6
BV
31
32extern struct srd_session *srd_sess;
2be182e6
BV
33
34static int opts_to_gvar(struct srd_decoder *dec, GHashTable *hash,
35 GHashTable **options)
36{
37 struct srd_decoder_option *o;
38 GSList *optl;
39 GVariant *gvar;
40 gint64 val_int;
41 int ret;
42 char *val_str, *conv;
43
44 ret = TRUE;
45 *options = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
46 (GDestroyNotify)g_variant_unref);
47
48 for (optl = dec->options; optl; optl = optl->next) {
49 o = optl->data;
50 if (!(val_str = g_hash_table_lookup(hash, o->id)))
51 /* Not specified. */
52 continue;
53 if (g_variant_is_of_type(o->def, G_VARIANT_TYPE_STRING)) {
54 gvar = g_variant_new_string(val_str);
55 } else if (g_variant_is_of_type(o->def, G_VARIANT_TYPE_INT64)) {
56 val_int = strtoll(val_str, &conv, 0);
57 if (!conv || conv == val_str) {
58 g_critical("Protocol decoder '%s' option '%s' "
59 "requires a number.", dec->name, o->id);
60 ret = FALSE;
61 break;
62 }
63 gvar = g_variant_new_int64(val_int);
64 } else {
65 g_critical("Unsupported type for option '%s' (%s)",
66 o->id, g_variant_get_type_string(o->def));
67 ret = FALSE;
68 break;
69 }
70 g_variant_ref_sink(gvar);
71 g_hash_table_insert(*options, g_strdup(o->id), gvar);
72 g_hash_table_remove(hash, o->id);
73 }
74
75 return ret;
76}
77
39aedc34 78static int move_hash_element(GHashTable *src, GHashTable *dest, void *key)
2be182e6 79{
39aedc34
DE
80 void *orig_key, *value;
81
82 if (!g_hash_table_lookup_extended(src, key, &orig_key, &value))
83 /* Not specified. */
84 return FALSE;
85 g_hash_table_steal(src, orig_key);
86 g_hash_table_insert(dest, orig_key, value);
87
88 return TRUE;
89}
90
3dfbfbc8 91static GHashTable *extract_channel_map(struct srd_decoder *dec, GHashTable *hash)
39aedc34 92{
3dfbfbc8
UH
93 GHashTable *channel_map;
94 struct srd_channel *pdch;
39aedc34 95 GSList *l;
2be182e6 96
3dfbfbc8 97 channel_map = g_hash_table_new_full(g_str_hash, g_str_equal,
39aedc34 98 g_free, g_free);
2be182e6 99
3dfbfbc8
UH
100 for (l = dec->channels; l; l = l->next) {
101 pdch = l->data;
102 move_hash_element(hash, channel_map, pdch->id);
39aedc34 103 }
3dfbfbc8
UH
104 for (l = dec->opt_channels; l; l = l->next) {
105 pdch = l->data;
106 move_hash_element(hash, channel_map, pdch->id);
2be182e6 107 }
2be182e6 108
3dfbfbc8 109 return channel_map;
2be182e6
BV
110}
111
551f570c 112static int register_pd(char *opt_pds, char *opt_pd_annotations)
2be182e6 113{
551f570c 114 int ret;
2be182e6 115 struct srd_decoder *dec;
551f570c
KP
116 struct srd_decoder_inst *di, *di_prior;
117 char **pdtokens, **pdtok, *pd_name;
3dfbfbc8 118 GHashTable *pd_opthash, *options, *channels;
2be182e6 119 GList *leftover, *l;
2be182e6 120
2be182e6
BV
121 ret = 0;
122 pd_name = NULL;
551f570c
KP
123 di_prior = NULL;
124 pd_opthash = options = channels = NULL;
125
26c63758 126 pdtokens = g_strsplit(opt_pds, ",", 0);
2be182e6
BV
127 for (pdtok = pdtokens; *pdtok; pdtok++) {
128 if (!(pd_opthash = parse_generic_arg(*pdtok, TRUE))) {
129 g_critical("Invalid protocol decoder option '%s'.", *pdtok);
130 break;
131 }
132
133 pd_name = g_strdup(g_hash_table_lookup(pd_opthash, "sigrok_key"));
134 g_hash_table_remove(pd_opthash, "sigrok_key");
135 if (srd_decoder_load(pd_name) != SRD_OK) {
136 g_critical("Failed to load protocol decoder %s.", pd_name);
137 ret = 1;
138 break;
139 }
3a8ceb6a
BV
140 if (!(dec = srd_decoder_get_by_id(pd_name))) {
141 g_critical("Failed to get decoder %s by id.", pd_name);
142 ret = 1;
143 break;
144 }
2be182e6 145
3dfbfbc8 146 /* Convert decoder option and channel values to GVariant. */
2be182e6
BV
147 if (!opts_to_gvar(dec, pd_opthash, &options)) {
148 ret = 1;
149 break;
150 }
3dfbfbc8 151 channels = extract_channel_map(dec, pd_opthash);
39aedc34 152
2be182e6
BV
153 if (g_hash_table_size(pd_opthash) > 0) {
154 leftover = g_hash_table_get_keys(pd_opthash);
155 for (l = leftover; l; l = l->next)
3dfbfbc8 156 g_critical("Unknown option or channel '%s'", (char *)l->data);
2be182e6
BV
157 g_list_free(leftover);
158 break;
159 }
160
161 if (!(di = srd_inst_new(srd_sess, pd_name, options))) {
162 g_critical("Failed to instantiate protocol decoder %s.", pd_name);
163 ret = 1;
164 break;
165 }
166
09fea207 167 if (pdtok == pdtokens) {
3dfbfbc8
UH
168 /*
169 * Save the channel setup for later, but only on the
170 * first decoder (stacked decoders don't get channels).
171 */
3dfbfbc8
UH
172 g_hash_table_insert(pd_channel_maps, g_strdup(di->inst_id), channels);
173 channels = NULL;
09fea207 174 }
39aedc34 175
f0f54487
UH
176 /*
177 * If no annotation list was specified, add them all in now.
2be182e6
BV
178 * This will be pared down later to leave only the last PD
179 * in the stack.
180 */
551f570c 181 if (!opt_pd_annotations) {
10d4fc25 182 g_hash_table_insert(pd_ann_visible, g_strdup(di->decoder->id),
790b0261 183 g_slist_append(NULL, GINT_TO_POINTER(-1)));
551f570c
KP
184 }
185 if (di_prior) {
186 if (srd_inst_stack(srd_sess, di_prior, di) != SRD_OK) {
187 g_critical("Failed to stack %s -> %s.",
188 di_prior->inst_id, di->inst_id);
189 ret = 1;
190 break;
191 }
192 /* Remove annotations from prior levels. */
193 if (!opt_pd_annotations)
194 g_hash_table_remove(pd_ann_visible, di_prior->inst_id);
195 }
196 di_prior = di;
c5e0e72e
KP
197 g_free(pd_name);
198 g_hash_table_destroy(pd_opthash);
199 g_hash_table_destroy(options);
200 pd_opthash = options = NULL;
2be182e6
BV
201 }
202
2be182e6
BV
203 if (pd_opthash)
204 g_hash_table_destroy(pd_opthash);
205 if (options)
206 g_hash_table_destroy(options);
3dfbfbc8
UH
207 if (channels)
208 g_hash_table_destroy(channels);
551f570c
KP
209
210 g_strfreev(pdtokens);
2be182e6
BV
211
212 return ret;
213}
214
551f570c
KP
215/*
216 * Register all the PDs from all stacks.
217 *
218 * Each PD string is a single stack such as "uart:baudrate=19200,modbus".
219 */
220int register_pds(gchar **all_pds, char *opt_pd_annotations)
221{
222 int ret;
223
224 ret = 0;
225 pd_ann_visible = g_hash_table_new_full(g_str_hash, g_str_equal,
226 g_free, NULL);
227 pd_channel_maps = g_hash_table_new_full(g_str_hash,
228 g_str_equal, g_free, (GDestroyNotify)g_hash_table_destroy);
229
230 for (int i = 0; all_pds[i]; i++)
231 ret += register_pd(all_pds[i], opt_pd_annotations);
232
233 return ret;
234}
235
3dfbfbc8 236static void map_pd_inst_channels(void *key, void *value, void *user_data)
39aedc34 237{
3dfbfbc8
UH
238 GHashTable *channel_map;
239 GHashTable *channel_indices;
240 GSList *channel_list;
39aedc34
DE
241 struct srd_decoder_inst *di;
242 GVariant *var;
3dfbfbc8 243 void *channel_id;
029d73fe
UH
244 void *channel_target;
245 struct sr_channel *ch;
39aedc34 246 GHashTableIter iter;
39aedc34 247
3dfbfbc8
UH
248 channel_map = value;
249 channel_list = user_data;
39aedc34
DE
250
251 di = srd_inst_find_by_id(srd_sess, key);
252 if (!di) {
253 g_critical("Protocol decoder instance \"%s\" not found.",
254 (char *)key);
255 return;
256 }
3dfbfbc8 257 channel_indices = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
39aedc34
DE
258 (GDestroyNotify)g_variant_unref);
259
3dfbfbc8
UH
260 g_hash_table_iter_init(&iter, channel_map);
261 while (g_hash_table_iter_next(&iter, &channel_id, &channel_target)) {
262 ch = find_channel(channel_list, channel_target);
029d73fe
UH
263 if (!ch) {
264 g_printerr("cli: No channel with name \"%s\" found.\n",
265 (char *)channel_target);
39aedc34
DE
266 continue;
267 }
029d73fe
UH
268 if (!ch->enabled)
269 g_printerr("cli: Target channel \"%s\" not enabled.\n",
270 (char *)channel_target);
39aedc34 271
029d73fe 272 var = g_variant_new_int32(ch->index);
39aedc34 273 g_variant_ref_sink(var);
3dfbfbc8 274 g_hash_table_insert(channel_indices, g_strdup(channel_id), var);
39aedc34
DE
275 }
276
ee639fb4 277 srd_inst_channel_set_all(di, channel_indices);
c5e0e72e 278 g_hash_table_destroy(channel_indices);
39aedc34
DE
279}
280
3dfbfbc8 281void map_pd_channels(struct sr_dev_inst *sdi)
39aedc34 282{
c6fa2b2e
UH
283 GSList *channels;
284
285 channels = sr_dev_inst_channels_get(sdi);
286
3dfbfbc8
UH
287 if (pd_channel_maps) {
288 g_hash_table_foreach(pd_channel_maps, &map_pd_inst_channels,
c6fa2b2e 289 channels);
3dfbfbc8
UH
290 g_hash_table_destroy(pd_channel_maps);
291 pd_channel_maps = NULL;
39aedc34
DE
292 }
293}
294
26c63758 295int setup_pd_annotations(char *opt_pd_annotations)
2be182e6 296{
790b0261 297 GSList *l, *l_ann;
2be182e6
BV
298 struct srd_decoder *dec;
299 int ann_class;
790b0261 300 char **pds, **pdtok, **keyval, **annlist, **ann, **ann_descr;
2be182e6
BV
301
302 /* Set up custom list of PDs and annotations to show. */
303 pds = g_strsplit(opt_pd_annotations, ",", 0);
304 for (pdtok = pds; *pdtok && **pdtok; pdtok++) {
305 keyval = g_strsplit(*pdtok, "=", 0);
306 if (!(dec = srd_decoder_get_by_id(keyval[0]))) {
307 g_critical("Protocol decoder '%s' not found.", keyval[0]);
308 return 1;
309 }
310 if (!dec->annotations) {
311 g_critical("Protocol decoder '%s' has no annotations.", keyval[0]);
312 return 1;
313 }
790b0261
BV
314 if (g_strv_length(keyval) == 2 && keyval[1][0] != '\0') {
315 annlist = g_strsplit(keyval[1], ":", 0);
316 for (ann = annlist; *ann && **ann; ann++) {
317 ann_class = 0;
318 for (l = dec->annotations; l; l = l->next, ann_class++) {
319 ann_descr = l->data;
320 if (!canon_cmp(ann_descr[0], *ann))
321 /* Found it. */
322 break;
323 }
324 if (!l) {
325 g_critical("Annotation '%s' not found "
326 "for protocol decoder '%s'.", *ann, keyval[0]);
327 return 1;
328 }
329 l_ann = g_hash_table_lookup(pd_ann_visible, keyval[0]);
330 l_ann = g_slist_append(l_ann, GINT_TO_POINTER(ann_class));
331 g_hash_table_replace(pd_ann_visible, g_strdup(keyval[0]), l_ann);
332 g_debug("cli: Showing protocol decoder %s annotation "
333 "class %d (%s).", keyval[0], ann_class, ann_descr[0]);
2be182e6 334 }
2be182e6
BV
335 } else {
336 /* No class specified: show all of them. */
790b0261
BV
337 g_hash_table_insert(pd_ann_visible, g_strdup(keyval[0]),
338 g_slist_append(NULL, GINT_TO_POINTER(-1)));
2be182e6
BV
339 g_debug("cli: Showing all annotation classes for protocol "
340 "decoder %s.", keyval[0]);
341 }
2be182e6
BV
342 g_strfreev(keyval);
343 }
344 g_strfreev(pds);
345
346 return 0;
347}
348
26c63758 349int setup_pd_meta(char *opt_pd_meta)
2be182e6
BV
350{
351 struct srd_decoder *dec;
352 char **pds, **pdtok;
353
354 pd_meta_visible = g_hash_table_new_full(g_str_hash, g_int_equal,
355 g_free, NULL);
356 pds = g_strsplit(opt_pd_meta, ",", 0);
357 for (pdtok = pds; *pdtok && **pdtok; pdtok++) {
358 if (!(dec = srd_decoder_get_by_id(*pdtok))) {
359 g_critical("Protocol decoder '%s' not found.", *pdtok);
360 return 1;
361 }
362 g_debug("cli: Showing protocol decoder meta output from '%s'.", *pdtok);
363 g_hash_table_insert(pd_meta_visible, g_strdup(*pdtok), NULL);
364 }
365 g_strfreev(pds);
366
367 return 0;
368}
369
26c63758 370int setup_pd_binary(char *opt_pd_binary)
2be182e6
BV
371{
372 GSList *l;
373 struct srd_decoder *dec;
374 int bin_class;
5ff52594 375 char **pds, **pdtok, **keyval, **bin_name;
2be182e6
BV
376
377 pd_binary_visible = g_hash_table_new_full(g_str_hash, g_int_equal,
378 g_free, NULL);
379 pds = g_strsplit(opt_pd_binary, ",", 0);
380 for (pdtok = pds; *pdtok && **pdtok; pdtok++) {
381 keyval = g_strsplit(*pdtok, "=", 0);
382 if (!(dec = srd_decoder_get_by_id(keyval[0]))) {
383 g_critical("Protocol decoder '%s' not found.", keyval[0]);
384 return 1;
385 }
386 if (!dec->binary) {
387 g_critical("Protocol decoder '%s' has no binary output.", keyval[0]);
388 return 1;
389 }
390 bin_class = 0;
391 if (g_strv_length(keyval) == 2) {
392 for (l = dec->binary; l; l = l->next, bin_class++) {
393 bin_name = l->data;
5ff52594 394 if (!strcmp(bin_name[0], keyval[1]))
2be182e6
BV
395 /* Found it. */
396 break;
397 }
398 if (!l) {
399 g_critical("binary output '%s' not found "
400 "for protocol decoder '%s'.", keyval[1], keyval[0]);
401 return 1;
402 }
403 g_debug("cli: Showing protocol decoder %s binary class "
5ff52594 404 "%d (%s).", keyval[0], bin_class, bin_name[0]);
2be182e6
BV
405 } else {
406 /* No class specified: output all of them. */
407 bin_class = -1;
408 g_debug("cli: Showing all binary classes for protocol "
409 "decoder %s.", keyval[0]);
410 }
411 g_hash_table_insert(pd_binary_visible, g_strdup(keyval[0]), GINT_TO_POINTER(bin_class));
412 g_strfreev(keyval);
413 }
414 g_strfreev(pds);
415
416 return 0;
417}
418
419void show_pd_annotations(struct srd_proto_data *pdata, void *cb_data)
420{
6fdcc67d 421 struct srd_decoder *dec;
2be182e6 422 struct srd_proto_data_annotation *pda;
790b0261
BV
423 GSList *ann_list, *l;
424 int i;
6fdcc67d 425 char **ann_descr;
790b0261 426 gboolean show;
2be182e6 427
2be182e6
BV
428 (void)cb_data;
429
430 if (!pd_ann_visible)
431 return;
432
10d4fc25
KP
433 if (!g_hash_table_lookup_extended(pd_ann_visible, pdata->pdo->di->decoder->id,
434 NULL, (void **)&ann_list)) {
2be182e6
BV
435 /* Not in the list of PDs whose annotations we're showing. */
436 return;
10d4fc25 437 }
2be182e6 438
6fdcc67d 439 dec = pdata->pdo->di->decoder;
2be182e6 440 pda = pdata->data;
790b0261
BV
441 show = FALSE;
442 for (l = ann_list; l; l = l->next) {
443 if (GPOINTER_TO_INT(l->data) == -1
a23105b1 444 || GPOINTER_TO_INT(l->data) == pda->ann_class) {
790b0261
BV
445 show = TRUE;
446 break;
447 }
448 }
449 if (!show)
2be182e6
BV
450 return;
451
6fdcc67d
BV
452 if (opt_loglevel <= SR_LOG_WARN) {
453 /* Show only the longest annotation. */
e9e4dc4d 454 printf("%s: %s", pdata->pdo->proto_id, pda->ann_text[0]);
6fdcc67d
BV
455 } else if (opt_loglevel >= SR_LOG_INFO) {
456 /* Sample numbers and quotes around the longest annotation. */
6d1dbe35 457 printf("%"PRIu64"-%"PRIu64"", pdata->start_sample, pdata->end_sample);
6fdcc67d 458 if (opt_loglevel == SR_LOG_INFO) {
e9e4dc4d 459 printf(" %s \"%s\"", pdata->pdo->proto_id, pda->ann_text[0]);
6fdcc67d
BV
460 } else {
461 /* Protocol decoder id, annotation class,
462 * all annotation strings. */
a23105b1 463 ann_descr = g_slist_nth_data(dec->annotations, pda->ann_class);
6d1dbe35 464 printf(" %s: %s:", pdata->pdo->proto_id, ann_descr[0]);
6fdcc67d 465 for (i = 0; pda->ann_text[i]; i++)
6d1dbe35 466 printf(" \"%s\"", pda->ann_text[i]);
6fdcc67d
BV
467 }
468 }
2be182e6
BV
469 printf("\n");
470 fflush(stdout);
471}
472
473void show_pd_meta(struct srd_proto_data *pdata, void *cb_data)
474{
2be182e6
BV
475 (void)cb_data;
476
477 if (!g_hash_table_lookup_extended(pd_meta_visible,
478 pdata->pdo->di->decoder->id, NULL, NULL))
479 /* Not in the list of PDs whose meta output we're showing. */
480 return;
481
482 if (opt_loglevel > SR_LOG_WARN)
483 printf("%"PRIu64"-%"PRIu64" ", pdata->start_sample, pdata->end_sample);
484 printf("%s: ", pdata->pdo->proto_id);
485 printf("%s: %s", pdata->pdo->meta_name, g_variant_print(pdata->data, FALSE));
486 printf("\n");
487 fflush(stdout);
488}
489
490void show_pd_binary(struct srd_proto_data *pdata, void *cb_data)
491{
492 struct srd_proto_data_binary *pdb;
493 gpointer classp;
494 int class;
495
2be182e6
BV
496 (void)cb_data;
497
498 if (!g_hash_table_lookup_extended(pd_binary_visible,
499 pdata->pdo->di->decoder->id, NULL, (void **)&classp))
500 /* Not in the list of PDs whose meta output we're showing. */
501 return;
502
503 class = GPOINTER_TO_INT(classp);
504 pdb = pdata->data;
505 if (class != -1 && class != pdb->bin_class)
506 /* Not showing this binary class. */
507 return;
508
509 /* Just send the binary output to stdout, no embellishments. */
510 fwrite(pdb->data, pdb->size, 1, stdout);
511 fflush(stdout);
512}
513#endif