2 * This file is part of the sigrok-cli project.
4 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
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.
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.
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/>.
20 #include "sigrok-cli.h"
24 static gint
sort_inputs(gconstpointer a
, gconstpointer b
)
26 return strcmp(sr_input_id_get((struct sr_input_module
*)a
),
27 sr_input_id_get((struct sr_input_module
*)b
));
30 static gint
sort_outputs(gconstpointer a
, gconstpointer b
)
32 return strcmp(sr_output_id_get((struct sr_output_module
*)a
),
33 sr_output_id_get((struct sr_output_module
*)b
));
36 static gint
sort_drivers(gconstpointer a
, gconstpointer b
)
38 const struct sr_dev_driver
*sdda
= a
, *sddb
= b
;
40 return strcmp(sdda
->name
, sddb
->name
);
44 static gint
sort_pds(gconstpointer a
, gconstpointer b
)
46 const struct srd_decoder
*sda
= a
, *sdb
= b
;
48 return strcmp(sda
->id
, sdb
->id
);
52 void show_version(void)
54 struct sr_dev_driver
**drivers
, *driver
;
55 const struct sr_input_module
**inputs
, *input
;
56 const struct sr_output_module
**outputs
, *output
;
61 struct srd_decoder
*dec
;
64 printf("sigrok-cli %s\n\n", VERSION
);
66 printf("Using libsigrok %s (lib version %s).\n",
67 sr_package_version_string_get(), sr_lib_version_string_get());
69 printf("Using libsigrokdecode %s (lib version %s).\n\n",
70 srd_package_version_string_get(), srd_lib_version_string_get());
73 printf("Supported hardware drivers:\n");
74 drivers
= sr_driver_list();
75 for (sl
= NULL
, i
= 0; drivers
[i
]; i
++)
76 sl
= g_slist_append(sl
, drivers
[i
]);
77 sl
= g_slist_sort(sl
, sort_drivers
);
78 for (l
= sl
; l
; l
= l
->next
) {
80 printf(" %-20s %s\n", driver
->name
, driver
->longname
);
85 printf("Supported input formats:\n");
86 inputs
= sr_input_list();
87 for (sl
= NULL
, i
= 0; inputs
[i
]; i
++)
88 sl
= g_slist_append(sl
, (gpointer
)inputs
[i
]);
89 sl
= g_slist_sort(sl
, sort_inputs
);
90 for (l
= sl
; l
; l
= l
->next
) {
92 printf(" %-20s %s\n", sr_input_id_get(input
),
93 sr_input_description_get(input
));
98 printf("Supported output formats:\n");
99 outputs
= sr_output_list();
100 for (sl
= NULL
, i
= 0; outputs
[i
]; i
++)
101 sl
= g_slist_append(sl
, (gpointer
)outputs
[i
]);
102 sl
= g_slist_sort(sl
, sort_outputs
);
103 for (l
= sl
; l
; l
= l
->next
) {
105 printf(" %-20s %s\n", sr_output_id_get(output
),
106 sr_output_description_get(output
));
112 if (srd_init(NULL
) == SRD_OK
) {
113 printf("Supported protocol decoders:\n");
114 srd_decoder_load_all();
115 sl
= g_slist_copy((GSList
*)srd_decoder_list());
116 sl
= g_slist_sort(sl
, sort_pds
);
117 for (l
= sl
; l
; l
= l
->next
) {
119 printf(" %-20s %s\n", dec
->id
, dec
->longname
);
120 /* Print protocol description upon "-l 3" or higher. */
121 if (opt_loglevel
>= SR_LOG_INFO
)
122 printf(" %-20s %s\n", "", dec
->desc
);
131 static gint
sort_channels(gconstpointer a
, gconstpointer b
)
133 const struct sr_channel
*pa
= a
, *pb
= b
;
135 return pa
->index
- pb
->index
;
138 static void print_dev_line(const struct sr_dev_inst
*sdi
)
140 struct sr_channel
*ch
;
141 GSList
*sl
, *l
, *channels
;
144 struct sr_dev_driver
*driver
;
145 const char *vendor
, *model
, *version
;
147 driver
= sr_dev_inst_driver_get(sdi
);
148 vendor
= sr_dev_inst_vendor_get(sdi
);
149 model
= sr_dev_inst_model_get(sdi
);
150 version
= sr_dev_inst_version_get(sdi
);
151 channels
= sr_dev_inst_channels_get(sdi
);
153 s
= g_string_sized_new(128);
154 g_string_assign(s
, driver
->name
);
155 if (maybe_config_get(driver
, sdi
, NULL
, SR_CONF_CONN
, &gvar
) == SR_OK
) {
156 g_string_append(s
, ":conn=");
157 g_string_append(s
, g_variant_get_string(gvar
, NULL
));
158 g_variant_unref(gvar
);
160 g_string_append(s
, " - ");
161 if (vendor
&& vendor
[0])
162 g_string_append_printf(s
, "%s ", vendor
);
163 if (model
&& model
[0])
164 g_string_append_printf(s
, "%s ", model
);
165 if (version
&& version
[0])
166 g_string_append_printf(s
, "%s ", version
);
168 if (g_slist_length(channels
) == 1) {
170 g_string_append_printf(s
, "with 1 channel: %s", ch
->name
);
172 sl
= g_slist_sort(g_slist_copy(channels
), sort_channels
);
173 g_string_append_printf(s
, "with %d channels:", g_slist_length(sl
));
174 for (l
= sl
; l
; l
= l
->next
) {
176 g_string_append_printf(s
, " %s", ch
->name
);
181 g_string_append_printf(s
, "\n");
182 printf("%s", s
->str
);
183 g_string_free(s
, TRUE
);
187 void show_dev_list(void)
189 struct sr_dev_inst
*sdi
;
192 if (!(devices
= device_scan()))
195 printf("The following devices were found:\n");
196 for (l
= devices
; l
; l
= l
->next
) {
200 g_slist_free(devices
);
204 void show_drv_detail(struct sr_dev_driver
*driver
)
206 const struct sr_config_info
*srci
;
208 const uint32_t *opts
;
209 gsize num_elements
, i
;
211 if (sr_config_list(driver
, NULL
, NULL
, SR_CONF_DEVICE_OPTIONS
,
212 &gvar_opts
) == SR_OK
) {
213 opts
= g_variant_get_fixed_array(gvar_opts
, &num_elements
,
216 printf("Driver functions:\n");
217 for (i
= 0; i
< num_elements
; i
++) {
218 if (!(srci
= sr_config_info_get(opts
[i
] & SR_CONF_MASK
)))
220 printf(" %s\n", srci
->name
);
223 g_variant_unref(gvar_opts
);
226 if (sr_config_list(driver
, NULL
, NULL
, SR_CONF_SCAN_OPTIONS
,
227 &gvar_opts
) == SR_OK
) {
228 opts
= g_variant_get_fixed_array(gvar_opts
, &num_elements
,
231 printf("Scan options:\n");
232 for (i
= 0; i
< num_elements
; i
++) {
233 if (!(srci
= sr_config_info_get(opts
[i
] & SR_CONF_MASK
)))
235 printf(" %s\n", srci
->id
);
238 g_variant_unref(gvar_opts
);
242 void show_dev_detail(void)
244 struct sr_dev_driver
*driver_from_opt
, *driver
;
245 struct sr_dev_inst
*sdi
;
246 const struct sr_config_info
*srci
;
247 struct sr_channel
*ch
;
248 struct sr_channel_group
*channel_group
, *cg
;
249 GSList
*devices
, *cgl
, *chl
, *channel_groups
;
250 GVariant
*gvar_opts
, *gvar_dict
, *gvar_list
, *gvar
;
251 gsize num_opts
, num_elements
;
252 double dlow
, dhigh
, dcur_low
, dcur_high
;
253 const uint64_t *uint64
, p
, q
, low
, high
;
254 uint64_t tmp_uint64
, cur_low
, cur_high
, cur_p
, cur_q
;
255 const uint32_t *opts
;
256 const int32_t *int32
;
258 unsigned int num_devices
, i
;
259 char *tmp_str
, *s
, c
;
260 const char **stropts
;
262 if (parse_driver(opt_drv
, &driver_from_opt
, NULL
)) {
263 /* A driver was specified, report driver-wide options now. */
264 show_drv_detail(driver_from_opt
);
267 if (!(devices
= device_scan())) {
268 g_critical("No devices found.");
272 num_devices
= g_slist_length(devices
);
273 if (num_devices
> 1) {
274 g_critical("%d devices found. Use --scan to show them, "
275 "and select one to show.", num_devices
);
280 g_slist_free(devices
);
283 driver
= sr_dev_inst_driver_get(sdi
);
284 channel_groups
= sr_dev_inst_channel_groups_get(sdi
);
286 if (sr_dev_open(sdi
) != SR_OK
) {
287 g_critical("Failed to open device.");
291 /* Selected channels and channel group may affect which options are
292 * returned, or which values for them. */
293 select_channels(sdi
);
294 channel_group
= select_channel_group(sdi
);
296 if (sr_config_list(driver
, sdi
, channel_group
, SR_CONF_DEVICE_OPTIONS
,
297 &gvar_opts
) != SR_OK
)
298 /* Driver supports no device instance options. */
301 if (channel_groups
) {
302 printf("Channel groups:\n");
303 for (cgl
= channel_groups
; cgl
; cgl
= cgl
->next
) {
305 printf(" %s: channel%s", cg
->name
,
306 g_slist_length(cg
->channels
) > 1 ? "s" : "");
307 for (chl
= cg
->channels
; chl
; chl
= chl
->next
) {
309 printf(" %s", ch
->name
);
315 printf("Supported configuration options");
316 if (channel_groups
) {
318 printf(" across all channel groups");
320 printf(" on channel group %s", channel_group
->name
);
323 opts
= g_variant_get_fixed_array(gvar_opts
, &num_opts
, sizeof(uint32_t));
324 for (o
= 0; o
< num_opts
; o
++) {
325 key
= opts
[o
] & SR_CONF_MASK
;
326 if (!(srci
= sr_config_info_get(key
)))
329 if (key
== SR_CONF_TRIGGER_MATCH
) {
330 if (maybe_config_list(driver
, sdi
, channel_group
, key
,
331 &gvar_list
) != SR_OK
) {
335 int32
= g_variant_get_fixed_array(gvar_list
,
336 &num_elements
, sizeof(int32_t));
337 printf(" Supported triggers: ");
338 for (i
= 0; i
< num_elements
; i
++) {
340 case SR_TRIGGER_ZERO
:
346 case SR_TRIGGER_RISING
:
349 case SR_TRIGGER_FALLING
:
352 case SR_TRIGGER_EDGE
:
355 case SR_TRIGGER_OVER
:
358 case SR_TRIGGER_UNDER
:
369 g_variant_unref(gvar_list
);
371 } else if (key
== SR_CONF_LIMIT_SAMPLES
372 && config_key_has_cap(driver
, sdi
, cg
, key
, SR_CONF_LIST
)) {
373 /* If implemented in config_list(), this denotes the
374 * maximum number of samples a device can send. This
375 * really applies only to logic analyzers, and then
376 * only to those that don't support compression, or
377 * have it turned off by default. The values returned
378 * are the low/high limits. */
379 if (sr_config_list(driver
, sdi
, channel_group
, key
,
381 g_variant_get(gvar
, "(tt)", &low
, &high
);
382 g_variant_unref(gvar
);
383 printf(" Maximum number of samples: %"PRIu64
"\n", high
);
386 } else if (key
== SR_CONF_SAMPLERATE
) {
387 /* Supported samplerates */
388 printf(" %s", srci
->id
);
389 if (maybe_config_list(driver
, sdi
, channel_group
, SR_CONF_SAMPLERATE
,
390 &gvar_dict
) != SR_OK
) {
394 if ((gvar_list
= g_variant_lookup_value(gvar_dict
,
395 "samplerates", G_VARIANT_TYPE("at")))) {
396 uint64
= g_variant_get_fixed_array(gvar_list
,
397 &num_elements
, sizeof(uint64_t));
398 printf(" - supported samplerates:\n");
399 for (i
= 0; i
< num_elements
; i
++) {
400 if (!(s
= sr_samplerate_string(uint64
[i
])))
405 g_variant_unref(gvar_list
);
406 } else if ((gvar_list
= g_variant_lookup_value(gvar_dict
,
407 "samplerate-steps", G_VARIANT_TYPE("at")))) {
408 uint64
= g_variant_get_fixed_array(gvar_list
,
409 &num_elements
, sizeof(uint64_t));
411 if (!(s
= sr_samplerate_string(uint64
[0])))
416 if (!(s
= sr_samplerate_string(uint64
[1])))
421 if (!(s
= sr_samplerate_string(uint64
[2])))
423 printf(" in steps of %s)\n", s
);
425 g_variant_unref(gvar_list
);
427 g_variant_unref(gvar_dict
);
429 } else if (srci
->datatype
== SR_T_UINT64
) {
430 printf(" %s: ", srci
->id
);
432 if (maybe_config_get(driver
, sdi
, channel_group
, key
,
434 tmp_uint64
= g_variant_get_uint64(gvar
);
435 g_variant_unref(gvar
);
438 if (maybe_config_list(driver
, sdi
, channel_group
,
439 key
, &gvar_list
) != SR_OK
) {
441 /* Can't list it, but we have a value to show. */
442 printf("%"PRIu64
" (current)", tmp_uint64
);
447 uint64
= g_variant_get_fixed_array(gvar_list
,
448 &num_elements
, sizeof(uint64_t));
449 printf(" - supported values:\n");
450 for (i
= 0; i
< num_elements
; i
++) {
451 printf(" %"PRIu64
, uint64
[i
]);
452 if (gvar
&& tmp_uint64
== uint64
[i
])
453 printf(" (current)");
456 g_variant_unref(gvar_list
);
458 } else if (srci
->datatype
== SR_T_STRING
) {
459 printf(" %s: ", srci
->id
);
460 if (maybe_config_get(driver
, sdi
, channel_group
, key
,
462 tmp_str
= g_strdup(g_variant_get_string(gvar
, NULL
));
463 g_variant_unref(gvar
);
467 if (maybe_config_list(driver
, sdi
, channel_group
, key
,
470 /* Can't list it, but we have a value to show. */
471 printf("%s (current)", tmp_str
);
478 stropts
= g_variant_get_strv(gvar
, &num_elements
);
479 for (i
= 0; i
< num_elements
; i
++) {
482 printf("%s", stropts
[i
]);
483 if (tmp_str
&& !strcmp(tmp_str
, stropts
[i
]))
484 printf(" (current)");
489 g_variant_unref(gvar
);
491 } else if (srci
->datatype
== SR_T_UINT64_RANGE
) {
492 printf(" %s: ", srci
->id
);
493 if (maybe_config_list(driver
, sdi
, channel_group
, key
,
494 &gvar_list
) != SR_OK
) {
499 if (maybe_config_get(driver
, sdi
, channel_group
, key
, &gvar
) == SR_OK
) {
500 g_variant_get(gvar
, "(tt)", &cur_low
, &cur_high
);
501 g_variant_unref(gvar
);
507 num_elements
= g_variant_n_children(gvar_list
);
508 for (i
= 0; i
< num_elements
; i
++) {
509 gvar
= g_variant_get_child_value(gvar_list
, i
);
510 g_variant_get(gvar
, "(tt)", &low
, &high
);
511 g_variant_unref(gvar
);
514 printf("%"PRIu64
"-%"PRIu64
, low
, high
);
515 if (low
== cur_low
&& high
== cur_high
)
516 printf(" (current)");
519 g_variant_unref(gvar_list
);
521 } else if (srci
->datatype
== SR_T_BOOL
) {
522 printf(" %s: ", srci
->id
);
523 if (maybe_config_get(driver
, sdi
, channel_group
, key
,
525 if (g_variant_get_boolean(gvar
))
526 printf("on (current), off\n");
528 printf("on, off (current)\n");
529 g_variant_unref(gvar
);
533 } else if (srci
->datatype
== SR_T_DOUBLE_RANGE
) {
534 printf(" %s: ", srci
->id
);
535 if (maybe_config_list(driver
, sdi
, channel_group
, key
,
536 &gvar_list
) != SR_OK
) {
541 if (maybe_config_get(driver
, sdi
, channel_group
, key
, &gvar
) == SR_OK
) {
542 g_variant_get(gvar
, "(dd)", &dcur_low
, &dcur_high
);
543 g_variant_unref(gvar
);
549 num_elements
= g_variant_n_children(gvar_list
);
550 for (i
= 0; i
< num_elements
; i
++) {
551 gvar
= g_variant_get_child_value(gvar_list
, i
);
552 g_variant_get(gvar
, "(dd)", &dlow
, &dhigh
);
553 g_variant_unref(gvar
);
556 printf("%.1f-%.1f", dlow
, dhigh
);
557 if (dlow
== dcur_low
&& dhigh
== dcur_high
)
558 printf(" (current)");
561 g_variant_unref(gvar_list
);
563 } else if (srci
->datatype
== SR_T_FLOAT
) {
564 printf(" %s: ", srci
->id
);
565 if (maybe_config_get(driver
, sdi
, channel_group
, key
,
567 printf("%f\n", g_variant_get_double(gvar
));
568 g_variant_unref(gvar
);
572 } else if (srci
->datatype
== SR_T_RATIONAL_PERIOD
573 || srci
->datatype
== SR_T_RATIONAL_VOLT
) {
574 printf(" %s", srci
->id
);
575 if (maybe_config_get(driver
, sdi
, channel_group
, key
,
577 g_variant_get(gvar
, "(tt)", &cur_p
, &cur_q
);
578 g_variant_unref(gvar
);
582 if (maybe_config_list(driver
, sdi
, channel_group
,
583 key
, &gvar_list
) != SR_OK
) {
587 printf(" - supported values:\n");
588 num_elements
= g_variant_n_children(gvar_list
);
589 for (i
= 0; i
< num_elements
; i
++) {
590 gvar
= g_variant_get_child_value(gvar_list
, i
);
591 g_variant_get(gvar
, "(tt)", &p
, &q
);
592 if (srci
->datatype
== SR_T_RATIONAL_PERIOD
)
593 s
= sr_period_string(p
* q
);
595 s
= sr_voltage_string(p
, q
);
598 if (p
== cur_p
&& q
== cur_q
)
599 printf(" (current)");
602 g_variant_unref(gvar_list
);
606 /* Everything else */
607 printf(" %s\n", srci
->id
);
610 g_variant_unref(gvar_opts
);
617 void show_pd_detail(void)
619 struct srd_decoder
*dec
;
620 struct srd_decoder_option
*o
;
621 struct srd_channel
*pdch
;
622 struct srd_decoder_annotation_row
*r
;
625 char **pdtokens
, **pdtok
, *optsep
, **ann
, *val
, *doc
;
627 pdtokens
= g_strsplit(opt_pds
, ",", -1);
628 for (pdtok
= pdtokens
; *pdtok
; pdtok
++) {
630 if ((optsep
= strchr(*pdtok
, ':')))
632 if (!(dec
= srd_decoder_get_by_id(*pdtok
))) {
633 g_critical("Protocol decoder %s not found.", *pdtok
);
636 printf("ID: %s\nName: %s\nLong name: %s\nDescription: %s\n",
637 dec
->id
, dec
->name
, dec
->longname
, dec
->desc
);
638 printf("License: %s\n", dec
->license
);
639 printf("Annotation classes:\n");
640 if (dec
->annotations
) {
641 for (l
= dec
->annotations
; l
; l
= l
->next
) {
643 printf("- %s: %s\n", ann
[0], ann
[1]);
648 printf("Annotation rows:\n");
649 if (dec
->annotation_rows
) {
650 for (l
= dec
->annotation_rows
; l
; l
= l
->next
) {
652 printf("- %s (%s): ", r
->id
, r
->desc
);
653 for (ll
= r
->ann_classes
; ll
; ll
= ll
->next
) {
654 idx
= GPOINTER_TO_INT(ll
->data
);
655 ann
= g_slist_nth_data(dec
->annotations
, idx
);
656 printf("%s", ann
[0]);
665 printf("Required channels:\n");
667 for (l
= dec
->channels
; l
; l
= l
->next
) {
669 printf("- %s (%s): %s\n",
670 pdch
->id
, pdch
->name
, pdch
->desc
);
675 printf("Optional channels:\n");
676 if (dec
->opt_channels
) {
677 for (l
= dec
->opt_channels
; l
; l
= l
->next
) {
679 printf("- %s (%s): %s\n",
680 pdch
->id
, pdch
->name
, pdch
->desc
);
685 printf("Options:\n");
687 for (l
= dec
->options
; l
; l
= l
->next
) {
689 printf("- %s: %s (", o
->id
, o
->desc
);
690 for (ol
= o
->values
; ol
; ol
= ol
->next
) {
691 val
= g_variant_print(ol
->data
, FALSE
);
695 val
= g_variant_print(o
->def
, FALSE
);
696 printf("default %s)\n", val
);
702 if ((doc
= srd_decoder_doc_get(dec
))) {
703 printf("Documentation:\n%s\n",
704 doc
[0] == '\n' ? doc
+ 1 : doc
);
709 g_strfreev(pdtokens
);
713 void show_input(void)
715 const struct sr_input_module
*imod
;
716 const struct sr_option
**opts
;
721 tok
= g_strsplit(opt_input_format
, ":", 0);
722 if (!tok
[0] || !(imod
= sr_input_find(tok
[0])))
723 g_critical("Input module '%s' not found.", opt_input_format
);
725 printf("ID: %s\nName: %s\n", sr_input_id_get(imod
),
726 sr_input_name_get(imod
));
727 printf("Description: %s\n", sr_input_description_get(imod
));
728 if ((opts
= sr_input_options_get(imod
))) {
729 printf("Options:\n");
730 for (i
= 0; opts
[i
]; i
++) {
731 printf(" %s: %s", opts
[i
]->id
, opts
[i
]->desc
);
733 s
= g_variant_print(opts
[i
]->def
, FALSE
);
734 printf(" (default %s", s
);
736 if (opts
[i
]->values
) {
737 printf(", possible values ");
738 for (l
= opts
[i
]->values
; l
; l
= l
->next
) {
739 s
= g_variant_print((GVariant
*)l
->data
, FALSE
);
740 printf("%s%s", s
, l
->next
? ", " : "");
748 sr_input_options_free(opts
);
753 void show_output(void)
755 const struct sr_output_module
*omod
;
756 const struct sr_option
**opts
;
761 tok
= g_strsplit(opt_output_format
, ":", 0);
762 if (!tok
[0] || !(omod
= sr_output_find(tok
[0])))
763 g_critical("Output module '%s' not found.", opt_output_format
);
765 printf("ID: %s\nName: %s\n", sr_output_id_get(omod
),
766 sr_output_name_get(omod
));
767 printf("Description: %s\n", sr_output_description_get(omod
));
768 if ((opts
= sr_output_options_get(omod
))) {
769 printf("Options:\n");
770 for (i
= 0; opts
[i
]; i
++) {
771 printf(" %s: %s", opts
[i
]->id
, opts
[i
]->desc
);
773 s
= g_variant_print(opts
[i
]->def
, FALSE
);
774 printf(" (default %s", s
);
776 if (opts
[i
]->values
) {
777 printf(", possible values ");
778 for (l
= opts
[i
]->values
; l
; l
= l
->next
) {
779 s
= g_variant_print((GVariant
*)l
->data
, FALSE
);
780 printf("%s%s", s
, l
->next
? ", " : "");
788 sr_output_options_free(opts
);