]> sigrok.org Git - sigrok-cli.git/blob - show.c
show: do print floating point results even if value is zero
[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, *sernum;
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         sernum = sr_dev_inst_sernum_get(sdi);
296         channels = sr_dev_inst_channels_get(sdi);
297
298         s = g_string_sized_new(128);
299         g_string_assign(s, driver->name);
300         if (maybe_config_get(driver, sdi, NULL, SR_CONF_CONN, &gvar) == SR_OK) {
301                 g_string_append(s, ":conn=");
302                 g_string_append(s, g_variant_get_string(gvar, NULL));
303                 g_variant_unref(gvar);
304         }
305         g_string_append(s, " - ");
306         if (vendor && vendor[0])
307                 g_string_append_printf(s, "%s ", vendor);
308         if (model && model[0])
309                 g_string_append_printf(s, "%s ", model);
310         if (version && version[0])
311                 g_string_append_printf(s, "%s ", version);
312         if (sernum && sernum[0])
313                 g_string_append_printf(s, "[S/N: %s] ", sernum);
314         if (channels) {
315                 if (g_slist_length(channels) == 1) {
316                         ch = channels->data;
317                         g_string_append_printf(s, "with 1 channel: %s", ch->name);
318                 } else {
319                         sl = g_slist_sort(g_slist_copy(channels), sort_channels);
320                         g_string_append_printf(s, "with %d channels:", g_slist_length(sl));
321                         for (l = sl; l; l = l->next) {
322                                 ch = l->data;
323                                 g_string_append_printf(s, " %s", ch->name);
324                         }
325                         g_slist_free(sl);
326                 }
327         }
328         g_string_append_printf(s, "\n");
329         printf("%s", s->str);
330         g_string_free(s, TRUE);
331
332 }
333
334 void show_dev_list(void)
335 {
336         struct sr_dev_inst *sdi;
337         GSList *devices, *l;
338
339         if (!(devices = device_scan()))
340                 return;
341
342         printf("The following devices were found:\n");
343         for (l = devices; l; l = l->next) {
344                 sdi = l->data;
345                 print_dev_line(sdi);
346         }
347         g_slist_free(devices);
348
349 }
350
351 void show_drv_detail(struct sr_dev_driver *driver)
352 {
353         const struct sr_key_info *srci;
354         GArray *opts;
355         guint i;
356
357         if ((opts = sr_dev_options(driver, NULL, NULL))) {
358                 if (opts->len > 0) {
359                         printf("Driver functions:\n");
360                         for (i = 0; i < opts->len; i++) {
361                                 if (!(srci = sr_key_info_get(SR_KEY_CONFIG,
362                                                 g_array_index(opts, uint32_t, i))))
363                                         continue;
364                                 printf("    %s\n", srci->name);
365                         }
366                 }
367                 g_array_free(opts, TRUE);
368         }
369
370         if ((opts = sr_driver_scan_options_list(driver))) {
371                 if (opts->len > 0) {
372                         printf("Scan options:\n");
373                         for (i = 0; i < opts->len; i++) {
374                                 if (!(srci = sr_key_info_get(SR_KEY_CONFIG,
375                                                 g_array_index(opts, uint32_t, i))))
376                                         continue;
377                                 printf("    %s\n", srci->id);
378                         }
379                 }
380                 g_array_free(opts, TRUE);
381         }
382 }
383
384 void show_dev_detail(void)
385 {
386         struct sr_dev_driver *driver_from_opt, *driver;
387         struct sr_dev_inst *sdi;
388         const struct sr_key_info *srci, *srmqi, *srmqfi;
389         struct sr_channel *ch;
390         struct sr_channel_group *channel_group, *cg;
391         GSList *devices, *cgl, *chl, *channel_groups;
392         GVariant *gvar_dict, *gvar_list, *gvar;
393         gsize num_elements;
394         double dlow, dhigh, dcur_low, dcur_high;
395         const uint64_t *uint64;
396         uint64_t cur_rate, rate;
397         uint64_t p = 0, q = 0, low = 0, high = 0;
398         uint64_t tmp_uint64, mask, cur_low, cur_high, cur_p, cur_q;
399         GArray *opts;
400         const int32_t *int32;
401         uint32_t key, o, cur_mq, mq;
402         uint64_t cur_mqflags, mqflags;
403         unsigned int num_devices, i, j;
404         char *tmp_str, *s, c;
405         const char **stropts;
406         double tmp_flt;
407         gboolean have_tmp_flt;
408         const double *fltopts;
409
410         if (parse_driver(opt_drv, &driver_from_opt, NULL)) {
411                 /* A driver was specified, report driver-wide options now. */
412                 show_drv_detail(driver_from_opt);
413         }
414
415         if (!(devices = device_scan())) {
416                 g_critical("No devices found.");
417                 return;
418         }
419
420         num_devices = g_slist_length(devices);
421         if (num_devices > 1) {
422                 g_critical("%d devices found. Use --scan to show them, "
423                                 "and select one to show.", num_devices);
424                 return;
425         }
426
427         sdi = devices->data;
428         g_slist_free(devices);
429         print_dev_line(sdi);
430
431         driver = sr_dev_inst_driver_get(sdi);
432         channel_groups = sr_dev_inst_channel_groups_get(sdi);
433
434         if (sr_dev_open(sdi) != SR_OK) {
435                 g_critical("Failed to open device.");
436                 return;
437         }
438
439         /*
440          * Selected channels and channel group may affect which options are
441          * returned, or which values for them.
442          */
443         select_channels(sdi);
444         channel_group = select_channel_group(sdi);
445
446         if (!(opts = sr_dev_options(driver, sdi, channel_group)))
447                 /* Driver supports no device instance options. */
448                 return;
449
450         if (channel_groups) {
451                 printf("Channel groups:\n");
452                 for (cgl = channel_groups; cgl; cgl = cgl->next) {
453                         cg = cgl->data;
454                         printf("    %s: channel%s", cg->name,
455                                         g_slist_length(cg->channels) > 1 ? "s" : "");
456                         for (chl = cg->channels; chl; chl = chl->next) {
457                                 ch = chl->data;
458                                 printf(" %s", ch->name);
459                         }
460                         printf("\n");
461                 }
462         }
463
464         printf("Supported configuration options");
465         if (channel_groups) {
466                 if (!channel_group)
467                         printf(" across all channel groups");
468                 else
469                         printf(" on channel group %s", channel_group->name);
470         }
471         printf(":\n");
472         for (o = 0; o < opts->len; o++) {
473                 key = g_array_index(opts, uint32_t, o);
474                 if (!(srci = sr_key_info_get(SR_KEY_CONFIG, key)))
475                         continue;
476
477                 if (key == SR_CONF_TRIGGER_MATCH) {
478                         if (maybe_config_list(driver, sdi, channel_group, key,
479                                         &gvar_list) != SR_OK) {
480                                 printf("\n");
481                                 continue;
482                         }
483                         int32 = g_variant_get_fixed_array(gvar_list,
484                                         &num_elements, sizeof(int32_t));
485                         printf("    Supported triggers: ");
486                         for (i = 0; i < num_elements; i++) {
487                                 switch (int32[i]) {
488                                 case SR_TRIGGER_ZERO:
489                                         c = '0';
490                                         break;
491                                 case SR_TRIGGER_ONE:
492                                         c = '1';
493                                         break;
494                                 case SR_TRIGGER_RISING:
495                                         c = 'r';
496                                         break;
497                                 case SR_TRIGGER_FALLING:
498                                         c = 'f';
499                                         break;
500                                 case SR_TRIGGER_EDGE:
501                                         c = 'e';
502                                         break;
503                                 case SR_TRIGGER_OVER:
504                                         c = 'o';
505                                         break;
506                                 case SR_TRIGGER_UNDER:
507                                         c = 'u';
508                                         break;
509                                 default:
510                                         c = 0;
511                                         break;
512                                 }
513                                 if (c)
514                                         printf("%c ", c);
515                         }
516                         printf("\n");
517                         g_variant_unref(gvar_list);
518
519                 } else if (key == SR_CONF_LIMIT_SAMPLES
520                                 && (sr_dev_config_capabilities_list(sdi, NULL, key)
521                                         & SR_CONF_LIST)) {
522                         /*
523                          * If implemented in config_list(), this denotes the
524                          * maximum number of samples a device can send. This
525                          * really applies only to logic analyzers, and then
526                          * only to those that don't support compression, or
527                          * have it turned off by default. The values returned
528                          * are the low/high limits.
529                          */
530                         if (sr_config_list(driver, sdi, channel_group, key,
531                                         &gvar) == SR_OK) {
532                                 g_variant_get(gvar, "(tt)", &low, &high);
533                                 g_variant_unref(gvar);
534                                 printf("    Maximum number of samples: %"PRIu64"\n", high);
535                         }
536
537                 } else if (key == SR_CONF_SAMPLERATE) {
538                         /* Supported samplerates */
539                         printf("    %s", srci->id);
540                         cur_rate = ~0ull;
541                         if (maybe_config_get(driver, sdi, channel_group,
542                                 SR_CONF_SAMPLERATE, &gvar) == SR_OK) {
543                                 if (g_variant_is_of_type(gvar, G_VARIANT_TYPE_UINT64))
544                                         cur_rate = g_variant_get_uint64(gvar);
545                                 g_variant_unref(gvar);
546                         }
547                         if (maybe_config_list(driver, sdi, channel_group, SR_CONF_SAMPLERATE,
548                                         &gvar_dict) != SR_OK) {
549                                 printf("\n");
550                                 continue;
551                         }
552                         if ((gvar_list = g_variant_lookup_value(gvar_dict,
553                                         "samplerates", G_VARIANT_TYPE("at")))) {
554                                 uint64 = g_variant_get_fixed_array(gvar_list,
555                                                 &num_elements, sizeof(uint64_t));
556                                 printf(" - supported samplerates:\n");
557                                 for (i = 0; i < num_elements; i++) {
558                                         rate = uint64[i];
559                                         s = sr_samplerate_string(rate);
560                                         if (!s)
561                                                 continue;
562                                         printf("      %s", s);
563                                         if (rate == cur_rate)
564                                                 printf(" (current)");
565                                         printf("\n");
566                                         g_free(s);
567                                 }
568                                 g_variant_unref(gvar_list);
569                         } else if ((gvar_list = g_variant_lookup_value(gvar_dict,
570                                         "samplerate-steps", G_VARIANT_TYPE("at")))) {
571                                 uint64 = g_variant_get_fixed_array(gvar_list,
572                                                 &num_elements, sizeof(uint64_t));
573                                 /* low */
574                                 if (!(s = sr_samplerate_string(uint64[0])))
575                                         continue;
576                                 printf(" (%s", s);
577                                 g_free(s);
578                                 /* high */
579                                 if (!(s = sr_samplerate_string(uint64[1])))
580                                         continue;
581                                 printf(" - %s", s);
582                                 g_free(s);
583                                 /* step */
584                                 if (!(s = sr_samplerate_string(uint64[2])))
585                                         continue;
586                                 printf(" in steps of %s)\n", s);
587                                 g_free(s);
588                                 g_variant_unref(gvar_list);
589                         }
590                         g_variant_unref(gvar_dict);
591
592                 } else if (srci->datatype == SR_T_UINT64) {
593                         printf("    %s: ", srci->id);
594                         gvar = NULL;
595                         if (maybe_config_get(driver, sdi, channel_group, key,
596                                         &gvar) == SR_OK) {
597                                 tmp_uint64 = g_variant_get_uint64(gvar);
598                                 g_variant_unref(gvar);
599                         } else
600                                 tmp_uint64 = 0;
601                         if (maybe_config_list(driver, sdi, channel_group,
602                                         key, &gvar_list) != SR_OK) {
603                                 if (gvar) {
604                                         /* Can't list it, but we have a value to show. */
605                                         printf("%"PRIu64" (current)", tmp_uint64);
606                                 }
607                                 printf("\n");
608                                 continue;
609                         }
610                         uint64 = g_variant_get_fixed_array(gvar_list,
611                                         &num_elements, sizeof(uint64_t));
612                         printf(" - supported values:\n");
613                         for (i = 0; i < num_elements; i++) {
614                                 printf("      %"PRIu64, uint64[i]);
615                                 if (gvar && tmp_uint64 == uint64[i])
616                                         printf(" (current)");
617                                 printf("\n");
618                         }
619                         g_variant_unref(gvar_list);
620
621                 } else if (srci->datatype == SR_T_STRING) {
622                         printf("    %s: ", srci->id);
623                         if (maybe_config_get(driver, sdi, channel_group, key,
624                                         &gvar) == SR_OK) {
625                                 tmp_str = g_strdup(g_variant_get_string(gvar, NULL));
626                                 g_variant_unref(gvar);
627                         } else
628                                 tmp_str = NULL;
629
630                         if (maybe_config_list(driver, sdi, channel_group, key,
631                                         &gvar) != SR_OK) {
632                                 if (tmp_str) {
633                                         /* Can't list it, but we have a value to show. */
634                                         printf("%s (current)", tmp_str);
635                                 }
636                                 printf("\n");
637                                 g_free(tmp_str);
638                                 continue;
639                         }
640
641                         stropts = g_variant_get_strv(gvar, &num_elements);
642                         for (i = 0; i < num_elements; i++) {
643                                 if (i)
644                                         printf(", ");
645                                 printf("%s", stropts[i]);
646                                 if (tmp_str && !strcmp(tmp_str, stropts[i]))
647                                         printf(" (current)");
648                         }
649                         printf("\n");
650                         g_free(stropts);
651                         g_free(tmp_str);
652                         g_variant_unref(gvar);
653
654                 } else if (srci->datatype == SR_T_UINT64_RANGE) {
655                         printf("    %s: ", srci->id);
656                         if (maybe_config_list(driver, sdi, channel_group, key,
657                                         &gvar_list) != SR_OK) {
658                                 printf("\n");
659                                 continue;
660                         }
661
662                         if (maybe_config_get(driver, sdi, channel_group, key, &gvar) == SR_OK) {
663                                 g_variant_get(gvar, "(tt)", &cur_low, &cur_high);
664                                 g_variant_unref(gvar);
665                         } else {
666                                 cur_low = 0;
667                                 cur_high = 0;
668                         }
669
670                         num_elements = g_variant_n_children(gvar_list);
671                         for (i = 0; i < num_elements; i++) {
672                                 gvar = g_variant_get_child_value(gvar_list, i);
673                                 g_variant_get(gvar, "(tt)", &low, &high);
674                                 g_variant_unref(gvar);
675                                 if (i)
676                                         printf(", ");
677                                 printf("%"PRIu64"-%"PRIu64, low, high);
678                                 if (low == cur_low && high == cur_high)
679                                         printf(" (current)");
680                         }
681                         printf("\n");
682                         g_variant_unref(gvar_list);
683
684                 } else if (srci->datatype == SR_T_BOOL) {
685                         printf("    %s: ", srci->id);
686                         if (maybe_config_get(driver, sdi, channel_group, key,
687                                         &gvar) == SR_OK) {
688                                 if (g_variant_get_boolean(gvar))
689                                         printf("on (current), off\n");
690                                 else
691                                         printf("on, off (current)\n");
692                                 g_variant_unref(gvar);
693                         } else
694                                 printf("on, off\n");
695
696                 } else if (srci->datatype == SR_T_DOUBLE_RANGE) {
697                         printf("    %s: ", srci->id);
698                         if (maybe_config_list(driver, sdi, channel_group, key,
699                                         &gvar_list) != SR_OK) {
700                                 printf("\n");
701                                 continue;
702                         }
703
704                         if (maybe_config_get(driver, sdi, channel_group, key, &gvar) == SR_OK) {
705                                 g_variant_get(gvar, "(dd)", &dcur_low, &dcur_high);
706                                 g_variant_unref(gvar);
707                         } else {
708                                 dcur_low = 0;
709                                 dcur_high = 0;
710                         }
711
712                         num_elements = g_variant_n_children(gvar_list);
713                         for (i = 0; i < num_elements; i++) {
714                                 gvar = g_variant_get_child_value(gvar_list, i);
715                                 g_variant_get(gvar, "(dd)", &dlow, &dhigh);
716                                 g_variant_unref(gvar);
717                                 if (i)
718                                         printf(", ");
719                                 printf("%.1f-%.1f", dlow, dhigh);
720                                 if (dlow == dcur_low && dhigh == dcur_high)
721                                         printf(" (current)");
722                         }
723                         printf("\n");
724                         g_variant_unref(gvar_list);
725
726                 } else if (srci->datatype == SR_T_FLOAT) {
727                         printf("    %s: ", srci->id);
728                         tmp_flt = 0.0;
729                         have_tmp_flt = FALSE;
730                         if (maybe_config_get(driver, sdi, channel_group, key,
731                                         &gvar) == SR_OK) {
732                                 tmp_flt = g_variant_get_double(gvar);
733                                 have_tmp_flt = TRUE;
734                                 g_variant_unref(gvar);
735                         }
736                         if (maybe_config_list(driver, sdi, channel_group, key,
737                                         &gvar) != SR_OK) {
738                                 if (have_tmp_flt) {
739                                         /* Can't list, but got a value to show. */
740                                         printf("%f (current)", tmp_flt);
741                                 }
742                                 printf("\n");
743                                 continue;
744                         }
745                         fltopts = g_variant_get_fixed_array(gvar,
746                                 &num_elements, sizeof(tmp_flt));
747                         for (i = 0; i < num_elements; i++) {
748                                 if (i)
749                                         printf(", ");
750                                 printf("%f", fltopts[i]);
751                                 if (have_tmp_flt && fltopts[i] == tmp_flt)
752                                         printf(" (current)");
753                         }
754                         printf("\n");
755                         g_variant_unref(gvar);
756
757                 } else if (srci->datatype == SR_T_RATIONAL_PERIOD
758                                 || srci->datatype == SR_T_RATIONAL_VOLT) {
759                         printf("    %s", srci->id);
760                         if (maybe_config_get(driver, sdi, channel_group, key,
761                                         &gvar) == SR_OK) {
762                                 g_variant_get(gvar, "(tt)", &cur_p, &cur_q);
763                                 g_variant_unref(gvar);
764                         } else
765                                 cur_p = cur_q = 0;
766
767                         if (maybe_config_list(driver, sdi, channel_group,
768                                         key, &gvar_list) != SR_OK) {
769                                 printf("\n");
770                                 continue;
771                         }
772                         printf(" - supported values:\n");
773                         num_elements = g_variant_n_children(gvar_list);
774                         for (i = 0; i < num_elements; i++) {
775                                 gvar = g_variant_get_child_value(gvar_list, i);
776                                 g_variant_get(gvar, "(tt)", &p, &q);
777                                 if (srci->datatype == SR_T_RATIONAL_PERIOD)
778                                         s = sr_period_string(p, q);
779                                 else
780                                         s = sr_voltage_string(p, q);
781                                 printf("      %s", s);
782                                 g_free(s);
783                                 if (p == cur_p && q == cur_q)
784                                         printf(" (current)");
785                                 printf("\n");
786                         }
787                         g_variant_unref(gvar_list);
788
789                 } else if (srci->datatype == SR_T_MQ) {
790                         printf("    %s: ", srci->id);
791                         if (maybe_config_get(driver, sdi, channel_group, key,
792                                         &gvar) == SR_OK
793                                         && g_variant_is_of_type(gvar, G_VARIANT_TYPE_TUPLE)
794                                         && g_variant_n_children(gvar) == 2) {
795                                 g_variant_get(gvar, "(ut)", &cur_mq, &cur_mqflags);
796                                 g_variant_unref(gvar);
797                         } else
798                                 cur_mq = cur_mqflags = 0;
799
800                         if (maybe_config_list(driver, sdi, channel_group,
801                                         key, &gvar_list) != SR_OK) {
802                                 printf("\n");
803                                 continue;
804                         }
805                         printf(" - supported measurements:\n");
806                         num_elements = g_variant_n_children(gvar_list);
807                         for (i = 0; i < num_elements; i++) {
808                                 printf("      ");
809                                 gvar = g_variant_get_child_value(gvar_list, i);
810                                 g_variant_get(gvar, "(ut)", &mq, &mqflags);
811                                 if ((srmqi = sr_key_info_get(SR_KEY_MQ, mq)))
812                                         printf("%s", srmqi->id);
813                                 else
814                                         printf("%d", mq);
815                                 for (j = 0, mask = 1; j < 32; j++, mask <<= 1) {
816                                         if (!(mqflags & mask))
817                                                 continue;
818                                         if ((srmqfi = sr_key_info_get(SR_KEY_MQFLAGS, mqflags & mask)))
819                                                 printf("/%s", srmqfi->id);
820                                         else
821                                                 printf("/%" PRIu64, mqflags & mask);
822                                 }
823                                 if (mq == cur_mq && mqflags == cur_mqflags)
824                                         printf(" (current)");
825                                 printf("\n");
826                         }
827                         g_variant_unref(gvar_list);
828
829                 } else {
830
831                         /* Everything else */
832                         printf("    %s\n", srci->id);
833                 }
834         }
835         g_array_free(opts, TRUE);
836
837         sr_dev_close(sdi);
838
839 }
840
841 #ifdef HAVE_SRD
842 static void show_pd_detail_single(const char *pd)
843 {
844         struct srd_decoder *dec;
845         struct srd_decoder_option *o;
846         struct srd_channel *pdch;
847         struct srd_decoder_annotation_row *r;
848         GSList *l, *ll, *ol;
849         int idx;
850         char **pdtokens, **pdtok, *optsep, **ann, **bin, *val, *doc, *str;
851
852         pdtokens = g_strsplit(pd, ",", -1);
853         for (pdtok = pdtokens; *pdtok; pdtok++) {
854                 /* Strip options. */
855                 if ((optsep = strchr(*pdtok, ':')))
856                         *optsep = '\0';
857                 if (!(dec = srd_decoder_get_by_id(*pdtok))) {
858                         g_critical("Protocol decoder %s not found.", *pdtok);
859                         return;
860                 }
861                 printf("ID: %s\nName: %s\nLong name: %s\nDescription: %s\n",
862                                 dec->id, dec->name, dec->longname, dec->desc);
863                 printf("License: %s\n", dec->license);
864                 printf("Possible decoder input IDs:\n");
865                 if (dec->inputs) {
866                         for (l = dec->inputs; l; l = l->next) {
867                                 str = l->data;
868                                 printf("- %s\n", str);
869                         }
870                 } else {
871                         printf("None.\n");
872                 }
873                 printf("Possible decoder output IDs:\n");
874                 if (dec->outputs) {
875                         for (l = dec->outputs; l; l = l->next) {
876                                 str = l->data;
877                                 printf("- %s\n", str);
878                         }
879                 } else {
880                         printf("None.\n");
881                 }
882                 printf("Decoder tags:\n");
883                 if (dec->tags) {
884                         for (l = dec->tags; l; l = l->next) {
885                                 str = l->data;
886                                 printf("- %s\n", str);
887                         }
888                 } else {
889                         printf("None.\n");
890                 }
891                 printf("Annotation classes:\n");
892                 if (dec->annotations) {
893                         for (l = dec->annotations; l; l = l->next) {
894                                 ann = l->data;
895                                 printf("- %s: %s\n", ann[0], ann[1]);
896                         }
897                 } else {
898                         printf("None.\n");
899                 }
900                 printf("Annotation rows:\n");
901                 if (dec->annotation_rows) {
902                         for (l = dec->annotation_rows; l; l = l->next) {
903                                 r = l->data;
904                                 printf("- %s (%s): ", r->id, r->desc);
905                                 for (ll = r->ann_classes; ll; ll = ll->next) {
906                                         idx = GPOINTER_TO_INT(ll->data);
907                                         ann = g_slist_nth_data(dec->annotations, idx);
908                                         printf("%s", ann[0]);
909                                         if (ll->next)
910                                                 printf(", ");
911                                 }
912                                 printf("\n");
913                         }
914                 } else {
915                         printf("None.\n");
916                 }
917                 printf("Binary classes:\n");
918                 if (dec->binary) {
919                         for (l = dec->binary; l; l = l->next) {
920                                 bin = l->data;
921                                 printf("- %s: %s\n", bin[0], bin[1]);
922                         }
923                 } else {
924                         printf("None.\n");
925                 }
926                 printf("Required channels:\n");
927                 if (dec->channels) {
928                         for (l = dec->channels; l; l = l->next) {
929                                 pdch = l->data;
930                                 printf("- %s (%s): %s\n",
931                                        pdch->id, pdch->name, pdch->desc);
932                         }
933                 } else {
934                         printf("None.\n");
935                 }
936                 printf("Optional channels:\n");
937                 if (dec->opt_channels) {
938                         for (l = dec->opt_channels; l; l = l->next) {
939                                 pdch = l->data;
940                                 printf("- %s (%s): %s\n",
941                                        pdch->id, pdch->name, pdch->desc);
942                         }
943                 } else {
944                         printf("None.\n");
945                 }
946                 printf("Options:\n");
947                 if (dec->options) {
948                         for (l = dec->options; l; l = l->next) {
949                                 o = l->data;
950                                 printf("- %s: %s (", o->id, o->desc);
951                                 for (ol = o->values; ol; ol = ol->next) {
952                                         val = g_variant_print(ol->data, FALSE);
953                                         printf("%s, ", val);
954                                         g_free(val);
955                                 }
956                                 val = g_variant_print(o->def, FALSE);
957                                 printf("default %s)\n", val);
958                                 g_free(val);
959                         }
960                 } else {
961                         printf("None.\n");
962                 }
963                 if ((doc = srd_decoder_doc_get(dec))) {
964                         printf("Documentation:\n%s\n",
965                                doc[0] == '\n' ? doc + 1 : doc);
966                         g_free(doc);
967                 }
968         }
969
970         g_strfreev(pdtokens);
971 }
972
973 void show_pd_detail(void)
974 {
975         for (int i = 0; opt_pds[i]; i++)
976                 show_pd_detail_single(opt_pds[i]);
977 }
978 #endif
979
980 void show_input(void)
981 {
982         const struct sr_input_module *imod;
983         const struct sr_option **opts;
984         GSList *l;
985         int i;
986         char *s, **tok;
987
988         tok = g_strsplit(opt_input_format, ":", 0);
989         if (!tok[0] || !(imod = sr_input_find(tok[0])))
990                 g_critical("Input module '%s' not found.", opt_input_format);
991
992         printf("ID: %s\nName: %s\n", sr_input_id_get(imod),
993                         sr_input_name_get(imod));
994         printf("Description: %s\n", sr_input_description_get(imod));
995         if ((opts = sr_input_options_get(imod))) {
996                 printf("Options:\n");
997                 for (i = 0; opts[i]; i++) {
998                         printf("  %s: %s", opts[i]->id, opts[i]->desc);
999                         if (opts[i]->def) {
1000                                 s = g_variant_print(opts[i]->def, FALSE);
1001                                 printf(" (default %s", s);
1002                                 g_free(s);
1003                                 if (opts[i]->values) {
1004                                         printf(", possible values ");
1005                                         for (l = opts[i]->values; l; l = l->next) {
1006                                                 s = g_variant_print((GVariant *)l->data, FALSE);
1007                                                 printf("%s%s", s, l->next ? ", " : "");
1008                                                 g_free(s);
1009                                         }
1010                                 }
1011                                 printf(")");
1012                         }
1013                         printf("\n");
1014                 }
1015                 sr_input_options_free(opts);
1016         }
1017         g_strfreev(tok);
1018 }
1019
1020 void show_output(void)
1021 {
1022         const struct sr_output_module *omod;
1023         const struct sr_option **opts;
1024         GSList *l;
1025         int i;
1026         char *s, **tok;
1027
1028         tok = g_strsplit(opt_output_format, ":", 0);
1029         if (!tok[0] || !(omod = sr_output_find(tok[0])))
1030                 g_critical("Output module '%s' not found.", opt_output_format);
1031
1032         printf("ID: %s\nName: %s\n", sr_output_id_get(omod),
1033                         sr_output_name_get(omod));
1034         printf("Description: %s\n", sr_output_description_get(omod));
1035         if ((opts = sr_output_options_get(omod))) {
1036                 printf("Options:\n");
1037                 for (i = 0; opts[i]; i++) {
1038                         printf("  %s: %s", opts[i]->id, opts[i]->desc);
1039                         if (opts[i]->def) {
1040                                 s = g_variant_print(opts[i]->def, FALSE);
1041                                 printf(" (default %s", s);
1042                                 g_free(s);
1043                                 if (opts[i]->values) {
1044                                         printf(", possible values ");
1045                                         for (l = opts[i]->values; l; l = l->next) {
1046                                                 s = g_variant_print((GVariant *)l->data, FALSE);
1047                                                 printf("%s%s", s, l->next ? ", " : "");
1048                                                 g_free(s);
1049                                         }
1050                                 }
1051                                 printf(")");
1052                         }
1053                         printf("\n");
1054                 }
1055                 sr_output_options_free(opts);
1056         }
1057         g_strfreev(tok);
1058 }
1059
1060 void show_transform(void)
1061 {
1062         const struct sr_transform_module *tmod;
1063         const struct sr_option **opts;
1064         GSList *l;
1065         int i;
1066         char *s, **tok;
1067
1068         tok = g_strsplit(opt_transform_module, ":", 0);
1069         if (!tok[0] || !(tmod = sr_transform_find(tok[0])))
1070                 g_critical("Transform module '%s' not found.", opt_transform_module);
1071
1072         printf("ID: %s\nName: %s\n", sr_transform_id_get(tmod),
1073                         sr_transform_name_get(tmod));
1074         printf("Description: %s\n", sr_transform_description_get(tmod));
1075         if ((opts = sr_transform_options_get(tmod))) {
1076                 printf("Options:\n");
1077                 for (i = 0; opts[i]; i++) {
1078                         printf("  %s: %s", opts[i]->id, opts[i]->desc);
1079                         if (opts[i]->def) {
1080                                 s = g_variant_print(opts[i]->def, FALSE);
1081                                 printf(" (default %s", s);
1082                                 g_free(s);
1083                                 if (opts[i]->values) {
1084                                         printf(", possible values ");
1085                                         for (l = opts[i]->values; l; l = l->next) {
1086                                                 s = g_variant_print((GVariant *)l->data, FALSE);
1087                                                 printf("%s%s", s, l->next ? ", " : "");
1088                                                 g_free(s);
1089                                         }
1090                                 }
1091                                 printf(")");
1092                         }
1093                         printf("\n");
1094                 }
1095                 sr_transform_options_free(opts);
1096         }
1097         g_strfreev(tok);
1098 }
1099
1100 static void print_serial_port(gpointer data, gpointer user_data)
1101 {
1102         struct sr_serial_port *port;
1103
1104         port = (void *)data;
1105         (void)user_data;
1106         printf("  %s\t%s\n", port->name, port->description);
1107 }
1108
1109 void show_serial_ports(void)
1110 {
1111         GSList *serial_ports;
1112
1113         serial_ports = sr_serial_list(NULL);
1114         if (!serial_ports)
1115                 return;
1116
1117         printf("Available serial/HID/BT/BLE ports:\n");
1118         g_slist_foreach(serial_ports, print_serial_port, NULL);
1119         g_slist_free_full(serial_ports, (GDestroyNotify)sr_serial_free);
1120 }