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 void show_version(void)
29 struct sr_dev_driver **drivers;
30 struct sr_input_format **inputs;
31 struct sr_output_format **outputs;
34 struct srd_decoder *dec;
38 printf("sigrok-cli %s\n\n", VERSION);
40 printf("Using libsigrok %s (lib version %s).\n",
41 sr_package_version_string_get(), sr_lib_version_string_get());
43 printf("Using libsigrokdecode %s (lib version %s).\n\n",
44 srd_package_version_string_get(), srd_lib_version_string_get());
47 printf("Supported hardware drivers:\n");
48 drivers = sr_driver_list();
49 for (i = 0; drivers[i]; i++) {
50 printf(" %-20s %s\n", drivers[i]->name, drivers[i]->longname);
54 printf("Supported input formats:\n");
55 inputs = sr_input_list();
56 for (i = 0; inputs[i]; i++)
57 printf(" %-20s %s\n", inputs[i]->id, inputs[i]->description);
60 printf("Supported output formats:\n");
61 outputs = sr_output_list();
62 for (i = 0; outputs[i]; i++)
63 printf(" %-20s %s\n", outputs[i]->id, outputs[i]->description);
64 printf(" %-20s %s\n", "sigrok", "Default file output format");
68 if (srd_init(NULL) == SRD_OK) {
69 printf("Supported protocol decoders:\n");
70 srd_decoder_load_all();
71 for (l = srd_decoder_list(); l; l = l->next) {
73 printf(" %-20s %s\n", dec->id, dec->longname);
74 /* Print protocol description upon "-l 3" or higher. */
75 if (opt_loglevel >= SR_LOG_INFO)
76 printf(" %-20s %s\n", "", dec->desc);
84 static void print_dev_line(const struct sr_dev_inst *sdi)
86 struct sr_probe *probe;
91 s = g_string_sized_new(128);
92 g_string_assign(s, sdi->driver->name);
93 if (sr_config_get(sdi->driver, sdi, NULL, SR_CONF_CONN, &gvar) == SR_OK) {
94 g_string_append(s, ":conn=");
95 g_string_append(s, g_variant_get_string(gvar, NULL));
96 g_variant_unref(gvar);
98 g_string_append(s, " - ");
99 if (sdi->vendor && sdi->vendor[0])
100 g_string_append_printf(s, "%s ", sdi->vendor);
101 if (sdi->model && sdi->model[0])
102 g_string_append_printf(s, "%s ", sdi->model);
103 if (sdi->version && sdi->version[0])
104 g_string_append_printf(s, "%s ", sdi->version);
106 if (g_slist_length(sdi->probes) == 1) {
107 probe = sdi->probes->data;
108 g_string_append_printf(s, "with 1 probe: %s", probe->name);
110 g_string_append_printf(s, "with %d probes:", g_slist_length(sdi->probes));
111 for (l = sdi->probes; l; l = l->next) {
113 g_string_append_printf(s, " %s", probe->name);
117 g_string_append_printf(s, "\n");
118 printf("%s", s->str);
119 g_string_free(s, TRUE);
123 void show_dev_list(void)
125 struct sr_dev_inst *sdi;
128 if (!(devices = device_scan()))
131 printf("The following devices were found:\n");
132 for (l = devices; l; l = l->next) {
136 g_slist_free(devices);
140 void show_dev_detail(void)
142 struct sr_dev_inst *sdi;
143 const struct sr_config_info *srci;
144 struct sr_probe *probe;
145 struct sr_probe_group *probe_group, *pg;
146 GSList *devices, *pgl, *prl;
147 GVariant *gvar_opts, *gvar_dict, *gvar_list, *gvar;
148 gsize num_opts, num_elements;
149 const uint64_t *uint64, p, q, low, high;
150 uint64_t cur_low, cur_high;
152 unsigned int num_devices, o, i;
155 const char *charopts, **stropts;
157 if (!(devices = device_scan())) {
158 g_critical("No devices found.");
162 num_devices = g_slist_length(devices);
163 if (num_devices > 1) {
164 g_critical("%d devices found. Use --scan to show them, "
165 "and select one to show.", num_devices);
172 if (sr_dev_open(sdi) != SR_OK) {
173 g_critical("Failed to open device.");
177 if ((sr_config_list(sdi->driver, NULL, NULL, SR_CONF_SCAN_OPTIONS,
178 &gvar_opts) == SR_OK)) {
179 opts = g_variant_get_fixed_array(gvar_opts, &num_elements,
181 printf("Supported driver options:\n");
182 for (i = 0; i < num_elements; i++) {
183 if (!(srci = sr_config_info_get(opts[i])))
185 printf(" %s\n", srci->id);
187 g_variant_unref(gvar_opts);
190 probe_group = select_probe_group(sdi);
191 if ((sr_config_list(sdi->driver, sdi, probe_group, SR_CONF_DEVICE_OPTIONS,
192 &gvar_opts)) != SR_OK)
193 /* Driver supports no device instance options. */
196 if (sdi->probe_groups) {
197 printf("Probe groups:\n");
198 for (pgl = sdi->probe_groups; pgl; pgl = pgl->next) {
200 printf(" %s: channel%s", pg->name,
201 g_slist_length(pg->probes) > 1 ? "s" : "");
202 for (prl = pg->probes; prl; prl = prl->next) {
204 printf(" %s", probe->name);
210 printf("Supported configuration options");
211 if (sdi->probe_groups) {
213 printf(" across all probe groups");
215 printf(" on probe group %s", probe_group->name);
218 opts = g_variant_get_fixed_array(gvar_opts, &num_opts, sizeof(int32_t));
219 for (o = 0; o < num_opts; o++) {
220 if (!(srci = sr_config_info_get(opts[o])))
223 if (srci->key == SR_CONF_TRIGGER_TYPE) {
224 if (sr_config_list(sdi->driver, sdi, probe_group, srci->key,
229 charopts = g_variant_get_string(gvar, NULL);
230 printf(" Supported triggers: ");
232 printf("%c ", *charopts);
236 g_variant_unref(gvar);
238 } else if (srci->key == SR_CONF_PATTERN_MODE) {
239 /* Pattern generator modes */
240 printf(" %s", srci->id);
241 if (sr_config_list(sdi->driver, sdi, probe_group, srci->key,
243 printf(" - supported patterns:\n");
244 stropts = g_variant_get_strv(gvar, &num_elements);
245 for (i = 0; i < num_elements; i++)
246 printf(" %s\n", stropts[i]);
247 g_variant_unref(gvar);
252 } else if (srci->key == SR_CONF_SAMPLERATE) {
253 /* Supported samplerates */
254 printf(" %s", srci->id);
255 if (sr_config_list(sdi->driver, sdi, probe_group, SR_CONF_SAMPLERATE,
256 &gvar_dict) != SR_OK) {
260 if ((gvar_list = g_variant_lookup_value(gvar_dict,
261 "samplerates", G_VARIANT_TYPE("at")))) {
262 uint64 = g_variant_get_fixed_array(gvar_list,
263 &num_elements, sizeof(uint64_t));
264 printf(" - supported samplerates:\n");
265 for (i = 0; i < num_elements; i++) {
266 if (!(s = sr_samplerate_string(uint64[i])))
271 g_variant_unref(gvar_list);
272 } else if ((gvar_list = g_variant_lookup_value(gvar_dict,
273 "samplerate-steps", G_VARIANT_TYPE("at")))) {
274 uint64 = g_variant_get_fixed_array(gvar_list,
275 &num_elements, sizeof(uint64_t));
277 if (!(s = sr_samplerate_string(uint64[0])))
282 if (!(s = sr_samplerate_string(uint64[1])))
287 if (!(s = sr_samplerate_string(uint64[2])))
289 printf(" in steps of %s)\n", s);
291 g_variant_unref(gvar_list);
293 g_variant_unref(gvar_dict);
295 } else if (srci->key == SR_CONF_BUFFERSIZE) {
296 /* Supported buffer sizes */
297 printf(" %s", srci->id);
298 if (sr_config_list(sdi->driver, sdi, probe_group,
299 SR_CONF_BUFFERSIZE, &gvar_list) != SR_OK) {
303 uint64 = g_variant_get_fixed_array(gvar_list,
304 &num_elements, sizeof(uint64_t));
305 printf(" - supported buffer sizes:\n");
306 for (i = 0; i < num_elements; i++)
307 printf(" %"PRIu64"\n", uint64[i]);
308 g_variant_unref(gvar_list);
310 } else if (srci->key == SR_CONF_TIMEBASE) {
311 /* Supported time bases */
312 printf(" %s", srci->id);
313 if (sr_config_list(sdi->driver, sdi, probe_group,
314 SR_CONF_TIMEBASE, &gvar_list) != SR_OK) {
318 printf(" - supported time bases:\n");
319 num_elements = g_variant_n_children(gvar_list);
320 for (i = 0; i < num_elements; i++) {
321 gvar = g_variant_get_child_value(gvar_list, i);
322 g_variant_get(gvar, "(tt)", &p, &q);
323 s = sr_period_string(p * q);
327 g_variant_unref(gvar_list);
329 } else if (srci->key == SR_CONF_VDIV) {
330 /* Supported volts/div values */
331 printf(" %s", srci->id);
332 if (sr_config_list(sdi->driver, sdi, probe_group,
333 SR_CONF_VDIV, &gvar_list) != SR_OK) {
337 printf(" - supported volts/div:\n");
338 num_elements = g_variant_n_children(gvar_list);
339 for (i = 0; i < num_elements; i++) {
340 gvar = g_variant_get_child_value(gvar_list, i);
341 g_variant_get(gvar, "(tt)", &p, &q);
342 s = sr_voltage_string(p, q);
346 g_variant_unref(gvar_list);
348 } else if (srci->datatype == SR_T_CHAR) {
349 printf(" %s: ", srci->id);
350 if (sr_config_get(sdi->driver, sdi, probe_group, srci->key,
352 tmp_str = g_strdup(g_variant_get_string(gvar, NULL));
353 g_variant_unref(gvar);
357 if (sr_config_list(sdi->driver, sdi, probe_group, srci->key,
363 stropts = g_variant_get_strv(gvar, &num_elements);
364 for (i = 0; i < num_elements; i++) {
367 printf("%s", stropts[i]);
368 if (tmp_str && !strcmp(tmp_str, stropts[i]))
369 printf(" (current)");
374 g_variant_unref(gvar);
376 } else if (srci->datatype == SR_T_UINT64_RANGE) {
377 printf(" %s: ", srci->id);
378 if (sr_config_list(sdi->driver, sdi, probe_group, srci->key,
379 &gvar_list) != SR_OK) {
384 if (sr_config_get(sdi->driver, sdi, NULL, srci->key, &gvar) == SR_OK) {
385 g_variant_get(gvar, "(tt)", &cur_low, &cur_high);
386 g_variant_unref(gvar);
392 num_elements = g_variant_n_children(gvar_list);
393 for (i = 0; i < num_elements; i++) {
394 gvar = g_variant_get_child_value(gvar_list, i);
395 g_variant_get(gvar, "(tt)", &low, &high);
396 g_variant_unref(gvar);
399 printf("%"PRIu64"-%"PRIu64, low, high);
400 if (low == cur_low && high == cur_high)
401 printf(" (current)");
404 g_variant_unref(gvar_list);
406 } else if (srci->datatype == SR_T_BOOL) {
407 printf(" %s: ", srci->id);
408 if (sr_config_get(sdi->driver, sdi, NULL, srci->key,
410 if (g_variant_get_boolean(gvar))
411 printf("on (current), off\n");
413 printf("on, off (current)\n");
414 g_variant_unref(gvar);
420 /* Everything else */
421 printf(" %s\n", srci->id);
424 g_variant_unref(gvar_opts);
427 g_slist_free(devices);
432 void show_pd_detail(void)
435 struct srd_decoder *dec;
436 struct srd_decoder_option *o;
437 char **pdtokens, **pdtok, *optsep, **ann, *val, *doc;
440 pdtokens = g_strsplit(opt_pds, ",", -1);
441 for (pdtok = pdtokens; *pdtok; pdtok++) {
443 if ((optsep = strchr(*pdtok, ':')))
445 if (!(dec = srd_decoder_get_by_id(*pdtok))) {
446 g_critical("Protocol decoder %s not found.", *pdtok);
449 printf("ID: %s\nName: %s\nLong name: %s\nDescription: %s\n",
450 dec->id, dec->name, dec->longname, dec->desc);
451 printf("License: %s\n", dec->license);
452 printf("Annotations:\n");
453 if (dec->annotations) {
454 for (l = dec->annotations; l; l = l->next) {
456 printf("- %s\n %s\n", ann[0], ann[1]);
461 printf("Required probes:\n");
463 for (l = dec->probes; l; l = l->next) {
465 printf("- %s (%s): %s\n",
466 p->name, p->id, p->desc);
471 printf("Optional probes:\n");
472 if (dec->opt_probes) {
473 for (l = dec->opt_probes; l; l = l->next) {
475 printf("- %s (%s): %s\n",
476 p->name, p->id, p->desc);
482 printf("Options:\n");
483 for (l = dec->options; l; l = l->next) {
485 val = g_variant_print(o->def, FALSE);
486 printf("- %s: %s (default %s)\n", o->id, o->desc, val);
490 if ((doc = srd_decoder_doc_get(dec))) {
491 printf("Documentation:\n%s\n",
492 doc[0] == '\n' ? doc + 1 : doc);
497 g_strfreev(pdtokens);