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