]> sigrok.org Git - sigrok-cli.git/blame - show.c
Check for config key availability before using it.
[sigrok-cli.git] / show.c
CommitLineData
2be182e6
BV
1/*
2 * This file is part of the sigrok-cli project.
3 *
4 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
20fb52e0 20#include "sigrok-cli.h"
2be182e6 21#include <glib.h>
55de58a2 22#include <string.h>
2be182e6 23
37e4fcd4
BV
24static gint sort_inputs(gconstpointer a, gconstpointer b)
25{
a8b4041a
BV
26 return strcmp(sr_input_id_get((struct sr_input_module *)a),
27 sr_input_id_get((struct sr_input_module *)b));
37e4fcd4
BV
28}
29
30static gint sort_outputs(gconstpointer a, gconstpointer b)
31{
ad6520c4
BV
32 return strcmp(sr_output_id_get((struct sr_output_module *)a),
33 sr_output_id_get((struct sr_output_module *)b));
37e4fcd4
BV
34}
35
36static gint sort_drivers(gconstpointer a, gconstpointer b)
37{
38 const struct sr_dev_driver *sdda = a, *sddb = b;
39
40 return strcmp(sdda->name, sddb->name);
41}
42
628197af 43#ifdef HAVE_SRD
37e4fcd4
BV
44static gint sort_pds(gconstpointer a, gconstpointer b)
45{
46 const struct srd_decoder *sda = a, *sdb = b;
47
48 return strcmp(sda->id, sdb->id);
49}
628197af 50#endif
37e4fcd4 51
2be182e6
BV
52void show_version(void)
53{
37e4fcd4 54 struct sr_dev_driver **drivers, *driver;
a8b4041a 55 const struct sr_input_module **inputs, *input;
ad6520c4 56 const struct sr_output_module **outputs, *output;
37e4fcd4
BV
57 const GSList *l;
58 GSList *sl;
2be182e6
BV
59 int i;
60#ifdef HAVE_SRD
61 struct srd_decoder *dec;
2be182e6
BV
62#endif
63
64 printf("sigrok-cli %s\n\n", VERSION);
65
66 printf("Using libsigrok %s (lib version %s).\n",
67 sr_package_version_string_get(), sr_lib_version_string_get());
68#ifdef HAVE_SRD
69 printf("Using libsigrokdecode %s (lib version %s).\n\n",
70 srd_package_version_string_get(), srd_lib_version_string_get());
71#endif
72
73 printf("Supported hardware drivers:\n");
74 drivers = sr_driver_list();
37e4fcd4
BV
75 for (sl = NULL, i = 0; drivers[i]; i++)
76 sl = g_slist_append(sl, drivers[i]);
77 sl = g_slist_sort(sl, sort_drivers);
78 for (l = sl; l; l = l->next) {
79 driver = l->data;
80 printf(" %-20s %s\n", driver->name, driver->longname);
2be182e6
BV
81 }
82 printf("\n");
37e4fcd4 83 g_slist_free(sl);
2be182e6
BV
84
85 printf("Supported input formats:\n");
86 inputs = sr_input_list();
37e4fcd4 87 for (sl = NULL, i = 0; inputs[i]; i++)
a8b4041a 88 sl = g_slist_append(sl, (gpointer)inputs[i]);
37e4fcd4
BV
89 sl = g_slist_sort(sl, sort_inputs);
90 for (l = sl; l; l = l->next) {
91 input = l->data;
a8b4041a
BV
92 printf(" %-20s %s\n", sr_input_id_get(input),
93 sr_input_description_get(input));
37e4fcd4 94 }
2be182e6 95 printf("\n");
37e4fcd4 96 g_slist_free(sl);
2be182e6
BV
97
98 printf("Supported output formats:\n");
99 outputs = sr_output_list();
37e4fcd4 100 for (sl = NULL, i = 0; outputs[i]; i++)
ad6520c4 101 sl = g_slist_append(sl, (gpointer)outputs[i]);
37e4fcd4
BV
102 sl = g_slist_sort(sl, sort_outputs);
103 for (l = sl; l; l = l->next) {
104 output = l->data;
ad6520c4
BV
105 printf(" %-20s %s\n", sr_output_id_get(output),
106 sr_output_description_get(output));
37e4fcd4 107 }
2be182e6 108 printf("\n");
37e4fcd4 109 g_slist_free(sl);
2be182e6
BV
110
111#ifdef HAVE_SRD
112 if (srd_init(NULL) == SRD_OK) {
113 printf("Supported protocol decoders:\n");
114 srd_decoder_load_all();
37e4fcd4
BV
115 sl = g_slist_copy((GSList *)srd_decoder_list());
116 sl = g_slist_sort(sl, sort_pds);
117 for (l = sl; l; l = l->next) {
2be182e6
BV
118 dec = l->data;
119 printf(" %-20s %s\n", dec->id, dec->longname);
120 /* Print protocol description upon "-l 3" or higher. */
121 if (opt_loglevel >= SR_LOG_INFO)
122 printf(" %-20s %s\n", "", dec->desc);
123 }
37e4fcd4 124 g_slist_free(sl);
2be182e6
BV
125 srd_exit();
126 }
127 printf("\n");
128#endif
129}
130
029d73fe 131static gint sort_channels(gconstpointer a, gconstpointer b)
dd2f206a 132{
029d73fe 133 const struct sr_channel *pa = a, *pb = b;
dd2f206a
BV
134
135 return pa->index - pb->index;
136}
137
783e263e
BV
138static gboolean config_key_has_cap(const struct sr_dev_inst *sdi,
139 struct sr_channel_group *cg, uint32_t key, uint32_t capability)
140{
141 struct sr_dev_driver *driver;
142 GVariant *gvar_opts;
143 const uint32_t *opts;
144 gsize num_opts, i;
145
146 driver = sr_dev_inst_driver_get(sdi);
147 if (sr_config_list(driver, sdi, cg, SR_CONF_DEVICE_OPTIONS,
148 &gvar_opts) != SR_OK)
149 return FALSE;
150
151 opts = g_variant_get_fixed_array(gvar_opts, &num_opts, sizeof(uint32_t));
152 for (i = 0; i < num_opts; i++) {
153 if ((opts[i] & SR_CONF_MASK) == key) {
154 if (opts[i] & capability)
155 return TRUE;
156 else
157 return FALSE;
158 }
159 }
160
161 return FALSE;
162}
163
164
2be182e6
BV
165static void print_dev_line(const struct sr_dev_inst *sdi)
166{
029d73fe 167 struct sr_channel *ch;
c6fa2b2e 168 GSList *sl, *l, *channels;
2be182e6
BV
169 GString *s;
170 GVariant *gvar;
c6fa2b2e
UH
171 struct sr_dev_driver *driver;
172 const char *vendor, *model, *version;
173
174 driver = sr_dev_inst_driver_get(sdi);
175 vendor = sr_dev_inst_vendor_get(sdi);
176 model = sr_dev_inst_model_get(sdi);
177 version = sr_dev_inst_version_get(sdi);
178 channels = sr_dev_inst_channels_get(sdi);
2be182e6
BV
179
180 s = g_string_sized_new(128);
c6fa2b2e 181 g_string_assign(s, driver->name);
783e263e
BV
182 if (config_key_has_cap(sdi, NULL, SR_CONF_CONN, SR_CONF_GET)
183 && sr_config_get(driver, sdi, NULL, SR_CONF_CONN, &gvar) == SR_OK) {
2be182e6
BV
184 g_string_append(s, ":conn=");
185 g_string_append(s, g_variant_get_string(gvar, NULL));
186 g_variant_unref(gvar);
187 }
188 g_string_append(s, " - ");
c6fa2b2e
UH
189 if (vendor && vendor[0])
190 g_string_append_printf(s, "%s ", vendor);
191 if (model && model[0])
192 g_string_append_printf(s, "%s ", model);
193 if (version && version[0])
194 g_string_append_printf(s, "%s ", version);
195 if (channels) {
196 if (g_slist_length(channels) == 1) {
197 ch = channels->data;
029d73fe 198 g_string_append_printf(s, "with 1 channel: %s", ch->name);
2be182e6 199 } else {
c6fa2b2e 200 sl = g_slist_sort(g_slist_copy(channels), sort_channels);
029d73fe 201 g_string_append_printf(s, "with %d channels:", g_slist_length(sl));
dd2f206a 202 for (l = sl; l; l = l->next) {
029d73fe
UH
203 ch = l->data;
204 g_string_append_printf(s, " %s", ch->name);
2be182e6 205 }
dd2f206a 206 g_slist_free(sl);
2be182e6
BV
207 }
208 }
209 g_string_append_printf(s, "\n");
210 printf("%s", s->str);
211 g_string_free(s, TRUE);
212
213}
214
215void show_dev_list(void)
216{
217 struct sr_dev_inst *sdi;
218 GSList *devices, *l;
219
220 if (!(devices = device_scan()))
221 return;
222
223 printf("The following devices were found:\n");
224 for (l = devices; l; l = l->next) {
225 sdi = l->data;
226 print_dev_line(sdi);
227 }
228 g_slist_free(devices);
229
230}
231
d75c8529
BV
232void show_drv_detail(struct sr_dev_driver *driver)
233{
234 const struct sr_config_info *srci;
235 GVariant *gvar_opts;
236 const uint32_t *opts;
237 gsize num_elements, i;
238
783e263e
BV
239 if (sr_config_list(driver, NULL, NULL, SR_CONF_DEVICE_OPTIONS,
240 &gvar_opts) == SR_OK) {
d75c8529
BV
241 opts = g_variant_get_fixed_array(gvar_opts, &num_elements,
242 sizeof(uint32_t));
243 if (num_elements) {
244 printf("Driver functions:\n");
245 for (i = 0; i < num_elements; i++) {
246 if (!(srci = sr_config_info_get(opts[i] & SR_CONF_MASK)))
247 continue;
248 printf(" %s\n", srci->name);
249 }
250 }
251 g_variant_unref(gvar_opts);
252 }
253
783e263e
BV
254 if (sr_config_list(driver, NULL, NULL, SR_CONF_SCAN_OPTIONS,
255 &gvar_opts) == SR_OK) {
d75c8529
BV
256 opts = g_variant_get_fixed_array(gvar_opts, &num_elements,
257 sizeof(uint32_t));
258 if (num_elements) {
259 printf("Scan options:\n");
260 for (i = 0; i < num_elements; i++) {
261 if (!(srci = sr_config_info_get(opts[i] & SR_CONF_MASK)))
262 continue;
263 printf(" %s\n", srci->id);
264 }
265 }
266 g_variant_unref(gvar_opts);
267 }
268}
269
2be182e6
BV
270void show_dev_detail(void)
271{
c6fa2b2e 272 struct sr_dev_driver *driver_from_opt, *driver;
2be182e6
BV
273 struct sr_dev_inst *sdi;
274 const struct sr_config_info *srci;
029d73fe 275 struct sr_channel *ch;
ca50f4b3 276 struct sr_channel_group *channel_group, *cg;
c6fa2b2e 277 GSList *devices, *cgl, *chl, *channel_groups;
2be182e6
BV
278 GVariant *gvar_opts, *gvar_dict, *gvar_list, *gvar;
279 gsize num_opts, num_elements;
426d0cda 280 double dlow, dhigh, dcur_low, dcur_high;
2be182e6 281 const uint64_t *uint64, p, q, low, high;
5e78186b 282 uint64_t tmp_uint64, cur_low, cur_high, cur_p, cur_q;
23cbb498
BV
283 const uint32_t *opts;
284 const int32_t *int32;
131ae8d0
BV
285 uint32_t key, o;
286 unsigned int num_devices, i;
23cbb498 287 char *tmp_str, *s, c;
6b27bde4 288 const char **stropts;
2be182e6 289
c6fa2b2e 290 if (parse_driver(opt_drv, &driver_from_opt, NULL)) {
d75c8529 291 /* A driver was specified, report driver-wide options now. */
c6fa2b2e 292 show_drv_detail(driver_from_opt);
d75c8529
BV
293 }
294
2be182e6
BV
295 if (!(devices = device_scan())) {
296 g_critical("No devices found.");
297 return;
298 }
299
300 num_devices = g_slist_length(devices);
301 if (num_devices > 1) {
302 g_critical("%d devices found. Use --scan to show them, "
303 "and select one to show.", num_devices);
304 return;
305 }
306
307 sdi = devices->data;
b4eece7c 308 g_slist_free(devices);
2be182e6
BV
309 print_dev_line(sdi);
310
c6fa2b2e
UH
311 driver = sr_dev_inst_driver_get(sdi);
312 channel_groups = sr_dev_inst_channel_groups_get(sdi);
313
2be182e6
BV
314 if (sr_dev_open(sdi) != SR_OK) {
315 g_critical("Failed to open device.");
316 return;
317 }
318
ca50f4b3 319 /* Selected channels and channel group may affect which options are
02c65935 320 * returned, or which values for them. */
029d73fe 321 select_channels(sdi);
ca50f4b3 322 channel_group = select_channel_group(sdi);
02c65935 323
783e263e
BV
324 if (sr_config_list(driver, sdi, channel_group, SR_CONF_DEVICE_OPTIONS,
325 &gvar_opts) != SR_OK)
2be182e6
BV
326 /* Driver supports no device instance options. */
327 return;
328
c6fa2b2e 329 if (channel_groups) {
ca50f4b3 330 printf("Channel groups:\n");
c6fa2b2e 331 for (cgl = channel_groups; cgl; cgl = cgl->next) {
ca50f4b3
UH
332 cg = cgl->data;
333 printf(" %s: channel%s", cg->name,
334 g_slist_length(cg->channels) > 1 ? "s" : "");
029d73fe
UH
335 for (chl = cg->channels; chl; chl = chl->next) {
336 ch = chl->data;
337 printf(" %s", ch->name);
2be182e6
BV
338 }
339 printf("\n");
340 }
341 }
342
343 printf("Supported configuration options");
c6fa2b2e 344 if (channel_groups) {
ca50f4b3
UH
345 if (!channel_group)
346 printf(" across all channel groups");
2be182e6 347 else
ca50f4b3 348 printf(" on channel group %s", channel_group->name);
2be182e6
BV
349 }
350 printf(":\n");
131ae8d0 351 opts = g_variant_get_fixed_array(gvar_opts, &num_opts, sizeof(uint32_t));
2be182e6 352 for (o = 0; o < num_opts; o++) {
23cbb498
BV
353 key = opts[o] & SR_CONF_MASK;
354 if (!(srci = sr_config_info_get(key)))
2be182e6
BV
355 continue;
356
23cbb498 357 if (key == SR_CONF_TRIGGER_MATCH) {
c6fa2b2e 358 if (sr_config_list(driver, sdi, channel_group, key,
6b27bde4 359 &gvar_list) != SR_OK) {
2be182e6
BV
360 printf("\n");
361 continue;
362 }
6b27bde4
BV
363 int32 = g_variant_get_fixed_array(gvar_list,
364 &num_elements, sizeof(int32_t));
2be182e6 365 printf(" Supported triggers: ");
6b27bde4
BV
366 for (i = 0; i < num_elements; i++) {
367 switch(int32[i]) {
368 case SR_TRIGGER_ZERO:
369 c = '0';
370 break;
371 case SR_TRIGGER_ONE:
372 c = '1';
373 break;
374 case SR_TRIGGER_RISING:
375 c = 'r';
376 break;
377 case SR_TRIGGER_FALLING:
378 c = 'f';
379 break;
380 case SR_TRIGGER_EDGE:
381 c = 'e';
382 break;
383 case SR_TRIGGER_OVER:
384 c = 'o';
385 break;
386 case SR_TRIGGER_UNDER:
387 c = 'u';
388 break;
50c641aa
AJ
389 default:
390 c = 0;
391 break;
6b27bde4 392 }
50c641aa
AJ
393 if (c)
394 printf("%c ", c);
2be182e6
BV
395 }
396 printf("\n");
6b27bde4 397 g_variant_unref(gvar_list);
2be182e6 398
23cbb498 399 } else if (key == SR_CONF_LIMIT_SAMPLES) {
02c65935
BV
400 /* If implemented in config_list(), this denotes the
401 * maximum number of samples a device can send. This
402 * really applies only to logic analyzers, and then
403 * only to those that don't support compression, or
404 * have it turned off by default. The values returned
405 * are the low/high limits. */
c6fa2b2e 406 if (sr_config_list(driver, sdi, channel_group, key,
02c65935
BV
407 &gvar) != SR_OK) {
408 continue;
409 }
410 g_variant_get(gvar, "(tt)", &low, &high);
411 g_variant_unref(gvar);
412 printf(" Maximum number of samples: %"PRIu64"\n", high);
413
23cbb498 414 } else if (key == SR_CONF_SAMPLERATE) {
2be182e6
BV
415 /* Supported samplerates */
416 printf(" %s", srci->id);
c6fa2b2e 417 if (sr_config_list(driver, sdi, channel_group, SR_CONF_SAMPLERATE,
2be182e6
BV
418 &gvar_dict) != SR_OK) {
419 printf("\n");
420 continue;
421 }
422 if ((gvar_list = g_variant_lookup_value(gvar_dict,
423 "samplerates", G_VARIANT_TYPE("at")))) {
424 uint64 = g_variant_get_fixed_array(gvar_list,
425 &num_elements, sizeof(uint64_t));
426 printf(" - supported samplerates:\n");
427 for (i = 0; i < num_elements; i++) {
428 if (!(s = sr_samplerate_string(uint64[i])))
429 continue;
430 printf(" %s\n", s);
431 g_free(s);
432 }
433 g_variant_unref(gvar_list);
434 } else if ((gvar_list = g_variant_lookup_value(gvar_dict,
435 "samplerate-steps", G_VARIANT_TYPE("at")))) {
436 uint64 = g_variant_get_fixed_array(gvar_list,
437 &num_elements, sizeof(uint64_t));
438 /* low */
439 if (!(s = sr_samplerate_string(uint64[0])))
440 continue;
441 printf(" (%s", s);
442 g_free(s);
443 /* high */
444 if (!(s = sr_samplerate_string(uint64[1])))
445 continue;
446 printf(" - %s", s);
447 g_free(s);
448 /* step */
449 if (!(s = sr_samplerate_string(uint64[2])))
450 continue;
451 printf(" in steps of %s)\n", s);
452 g_free(s);
453 g_variant_unref(gvar_list);
454 }
455 g_variant_unref(gvar_dict);
456
5e78186b 457 } else if (srci->datatype == SR_T_UINT64) {
2be182e6 458 printf(" %s", srci->id);
5e78186b 459 gvar = NULL;
c6fa2b2e 460 if (sr_config_get(driver, sdi, channel_group, key,
5e78186b
BV
461 &gvar) == SR_OK) {
462 tmp_uint64 = g_variant_get_uint64(gvar);
463 g_variant_unref(gvar);
464 } else
465 tmp_uint64 = 0;
c6fa2b2e 466 if (sr_config_list(driver, sdi, channel_group,
2be182e6 467 SR_CONF_BUFFERSIZE, &gvar_list) != SR_OK) {
5e78186b
BV
468 if (gvar) {
469 /* Can't list it, but we have a value to show. */
470 printf("%"PRIu64" (current)", tmp_uint64);
471 }
2be182e6
BV
472 printf("\n");
473 continue;
474 }
475 uint64 = g_variant_get_fixed_array(gvar_list,
476 &num_elements, sizeof(uint64_t));
5e78186b 477 printf(" - supported values:\n");
2be182e6 478 for (i = 0; i < num_elements; i++) {
5e78186b
BV
479 printf(" %"PRIu64, uint64[i]);
480 if (gvar && tmp_uint64 == uint64[i])
481 printf(" (current)");
2be182e6 482 printf("\n");
2be182e6
BV
483 }
484 g_variant_unref(gvar_list);
485
9db40e9f 486 } else if (srci->datatype == SR_T_STRING) {
2be182e6 487 printf(" %s: ", srci->id);
c6fa2b2e 488 if (sr_config_get(driver, sdi, channel_group, key,
2be182e6
BV
489 &gvar) == SR_OK) {
490 tmp_str = g_strdup(g_variant_get_string(gvar, NULL));
491 g_variant_unref(gvar);
492 } else
493 tmp_str = NULL;
494
c6fa2b2e 495 if (sr_config_list(driver, sdi, channel_group, key,
2be182e6 496 &gvar) != SR_OK) {
dcb420e7
BV
497 if (tmp_str) {
498 /* Can't list it, but we have a value to show. */
499 printf("%s (current)", tmp_str);
500 }
2be182e6 501 printf("\n");
55bc3776 502 g_free(tmp_str);
2be182e6
BV
503 continue;
504 }
505
506 stropts = g_variant_get_strv(gvar, &num_elements);
507 for (i = 0; i < num_elements; i++) {
508 if (i)
509 printf(", ");
510 printf("%s", stropts[i]);
511 if (tmp_str && !strcmp(tmp_str, stropts[i]))
512 printf(" (current)");
513 }
514 printf("\n");
515 g_free(stropts);
516 g_free(tmp_str);
517 g_variant_unref(gvar);
518
519 } else if (srci->datatype == SR_T_UINT64_RANGE) {
520 printf(" %s: ", srci->id);
c6fa2b2e 521 if (sr_config_list(driver, sdi, channel_group, key,
2be182e6
BV
522 &gvar_list) != SR_OK) {
523 printf("\n");
524 continue;
525 }
526
c6fa2b2e 527 if (sr_config_get(driver, sdi, channel_group, key, &gvar) == SR_OK) {
2be182e6
BV
528 g_variant_get(gvar, "(tt)", &cur_low, &cur_high);
529 g_variant_unref(gvar);
530 } else {
531 cur_low = 0;
532 cur_high = 0;
533 }
534
535 num_elements = g_variant_n_children(gvar_list);
536 for (i = 0; i < num_elements; i++) {
537 gvar = g_variant_get_child_value(gvar_list, i);
538 g_variant_get(gvar, "(tt)", &low, &high);
539 g_variant_unref(gvar);
540 if (i)
541 printf(", ");
542 printf("%"PRIu64"-%"PRIu64, low, high);
543 if (low == cur_low && high == cur_high)
544 printf(" (current)");
545 }
546 printf("\n");
547 g_variant_unref(gvar_list);
548
549 } else if (srci->datatype == SR_T_BOOL) {
550 printf(" %s: ", srci->id);
c6fa2b2e 551 if (sr_config_get(driver, sdi, channel_group, key,
2be182e6
BV
552 &gvar) == SR_OK) {
553 if (g_variant_get_boolean(gvar))
554 printf("on (current), off\n");
555 else
556 printf("on, off (current)\n");
557 g_variant_unref(gvar);
558 } else
559 printf("on, off\n");
560
426d0cda
BV
561 } else if (srci->datatype == SR_T_DOUBLE_RANGE) {
562 printf(" %s: ", srci->id);
c6fa2b2e 563 if (sr_config_list(driver, sdi, channel_group, key,
426d0cda
BV
564 &gvar_list) != SR_OK) {
565 printf("\n");
566 continue;
567 }
568
c6fa2b2e 569 if (sr_config_get(driver, sdi, channel_group, key, &gvar) == SR_OK) {
426d0cda
BV
570 g_variant_get(gvar, "(dd)", &dcur_low, &dcur_high);
571 g_variant_unref(gvar);
572 } else {
573 dcur_low = 0;
574 dcur_high = 0;
575 }
576
577 num_elements = g_variant_n_children(gvar_list);
578 for (i = 0; i < num_elements; i++) {
579 gvar = g_variant_get_child_value(gvar_list, i);
580 g_variant_get(gvar, "(dd)", &dlow, &dhigh);
581 g_variant_unref(gvar);
582 if (i)
583 printf(", ");
584 printf("%.1f-%.1f", dlow, dhigh);
585 if (dlow == dcur_low && dhigh == dcur_high)
586 printf(" (current)");
587 }
588 printf("\n");
589 g_variant_unref(gvar_list);
590
dcb420e7
BV
591 } else if (srci->datatype == SR_T_FLOAT) {
592 printf(" %s: ", srci->id);
c6fa2b2e 593 if (sr_config_get(driver, sdi, channel_group, key,
dcb420e7
BV
594 &gvar) == SR_OK) {
595 printf("%f\n", g_variant_get_double(gvar));
596 g_variant_unref(gvar);
597 } else
5e78186b
BV
598 printf("\n");
599
600 } else if (srci->datatype == SR_T_RATIONAL_PERIOD
601 || srci->datatype == SR_T_RATIONAL_VOLT) {
602 printf(" %s", srci->id);
c6fa2b2e 603 if (sr_config_get(driver, sdi, channel_group, key,
5e78186b
BV
604 &gvar) == SR_OK) {
605 g_variant_get(gvar, "(tt)", &cur_p, &cur_q);
606 g_variant_unref(gvar);
607 } else
608 cur_p = cur_q = 0;
609
c6fa2b2e 610 if (sr_config_list(driver, sdi, channel_group,
5e78186b
BV
611 key, &gvar_list) != SR_OK) {
612 printf("\n");
613 continue;
614 }
615 printf(" - supported values:\n");
616 num_elements = g_variant_n_children(gvar_list);
617 for (i = 0; i < num_elements; i++) {
618 gvar = g_variant_get_child_value(gvar_list, i);
619 g_variant_get(gvar, "(tt)", &p, &q);
620 if (srci->datatype == SR_T_RATIONAL_PERIOD)
621 s = sr_period_string(p * q);
622 else
623 s = sr_voltage_string(p, q);
624 printf(" %s", s);
625 g_free(s);
626 if (p == cur_p && q == cur_q)
627 printf(" (current)");
628 printf("\n");
629 }
630 g_variant_unref(gvar_list);
dcb420e7 631
2be182e6
BV
632 } else {
633
634 /* Everything else */
635 printf(" %s\n", srci->id);
636 }
637 }
638 g_variant_unref(gvar_opts);
639
640 sr_dev_close(sdi);
2be182e6
BV
641
642}
643
644#ifdef HAVE_SRD
645void show_pd_detail(void)
646{
2be182e6
BV
647 struct srd_decoder *dec;
648 struct srd_decoder_option *o;
3dfbfbc8 649 struct srd_channel *pdch;
1eb46be8 650 struct srd_decoder_annotation_row *r;
3d7bf979
BV
651 GSList *l, *ll, *ol;
652 int idx;
653 char **pdtokens, **pdtok, *optsep, **ann, *val, *doc;
2be182e6
BV
654
655 pdtokens = g_strsplit(opt_pds, ",", -1);
656 for (pdtok = pdtokens; *pdtok; pdtok++) {
657 /* Strip options. */
658 if ((optsep = strchr(*pdtok, ':')))
659 *optsep = '\0';
660 if (!(dec = srd_decoder_get_by_id(*pdtok))) {
661 g_critical("Protocol decoder %s not found.", *pdtok);
662 return;
663 }
664 printf("ID: %s\nName: %s\nLong name: %s\nDescription: %s\n",
665 dec->id, dec->name, dec->longname, dec->desc);
666 printf("License: %s\n", dec->license);
b39f3a1a 667 printf("Annotation classes:\n");
2be182e6
BV
668 if (dec->annotations) {
669 for (l = dec->annotations; l; l = l->next) {
670 ann = l->data;
b39f3a1a 671 printf("- %s: %s\n", ann[0], ann[1]);
2be182e6
BV
672 }
673 } else {
674 printf("None.\n");
675 }
1eb46be8
UH
676 printf("Annotation rows:\n");
677 if (dec->annotation_rows) {
678 for (l = dec->annotation_rows; l; l = l->next) {
679 r = l->data;
b39f3a1a 680 printf("- %s (%s): ", r->id, r->desc);
3d7bf979
BV
681 for (ll = r->ann_classes; ll; ll = ll->next) {
682 idx = GPOINTER_TO_INT(ll->data);
683 ann = g_slist_nth_data(dec->annotations, idx);
684 printf("%s", ann[0]);
685 if (ll->next)
686 printf(", ");
687 }
1eb46be8
UH
688 printf("\n");
689 }
690 } else {
691 printf("None.\n");
692 }
3dfbfbc8
UH
693 printf("Required channels:\n");
694 if (dec->channels) {
695 for (l = dec->channels; l; l = l->next) {
696 pdch = l->data;
2be182e6 697 printf("- %s (%s): %s\n",
3dfbfbc8 698 pdch->id, pdch->name, pdch->desc);
2be182e6
BV
699 }
700 } else {
701 printf("None.\n");
702 }
3dfbfbc8
UH
703 printf("Optional channels:\n");
704 if (dec->opt_channels) {
705 for (l = dec->opt_channels; l; l = l->next) {
706 pdch = l->data;
2be182e6 707 printf("- %s (%s): %s\n",
3dfbfbc8 708 pdch->id, pdch->name, pdch->desc);
2be182e6
BV
709 }
710 } else {
711 printf("None.\n");
712 }
1eb46be8 713 printf("Options:\n");
2be182e6 714 if (dec->options) {
2be182e6
BV
715 for (l = dec->options; l; l = l->next) {
716 o = l->data;
f2e82732
BV
717 printf("- %s: %s (", o->id, o->desc);
718 for (ol = o->values; ol; ol = ol->next) {
719 val = g_variant_print(ol->data, FALSE);
720 printf("%s, ", val);
721 g_free(val);
722 }
2be182e6 723 val = g_variant_print(o->def, FALSE);
f2e82732 724 printf("default %s)\n", val);
2be182e6
BV
725 g_free(val);
726 }
1eb46be8
UH
727 } else {
728 printf("None.\n");
2be182e6
BV
729 }
730 if ((doc = srd_decoder_doc_get(dec))) {
731 printf("Documentation:\n%s\n",
732 doc[0] == '\n' ? doc + 1 : doc);
733 g_free(doc);
734 }
735 }
736
737 g_strfreev(pdtokens);
738}
739#endif
740
a8b4041a
BV
741void show_input(void)
742{
743 const struct sr_input_module *imod;
744 const struct sr_option **opts;
745 GSList *l;
746 int i;
747 char *s, **tok;
748
749 tok = g_strsplit(opt_input_format, ":", 0);
750 if (!tok[0] || !(imod = sr_input_find(tok[0])))
751 g_critical("Input module '%s' not found.", opt_input_format);
752
753 printf("ID: %s\nName: %s\n", sr_input_id_get(imod),
754 sr_input_name_get(imod));
755 printf("Description: %s\n", sr_input_description_get(imod));
756 if ((opts = sr_input_options_get(imod))) {
757 printf("Options:\n");
758 for (i = 0; opts[i]; i++) {
759 printf(" %s: %s", opts[i]->id, opts[i]->desc);
760 if (opts[i]->def) {
761 s = g_variant_print(opts[i]->def, FALSE);
762 printf(" (default %s", s);
763 g_free(s);
764 if (opts[i]->values) {
765 printf(", possible values ");
766 for (l = opts[i]->values; l; l = l->next) {
767 s = g_variant_print((GVariant *)l->data, FALSE);
768 printf("%s%s", s, l->next ? ", " : "");
769 g_free(s);
770 }
771 }
772 printf(")");
773 }
774 printf("\n");
775 }
776 sr_input_options_free(opts);
777 }
778 g_strfreev(tok);
779}
780
ad6520c4
BV
781void show_output(void)
782{
783 const struct sr_output_module *omod;
7c6a0420 784 const struct sr_option **opts;
ad6520c4 785 GSList *l;
7c6a0420 786 int i;
e786e625 787 char *s, **tok;
ad6520c4 788
e786e625
BV
789 tok = g_strsplit(opt_output_format, ":", 0);
790 if (!tok[0] || !(omod = sr_output_find(tok[0])))
ad6520c4
BV
791 g_critical("Output module '%s' not found.", opt_output_format);
792
793 printf("ID: %s\nName: %s\n", sr_output_id_get(omod),
794 sr_output_name_get(omod));
795 printf("Description: %s\n", sr_output_description_get(omod));
b61a8850 796 if ((opts = sr_output_options_get(omod))) {
ad6520c4 797 printf("Options:\n");
7c6a0420
BV
798 for (i = 0; opts[i]; i++) {
799 printf(" %s: %s", opts[i]->id, opts[i]->desc);
800 if (opts[i]->def) {
801 s = g_variant_print(opts[i]->def, FALSE);
ad6520c4
BV
802 printf(" (default %s", s);
803 g_free(s);
7c6a0420 804 if (opts[i]->values) {
ad6520c4 805 printf(", possible values ");
7c6a0420 806 for (l = opts[i]->values; l; l = l->next) {
ad6520c4
BV
807 s = g_variant_print((GVariant *)l->data, FALSE);
808 printf("%s%s", s, l->next ? ", " : "");
809 g_free(s);
810 }
811 }
812 printf(")");
813 }
814 printf("\n");
ad6520c4 815 }
b61a8850 816 sr_output_options_free(opts);
ad6520c4 817 }
e786e625 818 g_strfreev(tok);
ad6520c4
BV
819}
820