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 extern gint opt_loglevel;
25 extern gchar *opt_pds;
27 static gint sort_inputs(gconstpointer a, gconstpointer b)
29 const struct sr_input_format *ia = a, *ib = b;
31 return strcmp(ia->id, ib->id);
34 static gint sort_outputs(gconstpointer a, gconstpointer b)
36 const struct sr_output_format *oa = a, *ob = b;
38 return strcmp(oa->id, ob->id);
41 static gint sort_drivers(gconstpointer a, gconstpointer b)
43 const struct sr_dev_driver *sdda = a, *sddb = b;
45 return strcmp(sdda->name, sddb->name);
49 static gint sort_pds(gconstpointer a, gconstpointer b)
51 const struct srd_decoder *sda = a, *sdb = b;
53 return strcmp(sda->id, sdb->id);
57 void show_version(void)
59 struct sr_dev_driver **drivers, *driver;
60 struct sr_input_format **inputs, *input;
61 struct sr_output_format **outputs, *output;
66 struct srd_decoder *dec;
69 printf("sigrok-cli %s\n\n", VERSION);
71 printf("Using libsigrok %s (lib version %s).\n",
72 sr_package_version_string_get(), sr_lib_version_string_get());
74 printf("Using libsigrokdecode %s (lib version %s).\n\n",
75 srd_package_version_string_get(), srd_lib_version_string_get());
78 printf("Supported hardware drivers:\n");
79 drivers = sr_driver_list();
80 for (sl = NULL, i = 0; drivers[i]; i++)
81 sl = g_slist_append(sl, drivers[i]);
82 sl = g_slist_sort(sl, sort_drivers);
83 for (l = sl; l; l = l->next) {
85 printf(" %-20s %s\n", driver->name, driver->longname);
90 printf("Supported input formats:\n");
91 inputs = sr_input_list();
92 for (sl = NULL, i = 0; inputs[i]; i++)
93 sl = g_slist_append(sl, inputs[i]);
94 sl = g_slist_sort(sl, sort_inputs);
95 for (l = sl; l; l = l->next) {
97 printf(" %-20s %s\n", input->id, input->description);
102 printf("Supported output formats:\n");
103 outputs = sr_output_list();
104 for (sl = NULL, i = 0; outputs[i]; i++)
105 sl = g_slist_append(sl, outputs[i]);
106 sl = g_slist_sort(sl, sort_outputs);
107 for (l = sl; l; l = l->next) {
109 printf(" %-20s %s\n", output->id, output->description);
115 if (srd_init(NULL) == SRD_OK) {
116 printf("Supported protocol decoders:\n");
117 srd_decoder_load_all();
118 sl = g_slist_copy((GSList *)srd_decoder_list());
119 sl = g_slist_sort(sl, sort_pds);
120 for (l = sl; l; l = l->next) {
122 printf(" %-20s %s\n", dec->id, dec->longname);
123 /* Print protocol description upon "-l 3" or higher. */
124 if (opt_loglevel >= SR_LOG_INFO)
125 printf(" %-20s %s\n", "", dec->desc);
134 static gint sort_channels(gconstpointer a, gconstpointer b)
136 const struct sr_channel *pa = a, *pb = b;
138 return pa->index - pb->index;
141 static void print_dev_line(const struct sr_dev_inst *sdi)
143 struct sr_channel *ch;
148 s = g_string_sized_new(128);
149 g_string_assign(s, sdi->driver->name);
150 if (sr_config_get(sdi->driver, sdi, NULL, SR_CONF_CONN, &gvar) == SR_OK) {
151 g_string_append(s, ":conn=");
152 g_string_append(s, g_variant_get_string(gvar, NULL));
153 g_variant_unref(gvar);
155 g_string_append(s, " - ");
156 if (sdi->vendor && sdi->vendor[0])
157 g_string_append_printf(s, "%s ", sdi->vendor);
158 if (sdi->model && sdi->model[0])
159 g_string_append_printf(s, "%s ", sdi->model);
160 if (sdi->version && sdi->version[0])
161 g_string_append_printf(s, "%s ", sdi->version);
163 if (g_slist_length(sdi->channels) == 1) {
164 ch = sdi->channels->data;
165 g_string_append_printf(s, "with 1 channel: %s", ch->name);
167 sl = g_slist_sort(g_slist_copy(sdi->channels), sort_channels);
168 g_string_append_printf(s, "with %d channels:", g_slist_length(sl));
169 for (l = sl; l; l = l->next) {
171 g_string_append_printf(s, " %s", ch->name);
176 g_string_append_printf(s, "\n");
177 printf("%s", s->str);
178 g_string_free(s, TRUE);
182 void show_dev_list(void)
184 struct sr_dev_inst *sdi;
187 if (!(devices = device_scan()))
190 printf("The following devices were found:\n");
191 for (l = devices; l; l = l->next) {
195 g_slist_free(devices);
199 void show_dev_detail(void)
201 struct sr_dev_inst *sdi;
202 const struct sr_config_info *srci;
203 struct sr_channel *ch;
204 struct sr_channel_group *channel_group, *cg;
205 GSList *devices, *cgl, *chl;
206 GVariant *gvar_opts, *gvar_dict, *gvar_list, *gvar;
207 gsize num_opts, num_elements;
208 double dlow, dhigh, dcur_low, dcur_high;
209 const uint64_t *uint64, p, q, low, high;
210 uint64_t cur_low, cur_high;
211 const int32_t *int32, *opts;
212 unsigned int num_devices, o, i;
215 const char **stropts;
217 if (!(devices = device_scan())) {
218 g_critical("No devices found.");
222 num_devices = g_slist_length(devices);
223 if (num_devices > 1) {
224 g_critical("%d devices found. Use --scan to show them, "
225 "and select one to show.", num_devices);
232 if (sr_dev_open(sdi) != SR_OK) {
233 g_critical("Failed to open device.");
237 if ((sr_config_list(sdi->driver, NULL, NULL, SR_CONF_SCAN_OPTIONS,
238 &gvar_opts) == SR_OK)) {
239 opts = g_variant_get_fixed_array(gvar_opts, &num_elements,
241 printf("Supported driver options:\n");
242 for (i = 0; i < num_elements; i++) {
243 if (!(srci = sr_config_info_get(opts[i])))
245 printf(" %s\n", srci->id);
247 g_variant_unref(gvar_opts);
250 /* Selected channels and channel group may affect which options are
251 * returned, or which values for them. */
252 select_channels(sdi);
253 channel_group = select_channel_group(sdi);
255 if ((sr_config_list(sdi->driver, sdi, channel_group, SR_CONF_DEVICE_OPTIONS,
256 &gvar_opts)) != SR_OK)
257 /* Driver supports no device instance options. */
260 if (sdi->channel_groups) {
261 printf("Channel groups:\n");
262 for (cgl = sdi->channel_groups; cgl; cgl = cgl->next) {
264 printf(" %s: channel%s", cg->name,
265 g_slist_length(cg->channels) > 1 ? "s" : "");
266 for (chl = cg->channels; chl; chl = chl->next) {
268 printf(" %s", ch->name);
274 printf("Supported configuration options");
275 if (sdi->channel_groups) {
277 printf(" across all channel groups");
279 printf(" on channel group %s", channel_group->name);
282 opts = g_variant_get_fixed_array(gvar_opts, &num_opts, sizeof(int32_t));
283 for (o = 0; o < num_opts; o++) {
284 if (!(srci = sr_config_info_get(opts[o])))
287 if (srci->key == SR_CONF_TRIGGER_MATCH) {
288 if (sr_config_list(sdi->driver, sdi, channel_group, srci->key,
289 &gvar_list) != SR_OK) {
293 int32 = g_variant_get_fixed_array(gvar_list,
294 &num_elements, sizeof(int32_t));
295 printf(" Supported triggers: ");
296 for (i = 0; i < num_elements; i++) {
298 case SR_TRIGGER_ZERO:
304 case SR_TRIGGER_RISING:
307 case SR_TRIGGER_FALLING:
310 case SR_TRIGGER_EDGE:
313 case SR_TRIGGER_OVER:
316 case SR_TRIGGER_UNDER:
323 g_variant_unref(gvar_list);
325 } else if (srci->key == SR_CONF_LIMIT_SAMPLES) {
326 /* If implemented in config_list(), this denotes the
327 * maximum number of samples a device can send. This
328 * really applies only to logic analyzers, and then
329 * only to those that don't support compression, or
330 * have it turned off by default. The values returned
331 * are the low/high limits. */
332 if (sr_config_list(sdi->driver, sdi, channel_group, srci->key,
336 g_variant_get(gvar, "(tt)", &low, &high);
337 g_variant_unref(gvar);
338 printf(" Maximum number of samples: %"PRIu64"\n", high);
340 } else if (srci->key == SR_CONF_SAMPLERATE) {
341 /* Supported samplerates */
342 printf(" %s", srci->id);
343 if (sr_config_list(sdi->driver, sdi, channel_group, SR_CONF_SAMPLERATE,
344 &gvar_dict) != SR_OK) {
348 if ((gvar_list = g_variant_lookup_value(gvar_dict,
349 "samplerates", G_VARIANT_TYPE("at")))) {
350 uint64 = g_variant_get_fixed_array(gvar_list,
351 &num_elements, sizeof(uint64_t));
352 printf(" - supported samplerates:\n");
353 for (i = 0; i < num_elements; i++) {
354 if (!(s = sr_samplerate_string(uint64[i])))
359 g_variant_unref(gvar_list);
360 } else if ((gvar_list = g_variant_lookup_value(gvar_dict,
361 "samplerate-steps", G_VARIANT_TYPE("at")))) {
362 uint64 = g_variant_get_fixed_array(gvar_list,
363 &num_elements, sizeof(uint64_t));
365 if (!(s = sr_samplerate_string(uint64[0])))
370 if (!(s = sr_samplerate_string(uint64[1])))
375 if (!(s = sr_samplerate_string(uint64[2])))
377 printf(" in steps of %s)\n", s);
379 g_variant_unref(gvar_list);
381 g_variant_unref(gvar_dict);
383 } else if (srci->key == SR_CONF_BUFFERSIZE) {
384 /* Supported buffer sizes */
385 printf(" %s", srci->id);
386 if (sr_config_list(sdi->driver, sdi, channel_group,
387 SR_CONF_BUFFERSIZE, &gvar_list) != SR_OK) {
391 uint64 = g_variant_get_fixed_array(gvar_list,
392 &num_elements, sizeof(uint64_t));
393 printf(" - supported buffer sizes:\n");
394 for (i = 0; i < num_elements; i++)
395 printf(" %"PRIu64"\n", uint64[i]);
396 g_variant_unref(gvar_list);
398 } else if (srci->key == SR_CONF_TIMEBASE) {
399 /* Supported time bases */
400 printf(" %s", srci->id);
401 if (sr_config_list(sdi->driver, sdi, channel_group,
402 SR_CONF_TIMEBASE, &gvar_list) != SR_OK) {
406 printf(" - supported time bases:\n");
407 num_elements = g_variant_n_children(gvar_list);
408 for (i = 0; i < num_elements; i++) {
409 gvar = g_variant_get_child_value(gvar_list, i);
410 g_variant_get(gvar, "(tt)", &p, &q);
411 s = sr_period_string(p * q);
415 g_variant_unref(gvar_list);
417 } else if (srci->key == SR_CONF_VDIV) {
418 /* Supported volts/div values */
419 printf(" %s", srci->id);
420 if (sr_config_list(sdi->driver, sdi, channel_group,
421 SR_CONF_VDIV, &gvar_list) != SR_OK) {
425 printf(" - supported volts/div:\n");
426 num_elements = g_variant_n_children(gvar_list);
427 for (i = 0; i < num_elements; i++) {
428 gvar = g_variant_get_child_value(gvar_list, i);
429 g_variant_get(gvar, "(tt)", &p, &q);
430 s = sr_voltage_string(p, q);
434 g_variant_unref(gvar_list);
436 } else if (srci->datatype == SR_T_STRING) {
437 printf(" %s: ", srci->id);
438 if (sr_config_get(sdi->driver, sdi, channel_group, srci->key,
440 tmp_str = g_strdup(g_variant_get_string(gvar, NULL));
441 g_variant_unref(gvar);
445 if (sr_config_list(sdi->driver, sdi, channel_group, srci->key,
451 stropts = g_variant_get_strv(gvar, &num_elements);
452 for (i = 0; i < num_elements; i++) {
455 printf("%s", stropts[i]);
456 if (tmp_str && !strcmp(tmp_str, stropts[i]))
457 printf(" (current)");
462 g_variant_unref(gvar);
464 } else if (srci->datatype == SR_T_UINT64_RANGE) {
465 printf(" %s: ", srci->id);
466 if (sr_config_list(sdi->driver, sdi, channel_group, srci->key,
467 &gvar_list) != SR_OK) {
472 if (sr_config_get(sdi->driver, sdi, NULL, srci->key, &gvar) == SR_OK) {
473 g_variant_get(gvar, "(tt)", &cur_low, &cur_high);
474 g_variant_unref(gvar);
480 num_elements = g_variant_n_children(gvar_list);
481 for (i = 0; i < num_elements; i++) {
482 gvar = g_variant_get_child_value(gvar_list, i);
483 g_variant_get(gvar, "(tt)", &low, &high);
484 g_variant_unref(gvar);
487 printf("%"PRIu64"-%"PRIu64, low, high);
488 if (low == cur_low && high == cur_high)
489 printf(" (current)");
492 g_variant_unref(gvar_list);
494 } else if (srci->datatype == SR_T_BOOL) {
495 printf(" %s: ", srci->id);
496 if (sr_config_get(sdi->driver, sdi, NULL, srci->key,
498 if (g_variant_get_boolean(gvar))
499 printf("on (current), off\n");
501 printf("on, off (current)\n");
502 g_variant_unref(gvar);
506 } else if (srci->datatype == SR_T_DOUBLE_RANGE) {
507 printf(" %s: ", srci->id);
508 if (sr_config_list(sdi->driver, sdi, channel_group, srci->key,
509 &gvar_list) != SR_OK) {
514 if (sr_config_get(sdi->driver, sdi, NULL, srci->key, &gvar) == SR_OK) {
515 g_variant_get(gvar, "(dd)", &dcur_low, &dcur_high);
516 g_variant_unref(gvar);
522 num_elements = g_variant_n_children(gvar_list);
523 for (i = 0; i < num_elements; i++) {
524 gvar = g_variant_get_child_value(gvar_list, i);
525 g_variant_get(gvar, "(dd)", &dlow, &dhigh);
526 g_variant_unref(gvar);
529 printf("%.1f-%.1f", dlow, dhigh);
530 if (dlow == dcur_low && dhigh == dcur_high)
531 printf(" (current)");
534 g_variant_unref(gvar_list);
538 /* Everything else */
539 printf(" %s\n", srci->id);
542 g_variant_unref(gvar_opts);
545 g_slist_free(devices);
550 void show_pd_detail(void)
553 struct srd_decoder *dec;
554 struct srd_decoder_option *o;
555 char **pdtokens, **pdtok, *optsep, **ann, *val, *doc;
556 struct srd_channel *pdch;
557 struct srd_decoder_annotation_row *r;
559 pdtokens = g_strsplit(opt_pds, ",", -1);
560 for (pdtok = pdtokens; *pdtok; pdtok++) {
562 if ((optsep = strchr(*pdtok, ':')))
564 if (!(dec = srd_decoder_get_by_id(*pdtok))) {
565 g_critical("Protocol decoder %s not found.", *pdtok);
568 printf("ID: %s\nName: %s\nLong name: %s\nDescription: %s\n",
569 dec->id, dec->name, dec->longname, dec->desc);
570 printf("License: %s\n", dec->license);
571 printf("Annotation classes:\n");
572 if (dec->annotations) {
573 for (l = dec->annotations; l; l = l->next) {
575 printf("- %s: %s\n", ann[0], ann[1]);
580 printf("Annotation rows:\n");
581 if (dec->annotation_rows) {
582 for (l = dec->annotation_rows; l; l = l->next) {
584 printf("- %s (%s): ", r->id, r->desc);
585 for (ll = r->ann_classes; ll; ll = ll->next)
586 printf("%d ", GPOINTER_TO_INT(ll->data));
592 printf("Required channels:\n");
594 for (l = dec->channels; l; l = l->next) {
596 printf("- %s (%s): %s\n",
597 pdch->id, pdch->name, pdch->desc);
602 printf("Optional channels:\n");
603 if (dec->opt_channels) {
604 for (l = dec->opt_channels; l; l = l->next) {
606 printf("- %s (%s): %s\n",
607 pdch->id, pdch->name, pdch->desc);
612 printf("Options:\n");
614 for (l = dec->options; l; l = l->next) {
616 printf("- %s: %s (", o->id, o->desc);
617 for (ol = o->values; ol; ol = ol->next) {
618 val = g_variant_print(ol->data, FALSE);
622 val = g_variant_print(o->def, FALSE);
623 printf("default %s)\n", val);
629 if ((doc = srd_decoder_doc_get(dec))) {
630 printf("Documentation:\n%s\n",
631 doc[0] == '\n' ? doc + 1 : doc);
636 g_strfreev(pdtokens);