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/>.
22 #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_transforms(gconstpointer a, gconstpointer b)
38 return strcmp(sr_transform_id_get((struct sr_transform_module *)a),
39 sr_transform_id_get((struct sr_transform_module *)b));
42 static gint sort_drivers(gconstpointer a, gconstpointer b)
44 const struct sr_dev_driver *sdda = a, *sddb = b;
46 return strcmp(sdda->name, sddb->name);
50 static gint sort_pds(gconstpointer a, gconstpointer b)
52 const struct srd_decoder *sda = a, *sdb = b;
54 return strcmp(sda->id, sdb->id);
58 void show_version(void)
60 struct sr_dev_driver **drivers, *driver;
61 const struct sr_input_module **inputs, *input;
62 const struct sr_output_module **outputs, *output;
63 const struct sr_transform_module **transforms, *transform;
68 struct srd_decoder *dec;
71 printf("sigrok-cli %s\n\n", VERSION);
73 printf("Using libsigrok %s (lib version %s).\n",
74 sr_package_version_string_get(), sr_lib_version_string_get());
76 printf("Using libsigrokdecode %s (lib version %s).\n\n",
77 srd_package_version_string_get(), srd_lib_version_string_get());
80 printf("Supported hardware drivers:\n");
81 drivers = sr_driver_list(sr_ctx);
82 for (sl = NULL, i = 0; drivers[i]; i++)
83 sl = g_slist_append(sl, drivers[i]);
84 sl = g_slist_sort(sl, sort_drivers);
85 for (l = sl; l; l = l->next) {
87 printf(" %-20s %s\n", driver->name, driver->longname);
92 printf("Supported input formats:\n");
93 inputs = sr_input_list();
94 for (sl = NULL, i = 0; inputs[i]; i++)
95 sl = g_slist_append(sl, (gpointer)inputs[i]);
96 sl = g_slist_sort(sl, sort_inputs);
97 for (l = sl; l; l = l->next) {
99 printf(" %-20s %s\n", sr_input_id_get(input),
100 sr_input_description_get(input));
105 printf("Supported output formats:\n");
106 outputs = sr_output_list();
107 for (sl = NULL, i = 0; outputs[i]; i++)
108 sl = g_slist_append(sl, (gpointer)outputs[i]);
109 sl = g_slist_sort(sl, sort_outputs);
110 for (l = sl; l; l = l->next) {
112 printf(" %-20s %s\n", sr_output_id_get(output),
113 sr_output_description_get(output));
118 printf("Supported transform modules:\n");
119 transforms = sr_transform_list();
120 for (sl = NULL, i = 0; transforms[i]; i++)
121 sl = g_slist_append(sl, (gpointer)transforms[i]);
122 sl = g_slist_sort(sl, sort_transforms);
123 for (l = sl; l; l = l->next) {
125 printf(" %-20s %s\n", sr_transform_id_get(transform),
126 sr_transform_description_get(transform));
132 if (srd_init(NULL) == SRD_OK) {
133 printf("Supported protocol decoders:\n");
134 srd_decoder_load_all();
135 sl = g_slist_copy((GSList *)srd_decoder_list());
136 sl = g_slist_sort(sl, sort_pds);
137 for (l = sl; l; l = l->next) {
139 printf(" %-20s %s\n", dec->id, dec->longname);
140 /* Print protocol description upon "-l 3" or higher. */
141 if (opt_loglevel >= SR_LOG_INFO)
142 printf(" %-20s %s\n", "", dec->desc);
151 static gint sort_channels(gconstpointer a, gconstpointer b)
153 const struct sr_channel *pa = a, *pb = b;
155 return pa->index - pb->index;
158 static void print_dev_line(const struct sr_dev_inst *sdi)
160 struct sr_channel *ch;
161 GSList *sl, *l, *channels;
164 struct sr_dev_driver *driver;
165 const char *vendor, *model, *version;
167 driver = sr_dev_inst_driver_get(sdi);
168 vendor = sr_dev_inst_vendor_get(sdi);
169 model = sr_dev_inst_model_get(sdi);
170 version = sr_dev_inst_version_get(sdi);
171 channels = sr_dev_inst_channels_get(sdi);
173 s = g_string_sized_new(128);
174 g_string_assign(s, driver->name);
175 if (maybe_config_get(driver, sdi, NULL, SR_CONF_CONN, &gvar) == SR_OK) {
176 g_string_append(s, ":conn=");
177 g_string_append(s, g_variant_get_string(gvar, NULL));
178 g_variant_unref(gvar);
180 g_string_append(s, " - ");
181 if (vendor && vendor[0])
182 g_string_append_printf(s, "%s ", vendor);
183 if (model && model[0])
184 g_string_append_printf(s, "%s ", model);
185 if (version && version[0])
186 g_string_append_printf(s, "%s ", version);
188 if (g_slist_length(channels) == 1) {
190 g_string_append_printf(s, "with 1 channel: %s", ch->name);
192 sl = g_slist_sort(g_slist_copy(channels), sort_channels);
193 g_string_append_printf(s, "with %d channels:", g_slist_length(sl));
194 for (l = sl; l; l = l->next) {
196 g_string_append_printf(s, " %s", ch->name);
201 g_string_append_printf(s, "\n");
202 printf("%s", s->str);
203 g_string_free(s, TRUE);
207 void show_dev_list(void)
209 struct sr_dev_inst *sdi;
212 if (!(devices = device_scan()))
215 printf("The following devices were found:\n");
216 for (l = devices; l; l = l->next) {
220 g_slist_free(devices);
224 void show_drv_detail(struct sr_dev_driver *driver)
226 const struct sr_config_info *srci;
228 const uint32_t *opts;
229 gsize num_elements, i;
231 if (sr_config_list(driver, NULL, NULL, SR_CONF_DEVICE_OPTIONS,
232 &gvar_opts) == SR_OK) {
233 opts = g_variant_get_fixed_array(gvar_opts, &num_elements,
236 printf("Driver functions:\n");
237 for (i = 0; i < num_elements; i++) {
238 if (!(srci = sr_config_info_get(opts[i] & SR_CONF_MASK)))
240 printf(" %s\n", srci->name);
243 g_variant_unref(gvar_opts);
246 if (sr_config_list(driver, NULL, NULL, SR_CONF_SCAN_OPTIONS,
247 &gvar_opts) == SR_OK) {
248 opts = g_variant_get_fixed_array(gvar_opts, &num_elements,
251 printf("Scan options:\n");
252 for (i = 0; i < num_elements; i++) {
253 if (!(srci = sr_config_info_get(opts[i] & SR_CONF_MASK)))
255 printf(" %s\n", srci->id);
258 g_variant_unref(gvar_opts);
262 void show_dev_detail(void)
264 struct sr_dev_driver *driver_from_opt, *driver;
265 struct sr_dev_inst *sdi;
266 const struct sr_config_info *srci;
267 struct sr_channel *ch;
268 struct sr_channel_group *channel_group, *cg;
269 GSList *devices, *cgl, *chl, *channel_groups;
270 GVariant *gvar_opts, *gvar_dict, *gvar_list, *gvar;
271 gsize num_opts, num_elements;
272 double dlow, dhigh, dcur_low, dcur_high;
273 const uint64_t *uint64, p, q, low, high;
274 uint64_t tmp_uint64, cur_low, cur_high, cur_p, cur_q;
275 const uint32_t *opts;
276 const int32_t *int32;
278 unsigned int num_devices, i;
279 char *tmp_str, *s, c;
280 const char **stropts;
282 if (parse_driver(opt_drv, &driver_from_opt, NULL)) {
283 /* A driver was specified, report driver-wide options now. */
284 show_drv_detail(driver_from_opt);
287 if (!(devices = device_scan())) {
288 g_critical("No devices found.");
292 num_devices = g_slist_length(devices);
293 if (num_devices > 1) {
294 g_critical("%d devices found. Use --scan to show them, "
295 "and select one to show.", num_devices);
300 g_slist_free(devices);
303 driver = sr_dev_inst_driver_get(sdi);
304 channel_groups = sr_dev_inst_channel_groups_get(sdi);
306 if (sr_dev_open(sdi) != SR_OK) {
307 g_critical("Failed to open device.");
312 * Selected channels and channel group may affect which options are
313 * returned, or which values for them.
315 select_channels(sdi);
316 channel_group = select_channel_group(sdi);
318 if (sr_config_list(driver, sdi, channel_group, SR_CONF_DEVICE_OPTIONS,
319 &gvar_opts) != SR_OK)
320 /* Driver supports no device instance options. */
323 if (channel_groups) {
324 printf("Channel groups:\n");
325 for (cgl = channel_groups; cgl; cgl = cgl->next) {
327 printf(" %s: channel%s", cg->name,
328 g_slist_length(cg->channels) > 1 ? "s" : "");
329 for (chl = cg->channels; chl; chl = chl->next) {
331 printf(" %s", ch->name);
337 printf("Supported configuration options");
338 if (channel_groups) {
340 printf(" across all channel groups");
342 printf(" on channel group %s", channel_group->name);
345 opts = g_variant_get_fixed_array(gvar_opts, &num_opts, sizeof(uint32_t));
346 for (o = 0; o < num_opts; o++) {
347 key = opts[o] & SR_CONF_MASK;
348 if (!(srci = sr_config_info_get(key)))
351 if (key == SR_CONF_TRIGGER_MATCH) {
352 if (maybe_config_list(driver, sdi, channel_group, key,
353 &gvar_list) != SR_OK) {
357 int32 = g_variant_get_fixed_array(gvar_list,
358 &num_elements, sizeof(int32_t));
359 printf(" Supported triggers: ");
360 for (i = 0; i < num_elements; i++) {
362 case SR_TRIGGER_ZERO:
368 case SR_TRIGGER_RISING:
371 case SR_TRIGGER_FALLING:
374 case SR_TRIGGER_EDGE:
377 case SR_TRIGGER_OVER:
380 case SR_TRIGGER_UNDER:
391 g_variant_unref(gvar_list);
393 } else if (key == SR_CONF_LIMIT_SAMPLES
394 && config_key_has_cap(driver, sdi, NULL, key, SR_CONF_LIST)) {
396 * If implemented in config_list(), this denotes the
397 * maximum number of samples a device can send. This
398 * really applies only to logic analyzers, and then
399 * only to those that don't support compression, or
400 * have it turned off by default. The values returned
401 * are the low/high limits.
403 if (sr_config_list(driver, sdi, channel_group, key,
405 g_variant_get(gvar, "(tt)", &low, &high);
406 g_variant_unref(gvar);
407 printf(" Maximum number of samples: %"PRIu64"\n", high);
410 } else if (key == SR_CONF_SAMPLERATE) {
411 /* Supported samplerates */
412 printf(" %s", srci->id);
413 if (maybe_config_list(driver, sdi, channel_group, SR_CONF_SAMPLERATE,
414 &gvar_dict) != SR_OK) {
418 if ((gvar_list = g_variant_lookup_value(gvar_dict,
419 "samplerates", G_VARIANT_TYPE("at")))) {
420 uint64 = g_variant_get_fixed_array(gvar_list,
421 &num_elements, sizeof(uint64_t));
422 printf(" - supported samplerates:\n");
423 for (i = 0; i < num_elements; i++) {
424 if (!(s = sr_samplerate_string(uint64[i])))
429 g_variant_unref(gvar_list);
430 } else if ((gvar_list = g_variant_lookup_value(gvar_dict,
431 "samplerate-steps", G_VARIANT_TYPE("at")))) {
432 uint64 = g_variant_get_fixed_array(gvar_list,
433 &num_elements, sizeof(uint64_t));
435 if (!(s = sr_samplerate_string(uint64[0])))
440 if (!(s = sr_samplerate_string(uint64[1])))
445 if (!(s = sr_samplerate_string(uint64[2])))
447 printf(" in steps of %s)\n", s);
449 g_variant_unref(gvar_list);
451 g_variant_unref(gvar_dict);
453 } else if (srci->datatype == SR_T_UINT64) {
454 printf(" %s: ", srci->id);
456 if (maybe_config_get(driver, sdi, channel_group, key,
458 tmp_uint64 = g_variant_get_uint64(gvar);
459 g_variant_unref(gvar);
462 if (maybe_config_list(driver, sdi, channel_group,
463 key, &gvar_list) != SR_OK) {
465 /* Can't list it, but we have a value to show. */
466 printf("%"PRIu64" (current)", tmp_uint64);
471 uint64 = g_variant_get_fixed_array(gvar_list,
472 &num_elements, sizeof(uint64_t));
473 printf(" - supported values:\n");
474 for (i = 0; i < num_elements; i++) {
475 printf(" %"PRIu64, uint64[i]);
476 if (gvar && tmp_uint64 == uint64[i])
477 printf(" (current)");
480 g_variant_unref(gvar_list);
482 } else if (srci->datatype == SR_T_STRING) {
483 printf(" %s: ", srci->id);
484 if (maybe_config_get(driver, sdi, channel_group, key,
486 tmp_str = g_strdup(g_variant_get_string(gvar, NULL));
487 g_variant_unref(gvar);
491 if (maybe_config_list(driver, sdi, channel_group, key,
494 /* Can't list it, but we have a value to show. */
495 printf("%s (current)", tmp_str);
502 stropts = g_variant_get_strv(gvar, &num_elements);
503 for (i = 0; i < num_elements; i++) {
506 printf("%s", stropts[i]);
507 if (tmp_str && !strcmp(tmp_str, stropts[i]))
508 printf(" (current)");
513 g_variant_unref(gvar);
515 } else if (srci->datatype == SR_T_UINT64_RANGE) {
516 printf(" %s: ", srci->id);
517 if (maybe_config_list(driver, sdi, channel_group, key,
518 &gvar_list) != SR_OK) {
523 if (maybe_config_get(driver, sdi, channel_group, key, &gvar) == SR_OK) {
524 g_variant_get(gvar, "(tt)", &cur_low, &cur_high);
525 g_variant_unref(gvar);
531 num_elements = g_variant_n_children(gvar_list);
532 for (i = 0; i < num_elements; i++) {
533 gvar = g_variant_get_child_value(gvar_list, i);
534 g_variant_get(gvar, "(tt)", &low, &high);
535 g_variant_unref(gvar);
538 printf("%"PRIu64"-%"PRIu64, low, high);
539 if (low == cur_low && high == cur_high)
540 printf(" (current)");
543 g_variant_unref(gvar_list);
545 } else if (srci->datatype == SR_T_BOOL) {
546 printf(" %s: ", srci->id);
547 if (maybe_config_get(driver, sdi, channel_group, key,
549 if (g_variant_get_boolean(gvar))
550 printf("on (current), off\n");
552 printf("on, off (current)\n");
553 g_variant_unref(gvar);
557 } else if (srci->datatype == SR_T_DOUBLE_RANGE) {
558 printf(" %s: ", srci->id);
559 if (maybe_config_list(driver, sdi, channel_group, key,
560 &gvar_list) != SR_OK) {
565 if (maybe_config_get(driver, sdi, channel_group, key, &gvar) == SR_OK) {
566 g_variant_get(gvar, "(dd)", &dcur_low, &dcur_high);
567 g_variant_unref(gvar);
573 num_elements = g_variant_n_children(gvar_list);
574 for (i = 0; i < num_elements; i++) {
575 gvar = g_variant_get_child_value(gvar_list, i);
576 g_variant_get(gvar, "(dd)", &dlow, &dhigh);
577 g_variant_unref(gvar);
580 printf("%.1f-%.1f", dlow, dhigh);
581 if (dlow == dcur_low && dhigh == dcur_high)
582 printf(" (current)");
585 g_variant_unref(gvar_list);
587 } else if (srci->datatype == SR_T_FLOAT) {
588 printf(" %s: ", srci->id);
589 if (maybe_config_get(driver, sdi, channel_group, key,
591 printf("%f\n", g_variant_get_double(gvar));
592 g_variant_unref(gvar);
596 } else if (srci->datatype == SR_T_RATIONAL_PERIOD
597 || srci->datatype == SR_T_RATIONAL_VOLT) {
598 printf(" %s", srci->id);
599 if (maybe_config_get(driver, sdi, channel_group, key,
601 g_variant_get(gvar, "(tt)", &cur_p, &cur_q);
602 g_variant_unref(gvar);
606 if (maybe_config_list(driver, sdi, channel_group,
607 key, &gvar_list) != SR_OK) {
611 printf(" - supported values:\n");
612 num_elements = g_variant_n_children(gvar_list);
613 for (i = 0; i < num_elements; i++) {
614 gvar = g_variant_get_child_value(gvar_list, i);
615 g_variant_get(gvar, "(tt)", &p, &q);
616 if (srci->datatype == SR_T_RATIONAL_PERIOD)
617 s = sr_period_string(p * q);
619 s = sr_voltage_string(p, q);
622 if (p == cur_p && q == cur_q)
623 printf(" (current)");
626 g_variant_unref(gvar_list);
630 /* Everything else */
631 printf(" %s\n", srci->id);
634 g_variant_unref(gvar_opts);
641 void show_pd_detail(void)
643 struct srd_decoder *dec;
644 struct srd_decoder_option *o;
645 struct srd_channel *pdch;
646 struct srd_decoder_annotation_row *r;
649 char **pdtokens, **pdtok, *optsep, **ann, *val, *doc;
651 pdtokens = g_strsplit(opt_pds, ",", -1);
652 for (pdtok = pdtokens; *pdtok; pdtok++) {
654 if ((optsep = strchr(*pdtok, ':')))
656 if (!(dec = srd_decoder_get_by_id(*pdtok))) {
657 g_critical("Protocol decoder %s not found.", *pdtok);
660 printf("ID: %s\nName: %s\nLong name: %s\nDescription: %s\n",
661 dec->id, dec->name, dec->longname, dec->desc);
662 printf("License: %s\n", dec->license);
663 printf("Annotation classes:\n");
664 if (dec->annotations) {
665 for (l = dec->annotations; l; l = l->next) {
667 printf("- %s: %s\n", ann[0], ann[1]);
672 printf("Annotation rows:\n");
673 if (dec->annotation_rows) {
674 for (l = dec->annotation_rows; l; l = l->next) {
676 printf("- %s (%s): ", r->id, r->desc);
677 for (ll = r->ann_classes; ll; ll = ll->next) {
678 idx = GPOINTER_TO_INT(ll->data);
679 ann = g_slist_nth_data(dec->annotations, idx);
680 printf("%s", ann[0]);
689 printf("Required channels:\n");
691 for (l = dec->channels; l; l = l->next) {
693 printf("- %s (%s): %s\n",
694 pdch->id, pdch->name, pdch->desc);
699 printf("Optional channels:\n");
700 if (dec->opt_channels) {
701 for (l = dec->opt_channels; l; l = l->next) {
703 printf("- %s (%s): %s\n",
704 pdch->id, pdch->name, pdch->desc);
709 printf("Options:\n");
711 for (l = dec->options; l; l = l->next) {
713 printf("- %s: %s (", o->id, o->desc);
714 for (ol = o->values; ol; ol = ol->next) {
715 val = g_variant_print(ol->data, FALSE);
719 val = g_variant_print(o->def, FALSE);
720 printf("default %s)\n", val);
726 if ((doc = srd_decoder_doc_get(dec))) {
727 printf("Documentation:\n%s\n",
728 doc[0] == '\n' ? doc + 1 : doc);
733 g_strfreev(pdtokens);
737 void show_input(void)
739 const struct sr_input_module *imod;
740 const struct sr_option **opts;
745 tok = g_strsplit(opt_input_format, ":", 0);
746 if (!tok[0] || !(imod = sr_input_find(tok[0])))
747 g_critical("Input module '%s' not found.", opt_input_format);
749 printf("ID: %s\nName: %s\n", sr_input_id_get(imod),
750 sr_input_name_get(imod));
751 printf("Description: %s\n", sr_input_description_get(imod));
752 if ((opts = sr_input_options_get(imod))) {
753 printf("Options:\n");
754 for (i = 0; opts[i]; i++) {
755 printf(" %s: %s", opts[i]->id, opts[i]->desc);
757 s = g_variant_print(opts[i]->def, FALSE);
758 printf(" (default %s", s);
760 if (opts[i]->values) {
761 printf(", possible values ");
762 for (l = opts[i]->values; l; l = l->next) {
763 s = g_variant_print((GVariant *)l->data, FALSE);
764 printf("%s%s", s, l->next ? ", " : "");
772 sr_input_options_free(opts);
777 void show_output(void)
779 const struct sr_output_module *omod;
780 const struct sr_option **opts;
785 tok = g_strsplit(opt_output_format, ":", 0);
786 if (!tok[0] || !(omod = sr_output_find(tok[0])))
787 g_critical("Output module '%s' not found.", opt_output_format);
789 printf("ID: %s\nName: %s\n", sr_output_id_get(omod),
790 sr_output_name_get(omod));
791 printf("Description: %s\n", sr_output_description_get(omod));
792 if ((opts = sr_output_options_get(omod))) {
793 printf("Options:\n");
794 for (i = 0; opts[i]; i++) {
795 printf(" %s: %s", opts[i]->id, opts[i]->desc);
797 s = g_variant_print(opts[i]->def, FALSE);
798 printf(" (default %s", s);
800 if (opts[i]->values) {
801 printf(", possible values ");
802 for (l = opts[i]->values; l; l = l->next) {
803 s = g_variant_print((GVariant *)l->data, FALSE);
804 printf("%s%s", s, l->next ? ", " : "");
812 sr_output_options_free(opts);
817 void show_transform(void)
819 const struct sr_transform_module *tmod;
820 const struct sr_option **opts;
825 tok = g_strsplit(opt_transform_module, ":", 0);
826 if (!tok[0] || !(tmod = sr_transform_find(tok[0])))
827 g_critical("Transform module '%s' not found.", opt_transform_module);
829 printf("ID: %s\nName: %s\n", sr_transform_id_get(tmod),
830 sr_transform_name_get(tmod));
831 printf("Description: %s\n", sr_transform_description_get(tmod));
832 if ((opts = sr_transform_options_get(tmod))) {
833 printf("Options:\n");
834 for (i = 0; opts[i]; i++) {
835 printf(" %s: %s", opts[i]->id, opts[i]->desc);
837 s = g_variant_print(opts[i]->def, FALSE);
838 printf(" (default %s", s);
840 if (opts[i]->values) {
841 printf(", possible values ");
842 for (l = opts[i]->values; l; l = l->next) {
843 s = g_variant_print((GVariant *)l->data, FALSE);
844 printf("%s%s", s, l->next ? ", " : "");
852 sr_transform_options_free(opts);