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