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