doc: update manpage, mention -A and annotation rows
[sigrok-cli.git] / decode.c
blob225162a45e4cde1238da788ec4e5c8754bd91f93
1 /*
2 * This file is part of the sigrok-cli project.
4 * Copyright (C) 2013 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 <config.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <glib.h>
24 #include "sigrok-cli.h"
26 #ifdef HAVE_SRD
27 static GHashTable *pd_ann_visible = NULL;
28 static GHashTable *pd_meta_visible = NULL;
29 static GHashTable *pd_binary_visible = NULL;
30 static GHashTable *pd_channel_maps = NULL;
32 extern struct srd_session *srd_sess;
34 static int opts_to_gvar(struct srd_decoder *dec, GHashTable *hash,
35 GHashTable **options)
37 struct srd_decoder_option *o;
38 GSList *optl;
39 GVariant *gvar;
40 gint64 val_int;
41 double val_dbl;
42 int ret;
43 char *val_str, *conv;
45 ret = TRUE;
46 *options = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
47 (GDestroyNotify)g_variant_unref);
49 for (optl = dec->options; optl; optl = optl->next) {
50 o = optl->data;
51 if (!(val_str = g_hash_table_lookup(hash, o->id)))
52 /* Not specified. */
53 continue;
54 if (g_variant_is_of_type(o->def, G_VARIANT_TYPE_STRING)) {
55 gvar = g_variant_new_string(val_str);
56 } else if (g_variant_is_of_type(o->def, G_VARIANT_TYPE_INT64)) {
57 conv = NULL;
58 val_int = strtoll(val_str, &conv, 0);
59 if (!conv || conv == val_str || *conv) {
60 g_critical("Protocol decoder '%s' option '%s' "
61 "requires a number.", dec->name, o->id);
62 ret = FALSE;
63 break;
65 gvar = g_variant_new_int64(val_int);
66 } else if (g_variant_is_of_type(o->def, G_VARIANT_TYPE_DOUBLE)) {
67 conv = NULL;
68 val_dbl = strtod(val_str, &conv);
69 if (!conv || conv == val_str || *conv) {
70 g_critical("Protocol decoder '%s' option '%s' requires a float number.",
71 dec->name, o->id);
72 ret = FALSE;
73 break;
75 gvar = g_variant_new_double(val_dbl);
76 } else {
77 g_critical("Unsupported type for option '%s' (%s)",
78 o->id, g_variant_get_type_string(o->def));
79 ret = FALSE;
80 break;
82 g_variant_ref_sink(gvar);
83 g_hash_table_insert(*options, g_strdup(o->id), gvar);
84 g_hash_table_remove(hash, o->id);
87 return ret;
90 static int move_hash_element(GHashTable *src, GHashTable *dest, void *key)
92 void *orig_key, *value;
94 if (!g_hash_table_lookup_extended(src, key, &orig_key, &value))
95 /* Not specified. */
96 return FALSE;
97 g_hash_table_steal(src, orig_key);
98 g_hash_table_insert(dest, orig_key, value);
100 return TRUE;
103 static GHashTable *extract_channel_map(struct srd_decoder *dec, GHashTable *hash)
105 GHashTable *channel_map;
106 struct srd_channel *pdch;
107 GSList *l;
109 channel_map = g_hash_table_new_full(g_str_hash, g_str_equal,
110 g_free, g_free);
112 for (l = dec->channels; l; l = l->next) {
113 pdch = l->data;
114 move_hash_element(hash, channel_map, pdch->id);
116 for (l = dec->opt_channels; l; l = l->next) {
117 pdch = l->data;
118 move_hash_element(hash, channel_map, pdch->id);
121 return channel_map;
124 static int register_pd(char *opt_pds, char *opt_pd_annotations)
126 int ret;
127 struct srd_decoder *dec;
128 struct srd_decoder_inst *di, *di_prior;
129 char **pdtokens, **pdtok, *pd_name;
130 GHashTable *pd_opthash, *options, *channels;
131 GList *leftover, *l;
133 ret = 0;
134 pd_name = NULL;
135 di_prior = NULL;
136 pd_opthash = options = channels = NULL;
138 pdtokens = g_strsplit(opt_pds, ",", 0);
139 for (pdtok = pdtokens; *pdtok; pdtok++) {
140 if (!(pd_opthash = parse_generic_arg(*pdtok, TRUE))) {
141 g_critical("Invalid protocol decoder option '%s'.", *pdtok);
142 break;
145 pd_name = g_strdup(g_hash_table_lookup(pd_opthash, "sigrok_key"));
146 g_hash_table_remove(pd_opthash, "sigrok_key");
147 if (srd_decoder_load(pd_name) != SRD_OK) {
148 g_critical("Failed to load protocol decoder %s.", pd_name);
149 ret = 1;
150 break;
152 if (!(dec = srd_decoder_get_by_id(pd_name))) {
153 g_critical("Failed to get decoder %s by id.", pd_name);
154 ret = 1;
155 break;
158 /* Convert decoder option and channel values to GVariant. */
159 if (!opts_to_gvar(dec, pd_opthash, &options)) {
160 ret = 1;
161 break;
163 channels = extract_channel_map(dec, pd_opthash);
165 if (g_hash_table_size(pd_opthash) > 0) {
166 leftover = g_hash_table_get_keys(pd_opthash);
167 for (l = leftover; l; l = l->next)
168 g_critical("Unknown option or channel '%s'", (char *)l->data);
169 g_list_free(leftover);
170 break;
173 if (!(di = srd_inst_new(srd_sess, pd_name, options))) {
174 g_critical("Failed to instantiate protocol decoder %s.", pd_name);
175 ret = 1;
176 break;
179 if (pdtok == pdtokens) {
181 * Save the channel setup for later, but only on the
182 * first decoder (stacked decoders don't get channels).
184 g_hash_table_insert(pd_channel_maps, g_strdup(di->inst_id), channels);
185 channels = NULL;
189 * If no annotation list was specified, add them all in now.
190 * This will be pared down later to leave only the last PD
191 * in the stack.
193 if (!opt_pd_annotations) {
194 g_hash_table_insert(pd_ann_visible, g_strdup(di->decoder->id),
195 g_slist_append(NULL, GINT_TO_POINTER(-1)));
197 if (di_prior) {
198 if (srd_inst_stack(srd_sess, di_prior, di) != SRD_OK) {
199 g_critical("Failed to stack %s -> %s.",
200 di_prior->inst_id, di->inst_id);
201 ret = 1;
202 break;
204 /* Remove annotations from prior levels. */
205 if (!opt_pd_annotations)
206 g_hash_table_remove(pd_ann_visible, di_prior->inst_id);
208 di_prior = di;
209 g_free(pd_name);
210 g_hash_table_destroy(pd_opthash);
211 g_hash_table_destroy(options);
212 pd_opthash = options = NULL;
215 if (pd_opthash)
216 g_hash_table_destroy(pd_opthash);
217 if (options)
218 g_hash_table_destroy(options);
219 if (channels)
220 g_hash_table_destroy(channels);
222 g_strfreev(pdtokens);
224 return ret;
228 * Register all the PDs from all stacks.
230 * Each PD string is a single stack such as "uart:baudrate=19200,modbus".
232 int register_pds(gchar **all_pds, char *opt_pd_annotations)
234 int ret;
236 ret = 0;
237 pd_ann_visible = g_hash_table_new_full(g_str_hash, g_str_equal,
238 g_free, NULL);
239 pd_channel_maps = g_hash_table_new_full(g_str_hash,
240 g_str_equal, g_free, (GDestroyNotify)g_hash_table_destroy);
242 for (int i = 0; all_pds[i]; i++)
243 ret += register_pd(all_pds[i], opt_pd_annotations);
245 return ret;
248 static void map_pd_inst_channels(void *key, void *value, void *user_data)
250 GHashTable *channel_map;
251 GHashTable *channel_indices;
252 GSList *channel_list;
253 struct srd_decoder_inst *di;
254 GVariant *var;
255 void *channel_id;
256 void *channel_target;
257 struct sr_channel *ch;
258 GHashTableIter iter;
260 channel_map = value;
261 channel_list = user_data;
263 di = srd_inst_find_by_id(srd_sess, key);
264 if (!di) {
265 g_critical("Protocol decoder instance \"%s\" not found.",
266 (char *)key);
267 return;
269 channel_indices = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
270 (GDestroyNotify)g_variant_unref);
272 g_hash_table_iter_init(&iter, channel_map);
273 while (g_hash_table_iter_next(&iter, &channel_id, &channel_target)) {
274 ch = find_channel(channel_list, channel_target);
275 if (!ch) {
276 g_printerr("cli: No channel with name \"%s\" found.\n",
277 (char *)channel_target);
278 continue;
280 if (!ch->enabled)
281 g_printerr("cli: Target channel \"%s\" not enabled.\n",
282 (char *)channel_target);
284 var = g_variant_new_int32(ch->index);
285 g_variant_ref_sink(var);
286 g_hash_table_insert(channel_indices, g_strdup(channel_id), var);
289 srd_inst_channel_set_all(di, channel_indices);
290 g_hash_table_destroy(channel_indices);
293 void map_pd_channels(struct sr_dev_inst *sdi)
295 GSList *channels;
297 channels = sr_dev_inst_channels_get(sdi);
299 if (pd_channel_maps) {
300 g_hash_table_foreach(pd_channel_maps, &map_pd_inst_channels,
301 channels);
302 g_hash_table_destroy(pd_channel_maps);
303 pd_channel_maps = NULL;
307 int setup_pd_annotations(char *opt_pd_annotations)
309 GSList *l, *l_ann;
310 struct srd_decoder *dec;
311 int ann_class;
312 char **pds, **pdtok, **keyval, **annlist, **ann, **ann_descr;
313 const char *dec_id;
314 const char *ann_txt;
315 const char *ann_id;
316 const struct srd_decoder_annotation_row *row_desc;
317 char **ann_diag;
319 /* Set up custom list of PDs and annotations to show. */
320 pds = g_strsplit(opt_pd_annotations, ",", 0);
321 for (pdtok = pds; *pdtok && **pdtok; pdtok++) {
322 keyval = g_strsplit(*pdtok, "=", 0);
323 dec_id = keyval[0];
324 if (!(dec = srd_decoder_get_by_id(dec_id))) {
325 g_critical("Protocol decoder '%s' not found.", dec_id);
326 g_strfreev(keyval);
327 g_strfreev(pds);
328 return 1;
330 if (!dec->annotations) {
331 g_critical("Protocol decoder '%s' has no annotations.", dec_id);
332 g_strfreev(keyval);
333 g_strfreev(pds);
334 return 1;
336 ann_txt = (g_strv_length(keyval) == 2) ? keyval[1] : NULL;
337 if (ann_txt && *ann_txt) {
338 annlist = g_strsplit(ann_txt, ":", 0);
339 for (ann = annlist; *ann && **ann; ann++) {
340 ann_id = *ann;
341 g_debug("cli: Lookup decoder %s annotation %s.", dec_id, ann_id);
342 /* Lookup annotation class. */
343 ann_class = 0;
344 for (l = dec->annotations; l; l = l->next, ann_class++) {
345 ann_descr = l->data;
346 if (!canon_cmp(ann_descr[0], ann_id))
347 /* Found it. */
348 break;
350 if (l) {
351 l_ann = g_hash_table_lookup(pd_ann_visible, dec_id);
352 l_ann = g_slist_append(l_ann, GINT_TO_POINTER(ann_class));
353 g_hash_table_replace(pd_ann_visible, g_strdup(dec_id), l_ann);
354 g_debug("cli: Showing protocol decoder %s annotation "
355 "class %d (%s).", dec_id, ann_class, ann_descr[0]);
356 continue;
358 /* Lookup annotation row. */
359 for (l = dec->annotation_rows; l; l = l->next) {
360 row_desc = l->data;
361 if (!canon_cmp(row_desc->id, ann_id))
362 break;
364 if (l) {
365 g_debug("cli: Showing decoder %s annotation row %s (%s).",
366 dec_id, row_desc->id, row_desc->desc);
367 l_ann = g_hash_table_lookup(pd_ann_visible, dec_id);
368 for (l = row_desc->ann_classes; l; l = l->next) {
370 * This could just be:
371 * l_ann = g_slist_append(l_ann, l->data);
372 * But we are explicit for readability
373 * and to access details for diagnostics.
375 ann_class = GPOINTER_TO_INT(l->data);
376 l_ann = g_slist_append(l_ann, GINT_TO_POINTER(ann_class));
377 ann_diag = g_slist_nth_data(dec->annotations, ann_class);
378 g_debug("cli: Adding class %d/%s from row %s.",
379 ann_class, ann_diag[0], row_desc->id);
381 g_hash_table_replace(pd_ann_visible, g_strdup(dec_id), l_ann);
382 continue;
384 /* No match found. */
385 g_critical("Annotation '%s' not found "
386 "for protocol decoder '%s'.", ann_id, dec_id);
387 g_strfreev(keyval);
388 g_strfreev(pds);
389 return 1;
391 } else {
392 /* No class specified: show all of them. */
393 ann_class = -1;
394 l_ann = g_slist_append(NULL, GINT_TO_POINTER(ann_class));
395 g_hash_table_insert(pd_ann_visible, g_strdup(dec_id), l_ann);
396 g_debug("cli: Showing all annotation classes for protocol "
397 "decoder %s.", dec_id);
399 g_strfreev(keyval);
401 g_strfreev(pds);
403 return 0;
406 int setup_pd_meta(char *opt_pd_meta)
408 struct srd_decoder *dec;
409 char **pds, **pdtok;
411 pd_meta_visible = g_hash_table_new_full(g_str_hash, g_int_equal,
412 g_free, NULL);
413 pds = g_strsplit(opt_pd_meta, ",", 0);
414 for (pdtok = pds; *pdtok && **pdtok; pdtok++) {
415 if (!(dec = srd_decoder_get_by_id(*pdtok))) {
416 g_critical("Protocol decoder '%s' not found.", *pdtok);
417 return 1;
419 g_debug("cli: Showing protocol decoder meta output from '%s'.", *pdtok);
420 g_hash_table_insert(pd_meta_visible, g_strdup(*pdtok), NULL);
422 g_strfreev(pds);
424 return 0;
427 int setup_pd_binary(char *opt_pd_binary)
429 GSList *l;
430 struct srd_decoder *dec;
431 int bin_class;
432 char **pds, **pdtok, **keyval, **bin_name;
434 pd_binary_visible = g_hash_table_new_full(g_str_hash, g_int_equal,
435 g_free, NULL);
436 pds = g_strsplit(opt_pd_binary, ",", 0);
437 for (pdtok = pds; *pdtok && **pdtok; pdtok++) {
438 keyval = g_strsplit(*pdtok, "=", 0);
439 if (!(dec = srd_decoder_get_by_id(keyval[0]))) {
440 g_critical("Protocol decoder '%s' not found.", keyval[0]);
441 return 1;
443 if (!dec->binary) {
444 g_critical("Protocol decoder '%s' has no binary output.", keyval[0]);
445 return 1;
447 bin_class = 0;
448 if (g_strv_length(keyval) == 2) {
449 for (l = dec->binary; l; l = l->next, bin_class++) {
450 bin_name = l->data;
451 if (!strcmp(bin_name[0], keyval[1]))
452 /* Found it. */
453 break;
455 if (!l) {
456 g_critical("binary output '%s' not found "
457 "for protocol decoder '%s'.", keyval[1], keyval[0]);
458 return 1;
460 g_debug("cli: Showing protocol decoder %s binary class "
461 "%d (%s).", keyval[0], bin_class, bin_name[0]);
462 } else {
463 /* No class specified: output all of them. */
464 bin_class = -1;
465 g_debug("cli: Showing all binary classes for protocol "
466 "decoder %s.", keyval[0]);
468 g_hash_table_insert(pd_binary_visible, g_strdup(keyval[0]), GINT_TO_POINTER(bin_class));
469 g_strfreev(keyval);
471 g_strfreev(pds);
473 return 0;
476 void show_pd_annotations(struct srd_proto_data *pdata, void *cb_data)
478 struct srd_decoder *dec;
479 struct srd_proto_data_annotation *pda;
480 GSList *ann_list, *l;
481 int i;
482 char **ann_descr;
483 gboolean show_ann, show_snum, show_class, show_quotes, show_abbrev;
484 const char *quote;
486 (void)cb_data;
488 if (!pd_ann_visible)
489 return;
491 if (!g_hash_table_lookup_extended(pd_ann_visible, pdata->pdo->di->decoder->id,
492 NULL, (void **)&ann_list)) {
493 /* Not in the list of PDs whose annotations we're showing. */
494 return;
497 dec = pdata->pdo->di->decoder;
498 pda = pdata->data;
499 show_ann = FALSE;
500 for (l = ann_list; l; l = l->next) {
501 if (GPOINTER_TO_INT(l->data) == -1
502 || GPOINTER_TO_INT(l->data) == pda->ann_class) {
503 show_ann = TRUE;
504 break;
507 if (!show_ann)
508 return;
511 * Determine which fields of the annotation to display. Inspect
512 * user specified options as well as the verbosity of the log level:
513 * - Optionally show the sample numbers for the annotation's span.
514 * - Always show the protocol decoder ID.
515 * - Optionally show the annotation's class description.
516 * - Always show the longest annotation text.
517 * - Optionally show alternative annotation text (abbreviations
518 * for different zoom levels).
519 * - Optionally put quote marks around annotation text, when
520 * recipients might have to deal with a set of text variants.
522 show_snum = show_class = show_quotes = show_abbrev = FALSE;
523 if (opt_pd_samplenum || opt_loglevel > SR_LOG_WARN) {
524 show_snum = TRUE;
526 if (opt_loglevel > SR_LOG_WARN) {
527 show_quotes = TRUE;
529 if (opt_loglevel > SR_LOG_INFO) {
530 show_class = TRUE;
531 show_abbrev = TRUE;
535 * Display the annotation's fields after the layout was
536 * determined above.
538 if (show_snum) {
539 printf("%" PRIu64 "-%" PRIu64 " ",
540 pdata->start_sample, pdata->end_sample);
542 printf("%s: ", pdata->pdo->proto_id);
543 if (show_class) {
544 ann_descr = g_slist_nth_data(dec->annotations, pda->ann_class);
545 printf("%s: ", ann_descr[0]);
547 quote = show_quotes ? "\"" : "";
548 printf("%s%s%s", quote, pda->ann_text[0], quote);
549 if (show_abbrev) {
550 for (i = 1; pda->ann_text[i]; i++)
551 printf(" %s%s%s", quote, pda->ann_text[i], quote);
553 printf("\n");
554 fflush(stdout);
557 void show_pd_meta(struct srd_proto_data *pdata, void *cb_data)
559 (void)cb_data;
561 if (!g_hash_table_lookup_extended(pd_meta_visible,
562 pdata->pdo->di->decoder->id, NULL, NULL))
563 /* Not in the list of PDs whose meta output we're showing. */
564 return;
566 if (opt_pd_samplenum || opt_loglevel > SR_LOG_WARN)
567 printf("%"PRIu64"-%"PRIu64" ", pdata->start_sample, pdata->end_sample);
568 printf("%s: ", pdata->pdo->proto_id);
569 printf("%s: %s", pdata->pdo->meta_name, g_variant_print(pdata->data, FALSE));
570 printf("\n");
571 fflush(stdout);
574 void show_pd_binary(struct srd_proto_data *pdata, void *cb_data)
576 struct srd_proto_data_binary *pdb;
577 gpointer classp;
578 int classi;
580 (void)cb_data;
582 if (!g_hash_table_lookup_extended(pd_binary_visible,
583 pdata->pdo->di->decoder->id, NULL, (void **)&classp))
584 /* Not in the list of PDs whose meta output we're showing. */
585 return;
587 classi = GPOINTER_TO_INT(classp);
588 pdb = pdata->data;
589 if (classi != -1 && classi != pdb->bin_class)
590 /* Not showing this binary class. */
591 return;
593 /* Just send the binary output to stdout, no embellishments. */
594 fwrite(pdb->data, pdb->size, 1, stdout);
595 fflush(stdout);
597 #endif