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/>.
23 #include "sigrok-cli.h"
25 static gint sort_inputs(gconstpointer a, gconstpointer b)
27 return strcmp(sr_input_id_get((struct sr_input_module *)a),
28 sr_input_id_get((struct sr_input_module *)b));
31 static gint sort_outputs(gconstpointer a, gconstpointer b)
33 return strcmp(sr_output_id_get((struct sr_output_module *)a),
34 sr_output_id_get((struct sr_output_module *)b));
37 static gint sort_transforms(gconstpointer a, gconstpointer b)
39 return strcmp(sr_transform_id_get((struct sr_transform_module *)a),
40 sr_transform_id_get((struct sr_transform_module *)b));
43 static gint sort_drivers(gconstpointer a, gconstpointer b)
45 const struct sr_dev_driver *sdda = a, *sddb = b;
47 return strcmp(sdda->name, sddb->name);
51 static gint sort_pds(gconstpointer a, gconstpointer b)
53 const struct srd_decoder *sda = a, *sdb = b;
55 return strcmp(sda->id, sdb->id);
59 void show_version(void)
61 struct sr_dev_driver **drivers, *driver;
62 const struct sr_input_module **inputs, *input;
63 const struct sr_output_module **outputs, *output;
64 const struct sr_transform_module **transforms, *transform;
69 struct srd_decoder *dec;
72 printf("sigrok-cli %s\n\n", SC_PACKAGE_VERSION_STRING);
74 printf("Using libsigrok %s (lib version %s).\n",
75 sr_package_version_string_get(), sr_lib_version_string_get());
77 printf("Using libsigrokdecode %s (lib version %s).\n\n",
78 srd_package_version_string_get(), srd_lib_version_string_get());
81 printf("Supported hardware drivers:\n");
82 drivers = sr_driver_list(sr_ctx);
83 for (sl = NULL, i = 0; drivers[i]; i++)
84 sl = g_slist_append(sl, drivers[i]);
85 sl = g_slist_sort(sl, sort_drivers);
86 for (l = sl; l; l = l->next) {
88 printf(" %-20s %s\n", driver->name, driver->longname);
93 printf("Supported input formats:\n");
94 inputs = sr_input_list();
95 for (sl = NULL, i = 0; inputs[i]; i++)
96 sl = g_slist_append(sl, (gpointer)inputs[i]);
97 sl = g_slist_sort(sl, sort_inputs);
98 for (l = sl; l; l = l->next) {
100 printf(" %-20s %s\n", sr_input_id_get(input),
101 sr_input_description_get(input));
106 printf("Supported output formats:\n");
107 outputs = sr_output_list();
108 for (sl = NULL, i = 0; outputs[i]; i++)
109 sl = g_slist_append(sl, (gpointer)outputs[i]);
110 sl = g_slist_sort(sl, sort_outputs);
111 for (l = sl; l; l = l->next) {
113 printf(" %-20s %s\n", sr_output_id_get(output),
114 sr_output_description_get(output));
119 printf("Supported transform modules:\n");
120 transforms = sr_transform_list();
121 for (sl = NULL, i = 0; transforms[i]; i++)
122 sl = g_slist_append(sl, (gpointer)transforms[i]);
123 sl = g_slist_sort(sl, sort_transforms);
124 for (l = sl; l; l = l->next) {
126 printf(" %-20s %s\n", sr_transform_id_get(transform),
127 sr_transform_description_get(transform));
133 if (srd_init(NULL) == SRD_OK) {
134 printf("Supported protocol decoders:\n");
135 srd_decoder_load_all();
136 sl = g_slist_copy((GSList *)srd_decoder_list());
137 sl = g_slist_sort(sl, sort_pds);
138 for (l = sl; l; l = l->next) {
140 printf(" %-20s %s\n", dec->id, dec->longname);
141 /* Print protocol description upon "-l 3" or higher. */
142 if (opt_loglevel >= SR_LOG_INFO)
143 printf(" %-20s %s\n", "", dec->desc);
152 static gint sort_channels(gconstpointer a, gconstpointer b)
154 const struct sr_channel *pa = a, *pb = b;
156 return pa->index - pb->index;
159 static void print_dev_line(const struct sr_dev_inst *sdi)
161 struct sr_channel *ch;
162 GSList *sl, *l, *channels;
165 struct sr_dev_driver *driver;
166 const char *vendor, *model, *version;
168 driver = sr_dev_inst_driver_get(sdi);
169 vendor = sr_dev_inst_vendor_get(sdi);
170 model = sr_dev_inst_model_get(sdi);
171 version = sr_dev_inst_version_get(sdi);
172 channels = sr_dev_inst_channels_get(sdi);
174 s = g_string_sized_new(128);
175 g_string_assign(s, driver->name);
176 if (maybe_config_get(driver, sdi, NULL, SR_CONF_CONN, &gvar) == SR_OK) {
177 g_string_append(s, ":conn=");
178 g_string_append(s, g_variant_get_string(gvar, NULL));
179 g_variant_unref(gvar);
181 g_string_append(s, " - ");
182 if (vendor && vendor[0])
183 g_string_append_printf(s, "%s ", vendor);
184 if (model && model[0])
185 g_string_append_printf(s, "%s ", model);
186 if (version && version[0])
187 g_string_append_printf(s, "%s ", version);
189 if (g_slist_length(channels) == 1) {
191 g_string_append_printf(s, "with 1 channel: %s", ch->name);
193 sl = g_slist_sort(g_slist_copy(channels), sort_channels);
194 g_string_append_printf(s, "with %d channels:", g_slist_length(sl));
195 for (l = sl; l; l = l->next) {
197 g_string_append_printf(s, " %s", ch->name);
202 g_string_append_printf(s, "\n");
203 printf("%s", s->str);
204 g_string_free(s, TRUE);
208 void show_dev_list(void)
210 struct sr_dev_inst *sdi;
213 if (!(devices = device_scan()))
216 printf("The following devices were found:\n");
217 for (l = devices; l; l = l->next) {
221 g_slist_free(devices);
225 void show_drv_detail(struct sr_dev_driver *driver)
227 const struct sr_key_info *srci;
231 if ((opts = sr_dev_options(driver, NULL, NULL))) {
233 printf("Driver functions:\n");
234 for (i = 0; i < opts->len; i++) {
235 if (!(srci = sr_key_info_get(SR_KEY_CONFIG,
236 g_array_index(opts, uint32_t, i))))
238 printf(" %s\n", srci->name);
241 g_array_free(opts, TRUE);
244 if ((opts = sr_driver_scan_options_list(driver))) {
246 printf("Scan options:\n");
247 for (i = 0; i < opts->len; i++) {
248 if (!(srci = sr_key_info_get(SR_KEY_CONFIG,
249 g_array_index(opts, uint32_t, i))))
251 printf(" %s\n", srci->id);
254 g_array_free(opts, TRUE);
258 void show_dev_detail(void)
260 struct sr_dev_driver *driver_from_opt, *driver;
261 struct sr_dev_inst *sdi;
262 const struct sr_key_info *srci, *srmqi, *srmqfi;
263 struct sr_channel *ch;
264 struct sr_channel_group *channel_group, *cg;
265 GSList *devices, *cgl, *chl, *channel_groups;
266 GVariant *gvar_dict, *gvar_list, *gvar;
268 double dlow, dhigh, dcur_low, dcur_high;
269 const uint64_t *uint64, p, q, low, high;
270 uint64_t tmp_uint64, mask, cur_low, cur_high, cur_p, cur_q;
272 const int32_t *int32;
273 uint32_t key, o, cur_mq, mq;
274 uint64_t cur_mqflags, mqflags;
275 unsigned int num_devices, i, j;
276 char *tmp_str, *s, c;
277 const char **stropts;
279 if (parse_driver(opt_drv, &driver_from_opt, NULL)) {
280 /* A driver was specified, report driver-wide options now. */
281 show_drv_detail(driver_from_opt);
284 if (!(devices = device_scan())) {
285 g_critical("No devices found.");
289 num_devices = g_slist_length(devices);
290 if (num_devices > 1) {
291 g_critical("%d devices found. Use --scan to show them, "
292 "and select one to show.", num_devices);
297 g_slist_free(devices);
300 driver = sr_dev_inst_driver_get(sdi);
301 channel_groups = sr_dev_inst_channel_groups_get(sdi);
303 if (sr_dev_open(sdi) != SR_OK) {
304 g_critical("Failed to open device.");
309 * Selected channels and channel group may affect which options are
310 * returned, or which values for them.
312 select_channels(sdi);
313 channel_group = select_channel_group(sdi);
315 if (!(opts = sr_dev_options(driver, sdi, channel_group)))
316 /* Driver supports no device instance options. */
319 if (channel_groups) {
320 printf("Channel groups:\n");
321 for (cgl = channel_groups; cgl; cgl = cgl->next) {
323 printf(" %s: channel%s", cg->name,
324 g_slist_length(cg->channels) > 1 ? "s" : "");
325 for (chl = cg->channels; chl; chl = chl->next) {
327 printf(" %s", ch->name);
333 printf("Supported configuration options");
334 if (channel_groups) {
336 printf(" across all channel groups");
338 printf(" on channel group %s", channel_group->name);
341 for (o = 0; o < opts->len; o++) {
342 key = g_array_index(opts, uint32_t, o);
343 if (!(srci = sr_key_info_get(SR_KEY_CONFIG, key)))
346 if (key == SR_CONF_TRIGGER_MATCH) {
347 if (maybe_config_list(driver, sdi, channel_group, key,
348 &gvar_list) != SR_OK) {
352 int32 = g_variant_get_fixed_array(gvar_list,
353 &num_elements, sizeof(int32_t));
354 printf(" Supported triggers: ");
355 for (i = 0; i < num_elements; i++) {
357 case SR_TRIGGER_ZERO:
363 case SR_TRIGGER_RISING:
366 case SR_TRIGGER_FALLING:
369 case SR_TRIGGER_EDGE:
372 case SR_TRIGGER_OVER:
375 case SR_TRIGGER_UNDER:
386 g_variant_unref(gvar_list);
388 } else if (key == SR_CONF_LIMIT_SAMPLES
389 && (sr_dev_config_capabilities_list(sdi, NULL, key)
392 * If implemented in config_list(), this denotes the
393 * maximum number of samples a device can send. This
394 * really applies only to logic analyzers, and then
395 * only to those that don't support compression, or
396 * have it turned off by default. The values returned
397 * are the low/high limits.
399 if (sr_config_list(driver, sdi, channel_group, key,
401 g_variant_get(gvar, "(tt)", &low, &high);
402 g_variant_unref(gvar);
403 printf(" Maximum number of samples: %"PRIu64"\n", high);
406 } else if (key == SR_CONF_SAMPLERATE) {
407 /* Supported samplerates */
408 printf(" %s", srci->id);
409 if (maybe_config_list(driver, sdi, channel_group, SR_CONF_SAMPLERATE,
410 &gvar_dict) != SR_OK) {
414 if ((gvar_list = g_variant_lookup_value(gvar_dict,
415 "samplerates", G_VARIANT_TYPE("at")))) {
416 uint64 = g_variant_get_fixed_array(gvar_list,
417 &num_elements, sizeof(uint64_t));
418 printf(" - supported samplerates:\n");
419 for (i = 0; i < num_elements; i++) {
420 if (!(s = sr_samplerate_string(uint64[i])))
425 g_variant_unref(gvar_list);
426 } else if ((gvar_list = g_variant_lookup_value(gvar_dict,
427 "samplerate-steps", G_VARIANT_TYPE("at")))) {
428 uint64 = g_variant_get_fixed_array(gvar_list,
429 &num_elements, sizeof(uint64_t));
431 if (!(s = sr_samplerate_string(uint64[0])))
436 if (!(s = sr_samplerate_string(uint64[1])))
441 if (!(s = sr_samplerate_string(uint64[2])))
443 printf(" in steps of %s)\n", s);
445 g_variant_unref(gvar_list);
447 g_variant_unref(gvar_dict);
449 } else if (srci->datatype == SR_T_UINT64) {
450 printf(" %s: ", srci->id);
452 if (maybe_config_get(driver, sdi, channel_group, key,
454 tmp_uint64 = g_variant_get_uint64(gvar);
455 g_variant_unref(gvar);
458 if (maybe_config_list(driver, sdi, channel_group,
459 key, &gvar_list) != SR_OK) {
461 /* Can't list it, but we have a value to show. */
462 printf("%"PRIu64" (current)", tmp_uint64);
467 uint64 = g_variant_get_fixed_array(gvar_list,
468 &num_elements, sizeof(uint64_t));
469 printf(" - supported values:\n");
470 for (i = 0; i < num_elements; i++) {
471 printf(" %"PRIu64, uint64[i]);
472 if (gvar && tmp_uint64 == uint64[i])
473 printf(" (current)");
476 g_variant_unref(gvar_list);
478 } else if (srci->datatype == SR_T_STRING) {
479 printf(" %s: ", srci->id);
480 if (maybe_config_get(driver, sdi, channel_group, key,
482 tmp_str = g_strdup(g_variant_get_string(gvar, NULL));
483 g_variant_unref(gvar);
487 if (maybe_config_list(driver, sdi, channel_group, key,
490 /* Can't list it, but we have a value to show. */
491 printf("%s (current)", tmp_str);
498 stropts = g_variant_get_strv(gvar, &num_elements);
499 for (i = 0; i < num_elements; i++) {
502 printf("%s", stropts[i]);
503 if (tmp_str && !strcmp(tmp_str, stropts[i]))
504 printf(" (current)");
509 g_variant_unref(gvar);
511 } else if (srci->datatype == SR_T_UINT64_RANGE) {
512 printf(" %s: ", srci->id);
513 if (maybe_config_list(driver, sdi, channel_group, key,
514 &gvar_list) != SR_OK) {
519 if (maybe_config_get(driver, sdi, channel_group, key, &gvar) == SR_OK) {
520 g_variant_get(gvar, "(tt)", &cur_low, &cur_high);
521 g_variant_unref(gvar);
527 num_elements = g_variant_n_children(gvar_list);
528 for (i = 0; i < num_elements; i++) {
529 gvar = g_variant_get_child_value(gvar_list, i);
530 g_variant_get(gvar, "(tt)", &low, &high);
531 g_variant_unref(gvar);
534 printf("%"PRIu64"-%"PRIu64, low, high);
535 if (low == cur_low && high == cur_high)
536 printf(" (current)");
539 g_variant_unref(gvar_list);
541 } else if (srci->datatype == SR_T_BOOL) {
542 printf(" %s: ", srci->id);
543 if (maybe_config_get(driver, sdi, channel_group, key,
545 if (g_variant_get_boolean(gvar))
546 printf("on (current), off\n");
548 printf("on, off (current)\n");
549 g_variant_unref(gvar);
553 } else if (srci->datatype == SR_T_DOUBLE_RANGE) {
554 printf(" %s: ", srci->id);
555 if (maybe_config_list(driver, sdi, channel_group, key,
556 &gvar_list) != SR_OK) {
561 if (maybe_config_get(driver, sdi, channel_group, key, &gvar) == SR_OK) {
562 g_variant_get(gvar, "(dd)", &dcur_low, &dcur_high);
563 g_variant_unref(gvar);
569 num_elements = g_variant_n_children(gvar_list);
570 for (i = 0; i < num_elements; i++) {
571 gvar = g_variant_get_child_value(gvar_list, i);
572 g_variant_get(gvar, "(dd)", &dlow, &dhigh);
573 g_variant_unref(gvar);
576 printf("%.1f-%.1f", dlow, dhigh);
577 if (dlow == dcur_low && dhigh == dcur_high)
578 printf(" (current)");
581 g_variant_unref(gvar_list);
583 } else if (srci->datatype == SR_T_FLOAT) {
584 printf(" %s: ", srci->id);
585 if (maybe_config_get(driver, sdi, channel_group, key,
587 printf("%f\n", g_variant_get_double(gvar));
588 g_variant_unref(gvar);
592 } else if (srci->datatype == SR_T_RATIONAL_PERIOD
593 || srci->datatype == SR_T_RATIONAL_VOLT) {
594 printf(" %s", srci->id);
595 if (maybe_config_get(driver, sdi, channel_group, key,
597 g_variant_get(gvar, "(tt)", &cur_p, &cur_q);
598 g_variant_unref(gvar);
602 if (maybe_config_list(driver, sdi, channel_group,
603 key, &gvar_list) != SR_OK) {
607 printf(" - supported values:\n");
608 num_elements = g_variant_n_children(gvar_list);
609 for (i = 0; i < num_elements; i++) {
610 gvar = g_variant_get_child_value(gvar_list, i);
611 g_variant_get(gvar, "(tt)", &p, &q);
612 if (srci->datatype == SR_T_RATIONAL_PERIOD)
613 s = sr_period_string(p * q);
615 s = sr_voltage_string(p, q);
618 if (p == cur_p && q == cur_q)
619 printf(" (current)");
622 g_variant_unref(gvar_list);
624 } else if (srci->datatype == SR_T_MQ) {
625 printf(" %s: ", srci->id);
626 if (maybe_config_get(driver, sdi, channel_group, key,
628 && g_variant_is_of_type(gvar, G_VARIANT_TYPE_TUPLE)
629 && g_variant_n_children(gvar) == 2) {
630 g_variant_get(gvar, "(ut)", &cur_mq, &cur_mqflags);
631 g_variant_unref(gvar);
633 cur_mq = cur_mqflags = 0;
635 if (maybe_config_list(driver, sdi, channel_group,
636 key, &gvar_list) != SR_OK) {
640 printf(" - supported measurements:\n");
641 num_elements = g_variant_n_children(gvar_list);
642 for (i = 0; i < num_elements; i++) {
644 gvar = g_variant_get_child_value(gvar_list, i);
645 g_variant_get(gvar, "(ut)", &mq, &mqflags);
646 if ((srmqi = sr_key_info_get(SR_KEY_MQ, mq)))
647 printf("%s", srmqi->id);
650 for (j = 0, mask = 1; j < 32; j++, mask <<= 1) {
651 if (!(mqflags & mask))
653 if ((srmqfi = sr_key_info_get(SR_KEY_MQFLAGS, mqflags & mask)))
654 printf("/%s", srmqfi->id);
656 printf("/%" PRIu64, mqflags & mask);
658 if (mq == cur_mq && mqflags == cur_mqflags)
659 printf(" (current)");
662 g_variant_unref(gvar_list);
666 /* Everything else */
667 printf(" %s\n", srci->id);
670 g_array_free(opts, TRUE);
677 void show_pd_detail(void)
679 struct srd_decoder *dec;
680 struct srd_decoder_option *o;
681 struct srd_channel *pdch;
682 struct srd_decoder_annotation_row *r;
685 char **pdtokens, **pdtok, *optsep, **ann, **bin, *val, *doc;
687 pdtokens = g_strsplit(opt_pds, ",", -1);
688 for (pdtok = pdtokens; *pdtok; pdtok++) {
690 if ((optsep = strchr(*pdtok, ':')))
692 if (!(dec = srd_decoder_get_by_id(*pdtok))) {
693 g_critical("Protocol decoder %s not found.", *pdtok);
696 printf("ID: %s\nName: %s\nLong name: %s\nDescription: %s\n",
697 dec->id, dec->name, dec->longname, dec->desc);
698 printf("License: %s\n", dec->license);
699 printf("Annotation classes:\n");
700 if (dec->annotations) {
701 for (l = dec->annotations; l; l = l->next) {
703 printf("- %s: %s\n", ann[0], ann[1]);
708 printf("Annotation rows:\n");
709 if (dec->annotation_rows) {
710 for (l = dec->annotation_rows; l; l = l->next) {
712 printf("- %s (%s): ", r->id, r->desc);
713 for (ll = r->ann_classes; ll; ll = ll->next) {
714 idx = GPOINTER_TO_INT(ll->data);
715 ann = g_slist_nth_data(dec->annotations, idx);
716 printf("%s", ann[0]);
725 printf("Binary classes:\n");
727 for (l = dec->binary; l; l = l->next) {
729 printf("- %s: %s\n", bin[0], bin[1]);
734 printf("Required channels:\n");
736 for (l = dec->channels; l; l = l->next) {
738 printf("- %s (%s): %s\n",
739 pdch->id, pdch->name, pdch->desc);
744 printf("Optional channels:\n");
745 if (dec->opt_channels) {
746 for (l = dec->opt_channels; l; l = l->next) {
748 printf("- %s (%s): %s\n",
749 pdch->id, pdch->name, pdch->desc);
754 printf("Options:\n");
756 for (l = dec->options; l; l = l->next) {
758 printf("- %s: %s (", o->id, o->desc);
759 for (ol = o->values; ol; ol = ol->next) {
760 val = g_variant_print(ol->data, FALSE);
764 val = g_variant_print(o->def, FALSE);
765 printf("default %s)\n", val);
771 if ((doc = srd_decoder_doc_get(dec))) {
772 printf("Documentation:\n%s\n",
773 doc[0] == '\n' ? doc + 1 : doc);
778 g_strfreev(pdtokens);
782 void show_input(void)
784 const struct sr_input_module *imod;
785 const struct sr_option **opts;
790 tok = g_strsplit(opt_input_format, ":", 0);
791 if (!tok[0] || !(imod = sr_input_find(tok[0])))
792 g_critical("Input module '%s' not found.", opt_input_format);
794 printf("ID: %s\nName: %s\n", sr_input_id_get(imod),
795 sr_input_name_get(imod));
796 printf("Description: %s\n", sr_input_description_get(imod));
797 if ((opts = sr_input_options_get(imod))) {
798 printf("Options:\n");
799 for (i = 0; opts[i]; i++) {
800 printf(" %s: %s", opts[i]->id, opts[i]->desc);
802 s = g_variant_print(opts[i]->def, FALSE);
803 printf(" (default %s", s);
805 if (opts[i]->values) {
806 printf(", possible values ");
807 for (l = opts[i]->values; l; l = l->next) {
808 s = g_variant_print((GVariant *)l->data, FALSE);
809 printf("%s%s", s, l->next ? ", " : "");
817 sr_input_options_free(opts);
822 void show_output(void)
824 const struct sr_output_module *omod;
825 const struct sr_option **opts;
830 tok = g_strsplit(opt_output_format, ":", 0);
831 if (!tok[0] || !(omod = sr_output_find(tok[0])))
832 g_critical("Output module '%s' not found.", opt_output_format);
834 printf("ID: %s\nName: %s\n", sr_output_id_get(omod),
835 sr_output_name_get(omod));
836 printf("Description: %s\n", sr_output_description_get(omod));
837 if ((opts = sr_output_options_get(omod))) {
838 printf("Options:\n");
839 for (i = 0; opts[i]; i++) {
840 printf(" %s: %s", opts[i]->id, opts[i]->desc);
842 s = g_variant_print(opts[i]->def, FALSE);
843 printf(" (default %s", s);
845 if (opts[i]->values) {
846 printf(", possible values ");
847 for (l = opts[i]->values; l; l = l->next) {
848 s = g_variant_print((GVariant *)l->data, FALSE);
849 printf("%s%s", s, l->next ? ", " : "");
857 sr_output_options_free(opts);
862 void show_transform(void)
864 const struct sr_transform_module *tmod;
865 const struct sr_option **opts;
870 tok = g_strsplit(opt_transform_module, ":", 0);
871 if (!tok[0] || !(tmod = sr_transform_find(tok[0])))
872 g_critical("Transform module '%s' not found.", opt_transform_module);
874 printf("ID: %s\nName: %s\n", sr_transform_id_get(tmod),
875 sr_transform_name_get(tmod));
876 printf("Description: %s\n", sr_transform_description_get(tmod));
877 if ((opts = sr_transform_options_get(tmod))) {
878 printf("Options:\n");
879 for (i = 0; opts[i]; i++) {
880 printf(" %s: %s", opts[i]->id, opts[i]->desc);
882 s = g_variant_print(opts[i]->def, FALSE);
883 printf(" (default %s", s);
885 if (opts[i]->values) {
886 printf(", possible values ");
887 for (l = opts[i]->values; l; l = l->next) {
888 s = g_variant_print((GVariant *)l->data, FALSE);
889 printf("%s%s", s, l->next ? ", " : "");
897 sr_transform_options_free(opts);