]> sigrok.org Git - sigrok-cli.git/blob - show.c
Always get samplerate at the start of acquisition.
[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_channels(gconstpointer a, gconstpointer b)
134 {
135         const struct sr_channel *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_channel *ch;
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->channels) {
162                 if (g_slist_length(sdi->channels) == 1) {
163                         ch = sdi->channels->data;
164                         g_string_append_printf(s, "with 1 channel: %s", ch->name);
165                 } else {
166                         sl = g_slist_sort(g_slist_copy(sdi->channels), sort_channels);
167                         g_string_append_printf(s, "with %d channels:", g_slist_length(sl));
168                         for (l = sl; l; l = l->next) {
169                                 ch = l->data;
170                                 g_string_append_printf(s, " %s", ch->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_channel *ch;
203         struct sr_channel_group *channel_group, *cg;
204         GSList *devices, *cgl, *chl;
205         GVariant *gvar_opts, *gvar_dict, *gvar_list, *gvar;
206         gsize num_opts, num_elements;
207         double dlow, dhigh, dcur_low, dcur_high;
208         const uint64_t *uint64, p, q, low, high;
209         uint64_t cur_low, cur_high;
210         const int32_t *opts;
211         unsigned int num_devices, o, i;
212         char *tmp_str;
213         char *s;
214         const char *charopts, **stropts;
215
216         if (!(devices = device_scan())) {
217                 g_critical("No devices found.");
218                 return;
219         }
220
221         num_devices = g_slist_length(devices);
222         if (num_devices > 1) {
223                 g_critical("%d devices found. Use --scan to show them, "
224                                 "and select one to show.", num_devices);
225                 return;
226         }
227
228         sdi = devices->data;
229         print_dev_line(sdi);
230
231         if (sr_dev_open(sdi) != SR_OK) {
232                 g_critical("Failed to open device.");
233                 return;
234         }
235
236         if ((sr_config_list(sdi->driver, NULL, NULL, SR_CONF_SCAN_OPTIONS,
237                         &gvar_opts) == SR_OK)) {
238                 opts = g_variant_get_fixed_array(gvar_opts, &num_elements,
239                                 sizeof(int32_t));
240                 printf("Supported driver options:\n");
241                 for (i = 0; i < num_elements; i++) {
242                         if (!(srci = sr_config_info_get(opts[i])))
243                                 continue;
244                         printf("    %s\n", srci->id);
245                 }
246                 g_variant_unref(gvar_opts);
247         }
248
249         /* Selected channels and channel group may affect which options are
250          * returned, or which values for them. */
251         select_channels(sdi);
252         channel_group = select_channel_group(sdi);
253
254         if ((sr_config_list(sdi->driver, sdi, channel_group, SR_CONF_DEVICE_OPTIONS,
255                         &gvar_opts)) != SR_OK)
256                 /* Driver supports no device instance options. */
257                 return;
258
259         if (sdi->channel_groups) {
260                 printf("Channel groups:\n");
261                 for (cgl = sdi->channel_groups; cgl; cgl = cgl->next) {
262                         cg = cgl->data;
263                         printf("    %s: channel%s", cg->name,
264                                         g_slist_length(cg->channels) > 1 ? "s" : "");
265                         for (chl = cg->channels; chl; chl = chl->next) {
266                                 ch = chl->data;
267                                 printf(" %s", ch->name);
268                         }
269                         printf("\n");
270                 }
271         }
272
273         printf("Supported configuration options");
274         if (sdi->channel_groups) {
275                 if (!channel_group)
276                         printf(" across all channel groups");
277                 else
278                         printf(" on channel group %s", channel_group->name);
279         }
280         printf(":\n");
281         opts = g_variant_get_fixed_array(gvar_opts, &num_opts, sizeof(int32_t));
282         for (o = 0; o < num_opts; o++) {
283                 if (!(srci = sr_config_info_get(opts[o])))
284                         continue;
285
286                 if (srci->key == SR_CONF_TRIGGER_TYPE) {
287                         if (sr_config_list(sdi->driver, sdi, channel_group, srci->key,
288                                         &gvar) != SR_OK) {
289                                 printf("\n");
290                                 continue;
291                         }
292                         charopts = g_variant_get_string(gvar, NULL);
293                         printf("    Supported triggers: ");
294                         while (*charopts) {
295                                 printf("%c ", *charopts);
296                                 charopts++;
297                         }
298                         printf("\n");
299                         g_variant_unref(gvar);
300
301                 } else if (srci->key == SR_CONF_LIMIT_SAMPLES) {
302                         /* If implemented in config_list(), this denotes the
303                          * maximum number of samples a device can send. This
304                          * really applies only to logic analyzers, and then
305                          * only to those that don't support compression, or
306                          * have it turned off by default. The values returned
307                          * are the low/high limits. */
308                         if (sr_config_list(sdi->driver, sdi, channel_group, srci->key,
309                                         &gvar) != SR_OK) {
310                                 continue;
311                         }
312                         g_variant_get(gvar, "(tt)", &low, &high);
313                         g_variant_unref(gvar);
314                         printf("    Maximum number of samples: %"PRIu64"\n", high);
315
316                 } else if (srci->key == SR_CONF_SAMPLERATE) {
317                         /* Supported samplerates */
318                         printf("    %s", srci->id);
319                         if (sr_config_list(sdi->driver, sdi, channel_group, SR_CONF_SAMPLERATE,
320                                         &gvar_dict) != SR_OK) {
321                                 printf("\n");
322                                 continue;
323                         }
324                         if ((gvar_list = g_variant_lookup_value(gvar_dict,
325                                         "samplerates", G_VARIANT_TYPE("at")))) {
326                                 uint64 = g_variant_get_fixed_array(gvar_list,
327                                                 &num_elements, sizeof(uint64_t));
328                                 printf(" - supported samplerates:\n");
329                                 for (i = 0; i < num_elements; i++) {
330                                         if (!(s = sr_samplerate_string(uint64[i])))
331                                                 continue;
332                                         printf("      %s\n", s);
333                                         g_free(s);
334                                 }
335                                 g_variant_unref(gvar_list);
336                         } else if ((gvar_list = g_variant_lookup_value(gvar_dict,
337                                         "samplerate-steps", G_VARIANT_TYPE("at")))) {
338                                 uint64 = g_variant_get_fixed_array(gvar_list,
339                                                 &num_elements, sizeof(uint64_t));
340                                 /* low */
341                                 if (!(s = sr_samplerate_string(uint64[0])))
342                                         continue;
343                                 printf(" (%s", s);
344                                 g_free(s);
345                                 /* high */
346                                 if (!(s = sr_samplerate_string(uint64[1])))
347                                         continue;
348                                 printf(" - %s", s);
349                                 g_free(s);
350                                 /* step */
351                                 if (!(s = sr_samplerate_string(uint64[2])))
352                                         continue;
353                                 printf(" in steps of %s)\n", s);
354                                 g_free(s);
355                                 g_variant_unref(gvar_list);
356                         }
357                         g_variant_unref(gvar_dict);
358
359                 } else if (srci->key == SR_CONF_BUFFERSIZE) {
360                         /* Supported buffer sizes */
361                         printf("    %s", srci->id);
362                         if (sr_config_list(sdi->driver, sdi, channel_group,
363                                         SR_CONF_BUFFERSIZE, &gvar_list) != SR_OK) {
364                                 printf("\n");
365                                 continue;
366                         }
367                         uint64 = g_variant_get_fixed_array(gvar_list,
368                                         &num_elements, sizeof(uint64_t));
369                         printf(" - supported buffer sizes:\n");
370                         for (i = 0; i < num_elements; i++)
371                                 printf("      %"PRIu64"\n", uint64[i]);
372                         g_variant_unref(gvar_list);
373
374                 } else if (srci->key == SR_CONF_TIMEBASE) {
375                         /* Supported time bases */
376                         printf("    %s", srci->id);
377                         if (sr_config_list(sdi->driver, sdi, channel_group,
378                                         SR_CONF_TIMEBASE, &gvar_list) != SR_OK) {
379                                 printf("\n");
380                                 continue;
381                         }
382                         printf(" - supported time bases:\n");
383                         num_elements = g_variant_n_children(gvar_list);
384                         for (i = 0; i < num_elements; i++) {
385                                 gvar = g_variant_get_child_value(gvar_list, i);
386                                 g_variant_get(gvar, "(tt)", &p, &q);
387                                 s = sr_period_string(p * q);
388                                 printf("      %s\n", s);
389                                 g_free(s);
390                         }
391                         g_variant_unref(gvar_list);
392
393                 } else if (srci->key == SR_CONF_VDIV) {
394                         /* Supported volts/div values */
395                         printf("    %s", srci->id);
396                         if (sr_config_list(sdi->driver, sdi, channel_group,
397                                         SR_CONF_VDIV, &gvar_list) != SR_OK) {
398                                 printf("\n");
399                                 continue;
400                         }
401                         printf(" - supported volts/div:\n");
402                         num_elements = g_variant_n_children(gvar_list);
403                         for (i = 0; i < num_elements; i++) {
404                                 gvar = g_variant_get_child_value(gvar_list, i);
405                                 g_variant_get(gvar, "(tt)", &p, &q);
406                                 s = sr_voltage_string(p, q);
407                                 printf("      %s\n", s);
408                                 g_free(s);
409                         }
410                         g_variant_unref(gvar_list);
411
412                 } else if (srci->datatype == SR_T_CHAR) {
413                         printf("    %s: ", srci->id);
414                         if (sr_config_get(sdi->driver, sdi, channel_group, srci->key,
415                                         &gvar) == SR_OK) {
416                                 tmp_str = g_strdup(g_variant_get_string(gvar, NULL));
417                                 g_variant_unref(gvar);
418                         } else
419                                 tmp_str = NULL;
420
421                         if (sr_config_list(sdi->driver, sdi, channel_group, srci->key,
422                                         &gvar) != SR_OK) {
423                                 printf("\n");
424                                 continue;
425                         }
426
427                         stropts = g_variant_get_strv(gvar, &num_elements);
428                         for (i = 0; i < num_elements; i++) {
429                                 if (i)
430                                         printf(", ");
431                                 printf("%s", stropts[i]);
432                                 if (tmp_str && !strcmp(tmp_str, stropts[i]))
433                                         printf(" (current)");
434                         }
435                         printf("\n");
436                         g_free(stropts);
437                         g_free(tmp_str);
438                         g_variant_unref(gvar);
439
440                 } else if (srci->datatype == SR_T_UINT64_RANGE) {
441                         printf("    %s: ", srci->id);
442                         if (sr_config_list(sdi->driver, sdi, channel_group, srci->key,
443                                         &gvar_list) != SR_OK) {
444                                 printf("\n");
445                                 continue;
446                         }
447
448                         if (sr_config_get(sdi->driver, sdi, NULL, srci->key, &gvar) == SR_OK) {
449                                 g_variant_get(gvar, "(tt)", &cur_low, &cur_high);
450                                 g_variant_unref(gvar);
451                         } else {
452                                 cur_low = 0;
453                                 cur_high = 0;
454                         }
455
456                         num_elements = g_variant_n_children(gvar_list);
457                         for (i = 0; i < num_elements; i++) {
458                                 gvar = g_variant_get_child_value(gvar_list, i);
459                                 g_variant_get(gvar, "(tt)", &low, &high);
460                                 g_variant_unref(gvar);
461                                 if (i)
462                                         printf(", ");
463                                 printf("%"PRIu64"-%"PRIu64, low, high);
464                                 if (low == cur_low && high == cur_high)
465                                         printf(" (current)");
466                         }
467                         printf("\n");
468                         g_variant_unref(gvar_list);
469
470                 } else if (srci->datatype == SR_T_BOOL) {
471                         printf("    %s: ", srci->id);
472                         if (sr_config_get(sdi->driver, sdi, NULL, srci->key,
473                                         &gvar) == SR_OK) {
474                                 if (g_variant_get_boolean(gvar))
475                                         printf("on (current), off\n");
476                                 else
477                                         printf("on, off (current)\n");
478                                 g_variant_unref(gvar);
479                         } else
480                                 printf("on, off\n");
481
482                 } else if (srci->datatype == SR_T_DOUBLE_RANGE) {
483                         printf("    %s: ", srci->id);
484                         if (sr_config_list(sdi->driver, sdi, channel_group, srci->key,
485                                         &gvar_list) != SR_OK) {
486                                 printf("\n");
487                                 continue;
488                         }
489
490                         if (sr_config_get(sdi->driver, sdi, NULL, srci->key, &gvar) == SR_OK) {
491                                 g_variant_get(gvar, "(dd)", &dcur_low, &dcur_high);
492                                 g_variant_unref(gvar);
493                         } else {
494                                 dcur_low = 0;
495                                 dcur_high = 0;
496                         }
497
498                         num_elements = g_variant_n_children(gvar_list);
499                         for (i = 0; i < num_elements; i++) {
500                                 gvar = g_variant_get_child_value(gvar_list, i);
501                                 g_variant_get(gvar, "(dd)", &dlow, &dhigh);
502                                 g_variant_unref(gvar);
503                                 if (i)
504                                         printf(", ");
505                                 printf("%.1f-%.1f", dlow, dhigh);
506                                 if (dlow == dcur_low && dhigh == dcur_high)
507                                         printf(" (current)");
508                         }
509                         printf("\n");
510                         g_variant_unref(gvar_list);
511
512                 } else {
513
514                         /* Everything else */
515                         printf("    %s\n", srci->id);
516                 }
517         }
518         g_variant_unref(gvar_opts);
519
520         sr_dev_close(sdi);
521         g_slist_free(devices);
522
523 }
524
525 #ifdef HAVE_SRD
526 void show_pd_detail(void)
527 {
528         GSList *l, *ll, *ol;
529         struct srd_decoder *dec;
530         struct srd_decoder_option *o;
531         char **pdtokens, **pdtok, *optsep, **ann, *val, *doc;
532         struct srd_probe *p;
533         struct srd_decoder_annotation_row *r;
534
535         pdtokens = g_strsplit(opt_pds, ",", -1);
536         for (pdtok = pdtokens; *pdtok; pdtok++) {
537                 /* Strip options. */
538                 if ((optsep = strchr(*pdtok, ':')))
539                         *optsep = '\0';
540                 if (!(dec = srd_decoder_get_by_id(*pdtok))) {
541                         g_critical("Protocol decoder %s not found.", *pdtok);
542                         return;
543                 }
544                 printf("ID: %s\nName: %s\nLong name: %s\nDescription: %s\n",
545                                 dec->id, dec->name, dec->longname, dec->desc);
546                 printf("License: %s\n", dec->license);
547                 printf("Annotation classes:\n");
548                 if (dec->annotations) {
549                         for (l = dec->annotations; l; l = l->next) {
550                                 ann = l->data;
551                                 printf("- %s: %s\n", ann[0], ann[1]);
552                         }
553                 } else {
554                         printf("None.\n");
555                 }
556                 printf("Annotation rows:\n");
557                 if (dec->annotation_rows) {
558                         for (l = dec->annotation_rows; l; l = l->next) {
559                                 r = l->data;
560                                 printf("- %s (%s): ", r->id, r->desc);
561                                 for (ll = r->ann_classes; ll; ll = ll->next)
562                                         printf("%d ", GPOINTER_TO_INT(ll->data));
563                                 printf("\n");
564                         }
565                 } else {
566                         printf("None.\n");
567                 }
568                 printf("Required probes:\n");
569                 if (dec->probes) {
570                         for (l = dec->probes; l; l = l->next) {
571                                 p = l->data;
572                                 printf("- %s (%s): %s\n",
573                                        p->id, p->name, p->desc);
574                         }
575                 } else {
576                         printf("None.\n");
577                 }
578                 printf("Optional probes:\n");
579                 if (dec->opt_probes) {
580                         for (l = dec->opt_probes; l; l = l->next) {
581                                 p = l->data;
582                                 printf("- %s (%s): %s\n",
583                                        p->id, p->name, p->desc);
584                         }
585                 } else {
586                         printf("None.\n");
587                 }
588                 printf("Options:\n");
589                 if (dec->options) {
590                         for (l = dec->options; l; l = l->next) {
591                                 o = l->data;
592                                 printf("- %s: %s (", o->id, o->desc);
593                                 for (ol = o->values; ol; ol = ol->next) {
594                                         val = g_variant_print(ol->data, FALSE);
595                                         printf("%s, ", val);
596                                         g_free(val);
597                                 }
598                                 val = g_variant_print(o->def, FALSE);
599                                 printf("default %s)\n", val);
600                                 g_free(val);
601                         }
602                 } else {
603                         printf("None.\n");
604                 }
605                 if ((doc = srd_decoder_doc_get(dec))) {
606                         printf("Documentation:\n%s\n",
607                                doc[0] == '\n' ? doc + 1 : doc);
608                         g_free(doc);
609                 }
610         }
611
612         g_strfreev(pdtokens);
613 }
614 #endif
615