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/>.
24 #include "sigrok-cli.h"
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
,
37 struct srd_decoder_option
*o
;
45 *options
= g_hash_table_new_full(g_str_hash
, g_str_equal
, g_free
,
46 (GDestroyNotify
)g_variant_unref
);
48 for (optl
= dec
->options
; optl
; optl
= optl
->next
) {
50 if (!(val_str
= g_hash_table_lookup(hash
, o
->id
)))
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
);
63 gvar
= g_variant_new_int64(val_int
);
65 g_critical("Unsupported type for option '%s' (%s)",
66 o
->id
, g_variant_get_type_string(o
->def
));
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
);
78 static int move_hash_element(GHashTable
*src
, GHashTable
*dest
, void *key
)
80 void *orig_key
, *value
;
82 if (!g_hash_table_lookup_extended(src
, key
, &orig_key
, &value
))
85 g_hash_table_steal(src
, orig_key
);
86 g_hash_table_insert(dest
, orig_key
, value
);
91 static GHashTable
*extract_channel_map(struct srd_decoder
*dec
, GHashTable
*hash
)
93 GHashTable
*channel_map
;
94 struct srd_channel
*pdch
;
97 channel_map
= g_hash_table_new_full(g_str_hash
, g_str_equal
,
100 for (l
= dec
->channels
; l
; l
= l
->next
) {
102 move_hash_element(hash
, channel_map
, pdch
->id
);
104 for (l
= dec
->opt_channels
; l
; l
= l
->next
) {
106 move_hash_element(hash
, channel_map
, pdch
->id
);
113 * Register the given PDs for this session.
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
118 int register_pds(const char *opt_pds
, char *opt_pd_annotations
)
120 struct srd_decoder
*dec
;
121 GHashTable
*pd_opthash
, *options
, *channels
;
123 struct srd_decoder_inst
*di
;
125 char **pdtokens
, **pdtok
, *pd_name
;
127 pd_ann_visible
= g_hash_table_new_full(g_str_hash
, g_str_equal
,
131 pd_opthash
= options
= channels
= pd_channel_maps
= NULL
;
132 pdtokens
= g_strsplit(opt_pds
, ",", 0);
133 for (pdtok
= pdtokens
; *pdtok
; pdtok
++) {
134 if (!(pd_opthash
= parse_generic_arg(*pdtok
, TRUE
))) {
135 g_critical("Invalid protocol decoder option '%s'.", *pdtok
);
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
);
146 if (!(dec
= srd_decoder_get_by_id(pd_name
))) {
147 g_critical("Failed to get decoder %s by id.", pd_name
);
152 /* Convert decoder option and channel values to GVariant. */
153 if (!opts_to_gvar(dec
, pd_opthash
, &options
)) {
157 channels
= extract_channel_map(dec
, pd_opthash
);
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
)
162 g_critical("Unknown option or channel '%s'", (char *)l
->data
);
163 g_list_free(leftover
);
167 if (!(di
= srd_inst_new(srd_sess
, pd_name
, options
))) {
168 g_critical("Failed to instantiate protocol decoder %s.", pd_name
);
173 if (pdtok
== pdtokens
) {
175 * Save the channel setup for later, but only on the
176 * first decoder (stacked decoders don't get channels).
178 pd_channel_maps
= g_hash_table_new_full(g_str_hash
,
179 g_str_equal
, g_free
, (GDestroyNotify
)g_hash_table_destroy
);
180 g_hash_table_insert(pd_channel_maps
, g_strdup(di
->inst_id
), channels
);
185 * If no annotation list was specified, add them all in now.
186 * This will be pared down later to leave only the last PD
189 if (!opt_pd_annotations
)
190 g_hash_table_insert(pd_ann_visible
, g_strdup(di
->inst_id
),
191 g_slist_append(NULL
, GINT_TO_POINTER(-1)));
194 g_strfreev(pdtokens
);
196 g_hash_table_destroy(pd_opthash
);
198 g_hash_table_destroy(options
);
200 g_hash_table_destroy(channels
);
206 static void map_pd_inst_channels(void *key
, void *value
, void *user_data
)
208 GHashTable
*channel_map
;
209 GHashTable
*channel_indices
;
210 GSList
*channel_list
;
211 struct srd_decoder_inst
*di
;
214 void *channel_target
;
215 struct sr_channel
*ch
;
219 channel_list
= user_data
;
221 di
= srd_inst_find_by_id(srd_sess
, key
);
223 g_critical("Protocol decoder instance \"%s\" not found.",
227 channel_indices
= g_hash_table_new_full(g_str_hash
, g_str_equal
, g_free
,
228 (GDestroyNotify
)g_variant_unref
);
230 g_hash_table_iter_init(&iter
, channel_map
);
231 while (g_hash_table_iter_next(&iter
, &channel_id
, &channel_target
)) {
232 ch
= find_channel(channel_list
, channel_target
);
234 g_printerr("cli: No channel with name \"%s\" found.\n",
235 (char *)channel_target
);
239 g_printerr("cli: Target channel \"%s\" not enabled.\n",
240 (char *)channel_target
);
242 var
= g_variant_new_int32(ch
->index
);
243 g_variant_ref_sink(var
);
244 g_hash_table_insert(channel_indices
, g_strdup(channel_id
), var
);
247 srd_inst_channel_set_all(di
, channel_indices
);
250 void map_pd_channels(struct sr_dev_inst
*sdi
)
254 channels
= sr_dev_inst_channels_get(sdi
);
256 if (pd_channel_maps
) {
257 g_hash_table_foreach(pd_channel_maps
, &map_pd_inst_channels
,
259 g_hash_table_destroy(pd_channel_maps
);
260 pd_channel_maps
= NULL
;
264 int setup_pd_stack(char *opt_pds
, char *opt_pd_stack
, char *opt_pd_annotations
)
266 struct srd_decoder_inst
*di_from
, *di_to
;
270 /* Set up the protocol decoder stack. */
271 pds
= g_strsplit(opt_pds
, ",", 0);
272 if (g_strv_length(pds
) > 1) {
274 /* A stack setup was specified, use that. */
276 pds
= g_strsplit(opt_pd_stack
, ",", 0);
277 if (g_strv_length(pds
) < 2) {
279 g_critical("Specify at least two protocol decoders to stack.");
284 /* First PD goes at the bottom of the stack. */
285 ids
= g_strsplit(pds
[0], ":", 0);
286 if (!(di_from
= srd_inst_find_by_id(srd_sess
, ids
[0]))) {
288 g_critical("Cannot stack protocol decoder '%s': "
289 "instance not found.", pds
[0]);
294 /* Every subsequent PD goes on top. */
295 for (i
= 1; pds
[i
]; i
++) {
296 ids
= g_strsplit(pds
[i
], ":", 0);
297 if (!(di_to
= srd_inst_find_by_id(srd_sess
, ids
[0]))) {
299 g_critical("Cannot stack protocol decoder '%s': "
300 "instance not found.", pds
[i
]);
304 if ((ret
= srd_inst_stack(srd_sess
, di_from
, di_to
)) != SRD_OK
)
308 * Don't show annotation from this PD. Only the last PD in
309 * the stack will be left on the annotation list (unless
310 * the annotation list was specifically provided).
312 if (!opt_pd_annotations
)
313 g_hash_table_remove(pd_ann_visible
, di_from
->inst_id
);
323 int setup_pd_annotations(char *opt_pd_annotations
)
326 struct srd_decoder
*dec
;
328 char **pds
, **pdtok
, **keyval
, **annlist
, **ann
, **ann_descr
;
330 /* Set up custom list of PDs and annotations to show. */
331 pds
= g_strsplit(opt_pd_annotations
, ",", 0);
332 for (pdtok
= pds
; *pdtok
&& **pdtok
; pdtok
++) {
333 keyval
= g_strsplit(*pdtok
, "=", 0);
334 if (!(dec
= srd_decoder_get_by_id(keyval
[0]))) {
335 g_critical("Protocol decoder '%s' not found.", keyval
[0]);
338 if (!dec
->annotations
) {
339 g_critical("Protocol decoder '%s' has no annotations.", keyval
[0]);
342 if (g_strv_length(keyval
) == 2 && keyval
[1][0] != '\0') {
343 annlist
= g_strsplit(keyval
[1], ":", 0);
344 for (ann
= annlist
; *ann
&& **ann
; ann
++) {
346 for (l
= dec
->annotations
; l
; l
= l
->next
, ann_class
++) {
348 if (!canon_cmp(ann_descr
[0], *ann
))
353 g_critical("Annotation '%s' not found "
354 "for protocol decoder '%s'.", *ann
, keyval
[0]);
357 l_ann
= g_hash_table_lookup(pd_ann_visible
, keyval
[0]);
358 l_ann
= g_slist_append(l_ann
, GINT_TO_POINTER(ann_class
));
359 g_hash_table_replace(pd_ann_visible
, g_strdup(keyval
[0]), l_ann
);
360 g_debug("cli: Showing protocol decoder %s annotation "
361 "class %d (%s).", keyval
[0], ann_class
, ann_descr
[0]);
364 /* No class specified: show all of them. */
365 g_hash_table_insert(pd_ann_visible
, g_strdup(keyval
[0]),
366 g_slist_append(NULL
, GINT_TO_POINTER(-1)));
367 g_debug("cli: Showing all annotation classes for protocol "
368 "decoder %s.", keyval
[0]);
377 int setup_pd_meta(char *opt_pd_meta
)
379 struct srd_decoder
*dec
;
382 pd_meta_visible
= g_hash_table_new_full(g_str_hash
, g_int_equal
,
384 pds
= g_strsplit(opt_pd_meta
, ",", 0);
385 for (pdtok
= pds
; *pdtok
&& **pdtok
; pdtok
++) {
386 if (!(dec
= srd_decoder_get_by_id(*pdtok
))) {
387 g_critical("Protocol decoder '%s' not found.", *pdtok
);
390 g_debug("cli: Showing protocol decoder meta output from '%s'.", *pdtok
);
391 g_hash_table_insert(pd_meta_visible
, g_strdup(*pdtok
), NULL
);
398 int setup_pd_binary(char *opt_pd_binary
)
401 struct srd_decoder
*dec
;
403 char **pds
, **pdtok
, **keyval
, **bin_name
;
405 pd_binary_visible
= g_hash_table_new_full(g_str_hash
, g_int_equal
,
407 pds
= g_strsplit(opt_pd_binary
, ",", 0);
408 for (pdtok
= pds
; *pdtok
&& **pdtok
; pdtok
++) {
409 keyval
= g_strsplit(*pdtok
, "=", 0);
410 if (!(dec
= srd_decoder_get_by_id(keyval
[0]))) {
411 g_critical("Protocol decoder '%s' not found.", keyval
[0]);
415 g_critical("Protocol decoder '%s' has no binary output.", keyval
[0]);
419 if (g_strv_length(keyval
) == 2) {
420 for (l
= dec
->binary
; l
; l
= l
->next
, bin_class
++) {
422 if (!strcmp(bin_name
[0], keyval
[1]))
427 g_critical("binary output '%s' not found "
428 "for protocol decoder '%s'.", keyval
[1], keyval
[0]);
431 g_debug("cli: Showing protocol decoder %s binary class "
432 "%d (%s).", keyval
[0], bin_class
, bin_name
[0]);
434 /* No class specified: output all of them. */
436 g_debug("cli: Showing all binary classes for protocol "
437 "decoder %s.", keyval
[0]);
439 g_hash_table_insert(pd_binary_visible
, g_strdup(keyval
[0]), GINT_TO_POINTER(bin_class
));
447 void show_pd_annotations(struct srd_proto_data
*pdata
, void *cb_data
)
449 struct srd_decoder
*dec
;
450 struct srd_proto_data_annotation
*pda
;
451 GSList
*ann_list
, *l
;
461 if (!g_hash_table_lookup_extended(pd_ann_visible
, pdata
->pdo
->di
->inst_id
,
462 NULL
, (void **)&ann_list
))
463 /* Not in the list of PDs whose annotations we're showing. */
466 dec
= pdata
->pdo
->di
->decoder
;
469 for (l
= ann_list
; l
; l
= l
->next
) {
470 if (GPOINTER_TO_INT(l
->data
) == -1
471 || GPOINTER_TO_INT(l
->data
) == pda
->ann_class
) {
479 if (opt_loglevel
<= SR_LOG_WARN
) {
480 /* Show only the longest annotation. */
481 printf("%s", pda
->ann_text
[0]);
482 } else if (opt_loglevel
>= SR_LOG_INFO
) {
483 /* Sample numbers and quotes around the longest annotation. */
484 printf("%"PRIu64
"-%"PRIu64
"", pdata
->start_sample
, pdata
->end_sample
);
485 if (opt_loglevel
== SR_LOG_INFO
) {
486 printf(" \"%s\"", pda
->ann_text
[0]);
488 /* Protocol decoder id, annotation class,
489 * all annotation strings. */
490 ann_descr
= g_slist_nth_data(dec
->annotations
, pda
->ann_class
);
491 printf(" %s: %s:", pdata
->pdo
->proto_id
, ann_descr
[0]);
492 for (i
= 0; pda
->ann_text
[i
]; i
++)
493 printf(" \"%s\"", pda
->ann_text
[i
]);
500 void show_pd_meta(struct srd_proto_data
*pdata
, void *cb_data
)
504 if (!g_hash_table_lookup_extended(pd_meta_visible
,
505 pdata
->pdo
->di
->decoder
->id
, NULL
, NULL
))
506 /* Not in the list of PDs whose meta output we're showing. */
509 if (opt_loglevel
> SR_LOG_WARN
)
510 printf("%"PRIu64
"-%"PRIu64
" ", pdata
->start_sample
, pdata
->end_sample
);
511 printf("%s: ", pdata
->pdo
->proto_id
);
512 printf("%s: %s", pdata
->pdo
->meta_name
, g_variant_print(pdata
->data
, FALSE
));
517 void show_pd_binary(struct srd_proto_data
*pdata
, void *cb_data
)
519 struct srd_proto_data_binary
*pdb
;
525 if (!g_hash_table_lookup_extended(pd_binary_visible
,
526 pdata
->pdo
->di
->decoder
->id
, NULL
, (void **)&classp
))
527 /* Not in the list of PDs whose meta output we're showing. */
530 class = GPOINTER_TO_INT(classp
);
532 if (class != -1 && class != pdb
->bin_class
)
533 /* Not showing this binary class. */
536 /* Just send the binary output to stdout, no embellishments. */
537 fwrite(pdb
->data
, pdb
->size
, 1, stdout
);