show: Sort ALL the things.
[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 void print_dev_line(const struct sr_dev_inst *sdi)
134 {
135         struct sr_probe *probe;
136         GSList *l;
137         GString *s;
138         GVariant *gvar;
139
140         s = g_string_sized_new(128);
141         g_string_assign(s, sdi->driver->name);
142         if (sr_config_get(sdi->driver, sdi, NULL, SR_CONF_CONN, &gvar) == SR_OK) {
143                 g_string_append(s, ":conn=");
144                 g_string_append(s, g_variant_get_string(gvar, NULL));
145                 g_variant_unref(gvar);
146         }
147         g_string_append(s, " - ");
148         if (sdi->vendor && sdi->vendor[0])
149                 g_string_append_printf(s, "%s ", sdi->vendor);
150         if (sdi->model && sdi->model[0])
151                 g_string_append_printf(s, "%s ", sdi->model);
152         if (sdi->version && sdi->version[0])
153                 g_string_append_printf(s, "%s ", sdi->version);
154         if (sdi->probes) {
155                 if (g_slist_length(sdi->probes) == 1) {
156                         probe = sdi->probes->data;
157                         g_string_append_printf(s, "with 1 probe: %s", probe->name);
158                 } else {
159                         g_string_append_printf(s, "with %d probes:", g_slist_length(sdi->probes));
160                         for (l = sdi->probes; l; l = l->next) {
161                                 probe = l->data;
162                                 g_string_append_printf(s, " %s", probe->name);
163                         }
164                 }
165         }
166         g_string_append_printf(s, "\n");
167         printf("%s", s->str);
168         g_string_free(s, TRUE);
169
170 }
171
172 void show_dev_list(void)
173 {
174         struct sr_dev_inst *sdi;
175         GSList *devices, *l;
176
177         if (!(devices = device_scan()))
178                 return;
179
180         printf("The following devices were found:\n");
181         for (l = devices; l; l = l->next) {
182                 sdi = l->data;
183                 print_dev_line(sdi);
184         }
185         g_slist_free(devices);
186
187 }
188
189 void show_dev_detail(void)
190 {
191         struct sr_dev_inst *sdi;
192         const struct sr_config_info *srci;
193         struct sr_probe *probe;
194         struct sr_probe_group *probe_group, *pg;
195         GSList *devices, *pgl, *prl;
196         GVariant *gvar_opts, *gvar_dict, *gvar_list, *gvar;
197         gsize num_opts, num_elements;
198         const uint64_t *uint64, p, q, low, high;
199         uint64_t cur_low, cur_high;
200         const int32_t *opts;
201         unsigned int num_devices, o, i;
202         char *tmp_str;
203         char *s;
204         const char *charopts, **stropts;
205
206         if (!(devices = device_scan())) {
207                 g_critical("No devices found.");
208                 return;
209         }
210
211         num_devices = g_slist_length(devices);
212         if (num_devices > 1) {
213                 g_critical("%d devices found. Use --scan to show them, "
214                                 "and select one to show.", num_devices);
215                 return;
216         }
217
218         sdi = devices->data;
219         print_dev_line(sdi);
220
221         if (sr_dev_open(sdi) != SR_OK) {
222                 g_critical("Failed to open device.");
223                 return;
224         }
225
226         if ((sr_config_list(sdi->driver, NULL, NULL, SR_CONF_SCAN_OPTIONS,
227                         &gvar_opts) == SR_OK)) {
228                 opts = g_variant_get_fixed_array(gvar_opts, &num_elements,
229                                 sizeof(int32_t));
230                 printf("Supported driver options:\n");
231                 for (i = 0; i < num_elements; i++) {
232                         if (!(srci = sr_config_info_get(opts[i])))
233                                 continue;
234                         printf("    %s\n", srci->id);
235                 }
236                 g_variant_unref(gvar_opts);
237         }
238
239         /* Selected probes and probe group may affect which options are
240          * returned, or which values for them. */
241         select_probes(sdi);
242         probe_group = select_probe_group(sdi);
243
244         if ((sr_config_list(sdi->driver, sdi, probe_group, SR_CONF_DEVICE_OPTIONS,
245                         &gvar_opts)) != SR_OK)
246                 /* Driver supports no device instance options. */
247                 return;
248
249         if (sdi->probe_groups) {
250                 printf("Probe groups:\n");
251                 for (pgl = sdi->probe_groups; pgl; pgl = pgl->next) {
252                         pg = pgl->data;
253                         printf("    %s: channel%s", pg->name,
254                                         g_slist_length(pg->probes) > 1 ? "s" : "");
255                         for (prl = pg->probes; prl; prl = prl->next) {
256                                 probe = prl->data;
257                                 printf(" %s", probe->name);
258                         }
259                         printf("\n");
260                 }
261         }
262
263         printf("Supported configuration options");
264         if (sdi->probe_groups) {
265                 if (!probe_group)
266                         printf(" across all probe groups");
267                 else
268                         printf(" on probe group %s", probe_group->name);
269         }
270         printf(":\n");
271         opts = g_variant_get_fixed_array(gvar_opts, &num_opts, sizeof(int32_t));
272         for (o = 0; o < num_opts; o++) {
273                 if (!(srci = sr_config_info_get(opts[o])))
274                         continue;
275
276                 if (srci->key == SR_CONF_TRIGGER_TYPE) {
277                         if (sr_config_list(sdi->driver, sdi, probe_group, srci->key,
278                                         &gvar) != SR_OK) {
279                                 printf("\n");
280                                 continue;
281                         }
282                         charopts = g_variant_get_string(gvar, NULL);
283                         printf("    Supported triggers: ");
284                         while (*charopts) {
285                                 printf("%c ", *charopts);
286                                 charopts++;
287                         }
288                         printf("\n");
289                         g_variant_unref(gvar);
290
291                 } else if (srci->key == SR_CONF_LIMIT_SAMPLES) {
292                         /* If implemented in config_list(), this denotes the
293                          * maximum number of samples a device can send. This
294                          * really applies only to logic analyzers, and then
295                          * only to those that don't support compression, or
296                          * have it turned off by default. The values returned
297                          * are the low/high limits. */
298                         if (sr_config_list(sdi->driver, sdi, probe_group, srci->key,
299                                         &gvar) != SR_OK) {
300                                 continue;
301                         }
302                         g_variant_get(gvar, "(tt)", &low, &high);
303                         g_variant_unref(gvar);
304                         printf("    Maximum number of samples: %"PRIu64"\n", high);
305
306                 } else if (srci->key == SR_CONF_SAMPLERATE) {
307                         /* Supported samplerates */
308                         printf("    %s", srci->id);
309                         if (sr_config_list(sdi->driver, sdi, probe_group, SR_CONF_SAMPLERATE,
310                                         &gvar_dict) != SR_OK) {
311                                 printf("\n");
312                                 continue;
313                         }
314                         if ((gvar_list = g_variant_lookup_value(gvar_dict,
315                                         "samplerates", G_VARIANT_TYPE("at")))) {
316                                 uint64 = g_variant_get_fixed_array(gvar_list,
317                                                 &num_elements, sizeof(uint64_t));
318                                 printf(" - supported samplerates:\n");
319                                 for (i = 0; i < num_elements; i++) {
320                                         if (!(s = sr_samplerate_string(uint64[i])))
321                                                 continue;
322                                         printf("      %s\n", s);
323                                         g_free(s);
324                                 }
325                                 g_variant_unref(gvar_list);
326                         } else if ((gvar_list = g_variant_lookup_value(gvar_dict,
327                                         "samplerate-steps", G_VARIANT_TYPE("at")))) {
328                                 uint64 = g_variant_get_fixed_array(gvar_list,
329                                                 &num_elements, sizeof(uint64_t));
330                                 /* low */
331                                 if (!(s = sr_samplerate_string(uint64[0])))
332                                         continue;
333                                 printf(" (%s", s);
334                                 g_free(s);
335                                 /* high */
336                                 if (!(s = sr_samplerate_string(uint64[1])))
337                                         continue;
338                                 printf(" - %s", s);
339                                 g_free(s);
340                                 /* step */
341                                 if (!(s = sr_samplerate_string(uint64[2])))
342                                         continue;
343                                 printf(" in steps of %s)\n", s);
344                                 g_free(s);
345                                 g_variant_unref(gvar_list);
346                         }
347                         g_variant_unref(gvar_dict);
348
349                 } else if (srci->key == SR_CONF_BUFFERSIZE) {
350                         /* Supported buffer sizes */
351                         printf("    %s", srci->id);
352                         if (sr_config_list(sdi->driver, sdi, probe_group,
353                                         SR_CONF_BUFFERSIZE, &gvar_list) != SR_OK) {
354                                 printf("\n");
355                                 continue;
356                         }
357                         uint64 = g_variant_get_fixed_array(gvar_list,
358                                         &num_elements, sizeof(uint64_t));
359                         printf(" - supported buffer sizes:\n");
360                         for (i = 0; i < num_elements; i++)
361                                 printf("      %"PRIu64"\n", uint64[i]);
362                         g_variant_unref(gvar_list);
363
364                 } else if (srci->key == SR_CONF_TIMEBASE) {
365                         /* Supported time bases */
366                         printf("    %s", srci->id);
367                         if (sr_config_list(sdi->driver, sdi, probe_group,
368                                         SR_CONF_TIMEBASE, &gvar_list) != SR_OK) {
369                                 printf("\n");
370                                 continue;
371                         }
372                         printf(" - supported time bases:\n");
373                         num_elements = g_variant_n_children(gvar_list);
374                         for (i = 0; i < num_elements; i++) {
375                                 gvar = g_variant_get_child_value(gvar_list, i);
376                                 g_variant_get(gvar, "(tt)", &p, &q);
377                                 s = sr_period_string(p * q);
378                                 printf("      %s\n", s);
379                                 g_free(s);
380                         }
381                         g_variant_unref(gvar_list);
382
383                 } else if (srci->key == SR_CONF_VDIV) {
384                         /* Supported volts/div values */
385                         printf("    %s", srci->id);
386                         if (sr_config_list(sdi->driver, sdi, probe_group,
387                                         SR_CONF_VDIV, &gvar_list) != SR_OK) {
388                                 printf("\n");
389                                 continue;
390                         }
391                         printf(" - supported volts/div:\n");
392                         num_elements = g_variant_n_children(gvar_list);
393                         for (i = 0; i < num_elements; i++) {
394                                 gvar = g_variant_get_child_value(gvar_list, i);
395                                 g_variant_get(gvar, "(tt)", &p, &q);
396                                 s = sr_voltage_string(p, q);
397                                 printf("      %s\n", s);
398                                 g_free(s);
399                         }
400                         g_variant_unref(gvar_list);
401
402                 } else if (srci->datatype == SR_T_CHAR) {
403                         printf("    %s: ", srci->id);
404                         if (sr_config_get(sdi->driver, sdi, probe_group, srci->key,
405                                         &gvar) == SR_OK) {
406                                 tmp_str = g_strdup(g_variant_get_string(gvar, NULL));
407                                 g_variant_unref(gvar);
408                         } else
409                                 tmp_str = NULL;
410
411                         if (sr_config_list(sdi->driver, sdi, probe_group, srci->key,
412                                         &gvar) != SR_OK) {
413                                 printf("\n");
414                                 continue;
415                         }
416
417                         stropts = g_variant_get_strv(gvar, &num_elements);
418                         for (i = 0; i < num_elements; i++) {
419                                 if (i)
420                                         printf(", ");
421                                 printf("%s", stropts[i]);
422                                 if (tmp_str && !strcmp(tmp_str, stropts[i]))
423                                         printf(" (current)");
424                         }
425                         printf("\n");
426                         g_free(stropts);
427                         g_free(tmp_str);
428                         g_variant_unref(gvar);
429
430                 } else if (srci->datatype == SR_T_UINT64_RANGE) {
431                         printf("    %s: ", srci->id);
432                         if (sr_config_list(sdi->driver, sdi, probe_group, srci->key,
433                                         &gvar_list) != SR_OK) {
434                                 printf("\n");
435                                 continue;
436                         }
437
438                         if (sr_config_get(sdi->driver, sdi, NULL, srci->key, &gvar) == SR_OK) {
439                                 g_variant_get(gvar, "(tt)", &cur_low, &cur_high);
440                                 g_variant_unref(gvar);
441                         } else {
442                                 cur_low = 0;
443                                 cur_high = 0;
444                         }
445
446                         num_elements = g_variant_n_children(gvar_list);
447                         for (i = 0; i < num_elements; i++) {
448                                 gvar = g_variant_get_child_value(gvar_list, i);
449                                 g_variant_get(gvar, "(tt)", &low, &high);
450                                 g_variant_unref(gvar);
451                                 if (i)
452                                         printf(", ");
453                                 printf("%"PRIu64"-%"PRIu64, low, high);
454                                 if (low == cur_low && high == cur_high)
455                                         printf(" (current)");
456                         }
457                         printf("\n");
458                         g_variant_unref(gvar_list);
459
460                 } else if (srci->datatype == SR_T_BOOL) {
461                         printf("    %s: ", srci->id);
462                         if (sr_config_get(sdi->driver, sdi, NULL, srci->key,
463                                         &gvar) == SR_OK) {
464                                 if (g_variant_get_boolean(gvar))
465                                         printf("on (current), off\n");
466                                 else
467                                         printf("on, off (current)\n");
468                                 g_variant_unref(gvar);
469                         } else
470                                 printf("on, off\n");
471
472                 } else {
473
474                         /* Everything else */
475                         printf("    %s\n", srci->id);
476                 }
477         }
478         g_variant_unref(gvar_opts);
479
480         sr_dev_close(sdi);
481         g_slist_free(devices);
482
483 }
484
485 #ifdef HAVE_SRD
486 void show_pd_detail(void)
487 {
488         GSList *l, *ll;
489         struct srd_decoder *dec;
490         struct srd_decoder_option *o;
491         char **pdtokens, **pdtok, *optsep, **ann, *val, *doc;
492         struct srd_probe *p;
493         struct srd_decoder_annotation_row *r;
494
495         pdtokens = g_strsplit(opt_pds, ",", -1);
496         for (pdtok = pdtokens; *pdtok; pdtok++) {
497                 /* Strip options. */
498                 if ((optsep = strchr(*pdtok, ':')))
499                         *optsep = '\0';
500                 if (!(dec = srd_decoder_get_by_id(*pdtok))) {
501                         g_critical("Protocol decoder %s not found.", *pdtok);
502                         return;
503                 }
504                 printf("ID: %s\nName: %s\nLong name: %s\nDescription: %s\n",
505                                 dec->id, dec->name, dec->longname, dec->desc);
506                 printf("License: %s\n", dec->license);
507                 printf("Annotation classes:\n");
508                 if (dec->annotations) {
509                         for (l = dec->annotations; l; l = l->next) {
510                                 ann = l->data;
511                                 printf("- %s: %s\n", ann[0], ann[1]);
512                         }
513                 } else {
514                         printf("None.\n");
515                 }
516                 printf("Annotation rows:\n");
517                 if (dec->annotation_rows) {
518                         for (l = dec->annotation_rows; l; l = l->next) {
519                                 r = l->data;
520                                 printf("- %s (%s): ", r->id, r->desc);
521                                 for (ll = r->ann_classes; ll; ll = ll->next)
522                                         printf("%d ", GPOINTER_TO_INT(ll->data));
523                                 printf("\n");
524                         }
525                 } else {
526                         printf("None.\n");
527                 }
528                 printf("Required probes:\n");
529                 if (dec->probes) {
530                         for (l = dec->probes; l; l = l->next) {
531                                 p = l->data;
532                                 printf("- %s (%s): %s\n",
533                                        p->id, p->name, p->desc);
534                         }
535                 } else {
536                         printf("None.\n");
537                 }
538                 printf("Optional probes:\n");
539                 if (dec->opt_probes) {
540                         for (l = dec->opt_probes; l; l = l->next) {
541                                 p = l->data;
542                                 printf("- %s (%s): %s\n",
543                                        p->id, p->name, p->desc);
544                         }
545                 } else {
546                         printf("None.\n");
547                 }
548                 printf("Options:\n");
549                 if (dec->options) {
550                         for (l = dec->options; l; l = l->next) {
551                                 o = l->data;
552                                 val = g_variant_print(o->def, FALSE);
553                                 printf("- %s: %s (default %s)\n", o->id,
554                                         o->desc, val);
555                                 g_free(val);
556                         }
557                 } else {
558                         printf("None.\n");
559                 }
560                 if ((doc = srd_decoder_doc_get(dec))) {
561                         printf("Documentation:\n%s\n",
562                                doc[0] == '\n' ? doc + 1 : doc);
563                         g_free(doc);
564                 }
565         }
566
567         g_strfreev(pdtokens);
568 }
569 #endif
570