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