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 "sigrok-cli.h"
25 static GHashTable
*pd_ann_visible
= NULL
;
26 static GHashTable
*pd_meta_visible
= NULL
;
27 static GHashTable
*pd_binary_visible
= NULL
;
28 static GHashTable
*pd_probe_maps
= NULL
;
30 extern struct srd_session
*srd_sess
;
31 extern gint opt_loglevel
;
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_probe_map(struct srd_decoder
*dec
, GHashTable
*hash
)
93 GHashTable
*probe_map
;
97 probe_map
= g_hash_table_new_full(g_str_hash
, g_str_equal
,
100 for (l
= dec
->probes
; l
; l
= l
->next
) {
102 move_hash_element(hash
, probe_map
, p
->id
);
104 for (l
= dec
->opt_probes
; l
; l
= l
->next
) {
106 move_hash_element(hash
, probe_map
, p
->id
);
112 /* Register the given PDs for this session.
113 * Accepts a string of the form: "spi:sck=3:sdata=4,spi:sck=3:sdata=5"
114 * That will instantiate two SPI decoders on the clock but different data
117 int register_pds(const char *opt_pds
, char *opt_pd_annotations
)
119 struct srd_decoder
*dec
;
120 GHashTable
*pd_opthash
, *options
, *probes
;
122 struct srd_decoder_inst
*di
;
124 char **pdtokens
, **pdtok
, *pd_name
;
126 pd_ann_visible
= g_hash_table_new_full(g_str_hash
, g_str_equal
,
130 pd_opthash
= options
= probes
= pd_probe_maps
= NULL
;
131 pdtokens
= g_strsplit(opt_pds
, ",", 0);
132 for (pdtok
= pdtokens
; *pdtok
; pdtok
++) {
133 if (!(pd_opthash
= parse_generic_arg(*pdtok
, TRUE
))) {
134 g_critical("Invalid protocol decoder option '%s'.", *pdtok
);
138 pd_name
= g_strdup(g_hash_table_lookup(pd_opthash
, "sigrok_key"));
139 g_hash_table_remove(pd_opthash
, "sigrok_key");
140 if (srd_decoder_load(pd_name
) != SRD_OK
) {
141 g_critical("Failed to load protocol decoder %s.", pd_name
);
145 dec
= srd_decoder_get_by_id(pd_name
);
147 /* Convert decoder option and probe values to GVariant. */
148 if (!opts_to_gvar(dec
, pd_opthash
, &options
)) {
152 probes
= extract_probe_map(dec
, pd_opthash
);
154 if (g_hash_table_size(pd_opthash
) > 0) {
155 leftover
= g_hash_table_get_keys(pd_opthash
);
156 for (l
= leftover
; l
; l
= l
->next
)
157 g_critical("Unknown option or probe '%s'", (char *)l
->data
);
158 g_list_free(leftover
);
162 if (!(di
= srd_inst_new(srd_sess
, pd_name
, options
))) {
163 g_critical("Failed to instantiate protocol decoder %s.", pd_name
);
168 if (pdtok
== pdtokens
) {
169 /* Save the probe setup for later, but only on the first
170 * decoder -- stacked decoders don't get probes. */
171 pd_probe_maps
= g_hash_table_new_full(g_str_hash
,
172 g_str_equal
, g_free
, (GDestroyNotify
)g_hash_table_destroy
);
173 g_hash_table_insert(pd_probe_maps
, g_strdup(di
->inst_id
), probes
);
177 /* If no annotation list was specified, add them all in now.
178 * This will be pared down later to leave only the last PD
181 if (!opt_pd_annotations
)
182 g_hash_table_insert(pd_ann_visible
,
183 g_strdup(di
->inst_id
), GINT_TO_POINTER(-1));
186 g_strfreev(pdtokens
);
188 g_hash_table_destroy(pd_opthash
);
190 g_hash_table_destroy(options
);
192 g_hash_table_destroy(probes
);
199 static void map_pd_inst_probes(void *key
, void *value
, void *user_data
)
201 GHashTable
*probe_map
;
202 GHashTable
*probe_indices
;
204 struct srd_decoder_inst
*di
;
208 struct sr_probe
*probe
;
213 probe_list
= user_data
;
215 di
= srd_inst_find_by_id(srd_sess
, key
);
217 g_critical("Protocol decoder instance \"%s\" not found.",
221 probe_indices
= g_hash_table_new_full(g_str_hash
, g_str_equal
, g_free
,
222 (GDestroyNotify
)g_variant_unref
);
224 g_hash_table_iter_init(&iter
, probe_map
);
225 while (g_hash_table_iter_next(&iter
, &probe_id
, &probe_target
)) {
226 probe
= find_probe(probe_list
, probe_target
);
228 g_printerr("cli: No probe with name \"%s\" found.\n",
229 (char *)probe_target
);
233 g_printerr("cli: Target probe \"%s\" not enabled.\n",
234 (char *)probe_target
);
236 var
= g_variant_new_int32(probe
->index
);
237 g_variant_ref_sink(var
);
238 g_hash_table_insert(probe_indices
, g_strdup(probe_id
), var
);
241 num_probes
= g_slist_length(probe_list
);
242 srd_inst_probe_set_all(di
, probe_indices
, (num_probes
+ 7) / 8);
245 void map_pd_probes(struct sr_dev_inst
*sdi
)
248 g_hash_table_foreach(pd_probe_maps
, &map_pd_inst_probes
,
250 g_hash_table_destroy(pd_probe_maps
);
251 pd_probe_maps
= NULL
;
255 int setup_pd_stack(char *opt_pds
, char *opt_pd_stack
, char *opt_pd_annotations
)
257 struct srd_decoder_inst
*di_from
, *di_to
;
261 /* Set up the protocol decoder stack. */
262 pds
= g_strsplit(opt_pds
, ",", 0);
263 if (g_strv_length(pds
) > 1) {
265 /* A stack setup was specified, use that. */
267 pds
= g_strsplit(opt_pd_stack
, ",", 0);
268 if (g_strv_length(pds
) < 2) {
270 g_critical("Specify at least two protocol decoders to stack.");
275 /* First PD goes at the bottom of the stack. */
276 ids
= g_strsplit(pds
[0], ":", 0);
277 if (!(di_from
= srd_inst_find_by_id(srd_sess
, ids
[0]))) {
279 g_critical("Cannot stack protocol decoder '%s': "
280 "instance not found.", pds
[0]);
285 /* Every subsequent PD goes on top. */
286 for (i
= 1; pds
[i
]; i
++) {
287 ids
= g_strsplit(pds
[i
], ":", 0);
288 if (!(di_to
= srd_inst_find_by_id(srd_sess
, ids
[0]))) {
290 g_critical("Cannot stack protocol decoder '%s': "
291 "instance not found.", pds
[i
]);
295 if ((ret
= srd_inst_stack(srd_sess
, di_from
, di_to
)) != SRD_OK
)
298 /* Don't show annotation from this PD. Only the last PD in
299 * the stack will be left on the annotation list (unless
300 * the annotation list was specifically provided).
302 if (!opt_pd_annotations
)
303 g_hash_table_remove(pd_ann_visible
,
314 int setup_pd_annotations(char *opt_pd_annotations
)
317 struct srd_decoder
*dec
;
319 char **pds
, **pdtok
, **keyval
, **ann_descr
;
321 /* Set up custom list of PDs and annotations to show. */
322 pds
= g_strsplit(opt_pd_annotations
, ",", 0);
323 for (pdtok
= pds
; *pdtok
&& **pdtok
; pdtok
++) {
324 keyval
= g_strsplit(*pdtok
, "=", 0);
325 if (!(dec
= srd_decoder_get_by_id(keyval
[0]))) {
326 g_critical("Protocol decoder '%s' not found.", keyval
[0]);
329 if (!dec
->annotations
) {
330 g_critical("Protocol decoder '%s' has no annotations.", keyval
[0]);
334 if (g_strv_length(keyval
) == 2) {
335 for (l
= dec
->annotations
; l
; l
= l
->next
, ann_class
++) {
337 if (!canon_cmp(ann_descr
[0], keyval
[1]))
342 g_critical("Annotation '%s' not found "
343 "for protocol decoder '%s'.", keyval
[1], keyval
[0]);
346 g_debug("cli: Showing protocol decoder %s annotation "
347 "class %d (%s).", keyval
[0], ann_class
, ann_descr
[0]);
349 /* No class specified: show all of them. */
351 g_debug("cli: Showing all annotation classes for protocol "
352 "decoder %s.", keyval
[0]);
354 g_hash_table_insert(pd_ann_visible
, g_strdup(keyval
[0]), GINT_TO_POINTER(ann_class
));
362 int setup_pd_meta(char *opt_pd_meta
)
364 struct srd_decoder
*dec
;
367 pd_meta_visible
= g_hash_table_new_full(g_str_hash
, g_int_equal
,
369 pds
= g_strsplit(opt_pd_meta
, ",", 0);
370 for (pdtok
= pds
; *pdtok
&& **pdtok
; pdtok
++) {
371 if (!(dec
= srd_decoder_get_by_id(*pdtok
))) {
372 g_critical("Protocol decoder '%s' not found.", *pdtok
);
375 g_debug("cli: Showing protocol decoder meta output from '%s'.", *pdtok
);
376 g_hash_table_insert(pd_meta_visible
, g_strdup(*pdtok
), NULL
);
383 int setup_pd_binary(char *opt_pd_binary
)
386 struct srd_decoder
*dec
;
388 char **pds
, **pdtok
, **keyval
, **bin_name
;
390 pd_binary_visible
= g_hash_table_new_full(g_str_hash
, g_int_equal
,
392 pds
= g_strsplit(opt_pd_binary
, ",", 0);
393 for (pdtok
= pds
; *pdtok
&& **pdtok
; pdtok
++) {
394 keyval
= g_strsplit(*pdtok
, "=", 0);
395 if (!(dec
= srd_decoder_get_by_id(keyval
[0]))) {
396 g_critical("Protocol decoder '%s' not found.", keyval
[0]);
400 g_critical("Protocol decoder '%s' has no binary output.", keyval
[0]);
404 if (g_strv_length(keyval
) == 2) {
405 for (l
= dec
->binary
; l
; l
= l
->next
, bin_class
++) {
407 if (!strcmp(bin_name
[0], keyval
[1]))
412 g_critical("binary output '%s' not found "
413 "for protocol decoder '%s'.", keyval
[1], keyval
[0]);
416 g_debug("cli: Showing protocol decoder %s binary class "
417 "%d (%s).", keyval
[0], bin_class
, bin_name
[0]);
419 /* No class specified: output all of them. */
421 g_debug("cli: Showing all binary classes for protocol "
422 "decoder %s.", keyval
[0]);
424 g_hash_table_insert(pd_binary_visible
, g_strdup(keyval
[0]), GINT_TO_POINTER(bin_class
));
432 void show_pd_annotations(struct srd_proto_data
*pdata
, void *cb_data
)
434 struct srd_decoder
*dec
;
435 struct srd_proto_data_annotation
*pda
;
440 /* 'cb_data' is not used in this specific callback. */
446 if (!g_hash_table_lookup_extended(pd_ann_visible
, pdata
->pdo
->di
->inst_id
,
448 /* Not in the list of PDs whose annotations we're showing. */
451 format
= GPOINTER_TO_INT(ann_format
);
452 dec
= pdata
->pdo
->di
->decoder
;
454 if (format
!= -1 && pda
->ann_format
!= format
)
455 /* We don't want this particular format from the PD. */
458 if (opt_loglevel
<= SR_LOG_WARN
) {
459 /* Show only the longest annotation. */
460 printf("%s", pda
->ann_text
[0]);
461 } else if (opt_loglevel
>= SR_LOG_INFO
) {
462 /* Sample numbers and quotes around the longest annotation. */
463 printf("%"PRIu64
"-%"PRIu64
"", pdata
->start_sample
, pdata
->end_sample
);
464 if (opt_loglevel
== SR_LOG_INFO
) {
465 printf(" \"%s\"", pda
->ann_text
[0]);
467 /* Protocol decoder id, annotation class,
468 * all annotation strings. */
469 ann_descr
= g_slist_nth_data(dec
->annotations
, pda
->ann_format
);
470 printf(" %s: %s:", pdata
->pdo
->proto_id
, ann_descr
[0]);
471 for (i
= 0; pda
->ann_text
[i
]; i
++)
472 printf(" \"%s\"", pda
->ann_text
[i
]);
479 void show_pd_meta(struct srd_proto_data
*pdata
, void *cb_data
)
482 /* 'cb_data' is not used in this specific callback. */
485 if (!g_hash_table_lookup_extended(pd_meta_visible
,
486 pdata
->pdo
->di
->decoder
->id
, NULL
, NULL
))
487 /* Not in the list of PDs whose meta output we're showing. */
490 if (opt_loglevel
> SR_LOG_WARN
)
491 printf("%"PRIu64
"-%"PRIu64
" ", pdata
->start_sample
, pdata
->end_sample
);
492 printf("%s: ", pdata
->pdo
->proto_id
);
493 printf("%s: %s", pdata
->pdo
->meta_name
, g_variant_print(pdata
->data
, FALSE
));
498 void show_pd_binary(struct srd_proto_data
*pdata
, void *cb_data
)
500 struct srd_proto_data_binary
*pdb
;
504 /* 'cb_data' is not used in this specific callback. */
507 if (!g_hash_table_lookup_extended(pd_binary_visible
,
508 pdata
->pdo
->di
->decoder
->id
, NULL
, (void **)&classp
))
509 /* Not in the list of PDs whose meta output we're showing. */
512 class = GPOINTER_TO_INT(classp
);
514 if (class != -1 && class != pdb
->bin_class
)
515 /* Not showing this binary class. */
518 /* Just send the binary output to stdout, no embellishments. */
519 fwrite(pdb
->data
, pdb
->size
, 1, stdout
);