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/>.
22 #include <libsigrok/libsigrok.h>
24 #include <libsigrokdecode/libsigrokdecode.h>
26 #include "sigrok-cli.h"
28 extern gint opt_loglevel;
29 extern gchar *opt_pds;
31 void show_version(void)
33 struct sr_dev_driver **drivers;
34 struct sr_input_format **inputs;
35 struct sr_output_format **outputs;
38 struct srd_decoder *dec;
42 printf("sigrok-cli %s\n\n", VERSION);
44 printf("Using libsigrok %s (lib version %s).\n",
45 sr_package_version_string_get(), sr_lib_version_string_get());
47 printf("Using libsigrokdecode %s (lib version %s).\n\n",
48 srd_package_version_string_get(), srd_lib_version_string_get());
51 printf("Supported hardware drivers:\n");
52 drivers = sr_driver_list();
53 for (i = 0; drivers[i]; i++) {
54 printf(" %-20s %s\n", drivers[i]->name, drivers[i]->longname);
58 printf("Supported input formats:\n");
59 inputs = sr_input_list();
60 for (i = 0; inputs[i]; i++)
61 printf(" %-20s %s\n", inputs[i]->id, inputs[i]->description);
64 printf("Supported output formats:\n");
65 outputs = sr_output_list();
66 for (i = 0; outputs[i]; i++)
67 printf(" %-20s %s\n", outputs[i]->id, outputs[i]->description);
68 printf(" %-20s %s\n", "sigrok", "Default file output format");
72 if (srd_init(NULL) == SRD_OK) {
73 printf("Supported protocol decoders:\n");
74 srd_decoder_load_all();
75 for (l = srd_decoder_list(); l; l = l->next) {
77 printf(" %-20s %s\n", dec->id, dec->longname);
78 /* Print protocol description upon "-l 3" or higher. */
79 if (opt_loglevel >= SR_LOG_INFO)
80 printf(" %-20s %s\n", "", dec->desc);
88 static void print_dev_line(const struct sr_dev_inst *sdi)
90 struct sr_probe *probe;
95 s = g_string_sized_new(128);
96 g_string_assign(s, sdi->driver->name);
97 if (sr_config_get(sdi->driver, sdi, NULL, SR_CONF_CONN, &gvar) == SR_OK) {
98 g_string_append(s, ":conn=");
99 g_string_append(s, g_variant_get_string(gvar, NULL));
100 g_variant_unref(gvar);
102 g_string_append(s, " - ");
103 if (sdi->vendor && sdi->vendor[0])
104 g_string_append_printf(s, "%s ", sdi->vendor);
105 if (sdi->model && sdi->model[0])
106 g_string_append_printf(s, "%s ", sdi->model);
107 if (sdi->version && sdi->version[0])
108 g_string_append_printf(s, "%s ", sdi->version);
110 if (g_slist_length(sdi->probes) == 1) {
111 probe = sdi->probes->data;
112 g_string_append_printf(s, "with 1 probe: %s", probe->name);
114 g_string_append_printf(s, "with %d probes:", g_slist_length(sdi->probes));
115 for (l = sdi->probes; l; l = l->next) {
117 g_string_append_printf(s, " %s", probe->name);
121 g_string_append_printf(s, "\n");
122 printf("%s", s->str);
123 g_string_free(s, TRUE);
127 void show_dev_list(void)
129 struct sr_dev_inst *sdi;
132 if (!(devices = device_scan()))
135 printf("The following devices were found:\n");
136 for (l = devices; l; l = l->next) {
140 g_slist_free(devices);
144 void show_dev_detail(void)
146 struct sr_dev_inst *sdi;
147 const struct sr_config_info *srci;
148 struct sr_probe *probe;
149 struct sr_probe_group *probe_group, *pg;
150 GSList *devices, *pgl, *prl;
151 GVariant *gvar_opts, *gvar_dict, *gvar_list, *gvar;
152 gsize num_opts, num_elements;
153 const uint64_t *uint64, p, q, low, high;
154 uint64_t cur_low, cur_high;
156 unsigned int num_devices, o, i;
159 const char *charopts, **stropts;
161 if (!(devices = device_scan())) {
162 g_critical("No devices found.");
166 num_devices = g_slist_length(devices);
167 if (num_devices > 1) {
168 g_critical("%d devices found. Use --scan to show them, "
169 "and select one to show.", num_devices);
176 if (sr_dev_open(sdi) != SR_OK) {
177 g_critical("Failed to open device.");
181 if ((sr_config_list(sdi->driver, NULL, NULL, SR_CONF_SCAN_OPTIONS,
182 &gvar_opts) == SR_OK)) {
183 opts = g_variant_get_fixed_array(gvar_opts, &num_elements,
185 printf("Supported driver options:\n");
186 for (i = 0; i < num_elements; i++) {
187 if (!(srci = sr_config_info_get(opts[i])))
189 printf(" %s\n", srci->id);
191 g_variant_unref(gvar_opts);
194 probe_group = select_probe_group(sdi);
195 if ((sr_config_list(sdi->driver, sdi, probe_group, SR_CONF_DEVICE_OPTIONS,
196 &gvar_opts)) != SR_OK)
197 /* Driver supports no device instance options. */
200 if (sdi->probe_groups) {
201 printf("Probe groups:\n");
202 for (pgl = sdi->probe_groups; pgl; pgl = pgl->next) {
204 printf(" %s: channel%s", pg->name,
205 g_slist_length(pg->probes) > 1 ? "s" : "");
206 for (prl = pg->probes; prl; prl = prl->next) {
208 printf(" %s", probe->name);
214 printf("Supported configuration options");
215 if (sdi->probe_groups) {
217 printf(" across all probe groups");
219 printf(" on probe group %s", probe_group->name);
222 opts = g_variant_get_fixed_array(gvar_opts, &num_opts, sizeof(int32_t));
223 for (o = 0; o < num_opts; o++) {
224 if (!(srci = sr_config_info_get(opts[o])))
227 if (srci->key == SR_CONF_TRIGGER_TYPE) {
228 if (sr_config_list(sdi->driver, sdi, probe_group, srci->key,
233 charopts = g_variant_get_string(gvar, NULL);
234 printf(" Supported triggers: ");
236 printf("%c ", *charopts);
240 g_variant_unref(gvar);
242 } else if (srci->key == SR_CONF_PATTERN_MODE) {
243 /* Pattern generator modes */
244 printf(" %s", srci->id);
245 if (sr_config_list(sdi->driver, sdi, probe_group, srci->key,
247 printf(" - supported patterns:\n");
248 stropts = g_variant_get_strv(gvar, &num_elements);
249 for (i = 0; i < num_elements; i++)
250 printf(" %s\n", stropts[i]);
251 g_variant_unref(gvar);
256 } else if (srci->key == SR_CONF_SAMPLERATE) {
257 /* Supported samplerates */
258 printf(" %s", srci->id);
259 if (sr_config_list(sdi->driver, sdi, probe_group, SR_CONF_SAMPLERATE,
260 &gvar_dict) != SR_OK) {
264 if ((gvar_list = g_variant_lookup_value(gvar_dict,
265 "samplerates", G_VARIANT_TYPE("at")))) {
266 uint64 = g_variant_get_fixed_array(gvar_list,
267 &num_elements, sizeof(uint64_t));
268 printf(" - supported samplerates:\n");
269 for (i = 0; i < num_elements; i++) {
270 if (!(s = sr_samplerate_string(uint64[i])))
275 g_variant_unref(gvar_list);
276 } else if ((gvar_list = g_variant_lookup_value(gvar_dict,
277 "samplerate-steps", G_VARIANT_TYPE("at")))) {
278 uint64 = g_variant_get_fixed_array(gvar_list,
279 &num_elements, sizeof(uint64_t));
281 if (!(s = sr_samplerate_string(uint64[0])))
286 if (!(s = sr_samplerate_string(uint64[1])))
291 if (!(s = sr_samplerate_string(uint64[2])))
293 printf(" in steps of %s)\n", s);
295 g_variant_unref(gvar_list);
297 g_variant_unref(gvar_dict);
299 } else if (srci->key == SR_CONF_BUFFERSIZE) {
300 /* Supported buffer sizes */
301 printf(" %s", srci->id);
302 if (sr_config_list(sdi->driver, sdi, probe_group,
303 SR_CONF_BUFFERSIZE, &gvar_list) != SR_OK) {
307 uint64 = g_variant_get_fixed_array(gvar_list,
308 &num_elements, sizeof(uint64_t));
309 printf(" - supported buffer sizes:\n");
310 for (i = 0; i < num_elements; i++)
311 printf(" %"PRIu64"\n", uint64[i]);
312 g_variant_unref(gvar_list);
314 } else if (srci->key == SR_CONF_TIMEBASE) {
315 /* Supported time bases */
316 printf(" %s", srci->id);
317 if (sr_config_list(sdi->driver, sdi, probe_group,
318 SR_CONF_TIMEBASE, &gvar_list) != SR_OK) {
322 printf(" - supported time bases:\n");
323 num_elements = g_variant_n_children(gvar_list);
324 for (i = 0; i < num_elements; i++) {
325 gvar = g_variant_get_child_value(gvar_list, i);
326 g_variant_get(gvar, "(tt)", &p, &q);
327 s = sr_period_string(p * q);
331 g_variant_unref(gvar_list);
333 } else if (srci->key == SR_CONF_VDIV) {
334 /* Supported volts/div values */
335 printf(" %s", srci->id);
336 if (sr_config_list(sdi->driver, sdi, probe_group,
337 SR_CONF_VDIV, &gvar_list) != SR_OK) {
341 printf(" - supported volts/div:\n");
342 num_elements = g_variant_n_children(gvar_list);
343 for (i = 0; i < num_elements; i++) {
344 gvar = g_variant_get_child_value(gvar_list, i);
345 g_variant_get(gvar, "(tt)", &p, &q);
346 s = sr_voltage_string(p, q);
350 g_variant_unref(gvar_list);
352 } else if (srci->datatype == SR_T_CHAR) {
353 printf(" %s: ", srci->id);
354 if (sr_config_get(sdi->driver, sdi, probe_group, srci->key,
356 tmp_str = g_strdup(g_variant_get_string(gvar, NULL));
357 g_variant_unref(gvar);
361 if (sr_config_list(sdi->driver, sdi, probe_group, srci->key,
367 stropts = g_variant_get_strv(gvar, &num_elements);
368 for (i = 0; i < num_elements; i++) {
371 printf("%s", stropts[i]);
372 if (tmp_str && !strcmp(tmp_str, stropts[i]))
373 printf(" (current)");
378 g_variant_unref(gvar);
380 } else if (srci->datatype == SR_T_UINT64_RANGE) {
381 printf(" %s: ", srci->id);
382 if (sr_config_list(sdi->driver, sdi, probe_group, srci->key,
383 &gvar_list) != SR_OK) {
388 if (sr_config_get(sdi->driver, sdi, NULL, srci->key, &gvar) == SR_OK) {
389 g_variant_get(gvar, "(tt)", &cur_low, &cur_high);
390 g_variant_unref(gvar);
396 num_elements = g_variant_n_children(gvar_list);
397 for (i = 0; i < num_elements; i++) {
398 gvar = g_variant_get_child_value(gvar_list, i);
399 g_variant_get(gvar, "(tt)", &low, &high);
400 g_variant_unref(gvar);
403 printf("%"PRIu64"-%"PRIu64, low, high);
404 if (low == cur_low && high == cur_high)
405 printf(" (current)");
408 g_variant_unref(gvar_list);
410 } else if (srci->datatype == SR_T_BOOL) {
411 printf(" %s: ", srci->id);
412 if (sr_config_get(sdi->driver, sdi, NULL, srci->key,
414 if (g_variant_get_boolean(gvar))
415 printf("on (current), off\n");
417 printf("on, off (current)\n");
418 g_variant_unref(gvar);
424 /* Everything else */
425 printf(" %s\n", srci->id);
428 g_variant_unref(gvar_opts);
431 g_slist_free(devices);
436 void show_pd_detail(void)
439 struct srd_decoder *dec;
440 struct srd_decoder_option *o;
441 char **pdtokens, **pdtok, *optsep, **ann, *val, *doc;
444 pdtokens = g_strsplit(opt_pds, ",", -1);
445 for (pdtok = pdtokens; *pdtok; pdtok++) {
447 if ((optsep = strchr(*pdtok, ':')))
449 if (!(dec = srd_decoder_get_by_id(*pdtok))) {
450 g_critical("Protocol decoder %s not found.", *pdtok);
453 printf("ID: %s\nName: %s\nLong name: %s\nDescription: %s\n",
454 dec->id, dec->name, dec->longname, dec->desc);
455 printf("License: %s\n", dec->license);
456 printf("Annotations:\n");
457 if (dec->annotations) {
458 for (l = dec->annotations; l; l = l->next) {
460 printf("- %s\n %s\n", ann[0], ann[1]);
465 printf("Required probes:\n");
467 for (l = dec->probes; l; l = l->next) {
469 printf("- %s (%s): %s\n",
470 p->name, p->id, p->desc);
475 printf("Optional probes:\n");
476 if (dec->opt_probes) {
477 for (l = dec->opt_probes; l; l = l->next) {
479 printf("- %s (%s): %s\n",
480 p->name, p->id, p->desc);
486 printf("Options:\n");
487 for (l = dec->options; l; l = l->next) {
489 val = g_variant_print(o->def, FALSE);
490 printf("- %s: %s (default %s)\n", o->id, o->desc, val);
494 if ((doc = srd_decoder_doc_get(dec))) {
495 printf("Documentation:\n%s\n",
496 doc[0] == '\n' ? doc + 1 : doc);
501 g_strfreev(pdtokens);