]> sigrok.org Git - sigrok-cli.git/blame - sigrok-cli.c
Update cmdline options to better reflect functionality
[sigrok-cli.git] / sigrok-cli.c
CommitLineData
43e5747a 1/*
630293b4 2 * This file is part of the sigrok-cli project.
43e5747a 3 *
110aa72a 4 * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
43e5747a
UH
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
de0a066e
BV
20#include "config.h"
21#ifdef HAVE_SRD
43e5747a 22#include <sigrokdecode.h> /* First, so we avoid a _POSIX_C_SOURCE warning. */
de0a066e 23#endif
43e5747a
UH
24#include <stdio.h>
25#include <stdlib.h>
26#include <unistd.h>
27#include <string.h>
28#include <time.h>
29#include <sys/time.h>
30#include <inttypes.h>
31#include <sys/types.h>
32#include <sys/stat.h>
33#include <errno.h>
34#include <glib.h>
35#include <glib/gstdio.h>
60ea8937 36#include <libsigrok/libsigrok.h>
43e5747a 37#include "sigrok-cli.h"
43e5747a
UH
38
39#define DEFAULT_OUTPUT_FORMAT "bits:width=64"
40
e2fe62cc
PS
41static struct sr_context *sr_ctx = NULL;
42
1999ae06 43static uint64_t limit_samples = 0;
ce48d892 44static uint64_t limit_frames = 0;
1999ae06
UH
45static struct sr_output_format *output_format = NULL;
46static int default_output_format = FALSE;
47static char *output_format_param = NULL;
de0a066e 48#ifdef HAVE_SRD
120f9ee7 49static GHashTable *pd_ann_visible = NULL;
de0a066e 50#endif
c27450ea 51static GByteArray *savebuf;
43e5747a
UH
52
53static gboolean opt_version = FALSE;
54static gint opt_loglevel = SR_LOG_WARN; /* Show errors+warnings per default. */
ea7741ee 55static gboolean opt_scan_devs = FALSE;
43e5747a
UH
56static gboolean opt_wait_trigger = FALSE;
57static gchar *opt_input_file = NULL;
58static gchar *opt_output_file = NULL;
9e2e414f 59static gchar *opt_drv = NULL;
ea7741ee 60static gchar *opt_config = NULL;
43e5747a
UH
61static gchar *opt_probes = NULL;
62static gchar *opt_triggers = NULL;
63static gchar *opt_pds = NULL;
de0a066e 64#ifdef HAVE_SRD
9f4a898e 65static gchar *opt_pd_stack = NULL;
b6bd032d 66static gchar *opt_pd_annotations = NULL;
de0a066e 67#endif
43e5747a 68static gchar *opt_input_format = NULL;
76ae913d 69static gchar *opt_output_format = NULL;
22981b2c 70static gchar *opt_show = NULL;
43e5747a
UH
71static gchar *opt_time = NULL;
72static gchar *opt_samples = NULL;
ce48d892 73static gchar *opt_frames = NULL;
43e5747a 74static gchar *opt_continuous = NULL;
2d73284e 75static gchar *opt_set = NULL;
43e5747a
UH
76
77static GOptionEntry optargs[] = {
03996962
BV
78 {"version", 'V', 0, G_OPTION_ARG_NONE, &opt_version,
79 "Show version and support list", NULL},
80 {"loglevel", 'l', 0, G_OPTION_ARG_INT, &opt_loglevel,
ea7741ee
BV
81 "Set loglevel (5 is most verbose)", NULL},
82 {"driver", 'd', 0, G_OPTION_ARG_STRING, &opt_drv,
83 "The driver to use", NULL},
84 {"config", 'c', 0, G_OPTION_ARG_STRING, &opt_config,
85 "Specify device configuration options", NULL},
03996962
BV
86 {"input-file", 'i', 0, G_OPTION_ARG_FILENAME, &opt_input_file,
87 "Load input from file", NULL},
88 {"input-format", 'I', 0, G_OPTION_ARG_STRING, &opt_input_format,
89 "Input format", NULL},
90 {"output-file", 'o', 0, G_OPTION_ARG_FILENAME, &opt_output_file,
91 "Save output to file", NULL},
92 {"output-format", 'O', 0, G_OPTION_ARG_STRING, &opt_output_format,
93 "Output format", NULL},
94 {"probes", 'p', 0, G_OPTION_ARG_STRING, &opt_probes,
95 "Probes to use", NULL},
96 {"triggers", 't', 0, G_OPTION_ARG_STRING, &opt_triggers,
97 "Trigger configuration", NULL},
98 {"wait-trigger", 'w', 0, G_OPTION_ARG_NONE, &opt_wait_trigger,
99 "Wait for trigger", NULL},
de0a066e 100#ifdef HAVE_SRD
ea7741ee 101 {"protocol-decoders", 'P', 0, G_OPTION_ARG_STRING, &opt_pds,
03996962 102 "Protocol decoders to run", NULL},
ea7741ee 103 {"protocol-decoder-stack", 'S', 0, G_OPTION_ARG_STRING, &opt_pd_stack,
03996962 104 "Protocol decoder stack", NULL},
b6bd032d
UH
105 {"protocol-decoder-annotations", 'A', 0, G_OPTION_ARG_STRING, &opt_pd_annotations,
106 "Protocol decoder annotation(s) to show", NULL},
de0a066e 107#endif
ea7741ee
BV
108 {"scan", 0, 0, G_OPTION_ARG_NONE, &opt_scan_devs,
109 "Scan for devices", NULL},
22981b2c
BV
110 {"show", 0, 0, G_OPTION_ARG_NONE, &opt_show,
111 "Show device detail", NULL},
03996962
BV
112 {"time", 0, 0, G_OPTION_ARG_STRING, &opt_time,
113 "How long to sample (ms)", NULL},
114 {"samples", 0, 0, G_OPTION_ARG_STRING, &opt_samples,
115 "Number of samples to acquire", NULL},
ce48d892
BV
116 {"frames", 0, 0, G_OPTION_ARG_STRING, &opt_frames,
117 "Number of frames to acquire", NULL},
03996962
BV
118 {"continuous", 0, 0, G_OPTION_ARG_NONE, &opt_continuous,
119 "Sample continuously", NULL},
2d73284e 120 {"set", 0, 0, G_OPTION_ARG_NONE, &opt_set, "Set device options only", NULL},
43e5747a
UH
121 {NULL, 0, 0, 0, NULL, NULL, NULL}
122};
123
9e2e414f 124
995713f4 125/* Convert driver options hash to GSList of struct sr_config. */
9e2e414f
BV
126static GSList *hash_to_hwopt(GHashTable *hash)
127{
995713f4
BV
128 const struct sr_config_info *srci;
129 struct sr_config *src;
9e2e414f
BV
130 GList *gl, *keys;
131 GSList *opts;
132 char *key, *value;
133
134 keys = g_hash_table_get_keys(hash);
135 opts = NULL;
136 for (gl = keys; gl; gl = gl->next) {
137 key = gl->data;
74b9bf0c 138 if (!(srci = sr_config_info_name_get(key))) {
9e2e414f
BV
139 g_critical("Unknown option %s", key);
140 return NULL;
141 }
995713f4
BV
142 src = g_try_malloc(sizeof(struct sr_config));
143 src->key = srci->key;
cfd3ec6e 144 value = g_hash_table_lookup(hash, key);
1616f663 145 src->data = g_variant_new_string(value);
995713f4 146 opts = g_slist_append(opts, src);
9e2e414f
BV
147 }
148 g_list_free(keys);
149
150 return opts;
151}
152
995713f4 153static void free_drvopts(struct sr_config *src)
a2853311 154{
1616f663 155 g_variant_unref(src->data);
995713f4 156 g_free(src);
a2853311
AG
157}
158
9e2e414f
BV
159static GSList *device_scan(void)
160{
161 struct sr_dev_driver **drivers, *driver;
162 GHashTable *drvargs;
163 GSList *drvopts, *devices, *tmpdevs, *l;
164 int i;
165 char *drvname;
166
167 if (opt_drv) {
168 drvargs = parse_generic_arg(opt_drv, TRUE);
169 drvname = g_strdup(g_hash_table_lookup(drvargs, "sigrok_key"));
170 g_hash_table_remove(drvargs, "sigrok_key");
171 driver = NULL;
172 drivers = sr_driver_list();
173 for (i = 0; drivers[i]; i++) {
174 if (strcmp(drivers[i]->name, drvname))
175 continue;
176 driver = drivers[i];
177 }
178 if (!driver) {
179 g_critical("Driver %s not found.", drvname);
180 return NULL;
181 }
182 g_free(drvname);
e2fe62cc 183 if (sr_driver_init(sr_ctx, driver) != SR_OK) {
9e2e414f
BV
184 g_critical("Failed to initialize driver.");
185 return NULL;
186 }
187 drvopts = NULL;
188 if (g_hash_table_size(drvargs) > 0)
189 if (!(drvopts = hash_to_hwopt(drvargs)))
190 /* Unknown options, already logged. */
191 return NULL;
192 devices = sr_driver_scan(driver, drvopts);
995713f4 193 g_slist_free_full(drvopts, (GDestroyNotify)free_drvopts);
9e2e414f
BV
194 } else {
195 /* No driver specified, let them all scan on their own. */
196 devices = NULL;
197 drivers = sr_driver_list();
198 for (i = 0; drivers[i]; i++) {
199 driver = drivers[i];
e2fe62cc 200 if (sr_driver_init(sr_ctx, driver) != SR_OK) {
9e2e414f
BV
201 g_critical("Failed to initialize driver.");
202 return NULL;
203 }
204 tmpdevs = sr_driver_scan(driver, NULL);
205 for (l = tmpdevs; l; l = l->next)
206 devices = g_slist_append(devices, l->data);
207 g_slist_free(tmpdevs);
208 }
209 }
210
211 return devices;
212}
213
43e5747a
UH
214static void show_version(void)
215{
a210c4cc 216 struct sr_dev_driver **drivers;
43e5747a
UH
217 struct sr_input_format **inputs;
218 struct sr_output_format **outputs;
43e5747a 219 int i;
de0a066e
BV
220#ifdef HAVE_SRD
221 struct srd_decoder *dec;
222 const GSList *l;
223#endif
43e5747a
UH
224
225 printf("sigrok-cli %s\n\n", VERSION);
15d920a9 226
4d167240
UH
227 printf("Using libsigrok %s (lib version %s).\n",
228 sr_package_version_string_get(), sr_lib_version_string_get());
de0a066e 229#ifdef HAVE_SRD
4d167240
UH
230 printf("Using libsigrokdecode %s (lib version %s).\n\n",
231 srd_package_version_string_get(), srd_lib_version_string_get());
de0a066e 232#endif
4d167240 233
43e5747a 234 printf("Supported hardware drivers:\n");
a214a2eb 235 drivers = sr_driver_list();
a210c4cc
UH
236 for (i = 0; drivers[i]; i++) {
237 printf(" %-20s %s\n", drivers[i]->name, drivers[i]->longname);
43e5747a
UH
238 }
239 printf("\n");
240
241 printf("Supported input formats:\n");
242 inputs = sr_input_list();
243 for (i = 0; inputs[i]; i++)
244 printf(" %-20s %s\n", inputs[i]->id, inputs[i]->description);
245 printf("\n");
246
247 printf("Supported output formats:\n");
248 outputs = sr_output_list();
249 for (i = 0; outputs[i]; i++)
250 printf(" %-20s %s\n", outputs[i]->id, outputs[i]->description);
251 printf("\n");
252
de0a066e 253#ifdef HAVE_SRD
15d920a9
BV
254 if (srd_init(NULL) == SRD_OK) {
255 printf("Supported protocol decoders:\n");
1b34803d
UH
256 srd_decoder_load_all();
257 for (l = srd_decoder_list(); l; l = l->next) {
15d920a9
BV
258 dec = l->data;
259 printf(" %-20s %s\n", dec->id, dec->longname);
20622036
UH
260 /* Print protocol description upon "-l 3" or higher. */
261 if (opt_loglevel >= SR_LOG_INFO)
262 printf(" %-20s %s\n", "", dec->desc);
15d920a9
BV
263 }
264 srd_exit();
43e5747a
UH
265 }
266 printf("\n");
de0a066e 267#endif
43e5747a
UH
268}
269
22981b2c 270static void print_dev_line(const struct sr_dev_inst *sdi)
43e5747a 271{
11b62b6f
BV
272 struct sr_probe *probe;
273 GSList *l;
fe6970ec
BV
274 GString *s;
275 GVariant *gvar;
43e5747a 276
fe6970ec
BV
277 s = g_string_sized_new(128);
278 g_string_assign(s, sdi->driver->name);
279 if (sr_config_get(sdi->driver, SR_CONF_CONN, &gvar, sdi) == SR_OK) {
280 g_string_append(s, ":conn=");
281 g_string_append(s, g_variant_get_string(gvar, NULL));
282 g_variant_unref(gvar);
283 }
284 g_string_append(s, " - ");
43e5747a 285 if (sdi->vendor && sdi->vendor[0])
fe6970ec 286 g_string_append_printf(s, "%s ", sdi->vendor);
43e5747a 287 if (sdi->model && sdi->model[0])
fe6970ec 288 g_string_append_printf(s, "%s ", sdi->model);
43e5747a 289 if (sdi->version && sdi->version[0])
fe6970ec 290 g_string_append_printf(s, "%s ", sdi->version);
11b62b6f
BV
291 if (sdi->probes) {
292 if (g_slist_length(sdi->probes) == 1) {
293 probe = sdi->probes->data;
fe6970ec 294 g_string_append_printf(s, "with 1 probe: %s", probe->name);
11b62b6f 295 } else {
fe6970ec 296 g_string_append_printf(s, "with %d probes:", g_slist_length(sdi->probes));
11b62b6f
BV
297 for (l = sdi->probes; l; l = l->next) {
298 probe = l->data;
fe6970ec 299 g_string_append_printf(s, " %s", probe->name);
11b62b6f
BV
300 }
301 }
4d326ca1 302 }
fe6970ec
BV
303 g_string_append_printf(s, "\n");
304 printf("%s", s->str);
305 g_string_free(s, TRUE);
306
43e5747a
UH
307}
308
1e0f9ed9 309static void show_dev_list(void)
43e5747a 310{
22981b2c
BV
311 struct sr_dev_inst *sdi;
312 GSList *devices, *l;
43e5747a 313
22981b2c 314 if (!(devices = device_scan()))
43e5747a
UH
315 return;
316
22981b2c
BV
317 printf("The following devices were found:\n");
318 for (l = devices; l; l = l->next) {
319 sdi = l->data;
320 print_dev_line(sdi);
43e5747a 321 }
22981b2c
BV
322 g_slist_free(devices);
323
43e5747a
UH
324}
325
1e0f9ed9 326static void show_dev_detail(void)
43e5747a 327{
22981b2c 328 struct sr_dev_inst *sdi;
995713f4 329 const struct sr_config_info *srci;
22981b2c 330 GSList *devices;
1616f663
BV
331 GVariant *gvar_opts, *gvar_dict, *gvar_list, *gvar;
332 gsize num_opts, num_elements;
4e0db44d 333 const uint64_t *uint64, p, q;
e07340ad 334 const int32_t *opts;
1616f663
BV
335 unsigned int num_devices, tmp_bool, o, i;
336 char *s;
9c9c1080 337 const char *charopts, **stropts;
43e5747a 338
22981b2c
BV
339 if (!(devices = device_scan())) {
340 g_critical("No devices found.");
43e5747a
UH
341 return;
342 }
343
22981b2c
BV
344 num_devices = g_slist_length(devices);
345 if (num_devices > 1) {
41d7fb23 346 g_critical("%d devices found. Use --list-devices to show them, "
d79d4806 347 "and select one to show.", num_devices);
41d7fb23
BV
348 return;
349 }
22981b2c 350
41d7fb23 351 sdi = devices->data;
22981b2c 352 print_dev_line(sdi);
43e5747a 353
0687f23d
BV
354 if (sr_dev_open(sdi) != SR_OK) {
355 g_critical("Failed to open device.");
8e8827f4
BV
356 return;
357 }
358
1616f663
BV
359 if ((sr_config_list(sdi->driver, SR_CONF_SCAN_OPTIONS, &gvar_opts,
360 NULL) == SR_OK)) {
361 opts = g_variant_get_fixed_array(gvar_opts, &num_elements,
362 sizeof(int32_t));
22981b2c 363 printf("Supported driver options:\n");
1616f663
BV
364 for (i = 0; i < num_elements; i++) {
365 if (!(srci = sr_config_info_get(opts[i])))
22981b2c 366 continue;
995713f4 367 printf(" %s\n", srci->id);
22981b2c 368 }
1616f663 369 g_variant_unref(gvar_opts);
22981b2c
BV
370 }
371
1616f663
BV
372 if ((sr_config_list(sdi->driver, SR_CONF_DEVICE_OPTIONS, &gvar_opts,
373 sdi) != SR_OK))
22981b2c
BV
374 /* Driver supports no device instance options. */
375 return;
376
1616f663
BV
377 printf("Supported device options:\n");
378 opts = g_variant_get_fixed_array(gvar_opts, &num_opts, sizeof(int32_t));
379 for (o = 0; o < num_opts; o++) {
380 if (!(srci = sr_config_info_get(opts[o])))
43e5747a
UH
381 continue;
382
1616f663
BV
383 if (srci->key == SR_CONF_TRIGGER_TYPE) {
384 if (sr_config_list(sdi->driver, srci->key, &gvar,
385 sdi) != SR_OK) {
386 printf("\n");
387 continue;
388 }
389 charopts = g_variant_get_string(gvar, NULL);
390 printf(" Supported triggers: ");
391 while (*charopts) {
392 printf("%c ", *charopts);
393 charopts++;
394 }
395 printf("\n");
396 g_variant_unref(gvar);
43e5747a 397
1616f663 398 } else if (srci->key == SR_CONF_PATTERN_MODE) {
c8db7172 399 /* Pattern generator modes */
995713f4 400 printf(" %s", srci->id);
1616f663
BV
401 if (sr_config_list(sdi->driver, srci->key, &gvar,
402 sdi) == SR_OK) {
0a56f4ec 403 printf(" - supported patterns:\n");
1e1316b3
BV
404 stropts = g_variant_get_strv(gvar, &num_elements);
405 for (i = 0; i < num_elements; i++)
9c9c1080 406 printf(" %s\n", stropts[i]);
1616f663 407 g_variant_unref(gvar);
9c9c1080 408 } else {
43e5747a 409 printf("\n");
43e5747a 410 }
c8db7172 411
b5c63de9 412 } else if (srci->key == SR_CONF_SAMPLERATE) {
43e5747a 413 /* Supported samplerates */
995713f4 414 printf(" %s", srci->id);
d2f671dc 415 if (sr_config_list(sdi->driver, SR_CONF_SAMPLERATE,
1616f663 416 &gvar_dict, sdi) != SR_OK) {
43e5747a
UH
417 printf("\n");
418 continue;
419 }
1616f663
BV
420 if ((gvar_list = g_variant_lookup_value(gvar_dict,
421 "samplerates", G_VARIANT_TYPE("at")))) {
4e0db44d 422 uint64 = g_variant_get_fixed_array(gvar_list,
1616f663
BV
423 &num_elements, sizeof(uint64_t));
424 printf(" - supported samplerates:\n");
425 for (i = 0; i < num_elements; i++)
4e0db44d 426 printf(" %s\n", sr_samplerate_string(uint64[i]));
1616f663
BV
427 } if ((gvar_list = g_variant_lookup_value(gvar_dict,
428 "samplerate-steps", G_VARIANT_TYPE("at")))) {
4e0db44d 429 uint64 = g_variant_get_fixed_array(gvar_list,
1616f663 430 &num_elements, sizeof(uint64_t));
43e5747a 431 /* low */
4e0db44d 432 if (!(s = sr_samplerate_string(uint64[0])))
43e5747a
UH
433 continue;
434 printf(" (%s", s);
c2c4a0de 435 g_free(s);
43e5747a 436 /* high */
4e0db44d 437 if (!(s = sr_samplerate_string(uint64[1])))
43e5747a
UH
438 continue;
439 printf(" - %s", s);
c2c4a0de 440 g_free(s);
43e5747a 441 /* step */
4e0db44d 442 if (!(s = sr_samplerate_string(uint64[2])))
43e5747a
UH
443 continue;
444 printf(" in steps of %s)\n", s);
c2c4a0de 445 g_free(s);
1e1316b3 446 g_variant_unref(gvar_list);
43e5747a 447 }
1616f663 448 g_variant_unref(gvar_dict);
c8db7172 449
b5c63de9 450 } else if (srci->key == SR_CONF_BUFFERSIZE) {
c8db7172 451 /* Supported buffer sizes */
995713f4 452 printf(" %s", srci->id);
2777cbd2 453 if (sr_config_list(sdi->driver, SR_CONF_BUFFERSIZE,
1e1316b3 454 &gvar_list, sdi) != SR_OK) {
c8db7172
BV
455 printf("\n");
456 continue;
457 }
4e0db44d 458 uint64 = g_variant_get_fixed_array(gvar_list,
1e1316b3 459 &num_elements, sizeof(uint64_t));
c8db7172 460 printf(" - supported buffer sizes:\n");
1e1316b3 461 for (i = 0; i < num_elements; i++)
4e0db44d 462 printf(" %"PRIu64"\n", uint64[i]);
1e1316b3 463 g_variant_unref(gvar_list);
c8db7172 464
b5c63de9 465 } else if (srci->key == SR_CONF_TIMEBASE) {
c8db7172 466 /* Supported time bases */
995713f4 467 printf(" %s", srci->id);
2bedb9bf 468 if (sr_config_list(sdi->driver, SR_CONF_TIMEBASE,
1e1316b3 469 &gvar_list, sdi) != SR_OK) {
c8db7172
BV
470 printf("\n");
471 continue;
472 }
473 printf(" - supported time bases:\n");
e07340ad
BV
474 num_elements = g_variant_n_children(gvar_list);
475 for (i = 0; i < num_elements; i++) {
476 gvar = g_variant_get_child_value(gvar_list, i);
477 g_variant_get(gvar, "(tt)", &p, &q);
478 s = sr_period_string(p * q);
479 printf(" %s\n", s);
480 g_free(s);
481 }
1e1316b3 482 g_variant_unref(gvar_list);
c8db7172 483
b5c63de9 484 } else if (srci->key == SR_CONF_TRIGGER_SOURCE) {
c8db7172 485 /* Supported trigger sources */
995713f4 486 printf(" %s", srci->id);
d3d7e30a 487 if (sr_config_list(sdi->driver, SR_CONF_TRIGGER_SOURCE,
1e1316b3 488 &gvar, sdi) != SR_OK) {
c8db7172
BV
489 printf("\n");
490 continue;
491 }
492 printf(" - supported trigger sources:\n");
1e1316b3
BV
493 stropts = g_variant_get_strv(gvar, &num_elements);
494 for (i = 0; i < num_elements; i++)
c8db7172 495 printf(" %s\n", stropts[i]);
1e1316b3 496 g_variant_unref(gvar);
c8db7172 497
b5c63de9 498 } else if (srci->key == SR_CONF_FILTER) {
e54290bd 499 /* Supported filters */
995713f4 500 printf(" %s", srci->id);
b4bd48e9 501 if (sr_config_list(sdi->driver, SR_CONF_FILTER,
1e1316b3 502 &gvar, sdi) != SR_OK) {
2850fca1
BV
503 printf("\n");
504 continue;
505 }
506 printf(" - supported filter targets:\n");
1e1316b3
BV
507 stropts = g_variant_get_strv(gvar, &num_elements);
508 for (i = 0; i < num_elements; i++)
2850fca1 509 printf(" %s\n", stropts[i]);
1e1316b3 510 g_variant_unref(gvar);
2850fca1 511
b5c63de9 512 } else if (srci->key == SR_CONF_VDIV) {
8f3b8464 513 /* Supported volts/div values */
995713f4 514 printf(" %s", srci->id);
9b0b39f6 515 if (sr_config_list(sdi->driver, SR_CONF_VDIV,
1e1316b3 516 &gvar_list, sdi) != SR_OK) {
8f3b8464
BV
517 printf("\n");
518 continue;
519 }
520 printf(" - supported volts/div:\n");
e07340ad
BV
521 num_elements = g_variant_n_children(gvar_list);
522 for (i = 0; i < num_elements; i++) {
523 gvar = g_variant_get_child_value(gvar_list, i);
524 g_variant_get(gvar, "(tt)", &p, &q);
525 s = sr_voltage_string(p, q);
526 printf(" %s\n", s);
527 g_free(s);
528 }
1e1316b3 529 g_variant_unref(gvar_list);
8f3b8464 530
b5c63de9 531 } else if (srci->key == SR_CONF_COUPLING) {
498f9167 532 /* Supported coupling settings */
995713f4 533 printf(" %s", srci->id);
3ea086fc 534 if (sr_config_list(sdi->driver, SR_CONF_COUPLING,
1e1316b3 535 &gvar, sdi) != SR_OK) {
498f9167
BV
536 printf("\n");
537 continue;
538 }
539 printf(" - supported coupling options:\n");
1e1316b3
BV
540 stropts = g_variant_get_strv(gvar, &num_elements);
541 for (i = 0; i < num_elements; i++)
498f9167 542 printf(" %s\n", stropts[i]);
1e1316b3 543 g_variant_unref(gvar);
498f9167 544
8e8827f4
BV
545 } else if (srci->key == SR_CONF_DATALOG) {
546 /* Turning on/off internal data logging. */
547 printf(" %s\t(on/off", srci->id);
548 if (sr_config_get(sdi->driver, SR_CONF_DATALOG,
1616f663
BV
549 &gvar, sdi) == SR_OK) {
550 tmp_bool = g_variant_get_boolean(gvar);
551 printf(", currently %s", tmp_bool ? "on" : "off");
1e1316b3 552 g_variant_unref(gvar);
8e8827f4
BV
553 }
554 printf(")\n");
43e5747a 555 } else {
c8db7172 556 /* Everything else */
995713f4 557 printf(" %s\n", srci->id);
43e5747a
UH
558 }
559 }
1616f663 560 g_variant_unref(gvar_opts);
22981b2c 561
0687f23d 562 sr_dev_close(sdi);
8e8827f4 563
43e5747a
UH
564}
565
de0a066e 566#ifdef HAVE_SRD
71b1ea4e
BV
567static void show_pd_detail(void)
568{
569 GSList *l;
570 struct srd_decoder *dec;
d142a3f7
BV
571 struct srd_decoder_option *o;
572 char **pdtokens, **pdtok, *optsep, **ann, *val, *doc;
e9ff6974 573 struct srd_probe *p;
71b1ea4e
BV
574
575 pdtokens = g_strsplit(opt_pds, ",", -1);
576 for (pdtok = pdtokens; *pdtok; pdtok++) {
d142a3f7
BV
577 /* Strip options. */
578 if ((optsep = strchr(*pdtok, ':')))
579 *optsep = '\0';
2358775c 580 if (!(dec = srd_decoder_get_by_id(*pdtok))) {
f8ccd825 581 g_critical("Protocol decoder %s not found.", *pdtok);
71b1ea4e
BV
582 return;
583 }
584 printf("ID: %s\nName: %s\nLong name: %s\nDescription: %s\n",
585 dec->id, dec->name, dec->longname, dec->desc);
586 printf("License: %s\n", dec->license);
e9ff6974 587 printf("Annotations:\n");
71b1ea4e 588 if (dec->annotations) {
71b1ea4e
BV
589 for (l = dec->annotations; l; l = l->next) {
590 ann = l->data;
591 printf("- %s\n %s\n", ann[0], ann[1]);
592 }
e9ff6974
UH
593 } else {
594 printf("None.\n");
595 }
e9ff6974
UH
596 printf("Required probes:\n");
597 if (dec->probes) {
598 for (l = dec->probes; l; l = l->next) {
599 p = l->data;
600 printf("- %s (%s): %s\n",
601 p->name, p->id, p->desc);
602 }
603 } else {
604 printf("None.\n");
605 }
606 printf("Optional probes:\n");
607 if (dec->opt_probes) {
608 for (l = dec->opt_probes; l; l = l->next) {
609 p = l->data;
610 printf("- %s (%s): %s\n",
611 p->name, p->id, p->desc);
612 }
613 } else {
614 printf("None.\n");
71b1ea4e 615 }
d142a3f7
BV
616 if (dec->options) {
617 printf("Options:\n");
618 for (l = dec->options; l; l = l->next) {
619 o = l->data;
620 val = g_variant_print(o->def, FALSE);
621 printf("- %s: %s (default %s)\n", o->id, o->desc, val);
622 g_free(val);
623 }
624 }
4359a4da 625 if ((doc = srd_decoder_doc_get(dec))) {
e9ff6974
UH
626 printf("Documentation:\n%s\n",
627 doc[0] == '\n' ? doc + 1 : doc);
71b1ea4e
BV
628 g_free(doc);
629 }
630 }
631
632 g_strfreev(pdtokens);
71b1ea4e 633}
de0a066e 634#endif
71b1ea4e 635
c27450ea
BV
636static GArray *get_enabled_logic_probes(const struct sr_dev_inst *sdi)
637{
638 struct sr_probe *probe;
639 GArray *probes;
640 GSList *l;
641
642 probes = g_array_new(FALSE, FALSE, sizeof(int));
643 for (l = sdi->probes; l; l = l->next) {
644 probe = l->data;
645 if (probe->type != SR_PROBE_LOGIC)
646 continue;
647 if (probe->enabled != TRUE)
648 continue;
649 g_array_append_val(probes, probe->index);
650 }
651
652 return probes;
653}
654
37d5ccc1 655static void datafeed_in(const struct sr_dev_inst *sdi,
74f6195b 656 const struct sr_datafeed_packet *packet, void *cb_data)
43e5747a 657{
7e97afa0 658 const struct sr_datafeed_meta *meta;
c27450ea
BV
659 const struct sr_datafeed_logic *logic;
660 const struct sr_datafeed_analog *analog;
7e97afa0 661 struct sr_config *src;
43e5747a 662 static struct sr_output *o = NULL;
c27450ea 663 static GArray *logic_probelist = NULL;
43e5747a
UH
664 static uint64_t received_samples = 0;
665 static int unitsize = 0;
666 static int triggered = 0;
667 static FILE *outfile = NULL;
1616f663
BV
668 GSList *l;
669 GString *out;
670 GVariant *gvar;
c27450ea 671 int sample_size, ret;
1616f663 672 uint64_t samplerate, output_len, filter_out_len;
8de01efe 673 uint8_t *output_buf, *filter_out;
43e5747a 674
74f6195b
ML
675 (void) cb_data;
676
43e5747a
UH
677 /* If the first packet to come in isn't a header, don't even try. */
678 if (packet->type != SR_DF_HEADER && o == NULL)
679 return;
680
681 sample_size = -1;
682 switch (packet->type) {
683 case SR_DF_HEADER:
8170b8ea 684 g_debug("cli: Received SR_DF_HEADER");
43e5747a 685 /* Initialize the output module. */
c2c4a0de 686 if (!(o = g_try_malloc(sizeof(struct sr_output)))) {
8170b8ea 687 g_critical("Output module malloc failed.");
43e5747a
UH
688 exit(1);
689 }
690 o->format = output_format;
37d5ccc1 691 o->sdi = (struct sr_dev_inst *)sdi;
43e5747a
UH
692 o->param = output_format_param;
693 if (o->format->init) {
694 if (o->format->init(o) != SR_OK) {
8170b8ea 695 g_critical("Output format initialization failed.");
43e5747a
UH
696 exit(1);
697 }
698 }
c27450ea
BV
699
700 /* Prepare non-stdout output. */
701 outfile = stdout;
702 if (opt_output_file) {
703 if (default_output_format) {
704 /* output file is in session format, so we'll
705 * keep a copy of everything as it comes in
706 * and save from there after the session. */
707 outfile = NULL;
708 savebuf = g_byte_array_new();
709 } else {
710 /* saving to a file in whatever format was set
711 * with --format, so all we need is a filehandle */
712 outfile = g_fopen(opt_output_file, "wb");
713 }
714 }
715
716 /* Prepare for logic data. */
717 logic_probelist = get_enabled_logic_probes(sdi);
718 /* How many bytes we need to store the packed samples. */
719 unitsize = (logic_probelist->len + 7) / 8;
720
de0a066e 721#ifdef HAVE_SRD
c27450ea 722 if (opt_pds && logic_probelist->len) {
9e0e651f 723 if (sr_config_get(sdi->driver, SR_CONF_SAMPLERATE,
1616f663 724 &gvar, sdi) != SR_OK) {
c27450ea
BV
725 g_critical("Unable to initialize protocol "
726 "decoders: no samplerate found.");
727 break;
728 }
1616f663
BV
729 samplerate = g_variant_get_uint64(gvar);
730 g_variant_unref(gvar);
731 srd_session_start(logic_probelist->len, unitsize, samplerate);
c27450ea 732 }
de0a066e 733#endif
43e5747a 734 break;
53993299 735
7e97afa0
BV
736 case SR_DF_META:
737 g_debug("cli: received SR_DF_META");
738 meta = packet->payload;
739 for (l = meta->config; l; l = l->next) {
740 src = l->data;
741 switch (src->key) {
b5c63de9 742 case SR_CONF_SAMPLERATE:
1616f663
BV
743 samplerate = g_variant_get_uint64(src->data);
744 g_debug("cli: got samplerate %"PRIu64" Hz", samplerate);
5570e0f2
BV
745 break;
746 case SR_CONF_SAMPLE_INTERVAL:
1616f663
BV
747 samplerate = g_variant_get_uint64(src->data);
748 g_debug("cli: got sample interval %"PRIu64" ms", samplerate);
7e97afa0
BV
749 break;
750 default:
751 /* Unknown metadata is not an error. */
752 break;
753 }
754 }
755 break;
756
43e5747a 757 case SR_DF_TRIGGER:
8170b8ea 758 g_debug("cli: received SR_DF_TRIGGER");
43e5747a
UH
759 if (o->format->event)
760 o->format->event(o, SR_DF_TRIGGER, &output_buf,
761 &output_len);
762 triggered = 1;
763 break;
53993299 764
43e5747a
UH
765 case SR_DF_LOGIC:
766 logic = packet->payload;
b46c4414 767 g_message("cli: received SR_DF_LOGIC, %"PRIu64" bytes", logic->length);
53993299
BV
768 sample_size = logic->unitsize;
769 if (logic->length == 0)
770 break;
771
772 /* Don't store any samples until triggered. */
773 if (opt_wait_trigger && !triggered)
774 break;
43e5747a 775
53993299
BV
776 if (limit_samples && received_samples >= limit_samples)
777 break;
43e5747a 778
53993299 779 ret = sr_filter_probes(sample_size, unitsize, logic_probelist,
f8ccd825
BV
780 logic->data, logic->length,
781 &filter_out, &filter_out_len);
53993299
BV
782 if (ret != SR_OK)
783 break;
43e5747a 784
c27450ea 785 /* What comes out of the filter is guaranteed to be packed into the
53993299
BV
786 * minimum size needed to support the number of samples at this sample
787 * size. however, the driver may have submitted too much -- cut off
788 * the buffer of the last packet according to the sample limit.
789 */
790 if (limit_samples && (received_samples + logic->length / sample_size >
791 limit_samples * sample_size))
792 filter_out_len = limit_samples * sample_size - received_samples;
43e5747a 793
c27450ea
BV
794 if (opt_output_file && default_output_format) {
795 /* Saving to a session file. */
796 g_byte_array_append(savebuf, filter_out, filter_out_len);
53993299 797 } else {
c27450ea 798 if (opt_pds) {
de0a066e 799#ifdef HAVE_SRD
c27450ea
BV
800 if (srd_session_send(received_samples, (uint8_t*)filter_out,
801 filter_out_len) != SRD_OK)
802 sr_session_stop();
de0a066e 803#endif
c27450ea
BV
804 } else {
805 output_len = 0;
806 if (o->format->data && packet->type == o->format->df_type)
807 o->format->data(o, filter_out, filter_out_len,
808 &output_buf, &output_len);
a172e2a0 809 if (output_len) {
c27450ea
BV
810 fwrite(output_buf, 1, output_len, outfile);
811 fflush(outfile);
812 g_free(output_buf);
813 }
53993299
BV
814 }
815 }
53993299 816 g_free(filter_out);
43e5747a 817
c27450ea 818 received_samples += logic->length / sample_size;
53993299
BV
819 break;
820
821 case SR_DF_ANALOG:
822 analog = packet->payload;
823 g_message("cli: received SR_DF_ANALOG, %d samples", analog->num_samples);
824 if (analog->num_samples == 0)
825 break;
826
827 if (limit_samples && received_samples >= limit_samples)
828 break;
829
48f71481
BV
830 if (o->format->data && packet->type == o->format->df_type) {
831 o->format->data(o, (const uint8_t *)analog->data,
832 analog->num_samples * sizeof(float),
833 &output_buf, &output_len);
834 if (output_buf) {
835 fwrite(output_buf, 1, output_len, outfile);
e09810e9 836 fflush(outfile);
48f71481 837 g_free(output_buf);
c8db7172 838 }
53993299
BV
839 }
840
53993299 841 received_samples += analog->num_samples;
ce48d892
BV
842 break;
843
844 case SR_DF_FRAME_BEGIN:
48f71481
BV
845 g_debug("cli: received SR_DF_FRAME_BEGIN");
846 if (o->format->event) {
847 o->format->event(o, SR_DF_FRAME_BEGIN, &output_buf,
848 &output_len);
849 if (output_buf) {
850 fwrite(output_buf, 1, output_len, outfile);
e09810e9 851 fflush(outfile);
48f71481
BV
852 g_free(output_buf);
853 }
854 }
ce48d892
BV
855 break;
856
857 case SR_DF_FRAME_END:
48f71481
BV
858 g_debug("cli: received SR_DF_FRAME_END");
859 if (o->format->event) {
860 o->format->event(o, SR_DF_FRAME_END, &output_buf,
861 &output_len);
862 if (output_buf) {
863 fwrite(output_buf, 1, output_len, outfile);
e09810e9 864 fflush(outfile);
48f71481
BV
865 g_free(output_buf);
866 }
867 }
ce48d892 868 break;
53993299
BV
869
870 default:
c27450ea 871 break;
43e5747a
UH
872 }
873
644bb1e8
BV
874 if (o && o->format->receive) {
875 if (o->format->receive(o, sdi, packet, &out) == SR_OK && out) {
05e7fa2c
BV
876 fwrite(out->str, 1, out->len, outfile);
877 fflush(outfile);
644bb1e8 878 g_string_free(out, TRUE);
05e7fa2c
BV
879 }
880 }
881
644bb1e8 882 /* SR_DF_END needs to be handled after the output module's receive()
bea29a44
BV
883 * is called, so it can properly clean up that module etc. */
884 if (packet->type == SR_DF_END) {
885 g_debug("cli: Received SR_DF_END");
886
887 if (o->format->event) {
888 o->format->event(o, SR_DF_END, &output_buf, &output_len);
889 if (output_buf) {
890 if (outfile)
891 fwrite(output_buf, 1, output_len, outfile);
892 g_free(output_buf);
893 output_len = 0;
894 }
895 }
896
897 if (limit_samples && received_samples < limit_samples)
898 g_warning("Device only sent %" PRIu64 " samples.",
899 received_samples);
900
901 if (opt_continuous)
902 g_warning("Device stopped after %" PRIu64 " samples.",
903 received_samples);
904
905 g_array_free(logic_probelist, TRUE);
906
907 if (o->format->cleanup)
908 o->format->cleanup(o);
909 g_free(o);
910 o = NULL;
911
912 if (outfile && outfile != stdout)
913 fclose(outfile);
914
915 if (opt_output_file && default_output_format) {
916 if (sr_session_save(opt_output_file, sdi, savebuf->data,
917 unitsize, savebuf->len / unitsize) != SR_OK)
918 g_critical("Failed to save session.");
919 g_byte_array_free(savebuf, FALSE);
920 }
921 }
922
43e5747a
UH
923}
924
de0a066e 925#ifdef HAVE_SRD
d142a3f7
BV
926static int opts_to_gvar(struct srd_decoder *dec, GHashTable *hash,
927 GHashTable **options)
928{
929 struct srd_decoder_option *o;
930 GSList *optl;
931 GVariant *gvar;
932 gint64 val_int;
933 int ret;
934 char *val_str, *conv;
935
936 ret = TRUE;
937 *options = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
938 (GDestroyNotify)g_variant_unref);
939
940 for (optl = dec->options; optl; optl = optl->next) {
941 o = optl->data;
942 if (!(val_str = g_hash_table_lookup(hash, o->id)))
943 /* Not specified. */
944 continue;
945 if (g_variant_is_of_type(o->def, G_VARIANT_TYPE_STRING)) {
946 gvar = g_variant_new_string(val_str);
947 } else if (g_variant_is_of_type(o->def, G_VARIANT_TYPE_INT64)) {
948 val_int = strtoll(val_str, &conv, 10);
949 if (!conv || conv == val_str) {
950 g_critical("Protocol decoder '%s' option '%s' "
951 "requires a number.", dec->name, o->id);
952 ret = FALSE;
953 break;
954 }
955 gvar = g_variant_new_int64(val_int);
956 } else {
957 g_critical("Unsupported type for option '%s' (%s)",
958 o->id, g_variant_get_type_string(o->def));
959 ret = FALSE;
960 break;
961 }
962 g_variant_ref_sink(gvar);
963 g_hash_table_insert(*options, g_strdup(o->id), gvar);
964 g_hash_table_remove(hash, o->id);
965 }
966
967 return ret;
968}
969
970static int probes_to_gvar(struct srd_decoder *dec, GHashTable *hash,
971 GHashTable **probes)
972{
973 struct srd_probe *p;
974 GSList *all_probes, *l;
975 GVariant *gvar;
976 gint32 val_int;
977 int ret;
978 char *val_str, *conv;
979
980 ret = TRUE;
981 *probes = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
982 (GDestroyNotify)g_variant_unref);
983
984 all_probes = g_slist_copy(dec->probes);
985 all_probes = g_slist_concat(all_probes, dec->opt_probes);
986 for (l = all_probes; l; l = l->next) {
987 p = l->data;
988 if (!(val_str = g_hash_table_lookup(hash, p->id)))
989 /* Not specified. */
990 continue;
991 val_int = strtoll(val_str, &conv, 10);
992 if (!conv || conv == val_str) {
993 g_critical("Protocol decoder '%s' probes '%s' "
994 "is not a number.", dec->name, p->id);
995 ret = FALSE;
996 break;
997 }
998 gvar = g_variant_new_int32(val_int);
999 g_variant_ref_sink(gvar);
1000 g_hash_table_insert(*probes, g_strdup(p->id), gvar);
1001 g_hash_table_remove(hash, p->id);
1002 }
1003 g_slist_free(all_probes);
1004
1005 return ret;
1006}
1007
9f4a898e
BV
1008/* Register the given PDs for this session.
1009 * Accepts a string of the form: "spi:sck=3:sdata=4,spi:sck=3:sdata=5"
43e5747a
UH
1010 * That will instantiate two SPI decoders on the clock but different data
1011 * lines.
1012 */
1e0f9ed9 1013static int register_pds(struct sr_dev *dev, const char *pdstring)
43e5747a 1014{
d142a3f7
BV
1015 struct srd_decoder *dec;
1016 GHashTable *pd_opthash, *options, *probes;
1017 GList *leftover, *l;
878e90d9 1018 struct srd_decoder_inst *di;
445950d3 1019 int ret;
a1418b73 1020 char **pdtokens, **pdtok, *pd_name;
43e5747a 1021
1e0f9ed9 1022 (void)dev;
43e5747a 1023
445950d3 1024 ret = 0;
120f9ee7
BV
1025 pd_ann_visible = g_hash_table_new_full(g_str_hash, g_int_equal,
1026 g_free, NULL);
a1418b73 1027 pd_name = NULL;
d142a3f7 1028 pd_opthash = options = probes = NULL;
120f9ee7 1029 pdtokens = g_strsplit(pdstring, ",", 0);
43e5747a 1030 for (pdtok = pdtokens; *pdtok; pdtok++) {
63bb454c 1031 if (!(pd_opthash = parse_generic_arg(*pdtok, TRUE))) {
8170b8ea 1032 g_critical("Invalid protocol decoder option '%s'.", *pdtok);
d142a3f7 1033 break;
43e5747a 1034 }
a1418b73 1035
9720f23a
BV
1036 pd_name = g_strdup(g_hash_table_lookup(pd_opthash, "sigrok_key"));
1037 g_hash_table_remove(pd_opthash, "sigrok_key");
2358775c 1038 if (srd_decoder_load(pd_name) != SRD_OK) {
8170b8ea 1039 g_critical("Failed to load protocol decoder %s.", pd_name);
445950d3 1040 ret = 1;
d142a3f7
BV
1041 break;
1042 }
1043 dec = srd_decoder_get_by_id(pd_name);
1044
1045 /* Convert decoder option and probe values to GVariant. */
1046 if (!opts_to_gvar(dec, pd_opthash, &options)) {
1047 ret = 1;
1048 break;
1049 }
1050 if (!probes_to_gvar(dec, pd_opthash, &probes)) {
1051 ret = 1;
1052 break;
15d920a9 1053 }
d142a3f7
BV
1054 if (g_hash_table_size(pd_opthash) > 0) {
1055 leftover = g_hash_table_get_keys(pd_opthash);
1056 for (l = leftover; l; l = l->next)
1057 g_critical("Unknown option or probe '%s'", (char *)l->data);
1058 g_list_free(leftover);
1059 break;
1060 }
1061
1062 if (!(di = srd_inst_new(pd_name, options))) {
8170b8ea 1063 g_critical("Failed to instantiate protocol decoder %s.", pd_name);
445950d3 1064 ret = 1;
d142a3f7 1065 break;
43e5747a 1066 }
120f9ee7
BV
1067
1068 /* If no annotation list was specified, add them all in now.
1069 * This will be pared down later to leave only the last PD
1070 * in the stack.
1071 */
b6bd032d
UH
1072 if (!opt_pd_annotations)
1073 g_hash_table_insert(pd_ann_visible,
1074 g_strdup(di->inst_id), NULL);
a1418b73 1075
d142a3f7
BV
1076 /* Remap the probes if needed. */
1077 if (srd_inst_probe_set_all(di, probes) != SRD_OK) {
445950d3 1078 ret = 1;
d142a3f7 1079 break;
445950d3 1080 }
67ce0d27 1081 }
43e5747a 1082
43e5747a 1083 g_strfreev(pdtokens);
9720f23a
BV
1084 if (pd_opthash)
1085 g_hash_table_destroy(pd_opthash);
d142a3f7
BV
1086 if (options)
1087 g_hash_table_destroy(options);
1088 if (probes)
1089 g_hash_table_destroy(probes);
a1418b73
BV
1090 if (pd_name)
1091 g_free(pd_name);
43e5747a 1092
445950d3 1093 return ret;
43e5747a
UH
1094}
1095
120f9ee7
BV
1096int setup_pd_stack(void)
1097{
1098 struct srd_decoder_inst *di_from, *di_to;
1099 int ret, i;
41e915a6 1100 char **pds, **ids;
120f9ee7
BV
1101
1102 /* Set up the protocol decoder stack. */
1103 pds = g_strsplit(opt_pds, ",", 0);
1104 if (g_strv_length(pds) > 1) {
1105 if (opt_pd_stack) {
1106 /* A stack setup was specified, use that. */
1107 g_strfreev(pds);
1108 pds = g_strsplit(opt_pd_stack, ",", 0);
1109 if (g_strv_length(pds) < 2) {
1110 g_strfreev(pds);
1111 g_critical("Specify at least two protocol decoders to stack.");
1112 return 1;
1113 }
1114 }
1115
41e915a6
BV
1116 /* First PD goes at the bottom of the stack. */
1117 ids = g_strsplit(pds[0], ":", 0);
1118 if (!(di_from = srd_inst_find_by_id(ids[0]))) {
1119 g_strfreev(ids);
120f9ee7
BV
1120 g_critical("Cannot stack protocol decoder '%s': "
1121 "instance not found.", pds[0]);
1122 return 1;
1123 }
41e915a6
BV
1124 g_strfreev(ids);
1125
1126 /* Every subsequent PD goes on top. */
120f9ee7 1127 for (i = 1; pds[i]; i++) {
41e915a6
BV
1128 ids = g_strsplit(pds[i], ":", 0);
1129 if (!(di_to = srd_inst_find_by_id(ids[0]))) {
1130 g_strfreev(ids);
120f9ee7
BV
1131 g_critical("Cannot stack protocol decoder '%s': "
1132 "instance not found.", pds[i]);
1133 return 1;
1134 }
41e915a6 1135 g_strfreev(ids);
120f9ee7
BV
1136 if ((ret = srd_inst_stack(di_from, di_to)) != SRD_OK)
1137 return 1;
1138
1139 /* Don't show annotation from this PD. Only the last PD in
1140 * the stack will be left on the annotation list (unless
1141 * the annotation list was specifically provided).
1142 */
478a782d 1143 if (!opt_pd_annotations)
b6bd032d
UH
1144 g_hash_table_remove(pd_ann_visible,
1145 di_from->inst_id);
120f9ee7
BV
1146
1147 di_from = di_to;
1148 }
1149 }
1150 g_strfreev(pds);
1151
1152 return 0;
1153}
1154
1155int setup_pd_annotations(void)
1156{
1157 GSList *l;
1158 struct srd_decoder *dec;
1159 int ann;
1160 char **pds, **pdtok, **keyval, **ann_descr;
1161
1162 /* Set up custom list of PDs and annotations to show. */
b6bd032d
UH
1163 if (opt_pd_annotations) {
1164 pds = g_strsplit(opt_pd_annotations, ",", 0);
120f9ee7
BV
1165 for (pdtok = pds; *pdtok && **pdtok; pdtok++) {
1166 ann = 0;
1167 keyval = g_strsplit(*pdtok, "=", 0);
1168 if (!(dec = srd_decoder_get_by_id(keyval[0]))) {
1169 g_critical("Protocol decoder '%s' not found.", keyval[0]);
1170 return 1;
1171 }
1172 if (!dec->annotations) {
1173 g_critical("Protocol decoder '%s' has no annotations.", keyval[0]);
1174 return 1;
1175 }
1176 if (g_strv_length(keyval) == 2) {
1177 for (l = dec->annotations; l; l = l->next, ann++) {
1178 ann_descr = l->data;
1179 if (!canon_cmp(ann_descr[0], keyval[1]))
1180 /* Found it. */
1181 break;
1182 }
1183 if (!l) {
1184 g_critical("Annotation '%s' not found "
1185 "for protocol decoder '%s'.", keyval[1], keyval[0]);
1186 return 1;
1187 }
1188 }
1189 g_debug("cli: showing protocol decoder annotation %d from '%s'", ann, keyval[0]);
1190 g_hash_table_insert(pd_ann_visible, g_strdup(keyval[0]), GINT_TO_POINTER(ann));
1191 g_strfreev(keyval);
1192 }
1193 g_strfreev(pds);
1194 }
1195
1196 return 0;
1197}
1198
de0a066e
BV
1199void show_pd_annotations(struct srd_proto_data *pdata, void *cb_data)
1200{
1201 int i;
1202 char **annotations;
1203 gpointer ann_format;
1204
1205 /* 'cb_data' is not used in this specific callback. */
1206 (void)cb_data;
1207
1208 if (!pd_ann_visible)
1209 return;
1210
1211 if (!g_hash_table_lookup_extended(pd_ann_visible, pdata->pdo->di->inst_id,
1212 NULL, &ann_format))
1213 /* Not in the list of PDs whose annotations we're showing. */
1214 return;
1215
1216 if (pdata->ann_format != GPOINTER_TO_INT(ann_format))
1217 /* We don't want this particular format from the PD. */
1218 return;
1219
1220 annotations = pdata->data;
1221 if (opt_loglevel > SR_LOG_WARN)
1222 printf("%"PRIu64"-%"PRIu64" ", pdata->start_sample, pdata->end_sample);
1223 printf("%s: ", pdata->pdo->proto_id);
1224 for (i = 0; annotations[i]; i++)
1225 printf("\"%s\" ", annotations[i]);
1226 printf("\n");
1227 fflush(stdout);
1228}
1229#endif
1230
ad2bc491
BV
1231int setup_output_format(void)
1232{
1233 GHashTable *fmtargs;
1234 GHashTableIter iter;
1235 gpointer key, value;
1236 struct sr_output_format **outputs;
1237 int i;
1238 char *fmtspec;
1239
1240 if (!opt_output_format) {
1241 opt_output_format = DEFAULT_OUTPUT_FORMAT;
1242 /* we'll need to remember this so when saving to a file
1243 * later, sigrok session format will be used.
1244 */
1245 default_output_format = TRUE;
1246 }
48f71481 1247
63bb454c 1248 fmtargs = parse_generic_arg(opt_output_format, TRUE);
ad2bc491
BV
1249 fmtspec = g_hash_table_lookup(fmtargs, "sigrok_key");
1250 if (!fmtspec) {
1251 g_critical("Invalid output format.");
1252 return 1;
1253 }
1254 outputs = sr_output_list();
1255 for (i = 0; outputs[i]; i++) {
1256 if (strcmp(outputs[i]->id, fmtspec))
1257 continue;
1258 g_hash_table_remove(fmtargs, "sigrok_key");
1259 output_format = outputs[i];
1260 g_hash_table_iter_init(&iter, fmtargs);
1261 while (g_hash_table_iter_next(&iter, &key, &value)) {
1262 /* only supporting one parameter per output module
1263 * for now, and only its value */
1264 output_format_param = g_strdup(value);
1265 break;
1266 }
1267 break;
1268 }
1269 if (!output_format) {
1270 g_critical("Invalid output format %s.", opt_output_format);
1271 return 1;
1272 }
1273 g_hash_table_destroy(fmtargs);
1274
1275 return 0;
1276}
1277
37d5ccc1 1278static int select_probes(struct sr_dev_inst *sdi)
43e5747a 1279{
497f5362
BV
1280 struct sr_probe *probe;
1281 GSList *selected_probes, *l;
43e5747a
UH
1282
1283 if (!opt_probes)
1284 return SR_OK;
1285
497f5362 1286 if (!(selected_probes = parse_probestring(sdi, opt_probes)))
43e5747a 1287 return SR_ERR;
43e5747a 1288
497f5362
BV
1289 for (l = sdi->probes; l; l = l->next) {
1290 probe = l->data;
1291 if (g_slist_find(selected_probes, probe))
1292 probe->enabled = TRUE;
1293 else
1294 probe->enabled = FALSE;
43e5747a 1295 }
497f5362 1296 g_slist_free(selected_probes);
43e5747a
UH
1297
1298 return SR_OK;
1299}
1300
1301/**
1302 * Return the input file format which the CLI tool should use.
1303 *
1304 * If the user specified -I / --input-format, use that one. Otherwise, try to
1305 * autodetect the format as good as possible. Failing that, return NULL.
1306 *
1307 * @param filename The filename of the input file. Must not be NULL.
1308 * @param opt The -I / --input-file option the user specified (or NULL).
1309 *
1310 * @return A pointer to the 'struct sr_input_format' that should be used,
1311 * or NULL if no input format was selected or auto-detected.
1312 */
1313static struct sr_input_format *determine_input_file_format(
1314 const char *filename, const char *opt)
1315{
1316 int i;
1317 struct sr_input_format **inputs;
1318
1319 /* If there are no input formats, return NULL right away. */
1320 inputs = sr_input_list();
1321 if (!inputs) {
8170b8ea 1322 g_critical("No supported input formats available.");
43e5747a
UH
1323 return NULL;
1324 }
1325
1326 /* If the user specified -I / --input-format, use that one. */
1327 if (opt) {
1328 for (i = 0; inputs[i]; i++) {
943d0c08 1329 if (strcasecmp(inputs[i]->id, opt))
43e5747a 1330 continue;
8170b8ea
BV
1331 g_debug("Using user-specified input file format '%s'.",
1332 inputs[i]->id);
43e5747a
UH
1333 return inputs[i];
1334 }
1335
1336 /* The user specified an unknown input format, return NULL. */
8170b8ea 1337 g_critical("Error: specified input file format '%s' is "
943d0c08 1338 "unknown.", opt);
43e5747a
UH
1339 return NULL;
1340 }
1341
1342 /* Otherwise, try to find an input module that can handle this file. */
1343 for (i = 0; inputs[i]; i++) {
1344 if (inputs[i]->format_match(filename))
1345 break;
1346 }
1347
1348 /* Return NULL if no input module wanted to touch this. */
1349 if (!inputs[i]) {
8170b8ea 1350 g_critical("Error: no matching input module found.");
43e5747a
UH
1351 return NULL;
1352 }
78912cc1
UH
1353
1354 g_debug("cli: Autodetected '%s' input format for file '%s'.",
1355 inputs[i]->id, filename);
43e5747a 1356
43e5747a
UH
1357 return inputs[i];
1358}
1359
1360static void load_input_file_format(void)
1361{
943d0c08 1362 GHashTable *fmtargs = NULL;
43e5747a
UH
1363 struct stat st;
1364 struct sr_input *in;
1365 struct sr_input_format *input_format;
943d0c08
TÅ 
1366 char *fmtspec = NULL;
1367
1368 if (opt_input_format) {
3e8e0c2d 1369 fmtargs = parse_generic_arg(opt_input_format, TRUE);
943d0c08
TÅ 
1370 fmtspec = g_hash_table_lookup(fmtargs, "sigrok_key");
1371 }
43e5747a 1372
8170b8ea 1373 if (!(input_format = determine_input_file_format(opt_input_file,
943d0c08 1374 fmtspec))) {
8170b8ea 1375 /* The exact cause was already logged. */
43e5747a 1376 return;
943d0c08 1377 }
3e8e0c2d 1378
943d0c08
TÅ 
1379 if (fmtargs)
1380 g_hash_table_remove(fmtargs, "sigrok_key");
43e5747a
UH
1381
1382 if (stat(opt_input_file, &st) == -1) {
8170b8ea 1383 g_critical("Failed to load %s: %s", opt_input_file,
43e5747a
UH
1384 strerror(errno));
1385 exit(1);
1386 }
1387
1388 /* Initialize the input module. */
c2c4a0de 1389 if (!(in = g_try_malloc(sizeof(struct sr_input)))) {
8170b8ea 1390 g_critical("Failed to allocate input module.");
43e5747a
UH
1391 exit(1);
1392 }
1393 in->format = input_format;
943d0c08 1394 in->param = fmtargs;
43e5747a 1395 if (in->format->init) {
41602d35 1396 if (in->format->init(in, opt_input_file) != SR_OK) {
8170b8ea 1397 g_critical("Input format init failed.");
43e5747a
UH
1398 exit(1);
1399 }
1400 }
1401
37d5ccc1 1402 if (select_probes(in->sdi) > 0)
c27450ea 1403 return;
43e5747a
UH
1404
1405 sr_session_new();
74f6195b 1406 sr_session_datafeed_callback_add(datafeed_in, NULL);
37d5ccc1 1407 if (sr_session_dev_add(in->sdi) != SR_OK) {
8170b8ea 1408 g_critical("Failed to use device.");
43e5747a
UH
1409 sr_session_destroy();
1410 return;
1411 }
1412
1413 input_format->loadfile(in, opt_input_file);
c27450ea 1414
43e5747a 1415 sr_session_destroy();
943d0c08
TÅ 
1416
1417 if (fmtargs)
1418 g_hash_table_destroy(fmtargs);
43e5747a
UH
1419}
1420
1421static void load_input_file(void)
1422{
1423
1424 if (sr_session_load(opt_input_file) == SR_OK) {
1425 /* sigrok session file */
74f6195b 1426 sr_session_datafeed_callback_add(datafeed_in, NULL);
43e5747a
UH
1427 sr_session_start();
1428 sr_session_run();
1429 sr_session_stop();
1430 }
1431 else {
1432 /* fall back on input modules */
1433 load_input_file_format();
1434 }
43e5747a
UH
1435}
1436
37d5ccc1 1437static int set_dev_options(struct sr_dev_inst *sdi, GHashTable *args)
43e5747a 1438{
995713f4 1439 const struct sr_config_info *srci;
43e5747a
UH
1440 GHashTableIter iter;
1441 gpointer key, value;
cfd3ec6e 1442 int ret;
2f50086c 1443 double tmp_double;
bd31fc3f 1444 uint64_t tmp_u64, p, q;
43e5747a 1445 gboolean tmp_bool;
bd31fc3f 1446 GVariant *val, *rational[2];
43e5747a
UH
1447
1448 g_hash_table_iter_init(&iter, args);
1449 while (g_hash_table_iter_next(&iter, &key, &value)) {
74b9bf0c 1450 if (!(srci = sr_config_info_name_get(key))) {
cfd3ec6e
BV
1451 g_critical("Unknown device option '%s'.", (char *) key);
1452 return SR_ERR;
1453 }
1454
1455 if ((value == NULL) &&
995713f4 1456 (srci->datatype != SR_T_BOOL)) {
cfd3ec6e
BV
1457 g_critical("Option '%s' needs a value.", (char *)key);
1458 return SR_ERR;
1459 }
1460 val = NULL;
995713f4 1461 switch (srci->datatype) {
cfd3ec6e
BV
1462 case SR_T_UINT64:
1463 ret = sr_parse_sizestring(value, &tmp_u64);
1464 if (ret != SR_OK)
8f3b8464 1465 break;
2f50086c 1466 val = g_variant_new_uint64(tmp_u64);
cfd3ec6e
BV
1467 break;
1468 case SR_T_CHAR:
2f50086c 1469 val = g_variant_new_string(value);
cfd3ec6e
BV
1470 break;
1471 case SR_T_BOOL:
1472 if (!value)
1473 tmp_bool = TRUE;
43e5747a 1474 else
cfd3ec6e 1475 tmp_bool = sr_parse_boolstring(value);
2f50086c 1476 val = g_variant_new_boolean(tmp_bool);
cfd3ec6e
BV
1477 break;
1478 case SR_T_FLOAT:
2f50086c
BV
1479 tmp_double = strtof(value, NULL);
1480 val = g_variant_new_double(tmp_double);
cfd3ec6e
BV
1481 break;
1482 case SR_T_RATIONAL_PERIOD:
bd31fc3f 1483 if ((ret = sr_parse_period(value, &p, &q)) != SR_OK)
cfd3ec6e 1484 break;
bd31fc3f
BV
1485 rational[0] = g_variant_new_uint64(p);
1486 rational[1] = g_variant_new_uint64(q);
1487 val = g_variant_new_tuple(rational, 2);
cfd3ec6e
BV
1488 break;
1489 case SR_T_RATIONAL_VOLT:
bd31fc3f 1490 if ((ret = sr_parse_voltage(value, &p, &q)) != SR_OK)
43e5747a 1491 break;
bd31fc3f
BV
1492 rational[0] = g_variant_new_uint64(p);
1493 rational[1] = g_variant_new_uint64(q);
1494 val = g_variant_new_tuple(rational, 2);
cfd3ec6e
BV
1495 break;
1496 default:
1497 ret = SR_ERR;
43e5747a 1498 }
cfd3ec6e 1499 if (val)
c0b9d56b 1500 ret = sr_config_set(sdi, srci->key, val);
cfd3ec6e
BV
1501 if (ret != SR_OK) {
1502 g_critical("Failed to set device option '%s'.", (char *)key);
1503 return ret;
43e5747a
UH
1504 }
1505 }
1506
1507 return SR_OK;
1508}
1509
2d73284e
BV
1510static void set_options(void)
1511{
1512 struct sr_dev_inst *sdi;
1513 GSList *devices;
1514 GHashTable *devargs;
1515
ea7741ee 1516 if (!opt_config) {
2d73284e
BV
1517 g_critical("No setting specified.");
1518 return;
1519 }
1520
ea7741ee 1521 if (!(devargs = parse_generic_arg(opt_config, FALSE)))
2d73284e
BV
1522 return;
1523
1524 if (!(devices = device_scan())) {
1525 g_critical("No devices found.");
1526 return;
1527 }
1528 sdi = devices->data;
1529
0687f23d
BV
1530 if (sr_dev_open(sdi) != SR_OK) {
1531 g_critical("Failed to open device.");
2d73284e
BV
1532 return;
1533 }
1534
1535 set_dev_options(sdi, devargs);
1536
0687f23d 1537 sr_dev_close(sdi);
2d73284e
BV
1538 g_slist_free(devices);
1539 g_hash_table_destroy(devargs);
1540
1541}
1542
37d5ccc1 1543static int set_limit_time(const struct sr_dev_inst *sdi)
43e5747a 1544{
1616f663 1545 GVariant *gvar;
9c9c1080 1546 uint64_t time_msec;
1616f663 1547 uint64_t samplerate;
43e5747a 1548
43c062e6 1549 if (!(time_msec = sr_parse_timestring(opt_time))) {
37d5ccc1 1550 g_critical("Invalid time '%s'", opt_time);
37d5ccc1 1551 return SR_ERR;
43e5747a
UH
1552 }
1553
e4ffb9d6 1554 if (sr_dev_has_option(sdi, SR_CONF_LIMIT_MSEC)) {
1616f663
BV
1555 gvar = g_variant_new_uint64(time_msec);
1556 if (sr_config_set(sdi, SR_CONF_LIMIT_MSEC, gvar) != SR_OK) {
37d5ccc1 1557 g_critical("Failed to configure time limit.");
37d5ccc1 1558 return SR_ERR;
43e5747a 1559 }
43c062e6
BV
1560 } else if (sr_dev_has_option(sdi, SR_CONF_SAMPLERATE)) {
1561 /* Convert to samples based on the samplerate. */
1616f663
BV
1562 sr_config_get(sdi->driver, SR_CONF_SAMPLERATE, &gvar, sdi);
1563 samplerate = g_variant_get_uint64(gvar);
1564 g_variant_unref(gvar);
1565 limit_samples = (samplerate) * time_msec / (uint64_t)1000;
37d5ccc1
BV
1566 if (limit_samples == 0) {
1567 g_critical("Not enough time at this samplerate.");
37d5ccc1 1568 return SR_ERR;
43e5747a 1569 }
1616f663
BV
1570 gvar = g_variant_new_uint64(limit_samples);
1571 if (sr_config_set(sdi, SR_CONF_LIMIT_SAMPLES, gvar) != SR_OK) {
37d5ccc1 1572 g_critical("Failed to configure time-based sample limit.");
37d5ccc1 1573 return SR_ERR;
43e5747a 1574 }
43c062e6
BV
1575 } else {
1576 g_critical("This device does not support time limits.");
1577 return SR_ERR;
37d5ccc1 1578 }
43e5747a 1579
37d5ccc1
BV
1580 return SR_OK;
1581}
1582
1583static void run_session(void)
1584{
06a3fb10 1585 GSList *devices;
37d5ccc1 1586 GHashTable *devargs;
1616f663 1587 GVariant *gvar;
37d5ccc1
BV
1588 struct sr_dev_inst *sdi;
1589 int max_probes, i;
06a3fb10
BV
1590 char **triggerlist;
1591
1592 devices = device_scan();
1593 if (!devices) {
1594 g_critical("No devices found.");
1595 return;
1596 }
1597 if (g_slist_length(devices) > 1) {
1598 g_critical("sigrok-cli only supports one device for capturing.");
1599 return;
1600 }
1601 sdi = devices->data;
37d5ccc1
BV
1602
1603 sr_session_new();
74f6195b 1604 sr_session_datafeed_callback_add(datafeed_in, NULL);
37d5ccc1 1605
0687f23d
BV
1606 if (sr_dev_open(sdi) != SR_OK) {
1607 g_critical("Failed to open device.");
1608 return;
1609 }
1610
8ab6aafc 1611 if (sr_session_dev_add(sdi) != SR_OK) {
0687f23d 1612 g_critical("Failed to add device to session.");
8ab6aafc
BV
1613 sr_session_destroy();
1614 return;
1615 }
1616
ea7741ee
BV
1617 if (opt_config) {
1618 if ((devargs = parse_generic_arg(opt_config, FALSE))) {
06a3fb10
BV
1619 if (set_dev_options(sdi, devargs) != SR_OK)
1620 return;
1621 g_hash_table_destroy(devargs);
43e5747a 1622 }
06a3fb10 1623 }
43e5747a 1624
06a3fb10
BV
1625 if (select_probes(sdi) != SR_OK) {
1626 g_critical("Failed to set probes.");
1627 sr_session_destroy();
1628 return;
1629 }
1630
1631 if (opt_triggers) {
1632 if (!(triggerlist = sr_parse_triggerstring(sdi, opt_triggers))) {
43e5747a
UH
1633 sr_session_destroy();
1634 return;
1635 }
06a3fb10
BV
1636 max_probes = g_slist_length(sdi->probes);
1637 for (i = 0; i < max_probes; i++) {
1638 if (triggerlist[i]) {
1639 sr_dev_trigger_set(sdi, i, triggerlist[i]);
1640 g_free(triggerlist[i]);
43e5747a
UH
1641 }
1642 }
06a3fb10
BV
1643 g_free(triggerlist);
1644 }
9c9c1080 1645
06a3fb10 1646 if (opt_continuous) {
e4ffb9d6 1647 if (!sr_dev_has_option(sdi, SR_CONF_CONTINUOUS)) {
06a3fb10 1648 g_critical("This device does not support continuous sampling.");
37d5ccc1
BV
1649 sr_session_destroy();
1650 return;
1651 }
37d5ccc1
BV
1652 }
1653
1654 if (opt_time) {
1655 if (set_limit_time(sdi) != SR_OK) {
1656 sr_session_destroy();
1657 return;
43e5747a
UH
1658 }
1659 }
1660
1661 if (opt_samples) {
1616f663
BV
1662 if ((sr_parse_sizestring(opt_samples, &limit_samples) != SR_OK)) {
1663 g_critical("Invalid sample limit '%s'.", opt_samples);
1664 sr_session_destroy();
1665 return;
1666 }
1667 gvar = g_variant_new_uint64(limit_samples);
1668 if (sr_config_set(sdi, SR_CONF_LIMIT_SAMPLES, gvar) != SR_OK) {
8170b8ea 1669 g_critical("Failed to configure sample limit.");
43e5747a
UH
1670 sr_session_destroy();
1671 return;
1672 }
1673 }
1674
ce48d892 1675 if (opt_frames) {
1616f663
BV
1676 if ((sr_parse_sizestring(opt_frames, &limit_frames) != SR_OK)) {
1677 g_critical("Invalid sample limit '%s'.", opt_samples);
1678 sr_session_destroy();
1679 return;
1680 }
2f50086c 1681 gvar = g_variant_new_uint64(limit_frames);
1616f663 1682 if (sr_config_set(sdi, SR_CONF_LIMIT_FRAMES, gvar) != SR_OK) {
f8ccd825 1683 g_critical("Failed to configure frame limit.");
ce48d892
BV
1684 sr_session_destroy();
1685 return;
1686 }
1687 }
1688
43e5747a 1689 if (sr_session_start() != SR_OK) {
8170b8ea 1690 g_critical("Failed to start session.");
43e5747a
UH
1691 sr_session_destroy();
1692 return;
1693 }
1694
1695 if (opt_continuous)
1696 add_anykey();
1697
1698 sr_session_run();
1699
1700 if (opt_continuous)
1701 clear_anykey();
1702
43e5747a 1703 sr_session_destroy();
37d5ccc1
BV
1704 g_slist_free(devices);
1705
43e5747a
UH
1706}
1707
1708static void logger(const gchar *log_domain, GLogLevelFlags log_level,
68cdf174 1709 const gchar *message, gpointer cb_data)
43e5747a 1710{
43e5747a 1711 (void)log_domain;
68cdf174 1712 (void)cb_data;
43e5747a
UH
1713
1714 /*
1715 * All messages, warnings, errors etc. go to stderr (not stdout) in
1716 * order to not mess up the CLI tool data output, e.g. VCD output.
1717 */
2d6ff326 1718 if (log_level & (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING)
d740e6ac 1719 || opt_loglevel > SR_LOG_WARN) {
43e5747a
UH
1720 fprintf(stderr, "%s\n", message);
1721 fflush(stderr);
43e5747a
UH
1722 }
1723}
1724
1725int main(int argc, char **argv)
1726{
198f4c6c 1727 int ret = 1;
43e5747a
UH
1728 GOptionContext *context;
1729 GError *error;
43e5747a
UH
1730
1731 g_log_set_default_handler(logger, NULL);
43e5747a
UH
1732
1733 error = NULL;
1734 context = g_option_context_new(NULL);
1735 g_option_context_add_main_entries(context, optargs, NULL);
1736
1737 if (!g_option_context_parse(context, &argc, &argv, &error)) {
8170b8ea 1738 g_critical("%s", error->message);
5acb7682 1739 goto done;
43e5747a
UH
1740 }
1741
1742 /* Set the loglevel (amount of messages to output) for libsigrok. */
120f9ee7 1743 if (sr_log_loglevel_set(opt_loglevel) != SR_OK)
5acb7682 1744 goto done;
43e5747a 1745
de0a066e 1746 if (sr_init(&sr_ctx) != SR_OK)
5acb7682 1747 goto done;
6de7ec06 1748
de0a066e
BV
1749#ifdef HAVE_SRD
1750 /* Set the loglevel (amount of messages to output) for libsigrokdecode. */
1751 if (srd_log_loglevel_set(opt_loglevel) != SRD_OK)
5acb7682 1752 goto done;
43e5747a
UH
1753
1754 if (opt_pds) {
120f9ee7 1755 if (srd_init(NULL) != SRD_OK)
198f4c6c 1756 goto done;
120f9ee7 1757 if (register_pds(NULL, opt_pds) != 0)
198f4c6c 1758 goto done;
4359a4da 1759 if (srd_pd_output_callback_add(SRD_OUTPUT_ANN,
b6bd032d 1760 show_pd_annotations, NULL) != SRD_OK)
198f4c6c 1761 goto done;
120f9ee7 1762 if (setup_pd_stack() != 0)
198f4c6c 1763 goto done;
120f9ee7 1764 if (setup_pd_annotations() != 0)
198f4c6c 1765 goto done;
9f4a898e 1766 }
de0a066e 1767#endif
9f4a898e 1768
ad2bc491 1769 if (setup_output_format() != 0)
198f4c6c 1770 goto done;
43e5747a
UH
1771
1772 if (opt_version)
1773 show_version();
ea7741ee 1774 else if (opt_scan_devs)
1e0f9ed9 1775 show_dev_list();
de0a066e 1776#ifdef HAVE_SRD
f83fbc57
BV
1777 else if (opt_pds && opt_show)
1778 show_pd_detail();
de0a066e 1779#endif
22981b2c
BV
1780 else if (opt_show)
1781 show_dev_detail();
43e5747a
UH
1782 else if (opt_input_file)
1783 load_input_file();
2d73284e
BV
1784 else if (opt_set)
1785 set_options();
ce48d892 1786 else if (opt_samples || opt_time || opt_frames || opt_continuous)
43e5747a 1787 run_session();
43e5747a
UH
1788 else
1789 printf("%s", g_option_context_get_help(context, TRUE, NULL));
1790
de0a066e 1791#ifdef HAVE_SRD
43e5747a
UH
1792 if (opt_pds)
1793 srd_exit();
de0a066e 1794#endif
43e5747a 1795
198f4c6c
PS
1796 ret = 0;
1797
1798done:
5acb7682
PS
1799 if (sr_ctx)
1800 sr_exit(sr_ctx);
198f4c6c 1801
8630d4a8 1802 g_option_context_free(context);
43e5747a 1803
198f4c6c 1804 return ret;
43e5747a 1805}