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