]> sigrok.org Git - sigrok-cli.git/blame - show.c
configure.ac: Bump package version to 0.5.0.
[sigrok-cli.git] / show.c
CommitLineData
2be182e6
BV
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
20fb52e0 20#include "sigrok-cli.h"
2be182e6 21#include <glib.h>
55de58a2 22#include <string.h>
2be182e6
BV
23
24extern gint opt_loglevel;
25extern gchar *opt_pds;
26
37e4fcd4
BV
27static 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
34static 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
41static 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
628197af 48#ifdef HAVE_SRD
37e4fcd4
BV
49static 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}
628197af 55#endif
37e4fcd4 56
2be182e6
BV
57void show_version(void)
58{
37e4fcd4
BV
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;
2be182e6
BV
64 int i;
65#ifdef HAVE_SRD
66 struct srd_decoder *dec;
2be182e6
BV
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();
37e4fcd4
BV
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);
2be182e6
BV
86 }
87 printf("\n");
37e4fcd4 88 g_slist_free(sl);
2be182e6
BV
89
90 printf("Supported input formats:\n");
91 inputs = sr_input_list();
37e4fcd4
BV
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 }
2be182e6 99 printf("\n");
37e4fcd4 100 g_slist_free(sl);
2be182e6
BV
101
102 printf("Supported output formats:\n");
103 outputs = sr_output_list();
37e4fcd4
BV
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 }
2be182e6 111 printf("\n");
37e4fcd4 112 g_slist_free(sl);
2be182e6
BV
113
114#ifdef HAVE_SRD
115 if (srd_init(NULL) == SRD_OK) {
116 printf("Supported protocol decoders:\n");
117 srd_decoder_load_all();
37e4fcd4
BV
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) {
2be182e6
BV
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 }
37e4fcd4 127 g_slist_free(sl);
2be182e6
BV
128 srd_exit();
129 }
130 printf("\n");
131#endif
132}
133
029d73fe 134static gint sort_channels(gconstpointer a, gconstpointer b)
dd2f206a 135{
029d73fe 136 const struct sr_channel *pa = a, *pb = b;
dd2f206a
BV
137
138 return pa->index - pb->index;
139}
140
2be182e6
BV
141static void print_dev_line(const struct sr_dev_inst *sdi)
142{
029d73fe 143 struct sr_channel *ch;
dd2f206a 144 GSList *sl, *l;
2be182e6
BV
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);
029d73fe
UH
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);
2be182e6 166 } else {
029d73fe
UH
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));
dd2f206a 169 for (l = sl; l; l = l->next) {
029d73fe
UH
170 ch = l->data;
171 g_string_append_printf(s, " %s", ch->name);
2be182e6 172 }
dd2f206a 173 g_slist_free(sl);
2be182e6
BV
174 }
175 }
176 g_string_append_printf(s, "\n");
177 printf("%s", s->str);
178 g_string_free(s, TRUE);
179
180}
181
182void 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
199void show_dev_detail(void)
200{
201 struct sr_dev_inst *sdi;
202 const struct sr_config_info *srci;
029d73fe 203 struct sr_channel *ch;
ca50f4b3 204 struct sr_channel_group *channel_group, *cg;
029d73fe 205 GSList *devices, *cgl, *chl;
2be182e6
BV
206 GVariant *gvar_opts, *gvar_dict, *gvar_list, *gvar;
207 gsize num_opts, num_elements;
426d0cda 208 double dlow, dhigh, dcur_low, dcur_high;
2be182e6
BV
209 const uint64_t *uint64, p, q, low, high;
210 uint64_t cur_low, cur_high;
211 const int32_t *opts;
212 unsigned int num_devices, o, i;
213 char *tmp_str;
214 char *s;
215 const char *charopts, **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
ca50f4b3 250 /* Selected channels and channel group may affect which options are
02c65935 251 * returned, or which values for them. */
029d73fe 252 select_channels(sdi);
ca50f4b3 253 channel_group = select_channel_group(sdi);
02c65935 254
ca50f4b3 255 if ((sr_config_list(sdi->driver, sdi, channel_group, SR_CONF_DEVICE_OPTIONS,
2be182e6
BV
256 &gvar_opts)) != SR_OK)
257 /* Driver supports no device instance options. */
258 return;
259
ca50f4b3
UH
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" : "");
029d73fe
UH
266 for (chl = cg->channels; chl; chl = chl->next) {
267 ch = chl->data;
268 printf(" %s", ch->name);
2be182e6
BV
269 }
270 printf("\n");
271 }
272 }
273
274 printf("Supported configuration options");
ca50f4b3
UH
275 if (sdi->channel_groups) {
276 if (!channel_group)
277 printf(" across all channel groups");
2be182e6 278 else
ca50f4b3 279 printf(" on channel group %s", channel_group->name);
2be182e6
BV
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_TYPE) {
ca50f4b3 288 if (sr_config_list(sdi->driver, sdi, channel_group, srci->key,
2be182e6
BV
289 &gvar) != SR_OK) {
290 printf("\n");
291 continue;
292 }
293 charopts = g_variant_get_string(gvar, NULL);
294 printf(" Supported triggers: ");
295 while (*charopts) {
296 printf("%c ", *charopts);
297 charopts++;
298 }
299 printf("\n");
300 g_variant_unref(gvar);
301
02c65935
BV
302 } else if (srci->key == SR_CONF_LIMIT_SAMPLES) {
303 /* If implemented in config_list(), this denotes the
304 * maximum number of samples a device can send. This
305 * really applies only to logic analyzers, and then
306 * only to those that don't support compression, or
307 * have it turned off by default. The values returned
308 * are the low/high limits. */
ca50f4b3 309 if (sr_config_list(sdi->driver, sdi, channel_group, srci->key,
02c65935
BV
310 &gvar) != SR_OK) {
311 continue;
312 }
313 g_variant_get(gvar, "(tt)", &low, &high);
314 g_variant_unref(gvar);
315 printf(" Maximum number of samples: %"PRIu64"\n", high);
316
2be182e6
BV
317 } else if (srci->key == SR_CONF_SAMPLERATE) {
318 /* Supported samplerates */
319 printf(" %s", srci->id);
ca50f4b3 320 if (sr_config_list(sdi->driver, sdi, channel_group, SR_CONF_SAMPLERATE,
2be182e6
BV
321 &gvar_dict) != SR_OK) {
322 printf("\n");
323 continue;
324 }
325 if ((gvar_list = g_variant_lookup_value(gvar_dict,
326 "samplerates", G_VARIANT_TYPE("at")))) {
327 uint64 = g_variant_get_fixed_array(gvar_list,
328 &num_elements, sizeof(uint64_t));
329 printf(" - supported samplerates:\n");
330 for (i = 0; i < num_elements; i++) {
331 if (!(s = sr_samplerate_string(uint64[i])))
332 continue;
333 printf(" %s\n", s);
334 g_free(s);
335 }
336 g_variant_unref(gvar_list);
337 } else if ((gvar_list = g_variant_lookup_value(gvar_dict,
338 "samplerate-steps", G_VARIANT_TYPE("at")))) {
339 uint64 = g_variant_get_fixed_array(gvar_list,
340 &num_elements, sizeof(uint64_t));
341 /* low */
342 if (!(s = sr_samplerate_string(uint64[0])))
343 continue;
344 printf(" (%s", s);
345 g_free(s);
346 /* high */
347 if (!(s = sr_samplerate_string(uint64[1])))
348 continue;
349 printf(" - %s", s);
350 g_free(s);
351 /* step */
352 if (!(s = sr_samplerate_string(uint64[2])))
353 continue;
354 printf(" in steps of %s)\n", s);
355 g_free(s);
356 g_variant_unref(gvar_list);
357 }
358 g_variant_unref(gvar_dict);
359
360 } else if (srci->key == SR_CONF_BUFFERSIZE) {
361 /* Supported buffer sizes */
362 printf(" %s", srci->id);
ca50f4b3 363 if (sr_config_list(sdi->driver, sdi, channel_group,
2be182e6
BV
364 SR_CONF_BUFFERSIZE, &gvar_list) != SR_OK) {
365 printf("\n");
366 continue;
367 }
368 uint64 = g_variant_get_fixed_array(gvar_list,
369 &num_elements, sizeof(uint64_t));
370 printf(" - supported buffer sizes:\n");
371 for (i = 0; i < num_elements; i++)
372 printf(" %"PRIu64"\n", uint64[i]);
373 g_variant_unref(gvar_list);
374
375 } else if (srci->key == SR_CONF_TIMEBASE) {
376 /* Supported time bases */
377 printf(" %s", srci->id);
ca50f4b3 378 if (sr_config_list(sdi->driver, sdi, channel_group,
2be182e6
BV
379 SR_CONF_TIMEBASE, &gvar_list) != SR_OK) {
380 printf("\n");
381 continue;
382 }
383 printf(" - supported time bases:\n");
384 num_elements = g_variant_n_children(gvar_list);
385 for (i = 0; i < num_elements; i++) {
386 gvar = g_variant_get_child_value(gvar_list, i);
387 g_variant_get(gvar, "(tt)", &p, &q);
388 s = sr_period_string(p * q);
389 printf(" %s\n", s);
390 g_free(s);
391 }
392 g_variant_unref(gvar_list);
393
394 } else if (srci->key == SR_CONF_VDIV) {
395 /* Supported volts/div values */
396 printf(" %s", srci->id);
ca50f4b3 397 if (sr_config_list(sdi->driver, sdi, channel_group,
2be182e6
BV
398 SR_CONF_VDIV, &gvar_list) != SR_OK) {
399 printf("\n");
400 continue;
401 }
402 printf(" - supported volts/div:\n");
403 num_elements = g_variant_n_children(gvar_list);
404 for (i = 0; i < num_elements; i++) {
405 gvar = g_variant_get_child_value(gvar_list, i);
406 g_variant_get(gvar, "(tt)", &p, &q);
407 s = sr_voltage_string(p, q);
408 printf(" %s\n", s);
409 g_free(s);
410 }
411 g_variant_unref(gvar_list);
412
9db40e9f 413 } else if (srci->datatype == SR_T_STRING) {
2be182e6 414 printf(" %s: ", srci->id);
ca50f4b3 415 if (sr_config_get(sdi->driver, sdi, channel_group, srci->key,
2be182e6
BV
416 &gvar) == SR_OK) {
417 tmp_str = g_strdup(g_variant_get_string(gvar, NULL));
418 g_variant_unref(gvar);
419 } else
420 tmp_str = NULL;
421
ca50f4b3 422 if (sr_config_list(sdi->driver, sdi, channel_group, srci->key,
2be182e6
BV
423 &gvar) != SR_OK) {
424 printf("\n");
425 continue;
426 }
427
428 stropts = g_variant_get_strv(gvar, &num_elements);
429 for (i = 0; i < num_elements; i++) {
430 if (i)
431 printf(", ");
432 printf("%s", stropts[i]);
433 if (tmp_str && !strcmp(tmp_str, stropts[i]))
434 printf(" (current)");
435 }
436 printf("\n");
437 g_free(stropts);
438 g_free(tmp_str);
439 g_variant_unref(gvar);
440
441 } else if (srci->datatype == SR_T_UINT64_RANGE) {
442 printf(" %s: ", srci->id);
ca50f4b3 443 if (sr_config_list(sdi->driver, sdi, channel_group, srci->key,
2be182e6
BV
444 &gvar_list) != SR_OK) {
445 printf("\n");
446 continue;
447 }
448
449 if (sr_config_get(sdi->driver, sdi, NULL, srci->key, &gvar) == SR_OK) {
450 g_variant_get(gvar, "(tt)", &cur_low, &cur_high);
451 g_variant_unref(gvar);
452 } else {
453 cur_low = 0;
454 cur_high = 0;
455 }
456
457 num_elements = g_variant_n_children(gvar_list);
458 for (i = 0; i < num_elements; i++) {
459 gvar = g_variant_get_child_value(gvar_list, i);
460 g_variant_get(gvar, "(tt)", &low, &high);
461 g_variant_unref(gvar);
462 if (i)
463 printf(", ");
464 printf("%"PRIu64"-%"PRIu64, low, high);
465 if (low == cur_low && high == cur_high)
466 printf(" (current)");
467 }
468 printf("\n");
469 g_variant_unref(gvar_list);
470
471 } else if (srci->datatype == SR_T_BOOL) {
472 printf(" %s: ", srci->id);
473 if (sr_config_get(sdi->driver, sdi, NULL, srci->key,
474 &gvar) == SR_OK) {
475 if (g_variant_get_boolean(gvar))
476 printf("on (current), off\n");
477 else
478 printf("on, off (current)\n");
479 g_variant_unref(gvar);
480 } else
481 printf("on, off\n");
482
426d0cda
BV
483 } else if (srci->datatype == SR_T_DOUBLE_RANGE) {
484 printf(" %s: ", srci->id);
029d73fe 485 if (sr_config_list(sdi->driver, sdi, channel_group, srci->key,
426d0cda
BV
486 &gvar_list) != SR_OK) {
487 printf("\n");
488 continue;
489 }
490
491 if (sr_config_get(sdi->driver, sdi, NULL, srci->key, &gvar) == SR_OK) {
492 g_variant_get(gvar, "(dd)", &dcur_low, &dcur_high);
493 g_variant_unref(gvar);
494 } else {
495 dcur_low = 0;
496 dcur_high = 0;
497 }
498
499 num_elements = g_variant_n_children(gvar_list);
500 for (i = 0; i < num_elements; i++) {
501 gvar = g_variant_get_child_value(gvar_list, i);
502 g_variant_get(gvar, "(dd)", &dlow, &dhigh);
503 g_variant_unref(gvar);
504 if (i)
505 printf(", ");
506 printf("%.1f-%.1f", dlow, dhigh);
507 if (dlow == dcur_low && dhigh == dcur_high)
508 printf(" (current)");
509 }
510 printf("\n");
511 g_variant_unref(gvar_list);
512
2be182e6
BV
513 } else {
514
515 /* Everything else */
516 printf(" %s\n", srci->id);
517 }
518 }
519 g_variant_unref(gvar_opts);
520
521 sr_dev_close(sdi);
522 g_slist_free(devices);
523
524}
525
526#ifdef HAVE_SRD
527void show_pd_detail(void)
528{
f2e82732 529 GSList *l, *ll, *ol;
2be182e6
BV
530 struct srd_decoder *dec;
531 struct srd_decoder_option *o;
532 char **pdtokens, **pdtok, *optsep, **ann, *val, *doc;
3dfbfbc8 533 struct srd_channel *pdch;
1eb46be8 534 struct srd_decoder_annotation_row *r;
2be182e6
BV
535
536 pdtokens = g_strsplit(opt_pds, ",", -1);
537 for (pdtok = pdtokens; *pdtok; pdtok++) {
538 /* Strip options. */
539 if ((optsep = strchr(*pdtok, ':')))
540 *optsep = '\0';
541 if (!(dec = srd_decoder_get_by_id(*pdtok))) {
542 g_critical("Protocol decoder %s not found.", *pdtok);
543 return;
544 }
545 printf("ID: %s\nName: %s\nLong name: %s\nDescription: %s\n",
546 dec->id, dec->name, dec->longname, dec->desc);
547 printf("License: %s\n", dec->license);
b39f3a1a 548 printf("Annotation classes:\n");
2be182e6
BV
549 if (dec->annotations) {
550 for (l = dec->annotations; l; l = l->next) {
551 ann = l->data;
b39f3a1a 552 printf("- %s: %s\n", ann[0], ann[1]);
2be182e6
BV
553 }
554 } else {
555 printf("None.\n");
556 }
1eb46be8
UH
557 printf("Annotation rows:\n");
558 if (dec->annotation_rows) {
559 for (l = dec->annotation_rows; l; l = l->next) {
560 r = l->data;
b39f3a1a 561 printf("- %s (%s): ", r->id, r->desc);
1eb46be8
UH
562 for (ll = r->ann_classes; ll; ll = ll->next)
563 printf("%d ", GPOINTER_TO_INT(ll->data));
564 printf("\n");
565 }
566 } else {
567 printf("None.\n");
568 }
3dfbfbc8
UH
569 printf("Required channels:\n");
570 if (dec->channels) {
571 for (l = dec->channels; l; l = l->next) {
572 pdch = l->data;
2be182e6 573 printf("- %s (%s): %s\n",
3dfbfbc8 574 pdch->id, pdch->name, pdch->desc);
2be182e6
BV
575 }
576 } else {
577 printf("None.\n");
578 }
3dfbfbc8
UH
579 printf("Optional channels:\n");
580 if (dec->opt_channels) {
581 for (l = dec->opt_channels; l; l = l->next) {
582 pdch = l->data;
2be182e6 583 printf("- %s (%s): %s\n",
3dfbfbc8 584 pdch->id, pdch->name, pdch->desc);
2be182e6
BV
585 }
586 } else {
587 printf("None.\n");
588 }
1eb46be8 589 printf("Options:\n");
2be182e6 590 if (dec->options) {
2be182e6
BV
591 for (l = dec->options; l; l = l->next) {
592 o = l->data;
f2e82732
BV
593 printf("- %s: %s (", o->id, o->desc);
594 for (ol = o->values; ol; ol = ol->next) {
595 val = g_variant_print(ol->data, FALSE);
596 printf("%s, ", val);
597 g_free(val);
598 }
2be182e6 599 val = g_variant_print(o->def, FALSE);
f2e82732 600 printf("default %s)\n", val);
2be182e6
BV
601 g_free(val);
602 }
1eb46be8
UH
603 } else {
604 printf("None.\n");
2be182e6
BV
605 }
606 if ((doc = srd_decoder_doc_get(dec))) {
607 printf("Documentation:\n%s\n",
608 doc[0] == '\n' ? doc + 1 : doc);
609 g_free(doc);
610 }
611 }
612
613 g_strfreev(pdtokens);
614}
615#endif
616