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_SAMPLERATE) {
239 /* Supported samplerates */
240 printf(" %s", srci->id);
241 if (sr_config_list(sdi->driver, sdi, probe_group, SR_CONF_SAMPLERATE,
242 &gvar_dict) != SR_OK) {
246 if ((gvar_list = g_variant_lookup_value(gvar_dict,
247 "samplerates", G_VARIANT_TYPE("at")))) {
248 uint64 = g_variant_get_fixed_array(gvar_list,
249 &num_elements, sizeof(uint64_t));
250 printf(" - supported samplerates:\n");
251 for (i = 0; i < num_elements; i++) {
252 if (!(s = sr_samplerate_string(uint64[i])))
257 g_variant_unref(gvar_list);
258 } else if ((gvar_list = g_variant_lookup_value(gvar_dict,
259 "samplerate-steps", G_VARIANT_TYPE("at")))) {
260 uint64 = g_variant_get_fixed_array(gvar_list,
261 &num_elements, sizeof(uint64_t));
263 if (!(s = sr_samplerate_string(uint64[0])))
268 if (!(s = sr_samplerate_string(uint64[1])))
273 if (!(s = sr_samplerate_string(uint64[2])))
275 printf(" in steps of %s)\n", s);
277 g_variant_unref(gvar_list);
279 g_variant_unref(gvar_dict);
281 } else if (srci->key == SR_CONF_BUFFERSIZE) {
282 /* Supported buffer sizes */
283 printf(" %s", srci->id);
284 if (sr_config_list(sdi->driver, sdi, probe_group,
285 SR_CONF_BUFFERSIZE, &gvar_list) != SR_OK) {
289 uint64 = g_variant_get_fixed_array(gvar_list,
290 &num_elements, sizeof(uint64_t));
291 printf(" - supported buffer sizes:\n");
292 for (i = 0; i < num_elements; i++)
293 printf(" %"PRIu64"\n", uint64[i]);
294 g_variant_unref(gvar_list);
296 } else if (srci->key == SR_CONF_TIMEBASE) {
297 /* Supported time bases */
298 printf(" %s", srci->id);
299 if (sr_config_list(sdi->driver, sdi, probe_group,
300 SR_CONF_TIMEBASE, &gvar_list) != SR_OK) {
304 printf(" - supported time bases:\n");
305 num_elements = g_variant_n_children(gvar_list);
306 for (i = 0; i < num_elements; i++) {
307 gvar = g_variant_get_child_value(gvar_list, i);
308 g_variant_get(gvar, "(tt)", &p, &q);
309 s = sr_period_string(p * q);
313 g_variant_unref(gvar_list);
315 } else if (srci->key == SR_CONF_VDIV) {
316 /* Supported volts/div values */
317 printf(" %s", srci->id);
318 if (sr_config_list(sdi->driver, sdi, probe_group,
319 SR_CONF_VDIV, &gvar_list) != SR_OK) {
323 printf(" - supported volts/div:\n");
324 num_elements = g_variant_n_children(gvar_list);
325 for (i = 0; i < num_elements; i++) {
326 gvar = g_variant_get_child_value(gvar_list, i);
327 g_variant_get(gvar, "(tt)", &p, &q);
328 s = sr_voltage_string(p, q);
332 g_variant_unref(gvar_list);
334 } else if (srci->datatype == SR_T_CHAR) {
335 printf(" %s: ", srci->id);
336 if (sr_config_get(sdi->driver, sdi, probe_group, srci->key,
338 tmp_str = g_strdup(g_variant_get_string(gvar, NULL));
339 g_variant_unref(gvar);
343 if (sr_config_list(sdi->driver, sdi, probe_group, srci->key,
349 stropts = g_variant_get_strv(gvar, &num_elements);
350 for (i = 0; i < num_elements; i++) {
353 printf("%s", stropts[i]);
354 if (tmp_str && !strcmp(tmp_str, stropts[i]))
355 printf(" (current)");
360 g_variant_unref(gvar);
362 } else if (srci->datatype == SR_T_UINT64_RANGE) {
363 printf(" %s: ", srci->id);
364 if (sr_config_list(sdi->driver, sdi, probe_group, srci->key,
365 &gvar_list) != SR_OK) {
370 if (sr_config_get(sdi->driver, sdi, NULL, srci->key, &gvar) == SR_OK) {
371 g_variant_get(gvar, "(tt)", &cur_low, &cur_high);
372 g_variant_unref(gvar);
378 num_elements = g_variant_n_children(gvar_list);
379 for (i = 0; i < num_elements; i++) {
380 gvar = g_variant_get_child_value(gvar_list, i);
381 g_variant_get(gvar, "(tt)", &low, &high);
382 g_variant_unref(gvar);
385 printf("%"PRIu64"-%"PRIu64, low, high);
386 if (low == cur_low && high == cur_high)
387 printf(" (current)");
390 g_variant_unref(gvar_list);
392 } else if (srci->datatype == SR_T_BOOL) {
393 printf(" %s: ", srci->id);
394 if (sr_config_get(sdi->driver, sdi, NULL, srci->key,
396 if (g_variant_get_boolean(gvar))
397 printf("on (current), off\n");
399 printf("on, off (current)\n");
400 g_variant_unref(gvar);
406 /* Everything else */
407 printf(" %s\n", srci->id);
410 g_variant_unref(gvar_opts);
413 g_slist_free(devices);
418 void show_pd_detail(void)
421 struct srd_decoder *dec;
422 struct srd_decoder_option *o;
423 char **pdtokens, **pdtok, *optsep, **ann, *val, *doc;
426 pdtokens = g_strsplit(opt_pds, ",", -1);
427 for (pdtok = pdtokens; *pdtok; pdtok++) {
429 if ((optsep = strchr(*pdtok, ':')))
431 if (!(dec = srd_decoder_get_by_id(*pdtok))) {
432 g_critical("Protocol decoder %s not found.", *pdtok);
435 printf("ID: %s\nName: %s\nLong name: %s\nDescription: %s\n",
436 dec->id, dec->name, dec->longname, dec->desc);
437 printf("License: %s\n", dec->license);
438 printf("Annotations:\n");
439 if (dec->annotations) {
440 for (l = dec->annotations; l; l = l->next) {
442 printf("- %s\n %s\n", ann[0], ann[1]);
447 printf("Required probes:\n");
449 for (l = dec->probes; l; l = l->next) {
451 printf("- %s (%s): %s\n",
452 p->name, p->id, p->desc);
457 printf("Optional probes:\n");
458 if (dec->opt_probes) {
459 for (l = dec->opt_probes; l; l = l->next) {
461 printf("- %s (%s): %s\n",
462 p->name, p->id, p->desc);
468 printf("Options:\n");
469 for (l = dec->options; l; l = l->next) {
471 val = g_variant_print(o->def, FALSE);
472 printf("- %s: %s (default %s)\n", o->id, o->desc, val);
476 if ((doc = srd_decoder_doc_get(dec))) {
477 printf("Documentation:\n%s\n",
478 doc[0] == '\n' ? doc + 1 : doc);
483 g_strfreev(pdtokens);