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