]> sigrok.org Git - sigrok-cli.git/blob - decode.c
sigrok-cli: AC_ARG_WITH uses withval, not enableval
[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_channel_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_channel_map(struct srd_decoder *dec, GHashTable *hash)
92 {
93         GHashTable *channel_map;
94         struct srd_channel *pdch;
95         GSList *l;
96
97         channel_map = g_hash_table_new_full(g_str_hash, g_str_equal,
98                                           g_free, g_free);
99
100         for (l = dec->channels; l; l = l->next) {
101                 pdch = l->data;
102                 move_hash_element(hash, channel_map, pdch->id);
103         }
104         for (l = dec->opt_channels; l; l = l->next) {
105                 pdch = l->data;
106                 move_hash_element(hash, channel_map, pdch->id);
107         }
108
109         return channel_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, *channels;
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         ret = 0;
129         pd_name = NULL;
130         pd_opthash = options = channels = pd_channel_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);
135                         break;
136                 }
137
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);
142                         ret = 1;
143                         break;
144                 }
145                 dec = srd_decoder_get_by_id(pd_name);
146
147                 /* Convert decoder option and channel values to GVariant. */
148                 if (!opts_to_gvar(dec, pd_opthash, &options)) {
149                         ret = 1;
150                         break;
151                 }
152                 channels = extract_channel_map(dec, pd_opthash);
153
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 channel '%s'", (char *)l->data);
158                         g_list_free(leftover);
159                         break;
160                 }
161
162                 if (!(di = srd_inst_new(srd_sess, pd_name, options))) {
163                         g_critical("Failed to instantiate protocol decoder %s.", pd_name);
164                         ret = 1;
165                         break;
166                 }
167
168                 if (pdtok == pdtokens) {
169                         /*
170                          * Save the channel setup for later, but only on the
171                          * first decoder (stacked decoders don't get channels).
172                          */
173                         pd_channel_maps = g_hash_table_new_full(g_str_hash,
174                                         g_str_equal, g_free, (GDestroyNotify)g_hash_table_destroy);
175                         g_hash_table_insert(pd_channel_maps, g_strdup(di->inst_id), channels);
176                         channels = NULL;
177                 }
178
179                 /* If no annotation list was specified, add them all in now.
180                  * This will be pared down later to leave only the last PD
181                  * in the stack.
182                  */
183                 if (!opt_pd_annotations)
184                         g_hash_table_insert(pd_ann_visible, g_strdup(di->inst_id),
185                                         g_slist_append(NULL, GINT_TO_POINTER(-1)));
186         }
187
188         g_strfreev(pdtokens);
189         if (pd_opthash)
190                 g_hash_table_destroy(pd_opthash);
191         if (options)
192                 g_hash_table_destroy(options);
193         if (channels)
194                 g_hash_table_destroy(channels);
195         if (pd_name)
196                 g_free(pd_name);
197
198         return ret;
199 }
200
201 static void map_pd_inst_channels(void *key, void *value, void *user_data)
202 {
203         GHashTable *channel_map;
204         GHashTable *channel_indices;
205         GSList *channel_list;
206         struct srd_decoder_inst *di;
207         GVariant *var;
208         void *channel_id;
209         void *channel_target;
210         struct sr_channel *ch;
211         GHashTableIter iter;
212         int num_channels;
213
214         channel_map = value;
215         channel_list = user_data;
216
217         di = srd_inst_find_by_id(srd_sess, key);
218         if (!di) {
219                 g_critical("Protocol decoder instance \"%s\" not found.",
220                            (char *)key);
221                 return;
222         }
223         channel_indices = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
224                                               (GDestroyNotify)g_variant_unref);
225
226         g_hash_table_iter_init(&iter, channel_map);
227         while (g_hash_table_iter_next(&iter, &channel_id, &channel_target)) {
228                 ch = find_channel(channel_list, channel_target);
229                 if (!ch) {
230                         g_printerr("cli: No channel with name \"%s\" found.\n",
231                                    (char *)channel_target);
232                         continue;
233                 }
234                 if (!ch->enabled)
235                         g_printerr("cli: Target channel \"%s\" not enabled.\n",
236                                    (char *)channel_target);
237
238                 var = g_variant_new_int32(ch->index);
239                 g_variant_ref_sink(var);
240                 g_hash_table_insert(channel_indices, g_strdup(channel_id), var);
241         }
242
243         num_channels = g_slist_length(channel_list);
244         srd_inst_channel_set_all(di, channel_indices, (num_channels + 7) / 8);
245 }
246
247 void map_pd_channels(struct sr_dev_inst *sdi)
248 {
249         if (pd_channel_maps) {
250                 g_hash_table_foreach(pd_channel_maps, &map_pd_inst_channels,
251                                      sdi->channels);
252                 g_hash_table_destroy(pd_channel_maps);
253                 pd_channel_maps = NULL;
254         }
255 }
256
257 int setup_pd_stack(char *opt_pds, char *opt_pd_stack, char *opt_pd_annotations)
258 {
259         struct srd_decoder_inst *di_from, *di_to;
260         int ret, i;
261         char **pds, **ids;
262
263         /* Set up the protocol decoder stack. */
264         pds = g_strsplit(opt_pds, ",", 0);
265         if (g_strv_length(pds) > 1) {
266                 if (opt_pd_stack) {
267                         /* A stack setup was specified, use that. */
268                         g_strfreev(pds);
269                         pds = g_strsplit(opt_pd_stack, ",", 0);
270                         if (g_strv_length(pds) < 2) {
271                                 g_strfreev(pds);
272                                 g_critical("Specify at least two protocol decoders to stack.");
273                                 return 1;
274                         }
275                 }
276
277                 /* First PD goes at the bottom of the stack. */
278                 ids = g_strsplit(pds[0], ":", 0);
279                 if (!(di_from = srd_inst_find_by_id(srd_sess, ids[0]))) {
280                         g_strfreev(ids);
281                         g_critical("Cannot stack protocol decoder '%s': "
282                                         "instance not found.", pds[0]);
283                         return 1;
284                 }
285                 g_strfreev(ids);
286
287                 /* Every subsequent PD goes on top. */
288                 for (i = 1; pds[i]; i++) {
289                         ids = g_strsplit(pds[i], ":", 0);
290                         if (!(di_to = srd_inst_find_by_id(srd_sess, ids[0]))) {
291                                 g_strfreev(ids);
292                                 g_critical("Cannot stack protocol decoder '%s': "
293                                                 "instance not found.", pds[i]);
294                                 return 1;
295                         }
296                         g_strfreev(ids);
297                         if ((ret = srd_inst_stack(srd_sess, di_from, di_to)) != SRD_OK)
298                                 return 1;
299
300                         /* Don't show annotation from this PD. Only the last PD in
301                          * the stack will be left on the annotation list (unless
302                          * the annotation list was specifically provided).
303                          */
304                         if (!opt_pd_annotations)
305                                 g_hash_table_remove(pd_ann_visible, 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, *l_ann;
318         struct srd_decoder *dec;
319         int ann_class;
320         char **pds, **pdtok, **keyval, **annlist, **ann, **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                 if (g_strv_length(keyval) == 2 && keyval[1][0] != '\0') {
335                         annlist = g_strsplit(keyval[1], ":", 0);
336                         for (ann = annlist; *ann && **ann; ann++) {
337                                 ann_class = 0;
338                                 for (l = dec->annotations; l; l = l->next, ann_class++) {
339                                         ann_descr = l->data;
340                                         if (!canon_cmp(ann_descr[0], *ann))
341                                                 /* Found it. */
342                                                 break;
343                                 }
344                                 if (!l) {
345                                         g_critical("Annotation '%s' not found "
346                                                         "for protocol decoder '%s'.", *ann, keyval[0]);
347                                         return 1;
348                                 }
349                                 l_ann = g_hash_table_lookup(pd_ann_visible, keyval[0]);
350                                 l_ann = g_slist_append(l_ann, GINT_TO_POINTER(ann_class));
351                                 g_hash_table_replace(pd_ann_visible, g_strdup(keyval[0]), l_ann);
352                                 g_debug("cli: Showing protocol decoder %s annotation "
353                                                 "class %d (%s).", keyval[0], ann_class, ann_descr[0]);
354                         }
355                 } else {
356                         /* No class specified: show all of them. */
357                                 g_hash_table_insert(pd_ann_visible, g_strdup(keyval[0]),
358                                                 g_slist_append(NULL, GINT_TO_POINTER(-1)));
359                         g_debug("cli: Showing all annotation classes for protocol "
360                                         "decoder %s.", keyval[0]);
361                 }
362                 g_strfreev(keyval);
363         }
364         g_strfreev(pds);
365
366         return 0;
367 }
368
369 int setup_pd_meta(char *opt_pd_meta)
370 {
371         struct srd_decoder *dec;
372         char **pds, **pdtok;
373
374         pd_meta_visible = g_hash_table_new_full(g_str_hash, g_int_equal,
375                         g_free, NULL);
376         pds = g_strsplit(opt_pd_meta, ",", 0);
377         for (pdtok = pds; *pdtok && **pdtok; pdtok++) {
378                 if (!(dec = srd_decoder_get_by_id(*pdtok))) {
379                         g_critical("Protocol decoder '%s' not found.", *pdtok);
380                         return 1;
381                 }
382                 g_debug("cli: Showing protocol decoder meta output from '%s'.", *pdtok);
383                 g_hash_table_insert(pd_meta_visible, g_strdup(*pdtok), NULL);
384         }
385         g_strfreev(pds);
386
387         return 0;
388 }
389
390 int setup_pd_binary(char *opt_pd_binary)
391 {
392         GSList *l;
393         struct srd_decoder *dec;
394         int bin_class;
395         char **pds, **pdtok, **keyval, **bin_name;
396
397         pd_binary_visible = g_hash_table_new_full(g_str_hash, g_int_equal,
398                         g_free, NULL);
399         pds = g_strsplit(opt_pd_binary, ",", 0);
400         for (pdtok = pds; *pdtok && **pdtok; pdtok++) {
401                 keyval = g_strsplit(*pdtok, "=", 0);
402                 if (!(dec = srd_decoder_get_by_id(keyval[0]))) {
403                         g_critical("Protocol decoder '%s' not found.", keyval[0]);
404                         return 1;
405                 }
406                 if (!dec->binary) {
407                         g_critical("Protocol decoder '%s' has no binary output.", keyval[0]);
408                         return 1;
409                 }
410                 bin_class = 0;
411                 if (g_strv_length(keyval) == 2) {
412                         for (l = dec->binary; l; l = l->next, bin_class++) {
413                                 bin_name = l->data;
414                                 if (!strcmp(bin_name[0], keyval[1]))
415                                         /* Found it. */
416                                         break;
417                         }
418                         if (!l) {
419                                 g_critical("binary output '%s' not found "
420                                                 "for protocol decoder '%s'.", keyval[1], keyval[0]);
421                                 return 1;
422                         }
423                         g_debug("cli: Showing protocol decoder %s binary class "
424                                         "%d (%s).", keyval[0], bin_class, bin_name[0]);
425                 } else {
426                         /* No class specified: output all of them. */
427                         bin_class = -1;
428                         g_debug("cli: Showing all binary classes for protocol "
429                                         "decoder %s.", keyval[0]);
430                 }
431                 g_hash_table_insert(pd_binary_visible, g_strdup(keyval[0]), GINT_TO_POINTER(bin_class));
432                 g_strfreev(keyval);
433         }
434         g_strfreev(pds);
435
436         return 0;
437 }
438
439 void show_pd_annotations(struct srd_proto_data *pdata, void *cb_data)
440 {
441         struct srd_decoder *dec;
442         struct srd_proto_data_annotation *pda;
443         GSList *ann_list, *l;
444         int i;
445         char **ann_descr;
446         gboolean show;
447
448         /* 'cb_data' is not used in this specific callback. */
449         (void)cb_data;
450
451         if (!pd_ann_visible)
452                 return;
453
454         if (!g_hash_table_lookup_extended(pd_ann_visible, pdata->pdo->di->inst_id,
455                         NULL, (void **)&ann_list))
456                 /* Not in the list of PDs whose annotations we're showing. */
457                 return;
458
459         dec = pdata->pdo->di->decoder;
460         pda = pdata->data;
461         show = FALSE;
462         for (l = ann_list; l; l = l->next) {
463                 if (GPOINTER_TO_INT(l->data) == -1
464                                 || GPOINTER_TO_INT(l->data) == pda->ann_format) {
465                         show = TRUE;
466                         break;
467                 }
468         }
469         if (!show)
470                 return;
471
472         if (opt_loglevel <= SR_LOG_WARN) {
473                 /* Show only the longest annotation. */
474                 printf("%s", pda->ann_text[0]);
475         } else if (opt_loglevel >= SR_LOG_INFO) {
476                 /* Sample numbers and quotes around the longest annotation. */
477                 printf("%"PRIu64"-%"PRIu64"", pdata->start_sample, pdata->end_sample);
478                 if (opt_loglevel == SR_LOG_INFO) {
479                         printf(" \"%s\"", pda->ann_text[0]);
480                 } else {
481                         /* Protocol decoder id, annotation class,
482                          * all annotation strings. */
483                         ann_descr = g_slist_nth_data(dec->annotations, pda->ann_format);
484                         printf(" %s: %s:", pdata->pdo->proto_id, ann_descr[0]);
485                         for (i = 0; pda->ann_text[i]; i++)
486                                 printf(" \"%s\"", pda->ann_text[i]);
487                 }
488         }
489         printf("\n");
490         fflush(stdout);
491 }
492
493 void show_pd_meta(struct srd_proto_data *pdata, void *cb_data)
494 {
495
496         /* 'cb_data' is not used in this specific callback. */
497         (void)cb_data;
498
499         if (!g_hash_table_lookup_extended(pd_meta_visible,
500                         pdata->pdo->di->decoder->id, NULL, NULL))
501                 /* Not in the list of PDs whose meta output we're showing. */
502                 return;
503
504         if (opt_loglevel > SR_LOG_WARN)
505                 printf("%"PRIu64"-%"PRIu64" ", pdata->start_sample, pdata->end_sample);
506         printf("%s: ", pdata->pdo->proto_id);
507         printf("%s: %s", pdata->pdo->meta_name, g_variant_print(pdata->data, FALSE));
508         printf("\n");
509         fflush(stdout);
510 }
511
512 void show_pd_binary(struct srd_proto_data *pdata, void *cb_data)
513 {
514         struct srd_proto_data_binary *pdb;
515         gpointer classp;
516         int class;
517
518         /* 'cb_data' is not used in this specific callback. */
519         (void)cb_data;
520
521         if (!g_hash_table_lookup_extended(pd_binary_visible,
522                         pdata->pdo->di->decoder->id, NULL, (void **)&classp))
523                 /* Not in the list of PDs whose meta output we're showing. */
524                 return;
525
526         class = GPOINTER_TO_INT(classp);
527         pdb = pdata->data;
528         if (class != -1 && class != pdb->bin_class)
529                 /* Not showing this binary class. */
530                 return;
531
532         /* Just send the binary output to stdout, no embellishments. */
533         fwrite(pdb->data, pdb->size, 1, stdout);
534         fflush(stdout);
535 }
536 #endif
537