decode: Show possible values for decoder options.
[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 "sigrok-cli.h"
21 #include "config.h"
22 #include <glib.h>
23 #include <string.h>
24
25 extern gint opt_loglevel;
26 extern gchar *opt_pds;
27
28 static gint sort_inputs(gconstpointer a, gconstpointer b)
29 {
30         const struct sr_input_format *ia = a, *ib = b;
31
32         return strcmp(ia->id, ib->id);
33 }
34
35 static gint sort_outputs(gconstpointer a, gconstpointer b)
36 {
37         const struct sr_output_format *oa = a, *ob = b;
38
39         return strcmp(oa->id, ob->id);
40 }
41
42 static gint sort_drivers(gconstpointer a, gconstpointer b)
43 {
44         const struct sr_dev_driver *sdda = a, *sddb = b;
45
46         return strcmp(sdda->name, sddb->name);
47 }
48
49 static gint sort_pds(gconstpointer a, gconstpointer b)
50 {
51         const struct srd_decoder *sda = a, *sdb = b;
52
53         return strcmp(sda->id, sdb->id);
54 }
55
56 void show_version(void)
57 {
58         struct sr_dev_driver **drivers, *driver;
59         struct sr_input_format **inputs, *input;
60         struct sr_output_format **outputs, *output;
61         const GSList *l;
62         GSList *sl;
63         int i;
64 #ifdef HAVE_SRD
65         struct srd_decoder *dec;
66 #endif
67
68         printf("sigrok-cli %s\n\n", VERSION);
69
70         printf("Using libsigrok %s (lib version %s).\n",
71                sr_package_version_string_get(), sr_lib_version_string_get());
72 #ifdef HAVE_SRD
73         printf("Using libsigrokdecode %s (lib version %s).\n\n",
74                srd_package_version_string_get(), srd_lib_version_string_get());
75 #endif
76
77         printf("Supported hardware drivers:\n");
78         drivers = sr_driver_list();
79         for (sl = NULL, i = 0; drivers[i]; i++)
80                 sl = g_slist_append(sl, drivers[i]);
81         sl = g_slist_sort(sl, sort_drivers);
82         for (l = sl; l; l = l->next) {
83                 driver = l->data;
84                 printf("  %-20s %s\n", driver->name, driver->longname);
85         }
86         printf("\n");
87         g_slist_free(sl);
88
89         printf("Supported input formats:\n");
90         inputs = sr_input_list();
91         for (sl = NULL, i = 0; inputs[i]; i++)
92                 sl = g_slist_append(sl, inputs[i]);
93         sl = g_slist_sort(sl, sort_inputs);
94         for (l = sl; l; l = l->next) {
95                 input = l->data;
96                 printf("  %-20s %s\n", input->id, input->description);
97         }
98         printf("\n");
99         g_slist_free(sl);
100
101         printf("Supported output formats:\n");
102         outputs = sr_output_list();
103         for (sl = NULL, i = 0; outputs[i]; i++)
104                 sl = g_slist_append(sl, outputs[i]);
105         sl = g_slist_sort(sl, sort_outputs);
106         for (l = sl; l; l = l->next) {
107                 output = l->data;
108                 printf("  %-20s %s\n", output->id, output->description);
109         }
110         printf("\n");
111         g_slist_free(sl);
112
113 #ifdef HAVE_SRD
114         if (srd_init(NULL) == SRD_OK) {
115                 printf("Supported protocol decoders:\n");
116                 srd_decoder_load_all();
117                 sl = g_slist_copy((GSList *)srd_decoder_list());
118                 sl = g_slist_sort(sl, sort_pds);
119                 for (l = sl; l; l = l->next) {
120                         dec = l->data;
121                         printf("  %-20s %s\n", dec->id, dec->longname);
122                         /* Print protocol description upon "-l 3" or higher. */
123                         if (opt_loglevel >= SR_LOG_INFO)
124                                 printf("  %-20s %s\n", "", dec->desc);
125                 }
126                 g_slist_free(sl);
127                 srd_exit();
128         }
129         printf("\n");
130 #endif
131 }
132
133 static gint sort_probes(gconstpointer a, gconstpointer b)
134 {
135         const struct sr_probe *pa = a, *pb = b;
136
137         return pa->index - pb->index;
138 }
139
140 static void print_dev_line(const struct sr_dev_inst *sdi)
141 {
142         struct sr_probe *probe;
143         GSList *sl, *l;
144         GString *s;
145         GVariant *gvar;
146
147         s = g_string_sized_new(128);
148         g_string_assign(s, sdi->driver->name);
149         if (sr_config_get(sdi->driver, sdi, NULL, SR_CONF_CONN, &gvar) == SR_OK) {
150                 g_string_append(s, ":conn=");
151                 g_string_append(s, g_variant_get_string(gvar, NULL));
152                 g_variant_unref(gvar);
153         }
154         g_string_append(s, " - ");
155         if (sdi->vendor && sdi->vendor[0])
156                 g_string_append_printf(s, "%s ", sdi->vendor);
157         if (sdi->model && sdi->model[0])
158                 g_string_append_printf(s, "%s ", sdi->model);
159         if (sdi->version && sdi->version[0])
160                 g_string_append_printf(s, "%s ", sdi->version);
161         if (sdi->probes) {
162                 if (g_slist_length(sdi->probes) == 1) {
163                         probe = sdi->probes->data;
164                         g_string_append_printf(s, "with 1 probe: %s", probe->name);
165                 } else {
166                         sl = g_slist_sort(g_slist_copy(sdi->probes), sort_probes);
167                         g_string_append_printf(s, "with %d probes:", g_slist_length(sl));
168                         for (l = sl; l; l = l->next) {
169                                 probe = l->data;
170                                 g_string_append_printf(s, " %s", probe->name);
171                         }
172                         g_slist_free(sl);
173                 }
174         }
175         g_string_append_printf(s, "\n");
176         printf("%s", s->str);
177         g_string_free(s, TRUE);
178
179 }
180
181 void show_dev_list(void)
182 {
183         struct sr_dev_inst *sdi;
184         GSList *devices, *l;
185
186         if (!(devices = device_scan()))
187                 return;
188
189         printf("The following devices were found:\n");
190         for (l = devices; l; l = l->next) {
191                 sdi = l->data;
192                 print_dev_line(sdi);
193         }
194         g_slist_free(devices);
195
196 }
197
198 void show_dev_detail(void)
199 {
200         struct sr_dev_inst *sdi;
201         const struct sr_config_info *srci;
202         struct sr_probe *probe;
203         struct sr_probe_group *probe_group, *pg;
204         GSList *devices, *pgl, *prl;
205         GVariant *gvar_opts, *gvar_dict, *gvar_list, *gvar;
206         gsize num_opts, num_elements;
207         const uint64_t *uint64, p, q, low, high;
208         uint64_t cur_low, cur_high;
209         const int32_t *opts;
210         unsigned int num_devices, o, i;
211         char *tmp_str;
212         char *s;
213         const char *charopts, **stropts;
214
215         if (!(devices = device_scan())) {
216                 g_critical("No devices found.");
217                 return;
218         }
219
220         num_devices = g_slist_length(devices);
221         if (num_devices > 1) {
222                 g_critical("%d devices found. Use --scan to show them, "
223                                 "and select one to show.", num_devices);
224                 return;
225         }
226
227         sdi = devices->data;
228         print_dev_line(sdi);
229
230         if (sr_dev_open(sdi) != SR_OK) {
231                 g_critical("Failed to open device.");
232                 return;
233         }
234
235         if ((sr_config_list(sdi->driver, NULL, NULL, SR_CONF_SCAN_OPTIONS,
236                         &gvar_opts) == SR_OK)) {
237                 opts = g_variant_get_fixed_array(gvar_opts, &num_elements,
238                                 sizeof(int32_t));
239                 printf("Supported driver options:\n");
240                 for (i = 0; i < num_elements; i++) {
241                         if (!(srci = sr_config_info_get(opts[i])))
242                                 continue;
243                         printf("    %s\n", srci->id);
244                 }
245                 g_variant_unref(gvar_opts);
246         }
247
248         /* Selected probes and probe group may affect which options are
249          * returned, or which values for them. */
250         select_probes(sdi);
251         probe_group = select_probe_group(sdi);
252
253         if ((sr_config_list(sdi->driver, sdi, probe_group, SR_CONF_DEVICE_OPTIONS,
254                         &gvar_opts)) != SR_OK)
255                 /* Driver supports no device instance options. */
256                 return;
257
258         if (sdi->probe_groups) {
259                 printf("Probe groups:\n");
260                 for (pgl = sdi->probe_groups; pgl; pgl = pgl->next) {
261                         pg = pgl->data;
262                         printf("    %s: channel%s", pg->name,
263                                         g_slist_length(pg->probes) > 1 ? "s" : "");
264                         for (prl = pg->probes; prl; prl = prl->next) {
265                                 probe = prl->data;
266                                 printf(" %s", probe->name);
267                         }
268                         printf("\n");
269                 }
270         }
271
272         printf("Supported configuration options");
273         if (sdi->probe_groups) {
274                 if (!probe_group)
275                         printf(" across all probe groups");
276                 else
277                         printf(" on probe group %s", probe_group->name);
278         }
279         printf(":\n");
280         opts = g_variant_get_fixed_array(gvar_opts, &num_opts, sizeof(int32_t));
281         for (o = 0; o < num_opts; o++) {
282                 if (!(srci = sr_config_info_get(opts[o])))
283                         continue;
284
285                 if (srci->key == SR_CONF_TRIGGER_TYPE) {
286                         if (sr_config_list(sdi->driver, sdi, probe_group, srci->key,
287                                         &gvar) != SR_OK) {
288                                 printf("\n");
289                                 continue;
290                         }
291                         charopts = g_variant_get_string(gvar, NULL);
292                         printf("    Supported triggers: ");
293                         while (*charopts) {
294                                 printf("%c ", *charopts);
295                                 charopts++;
296                         }
297                         printf("\n");
298                         g_variant_unref(gvar);
299
300                 } else if (srci->key == SR_CONF_LIMIT_SAMPLES) {
301                         /* If implemented in config_list(), this denotes the
302                          * maximum number of samples a device can send. This
303                          * really applies only to logic analyzers, and then
304                          * only to those that don't support compression, or
305                          * have it turned off by default. The values returned
306                          * are the low/high limits. */
307                         if (sr_config_list(sdi->driver, sdi, probe_group, srci->key,
308                                         &gvar) != SR_OK) {
309                                 continue;
310                         }
311                         g_variant_get(gvar, "(tt)", &low, &high);
312                         g_variant_unref(gvar);
313                         printf("    Maximum number of samples: %"PRIu64"\n", high);
314
315                 } else if (srci->key == SR_CONF_SAMPLERATE) {
316                         /* Supported samplerates */
317                         printf("    %s", srci->id);
318                         if (sr_config_list(sdi->driver, sdi, probe_group, SR_CONF_SAMPLERATE,
319                                         &gvar_dict) != SR_OK) {
320                                 printf("\n");
321                                 continue;
322                         }
323                         if ((gvar_list = g_variant_lookup_value(gvar_dict,
324                                         "samplerates", G_VARIANT_TYPE("at")))) {
325                                 uint64 = g_variant_get_fixed_array(gvar_list,
326                                                 &num_elements, sizeof(uint64_t));
327                                 printf(" - supported samplerates:\n");
328                                 for (i = 0; i < num_elements; i++) {
329                                         if (!(s = sr_samplerate_string(uint64[i])))
330                                                 continue;
331                                         printf("      %s\n", s);
332                                         g_free(s);
333                                 }
334                                 g_variant_unref(gvar_list);
335                         } else if ((gvar_list = g_variant_lookup_value(gvar_dict,
336                                         "samplerate-steps", G_VARIANT_TYPE("at")))) {
337                                 uint64 = g_variant_get_fixed_array(gvar_list,
338                                                 &num_elements, sizeof(uint64_t));
339                                 /* low */
340                                 if (!(s = sr_samplerate_string(uint64[0])))
341                                         continue;
342                                 printf(" (%s", s);
343                                 g_free(s);
344                                 /* high */
345                                 if (!(s = sr_samplerate_string(uint64[1])))
346                                         continue;
347                                 printf(" - %s", s);
348                                 g_free(s);
349                                 /* step */
350                                 if (!(s = sr_samplerate_string(uint64[2])))
351                                         continue;
352                                 printf(" in steps of %s)\n", s);
353                                 g_free(s);
354                                 g_variant_unref(gvar_list);
355                         }
356                         g_variant_unref(gvar_dict);
357
358                 } else if (srci->key == SR_CONF_BUFFERSIZE) {
359                         /* Supported buffer sizes */
360                         printf("    %s", srci->id);
361                         if (sr_config_list(sdi->driver, sdi, probe_group,
362                                         SR_CONF_BUFFERSIZE, &gvar_list) != SR_OK) {
363                                 printf("\n");
364                                 continue;
365                         }
366                         uint64 = g_variant_get_fixed_array(gvar_list,
367                                         &num_elements, sizeof(uint64_t));
368                         printf(" - supported buffer sizes:\n");
369                         for (i = 0; i < num_elements; i++)
370                                 printf("      %"PRIu64"\n", uint64[i]);
371                         g_variant_unref(gvar_list);
372
373                 } else if (srci->key == SR_CONF_TIMEBASE) {
374                         /* Supported time bases */
375                         printf("    %s", srci->id);
376                         if (sr_config_list(sdi->driver, sdi, probe_group,
377                                         SR_CONF_TIMEBASE, &gvar_list) != SR_OK) {
378                                 printf("\n");
379                                 continue;
380                         }
381                         printf(" - supported time bases:\n");
382                         num_elements = g_variant_n_children(gvar_list);
383                         for (i = 0; i < num_elements; i++) {
384                                 gvar = g_variant_get_child_value(gvar_list, i);
385                                 g_variant_get(gvar, "(tt)", &p, &q);
386                                 s = sr_period_string(p * q);
387                                 printf("      %s\n", s);
388                                 g_free(s);
389                         }
390                         g_variant_unref(gvar_list);
391
392                 } else if (srci->key == SR_CONF_VDIV) {
393                         /* Supported volts/div values */
394                         printf("    %s", srci->id);
395                         if (sr_config_list(sdi->driver, sdi, probe_group,
396                                         SR_CONF_VDIV, &gvar_list) != SR_OK) {
397                                 printf("\n");
398                                 continue;
399                         }
400                         printf(" - supported volts/div:\n");
401                         num_elements = g_variant_n_children(gvar_list);
402                         for (i = 0; i < num_elements; i++) {
403                                 gvar = g_variant_get_child_value(gvar_list, i);
404                                 g_variant_get(gvar, "(tt)", &p, &q);
405                                 s = sr_voltage_string(p, q);
406                                 printf("      %s\n", s);
407                                 g_free(s);
408                         }
409                         g_variant_unref(gvar_list);
410
411                 } else if (srci->datatype == SR_T_CHAR) {
412                         printf("    %s: ", srci->id);
413                         if (sr_config_get(sdi->driver, sdi, probe_group, srci->key,
414                                         &gvar) == SR_OK) {
415                                 tmp_str = g_strdup(g_variant_get_string(gvar, NULL));
416                                 g_variant_unref(gvar);
417                         } else
418                                 tmp_str = NULL;
419
420                         if (sr_config_list(sdi->driver, sdi, probe_group, srci->key,
421                                         &gvar) != SR_OK) {
422                                 printf("\n");
423                                 continue;
424                         }
425
426                         stropts = g_variant_get_strv(gvar, &num_elements);
427                         for (i = 0; i < num_elements; i++) {
428                                 if (i)
429                                         printf(", ");
430                                 printf("%s", stropts[i]);
431                                 if (tmp_str && !strcmp(tmp_str, stropts[i]))
432                                         printf(" (current)");
433                         }
434                         printf("\n");
435                         g_free(stropts);
436                         g_free(tmp_str);
437                         g_variant_unref(gvar);
438
439                 } else if (srci->datatype == SR_T_UINT64_RANGE) {
440                         printf("    %s: ", srci->id);
441                         if (sr_config_list(sdi->driver, sdi, probe_group, srci->key,
442                                         &gvar_list) != SR_OK) {
443                                 printf("\n");
444                                 continue;
445                         }
446
447                         if (sr_config_get(sdi->driver, sdi, NULL, srci->key, &gvar) == SR_OK) {
448                                 g_variant_get(gvar, "(tt)", &cur_low, &cur_high);
449                                 g_variant_unref(gvar);
450                         } else {
451                                 cur_low = 0;
452                                 cur_high = 0;
453                         }
454
455                         num_elements = g_variant_n_children(gvar_list);
456                         for (i = 0; i < num_elements; i++) {
457                                 gvar = g_variant_get_child_value(gvar_list, i);
458                                 g_variant_get(gvar, "(tt)", &low, &high);
459                                 g_variant_unref(gvar);
460                                 if (i)
461                                         printf(", ");
462                                 printf("%"PRIu64"-%"PRIu64, low, high);
463                                 if (low == cur_low && high == cur_high)
464                                         printf(" (current)");
465                         }
466                         printf("\n");
467                         g_variant_unref(gvar_list);
468
469                 } else if (srci->datatype == SR_T_BOOL) {
470                         printf("    %s: ", srci->id);
471                         if (sr_config_get(sdi->driver, sdi, NULL, srci->key,
472                                         &gvar) == SR_OK) {
473                                 if (g_variant_get_boolean(gvar))
474                                         printf("on (current), off\n");
475                                 else
476                                         printf("on, off (current)\n");
477                                 g_variant_unref(gvar);
478                         } else
479                                 printf("on, off\n");
480
481                 } else {
482
483                         /* Everything else */
484                         printf("    %s\n", srci->id);
485                 }
486         }
487         g_variant_unref(gvar_opts);
488
489         sr_dev_close(sdi);
490         g_slist_free(devices);
491
492 }
493
494 #ifdef HAVE_SRD
495 void show_pd_detail(void)
496 {
497         GSList *l, *ll, *ol;
498         struct srd_decoder *dec;
499         struct srd_decoder_option *o;
500         char **pdtokens, **pdtok, *optsep, **ann, *val, *doc;
501         struct srd_probe *p;
502         struct srd_decoder_annotation_row *r;
503
504         pdtokens = g_strsplit(opt_pds, ",", -1);
505         for (pdtok = pdtokens; *pdtok; pdtok++) {
506                 /* Strip options. */
507                 if ((optsep = strchr(*pdtok, ':')))
508                         *optsep = '\0';
509                 if (!(dec = srd_decoder_get_by_id(*pdtok))) {
510                         g_critical("Protocol decoder %s not found.", *pdtok);
511                         return;
512                 }
513                 printf("ID: %s\nName: %s\nLong name: %s\nDescription: %s\n",
514                                 dec->id, dec->name, dec->longname, dec->desc);
515                 printf("License: %s\n", dec->license);
516                 printf("Annotation classes:\n");
517                 if (dec->annotations) {
518                         for (l = dec->annotations; l; l = l->next) {
519                                 ann = l->data;
520                                 printf("- %s: %s\n", ann[0], ann[1]);
521                         }
522                 } else {
523                         printf("None.\n");
524                 }
525                 printf("Annotation rows:\n");
526                 if (dec->annotation_rows) {
527                         for (l = dec->annotation_rows; l; l = l->next) {
528                                 r = l->data;
529                                 printf("- %s (%s): ", r->id, r->desc);
530                                 for (ll = r->ann_classes; ll; ll = ll->next)
531                                         printf("%d ", GPOINTER_TO_INT(ll->data));
532                                 printf("\n");
533                         }
534                 } else {
535                         printf("None.\n");
536                 }
537                 printf("Required probes:\n");
538                 if (dec->probes) {
539                         for (l = dec->probes; l; l = l->next) {
540                                 p = l->data;
541                                 printf("- %s (%s): %s\n",
542                                        p->id, p->name, p->desc);
543                         }
544                 } else {
545                         printf("None.\n");
546                 }
547                 printf("Optional probes:\n");
548                 if (dec->opt_probes) {
549                         for (l = dec->opt_probes; l; l = l->next) {
550                                 p = l->data;
551                                 printf("- %s (%s): %s\n",
552                                        p->id, p->name, p->desc);
553                         }
554                 } else {
555                         printf("None.\n");
556                 }
557                 printf("Options:\n");
558                 if (dec->options) {
559                         for (l = dec->options; l; l = l->next) {
560                                 o = l->data;
561                                 printf("- %s: %s (", o->id, o->desc);
562                                 for (ol = o->values; ol; ol = ol->next) {
563                                         val = g_variant_print(ol->data, FALSE);
564                                         printf("%s, ", val);
565                                         g_free(val);
566                                 }
567                                 val = g_variant_print(o->def, FALSE);
568                                 printf("default %s)\n", val);
569                                 g_free(val);
570                         }
571                 } else {
572                         printf("None.\n");
573                 }
574                 if ((doc = srd_decoder_doc_get(dec))) {
575                         printf("Documentation:\n%s\n",
576                                doc[0] == '\n' ? doc + 1 : doc);
577                         g_free(doc);
578                 }
579         }
580
581         g_strfreev(pdtokens);
582 }
583 #endif
584