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/>.
22 #include <libsigrok/libsigrok.h>
24 #include <libsigrokdecode/libsigrokdecode.h> /* First, so we avoid a _POSIX_C_SOURCE warning. */
26 #include "sigrok-cli.h"
29 static GHashTable *pd_ann_visible = NULL;
30 static GHashTable *pd_meta_visible = NULL;
31 static GHashTable *pd_binary_visible = NULL;
33 extern struct srd_session *srd_sess;
34 extern gint opt_loglevel;
37 static int opts_to_gvar(struct srd_decoder *dec, GHashTable *hash,
40 struct srd_decoder_option *o;
48 *options = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
49 (GDestroyNotify)g_variant_unref);
51 for (optl = dec->options; optl; optl = optl->next) {
53 if (!(val_str = g_hash_table_lookup(hash, o->id)))
56 if (g_variant_is_of_type(o->def, G_VARIANT_TYPE_STRING)) {
57 gvar = g_variant_new_string(val_str);
58 } else if (g_variant_is_of_type(o->def, G_VARIANT_TYPE_INT64)) {
59 val_int = strtoll(val_str, &conv, 0);
60 if (!conv || conv == val_str) {
61 g_critical("Protocol decoder '%s' option '%s' "
62 "requires a number.", dec->name, o->id);
66 gvar = g_variant_new_int64(val_int);
68 g_critical("Unsupported type for option '%s' (%s)",
69 o->id, g_variant_get_type_string(o->def));
73 g_variant_ref_sink(gvar);
74 g_hash_table_insert(*options, g_strdup(o->id), gvar);
75 g_hash_table_remove(hash, o->id);
81 static int probes_to_gvar(struct srd_decoder *dec, GHashTable *hash,
85 GSList *all_probes, *l;
92 *probes = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
93 (GDestroyNotify)g_variant_unref);
95 all_probes = g_slist_copy(dec->probes);
96 all_probes = g_slist_concat(all_probes, g_slist_copy(dec->opt_probes));
97 for (l = all_probes; l; l = l->next) {
99 if (!(val_str = g_hash_table_lookup(hash, p->id)))
102 val_int = strtoll(val_str, &conv, 10);
103 if (!conv || conv == val_str) {
104 g_critical("Protocol decoder '%s' probes '%s' "
105 "is not a number.", dec->name, p->id);
109 gvar = g_variant_new_int32(val_int);
110 g_variant_ref_sink(gvar);
111 g_hash_table_insert(*probes, g_strdup(p->id), gvar);
112 g_hash_table_remove(hash, p->id);
114 g_slist_free(all_probes);
119 /* Register the given PDs for this session.
120 * Accepts a string of the form: "spi:sck=3:sdata=4,spi:sck=3:sdata=5"
121 * That will instantiate two SPI decoders on the clock but different data
124 int register_pds(const char *opt_pds, char *opt_pd_annotations)
126 struct srd_decoder *dec;
127 GHashTable *pd_opthash, *options, *probes;
129 struct srd_decoder_inst *di;
131 char **pdtokens, **pdtok, *pd_name;
133 pd_ann_visible = g_hash_table_new_full(g_str_hash, g_int_equal,
137 pd_opthash = options = probes = 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);
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);
152 dec = srd_decoder_get_by_id(pd_name);
154 /* Convert decoder option and probe values to GVariant. */
155 if (!opts_to_gvar(dec, pd_opthash, &options)) {
159 if (!probes_to_gvar(dec, pd_opthash, &probes)) {
163 if (g_hash_table_size(pd_opthash) > 0) {
164 leftover = g_hash_table_get_keys(pd_opthash);
165 for (l = leftover; l; l = l->next)
166 g_critical("Unknown option or probe '%s'", (char *)l->data);
167 g_list_free(leftover);
171 if (!(di = srd_inst_new(srd_sess, pd_name, options))) {
172 g_critical("Failed to instantiate protocol decoder %s.", pd_name);
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));
185 /* Remap the probes if needed. */
186 if (srd_inst_probe_set_all(di, probes) != SRD_OK) {
192 g_strfreev(pdtokens);
194 g_hash_table_destroy(pd_opthash);
196 g_hash_table_destroy(options);
198 g_hash_table_destroy(probes);
205 int setup_pd_stack(char *opt_pds, char *opt_pd_stack, char *opt_pd_annotations)
207 struct srd_decoder_inst *di_from, *di_to;
211 /* Set up the protocol decoder stack. */
212 pds = g_strsplit(opt_pds, ",", 0);
213 if (g_strv_length(pds) > 1) {
215 /* A stack setup was specified, use that. */
217 pds = g_strsplit(opt_pd_stack, ",", 0);
218 if (g_strv_length(pds) < 2) {
220 g_critical("Specify at least two protocol decoders to stack.");
225 /* First PD goes at the bottom of the stack. */
226 ids = g_strsplit(pds[0], ":", 0);
227 if (!(di_from = srd_inst_find_by_id(srd_sess, ids[0]))) {
229 g_critical("Cannot stack protocol decoder '%s': "
230 "instance not found.", pds[0]);
235 /* Every subsequent PD goes on top. */
236 for (i = 1; pds[i]; i++) {
237 ids = g_strsplit(pds[i], ":", 0);
238 if (!(di_to = srd_inst_find_by_id(srd_sess, ids[0]))) {
240 g_critical("Cannot stack protocol decoder '%s': "
241 "instance not found.", pds[i]);
245 if ((ret = srd_inst_stack(srd_sess, di_from, di_to)) != SRD_OK)
248 /* Don't show annotation from this PD. Only the last PD in
249 * the stack will be left on the annotation list (unless
250 * the annotation list was specifically provided).
252 if (!opt_pd_annotations)
253 g_hash_table_remove(pd_ann_visible,
264 int setup_pd_annotations(char *opt_pd_annotations)
267 struct srd_decoder *dec;
269 char **pds, **pdtok, **keyval, **ann_descr;
271 /* Set up custom list of PDs and annotations to show. */
272 pds = g_strsplit(opt_pd_annotations, ",", 0);
273 for (pdtok = pds; *pdtok && **pdtok; pdtok++) {
274 keyval = g_strsplit(*pdtok, "=", 0);
275 if (!(dec = srd_decoder_get_by_id(keyval[0]))) {
276 g_critical("Protocol decoder '%s' not found.", keyval[0]);
279 if (!dec->annotations) {
280 g_critical("Protocol decoder '%s' has no annotations.", keyval[0]);
284 if (g_strv_length(keyval) == 2) {
285 for (l = dec->annotations; l; l = l->next, ann_class++) {
287 if (!canon_cmp(ann_descr[0], keyval[1]))
292 g_critical("Annotation '%s' not found "
293 "for protocol decoder '%s'.", keyval[1], keyval[0]);
296 g_debug("cli: Showing protocol decoder %s annotation "
297 "class %d (%s).", keyval[0], ann_class, ann_descr[0]);
299 /* No class specified: show all of them. */
301 g_debug("cli: Showing all annotation classes for protocol "
302 "decoder %s.", keyval[0]);
304 g_hash_table_insert(pd_ann_visible, g_strdup(keyval[0]), GINT_TO_POINTER(ann_class));
312 int setup_pd_meta(char *opt_pd_meta)
314 struct srd_decoder *dec;
317 pd_meta_visible = g_hash_table_new_full(g_str_hash, g_int_equal,
319 pds = g_strsplit(opt_pd_meta, ",", 0);
320 for (pdtok = pds; *pdtok && **pdtok; pdtok++) {
321 if (!(dec = srd_decoder_get_by_id(*pdtok))) {
322 g_critical("Protocol decoder '%s' not found.", *pdtok);
325 g_debug("cli: Showing protocol decoder meta output from '%s'.", *pdtok);
326 g_hash_table_insert(pd_meta_visible, g_strdup(*pdtok), NULL);
333 int setup_pd_binary(char *opt_pd_binary)
336 struct srd_decoder *dec;
338 char **pds, **pdtok, **keyval, *bin_name;
340 pd_binary_visible = g_hash_table_new_full(g_str_hash, g_int_equal,
342 pds = g_strsplit(opt_pd_binary, ",", 0);
343 for (pdtok = pds; *pdtok && **pdtok; pdtok++) {
344 keyval = g_strsplit(*pdtok, "=", 0);
345 if (!(dec = srd_decoder_get_by_id(keyval[0]))) {
346 g_critical("Protocol decoder '%s' not found.", keyval[0]);
350 g_critical("Protocol decoder '%s' has no binary output.", keyval[0]);
354 if (g_strv_length(keyval) == 2) {
355 for (l = dec->binary; l; l = l->next, bin_class++) {
357 if (!canon_cmp(bin_name, keyval[1]))
362 g_critical("binary output '%s' not found "
363 "for protocol decoder '%s'.", keyval[1], keyval[0]);
366 g_debug("cli: Showing protocol decoder %s binary class "
367 "%d (%s).", keyval[0], bin_class, bin_name);
369 /* No class specified: output all of them. */
371 g_debug("cli: Showing all binary classes for protocol "
372 "decoder %s.", keyval[0]);
374 g_hash_table_insert(pd_binary_visible, g_strdup(keyval[0]), GINT_TO_POINTER(bin_class));
382 void show_pd_annotations(struct srd_proto_data *pdata, void *cb_data)
384 struct srd_proto_data_annotation *pda;
388 /* 'cb_data' is not used in this specific callback. */
394 if (!g_hash_table_lookup_extended(pd_ann_visible, pdata->pdo->di->inst_id,
396 /* Not in the list of PDs whose annotations we're showing. */
399 format = GPOINTER_TO_INT(ann_format);
401 if (format != -1 && pda->ann_format != format)
402 /* We don't want this particular format from the PD. */
405 if (opt_loglevel > SR_LOG_WARN)
406 printf("%"PRIu64"-%"PRIu64" ", pdata->start_sample, pdata->end_sample);
407 printf("%s: ", pdata->pdo->proto_id);
408 /* Show only the longest annotation. */
409 printf("\"%s\" ", pda->ann_text[0]);
414 void show_pd_meta(struct srd_proto_data *pdata, void *cb_data)
417 /* 'cb_data' is not used in this specific callback. */
420 if (!g_hash_table_lookup_extended(pd_meta_visible,
421 pdata->pdo->di->decoder->id, NULL, NULL))
422 /* Not in the list of PDs whose meta output we're showing. */
425 if (opt_loglevel > SR_LOG_WARN)
426 printf("%"PRIu64"-%"PRIu64" ", pdata->start_sample, pdata->end_sample);
427 printf("%s: ", pdata->pdo->proto_id);
428 printf("%s: %s", pdata->pdo->meta_name, g_variant_print(pdata->data, FALSE));
433 void show_pd_binary(struct srd_proto_data *pdata, void *cb_data)
435 struct srd_proto_data_binary *pdb;
439 /* 'cb_data' is not used in this specific callback. */
442 if (!g_hash_table_lookup_extended(pd_binary_visible,
443 pdata->pdo->di->decoder->id, NULL, (void **)&classp))
444 /* Not in the list of PDs whose meta output we're showing. */
447 class = GPOINTER_TO_INT(classp);
449 if (class != -1 && class != pdb->bin_class)
450 /* Not showing this binary class. */
453 /* Just send the binary output to stdout, no embellishments. */
454 fwrite(pdb->data, pdb->size, 1, stdout);