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