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