]> sigrok.org Git - sigrok-cli.git/blame - sigrok-cli.c
Open device before adding it to the session
[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. */
1e0f9ed9 55static gboolean opt_list_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;
1e0f9ed9 60static gchar *opt_dev = 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,
b64d15a3 81 "Set libsigrok/libsigrokdecode loglevel", NULL},
03996962
BV
82 {"list-devices", 'D', 0, G_OPTION_ARG_NONE, &opt_list_devs,
83 "Scan for devices", NULL},
9e2e414f
BV
84 {"driver", 0, 0, G_OPTION_ARG_STRING, &opt_drv,
85 "Use only this driver", NULL},
03996962
BV
86 {"device", 'd', 0, G_OPTION_ARG_STRING, &opt_dev,
87 "Use specified device", NULL},
88 {"input-file", 'i', 0, G_OPTION_ARG_FILENAME, &opt_input_file,
89 "Load input from file", NULL},
90 {"input-format", 'I', 0, G_OPTION_ARG_STRING, &opt_input_format,
91 "Input format", NULL},
92 {"output-file", 'o', 0, G_OPTION_ARG_FILENAME, &opt_output_file,
93 "Save output to file", NULL},
94 {"output-format", 'O', 0, G_OPTION_ARG_STRING, &opt_output_format,
95 "Output format", NULL},
96 {"probes", 'p', 0, G_OPTION_ARG_STRING, &opt_probes,
97 "Probes to use", NULL},
98 {"triggers", 't', 0, G_OPTION_ARG_STRING, &opt_triggers,
99 "Trigger configuration", NULL},
100 {"wait-trigger", 'w', 0, G_OPTION_ARG_NONE, &opt_wait_trigger,
101 "Wait for trigger", NULL},
de0a066e 102#ifdef HAVE_SRD
03996962
BV
103 {"protocol-decoders", 'a', 0, G_OPTION_ARG_STRING, &opt_pds,
104 "Protocol decoders to run", NULL},
105 {"protocol-decoder-stack", 's', 0, G_OPTION_ARG_STRING, &opt_pd_stack,
106 "Protocol decoder stack", NULL},
b6bd032d
UH
107 {"protocol-decoder-annotations", 'A', 0, G_OPTION_ARG_STRING, &opt_pd_annotations,
108 "Protocol decoder annotation(s) to show", NULL},
de0a066e 109#endif
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
BV
346 g_critical("%d devices found. Use --list-devices to show them, "
347 "and --device to select one.", num_devices);
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 545 } else if (srci->key == SR_CONF_DATALOG) {
1e1316b3 546 /* TODO test */
8e8827f4
BV
547 /* Turning on/off internal data logging. */
548 printf(" %s\t(on/off", srci->id);
549 if (sr_config_get(sdi->driver, SR_CONF_DATALOG,
1616f663
BV
550 &gvar, sdi) == SR_OK) {
551 tmp_bool = g_variant_get_boolean(gvar);
552 printf(", currently %s", tmp_bool ? "on" : "off");
1e1316b3 553 g_variant_unref(gvar);
8e8827f4
BV
554 }
555 printf(")\n");
43e5747a 556 } else {
c8db7172 557 /* Everything else */
995713f4 558 printf(" %s\n", srci->id);
43e5747a
UH
559 }
560 }
1616f663 561 g_variant_unref(gvar_opts);
22981b2c 562
0687f23d 563 sr_dev_close(sdi);
8e8827f4 564
43e5747a
UH
565}
566
de0a066e 567#ifdef HAVE_SRD
71b1ea4e
BV
568static void show_pd_detail(void)
569{
570 GSList *l;
571 struct srd_decoder *dec;
d142a3f7
BV
572 struct srd_decoder_option *o;
573 char **pdtokens, **pdtok, *optsep, **ann, *val, *doc;
e9ff6974 574 struct srd_probe *p;
71b1ea4e
BV
575
576 pdtokens = g_strsplit(opt_pds, ",", -1);
577 for (pdtok = pdtokens; *pdtok; pdtok++) {
d142a3f7
BV
578 /* Strip options. */
579 if ((optsep = strchr(*pdtok, ':')))
580 *optsep = '\0';
2358775c 581 if (!(dec = srd_decoder_get_by_id(*pdtok))) {
f8ccd825 582 g_critical("Protocol decoder %s not found.", *pdtok);
71b1ea4e
BV
583 return;
584 }
585 printf("ID: %s\nName: %s\nLong name: %s\nDescription: %s\n",
586 dec->id, dec->name, dec->longname, dec->desc);
587 printf("License: %s\n", dec->license);
e9ff6974 588 printf("Annotations:\n");
71b1ea4e 589 if (dec->annotations) {
71b1ea4e
BV
590 for (l = dec->annotations; l; l = l->next) {
591 ann = l->data;
592 printf("- %s\n %s\n", ann[0], ann[1]);
593 }
e9ff6974
UH
594 } else {
595 printf("None.\n");
596 }
e9ff6974
UH
597 printf("Required probes:\n");
598 if (dec->probes) {
599 for (l = dec->probes; l; l = l->next) {
600 p = l->data;
601 printf("- %s (%s): %s\n",
602 p->name, p->id, p->desc);
603 }
604 } else {
605 printf("None.\n");
606 }
607 printf("Optional probes:\n");
608 if (dec->opt_probes) {
609 for (l = dec->opt_probes; l; l = l->next) {
610 p = l->data;
611 printf("- %s (%s): %s\n",
612 p->name, p->id, p->desc);
613 }
614 } else {
615 printf("None.\n");
71b1ea4e 616 }
d142a3f7
BV
617 if (dec->options) {
618 printf("Options:\n");
619 for (l = dec->options; l; l = l->next) {
620 o = l->data;
621 val = g_variant_print(o->def, FALSE);
622 printf("- %s: %s (default %s)\n", o->id, o->desc, val);
623 g_free(val);
624 }
625 }
4359a4da 626 if ((doc = srd_decoder_doc_get(dec))) {
e9ff6974
UH
627 printf("Documentation:\n%s\n",
628 doc[0] == '\n' ? doc + 1 : doc);
71b1ea4e
BV
629 g_free(doc);
630 }
631 }
632
633 g_strfreev(pdtokens);
71b1ea4e 634}
de0a066e 635#endif
71b1ea4e 636
c27450ea
BV
637static GArray *get_enabled_logic_probes(const struct sr_dev_inst *sdi)
638{
639 struct sr_probe *probe;
640 GArray *probes;
641 GSList *l;
642
643 probes = g_array_new(FALSE, FALSE, sizeof(int));
644 for (l = sdi->probes; l; l = l->next) {
645 probe = l->data;
646 if (probe->type != SR_PROBE_LOGIC)
647 continue;
648 if (probe->enabled != TRUE)
649 continue;
650 g_array_append_val(probes, probe->index);
651 }
652
653 return probes;
654}
655
37d5ccc1 656static void datafeed_in(const struct sr_dev_inst *sdi,
74f6195b 657 const struct sr_datafeed_packet *packet, void *cb_data)
43e5747a 658{
7e97afa0 659 const struct sr_datafeed_meta *meta;
c27450ea
BV
660 const struct sr_datafeed_logic *logic;
661 const struct sr_datafeed_analog *analog;
7e97afa0 662 struct sr_config *src;
43e5747a 663 static struct sr_output *o = NULL;
c27450ea 664 static GArray *logic_probelist = NULL;
43e5747a
UH
665 static uint64_t received_samples = 0;
666 static int unitsize = 0;
667 static int triggered = 0;
668 static FILE *outfile = NULL;
1616f663
BV
669 GSList *l;
670 GString *out;
671 GVariant *gvar;
c27450ea 672 int sample_size, ret;
1616f663 673 uint64_t samplerate, output_len, filter_out_len;
8de01efe 674 uint8_t *output_buf, *filter_out;
43e5747a 675
74f6195b
ML
676 (void) cb_data;
677
43e5747a
UH
678 /* If the first packet to come in isn't a header, don't even try. */
679 if (packet->type != SR_DF_HEADER && o == NULL)
680 return;
681
682 sample_size = -1;
683 switch (packet->type) {
684 case SR_DF_HEADER:
8170b8ea 685 g_debug("cli: Received SR_DF_HEADER");
43e5747a 686 /* Initialize the output module. */
c2c4a0de 687 if (!(o = g_try_malloc(sizeof(struct sr_output)))) {
8170b8ea 688 g_critical("Output module malloc failed.");
43e5747a
UH
689 exit(1);
690 }
691 o->format = output_format;
37d5ccc1 692 o->sdi = (struct sr_dev_inst *)sdi;
43e5747a
UH
693 o->param = output_format_param;
694 if (o->format->init) {
695 if (o->format->init(o) != SR_OK) {
8170b8ea 696 g_critical("Output format initialization failed.");
43e5747a
UH
697 exit(1);
698 }
699 }
c27450ea
BV
700
701 /* Prepare non-stdout output. */
702 outfile = stdout;
703 if (opt_output_file) {
704 if (default_output_format) {
705 /* output file is in session format, so we'll
706 * keep a copy of everything as it comes in
707 * and save from there after the session. */
708 outfile = NULL;
709 savebuf = g_byte_array_new();
710 } else {
711 /* saving to a file in whatever format was set
712 * with --format, so all we need is a filehandle */
713 outfile = g_fopen(opt_output_file, "wb");
714 }
715 }
716
717 /* Prepare for logic data. */
718 logic_probelist = get_enabled_logic_probes(sdi);
719 /* How many bytes we need to store the packed samples. */
720 unitsize = (logic_probelist->len + 7) / 8;
721
de0a066e 722#ifdef HAVE_SRD
c27450ea 723 if (opt_pds && logic_probelist->len) {
9e0e651f 724 if (sr_config_get(sdi->driver, SR_CONF_SAMPLERATE,
1616f663 725 &gvar, sdi) != SR_OK) {
c27450ea
BV
726 g_critical("Unable to initialize protocol "
727 "decoders: no samplerate found.");
728 break;
729 }
1616f663
BV
730 samplerate = g_variant_get_uint64(gvar);
731 g_variant_unref(gvar);
732 srd_session_start(logic_probelist->len, unitsize, samplerate);
c27450ea 733 }
de0a066e 734#endif
43e5747a 735 break;
53993299 736
7e97afa0
BV
737 case SR_DF_META:
738 g_debug("cli: received SR_DF_META");
739 meta = packet->payload;
740 for (l = meta->config; l; l = l->next) {
741 src = l->data;
742 switch (src->key) {
b5c63de9 743 case SR_CONF_SAMPLERATE:
1616f663
BV
744 samplerate = g_variant_get_uint64(src->data);
745 g_debug("cli: got samplerate %"PRIu64" Hz", samplerate);
5570e0f2
BV
746 break;
747 case SR_CONF_SAMPLE_INTERVAL:
1616f663
BV
748 samplerate = g_variant_get_uint64(src->data);
749 g_debug("cli: got sample interval %"PRIu64" ms", samplerate);
7e97afa0
BV
750 break;
751 default:
752 /* Unknown metadata is not an error. */
753 break;
754 }
755 }
756 break;
757
43e5747a 758 case SR_DF_TRIGGER:
8170b8ea 759 g_debug("cli: received SR_DF_TRIGGER");
43e5747a
UH
760 if (o->format->event)
761 o->format->event(o, SR_DF_TRIGGER, &output_buf,
762 &output_len);
763 triggered = 1;
764 break;
53993299 765
43e5747a
UH
766 case SR_DF_LOGIC:
767 logic = packet->payload;
b46c4414 768 g_message("cli: received SR_DF_LOGIC, %"PRIu64" bytes", logic->length);
53993299
BV
769 sample_size = logic->unitsize;
770 if (logic->length == 0)
771 break;
772
773 /* Don't store any samples until triggered. */
774 if (opt_wait_trigger && !triggered)
775 break;
43e5747a 776
53993299
BV
777 if (limit_samples && received_samples >= limit_samples)
778 break;
43e5747a 779
53993299 780 ret = sr_filter_probes(sample_size, unitsize, logic_probelist,
f8ccd825
BV
781 logic->data, logic->length,
782 &filter_out, &filter_out_len);
53993299
BV
783 if (ret != SR_OK)
784 break;
43e5747a 785
c27450ea 786 /* What comes out of the filter is guaranteed to be packed into the
53993299
BV
787 * minimum size needed to support the number of samples at this sample
788 * size. however, the driver may have submitted too much -- cut off
789 * the buffer of the last packet according to the sample limit.
790 */
791 if (limit_samples && (received_samples + logic->length / sample_size >
792 limit_samples * sample_size))
793 filter_out_len = limit_samples * sample_size - received_samples;
43e5747a 794
c27450ea
BV
795 if (opt_output_file && default_output_format) {
796 /* Saving to a session file. */
797 g_byte_array_append(savebuf, filter_out, filter_out_len);
53993299 798 } else {
c27450ea 799 if (opt_pds) {
de0a066e 800#ifdef HAVE_SRD
c27450ea
BV
801 if (srd_session_send(received_samples, (uint8_t*)filter_out,
802 filter_out_len) != SRD_OK)
803 sr_session_stop();
de0a066e 804#endif
c27450ea
BV
805 } else {
806 output_len = 0;
807 if (o->format->data && packet->type == o->format->df_type)
808 o->format->data(o, filter_out, filter_out_len,
809 &output_buf, &output_len);
a172e2a0 810 if (output_len) {
c27450ea
BV
811 fwrite(output_buf, 1, output_len, outfile);
812 fflush(outfile);
813 g_free(output_buf);
814 }
53993299
BV
815 }
816 }
53993299 817 g_free(filter_out);
43e5747a 818
c27450ea 819 received_samples += logic->length / sample_size;
53993299
BV
820 break;
821
822 case SR_DF_ANALOG:
823 analog = packet->payload;
824 g_message("cli: received SR_DF_ANALOG, %d samples", analog->num_samples);
825 if (analog->num_samples == 0)
826 break;
827
828 if (limit_samples && received_samples >= limit_samples)
829 break;
830
48f71481
BV
831 if (o->format->data && packet->type == o->format->df_type) {
832 o->format->data(o, (const uint8_t *)analog->data,
833 analog->num_samples * sizeof(float),
834 &output_buf, &output_len);
835 if (output_buf) {
836 fwrite(output_buf, 1, output_len, outfile);
e09810e9 837 fflush(outfile);
48f71481 838 g_free(output_buf);
c8db7172 839 }
53993299
BV
840 }
841
53993299 842 received_samples += analog->num_samples;
ce48d892
BV
843 break;
844
845 case SR_DF_FRAME_BEGIN:
48f71481
BV
846 g_debug("cli: received SR_DF_FRAME_BEGIN");
847 if (o->format->event) {
848 o->format->event(o, SR_DF_FRAME_BEGIN, &output_buf,
849 &output_len);
850 if (output_buf) {
851 fwrite(output_buf, 1, output_len, outfile);
e09810e9 852 fflush(outfile);
48f71481
BV
853 g_free(output_buf);
854 }
855 }
ce48d892
BV
856 break;
857
858 case SR_DF_FRAME_END:
48f71481
BV
859 g_debug("cli: received SR_DF_FRAME_END");
860 if (o->format->event) {
861 o->format->event(o, SR_DF_FRAME_END, &output_buf,
862 &output_len);
863 if (output_buf) {
864 fwrite(output_buf, 1, output_len, outfile);
e09810e9 865 fflush(outfile);
48f71481
BV
866 g_free(output_buf);
867 }
868 }
ce48d892 869 break;
53993299
BV
870
871 default:
c27450ea 872 break;
43e5747a
UH
873 }
874
05e7fa2c
BV
875 if (o && o->format->recv) {
876 out = o->format->recv(o, sdi, packet);
877 if (out && out->len) {
878 fwrite(out->str, 1, out->len, outfile);
879 fflush(outfile);
880 }
881 }
882
bea29a44
BV
883 /* SR_DF_END needs to be handled after the output module's recv()
884 * is called, so it can properly clean up that module etc. */
885 if (packet->type == SR_DF_END) {
886 g_debug("cli: Received SR_DF_END");
887
888 if (o->format->event) {
889 o->format->event(o, SR_DF_END, &output_buf, &output_len);
890 if (output_buf) {
891 if (outfile)
892 fwrite(output_buf, 1, output_len, outfile);
893 g_free(output_buf);
894 output_len = 0;
895 }
896 }
897
898 if (limit_samples && received_samples < limit_samples)
899 g_warning("Device only sent %" PRIu64 " samples.",
900 received_samples);
901
902 if (opt_continuous)
903 g_warning("Device stopped after %" PRIu64 " samples.",
904 received_samples);
905
906 g_array_free(logic_probelist, TRUE);
907
908 if (o->format->cleanup)
909 o->format->cleanup(o);
910 g_free(o);
911 o = NULL;
912
913 if (outfile && outfile != stdout)
914 fclose(outfile);
915
916 if (opt_output_file && default_output_format) {
917 if (sr_session_save(opt_output_file, sdi, savebuf->data,
918 unitsize, savebuf->len / unitsize) != SR_OK)
919 g_critical("Failed to save session.");
920 g_byte_array_free(savebuf, FALSE);
921 }
922 }
923
43e5747a
UH
924}
925
de0a066e 926#ifdef HAVE_SRD
d142a3f7
BV
927static int opts_to_gvar(struct srd_decoder *dec, GHashTable *hash,
928 GHashTable **options)
929{
930 struct srd_decoder_option *o;
931 GSList *optl;
932 GVariant *gvar;
933 gint64 val_int;
934 int ret;
935 char *val_str, *conv;
936
937 ret = TRUE;
938 *options = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
939 (GDestroyNotify)g_variant_unref);
940
941 for (optl = dec->options; optl; optl = optl->next) {
942 o = optl->data;
943 if (!(val_str = g_hash_table_lookup(hash, o->id)))
944 /* Not specified. */
945 continue;
946 if (g_variant_is_of_type(o->def, G_VARIANT_TYPE_STRING)) {
947 gvar = g_variant_new_string(val_str);
948 } else if (g_variant_is_of_type(o->def, G_VARIANT_TYPE_INT64)) {
949 val_int = strtoll(val_str, &conv, 10);
950 if (!conv || conv == val_str) {
951 g_critical("Protocol decoder '%s' option '%s' "
952 "requires a number.", dec->name, o->id);
953 ret = FALSE;
954 break;
955 }
956 gvar = g_variant_new_int64(val_int);
957 } else {
958 g_critical("Unsupported type for option '%s' (%s)",
959 o->id, g_variant_get_type_string(o->def));
960 ret = FALSE;
961 break;
962 }
963 g_variant_ref_sink(gvar);
964 g_hash_table_insert(*options, g_strdup(o->id), gvar);
965 g_hash_table_remove(hash, o->id);
966 }
967
968 return ret;
969}
970
971static int probes_to_gvar(struct srd_decoder *dec, GHashTable *hash,
972 GHashTable **probes)
973{
974 struct srd_probe *p;
975 GSList *all_probes, *l;
976 GVariant *gvar;
977 gint32 val_int;
978 int ret;
979 char *val_str, *conv;
980
981 ret = TRUE;
982 *probes = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
983 (GDestroyNotify)g_variant_unref);
984
985 all_probes = g_slist_copy(dec->probes);
986 all_probes = g_slist_concat(all_probes, dec->opt_probes);
987 for (l = all_probes; l; l = l->next) {
988 p = l->data;
989 if (!(val_str = g_hash_table_lookup(hash, p->id)))
990 /* Not specified. */
991 continue;
992 val_int = strtoll(val_str, &conv, 10);
993 if (!conv || conv == val_str) {
994 g_critical("Protocol decoder '%s' probes '%s' "
995 "is not a number.", dec->name, p->id);
996 ret = FALSE;
997 break;
998 }
999 gvar = g_variant_new_int32(val_int);
1000 g_variant_ref_sink(gvar);
1001 g_hash_table_insert(*probes, g_strdup(p->id), gvar);
1002 g_hash_table_remove(hash, p->id);
1003 }
1004 g_slist_free(all_probes);
1005
1006 return ret;
1007}
1008
9f4a898e
BV
1009/* Register the given PDs for this session.
1010 * Accepts a string of the form: "spi:sck=3:sdata=4,spi:sck=3:sdata=5"
43e5747a
UH
1011 * That will instantiate two SPI decoders on the clock but different data
1012 * lines.
1013 */
1e0f9ed9 1014static int register_pds(struct sr_dev *dev, const char *pdstring)
43e5747a 1015{
d142a3f7
BV
1016 struct srd_decoder *dec;
1017 GHashTable *pd_opthash, *options, *probes;
1018 GList *leftover, *l;
878e90d9 1019 struct srd_decoder_inst *di;
445950d3 1020 int ret;
a1418b73 1021 char **pdtokens, **pdtok, *pd_name;
43e5747a 1022
1e0f9ed9 1023 (void)dev;
43e5747a 1024
445950d3 1025 ret = 0;
120f9ee7
BV
1026 pd_ann_visible = g_hash_table_new_full(g_str_hash, g_int_equal,
1027 g_free, NULL);
a1418b73 1028 pd_name = NULL;
d142a3f7 1029 pd_opthash = options = probes = NULL;
120f9ee7 1030 pdtokens = g_strsplit(pdstring, ",", 0);
43e5747a 1031 for (pdtok = pdtokens; *pdtok; pdtok++) {
63bb454c 1032 if (!(pd_opthash = parse_generic_arg(*pdtok, TRUE))) {
8170b8ea 1033 g_critical("Invalid protocol decoder option '%s'.", *pdtok);
d142a3f7 1034 break;
43e5747a 1035 }
a1418b73 1036
9720f23a
BV
1037 pd_name = g_strdup(g_hash_table_lookup(pd_opthash, "sigrok_key"));
1038 g_hash_table_remove(pd_opthash, "sigrok_key");
2358775c 1039 if (srd_decoder_load(pd_name) != SRD_OK) {
8170b8ea 1040 g_critical("Failed to load protocol decoder %s.", pd_name);
445950d3 1041 ret = 1;
d142a3f7
BV
1042 break;
1043 }
1044 dec = srd_decoder_get_by_id(pd_name);
1045
1046 /* Convert decoder option and probe values to GVariant. */
1047 if (!opts_to_gvar(dec, pd_opthash, &options)) {
1048 ret = 1;
1049 break;
1050 }
1051 if (!probes_to_gvar(dec, pd_opthash, &probes)) {
1052 ret = 1;
1053 break;
15d920a9 1054 }
d142a3f7
BV
1055 if (g_hash_table_size(pd_opthash) > 0) {
1056 leftover = g_hash_table_get_keys(pd_opthash);
1057 for (l = leftover; l; l = l->next)
1058 g_critical("Unknown option or probe '%s'", (char *)l->data);
1059 g_list_free(leftover);
1060 break;
1061 }
1062
1063 if (!(di = srd_inst_new(pd_name, options))) {
8170b8ea 1064 g_critical("Failed to instantiate protocol decoder %s.", pd_name);
445950d3 1065 ret = 1;
d142a3f7 1066 break;
43e5747a 1067 }
120f9ee7
BV
1068
1069 /* If no annotation list was specified, add them all in now.
1070 * This will be pared down later to leave only the last PD
1071 * in the stack.
1072 */
b6bd032d
UH
1073 if (!opt_pd_annotations)
1074 g_hash_table_insert(pd_ann_visible,
1075 g_strdup(di->inst_id), NULL);
a1418b73 1076
d142a3f7
BV
1077 /* Remap the probes if needed. */
1078 if (srd_inst_probe_set_all(di, probes) != SRD_OK) {
445950d3 1079 ret = 1;
d142a3f7 1080 break;
445950d3 1081 }
67ce0d27 1082 }
43e5747a 1083
43e5747a 1084 g_strfreev(pdtokens);
9720f23a
BV
1085 if (pd_opthash)
1086 g_hash_table_destroy(pd_opthash);
d142a3f7
BV
1087 if (options)
1088 g_hash_table_destroy(options);
1089 if (probes)
1090 g_hash_table_destroy(probes);
a1418b73
BV
1091 if (pd_name)
1092 g_free(pd_name);
43e5747a 1093
445950d3 1094 return ret;
43e5747a
UH
1095}
1096
120f9ee7
BV
1097int setup_pd_stack(void)
1098{
1099 struct srd_decoder_inst *di_from, *di_to;
1100 int ret, i;
41e915a6 1101 char **pds, **ids;
120f9ee7
BV
1102
1103 /* Set up the protocol decoder stack. */
1104 pds = g_strsplit(opt_pds, ",", 0);
1105 if (g_strv_length(pds) > 1) {
1106 if (opt_pd_stack) {
1107 /* A stack setup was specified, use that. */
1108 g_strfreev(pds);
1109 pds = g_strsplit(opt_pd_stack, ",", 0);
1110 if (g_strv_length(pds) < 2) {
1111 g_strfreev(pds);
1112 g_critical("Specify at least two protocol decoders to stack.");
1113 return 1;
1114 }
1115 }
1116
41e915a6
BV
1117 /* First PD goes at the bottom of the stack. */
1118 ids = g_strsplit(pds[0], ":", 0);
1119 if (!(di_from = srd_inst_find_by_id(ids[0]))) {
1120 g_strfreev(ids);
120f9ee7
BV
1121 g_critical("Cannot stack protocol decoder '%s': "
1122 "instance not found.", pds[0]);
1123 return 1;
1124 }
41e915a6
BV
1125 g_strfreev(ids);
1126
1127 /* Every subsequent PD goes on top. */
120f9ee7 1128 for (i = 1; pds[i]; i++) {
41e915a6
BV
1129 ids = g_strsplit(pds[i], ":", 0);
1130 if (!(di_to = srd_inst_find_by_id(ids[0]))) {
1131 g_strfreev(ids);
120f9ee7
BV
1132 g_critical("Cannot stack protocol decoder '%s': "
1133 "instance not found.", pds[i]);
1134 return 1;
1135 }
41e915a6 1136 g_strfreev(ids);
120f9ee7
BV
1137 if ((ret = srd_inst_stack(di_from, di_to)) != SRD_OK)
1138 return 1;
1139
1140 /* Don't show annotation from this PD. Only the last PD in
1141 * the stack will be left on the annotation list (unless
1142 * the annotation list was specifically provided).
1143 */
478a782d 1144 if (!opt_pd_annotations)
b6bd032d
UH
1145 g_hash_table_remove(pd_ann_visible,
1146 di_from->inst_id);
120f9ee7
BV
1147
1148 di_from = di_to;
1149 }
1150 }
1151 g_strfreev(pds);
1152
1153 return 0;
1154}
1155
1156int setup_pd_annotations(void)
1157{
1158 GSList *l;
1159 struct srd_decoder *dec;
1160 int ann;
1161 char **pds, **pdtok, **keyval, **ann_descr;
1162
1163 /* Set up custom list of PDs and annotations to show. */
b6bd032d
UH
1164 if (opt_pd_annotations) {
1165 pds = g_strsplit(opt_pd_annotations, ",", 0);
120f9ee7
BV
1166 for (pdtok = pds; *pdtok && **pdtok; pdtok++) {
1167 ann = 0;
1168 keyval = g_strsplit(*pdtok, "=", 0);
1169 if (!(dec = srd_decoder_get_by_id(keyval[0]))) {
1170 g_critical("Protocol decoder '%s' not found.", keyval[0]);
1171 return 1;
1172 }
1173 if (!dec->annotations) {
1174 g_critical("Protocol decoder '%s' has no annotations.", keyval[0]);
1175 return 1;
1176 }
1177 if (g_strv_length(keyval) == 2) {
1178 for (l = dec->annotations; l; l = l->next, ann++) {
1179 ann_descr = l->data;
1180 if (!canon_cmp(ann_descr[0], keyval[1]))
1181 /* Found it. */
1182 break;
1183 }
1184 if (!l) {
1185 g_critical("Annotation '%s' not found "
1186 "for protocol decoder '%s'.", keyval[1], keyval[0]);
1187 return 1;
1188 }
1189 }
1190 g_debug("cli: showing protocol decoder annotation %d from '%s'", ann, keyval[0]);
1191 g_hash_table_insert(pd_ann_visible, g_strdup(keyval[0]), GINT_TO_POINTER(ann));
1192 g_strfreev(keyval);
1193 }
1194 g_strfreev(pds);
1195 }
1196
1197 return 0;
1198}
1199
de0a066e
BV
1200void show_pd_annotations(struct srd_proto_data *pdata, void *cb_data)
1201{
1202 int i;
1203 char **annotations;
1204 gpointer ann_format;
1205
1206 /* 'cb_data' is not used in this specific callback. */
1207 (void)cb_data;
1208
1209 if (!pd_ann_visible)
1210 return;
1211
1212 if (!g_hash_table_lookup_extended(pd_ann_visible, pdata->pdo->di->inst_id,
1213 NULL, &ann_format))
1214 /* Not in the list of PDs whose annotations we're showing. */
1215 return;
1216
1217 if (pdata->ann_format != GPOINTER_TO_INT(ann_format))
1218 /* We don't want this particular format from the PD. */
1219 return;
1220
1221 annotations = pdata->data;
1222 if (opt_loglevel > SR_LOG_WARN)
1223 printf("%"PRIu64"-%"PRIu64" ", pdata->start_sample, pdata->end_sample);
1224 printf("%s: ", pdata->pdo->proto_id);
1225 for (i = 0; annotations[i]; i++)
1226 printf("\"%s\" ", annotations[i]);
1227 printf("\n");
1228 fflush(stdout);
1229}
1230#endif
1231
ad2bc491
BV
1232int setup_output_format(void)
1233{
1234 GHashTable *fmtargs;
1235 GHashTableIter iter;
1236 gpointer key, value;
1237 struct sr_output_format **outputs;
1238 int i;
1239 char *fmtspec;
1240
1241 if (!opt_output_format) {
1242 opt_output_format = DEFAULT_OUTPUT_FORMAT;
1243 /* we'll need to remember this so when saving to a file
1244 * later, sigrok session format will be used.
1245 */
1246 default_output_format = TRUE;
1247 }
48f71481 1248
63bb454c 1249 fmtargs = parse_generic_arg(opt_output_format, TRUE);
ad2bc491
BV
1250 fmtspec = g_hash_table_lookup(fmtargs, "sigrok_key");
1251 if (!fmtspec) {
1252 g_critical("Invalid output format.");
1253 return 1;
1254 }
1255 outputs = sr_output_list();
1256 for (i = 0; outputs[i]; i++) {
1257 if (strcmp(outputs[i]->id, fmtspec))
1258 continue;
1259 g_hash_table_remove(fmtargs, "sigrok_key");
1260 output_format = outputs[i];
1261 g_hash_table_iter_init(&iter, fmtargs);
1262 while (g_hash_table_iter_next(&iter, &key, &value)) {
1263 /* only supporting one parameter per output module
1264 * for now, and only its value */
1265 output_format_param = g_strdup(value);
1266 break;
1267 }
1268 break;
1269 }
1270 if (!output_format) {
1271 g_critical("Invalid output format %s.", opt_output_format);
1272 return 1;
1273 }
1274 g_hash_table_destroy(fmtargs);
1275
1276 return 0;
1277}
1278
37d5ccc1 1279static int select_probes(struct sr_dev_inst *sdi)
43e5747a 1280{
497f5362
BV
1281 struct sr_probe *probe;
1282 GSList *selected_probes, *l;
43e5747a
UH
1283
1284 if (!opt_probes)
1285 return SR_OK;
1286
497f5362 1287 if (!(selected_probes = parse_probestring(sdi, opt_probes)))
43e5747a 1288 return SR_ERR;
43e5747a 1289
497f5362
BV
1290 for (l = sdi->probes; l; l = l->next) {
1291 probe = l->data;
1292 if (g_slist_find(selected_probes, probe))
1293 probe->enabled = TRUE;
1294 else
1295 probe->enabled = FALSE;
43e5747a 1296 }
497f5362 1297 g_slist_free(selected_probes);
43e5747a
UH
1298
1299 return SR_OK;
1300}
1301
1302/**
1303 * Return the input file format which the CLI tool should use.
1304 *
1305 * If the user specified -I / --input-format, use that one. Otherwise, try to
1306 * autodetect the format as good as possible. Failing that, return NULL.
1307 *
1308 * @param filename The filename of the input file. Must not be NULL.
1309 * @param opt The -I / --input-file option the user specified (or NULL).
1310 *
1311 * @return A pointer to the 'struct sr_input_format' that should be used,
1312 * or NULL if no input format was selected or auto-detected.
1313 */
1314static struct sr_input_format *determine_input_file_format(
1315 const char *filename, const char *opt)
1316{
1317 int i;
1318 struct sr_input_format **inputs;
1319
1320 /* If there are no input formats, return NULL right away. */
1321 inputs = sr_input_list();
1322 if (!inputs) {
8170b8ea 1323 g_critical("No supported input formats available.");
43e5747a
UH
1324 return NULL;
1325 }
1326
1327 /* If the user specified -I / --input-format, use that one. */
1328 if (opt) {
1329 for (i = 0; inputs[i]; i++) {
943d0c08 1330 if (strcasecmp(inputs[i]->id, opt))
43e5747a 1331 continue;
8170b8ea
BV
1332 g_debug("Using user-specified input file format '%s'.",
1333 inputs[i]->id);
43e5747a
UH
1334 return inputs[i];
1335 }
1336
1337 /* The user specified an unknown input format, return NULL. */
8170b8ea 1338 g_critical("Error: specified input file format '%s' is "
943d0c08 1339 "unknown.", opt);
43e5747a
UH
1340 return NULL;
1341 }
1342
1343 /* Otherwise, try to find an input module that can handle this file. */
1344 for (i = 0; inputs[i]; i++) {
1345 if (inputs[i]->format_match(filename))
1346 break;
1347 }
1348
1349 /* Return NULL if no input module wanted to touch this. */
1350 if (!inputs[i]) {
8170b8ea 1351 g_critical("Error: no matching input module found.");
43e5747a
UH
1352 return NULL;
1353 }
78912cc1
UH
1354
1355 g_debug("cli: Autodetected '%s' input format for file '%s'.",
1356 inputs[i]->id, filename);
43e5747a 1357
43e5747a
UH
1358 return inputs[i];
1359}
1360
1361static void load_input_file_format(void)
1362{
943d0c08 1363 GHashTable *fmtargs = NULL;
43e5747a
UH
1364 struct stat st;
1365 struct sr_input *in;
1366 struct sr_input_format *input_format;
943d0c08
TÅ 
1367 char *fmtspec = NULL;
1368
1369 if (opt_input_format) {
3e8e0c2d 1370 fmtargs = parse_generic_arg(opt_input_format, TRUE);
943d0c08
TÅ 
1371 fmtspec = g_hash_table_lookup(fmtargs, "sigrok_key");
1372 }
43e5747a 1373
8170b8ea 1374 if (!(input_format = determine_input_file_format(opt_input_file,
943d0c08 1375 fmtspec))) {
8170b8ea 1376 /* The exact cause was already logged. */
43e5747a 1377 return;
943d0c08 1378 }
3e8e0c2d 1379
943d0c08
TÅ 
1380 if (fmtargs)
1381 g_hash_table_remove(fmtargs, "sigrok_key");
43e5747a
UH
1382
1383 if (stat(opt_input_file, &st) == -1) {
8170b8ea 1384 g_critical("Failed to load %s: %s", opt_input_file,
43e5747a
UH
1385 strerror(errno));
1386 exit(1);
1387 }
1388
1389 /* Initialize the input module. */
c2c4a0de 1390 if (!(in = g_try_malloc(sizeof(struct sr_input)))) {
8170b8ea 1391 g_critical("Failed to allocate input module.");
43e5747a
UH
1392 exit(1);
1393 }
1394 in->format = input_format;
943d0c08 1395 in->param = fmtargs;
43e5747a 1396 if (in->format->init) {
41602d35 1397 if (in->format->init(in, opt_input_file) != SR_OK) {
8170b8ea 1398 g_critical("Input format init failed.");
43e5747a
UH
1399 exit(1);
1400 }
1401 }
1402
37d5ccc1 1403 if (select_probes(in->sdi) > 0)
c27450ea 1404 return;
43e5747a
UH
1405
1406 sr_session_new();
74f6195b 1407 sr_session_datafeed_callback_add(datafeed_in, NULL);
37d5ccc1 1408 if (sr_session_dev_add(in->sdi) != SR_OK) {
8170b8ea 1409 g_critical("Failed to use device.");
43e5747a
UH
1410 sr_session_destroy();
1411 return;
1412 }
1413
1414 input_format->loadfile(in, opt_input_file);
c27450ea 1415
43e5747a 1416 sr_session_destroy();
943d0c08
TÅ 
1417
1418 if (fmtargs)
1419 g_hash_table_destroy(fmtargs);
43e5747a
UH
1420}
1421
1422static void load_input_file(void)
1423{
1424
1425 if (sr_session_load(opt_input_file) == SR_OK) {
1426 /* sigrok session file */
74f6195b 1427 sr_session_datafeed_callback_add(datafeed_in, NULL);
43e5747a
UH
1428 sr_session_start();
1429 sr_session_run();
1430 sr_session_stop();
1431 }
1432 else {
1433 /* fall back on input modules */
1434 load_input_file_format();
1435 }
43e5747a
UH
1436}
1437
37d5ccc1 1438static int set_dev_options(struct sr_dev_inst *sdi, GHashTable *args)
43e5747a 1439{
995713f4 1440 const struct sr_config_info *srci;
43e5747a
UH
1441 GHashTableIter iter;
1442 gpointer key, value;
cfd3ec6e 1443 int ret;
2f50086c 1444 double tmp_double;
bd31fc3f 1445 uint64_t tmp_u64, p, q;
43e5747a 1446 gboolean tmp_bool;
bd31fc3f 1447 GVariant *val, *rational[2];
43e5747a
UH
1448
1449 g_hash_table_iter_init(&iter, args);
1450 while (g_hash_table_iter_next(&iter, &key, &value)) {
74b9bf0c 1451 if (!(srci = sr_config_info_name_get(key))) {
cfd3ec6e
BV
1452 g_critical("Unknown device option '%s'.", (char *) key);
1453 return SR_ERR;
1454 }
1455
1456 if ((value == NULL) &&
995713f4 1457 (srci->datatype != SR_T_BOOL)) {
cfd3ec6e
BV
1458 g_critical("Option '%s' needs a value.", (char *)key);
1459 return SR_ERR;
1460 }
1461 val = NULL;
995713f4 1462 switch (srci->datatype) {
cfd3ec6e
BV
1463 case SR_T_UINT64:
1464 ret = sr_parse_sizestring(value, &tmp_u64);
1465 if (ret != SR_OK)
8f3b8464 1466 break;
2f50086c 1467 val = g_variant_new_uint64(tmp_u64);
cfd3ec6e
BV
1468 break;
1469 case SR_T_CHAR:
2f50086c 1470 val = g_variant_new_string(value);
cfd3ec6e
BV
1471 break;
1472 case SR_T_BOOL:
1473 if (!value)
1474 tmp_bool = TRUE;
43e5747a 1475 else
cfd3ec6e 1476 tmp_bool = sr_parse_boolstring(value);
2f50086c 1477 val = g_variant_new_boolean(tmp_bool);
cfd3ec6e
BV
1478 break;
1479 case SR_T_FLOAT:
2f50086c
BV
1480 tmp_double = strtof(value, NULL);
1481 val = g_variant_new_double(tmp_double);
cfd3ec6e
BV
1482 break;
1483 case SR_T_RATIONAL_PERIOD:
bd31fc3f 1484 if ((ret = sr_parse_period(value, &p, &q)) != SR_OK)
cfd3ec6e 1485 break;
bd31fc3f
BV
1486 rational[0] = g_variant_new_uint64(p);
1487 rational[1] = g_variant_new_uint64(q);
1488 val = g_variant_new_tuple(rational, 2);
cfd3ec6e
BV
1489 break;
1490 case SR_T_RATIONAL_VOLT:
bd31fc3f 1491 if ((ret = sr_parse_voltage(value, &p, &q)) != SR_OK)
43e5747a 1492 break;
bd31fc3f
BV
1493 rational[0] = g_variant_new_uint64(p);
1494 rational[1] = g_variant_new_uint64(q);
1495 val = g_variant_new_tuple(rational, 2);
cfd3ec6e
BV
1496 break;
1497 default:
1498 ret = SR_ERR;
43e5747a 1499 }
cfd3ec6e 1500 if (val)
c0b9d56b 1501 ret = sr_config_set(sdi, srci->key, val);
cfd3ec6e
BV
1502 if (ret != SR_OK) {
1503 g_critical("Failed to set device option '%s'.", (char *)key);
1504 return ret;
43e5747a
UH
1505 }
1506 }
1507
1508 return SR_OK;
1509}
1510
2d73284e
BV
1511static void set_options(void)
1512{
1513 struct sr_dev_inst *sdi;
1514 GSList *devices;
1515 GHashTable *devargs;
1516
1517 if (!opt_dev) {
1518 g_critical("No setting specified.");
1519 return;
1520 }
1521
1522 if (!(devargs = parse_generic_arg(opt_dev, FALSE)))
1523 return;
1524
1525 if (!(devices = device_scan())) {
1526 g_critical("No devices found.");
1527 return;
1528 }
1529 sdi = devices->data;
1530
0687f23d
BV
1531 if (sr_dev_open(sdi) != SR_OK) {
1532 g_critical("Failed to open device.");
2d73284e
BV
1533 return;
1534 }
1535
1536 set_dev_options(sdi, devargs);
1537
0687f23d 1538 sr_dev_close(sdi);
2d73284e
BV
1539 g_slist_free(devices);
1540 g_hash_table_destroy(devargs);
1541
1542}
1543
37d5ccc1 1544static int set_limit_time(const struct sr_dev_inst *sdi)
43e5747a 1545{
1616f663 1546 GVariant *gvar;
9c9c1080 1547 uint64_t time_msec;
1616f663 1548 uint64_t samplerate;
43e5747a 1549
43c062e6 1550 if (!(time_msec = sr_parse_timestring(opt_time))) {
37d5ccc1 1551 g_critical("Invalid time '%s'", opt_time);
37d5ccc1 1552 return SR_ERR;
43e5747a
UH
1553 }
1554
e4ffb9d6 1555 if (sr_dev_has_option(sdi, SR_CONF_LIMIT_MSEC)) {
1616f663
BV
1556 gvar = g_variant_new_uint64(time_msec);
1557 if (sr_config_set(sdi, SR_CONF_LIMIT_MSEC, gvar) != SR_OK) {
37d5ccc1 1558 g_critical("Failed to configure time limit.");
37d5ccc1 1559 return SR_ERR;
43e5747a 1560 }
43c062e6
BV
1561 } else if (sr_dev_has_option(sdi, SR_CONF_SAMPLERATE)) {
1562 /* Convert to samples based on the samplerate. */
1616f663
BV
1563 sr_config_get(sdi->driver, SR_CONF_SAMPLERATE, &gvar, sdi);
1564 samplerate = g_variant_get_uint64(gvar);
1565 g_variant_unref(gvar);
1566 limit_samples = (samplerate) * time_msec / (uint64_t)1000;
37d5ccc1
BV
1567 if (limit_samples == 0) {
1568 g_critical("Not enough time at this samplerate.");
37d5ccc1 1569 return SR_ERR;
43e5747a 1570 }
1616f663
BV
1571 gvar = g_variant_new_uint64(limit_samples);
1572 if (sr_config_set(sdi, SR_CONF_LIMIT_SAMPLES, gvar) != SR_OK) {
37d5ccc1 1573 g_critical("Failed to configure time-based sample limit.");
37d5ccc1 1574 return SR_ERR;
43e5747a 1575 }
43c062e6
BV
1576 } else {
1577 g_critical("This device does not support time limits.");
1578 return SR_ERR;
37d5ccc1 1579 }
43e5747a 1580
37d5ccc1
BV
1581 return SR_OK;
1582}
1583
1584static void run_session(void)
1585{
06a3fb10 1586 GSList *devices;
37d5ccc1 1587 GHashTable *devargs;
1616f663 1588 GVariant *gvar;
37d5ccc1
BV
1589 struct sr_dev_inst *sdi;
1590 int max_probes, i;
06a3fb10
BV
1591 char **triggerlist;
1592
1593 devices = device_scan();
1594 if (!devices) {
1595 g_critical("No devices found.");
1596 return;
1597 }
1598 if (g_slist_length(devices) > 1) {
1599 g_critical("sigrok-cli only supports one device for capturing.");
1600 return;
1601 }
1602 sdi = devices->data;
37d5ccc1
BV
1603
1604 sr_session_new();
74f6195b 1605 sr_session_datafeed_callback_add(datafeed_in, NULL);
37d5ccc1 1606
0687f23d
BV
1607 if (sr_dev_open(sdi) != SR_OK) {
1608 g_critical("Failed to open device.");
1609 return;
1610 }
1611
8ab6aafc 1612 if (sr_session_dev_add(sdi) != SR_OK) {
0687f23d 1613 g_critical("Failed to add device to session.");
8ab6aafc
BV
1614 sr_session_destroy();
1615 return;
1616 }
1617
06a3fb10
BV
1618 if (opt_dev) {
1619 if ((devargs = parse_generic_arg(opt_dev, FALSE))) {
1620 if (set_dev_options(sdi, devargs) != SR_OK)
1621 return;
1622 g_hash_table_destroy(devargs);
43e5747a 1623 }
06a3fb10 1624 }
43e5747a 1625
06a3fb10
BV
1626 if (select_probes(sdi) != SR_OK) {
1627 g_critical("Failed to set probes.");
1628 sr_session_destroy();
1629 return;
1630 }
1631
1632 if (opt_triggers) {
1633 if (!(triggerlist = sr_parse_triggerstring(sdi, opt_triggers))) {
43e5747a
UH
1634 sr_session_destroy();
1635 return;
1636 }
06a3fb10
BV
1637 max_probes = g_slist_length(sdi->probes);
1638 for (i = 0; i < max_probes; i++) {
1639 if (triggerlist[i]) {
1640 sr_dev_trigger_set(sdi, i, triggerlist[i]);
1641 g_free(triggerlist[i]);
43e5747a
UH
1642 }
1643 }
06a3fb10
BV
1644 g_free(triggerlist);
1645 }
9c9c1080 1646
06a3fb10 1647 if (opt_continuous) {
e4ffb9d6 1648 if (!sr_dev_has_option(sdi, SR_CONF_CONTINUOUS)) {
06a3fb10 1649 g_critical("This device does not support continuous sampling.");
37d5ccc1
BV
1650 sr_session_destroy();
1651 return;
1652 }
37d5ccc1
BV
1653 }
1654
1655 if (opt_time) {
1656 if (set_limit_time(sdi) != SR_OK) {
1657 sr_session_destroy();
1658 return;
43e5747a
UH
1659 }
1660 }
1661
1662 if (opt_samples) {
1616f663
BV
1663 if ((sr_parse_sizestring(opt_samples, &limit_samples) != SR_OK)) {
1664 g_critical("Invalid sample limit '%s'.", opt_samples);
1665 sr_session_destroy();
1666 return;
1667 }
1668 gvar = g_variant_new_uint64(limit_samples);
1669 if (sr_config_set(sdi, SR_CONF_LIMIT_SAMPLES, gvar) != SR_OK) {
8170b8ea 1670 g_critical("Failed to configure sample limit.");
43e5747a
UH
1671 sr_session_destroy();
1672 return;
1673 }
1674 }
1675
ce48d892 1676 if (opt_frames) {
1616f663
BV
1677 if ((sr_parse_sizestring(opt_frames, &limit_frames) != SR_OK)) {
1678 g_critical("Invalid sample limit '%s'.", opt_samples);
1679 sr_session_destroy();
1680 return;
1681 }
2f50086c 1682 gvar = g_variant_new_uint64(limit_frames);
1616f663 1683 if (sr_config_set(sdi, SR_CONF_LIMIT_FRAMES, gvar) != SR_OK) {
f8ccd825 1684 g_critical("Failed to configure frame limit.");
ce48d892
BV
1685 sr_session_destroy();
1686 return;
1687 }
1688 }
1689
43e5747a 1690 if (sr_session_start() != SR_OK) {
8170b8ea 1691 g_critical("Failed to start session.");
43e5747a
UH
1692 sr_session_destroy();
1693 return;
1694 }
1695
1696 if (opt_continuous)
1697 add_anykey();
1698
1699 sr_session_run();
1700
1701 if (opt_continuous)
1702 clear_anykey();
1703
43e5747a 1704 sr_session_destroy();
37d5ccc1
BV
1705 g_slist_free(devices);
1706
43e5747a
UH
1707}
1708
1709static void logger(const gchar *log_domain, GLogLevelFlags log_level,
68cdf174 1710 const gchar *message, gpointer cb_data)
43e5747a 1711{
43e5747a 1712 (void)log_domain;
68cdf174 1713 (void)cb_data;
43e5747a
UH
1714
1715 /*
1716 * All messages, warnings, errors etc. go to stderr (not stdout) in
1717 * order to not mess up the CLI tool data output, e.g. VCD output.
1718 */
2d6ff326 1719 if (log_level & (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING)
d740e6ac 1720 || opt_loglevel > SR_LOG_WARN) {
43e5747a
UH
1721 fprintf(stderr, "%s\n", message);
1722 fflush(stderr);
43e5747a
UH
1723 }
1724}
1725
1726int main(int argc, char **argv)
1727{
198f4c6c 1728 int ret = 1;
43e5747a
UH
1729 GOptionContext *context;
1730 GError *error;
43e5747a
UH
1731
1732 g_log_set_default_handler(logger, NULL);
43e5747a
UH
1733
1734 error = NULL;
1735 context = g_option_context_new(NULL);
1736 g_option_context_add_main_entries(context, optargs, NULL);
1737
1738 if (!g_option_context_parse(context, &argc, &argv, &error)) {
8170b8ea 1739 g_critical("%s", error->message);
5acb7682 1740 goto done;
43e5747a
UH
1741 }
1742
1743 /* Set the loglevel (amount of messages to output) for libsigrok. */
120f9ee7 1744 if (sr_log_loglevel_set(opt_loglevel) != SR_OK)
5acb7682 1745 goto done;
43e5747a 1746
de0a066e 1747 if (sr_init(&sr_ctx) != SR_OK)
5acb7682 1748 goto done;
6de7ec06 1749
de0a066e
BV
1750#ifdef HAVE_SRD
1751 /* Set the loglevel (amount of messages to output) for libsigrokdecode. */
1752 if (srd_log_loglevel_set(opt_loglevel) != SRD_OK)
5acb7682 1753 goto done;
43e5747a
UH
1754
1755 if (opt_pds) {
120f9ee7 1756 if (srd_init(NULL) != SRD_OK)
198f4c6c 1757 goto done;
120f9ee7 1758 if (register_pds(NULL, opt_pds) != 0)
198f4c6c 1759 goto done;
4359a4da 1760 if (srd_pd_output_callback_add(SRD_OUTPUT_ANN,
b6bd032d 1761 show_pd_annotations, NULL) != SRD_OK)
198f4c6c 1762 goto done;
120f9ee7 1763 if (setup_pd_stack() != 0)
198f4c6c 1764 goto done;
120f9ee7 1765 if (setup_pd_annotations() != 0)
198f4c6c 1766 goto done;
9f4a898e 1767 }
de0a066e 1768#endif
9f4a898e 1769
ad2bc491 1770 if (setup_output_format() != 0)
198f4c6c 1771 goto done;
43e5747a
UH
1772
1773 if (opt_version)
1774 show_version();
1e0f9ed9
UH
1775 else if (opt_list_devs)
1776 show_dev_list();
de0a066e 1777#ifdef HAVE_SRD
f83fbc57
BV
1778 else if (opt_pds && opt_show)
1779 show_pd_detail();
de0a066e 1780#endif
22981b2c
BV
1781 else if (opt_show)
1782 show_dev_detail();
43e5747a
UH
1783 else if (opt_input_file)
1784 load_input_file();
2d73284e
BV
1785 else if (opt_set)
1786 set_options();
ce48d892 1787 else if (opt_samples || opt_time || opt_frames || opt_continuous)
43e5747a 1788 run_session();
43e5747a
UH
1789 else
1790 printf("%s", g_option_context_get_help(context, TRUE, NULL));
1791
de0a066e 1792#ifdef HAVE_SRD
43e5747a
UH
1793 if (opt_pds)
1794 srd_exit();
de0a066e 1795#endif
43e5747a 1796
198f4c6c
PS
1797 ret = 0;
1798
1799done:
5acb7682
PS
1800 if (sr_ctx)
1801 sr_exit(sr_ctx);
198f4c6c 1802
8630d4a8 1803 g_option_context_free(context);
43e5747a 1804
198f4c6c 1805 return ret;
43e5747a 1806}