7d02326e86bad5860b698c38a9d4106fcd88d61f
[sigrok-cli.git] / show.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 <glib.h>
22 #include <string.h>
23 #include "sigrok-cli.h"
24
25 static gint sort_inputs(gconstpointer a, gconstpointer b)
26 {
27         return strcmp(sr_input_id_get((struct sr_input_module *)a),
28                         sr_input_id_get((struct sr_input_module *)b));
29 }
30
31 static gint sort_outputs(gconstpointer a, gconstpointer b)
32 {
33         return strcmp(sr_output_id_get((struct sr_output_module *)a),
34                         sr_output_id_get((struct sr_output_module *)b));
35 }
36
37 static gint sort_transforms(gconstpointer a, gconstpointer b)
38 {
39         return strcmp(sr_transform_id_get((struct sr_transform_module *)a),
40                         sr_transform_id_get((struct sr_transform_module *)b));
41 }
42
43 static gint sort_drivers(gconstpointer a, gconstpointer b)
44 {
45         const struct sr_dev_driver *sdda = a, *sddb = b;
46
47         return strcmp(sdda->name, sddb->name);
48 }
49
50 #ifdef HAVE_SRD
51 static gint sort_pds(gconstpointer a, gconstpointer b)
52 {
53         const struct srd_decoder *sda = a, *sdb = b;
54
55         return strcmp(sda->id, sdb->id);
56 }
57 #endif
58
59 void show_version(void)
60 {
61         struct sr_dev_driver **drivers, *driver;
62         const struct sr_input_module **inputs, *input;
63         const struct sr_output_module **outputs, *output;
64         const struct sr_transform_module **transforms, *transform;
65         const GSList *l;
66         GSList *sl;
67         int i;
68 #ifdef HAVE_SRD
69         struct srd_decoder *dec;
70 #endif
71
72         printf("sigrok-cli %s\n\n", SC_PACKAGE_VERSION_STRING);
73
74         printf("Using libsigrok %s (lib version %s).\n",
75                sr_package_version_string_get(), sr_lib_version_string_get());
76 #ifdef HAVE_SRD
77         printf("Using libsigrokdecode %s (lib version %s).\n\n",
78                srd_package_version_string_get(), srd_lib_version_string_get());
79 #endif
80
81         printf("Supported hardware drivers:\n");
82         drivers = sr_driver_list(sr_ctx);
83         for (sl = NULL, i = 0; drivers[i]; i++)
84                 sl = g_slist_append(sl, drivers[i]);
85         sl = g_slist_sort(sl, sort_drivers);
86         for (l = sl; l; l = l->next) {
87                 driver = l->data;
88                 printf("  %-20s %s\n", driver->name, driver->longname);
89         }
90         printf("\n");
91         g_slist_free(sl);
92
93         printf("Supported input formats:\n");
94         inputs = sr_input_list();
95         for (sl = NULL, i = 0; inputs[i]; i++)
96                 sl = g_slist_append(sl, (gpointer)inputs[i]);
97         sl = g_slist_sort(sl, sort_inputs);
98         for (l = sl; l; l = l->next) {
99                 input = l->data;
100                 printf("  %-20s %s\n", sr_input_id_get(input),
101                                 sr_input_description_get(input));
102         }
103         printf("\n");
104         g_slist_free(sl);
105
106         printf("Supported output formats:\n");
107         outputs = sr_output_list();
108         for (sl = NULL, i = 0; outputs[i]; i++)
109                 sl = g_slist_append(sl, (gpointer)outputs[i]);
110         sl = g_slist_sort(sl, sort_outputs);
111         for (l = sl; l; l = l->next) {
112                 output = l->data;
113                 printf("  %-20s %s\n", sr_output_id_get(output),
114                                 sr_output_description_get(output));
115         }
116         printf("\n");
117         g_slist_free(sl);
118
119         printf("Supported transform modules:\n");
120         transforms = sr_transform_list();
121         for (sl = NULL, i = 0; transforms[i]; i++)
122                 sl = g_slist_append(sl, (gpointer)transforms[i]);
123         sl = g_slist_sort(sl, sort_transforms);
124         for (l = sl; l; l = l->next) {
125                 transform = l->data;
126                 printf("  %-20s %s\n", sr_transform_id_get(transform),
127                                 sr_transform_description_get(transform));
128         }
129         printf("\n");
130         g_slist_free(sl);
131
132 #ifdef HAVE_SRD
133         if (srd_init(NULL) == SRD_OK) {
134                 printf("Supported protocol decoders:\n");
135                 srd_decoder_load_all();
136                 sl = g_slist_copy((GSList *)srd_decoder_list());
137                 sl = g_slist_sort(sl, sort_pds);
138                 for (l = sl; l; l = l->next) {
139                         dec = l->data;
140                         printf("  %-20s %s\n", dec->id, dec->longname);
141                         /* Print protocol description upon "-l 3" or higher. */
142                         if (opt_loglevel >= SR_LOG_INFO)
143                                 printf("  %-20s %s\n", "", dec->desc);
144                 }
145                 g_slist_free(sl);
146                 srd_exit();
147         }
148         printf("\n");
149 #endif
150 }
151
152 static gint sort_channels(gconstpointer a, gconstpointer b)
153 {
154         const struct sr_channel *pa = a, *pb = b;
155
156         return pa->index - pb->index;
157 }
158
159 static void print_dev_line(const struct sr_dev_inst *sdi)
160 {
161         struct sr_channel *ch;
162         GSList *sl, *l, *channels;
163         GString *s;
164         GVariant *gvar;
165         struct sr_dev_driver *driver;
166         const char *vendor, *model, *version;
167
168         driver = sr_dev_inst_driver_get(sdi);
169         vendor = sr_dev_inst_vendor_get(sdi);
170         model = sr_dev_inst_model_get(sdi);
171         version = sr_dev_inst_version_get(sdi);
172         channels = sr_dev_inst_channels_get(sdi);
173
174         s = g_string_sized_new(128);
175         g_string_assign(s, driver->name);
176         if (maybe_config_get(driver, sdi, NULL, SR_CONF_CONN, &gvar) == SR_OK) {
177                 g_string_append(s, ":conn=");
178                 g_string_append(s, g_variant_get_string(gvar, NULL));
179                 g_variant_unref(gvar);
180         }
181         g_string_append(s, " - ");
182         if (vendor && vendor[0])
183                 g_string_append_printf(s, "%s ", vendor);
184         if (model && model[0])
185                 g_string_append_printf(s, "%s ", model);
186         if (version && version[0])
187                 g_string_append_printf(s, "%s ", version);
188         if (channels) {
189                 if (g_slist_length(channels) == 1) {
190                         ch = channels->data;
191                         g_string_append_printf(s, "with 1 channel: %s", ch->name);
192                 } else {
193                         sl = g_slist_sort(g_slist_copy(channels), sort_channels);
194                         g_string_append_printf(s, "with %d channels:", g_slist_length(sl));
195                         for (l = sl; l; l = l->next) {
196                                 ch = l->data;
197                                 g_string_append_printf(s, " %s", ch->name);
198                         }
199                         g_slist_free(sl);
200                 }
201         }
202         g_string_append_printf(s, "\n");
203         printf("%s", s->str);
204         g_string_free(s, TRUE);
205
206 }
207
208 void show_dev_list(void)
209 {
210         struct sr_dev_inst *sdi;
211         GSList *devices, *l;
212
213         if (!(devices = device_scan()))
214                 return;
215
216         printf("The following devices were found:\n");
217         for (l = devices; l; l = l->next) {
218                 sdi = l->data;
219                 print_dev_line(sdi);
220         }
221         g_slist_free(devices);
222
223 }
224
225 void show_drv_detail(struct sr_dev_driver *driver)
226 {
227         const struct sr_key_info *srci;
228         GArray *opts;
229         guint i;
230
231         if ((opts = sr_dev_options(driver, NULL, NULL))) {
232                 if (opts->len > 0) {
233                         printf("Driver functions:\n");
234                         for (i = 0; i < opts->len; i++) {
235                                 if (!(srci = sr_key_info_get(SR_KEY_CONFIG,
236                                                 g_array_index(opts, uint32_t, i))))
237                                         continue;
238                                 printf("    %s\n", srci->name);
239                         }
240                 }
241                 g_array_free(opts, TRUE);
242         }
243
244         if ((opts = sr_driver_scan_options_list(driver))) {
245                 if (opts->len > 0) {
246                         printf("Scan options:\n");
247                         for (i = 0; i < opts->len; i++) {
248                                 if (!(srci = sr_key_info_get(SR_KEY_CONFIG,
249                                                 g_array_index(opts, uint32_t, i))))
250                                         continue;
251                                 printf("    %s\n", srci->id);
252                         }
253                 }
254                 g_array_free(opts, TRUE);
255         }
256 }
257
258 void show_dev_detail(void)
259 {
260         struct sr_dev_driver *driver_from_opt, *driver;
261         struct sr_dev_inst *sdi;
262         const struct sr_key_info *srci, *srmqi, *srmqfi;
263         struct sr_channel *ch;
264         struct sr_channel_group *channel_group, *cg;
265         GSList *devices, *cgl, *chl, *channel_groups;
266         GVariant *gvar_dict, *gvar_list, *gvar;
267         gsize num_elements;
268         double dlow, dhigh, dcur_low, dcur_high;
269         const uint64_t *uint64, p, q, low, high;
270         uint64_t tmp_uint64, mask, cur_low, cur_high, cur_p, cur_q;
271         GArray *opts;
272         const int32_t *int32;
273         uint32_t key, o, cur_mq, mq;
274         uint64_t cur_mqflags, mqflags;
275         unsigned int num_devices, i, j;
276         char *tmp_str, *s, c;
277         const char **stropts;
278
279         if (parse_driver(opt_drv, &driver_from_opt, NULL)) {
280                 /* A driver was specified, report driver-wide options now. */
281                 show_drv_detail(driver_from_opt);
282         }
283
284         if (!(devices = device_scan())) {
285                 g_critical("No devices found.");
286                 return;
287         }
288
289         num_devices = g_slist_length(devices);
290         if (num_devices > 1) {
291                 g_critical("%d devices found. Use --scan to show them, "
292                                 "and select one to show.", num_devices);
293                 return;
294         }
295
296         sdi = devices->data;
297         g_slist_free(devices);
298         print_dev_line(sdi);
299
300         driver = sr_dev_inst_driver_get(sdi);
301         channel_groups = sr_dev_inst_channel_groups_get(sdi);
302
303         if (sr_dev_open(sdi) != SR_OK) {
304                 g_critical("Failed to open device.");
305                 return;
306         }
307
308         /*
309          * Selected channels and channel group may affect which options are
310          * returned, or which values for them.
311          */
312         select_channels(sdi);
313         channel_group = select_channel_group(sdi);
314
315         if (!(opts = sr_dev_options(driver, sdi, channel_group)))
316                 /* Driver supports no device instance options. */
317                 return;
318
319         if (channel_groups) {
320                 printf("Channel groups:\n");
321                 for (cgl = channel_groups; cgl; cgl = cgl->next) {
322                         cg = cgl->data;
323                         printf("    %s: channel%s", cg->name,
324                                         g_slist_length(cg->channels) > 1 ? "s" : "");
325                         for (chl = cg->channels; chl; chl = chl->next) {
326                                 ch = chl->data;
327                                 printf(" %s", ch->name);
328                         }
329                         printf("\n");
330                 }
331         }
332
333         printf("Supported configuration options");
334         if (channel_groups) {
335                 if (!channel_group)
336                         printf(" across all channel groups");
337                 else
338                         printf(" on channel group %s", channel_group->name);
339         }
340         printf(":\n");
341         for (o = 0; o < opts->len; o++) {
342                 key = g_array_index(opts, uint32_t, o);
343                 if (!(srci = sr_key_info_get(SR_KEY_CONFIG, key)))
344                         continue;
345
346                 if (key == SR_CONF_TRIGGER_MATCH) {
347                         if (maybe_config_list(driver, sdi, channel_group, key,
348                                         &gvar_list) != SR_OK) {
349                                 printf("\n");
350                                 continue;
351                         }
352                         int32 = g_variant_get_fixed_array(gvar_list,
353                                         &num_elements, sizeof(int32_t));
354                         printf("    Supported triggers: ");
355                         for (i = 0; i < num_elements; i++) {
356                                 switch (int32[i]) {
357                                 case SR_TRIGGER_ZERO:
358                                         c = '0';
359                                         break;
360                                 case SR_TRIGGER_ONE:
361                                         c = '1';
362                                         break;
363                                 case SR_TRIGGER_RISING:
364                                         c = 'r';
365                                         break;
366                                 case SR_TRIGGER_FALLING:
367                                         c = 'f';
368                                         break;
369                                 case SR_TRIGGER_EDGE:
370                                         c = 'e';
371                                         break;
372                                 case SR_TRIGGER_OVER:
373                                         c = 'o';
374                                         break;
375                                 case SR_TRIGGER_UNDER:
376                                         c = 'u';
377                                         break;
378                                 default:
379                                         c = 0;
380                                         break;
381                                 }
382                                 if (c)
383                                         printf("%c ", c);
384                         }
385                         printf("\n");
386                         g_variant_unref(gvar_list);
387
388                 } else if (key == SR_CONF_LIMIT_SAMPLES
389                                 && (sr_dev_config_capabilities_list(sdi, NULL, key)
390                                         & SR_CONF_LIST)) {
391                         /*
392                          * If implemented in config_list(), this denotes the
393                          * maximum number of samples a device can send. This
394                          * really applies only to logic analyzers, and then
395                          * only to those that don't support compression, or
396                          * have it turned off by default. The values returned
397                          * are the low/high limits.
398                          */
399                         if (sr_config_list(driver, sdi, channel_group, key,
400                                         &gvar) == SR_OK) {
401                                 g_variant_get(gvar, "(tt)", &low, &high);
402                                 g_variant_unref(gvar);
403                                 printf("    Maximum number of samples: %"PRIu64"\n", high);
404                         }
405
406                 } else if (key == SR_CONF_SAMPLERATE) {
407                         /* Supported samplerates */
408                         printf("    %s", srci->id);
409                         if (maybe_config_list(driver, sdi, channel_group, SR_CONF_SAMPLERATE,
410                                         &gvar_dict) != SR_OK) {
411                                 printf("\n");
412                                 continue;
413                         }
414                         if ((gvar_list = g_variant_lookup_value(gvar_dict,
415                                         "samplerates", G_VARIANT_TYPE("at")))) {
416                                 uint64 = g_variant_get_fixed_array(gvar_list,
417                                                 &num_elements, sizeof(uint64_t));
418                                 printf(" - supported samplerates:\n");
419                                 for (i = 0; i < num_elements; i++) {
420                                         if (!(s = sr_samplerate_string(uint64[i])))
421                                                 continue;
422                                         printf("      %s\n", s);
423                                         g_free(s);
424                                 }
425                                 g_variant_unref(gvar_list);
426                         } else if ((gvar_list = g_variant_lookup_value(gvar_dict,
427                                         "samplerate-steps", G_VARIANT_TYPE("at")))) {
428                                 uint64 = g_variant_get_fixed_array(gvar_list,
429                                                 &num_elements, sizeof(uint64_t));
430                                 /* low */
431                                 if (!(s = sr_samplerate_string(uint64[0])))
432                                         continue;
433                                 printf(" (%s", s);
434                                 g_free(s);
435                                 /* high */
436                                 if (!(s = sr_samplerate_string(uint64[1])))
437                                         continue;
438                                 printf(" - %s", s);
439                                 g_free(s);
440                                 /* step */
441                                 if (!(s = sr_samplerate_string(uint64[2])))
442                                         continue;
443                                 printf(" in steps of %s)\n", s);
444                                 g_free(s);
445                                 g_variant_unref(gvar_list);
446                         }
447                         g_variant_unref(gvar_dict);
448
449                 } else if (srci->datatype == SR_T_UINT64) {
450                         printf("    %s: ", srci->id);
451                         gvar = NULL;
452                         if (maybe_config_get(driver, sdi, channel_group, key,
453                                         &gvar) == SR_OK) {
454                                 tmp_uint64 = g_variant_get_uint64(gvar);
455                                 g_variant_unref(gvar);
456                         } else
457                                 tmp_uint64 = 0;
458                         if (maybe_config_list(driver, sdi, channel_group,
459                                         key, &gvar_list) != SR_OK) {
460                                 if (gvar) {
461                                         /* Can't list it, but we have a value to show. */
462                                         printf("%"PRIu64" (current)", tmp_uint64);
463                                 }
464                                 printf("\n");
465                                 continue;
466                         }
467                         uint64 = g_variant_get_fixed_array(gvar_list,
468                                         &num_elements, sizeof(uint64_t));
469                         printf(" - supported values:\n");
470                         for (i = 0; i < num_elements; i++) {
471                                 printf("      %"PRIu64, uint64[i]);
472                                 if (gvar && tmp_uint64 == uint64[i])
473                                         printf(" (current)");
474                                 printf("\n");
475                         }
476                         g_variant_unref(gvar_list);
477
478                 } else if (srci->datatype == SR_T_STRING) {
479                         printf("    %s: ", srci->id);
480                         if (maybe_config_get(driver, sdi, channel_group, key,
481                                         &gvar) == SR_OK) {
482                                 tmp_str = g_strdup(g_variant_get_string(gvar, NULL));
483                                 g_variant_unref(gvar);
484                         } else
485                                 tmp_str = NULL;
486
487                         if (maybe_config_list(driver, sdi, channel_group, key,
488                                         &gvar) != SR_OK) {
489                                 if (tmp_str) {
490                                         /* Can't list it, but we have a value to show. */
491                                         printf("%s (current)", tmp_str);
492                                 }
493                                 printf("\n");
494                                 g_free(tmp_str);
495                                 continue;
496                         }
497
498                         stropts = g_variant_get_strv(gvar, &num_elements);
499                         for (i = 0; i < num_elements; i++) {
500                                 if (i)
501                                         printf(", ");
502                                 printf("%s", stropts[i]);
503                                 if (tmp_str && !strcmp(tmp_str, stropts[i]))
504                                         printf(" (current)");
505                         }
506                         printf("\n");
507                         g_free(stropts);
508                         g_free(tmp_str);
509                         g_variant_unref(gvar);
510
511                 } else if (srci->datatype == SR_T_UINT64_RANGE) {
512                         printf("    %s: ", srci->id);
513                         if (maybe_config_list(driver, sdi, channel_group, key,
514                                         &gvar_list) != SR_OK) {
515                                 printf("\n");
516                                 continue;
517                         }
518
519                         if (maybe_config_get(driver, sdi, channel_group, key, &gvar) == SR_OK) {
520                                 g_variant_get(gvar, "(tt)", &cur_low, &cur_high);
521                                 g_variant_unref(gvar);
522                         } else {
523                                 cur_low = 0;
524                                 cur_high = 0;
525                         }
526
527                         num_elements = g_variant_n_children(gvar_list);
528                         for (i = 0; i < num_elements; i++) {
529                                 gvar = g_variant_get_child_value(gvar_list, i);
530                                 g_variant_get(gvar, "(tt)", &low, &high);
531                                 g_variant_unref(gvar);
532                                 if (i)
533                                         printf(", ");
534                                 printf("%"PRIu64"-%"PRIu64, low, high);
535                                 if (low == cur_low && high == cur_high)
536                                         printf(" (current)");
537                         }
538                         printf("\n");
539                         g_variant_unref(gvar_list);
540
541                 } else if (srci->datatype == SR_T_BOOL) {
542                         printf("    %s: ", srci->id);
543                         if (maybe_config_get(driver, sdi, channel_group, key,
544                                         &gvar) == SR_OK) {
545                                 if (g_variant_get_boolean(gvar))
546                                         printf("on (current), off\n");
547                                 else
548                                         printf("on, off (current)\n");
549                                 g_variant_unref(gvar);
550                         } else
551                                 printf("on, off\n");
552
553                 } else if (srci->datatype == SR_T_DOUBLE_RANGE) {
554                         printf("    %s: ", srci->id);
555                         if (maybe_config_list(driver, sdi, channel_group, key,
556                                         &gvar_list) != SR_OK) {
557                                 printf("\n");
558                                 continue;
559                         }
560
561                         if (maybe_config_get(driver, sdi, channel_group, key, &gvar) == SR_OK) {
562                                 g_variant_get(gvar, "(dd)", &dcur_low, &dcur_high);
563                                 g_variant_unref(gvar);
564                         } else {
565                                 dcur_low = 0;
566                                 dcur_high = 0;
567                         }
568
569                         num_elements = g_variant_n_children(gvar_list);
570                         for (i = 0; i < num_elements; i++) {
571                                 gvar = g_variant_get_child_value(gvar_list, i);
572                                 g_variant_get(gvar, "(dd)", &dlow, &dhigh);
573                                 g_variant_unref(gvar);
574                                 if (i)
575                                         printf(", ");
576                                 printf("%.1f-%.1f", dlow, dhigh);
577                                 if (dlow == dcur_low && dhigh == dcur_high)
578                                         printf(" (current)");
579                         }
580                         printf("\n");
581                         g_variant_unref(gvar_list);
582
583                 } else if (srci->datatype == SR_T_FLOAT) {
584                         printf("    %s: ", srci->id);
585                         if (maybe_config_get(driver, sdi, channel_group, key,
586                                         &gvar) == SR_OK) {
587                                 printf("%f\n", g_variant_get_double(gvar));
588                                 g_variant_unref(gvar);
589                         } else
590                                 printf("\n");
591
592                 } else if (srci->datatype == SR_T_RATIONAL_PERIOD
593                                 || srci->datatype == SR_T_RATIONAL_VOLT) {
594                         printf("    %s", srci->id);
595                         if (maybe_config_get(driver, sdi, channel_group, key,
596                                         &gvar) == SR_OK) {
597                                 g_variant_get(gvar, "(tt)", &cur_p, &cur_q);
598                                 g_variant_unref(gvar);
599                         } else
600                                 cur_p = cur_q = 0;
601
602                         if (maybe_config_list(driver, sdi, channel_group,
603                                         key, &gvar_list) != SR_OK) {
604                                 printf("\n");
605                                 continue;
606                         }
607                         printf(" - supported values:\n");
608                         num_elements = g_variant_n_children(gvar_list);
609                         for (i = 0; i < num_elements; i++) {
610                                 gvar = g_variant_get_child_value(gvar_list, i);
611                                 g_variant_get(gvar, "(tt)", &p, &q);
612                                 if (srci->datatype == SR_T_RATIONAL_PERIOD)
613                                         s = sr_period_string(p * q);
614                                 else
615                                         s = sr_voltage_string(p, q);
616                                 printf("      %s", s);
617                                 g_free(s);
618                                 if (p == cur_p && q == cur_q)
619                                         printf(" (current)");
620                                 printf("\n");
621                         }
622                         g_variant_unref(gvar_list);
623
624                 } else if (srci->datatype == SR_T_MQ) {
625                         printf("    %s: ", srci->id);
626                         if (maybe_config_get(driver, sdi, channel_group, key,
627                                         &gvar) == SR_OK
628                                         && g_variant_is_of_type(gvar, G_VARIANT_TYPE_TUPLE)
629                                         && g_variant_n_children(gvar) == 2) {
630                                 g_variant_get(gvar, "(ut)", &cur_mq, &cur_mqflags);
631                                 g_variant_unref(gvar);
632                         } else
633                                 cur_mq = cur_mqflags = 0;
634
635                         if (maybe_config_list(driver, sdi, channel_group,
636                                         key, &gvar_list) != SR_OK) {
637                                 printf("\n");
638                                 continue;
639                         }
640                         printf(" - supported measurements:\n");
641                         num_elements = g_variant_n_children(gvar_list);
642                         for (i = 0; i < num_elements; i++) {
643                                 printf("      ");
644                                 gvar = g_variant_get_child_value(gvar_list, i);
645                                 g_variant_get(gvar, "(ut)", &mq, &mqflags);
646                                 if ((srmqi = sr_key_info_get(SR_KEY_MQ, mq)))
647                                         printf("%s", srmqi->id);
648                                 else
649                                         printf("%d", mq);
650                                 for (j = 0, mask = 1; j < 32; j++, mask <<= 1) {
651                                         if (!(mqflags & mask))
652                                                 continue;
653                                         if ((srmqfi = sr_key_info_get(SR_KEY_MQFLAGS, mqflags & mask)))
654                                                 printf("/%s", srmqfi->id);
655                                         else
656                                                 printf("/%" PRIu64, mqflags & mask);
657                                 }
658                                 if (mq == cur_mq && mqflags == cur_mqflags)
659                                         printf(" (current)");
660                                 printf("\n");
661                         }
662                         g_variant_unref(gvar_list);
663
664                 } else {
665
666                         /* Everything else */
667                         printf("    %s\n", srci->id);
668                 }
669         }
670         g_array_free(opts, TRUE);
671
672         sr_dev_close(sdi);
673
674 }
675
676 #ifdef HAVE_SRD
677 void show_pd_detail(void)
678 {
679         struct srd_decoder *dec;
680         struct srd_decoder_option *o;
681         struct srd_channel *pdch;
682         struct srd_decoder_annotation_row *r;
683         GSList *l, *ll, *ol;
684         int idx;
685         char **pdtokens, **pdtok, *optsep, **ann, **bin, *val, *doc;
686
687         pdtokens = g_strsplit(opt_pds, ",", -1);
688         for (pdtok = pdtokens; *pdtok; pdtok++) {
689                 /* Strip options. */
690                 if ((optsep = strchr(*pdtok, ':')))
691                         *optsep = '\0';
692                 if (!(dec = srd_decoder_get_by_id(*pdtok))) {
693                         g_critical("Protocol decoder %s not found.", *pdtok);
694                         return;
695                 }
696                 printf("ID: %s\nName: %s\nLong name: %s\nDescription: %s\n",
697                                 dec->id, dec->name, dec->longname, dec->desc);
698                 printf("License: %s\n", dec->license);
699                 printf("Annotation classes:\n");
700                 if (dec->annotations) {
701                         for (l = dec->annotations; l; l = l->next) {
702                                 ann = l->data;
703                                 printf("- %s: %s\n", ann[0], ann[1]);
704                         }
705                 } else {
706                         printf("None.\n");
707                 }
708                 printf("Annotation rows:\n");
709                 if (dec->annotation_rows) {
710                         for (l = dec->annotation_rows; l; l = l->next) {
711                                 r = l->data;
712                                 printf("- %s (%s): ", r->id, r->desc);
713                                 for (ll = r->ann_classes; ll; ll = ll->next) {
714                                         idx = GPOINTER_TO_INT(ll->data);
715                                         ann = g_slist_nth_data(dec->annotations, idx);
716                                         printf("%s", ann[0]);
717                                         if (ll->next)
718                                                 printf(", ");
719                                 }
720                                 printf("\n");
721                         }
722                 } else {
723                         printf("None.\n");
724                 }
725                 printf("Binary classes:\n");
726                 if (dec->binary) {
727                         for (l = dec->binary; l; l = l->next) {
728                                 bin = l->data;
729                                 printf("- %s: %s\n", bin[0], bin[1]);
730                         }
731                 } else {
732                         printf("None.\n");
733                 }
734                 printf("Required channels:\n");
735                 if (dec->channels) {
736                         for (l = dec->channels; l; l = l->next) {
737                                 pdch = l->data;
738                                 printf("- %s (%s): %s\n",
739                                        pdch->id, pdch->name, pdch->desc);
740                         }
741                 } else {
742                         printf("None.\n");
743                 }
744                 printf("Optional channels:\n");
745                 if (dec->opt_channels) {
746                         for (l = dec->opt_channels; l; l = l->next) {
747                                 pdch = l->data;
748                                 printf("- %s (%s): %s\n",
749                                        pdch->id, pdch->name, pdch->desc);
750                         }
751                 } else {
752                         printf("None.\n");
753                 }
754                 printf("Options:\n");
755                 if (dec->options) {
756                         for (l = dec->options; l; l = l->next) {
757                                 o = l->data;
758                                 printf("- %s: %s (", o->id, o->desc);
759                                 for (ol = o->values; ol; ol = ol->next) {
760                                         val = g_variant_print(ol->data, FALSE);
761                                         printf("%s, ", val);
762                                         g_free(val);
763                                 }
764                                 val = g_variant_print(o->def, FALSE);
765                                 printf("default %s)\n", val);
766                                 g_free(val);
767                         }
768                 } else {
769                         printf("None.\n");
770                 }
771                 if ((doc = srd_decoder_doc_get(dec))) {
772                         printf("Documentation:\n%s\n",
773                                doc[0] == '\n' ? doc + 1 : doc);
774                         g_free(doc);
775                 }
776         }
777
778         g_strfreev(pdtokens);
779 }
780 #endif
781
782 void show_input(void)
783 {
784         const struct sr_input_module *imod;
785         const struct sr_option **opts;
786         GSList *l;
787         int i;
788         char *s, **tok;
789
790         tok = g_strsplit(opt_input_format, ":", 0);
791         if (!tok[0] || !(imod = sr_input_find(tok[0])))
792                 g_critical("Input module '%s' not found.", opt_input_format);
793
794         printf("ID: %s\nName: %s\n", sr_input_id_get(imod),
795                         sr_input_name_get(imod));
796         printf("Description: %s\n", sr_input_description_get(imod));
797         if ((opts = sr_input_options_get(imod))) {
798                 printf("Options:\n");
799                 for (i = 0; opts[i]; i++) {
800                         printf("  %s: %s", opts[i]->id, opts[i]->desc);
801                         if (opts[i]->def) {
802                                 s = g_variant_print(opts[i]->def, FALSE);
803                                 printf(" (default %s", s);
804                                 g_free(s);
805                                 if (opts[i]->values) {
806                                         printf(", possible values ");
807                                         for (l = opts[i]->values; l; l = l->next) {
808                                                 s = g_variant_print((GVariant *)l->data, FALSE);
809                                                 printf("%s%s", s, l->next ? ", " : "");
810                                                 g_free(s);
811                                         }
812                                 }
813                                 printf(")");
814                         }
815                         printf("\n");
816                 }
817                 sr_input_options_free(opts);
818         }
819         g_strfreev(tok);
820 }
821
822 void show_output(void)
823 {
824         const struct sr_output_module *omod;
825         const struct sr_option **opts;
826         GSList *l;
827         int i;
828         char *s, **tok;
829
830         tok = g_strsplit(opt_output_format, ":", 0);
831         if (!tok[0] || !(omod = sr_output_find(tok[0])))
832                 g_critical("Output module '%s' not found.", opt_output_format);
833
834         printf("ID: %s\nName: %s\n", sr_output_id_get(omod),
835                         sr_output_name_get(omod));
836         printf("Description: %s\n", sr_output_description_get(omod));
837         if ((opts = sr_output_options_get(omod))) {
838                 printf("Options:\n");
839                 for (i = 0; opts[i]; i++) {
840                         printf("  %s: %s", opts[i]->id, opts[i]->desc);
841                         if (opts[i]->def) {
842                                 s = g_variant_print(opts[i]->def, FALSE);
843                                 printf(" (default %s", s);
844                                 g_free(s);
845                                 if (opts[i]->values) {
846                                         printf(", possible values ");
847                                         for (l = opts[i]->values; l; l = l->next) {
848                                                 s = g_variant_print((GVariant *)l->data, FALSE);
849                                                 printf("%s%s", s, l->next ? ", " : "");
850                                                 g_free(s);
851                                         }
852                                 }
853                                 printf(")");
854                         }
855                         printf("\n");
856                 }
857                 sr_output_options_free(opts);
858         }
859         g_strfreev(tok);
860 }
861
862 void show_transform(void)
863 {
864         const struct sr_transform_module *tmod;
865         const struct sr_option **opts;
866         GSList *l;
867         int i;
868         char *s, **tok;
869
870         tok = g_strsplit(opt_transform_module, ":", 0);
871         if (!tok[0] || !(tmod = sr_transform_find(tok[0])))
872                 g_critical("Transform module '%s' not found.", opt_transform_module);
873
874         printf("ID: %s\nName: %s\n", sr_transform_id_get(tmod),
875                         sr_transform_name_get(tmod));
876         printf("Description: %s\n", sr_transform_description_get(tmod));
877         if ((opts = sr_transform_options_get(tmod))) {
878                 printf("Options:\n");
879                 for (i = 0; opts[i]; i++) {
880                         printf("  %s: %s", opts[i]->id, opts[i]->desc);
881                         if (opts[i]->def) {
882                                 s = g_variant_print(opts[i]->def, FALSE);
883                                 printf(" (default %s", s);
884                                 g_free(s);
885                                 if (opts[i]->values) {
886                                         printf(", possible values ");
887                                         for (l = opts[i]->values; l; l = l->next) {
888                                                 s = g_variant_print((GVariant *)l->data, FALSE);
889                                                 printf("%s%s", s, l->next ? ", " : "");
890                                                 g_free(s);
891                                         }
892                                 }
893                                 printf(")");
894                         }
895                         printf("\n");
896                 }
897                 sr_transform_options_free(opts);
898         }
899         g_strfreev(tok);
900 }