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 void show_version(void)
30 struct sr_dev_driver **drivers;
31 struct sr_input_format **inputs;
32 struct sr_output_format **outputs;
35 struct srd_decoder *dec;
39 printf("sigrok-cli %s\n\n", VERSION);
41 printf("Using libsigrok %s (lib version %s).\n",
42 sr_package_version_string_get(), sr_lib_version_string_get());
44 printf("Using libsigrokdecode %s (lib version %s).\n\n",
45 srd_package_version_string_get(), srd_lib_version_string_get());
48 printf("Supported hardware drivers:\n");
49 drivers = sr_driver_list();
50 for (i = 0; drivers[i]; i++) {
51 printf(" %-20s %s\n", drivers[i]->name, drivers[i]->longname);
55 printf("Supported input formats:\n");
56 inputs = sr_input_list();
57 for (i = 0; inputs[i]; i++)
58 printf(" %-20s %s\n", inputs[i]->id, inputs[i]->description);
61 printf("Supported output formats:\n");
62 outputs = sr_output_list();
63 for (i = 0; outputs[i]; i++)
64 printf(" %-20s %s\n", outputs[i]->id, outputs[i]->description);
65 printf(" %-20s %s\n", "sigrok", "Default file output format");
69 if (srd_init(NULL) == SRD_OK) {
70 printf("Supported protocol decoders:\n");
71 srd_decoder_load_all();
72 for (l = srd_decoder_list(); l; l = l->next) {
74 printf(" %-20s %s\n", dec->id, dec->longname);
75 /* Print protocol description upon "-l 3" or higher. */
76 if (opt_loglevel >= SR_LOG_INFO)
77 printf(" %-20s %s\n", "", dec->desc);
85 static void print_dev_line(const struct sr_dev_inst *sdi)
87 struct sr_probe *probe;
92 s = g_string_sized_new(128);
93 g_string_assign(s, sdi->driver->name);
94 if (sr_config_get(sdi->driver, sdi, NULL, SR_CONF_CONN, &gvar) == SR_OK) {
95 g_string_append(s, ":conn=");
96 g_string_append(s, g_variant_get_string(gvar, NULL));
97 g_variant_unref(gvar);
99 g_string_append(s, " - ");
100 if (sdi->vendor && sdi->vendor[0])
101 g_string_append_printf(s, "%s ", sdi->vendor);
102 if (sdi->model && sdi->model[0])
103 g_string_append_printf(s, "%s ", sdi->model);
104 if (sdi->version && sdi->version[0])
105 g_string_append_printf(s, "%s ", sdi->version);
107 if (g_slist_length(sdi->probes) == 1) {
108 probe = sdi->probes->data;
109 g_string_append_printf(s, "with 1 probe: %s", probe->name);
111 g_string_append_printf(s, "with %d probes:", g_slist_length(sdi->probes));
112 for (l = sdi->probes; l; l = l->next) {
114 g_string_append_printf(s, " %s", probe->name);
118 g_string_append_printf(s, "\n");
119 printf("%s", s->str);
120 g_string_free(s, TRUE);
124 void show_dev_list(void)
126 struct sr_dev_inst *sdi;
129 if (!(devices = device_scan()))
132 printf("The following devices were found:\n");
133 for (l = devices; l; l = l->next) {
137 g_slist_free(devices);
141 void show_dev_detail(void)
143 struct sr_dev_inst *sdi;
144 const struct sr_config_info *srci;
145 struct sr_probe *probe;
146 struct sr_probe_group *probe_group, *pg;
147 GSList *devices, *pgl, *prl;
148 GVariant *gvar_opts, *gvar_dict, *gvar_list, *gvar;
149 gsize num_opts, num_elements;
150 const uint64_t *uint64, p, q, low, high;
151 uint64_t cur_low, cur_high;
153 unsigned int num_devices, o, i;
156 const char *charopts, **stropts;
158 if (!(devices = device_scan())) {
159 g_critical("No devices found.");
163 num_devices = g_slist_length(devices);
164 if (num_devices > 1) {
165 g_critical("%d devices found. Use --scan to show them, "
166 "and select one to show.", num_devices);
173 if (sr_dev_open(sdi) != SR_OK) {
174 g_critical("Failed to open device.");
178 if ((sr_config_list(sdi->driver, NULL, NULL, SR_CONF_SCAN_OPTIONS,
179 &gvar_opts) == SR_OK)) {
180 opts = g_variant_get_fixed_array(gvar_opts, &num_elements,
182 printf("Supported driver options:\n");
183 for (i = 0; i < num_elements; i++) {
184 if (!(srci = sr_config_info_get(opts[i])))
186 printf(" %s\n", srci->id);
188 g_variant_unref(gvar_opts);
191 /* Selected probes and probe group may affect which options are
192 * returned, or which values for them. */
194 probe_group = select_probe_group(sdi);
196 if ((sr_config_list(sdi->driver, sdi, probe_group, SR_CONF_DEVICE_OPTIONS,
197 &gvar_opts)) != SR_OK)
198 /* Driver supports no device instance options. */
201 if (sdi->probe_groups) {
202 printf("Probe groups:\n");
203 for (pgl = sdi->probe_groups; pgl; pgl = pgl->next) {
205 printf(" %s: channel%s", pg->name,
206 g_slist_length(pg->probes) > 1 ? "s" : "");
207 for (prl = pg->probes; prl; prl = prl->next) {
209 printf(" %s", probe->name);
215 printf("Supported configuration options");
216 if (sdi->probe_groups) {
218 printf(" across all probe groups");
220 printf(" on probe group %s", probe_group->name);
223 opts = g_variant_get_fixed_array(gvar_opts, &num_opts, sizeof(int32_t));
224 for (o = 0; o < num_opts; o++) {
225 if (!(srci = sr_config_info_get(opts[o])))
228 if (srci->key == SR_CONF_TRIGGER_TYPE) {
229 if (sr_config_list(sdi->driver, sdi, probe_group, srci->key,
234 charopts = g_variant_get_string(gvar, NULL);
235 printf(" Supported triggers: ");
237 printf("%c ", *charopts);
241 g_variant_unref(gvar);
243 } else if (srci->key == SR_CONF_LIMIT_SAMPLES) {
244 /* If implemented in config_list(), this denotes the
245 * maximum number of samples a device can send. This
246 * really applies only to logic analyzers, and then
247 * only to those that don't support compression, or
248 * have it turned off by default. The values returned
249 * are the low/high limits. */
250 if (sr_config_list(sdi->driver, sdi, probe_group, srci->key,
254 g_variant_get(gvar, "(tt)", &low, &high);
255 g_variant_unref(gvar);
256 printf(" Maximum number of samples: %"PRIu64"\n", high);
258 } else if (srci->key == SR_CONF_SAMPLERATE) {
259 /* Supported samplerates */
260 printf(" %s", srci->id);
261 if (sr_config_list(sdi->driver, sdi, probe_group, SR_CONF_SAMPLERATE,
262 &gvar_dict) != SR_OK) {
266 if ((gvar_list = g_variant_lookup_value(gvar_dict,
267 "samplerates", G_VARIANT_TYPE("at")))) {
268 uint64 = g_variant_get_fixed_array(gvar_list,
269 &num_elements, sizeof(uint64_t));
270 printf(" - supported samplerates:\n");
271 for (i = 0; i < num_elements; i++) {
272 if (!(s = sr_samplerate_string(uint64[i])))
277 g_variant_unref(gvar_list);
278 } else if ((gvar_list = g_variant_lookup_value(gvar_dict,
279 "samplerate-steps", G_VARIANT_TYPE("at")))) {
280 uint64 = g_variant_get_fixed_array(gvar_list,
281 &num_elements, sizeof(uint64_t));
283 if (!(s = sr_samplerate_string(uint64[0])))
288 if (!(s = sr_samplerate_string(uint64[1])))
293 if (!(s = sr_samplerate_string(uint64[2])))
295 printf(" in steps of %s)\n", s);
297 g_variant_unref(gvar_list);
299 g_variant_unref(gvar_dict);
301 } else if (srci->key == SR_CONF_BUFFERSIZE) {
302 /* Supported buffer sizes */
303 printf(" %s", srci->id);
304 if (sr_config_list(sdi->driver, sdi, probe_group,
305 SR_CONF_BUFFERSIZE, &gvar_list) != SR_OK) {
309 uint64 = g_variant_get_fixed_array(gvar_list,
310 &num_elements, sizeof(uint64_t));
311 printf(" - supported buffer sizes:\n");
312 for (i = 0; i < num_elements; i++)
313 printf(" %"PRIu64"\n", uint64[i]);
314 g_variant_unref(gvar_list);
316 } else if (srci->key == SR_CONF_TIMEBASE) {
317 /* Supported time bases */
318 printf(" %s", srci->id);
319 if (sr_config_list(sdi->driver, sdi, probe_group,
320 SR_CONF_TIMEBASE, &gvar_list) != SR_OK) {
324 printf(" - supported time bases:\n");
325 num_elements = g_variant_n_children(gvar_list);
326 for (i = 0; i < num_elements; i++) {
327 gvar = g_variant_get_child_value(gvar_list, i);
328 g_variant_get(gvar, "(tt)", &p, &q);
329 s = sr_period_string(p * q);
333 g_variant_unref(gvar_list);
335 } else if (srci->key == SR_CONF_VDIV) {
336 /* Supported volts/div values */
337 printf(" %s", srci->id);
338 if (sr_config_list(sdi->driver, sdi, probe_group,
339 SR_CONF_VDIV, &gvar_list) != SR_OK) {
343 printf(" - supported volts/div:\n");
344 num_elements = g_variant_n_children(gvar_list);
345 for (i = 0; i < num_elements; i++) {
346 gvar = g_variant_get_child_value(gvar_list, i);
347 g_variant_get(gvar, "(tt)", &p, &q);
348 s = sr_voltage_string(p, q);
352 g_variant_unref(gvar_list);
354 } else if (srci->datatype == SR_T_CHAR) {
355 printf(" %s: ", srci->id);
356 if (sr_config_get(sdi->driver, sdi, probe_group, srci->key,
358 tmp_str = g_strdup(g_variant_get_string(gvar, NULL));
359 g_variant_unref(gvar);
363 if (sr_config_list(sdi->driver, sdi, probe_group, srci->key,
369 stropts = g_variant_get_strv(gvar, &num_elements);
370 for (i = 0; i < num_elements; i++) {
373 printf("%s", stropts[i]);
374 if (tmp_str && !strcmp(tmp_str, stropts[i]))
375 printf(" (current)");
380 g_variant_unref(gvar);
382 } else if (srci->datatype == SR_T_UINT64_RANGE) {
383 printf(" %s: ", srci->id);
384 if (sr_config_list(sdi->driver, sdi, probe_group, srci->key,
385 &gvar_list) != SR_OK) {
390 if (sr_config_get(sdi->driver, sdi, NULL, srci->key, &gvar) == SR_OK) {
391 g_variant_get(gvar, "(tt)", &cur_low, &cur_high);
392 g_variant_unref(gvar);
398 num_elements = g_variant_n_children(gvar_list);
399 for (i = 0; i < num_elements; i++) {
400 gvar = g_variant_get_child_value(gvar_list, i);
401 g_variant_get(gvar, "(tt)", &low, &high);
402 g_variant_unref(gvar);
405 printf("%"PRIu64"-%"PRIu64, low, high);
406 if (low == cur_low && high == cur_high)
407 printf(" (current)");
410 g_variant_unref(gvar_list);
412 } else if (srci->datatype == SR_T_BOOL) {
413 printf(" %s: ", srci->id);
414 if (sr_config_get(sdi->driver, sdi, NULL, srci->key,
416 if (g_variant_get_boolean(gvar))
417 printf("on (current), off\n");
419 printf("on, off (current)\n");
420 g_variant_unref(gvar);
426 /* Everything else */
427 printf(" %s\n", srci->id);
430 g_variant_unref(gvar_opts);
433 g_slist_free(devices);
438 void show_pd_detail(void)
441 struct srd_decoder *dec;
442 struct srd_decoder_option *o;
443 char **pdtokens, **pdtok, *optsep, **ann, *val, *doc;
445 struct srd_decoder_annotation_row *r;
447 pdtokens = g_strsplit(opt_pds, ",", -1);
448 for (pdtok = pdtokens; *pdtok; pdtok++) {
450 if ((optsep = strchr(*pdtok, ':')))
452 if (!(dec = srd_decoder_get_by_id(*pdtok))) {
453 g_critical("Protocol decoder %s not found.", *pdtok);
456 printf("ID: %s\nName: %s\nLong name: %s\nDescription: %s\n",
457 dec->id, dec->name, dec->longname, dec->desc);
458 printf("License: %s\n", dec->license);
459 printf("Annotation classes:\n");
460 if (dec->annotations) {
461 for (l = dec->annotations; l; l = l->next) {
463 printf("- %s: %s\n", ann[0], ann[1]);
468 printf("Annotation rows:\n");
469 if (dec->annotation_rows) {
470 for (l = dec->annotation_rows; l; l = l->next) {
472 printf("- %s (%s): ", r->id, r->desc);
473 for (ll = r->ann_classes; ll; ll = ll->next)
474 printf("%d ", GPOINTER_TO_INT(ll->data));
480 printf("Required probes:\n");
482 for (l = dec->probes; l; l = l->next) {
484 printf("- %s (%s): %s\n",
485 p->id, p->name, p->desc);
490 printf("Optional probes:\n");
491 if (dec->opt_probes) {
492 for (l = dec->opt_probes; l; l = l->next) {
494 printf("- %s (%s): %s\n",
495 p->id, p->name, p->desc);
500 printf("Options:\n");
502 for (l = dec->options; l; l = l->next) {
504 val = g_variant_print(o->def, FALSE);
505 printf("- %s: %s (default %s)\n", o->id,
512 if ((doc = srd_decoder_doc_get(dec))) {
513 printf("Documentation:\n%s\n",
514 doc[0] == '\n' ? doc + 1 : doc);
519 g_strfreev(pdtokens);