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