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"
25 extern gint opt_loglevel;
26 extern gchar *opt_pds;
28 static gint sort_inputs(gconstpointer a, gconstpointer b)
30 const struct sr_input_format *ia = a, *ib = b;
32 return strcmp(ia->id, ib->id);
35 static gint sort_outputs(gconstpointer a, gconstpointer b)
37 const struct sr_output_format *oa = a, *ob = b;
39 return strcmp(oa->id, ob->id);
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);
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);
56 void show_version(void)
58 struct sr_dev_driver **drivers, *driver;
59 struct sr_input_format **inputs, *input;
60 struct sr_output_format **outputs, *output;
65 struct srd_decoder *dec;
68 printf("sigrok-cli %s\n\n", VERSION);
70 printf("Using libsigrok %s (lib version %s).\n",
71 sr_package_version_string_get(), sr_lib_version_string_get());
73 printf("Using libsigrokdecode %s (lib version %s).\n\n",
74 srd_package_version_string_get(), srd_lib_version_string_get());
77 printf("Supported hardware drivers:\n");
78 drivers = sr_driver_list();
79 for (sl = NULL, i = 0; drivers[i]; i++)
80 sl = g_slist_append(sl, drivers[i]);
81 sl = g_slist_sort(sl, sort_drivers);
82 for (l = sl; l; l = l->next) {
84 printf(" %-20s %s\n", driver->name, driver->longname);
89 printf("Supported input formats:\n");
90 inputs = sr_input_list();
91 for (sl = NULL, i = 0; inputs[i]; i++)
92 sl = g_slist_append(sl, inputs[i]);
93 sl = g_slist_sort(sl, sort_inputs);
94 for (l = sl; l; l = l->next) {
96 printf(" %-20s %s\n", input->id, input->description);
101 printf("Supported output formats:\n");
102 outputs = sr_output_list();
103 for (sl = NULL, i = 0; outputs[i]; i++)
104 sl = g_slist_append(sl, outputs[i]);
105 sl = g_slist_sort(sl, sort_outputs);
106 for (l = sl; l; l = l->next) {
108 printf(" %-20s %s\n", output->id, output->description);
114 if (srd_init(NULL) == SRD_OK) {
115 printf("Supported protocol decoders:\n");
116 srd_decoder_load_all();
117 sl = g_slist_copy((GSList *)srd_decoder_list());
118 sl = g_slist_sort(sl, sort_pds);
119 for (l = sl; l; l = l->next) {
121 printf(" %-20s %s\n", dec->id, dec->longname);
122 /* Print protocol description upon "-l 3" or higher. */
123 if (opt_loglevel >= SR_LOG_INFO)
124 printf(" %-20s %s\n", "", dec->desc);
133 static gint sort_probes(gconstpointer a, gconstpointer b)
135 const struct sr_probe *pa = a, *pb = b;
137 return pa->index - pb->index;
140 static void print_dev_line(const struct sr_dev_inst *sdi)
142 struct sr_probe *probe;
147 s = g_string_sized_new(128);
148 g_string_assign(s, sdi->driver->name);
149 if (sr_config_get(sdi->driver, sdi, NULL, SR_CONF_CONN, &gvar) == SR_OK) {
150 g_string_append(s, ":conn=");
151 g_string_append(s, g_variant_get_string(gvar, NULL));
152 g_variant_unref(gvar);
154 g_string_append(s, " - ");
155 if (sdi->vendor && sdi->vendor[0])
156 g_string_append_printf(s, "%s ", sdi->vendor);
157 if (sdi->model && sdi->model[0])
158 g_string_append_printf(s, "%s ", sdi->model);
159 if (sdi->version && sdi->version[0])
160 g_string_append_printf(s, "%s ", sdi->version);
162 if (g_slist_length(sdi->probes) == 1) {
163 probe = sdi->probes->data;
164 g_string_append_printf(s, "with 1 probe: %s", probe->name);
166 sl = g_slist_sort(g_slist_copy(sdi->probes), sort_probes);
167 g_string_append_printf(s, "with %d probes:", g_slist_length(sl));
168 for (l = sl; l; l = l->next) {
170 g_string_append_printf(s, " %s", probe->name);
175 g_string_append_printf(s, "\n");
176 printf("%s", s->str);
177 g_string_free(s, TRUE);
181 void show_dev_list(void)
183 struct sr_dev_inst *sdi;
186 if (!(devices = device_scan()))
189 printf("The following devices were found:\n");
190 for (l = devices; l; l = l->next) {
194 g_slist_free(devices);
198 void show_dev_detail(void)
200 struct sr_dev_inst *sdi;
201 const struct sr_config_info *srci;
202 struct sr_probe *probe;
203 struct sr_probe_group *probe_group, *pg;
204 GSList *devices, *pgl, *prl;
205 GVariant *gvar_opts, *gvar_dict, *gvar_list, *gvar;
206 gsize num_opts, num_elements;
207 const uint64_t *uint64, p, q, low, high;
208 uint64_t cur_low, cur_high;
210 unsigned int num_devices, o, i;
213 const char *charopts, **stropts;
215 if (!(devices = device_scan())) {
216 g_critical("No devices found.");
220 num_devices = g_slist_length(devices);
221 if (num_devices > 1) {
222 g_critical("%d devices found. Use --scan to show them, "
223 "and select one to show.", num_devices);
230 if (sr_dev_open(sdi) != SR_OK) {
231 g_critical("Failed to open device.");
235 if ((sr_config_list(sdi->driver, NULL, NULL, SR_CONF_SCAN_OPTIONS,
236 &gvar_opts) == SR_OK)) {
237 opts = g_variant_get_fixed_array(gvar_opts, &num_elements,
239 printf("Supported driver options:\n");
240 for (i = 0; i < num_elements; i++) {
241 if (!(srci = sr_config_info_get(opts[i])))
243 printf(" %s\n", srci->id);
245 g_variant_unref(gvar_opts);
248 /* Selected probes and probe group may affect which options are
249 * returned, or which values for them. */
251 probe_group = select_probe_group(sdi);
253 if ((sr_config_list(sdi->driver, sdi, probe_group, SR_CONF_DEVICE_OPTIONS,
254 &gvar_opts)) != SR_OK)
255 /* Driver supports no device instance options. */
258 if (sdi->probe_groups) {
259 printf("Probe groups:\n");
260 for (pgl = sdi->probe_groups; pgl; pgl = pgl->next) {
262 printf(" %s: channel%s", pg->name,
263 g_slist_length(pg->probes) > 1 ? "s" : "");
264 for (prl = pg->probes; prl; prl = prl->next) {
266 printf(" %s", probe->name);
272 printf("Supported configuration options");
273 if (sdi->probe_groups) {
275 printf(" across all probe groups");
277 printf(" on probe group %s", probe_group->name);
280 opts = g_variant_get_fixed_array(gvar_opts, &num_opts, sizeof(int32_t));
281 for (o = 0; o < num_opts; o++) {
282 if (!(srci = sr_config_info_get(opts[o])))
285 if (srci->key == SR_CONF_TRIGGER_TYPE) {
286 if (sr_config_list(sdi->driver, sdi, probe_group, srci->key,
291 charopts = g_variant_get_string(gvar, NULL);
292 printf(" Supported triggers: ");
294 printf("%c ", *charopts);
298 g_variant_unref(gvar);
300 } else if (srci->key == SR_CONF_LIMIT_SAMPLES) {
301 /* If implemented in config_list(), this denotes the
302 * maximum number of samples a device can send. This
303 * really applies only to logic analyzers, and then
304 * only to those that don't support compression, or
305 * have it turned off by default. The values returned
306 * are the low/high limits. */
307 if (sr_config_list(sdi->driver, sdi, probe_group, srci->key,
311 g_variant_get(gvar, "(tt)", &low, &high);
312 g_variant_unref(gvar);
313 printf(" Maximum number of samples: %"PRIu64"\n", high);
315 } else if (srci->key == SR_CONF_SAMPLERATE) {
316 /* Supported samplerates */
317 printf(" %s", srci->id);
318 if (sr_config_list(sdi->driver, sdi, probe_group, SR_CONF_SAMPLERATE,
319 &gvar_dict) != SR_OK) {
323 if ((gvar_list = g_variant_lookup_value(gvar_dict,
324 "samplerates", G_VARIANT_TYPE("at")))) {
325 uint64 = g_variant_get_fixed_array(gvar_list,
326 &num_elements, sizeof(uint64_t));
327 printf(" - supported samplerates:\n");
328 for (i = 0; i < num_elements; i++) {
329 if (!(s = sr_samplerate_string(uint64[i])))
334 g_variant_unref(gvar_list);
335 } else if ((gvar_list = g_variant_lookup_value(gvar_dict,
336 "samplerate-steps", G_VARIANT_TYPE("at")))) {
337 uint64 = g_variant_get_fixed_array(gvar_list,
338 &num_elements, sizeof(uint64_t));
340 if (!(s = sr_samplerate_string(uint64[0])))
345 if (!(s = sr_samplerate_string(uint64[1])))
350 if (!(s = sr_samplerate_string(uint64[2])))
352 printf(" in steps of %s)\n", s);
354 g_variant_unref(gvar_list);
356 g_variant_unref(gvar_dict);
358 } else if (srci->key == SR_CONF_BUFFERSIZE) {
359 /* Supported buffer sizes */
360 printf(" %s", srci->id);
361 if (sr_config_list(sdi->driver, sdi, probe_group,
362 SR_CONF_BUFFERSIZE, &gvar_list) != SR_OK) {
366 uint64 = g_variant_get_fixed_array(gvar_list,
367 &num_elements, sizeof(uint64_t));
368 printf(" - supported buffer sizes:\n");
369 for (i = 0; i < num_elements; i++)
370 printf(" %"PRIu64"\n", uint64[i]);
371 g_variant_unref(gvar_list);
373 } else if (srci->key == SR_CONF_TIMEBASE) {
374 /* Supported time bases */
375 printf(" %s", srci->id);
376 if (sr_config_list(sdi->driver, sdi, probe_group,
377 SR_CONF_TIMEBASE, &gvar_list) != SR_OK) {
381 printf(" - supported time bases:\n");
382 num_elements = g_variant_n_children(gvar_list);
383 for (i = 0; i < num_elements; i++) {
384 gvar = g_variant_get_child_value(gvar_list, i);
385 g_variant_get(gvar, "(tt)", &p, &q);
386 s = sr_period_string(p * q);
390 g_variant_unref(gvar_list);
392 } else if (srci->key == SR_CONF_VDIV) {
393 /* Supported volts/div values */
394 printf(" %s", srci->id);
395 if (sr_config_list(sdi->driver, sdi, probe_group,
396 SR_CONF_VDIV, &gvar_list) != SR_OK) {
400 printf(" - supported volts/div:\n");
401 num_elements = g_variant_n_children(gvar_list);
402 for (i = 0; i < num_elements; i++) {
403 gvar = g_variant_get_child_value(gvar_list, i);
404 g_variant_get(gvar, "(tt)", &p, &q);
405 s = sr_voltage_string(p, q);
409 g_variant_unref(gvar_list);
411 } else if (srci->datatype == SR_T_CHAR) {
412 printf(" %s: ", srci->id);
413 if (sr_config_get(sdi->driver, sdi, probe_group, srci->key,
415 tmp_str = g_strdup(g_variant_get_string(gvar, NULL));
416 g_variant_unref(gvar);
420 if (sr_config_list(sdi->driver, sdi, probe_group, srci->key,
426 stropts = g_variant_get_strv(gvar, &num_elements);
427 for (i = 0; i < num_elements; i++) {
430 printf("%s", stropts[i]);
431 if (tmp_str && !strcmp(tmp_str, stropts[i]))
432 printf(" (current)");
437 g_variant_unref(gvar);
439 } else if (srci->datatype == SR_T_UINT64_RANGE) {
440 printf(" %s: ", srci->id);
441 if (sr_config_list(sdi->driver, sdi, probe_group, srci->key,
442 &gvar_list) != SR_OK) {
447 if (sr_config_get(sdi->driver, sdi, NULL, srci->key, &gvar) == SR_OK) {
448 g_variant_get(gvar, "(tt)", &cur_low, &cur_high);
449 g_variant_unref(gvar);
455 num_elements = g_variant_n_children(gvar_list);
456 for (i = 0; i < num_elements; i++) {
457 gvar = g_variant_get_child_value(gvar_list, i);
458 g_variant_get(gvar, "(tt)", &low, &high);
459 g_variant_unref(gvar);
462 printf("%"PRIu64"-%"PRIu64, low, high);
463 if (low == cur_low && high == cur_high)
464 printf(" (current)");
467 g_variant_unref(gvar_list);
469 } else if (srci->datatype == SR_T_BOOL) {
470 printf(" %s: ", srci->id);
471 if (sr_config_get(sdi->driver, sdi, NULL, srci->key,
473 if (g_variant_get_boolean(gvar))
474 printf("on (current), off\n");
476 printf("on, off (current)\n");
477 g_variant_unref(gvar);
483 /* Everything else */
484 printf(" %s\n", srci->id);
487 g_variant_unref(gvar_opts);
490 g_slist_free(devices);
495 void show_pd_detail(void)
498 struct srd_decoder *dec;
499 struct srd_decoder_option *o;
500 char **pdtokens, **pdtok, *optsep, **ann, *val, *doc;
502 struct srd_decoder_annotation_row *r;
504 pdtokens = g_strsplit(opt_pds, ",", -1);
505 for (pdtok = pdtokens; *pdtok; pdtok++) {
507 if ((optsep = strchr(*pdtok, ':')))
509 if (!(dec = srd_decoder_get_by_id(*pdtok))) {
510 g_critical("Protocol decoder %s not found.", *pdtok);
513 printf("ID: %s\nName: %s\nLong name: %s\nDescription: %s\n",
514 dec->id, dec->name, dec->longname, dec->desc);
515 printf("License: %s\n", dec->license);
516 printf("Annotation classes:\n");
517 if (dec->annotations) {
518 for (l = dec->annotations; l; l = l->next) {
520 printf("- %s: %s\n", ann[0], ann[1]);
525 printf("Annotation rows:\n");
526 if (dec->annotation_rows) {
527 for (l = dec->annotation_rows; l; l = l->next) {
529 printf("- %s (%s): ", r->id, r->desc);
530 for (ll = r->ann_classes; ll; ll = ll->next)
531 printf("%d ", GPOINTER_TO_INT(ll->data));
537 printf("Required probes:\n");
539 for (l = dec->probes; l; l = l->next) {
541 printf("- %s (%s): %s\n",
542 p->id, p->name, p->desc);
547 printf("Optional probes:\n");
548 if (dec->opt_probes) {
549 for (l = dec->opt_probes; l; l = l->next) {
551 printf("- %s (%s): %s\n",
552 p->id, p->name, p->desc);
557 printf("Options:\n");
559 for (l = dec->options; l; l = l->next) {
561 val = g_variant_print(o->def, FALSE);
562 printf("- %s: %s (default %s)\n", o->id,
569 if ((doc = srd_decoder_doc_get(dec))) {
570 printf("Documentation:\n%s\n",
571 doc[0] == '\n' ? doc + 1 : doc);
576 g_strfreev(pdtokens);