]> sigrok.org Git - sigrok-cli.git/blame - decode.c
Update due to sr_driver_list() now taking a context.
[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
f0f54487
UH
20#include <stdlib.h>
21#include <string.h>
2be182e6 22#include <glib.h>
662a1e27
UH
23#include "sigrok-cli.h"
24#include "config.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
f0f54487
UH
112/*
113 * Register the given PDs for this session.
2be182e6
BV
114 * Accepts a string of the form: "spi:sck=3:sdata=4,spi:sck=3:sdata=5"
115 * That will instantiate two SPI decoders on the clock but different data
116 * lines.
117 */
26c63758 118int register_pds(const char *opt_pds, char *opt_pd_annotations)
2be182e6
BV
119{
120 struct srd_decoder *dec;
3dfbfbc8 121 GHashTable *pd_opthash, *options, *channels;
2be182e6
BV
122 GList *leftover, *l;
123 struct srd_decoder_inst *di;
124 int ret;
125 char **pdtokens, **pdtok, *pd_name;
126
39aedc34
DE
127 pd_ann_visible = g_hash_table_new_full(g_str_hash, g_str_equal,
128 g_free, NULL);
2be182e6
BV
129 ret = 0;
130 pd_name = NULL;
3dfbfbc8 131 pd_opthash = options = channels = pd_channel_maps = NULL;
26c63758 132 pdtokens = g_strsplit(opt_pds, ",", 0);
2be182e6
BV
133 for (pdtok = pdtokens; *pdtok; pdtok++) {
134 if (!(pd_opthash = parse_generic_arg(*pdtok, TRUE))) {
135 g_critical("Invalid protocol decoder option '%s'.", *pdtok);
136 break;
137 }
138
139 pd_name = g_strdup(g_hash_table_lookup(pd_opthash, "sigrok_key"));
140 g_hash_table_remove(pd_opthash, "sigrok_key");
141 if (srd_decoder_load(pd_name) != SRD_OK) {
142 g_critical("Failed to load protocol decoder %s.", pd_name);
143 ret = 1;
144 break;
145 }
3a8ceb6a
BV
146 if (!(dec = srd_decoder_get_by_id(pd_name))) {
147 g_critical("Failed to get decoder %s by id.", pd_name);
148 ret = 1;
149 break;
150 }
2be182e6 151
3dfbfbc8 152 /* Convert decoder option and channel values to GVariant. */
2be182e6
BV
153 if (!opts_to_gvar(dec, pd_opthash, &options)) {
154 ret = 1;
155 break;
156 }
3dfbfbc8 157 channels = extract_channel_map(dec, pd_opthash);
39aedc34 158
2be182e6
BV
159 if (g_hash_table_size(pd_opthash) > 0) {
160 leftover = g_hash_table_get_keys(pd_opthash);
161 for (l = leftover; l; l = l->next)
3dfbfbc8 162 g_critical("Unknown option or channel '%s'", (char *)l->data);
2be182e6
BV
163 g_list_free(leftover);
164 break;
165 }
166
167 if (!(di = srd_inst_new(srd_sess, pd_name, options))) {
168 g_critical("Failed to instantiate protocol decoder %s.", pd_name);
169 ret = 1;
170 break;
171 }
172
09fea207 173 if (pdtok == pdtokens) {
3dfbfbc8
UH
174 /*
175 * Save the channel setup for later, but only on the
176 * first decoder (stacked decoders don't get channels).
177 */
178 pd_channel_maps = g_hash_table_new_full(g_str_hash,
09fea207 179 g_str_equal, g_free, (GDestroyNotify)g_hash_table_destroy);
3dfbfbc8
UH
180 g_hash_table_insert(pd_channel_maps, g_strdup(di->inst_id), channels);
181 channels = NULL;
09fea207 182 }
39aedc34 183
f0f54487
UH
184 /*
185 * If no annotation list was specified, add them all in now.
2be182e6
BV
186 * This will be pared down later to leave only the last PD
187 * in the stack.
188 */
189 if (!opt_pd_annotations)
790b0261
BV
190 g_hash_table_insert(pd_ann_visible, g_strdup(di->inst_id),
191 g_slist_append(NULL, GINT_TO_POINTER(-1)));
2be182e6
BV
192 }
193
194 g_strfreev(pdtokens);
195 if (pd_opthash)
196 g_hash_table_destroy(pd_opthash);
197 if (options)
198 g_hash_table_destroy(options);
3dfbfbc8
UH
199 if (channels)
200 g_hash_table_destroy(channels);
ad92a2b3 201 g_free(pd_name);
2be182e6
BV
202
203 return ret;
204}
205
3dfbfbc8 206static void map_pd_inst_channels(void *key, void *value, void *user_data)
39aedc34 207{
3dfbfbc8
UH
208 GHashTable *channel_map;
209 GHashTable *channel_indices;
210 GSList *channel_list;
39aedc34
DE
211 struct srd_decoder_inst *di;
212 GVariant *var;
3dfbfbc8 213 void *channel_id;
029d73fe
UH
214 void *channel_target;
215 struct sr_channel *ch;
39aedc34 216 GHashTableIter iter;
029d73fe 217 int num_channels;
39aedc34 218
3dfbfbc8
UH
219 channel_map = value;
220 channel_list = user_data;
39aedc34
DE
221
222 di = srd_inst_find_by_id(srd_sess, key);
223 if (!di) {
224 g_critical("Protocol decoder instance \"%s\" not found.",
225 (char *)key);
226 return;
227 }
3dfbfbc8 228 channel_indices = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
39aedc34
DE
229 (GDestroyNotify)g_variant_unref);
230
3dfbfbc8
UH
231 g_hash_table_iter_init(&iter, channel_map);
232 while (g_hash_table_iter_next(&iter, &channel_id, &channel_target)) {
233 ch = find_channel(channel_list, channel_target);
029d73fe
UH
234 if (!ch) {
235 g_printerr("cli: No channel with name \"%s\" found.\n",
236 (char *)channel_target);
39aedc34
DE
237 continue;
238 }
029d73fe
UH
239 if (!ch->enabled)
240 g_printerr("cli: Target channel \"%s\" not enabled.\n",
241 (char *)channel_target);
39aedc34 242
029d73fe 243 var = g_variant_new_int32(ch->index);
39aedc34 244 g_variant_ref_sink(var);
3dfbfbc8 245 g_hash_table_insert(channel_indices, g_strdup(channel_id), var);
39aedc34
DE
246 }
247
3dfbfbc8
UH
248 num_channels = g_slist_length(channel_list);
249 srd_inst_channel_set_all(di, channel_indices, (num_channels + 7) / 8);
39aedc34
DE
250}
251
3dfbfbc8 252void map_pd_channels(struct sr_dev_inst *sdi)
39aedc34 253{
c6fa2b2e
UH
254 GSList *channels;
255
256 channels = sr_dev_inst_channels_get(sdi);
257
3dfbfbc8
UH
258 if (pd_channel_maps) {
259 g_hash_table_foreach(pd_channel_maps, &map_pd_inst_channels,
c6fa2b2e 260 channels);
3dfbfbc8
UH
261 g_hash_table_destroy(pd_channel_maps);
262 pd_channel_maps = NULL;
39aedc34
DE
263 }
264}
265
26c63758 266int setup_pd_stack(char *opt_pds, char *opt_pd_stack, char *opt_pd_annotations)
2be182e6
BV
267{
268 struct srd_decoder_inst *di_from, *di_to;
269 int ret, i;
270 char **pds, **ids;
271
272 /* Set up the protocol decoder stack. */
273 pds = g_strsplit(opt_pds, ",", 0);
274 if (g_strv_length(pds) > 1) {
275 if (opt_pd_stack) {
276 /* A stack setup was specified, use that. */
277 g_strfreev(pds);
278 pds = g_strsplit(opt_pd_stack, ",", 0);
279 if (g_strv_length(pds) < 2) {
280 g_strfreev(pds);
281 g_critical("Specify at least two protocol decoders to stack.");
282 return 1;
283 }
284 }
285
286 /* First PD goes at the bottom of the stack. */
287 ids = g_strsplit(pds[0], ":", 0);
288 if (!(di_from = srd_inst_find_by_id(srd_sess, ids[0]))) {
289 g_strfreev(ids);
290 g_critical("Cannot stack protocol decoder '%s': "
291 "instance not found.", pds[0]);
292 return 1;
293 }
294 g_strfreev(ids);
295
296 /* Every subsequent PD goes on top. */
297 for (i = 1; pds[i]; i++) {
298 ids = g_strsplit(pds[i], ":", 0);
299 if (!(di_to = srd_inst_find_by_id(srd_sess, ids[0]))) {
300 g_strfreev(ids);
301 g_critical("Cannot stack protocol decoder '%s': "
302 "instance not found.", pds[i]);
303 return 1;
304 }
305 g_strfreev(ids);
306 if ((ret = srd_inst_stack(srd_sess, di_from, di_to)) != SRD_OK)
307 return 1;
308
f0f54487
UH
309 /*
310 * Don't show annotation from this PD. Only the last PD in
2be182e6
BV
311 * the stack will be left on the annotation list (unless
312 * the annotation list was specifically provided).
313 */
314 if (!opt_pd_annotations)
790b0261 315 g_hash_table_remove(pd_ann_visible, di_from->inst_id);
2be182e6
BV
316
317 di_from = di_to;
318 }
319 }
320 g_strfreev(pds);
321
322 return 0;
323}
324
26c63758 325int setup_pd_annotations(char *opt_pd_annotations)
2be182e6 326{
790b0261 327 GSList *l, *l_ann;
2be182e6
BV
328 struct srd_decoder *dec;
329 int ann_class;
790b0261 330 char **pds, **pdtok, **keyval, **annlist, **ann, **ann_descr;
2be182e6
BV
331
332 /* Set up custom list of PDs and annotations to show. */
333 pds = g_strsplit(opt_pd_annotations, ",", 0);
334 for (pdtok = pds; *pdtok && **pdtok; pdtok++) {
335 keyval = g_strsplit(*pdtok, "=", 0);
336 if (!(dec = srd_decoder_get_by_id(keyval[0]))) {
337 g_critical("Protocol decoder '%s' not found.", keyval[0]);
338 return 1;
339 }
340 if (!dec->annotations) {
341 g_critical("Protocol decoder '%s' has no annotations.", keyval[0]);
342 return 1;
343 }
790b0261
BV
344 if (g_strv_length(keyval) == 2 && keyval[1][0] != '\0') {
345 annlist = g_strsplit(keyval[1], ":", 0);
346 for (ann = annlist; *ann && **ann; ann++) {
347 ann_class = 0;
348 for (l = dec->annotations; l; l = l->next, ann_class++) {
349 ann_descr = l->data;
350 if (!canon_cmp(ann_descr[0], *ann))
351 /* Found it. */
352 break;
353 }
354 if (!l) {
355 g_critical("Annotation '%s' not found "
356 "for protocol decoder '%s'.", *ann, keyval[0]);
357 return 1;
358 }
359 l_ann = g_hash_table_lookup(pd_ann_visible, keyval[0]);
360 l_ann = g_slist_append(l_ann, GINT_TO_POINTER(ann_class));
361 g_hash_table_replace(pd_ann_visible, g_strdup(keyval[0]), l_ann);
362 g_debug("cli: Showing protocol decoder %s annotation "
363 "class %d (%s).", keyval[0], ann_class, ann_descr[0]);
2be182e6 364 }
2be182e6
BV
365 } else {
366 /* No class specified: show all of them. */
790b0261
BV
367 g_hash_table_insert(pd_ann_visible, g_strdup(keyval[0]),
368 g_slist_append(NULL, GINT_TO_POINTER(-1)));
2be182e6
BV
369 g_debug("cli: Showing all annotation classes for protocol "
370 "decoder %s.", keyval[0]);
371 }
2be182e6
BV
372 g_strfreev(keyval);
373 }
374 g_strfreev(pds);
375
376 return 0;
377}
378
26c63758 379int setup_pd_meta(char *opt_pd_meta)
2be182e6
BV
380{
381 struct srd_decoder *dec;
382 char **pds, **pdtok;
383
384 pd_meta_visible = g_hash_table_new_full(g_str_hash, g_int_equal,
385 g_free, NULL);
386 pds = g_strsplit(opt_pd_meta, ",", 0);
387 for (pdtok = pds; *pdtok && **pdtok; pdtok++) {
388 if (!(dec = srd_decoder_get_by_id(*pdtok))) {
389 g_critical("Protocol decoder '%s' not found.", *pdtok);
390 return 1;
391 }
392 g_debug("cli: Showing protocol decoder meta output from '%s'.", *pdtok);
393 g_hash_table_insert(pd_meta_visible, g_strdup(*pdtok), NULL);
394 }
395 g_strfreev(pds);
396
397 return 0;
398}
399
26c63758 400int setup_pd_binary(char *opt_pd_binary)
2be182e6
BV
401{
402 GSList *l;
403 struct srd_decoder *dec;
404 int bin_class;
5ff52594 405 char **pds, **pdtok, **keyval, **bin_name;
2be182e6
BV
406
407 pd_binary_visible = g_hash_table_new_full(g_str_hash, g_int_equal,
408 g_free, NULL);
409 pds = g_strsplit(opt_pd_binary, ",", 0);
410 for (pdtok = pds; *pdtok && **pdtok; pdtok++) {
411 keyval = g_strsplit(*pdtok, "=", 0);
412 if (!(dec = srd_decoder_get_by_id(keyval[0]))) {
413 g_critical("Protocol decoder '%s' not found.", keyval[0]);
414 return 1;
415 }
416 if (!dec->binary) {
417 g_critical("Protocol decoder '%s' has no binary output.", keyval[0]);
418 return 1;
419 }
420 bin_class = 0;
421 if (g_strv_length(keyval) == 2) {
422 for (l = dec->binary; l; l = l->next, bin_class++) {
423 bin_name = l->data;
5ff52594 424 if (!strcmp(bin_name[0], keyval[1]))
2be182e6
BV
425 /* Found it. */
426 break;
427 }
428 if (!l) {
429 g_critical("binary output '%s' not found "
430 "for protocol decoder '%s'.", keyval[1], keyval[0]);
431 return 1;
432 }
433 g_debug("cli: Showing protocol decoder %s binary class "
5ff52594 434 "%d (%s).", keyval[0], bin_class, bin_name[0]);
2be182e6
BV
435 } else {
436 /* No class specified: output all of them. */
437 bin_class = -1;
438 g_debug("cli: Showing all binary classes for protocol "
439 "decoder %s.", keyval[0]);
440 }
441 g_hash_table_insert(pd_binary_visible, g_strdup(keyval[0]), GINT_TO_POINTER(bin_class));
442 g_strfreev(keyval);
443 }
444 g_strfreev(pds);
445
446 return 0;
447}
448
449void show_pd_annotations(struct srd_proto_data *pdata, void *cb_data)
450{
6fdcc67d 451 struct srd_decoder *dec;
2be182e6 452 struct srd_proto_data_annotation *pda;
790b0261
BV
453 GSList *ann_list, *l;
454 int i;
6fdcc67d 455 char **ann_descr;
790b0261 456 gboolean show;
2be182e6 457
2be182e6
BV
458 (void)cb_data;
459
460 if (!pd_ann_visible)
461 return;
462
463 if (!g_hash_table_lookup_extended(pd_ann_visible, pdata->pdo->di->inst_id,
790b0261 464 NULL, (void **)&ann_list))
2be182e6
BV
465 /* Not in the list of PDs whose annotations we're showing. */
466 return;
467
6fdcc67d 468 dec = pdata->pdo->di->decoder;
2be182e6 469 pda = pdata->data;
790b0261
BV
470 show = FALSE;
471 for (l = ann_list; l; l = l->next) {
472 if (GPOINTER_TO_INT(l->data) == -1
a23105b1 473 || GPOINTER_TO_INT(l->data) == pda->ann_class) {
790b0261
BV
474 show = TRUE;
475 break;
476 }
477 }
478 if (!show)
2be182e6
BV
479 return;
480
6fdcc67d
BV
481 if (opt_loglevel <= SR_LOG_WARN) {
482 /* Show only the longest annotation. */
6d1dbe35 483 printf("%s", pda->ann_text[0]);
6fdcc67d
BV
484 } else if (opt_loglevel >= SR_LOG_INFO) {
485 /* Sample numbers and quotes around the longest annotation. */
6d1dbe35 486 printf("%"PRIu64"-%"PRIu64"", pdata->start_sample, pdata->end_sample);
6fdcc67d 487 if (opt_loglevel == SR_LOG_INFO) {
6d1dbe35 488 printf(" \"%s\"", pda->ann_text[0]);
6fdcc67d
BV
489 } else {
490 /* Protocol decoder id, annotation class,
491 * all annotation strings. */
a23105b1 492 ann_descr = g_slist_nth_data(dec->annotations, pda->ann_class);
6d1dbe35 493 printf(" %s: %s:", pdata->pdo->proto_id, ann_descr[0]);
6fdcc67d 494 for (i = 0; pda->ann_text[i]; i++)
6d1dbe35 495 printf(" \"%s\"", pda->ann_text[i]);
6fdcc67d
BV
496 }
497 }
2be182e6
BV
498 printf("\n");
499 fflush(stdout);
500}
501
502void show_pd_meta(struct srd_proto_data *pdata, void *cb_data)
503{
2be182e6
BV
504 (void)cb_data;
505
506 if (!g_hash_table_lookup_extended(pd_meta_visible,
507 pdata->pdo->di->decoder->id, NULL, NULL))
508 /* Not in the list of PDs whose meta output we're showing. */
509 return;
510
511 if (opt_loglevel > SR_LOG_WARN)
512 printf("%"PRIu64"-%"PRIu64" ", pdata->start_sample, pdata->end_sample);
513 printf("%s: ", pdata->pdo->proto_id);
514 printf("%s: %s", pdata->pdo->meta_name, g_variant_print(pdata->data, FALSE));
515 printf("\n");
516 fflush(stdout);
517}
518
519void show_pd_binary(struct srd_proto_data *pdata, void *cb_data)
520{
521 struct srd_proto_data_binary *pdb;
522 gpointer classp;
523 int class;
524
2be182e6
BV
525 (void)cb_data;
526
527 if (!g_hash_table_lookup_extended(pd_binary_visible,
528 pdata->pdo->di->decoder->id, NULL, (void **)&classp))
529 /* Not in the list of PDs whose meta output we're showing. */
530 return;
531
532 class = GPOINTER_TO_INT(classp);
533 pdb = pdata->data;
534 if (class != -1 && class != pdb->bin_class)
535 /* Not showing this binary class. */
536 return;
537
538 /* Just send the binary output to stdout, no embellishments. */
539 fwrite(pdb->data, pdb->size, 1, stdout);
540 fflush(stdout);
541}
542#endif