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 gboolean config_key_has_cap(const struct sr_dev_inst *sdi,
139 struct sr_channel_group *cg, uint32_t key, uint32_t capability)
141 struct sr_dev_driver *driver;
143 const uint32_t *opts;
146 driver = sr_dev_inst_driver_get(sdi);
147 if (sr_config_list(driver, sdi, cg, SR_CONF_DEVICE_OPTIONS,
148 &gvar_opts) != SR_OK)
151 opts = g_variant_get_fixed_array(gvar_opts, &num_opts, sizeof(uint32_t));
152 for (i = 0; i < num_opts; i++) {
153 if ((opts[i] & SR_CONF_MASK) == key) {
154 if (opts[i] & capability)
165 static void print_dev_line(const struct sr_dev_inst *sdi)
167 struct sr_channel *ch;
168 GSList *sl, *l, *channels;
171 struct sr_dev_driver *driver;
172 const char *vendor, *model, *version;
174 driver = sr_dev_inst_driver_get(sdi);
175 vendor = sr_dev_inst_vendor_get(sdi);
176 model = sr_dev_inst_model_get(sdi);
177 version = sr_dev_inst_version_get(sdi);
178 channels = sr_dev_inst_channels_get(sdi);
180 s = g_string_sized_new(128);
181 g_string_assign(s, driver->name);
182 if (config_key_has_cap(sdi, NULL, SR_CONF_CONN, SR_CONF_GET)
183 && sr_config_get(driver, sdi, NULL, SR_CONF_CONN, &gvar) == SR_OK) {
184 g_string_append(s, ":conn=");
185 g_string_append(s, g_variant_get_string(gvar, NULL));
186 g_variant_unref(gvar);
188 g_string_append(s, " - ");
189 if (vendor && vendor[0])
190 g_string_append_printf(s, "%s ", vendor);
191 if (model && model[0])
192 g_string_append_printf(s, "%s ", model);
193 if (version && version[0])
194 g_string_append_printf(s, "%s ", version);
196 if (g_slist_length(channels) == 1) {
198 g_string_append_printf(s, "with 1 channel: %s", ch->name);
200 sl = g_slist_sort(g_slist_copy(channels), sort_channels);
201 g_string_append_printf(s, "with %d channels:", g_slist_length(sl));
202 for (l = sl; l; l = l->next) {
204 g_string_append_printf(s, " %s", ch->name);
209 g_string_append_printf(s, "\n");
210 printf("%s", s->str);
211 g_string_free(s, TRUE);
215 void show_dev_list(void)
217 struct sr_dev_inst *sdi;
220 if (!(devices = device_scan()))
223 printf("The following devices were found:\n");
224 for (l = devices; l; l = l->next) {
228 g_slist_free(devices);
232 void show_drv_detail(struct sr_dev_driver *driver)
234 const struct sr_config_info *srci;
236 const uint32_t *opts;
237 gsize num_elements, i;
239 if (sr_config_list(driver, NULL, NULL, SR_CONF_DEVICE_OPTIONS,
240 &gvar_opts) == SR_OK) {
241 opts = g_variant_get_fixed_array(gvar_opts, &num_elements,
244 printf("Driver functions:\n");
245 for (i = 0; i < num_elements; i++) {
246 if (!(srci = sr_config_info_get(opts[i] & SR_CONF_MASK)))
248 printf(" %s\n", srci->name);
251 g_variant_unref(gvar_opts);
254 if (sr_config_list(driver, NULL, NULL, SR_CONF_SCAN_OPTIONS,
255 &gvar_opts) == SR_OK) {
256 opts = g_variant_get_fixed_array(gvar_opts, &num_elements,
259 printf("Scan options:\n");
260 for (i = 0; i < num_elements; i++) {
261 if (!(srci = sr_config_info_get(opts[i] & SR_CONF_MASK)))
263 printf(" %s\n", srci->id);
266 g_variant_unref(gvar_opts);
270 void show_dev_detail(void)
272 struct sr_dev_driver *driver_from_opt, *driver;
273 struct sr_dev_inst *sdi;
274 const struct sr_config_info *srci;
275 struct sr_channel *ch;
276 struct sr_channel_group *channel_group, *cg;
277 GSList *devices, *cgl, *chl, *channel_groups;
278 GVariant *gvar_opts, *gvar_dict, *gvar_list, *gvar;
279 gsize num_opts, num_elements;
280 double dlow, dhigh, dcur_low, dcur_high;
281 const uint64_t *uint64, p, q, low, high;
282 uint64_t tmp_uint64, cur_low, cur_high, cur_p, cur_q;
283 const uint32_t *opts;
284 const int32_t *int32;
286 unsigned int num_devices, i;
287 char *tmp_str, *s, c;
288 const char **stropts;
290 if (parse_driver(opt_drv, &driver_from_opt, NULL)) {
291 /* A driver was specified, report driver-wide options now. */
292 show_drv_detail(driver_from_opt);
295 if (!(devices = device_scan())) {
296 g_critical("No devices found.");
300 num_devices = g_slist_length(devices);
301 if (num_devices > 1) {
302 g_critical("%d devices found. Use --scan to show them, "
303 "and select one to show.", num_devices);
308 g_slist_free(devices);
311 driver = sr_dev_inst_driver_get(sdi);
312 channel_groups = sr_dev_inst_channel_groups_get(sdi);
314 if (sr_dev_open(sdi) != SR_OK) {
315 g_critical("Failed to open device.");
319 /* Selected channels and channel group may affect which options are
320 * returned, or which values for them. */
321 select_channels(sdi);
322 channel_group = select_channel_group(sdi);
324 if (sr_config_list(driver, sdi, channel_group, SR_CONF_DEVICE_OPTIONS,
325 &gvar_opts) != SR_OK)
326 /* Driver supports no device instance options. */
329 if (channel_groups) {
330 printf("Channel groups:\n");
331 for (cgl = channel_groups; cgl; cgl = cgl->next) {
333 printf(" %s: channel%s", cg->name,
334 g_slist_length(cg->channels) > 1 ? "s" : "");
335 for (chl = cg->channels; chl; chl = chl->next) {
337 printf(" %s", ch->name);
343 printf("Supported configuration options");
344 if (channel_groups) {
346 printf(" across all channel groups");
348 printf(" on channel group %s", channel_group->name);
351 opts = g_variant_get_fixed_array(gvar_opts, &num_opts, sizeof(uint32_t));
352 for (o = 0; o < num_opts; o++) {
353 key = opts[o] & SR_CONF_MASK;
354 if (!(srci = sr_config_info_get(key)))
357 if (key == SR_CONF_TRIGGER_MATCH) {
358 if (sr_config_list(driver, sdi, channel_group, key,
359 &gvar_list) != SR_OK) {
363 int32 = g_variant_get_fixed_array(gvar_list,
364 &num_elements, sizeof(int32_t));
365 printf(" Supported triggers: ");
366 for (i = 0; i < num_elements; i++) {
368 case SR_TRIGGER_ZERO:
374 case SR_TRIGGER_RISING:
377 case SR_TRIGGER_FALLING:
380 case SR_TRIGGER_EDGE:
383 case SR_TRIGGER_OVER:
386 case SR_TRIGGER_UNDER:
397 g_variant_unref(gvar_list);
399 } else if (key == SR_CONF_LIMIT_SAMPLES) {
400 /* If implemented in config_list(), this denotes the
401 * maximum number of samples a device can send. This
402 * really applies only to logic analyzers, and then
403 * only to those that don't support compression, or
404 * have it turned off by default. The values returned
405 * are the low/high limits. */
406 if (sr_config_list(driver, sdi, channel_group, key,
410 g_variant_get(gvar, "(tt)", &low, &high);
411 g_variant_unref(gvar);
412 printf(" Maximum number of samples: %"PRIu64"\n", high);
414 } else if (key == SR_CONF_SAMPLERATE) {
415 /* Supported samplerates */
416 printf(" %s", srci->id);
417 if (sr_config_list(driver, sdi, channel_group, SR_CONF_SAMPLERATE,
418 &gvar_dict) != SR_OK) {
422 if ((gvar_list = g_variant_lookup_value(gvar_dict,
423 "samplerates", G_VARIANT_TYPE("at")))) {
424 uint64 = g_variant_get_fixed_array(gvar_list,
425 &num_elements, sizeof(uint64_t));
426 printf(" - supported samplerates:\n");
427 for (i = 0; i < num_elements; i++) {
428 if (!(s = sr_samplerate_string(uint64[i])))
433 g_variant_unref(gvar_list);
434 } else if ((gvar_list = g_variant_lookup_value(gvar_dict,
435 "samplerate-steps", G_VARIANT_TYPE("at")))) {
436 uint64 = g_variant_get_fixed_array(gvar_list,
437 &num_elements, sizeof(uint64_t));
439 if (!(s = sr_samplerate_string(uint64[0])))
444 if (!(s = sr_samplerate_string(uint64[1])))
449 if (!(s = sr_samplerate_string(uint64[2])))
451 printf(" in steps of %s)\n", s);
453 g_variant_unref(gvar_list);
455 g_variant_unref(gvar_dict);
457 } else if (srci->datatype == SR_T_UINT64) {
458 printf(" %s", srci->id);
460 if (sr_config_get(driver, sdi, channel_group, key,
462 tmp_uint64 = g_variant_get_uint64(gvar);
463 g_variant_unref(gvar);
466 if (sr_config_list(driver, sdi, channel_group,
467 SR_CONF_BUFFERSIZE, &gvar_list) != SR_OK) {
469 /* Can't list it, but we have a value to show. */
470 printf("%"PRIu64" (current)", tmp_uint64);
475 uint64 = g_variant_get_fixed_array(gvar_list,
476 &num_elements, sizeof(uint64_t));
477 printf(" - supported values:\n");
478 for (i = 0; i < num_elements; i++) {
479 printf(" %"PRIu64, uint64[i]);
480 if (gvar && tmp_uint64 == uint64[i])
481 printf(" (current)");
484 g_variant_unref(gvar_list);
486 } else if (srci->datatype == SR_T_STRING) {
487 printf(" %s: ", srci->id);
488 if (sr_config_get(driver, sdi, channel_group, key,
490 tmp_str = g_strdup(g_variant_get_string(gvar, NULL));
491 g_variant_unref(gvar);
495 if (sr_config_list(driver, sdi, channel_group, key,
498 /* Can't list it, but we have a value to show. */
499 printf("%s (current)", tmp_str);
506 stropts = g_variant_get_strv(gvar, &num_elements);
507 for (i = 0; i < num_elements; i++) {
510 printf("%s", stropts[i]);
511 if (tmp_str && !strcmp(tmp_str, stropts[i]))
512 printf(" (current)");
517 g_variant_unref(gvar);
519 } else if (srci->datatype == SR_T_UINT64_RANGE) {
520 printf(" %s: ", srci->id);
521 if (sr_config_list(driver, sdi, channel_group, key,
522 &gvar_list) != SR_OK) {
527 if (sr_config_get(driver, sdi, channel_group, key, &gvar) == SR_OK) {
528 g_variant_get(gvar, "(tt)", &cur_low, &cur_high);
529 g_variant_unref(gvar);
535 num_elements = g_variant_n_children(gvar_list);
536 for (i = 0; i < num_elements; i++) {
537 gvar = g_variant_get_child_value(gvar_list, i);
538 g_variant_get(gvar, "(tt)", &low, &high);
539 g_variant_unref(gvar);
542 printf("%"PRIu64"-%"PRIu64, low, high);
543 if (low == cur_low && high == cur_high)
544 printf(" (current)");
547 g_variant_unref(gvar_list);
549 } else if (srci->datatype == SR_T_BOOL) {
550 printf(" %s: ", srci->id);
551 if (sr_config_get(driver, sdi, channel_group, key,
553 if (g_variant_get_boolean(gvar))
554 printf("on (current), off\n");
556 printf("on, off (current)\n");
557 g_variant_unref(gvar);
561 } else if (srci->datatype == SR_T_DOUBLE_RANGE) {
562 printf(" %s: ", srci->id);
563 if (sr_config_list(driver, sdi, channel_group, key,
564 &gvar_list) != SR_OK) {
569 if (sr_config_get(driver, sdi, channel_group, key, &gvar) == SR_OK) {
570 g_variant_get(gvar, "(dd)", &dcur_low, &dcur_high);
571 g_variant_unref(gvar);
577 num_elements = g_variant_n_children(gvar_list);
578 for (i = 0; i < num_elements; i++) {
579 gvar = g_variant_get_child_value(gvar_list, i);
580 g_variant_get(gvar, "(dd)", &dlow, &dhigh);
581 g_variant_unref(gvar);
584 printf("%.1f-%.1f", dlow, dhigh);
585 if (dlow == dcur_low && dhigh == dcur_high)
586 printf(" (current)");
589 g_variant_unref(gvar_list);
591 } else if (srci->datatype == SR_T_FLOAT) {
592 printf(" %s: ", srci->id);
593 if (sr_config_get(driver, sdi, channel_group, key,
595 printf("%f\n", g_variant_get_double(gvar));
596 g_variant_unref(gvar);
600 } else if (srci->datatype == SR_T_RATIONAL_PERIOD
601 || srci->datatype == SR_T_RATIONAL_VOLT) {
602 printf(" %s", srci->id);
603 if (sr_config_get(driver, sdi, channel_group, key,
605 g_variant_get(gvar, "(tt)", &cur_p, &cur_q);
606 g_variant_unref(gvar);
610 if (sr_config_list(driver, sdi, channel_group,
611 key, &gvar_list) != SR_OK) {
615 printf(" - supported values:\n");
616 num_elements = g_variant_n_children(gvar_list);
617 for (i = 0; i < num_elements; i++) {
618 gvar = g_variant_get_child_value(gvar_list, i);
619 g_variant_get(gvar, "(tt)", &p, &q);
620 if (srci->datatype == SR_T_RATIONAL_PERIOD)
621 s = sr_period_string(p * q);
623 s = sr_voltage_string(p, q);
626 if (p == cur_p && q == cur_q)
627 printf(" (current)");
630 g_variant_unref(gvar_list);
634 /* Everything else */
635 printf(" %s\n", srci->id);
638 g_variant_unref(gvar_opts);
645 void show_pd_detail(void)
647 struct srd_decoder *dec;
648 struct srd_decoder_option *o;
649 struct srd_channel *pdch;
650 struct srd_decoder_annotation_row *r;
653 char **pdtokens, **pdtok, *optsep, **ann, *val, *doc;
655 pdtokens = g_strsplit(opt_pds, ",", -1);
656 for (pdtok = pdtokens; *pdtok; pdtok++) {
658 if ((optsep = strchr(*pdtok, ':')))
660 if (!(dec = srd_decoder_get_by_id(*pdtok))) {
661 g_critical("Protocol decoder %s not found.", *pdtok);
664 printf("ID: %s\nName: %s\nLong name: %s\nDescription: %s\n",
665 dec->id, dec->name, dec->longname, dec->desc);
666 printf("License: %s\n", dec->license);
667 printf("Annotation classes:\n");
668 if (dec->annotations) {
669 for (l = dec->annotations; l; l = l->next) {
671 printf("- %s: %s\n", ann[0], ann[1]);
676 printf("Annotation rows:\n");
677 if (dec->annotation_rows) {
678 for (l = dec->annotation_rows; l; l = l->next) {
680 printf("- %s (%s): ", r->id, r->desc);
681 for (ll = r->ann_classes; ll; ll = ll->next) {
682 idx = GPOINTER_TO_INT(ll->data);
683 ann = g_slist_nth_data(dec->annotations, idx);
684 printf("%s", ann[0]);
693 printf("Required channels:\n");
695 for (l = dec->channels; l; l = l->next) {
697 printf("- %s (%s): %s\n",
698 pdch->id, pdch->name, pdch->desc);
703 printf("Optional channels:\n");
704 if (dec->opt_channels) {
705 for (l = dec->opt_channels; l; l = l->next) {
707 printf("- %s (%s): %s\n",
708 pdch->id, pdch->name, pdch->desc);
713 printf("Options:\n");
715 for (l = dec->options; l; l = l->next) {
717 printf("- %s: %s (", o->id, o->desc);
718 for (ol = o->values; ol; ol = ol->next) {
719 val = g_variant_print(ol->data, FALSE);
723 val = g_variant_print(o->def, FALSE);
724 printf("default %s)\n", val);
730 if ((doc = srd_decoder_doc_get(dec))) {
731 printf("Documentation:\n%s\n",
732 doc[0] == '\n' ? doc + 1 : doc);
737 g_strfreev(pdtokens);
741 void show_input(void)
743 const struct sr_input_module *imod;
744 const struct sr_option **opts;
749 tok = g_strsplit(opt_input_format, ":", 0);
750 if (!tok[0] || !(imod = sr_input_find(tok[0])))
751 g_critical("Input module '%s' not found.", opt_input_format);
753 printf("ID: %s\nName: %s\n", sr_input_id_get(imod),
754 sr_input_name_get(imod));
755 printf("Description: %s\n", sr_input_description_get(imod));
756 if ((opts = sr_input_options_get(imod))) {
757 printf("Options:\n");
758 for (i = 0; opts[i]; i++) {
759 printf(" %s: %s", opts[i]->id, opts[i]->desc);
761 s = g_variant_print(opts[i]->def, FALSE);
762 printf(" (default %s", s);
764 if (opts[i]->values) {
765 printf(", possible values ");
766 for (l = opts[i]->values; l; l = l->next) {
767 s = g_variant_print((GVariant *)l->data, FALSE);
768 printf("%s%s", s, l->next ? ", " : "");
776 sr_input_options_free(opts);
781 void show_output(void)
783 const struct sr_output_module *omod;
784 const struct sr_option **opts;
789 tok = g_strsplit(opt_output_format, ":", 0);
790 if (!tok[0] || !(omod = sr_output_find(tok[0])))
791 g_critical("Output module '%s' not found.", opt_output_format);
793 printf("ID: %s\nName: %s\n", sr_output_id_get(omod),
794 sr_output_name_get(omod));
795 printf("Description: %s\n", sr_output_description_get(omod));
796 if ((opts = sr_output_options_get(omod))) {
797 printf("Options:\n");
798 for (i = 0; opts[i]; i++) {
799 printf(" %s: %s", opts[i]->id, opts[i]->desc);
801 s = g_variant_print(opts[i]->def, FALSE);
802 printf(" (default %s", s);
804 if (opts[i]->values) {
805 printf(", possible values ");
806 for (l = opts[i]->values; l; l = l->next) {
807 s = g_variant_print((GVariant *)l->data, FALSE);
808 printf("%s%s", s, l->next ? ", " : "");
816 sr_output_options_free(opts);