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