]> sigrok.org Git - sigrok-cli.git/blame - sigrok-cli.c
GPL headers: Use correct project name.
[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
8e8827f4
BV
354 /* This properly opens and initializes the device, so we can get
355 * current settings. */
356 sr_session_new();
357 if (sr_session_dev_add(sdi) != SR_OK) {
358 g_critical("Failed to use device.");
359 return;
360 }
361
1616f663
BV
362 if ((sr_config_list(sdi->driver, SR_CONF_SCAN_OPTIONS, &gvar_opts,
363 NULL) == SR_OK)) {
364 opts = g_variant_get_fixed_array(gvar_opts, &num_elements,
365 sizeof(int32_t));
22981b2c 366 printf("Supported driver options:\n");
1616f663
BV
367 for (i = 0; i < num_elements; i++) {
368 if (!(srci = sr_config_info_get(opts[i])))
22981b2c 369 continue;
995713f4 370 printf(" %s\n", srci->id);
22981b2c 371 }
1616f663 372 g_variant_unref(gvar_opts);
22981b2c
BV
373 }
374
1616f663
BV
375 if ((sr_config_list(sdi->driver, SR_CONF_DEVICE_OPTIONS, &gvar_opts,
376 sdi) != SR_OK))
22981b2c
BV
377 /* Driver supports no device instance options. */
378 return;
379
1616f663
BV
380 printf("Supported device options:\n");
381 opts = g_variant_get_fixed_array(gvar_opts, &num_opts, sizeof(int32_t));
382 for (o = 0; o < num_opts; o++) {
383 if (!(srci = sr_config_info_get(opts[o])))
43e5747a
UH
384 continue;
385
1616f663
BV
386 if (srci->key == SR_CONF_TRIGGER_TYPE) {
387 if (sr_config_list(sdi->driver, srci->key, &gvar,
388 sdi) != SR_OK) {
389 printf("\n");
390 continue;
391 }
392 charopts = g_variant_get_string(gvar, NULL);
393 printf(" Supported triggers: ");
394 while (*charopts) {
395 printf("%c ", *charopts);
396 charopts++;
397 }
398 printf("\n");
399 g_variant_unref(gvar);
43e5747a 400
1616f663 401 } else if (srci->key == SR_CONF_PATTERN_MODE) {
c8db7172 402 /* Pattern generator modes */
995713f4 403 printf(" %s", srci->id);
1616f663
BV
404 if (sr_config_list(sdi->driver, srci->key, &gvar,
405 sdi) == SR_OK) {
0a56f4ec 406 printf(" - supported patterns:\n");
1e1316b3
BV
407 stropts = g_variant_get_strv(gvar, &num_elements);
408 for (i = 0; i < num_elements; i++)
9c9c1080 409 printf(" %s\n", stropts[i]);
1616f663 410 g_variant_unref(gvar);
9c9c1080 411 } else {
43e5747a 412 printf("\n");
43e5747a 413 }
c8db7172 414
b5c63de9 415 } else if (srci->key == SR_CONF_SAMPLERATE) {
43e5747a 416 /* Supported samplerates */
995713f4 417 printf(" %s", srci->id);
d2f671dc 418 if (sr_config_list(sdi->driver, SR_CONF_SAMPLERATE,
1616f663 419 &gvar_dict, sdi) != SR_OK) {
43e5747a
UH
420 printf("\n");
421 continue;
422 }
1616f663
BV
423 if ((gvar_list = g_variant_lookup_value(gvar_dict,
424 "samplerates", G_VARIANT_TYPE("at")))) {
4e0db44d 425 uint64 = g_variant_get_fixed_array(gvar_list,
1616f663
BV
426 &num_elements, sizeof(uint64_t));
427 printf(" - supported samplerates:\n");
428 for (i = 0; i < num_elements; i++)
4e0db44d 429 printf(" %s\n", sr_samplerate_string(uint64[i]));
1616f663
BV
430 } if ((gvar_list = g_variant_lookup_value(gvar_dict,
431 "samplerate-steps", G_VARIANT_TYPE("at")))) {
4e0db44d 432 uint64 = g_variant_get_fixed_array(gvar_list,
1616f663 433 &num_elements, sizeof(uint64_t));
43e5747a 434 /* low */
4e0db44d 435 if (!(s = sr_samplerate_string(uint64[0])))
43e5747a
UH
436 continue;
437 printf(" (%s", s);
c2c4a0de 438 g_free(s);
43e5747a 439 /* high */
4e0db44d 440 if (!(s = sr_samplerate_string(uint64[1])))
43e5747a
UH
441 continue;
442 printf(" - %s", s);
c2c4a0de 443 g_free(s);
43e5747a 444 /* step */
4e0db44d 445 if (!(s = sr_samplerate_string(uint64[2])))
43e5747a
UH
446 continue;
447 printf(" in steps of %s)\n", s);
c2c4a0de 448 g_free(s);
1e1316b3 449 g_variant_unref(gvar_list);
43e5747a 450 }
1616f663 451 g_variant_unref(gvar_dict);
c8db7172 452
b5c63de9 453 } else if (srci->key == SR_CONF_BUFFERSIZE) {
c8db7172 454 /* Supported buffer sizes */
995713f4 455 printf(" %s", srci->id);
2777cbd2 456 if (sr_config_list(sdi->driver, SR_CONF_BUFFERSIZE,
1e1316b3 457 &gvar_list, sdi) != SR_OK) {
c8db7172
BV
458 printf("\n");
459 continue;
460 }
4e0db44d 461 uint64 = g_variant_get_fixed_array(gvar_list,
1e1316b3 462 &num_elements, sizeof(uint64_t));
c8db7172 463 printf(" - supported buffer sizes:\n");
1e1316b3 464 for (i = 0; i < num_elements; i++)
4e0db44d 465 printf(" %"PRIu64"\n", uint64[i]);
1e1316b3 466 g_variant_unref(gvar_list);
c8db7172 467
b5c63de9 468 } else if (srci->key == SR_CONF_TIMEBASE) {
c8db7172 469 /* Supported time bases */
995713f4 470 printf(" %s", srci->id);
2bedb9bf 471 if (sr_config_list(sdi->driver, SR_CONF_TIMEBASE,
1e1316b3 472 &gvar_list, sdi) != SR_OK) {
c8db7172
BV
473 printf("\n");
474 continue;
475 }
476 printf(" - supported time bases:\n");
e07340ad
BV
477 num_elements = g_variant_n_children(gvar_list);
478 for (i = 0; i < num_elements; i++) {
479 gvar = g_variant_get_child_value(gvar_list, i);
480 g_variant_get(gvar, "(tt)", &p, &q);
481 s = sr_period_string(p * q);
482 printf(" %s\n", s);
483 g_free(s);
484 }
1e1316b3 485 g_variant_unref(gvar_list);
c8db7172 486
b5c63de9 487 } else if (srci->key == SR_CONF_TRIGGER_SOURCE) {
c8db7172 488 /* Supported trigger sources */
995713f4 489 printf(" %s", srci->id);
d3d7e30a 490 if (sr_config_list(sdi->driver, SR_CONF_TRIGGER_SOURCE,
1e1316b3 491 &gvar, sdi) != SR_OK) {
c8db7172
BV
492 printf("\n");
493 continue;
494 }
495 printf(" - supported trigger sources:\n");
1e1316b3
BV
496 stropts = g_variant_get_strv(gvar, &num_elements);
497 for (i = 0; i < num_elements; i++)
c8db7172 498 printf(" %s\n", stropts[i]);
1e1316b3 499 g_variant_unref(gvar);
c8db7172 500
b5c63de9 501 } else if (srci->key == SR_CONF_FILTER) {
e54290bd 502 /* Supported filters */
995713f4 503 printf(" %s", srci->id);
b4bd48e9 504 if (sr_config_list(sdi->driver, SR_CONF_FILTER,
1e1316b3 505 &gvar, sdi) != SR_OK) {
2850fca1
BV
506 printf("\n");
507 continue;
508 }
509 printf(" - supported filter targets:\n");
1e1316b3
BV
510 stropts = g_variant_get_strv(gvar, &num_elements);
511 for (i = 0; i < num_elements; i++)
2850fca1 512 printf(" %s\n", stropts[i]);
1e1316b3 513 g_variant_unref(gvar);
2850fca1 514
b5c63de9 515 } else if (srci->key == SR_CONF_VDIV) {
8f3b8464 516 /* Supported volts/div values */
995713f4 517 printf(" %s", srci->id);
9b0b39f6 518 if (sr_config_list(sdi->driver, SR_CONF_VDIV,
1e1316b3 519 &gvar_list, sdi) != SR_OK) {
8f3b8464
BV
520 printf("\n");
521 continue;
522 }
523 printf(" - supported volts/div:\n");
e07340ad
BV
524 num_elements = g_variant_n_children(gvar_list);
525 for (i = 0; i < num_elements; i++) {
526 gvar = g_variant_get_child_value(gvar_list, i);
527 g_variant_get(gvar, "(tt)", &p, &q);
528 s = sr_voltage_string(p, q);
529 printf(" %s\n", s);
530 g_free(s);
531 }
1e1316b3 532 g_variant_unref(gvar_list);
8f3b8464 533
b5c63de9 534 } else if (srci->key == SR_CONF_COUPLING) {
498f9167 535 /* Supported coupling settings */
995713f4 536 printf(" %s", srci->id);
3ea086fc 537 if (sr_config_list(sdi->driver, SR_CONF_COUPLING,
1e1316b3 538 &gvar, sdi) != SR_OK) {
498f9167
BV
539 printf("\n");
540 continue;
541 }
542 printf(" - supported coupling options:\n");
1e1316b3
BV
543 stropts = g_variant_get_strv(gvar, &num_elements);
544 for (i = 0; i < num_elements; i++)
498f9167 545 printf(" %s\n", stropts[i]);
1e1316b3 546 g_variant_unref(gvar);
498f9167 547
8e8827f4 548 } else if (srci->key == SR_CONF_DATALOG) {
1e1316b3 549 /* TODO test */
8e8827f4
BV
550 /* Turning on/off internal data logging. */
551 printf(" %s\t(on/off", srci->id);
552 if (sr_config_get(sdi->driver, SR_CONF_DATALOG,
1616f663
BV
553 &gvar, sdi) == SR_OK) {
554 tmp_bool = g_variant_get_boolean(gvar);
555 printf(", currently %s", tmp_bool ? "on" : "off");
1e1316b3 556 g_variant_unref(gvar);
8e8827f4
BV
557 }
558 printf(")\n");
43e5747a 559 } else {
c8db7172 560 /* Everything else */
995713f4 561 printf(" %s\n", srci->id);
43e5747a
UH
562 }
563 }
1616f663 564 g_variant_unref(gvar_opts);
22981b2c 565
8e8827f4
BV
566 sr_session_destroy();
567
43e5747a
UH
568}
569
de0a066e 570#ifdef HAVE_SRD
71b1ea4e
BV
571static void show_pd_detail(void)
572{
573 GSList *l;
574 struct srd_decoder *dec;
d142a3f7
BV
575 struct srd_decoder_option *o;
576 char **pdtokens, **pdtok, *optsep, **ann, *val, *doc;
e9ff6974 577 struct srd_probe *p;
71b1ea4e
BV
578
579 pdtokens = g_strsplit(opt_pds, ",", -1);
580 for (pdtok = pdtokens; *pdtok; pdtok++) {
d142a3f7
BV
581 /* Strip options. */
582 if ((optsep = strchr(*pdtok, ':')))
583 *optsep = '\0';
2358775c 584 if (!(dec = srd_decoder_get_by_id(*pdtok))) {
f8ccd825 585 g_critical("Protocol decoder %s not found.", *pdtok);
71b1ea4e
BV
586 return;
587 }
588 printf("ID: %s\nName: %s\nLong name: %s\nDescription: %s\n",
589 dec->id, dec->name, dec->longname, dec->desc);
590 printf("License: %s\n", dec->license);
e9ff6974 591 printf("Annotations:\n");
71b1ea4e 592 if (dec->annotations) {
71b1ea4e
BV
593 for (l = dec->annotations; l; l = l->next) {
594 ann = l->data;
595 printf("- %s\n %s\n", ann[0], ann[1]);
596 }
e9ff6974
UH
597 } else {
598 printf("None.\n");
599 }
e9ff6974
UH
600 printf("Required probes:\n");
601 if (dec->probes) {
602 for (l = dec->probes; l; l = l->next) {
603 p = l->data;
604 printf("- %s (%s): %s\n",
605 p->name, p->id, p->desc);
606 }
607 } else {
608 printf("None.\n");
609 }
610 printf("Optional probes:\n");
611 if (dec->opt_probes) {
612 for (l = dec->opt_probes; l; l = l->next) {
613 p = l->data;
614 printf("- %s (%s): %s\n",
615 p->name, p->id, p->desc);
616 }
617 } else {
618 printf("None.\n");
71b1ea4e 619 }
d142a3f7
BV
620 if (dec->options) {
621 printf("Options:\n");
622 for (l = dec->options; l; l = l->next) {
623 o = l->data;
624 val = g_variant_print(o->def, FALSE);
625 printf("- %s: %s (default %s)\n", o->id, o->desc, val);
626 g_free(val);
627 }
628 }
4359a4da 629 if ((doc = srd_decoder_doc_get(dec))) {
e9ff6974
UH
630 printf("Documentation:\n%s\n",
631 doc[0] == '\n' ? doc + 1 : doc);
71b1ea4e
BV
632 g_free(doc);
633 }
634 }
635
636 g_strfreev(pdtokens);
71b1ea4e 637}
de0a066e 638#endif
71b1ea4e 639
c27450ea
BV
640static GArray *get_enabled_logic_probes(const struct sr_dev_inst *sdi)
641{
642 struct sr_probe *probe;
643 GArray *probes;
644 GSList *l;
645
646 probes = g_array_new(FALSE, FALSE, sizeof(int));
647 for (l = sdi->probes; l; l = l->next) {
648 probe = l->data;
649 if (probe->type != SR_PROBE_LOGIC)
650 continue;
651 if (probe->enabled != TRUE)
652 continue;
653 g_array_append_val(probes, probe->index);
654 }
655
656 return probes;
657}
658
37d5ccc1 659static void datafeed_in(const struct sr_dev_inst *sdi,
74f6195b 660 const struct sr_datafeed_packet *packet, void *cb_data)
43e5747a 661{
7e97afa0 662 const struct sr_datafeed_meta *meta;
c27450ea
BV
663 const struct sr_datafeed_logic *logic;
664 const struct sr_datafeed_analog *analog;
7e97afa0 665 struct sr_config *src;
43e5747a 666 static struct sr_output *o = NULL;
c27450ea 667 static GArray *logic_probelist = NULL;
43e5747a
UH
668 static uint64_t received_samples = 0;
669 static int unitsize = 0;
670 static int triggered = 0;
671 static FILE *outfile = NULL;
1616f663
BV
672 GSList *l;
673 GString *out;
674 GVariant *gvar;
c27450ea 675 int sample_size, ret;
1616f663 676 uint64_t samplerate, output_len, filter_out_len;
8de01efe 677 uint8_t *output_buf, *filter_out;
43e5747a 678
74f6195b
ML
679 (void) cb_data;
680
43e5747a
UH
681 /* If the first packet to come in isn't a header, don't even try. */
682 if (packet->type != SR_DF_HEADER && o == NULL)
683 return;
684
685 sample_size = -1;
686 switch (packet->type) {
687 case SR_DF_HEADER:
8170b8ea 688 g_debug("cli: Received SR_DF_HEADER");
43e5747a 689 /* Initialize the output module. */
c2c4a0de 690 if (!(o = g_try_malloc(sizeof(struct sr_output)))) {
8170b8ea 691 g_critical("Output module malloc failed.");
43e5747a
UH
692 exit(1);
693 }
694 o->format = output_format;
37d5ccc1 695 o->sdi = (struct sr_dev_inst *)sdi;
43e5747a
UH
696 o->param = output_format_param;
697 if (o->format->init) {
698 if (o->format->init(o) != SR_OK) {
8170b8ea 699 g_critical("Output format initialization failed.");
43e5747a
UH
700 exit(1);
701 }
702 }
c27450ea
BV
703
704 /* Prepare non-stdout output. */
705 outfile = stdout;
706 if (opt_output_file) {
707 if (default_output_format) {
708 /* output file is in session format, so we'll
709 * keep a copy of everything as it comes in
710 * and save from there after the session. */
711 outfile = NULL;
712 savebuf = g_byte_array_new();
713 } else {
714 /* saving to a file in whatever format was set
715 * with --format, so all we need is a filehandle */
716 outfile = g_fopen(opt_output_file, "wb");
717 }
718 }
719
720 /* Prepare for logic data. */
721 logic_probelist = get_enabled_logic_probes(sdi);
722 /* How many bytes we need to store the packed samples. */
723 unitsize = (logic_probelist->len + 7) / 8;
724
de0a066e 725#ifdef HAVE_SRD
c27450ea 726 if (opt_pds && logic_probelist->len) {
9e0e651f 727 if (sr_config_get(sdi->driver, SR_CONF_SAMPLERATE,
1616f663 728 &gvar, sdi) != SR_OK) {
c27450ea
BV
729 g_critical("Unable to initialize protocol "
730 "decoders: no samplerate found.");
731 break;
732 }
1616f663
BV
733 samplerate = g_variant_get_uint64(gvar);
734 g_variant_unref(gvar);
735 srd_session_start(logic_probelist->len, unitsize, samplerate);
c27450ea 736 }
de0a066e 737#endif
43e5747a 738 break;
53993299 739
7e97afa0
BV
740 case SR_DF_META:
741 g_debug("cli: received SR_DF_META");
742 meta = packet->payload;
743 for (l = meta->config; l; l = l->next) {
744 src = l->data;
745 switch (src->key) {
b5c63de9 746 case SR_CONF_SAMPLERATE:
1616f663
BV
747 samplerate = g_variant_get_uint64(src->data);
748 g_debug("cli: got samplerate %"PRIu64" Hz", samplerate);
5570e0f2
BV
749 break;
750 case SR_CONF_SAMPLE_INTERVAL:
1616f663
BV
751 samplerate = g_variant_get_uint64(src->data);
752 g_debug("cli: got sample interval %"PRIu64" ms", samplerate);
7e97afa0
BV
753 break;
754 default:
755 /* Unknown metadata is not an error. */
756 break;
757 }
758 }
759 break;
760
43e5747a 761 case SR_DF_TRIGGER:
8170b8ea 762 g_debug("cli: received SR_DF_TRIGGER");
43e5747a
UH
763 if (o->format->event)
764 o->format->event(o, SR_DF_TRIGGER, &output_buf,
765 &output_len);
766 triggered = 1;
767 break;
53993299 768
43e5747a
UH
769 case SR_DF_LOGIC:
770 logic = packet->payload;
b46c4414 771 g_message("cli: received SR_DF_LOGIC, %"PRIu64" bytes", logic->length);
53993299
BV
772 sample_size = logic->unitsize;
773 if (logic->length == 0)
774 break;
775
776 /* Don't store any samples until triggered. */
777 if (opt_wait_trigger && !triggered)
778 break;
43e5747a 779
53993299
BV
780 if (limit_samples && received_samples >= limit_samples)
781 break;
43e5747a 782
53993299 783 ret = sr_filter_probes(sample_size, unitsize, logic_probelist,
f8ccd825
BV
784 logic->data, logic->length,
785 &filter_out, &filter_out_len);
53993299
BV
786 if (ret != SR_OK)
787 break;
43e5747a 788
c27450ea 789 /* What comes out of the filter is guaranteed to be packed into the
53993299
BV
790 * minimum size needed to support the number of samples at this sample
791 * size. however, the driver may have submitted too much -- cut off
792 * the buffer of the last packet according to the sample limit.
793 */
794 if (limit_samples && (received_samples + logic->length / sample_size >
795 limit_samples * sample_size))
796 filter_out_len = limit_samples * sample_size - received_samples;
43e5747a 797
c27450ea
BV
798 if (opt_output_file && default_output_format) {
799 /* Saving to a session file. */
800 g_byte_array_append(savebuf, filter_out, filter_out_len);
53993299 801 } else {
c27450ea 802 if (opt_pds) {
de0a066e 803#ifdef HAVE_SRD
c27450ea
BV
804 if (srd_session_send(received_samples, (uint8_t*)filter_out,
805 filter_out_len) != SRD_OK)
806 sr_session_stop();
de0a066e 807#endif
c27450ea
BV
808 } else {
809 output_len = 0;
810 if (o->format->data && packet->type == o->format->df_type)
811 o->format->data(o, filter_out, filter_out_len,
812 &output_buf, &output_len);
a172e2a0 813 if (output_len) {
c27450ea
BV
814 fwrite(output_buf, 1, output_len, outfile);
815 fflush(outfile);
816 g_free(output_buf);
817 }
53993299
BV
818 }
819 }
53993299 820 g_free(filter_out);
43e5747a 821
c27450ea 822 received_samples += logic->length / sample_size;
53993299
BV
823 break;
824
825 case SR_DF_ANALOG:
826 analog = packet->payload;
827 g_message("cli: received SR_DF_ANALOG, %d samples", analog->num_samples);
828 if (analog->num_samples == 0)
829 break;
830
831 if (limit_samples && received_samples >= limit_samples)
832 break;
833
48f71481
BV
834 if (o->format->data && packet->type == o->format->df_type) {
835 o->format->data(o, (const uint8_t *)analog->data,
836 analog->num_samples * sizeof(float),
837 &output_buf, &output_len);
838 if (output_buf) {
839 fwrite(output_buf, 1, output_len, outfile);
e09810e9 840 fflush(outfile);
48f71481 841 g_free(output_buf);
c8db7172 842 }
53993299
BV
843 }
844
53993299 845 received_samples += analog->num_samples;
ce48d892
BV
846 break;
847
848 case SR_DF_FRAME_BEGIN:
48f71481
BV
849 g_debug("cli: received SR_DF_FRAME_BEGIN");
850 if (o->format->event) {
851 o->format->event(o, SR_DF_FRAME_BEGIN, &output_buf,
852 &output_len);
853 if (output_buf) {
854 fwrite(output_buf, 1, output_len, outfile);
e09810e9 855 fflush(outfile);
48f71481
BV
856 g_free(output_buf);
857 }
858 }
ce48d892
BV
859 break;
860
861 case SR_DF_FRAME_END:
48f71481
BV
862 g_debug("cli: received SR_DF_FRAME_END");
863 if (o->format->event) {
864 o->format->event(o, SR_DF_FRAME_END, &output_buf,
865 &output_len);
866 if (output_buf) {
867 fwrite(output_buf, 1, output_len, outfile);
e09810e9 868 fflush(outfile);
48f71481
BV
869 g_free(output_buf);
870 }
871 }
ce48d892 872 break;
53993299
BV
873
874 default:
c27450ea 875 break;
43e5747a
UH
876 }
877
05e7fa2c
BV
878 if (o && o->format->recv) {
879 out = o->format->recv(o, sdi, packet);
880 if (out && out->len) {
881 fwrite(out->str, 1, out->len, outfile);
882 fflush(outfile);
883 }
884 }
885
bea29a44
BV
886 /* SR_DF_END needs to be handled after the output module's recv()
887 * is called, so it can properly clean up that module etc. */
888 if (packet->type == SR_DF_END) {
889 g_debug("cli: Received SR_DF_END");
890
891 if (o->format->event) {
892 o->format->event(o, SR_DF_END, &output_buf, &output_len);
893 if (output_buf) {
894 if (outfile)
895 fwrite(output_buf, 1, output_len, outfile);
896 g_free(output_buf);
897 output_len = 0;
898 }
899 }
900
901 if (limit_samples && received_samples < limit_samples)
902 g_warning("Device only sent %" PRIu64 " samples.",
903 received_samples);
904
905 if (opt_continuous)
906 g_warning("Device stopped after %" PRIu64 " samples.",
907 received_samples);
908
909 g_array_free(logic_probelist, TRUE);
910
911 if (o->format->cleanup)
912 o->format->cleanup(o);
913 g_free(o);
914 o = NULL;
915
916 if (outfile && outfile != stdout)
917 fclose(outfile);
918
919 if (opt_output_file && default_output_format) {
920 if (sr_session_save(opt_output_file, sdi, savebuf->data,
921 unitsize, savebuf->len / unitsize) != SR_OK)
922 g_critical("Failed to save session.");
923 g_byte_array_free(savebuf, FALSE);
924 }
925 }
926
43e5747a
UH
927}
928
de0a066e 929#ifdef HAVE_SRD
d142a3f7
BV
930static int opts_to_gvar(struct srd_decoder *dec, GHashTable *hash,
931 GHashTable **options)
932{
933 struct srd_decoder_option *o;
934 GSList *optl;
935 GVariant *gvar;
936 gint64 val_int;
937 int ret;
938 char *val_str, *conv;
939
940 ret = TRUE;
941 *options = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
942 (GDestroyNotify)g_variant_unref);
943
944 for (optl = dec->options; optl; optl = optl->next) {
945 o = optl->data;
946 if (!(val_str = g_hash_table_lookup(hash, o->id)))
947 /* Not specified. */
948 continue;
949 if (g_variant_is_of_type(o->def, G_VARIANT_TYPE_STRING)) {
950 gvar = g_variant_new_string(val_str);
951 } else if (g_variant_is_of_type(o->def, G_VARIANT_TYPE_INT64)) {
952 val_int = strtoll(val_str, &conv, 10);
953 if (!conv || conv == val_str) {
954 g_critical("Protocol decoder '%s' option '%s' "
955 "requires a number.", dec->name, o->id);
956 ret = FALSE;
957 break;
958 }
959 gvar = g_variant_new_int64(val_int);
960 } else {
961 g_critical("Unsupported type for option '%s' (%s)",
962 o->id, g_variant_get_type_string(o->def));
963 ret = FALSE;
964 break;
965 }
966 g_variant_ref_sink(gvar);
967 g_hash_table_insert(*options, g_strdup(o->id), gvar);
968 g_hash_table_remove(hash, o->id);
969 }
970
971 return ret;
972}
973
974static int probes_to_gvar(struct srd_decoder *dec, GHashTable *hash,
975 GHashTable **probes)
976{
977 struct srd_probe *p;
978 GSList *all_probes, *l;
979 GVariant *gvar;
980 gint32 val_int;
981 int ret;
982 char *val_str, *conv;
983
984 ret = TRUE;
985 *probes = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
986 (GDestroyNotify)g_variant_unref);
987
988 all_probes = g_slist_copy(dec->probes);
989 all_probes = g_slist_concat(all_probes, dec->opt_probes);
990 for (l = all_probes; l; l = l->next) {
991 p = l->data;
992 if (!(val_str = g_hash_table_lookup(hash, p->id)))
993 /* Not specified. */
994 continue;
995 val_int = strtoll(val_str, &conv, 10);
996 if (!conv || conv == val_str) {
997 g_critical("Protocol decoder '%s' probes '%s' "
998 "is not a number.", dec->name, p->id);
999 ret = FALSE;
1000 break;
1001 }
1002 gvar = g_variant_new_int32(val_int);
1003 g_variant_ref_sink(gvar);
1004 g_hash_table_insert(*probes, g_strdup(p->id), gvar);
1005 g_hash_table_remove(hash, p->id);
1006 }
1007 g_slist_free(all_probes);
1008
1009 return ret;
1010}
1011
9f4a898e
BV
1012/* Register the given PDs for this session.
1013 * Accepts a string of the form: "spi:sck=3:sdata=4,spi:sck=3:sdata=5"
43e5747a
UH
1014 * That will instantiate two SPI decoders on the clock but different data
1015 * lines.
1016 */
1e0f9ed9 1017static int register_pds(struct sr_dev *dev, const char *pdstring)
43e5747a 1018{
d142a3f7
BV
1019 struct srd_decoder *dec;
1020 GHashTable *pd_opthash, *options, *probes;
1021 GList *leftover, *l;
878e90d9 1022 struct srd_decoder_inst *di;
445950d3 1023 int ret;
a1418b73 1024 char **pdtokens, **pdtok, *pd_name;
43e5747a 1025
1e0f9ed9 1026 (void)dev;
43e5747a 1027
445950d3 1028 ret = 0;
120f9ee7
BV
1029 pd_ann_visible = g_hash_table_new_full(g_str_hash, g_int_equal,
1030 g_free, NULL);
a1418b73 1031 pd_name = NULL;
d142a3f7 1032 pd_opthash = options = probes = NULL;
120f9ee7 1033 pdtokens = g_strsplit(pdstring, ",", 0);
43e5747a 1034 for (pdtok = pdtokens; *pdtok; pdtok++) {
63bb454c 1035 if (!(pd_opthash = parse_generic_arg(*pdtok, TRUE))) {
8170b8ea 1036 g_critical("Invalid protocol decoder option '%s'.", *pdtok);
d142a3f7 1037 break;
43e5747a 1038 }
a1418b73 1039
9720f23a
BV
1040 pd_name = g_strdup(g_hash_table_lookup(pd_opthash, "sigrok_key"));
1041 g_hash_table_remove(pd_opthash, "sigrok_key");
2358775c 1042 if (srd_decoder_load(pd_name) != SRD_OK) {
8170b8ea 1043 g_critical("Failed to load protocol decoder %s.", pd_name);
445950d3 1044 ret = 1;
d142a3f7
BV
1045 break;
1046 }
1047 dec = srd_decoder_get_by_id(pd_name);
1048
1049 /* Convert decoder option and probe values to GVariant. */
1050 if (!opts_to_gvar(dec, pd_opthash, &options)) {
1051 ret = 1;
1052 break;
1053 }
1054 if (!probes_to_gvar(dec, pd_opthash, &probes)) {
1055 ret = 1;
1056 break;
15d920a9 1057 }
d142a3f7
BV
1058 if (g_hash_table_size(pd_opthash) > 0) {
1059 leftover = g_hash_table_get_keys(pd_opthash);
1060 for (l = leftover; l; l = l->next)
1061 g_critical("Unknown option or probe '%s'", (char *)l->data);
1062 g_list_free(leftover);
1063 break;
1064 }
1065
1066 if (!(di = srd_inst_new(pd_name, options))) {
8170b8ea 1067 g_critical("Failed to instantiate protocol decoder %s.", pd_name);
445950d3 1068 ret = 1;
d142a3f7 1069 break;
43e5747a 1070 }
120f9ee7
BV
1071
1072 /* If no annotation list was specified, add them all in now.
1073 * This will be pared down later to leave only the last PD
1074 * in the stack.
1075 */
b6bd032d
UH
1076 if (!opt_pd_annotations)
1077 g_hash_table_insert(pd_ann_visible,
1078 g_strdup(di->inst_id), NULL);
a1418b73 1079
d142a3f7
BV
1080 /* Remap the probes if needed. */
1081 if (srd_inst_probe_set_all(di, probes) != SRD_OK) {
445950d3 1082 ret = 1;
d142a3f7 1083 break;
445950d3 1084 }
67ce0d27 1085 }
43e5747a 1086
43e5747a 1087 g_strfreev(pdtokens);
9720f23a
BV
1088 if (pd_opthash)
1089 g_hash_table_destroy(pd_opthash);
d142a3f7
BV
1090 if (options)
1091 g_hash_table_destroy(options);
1092 if (probes)
1093 g_hash_table_destroy(probes);
a1418b73
BV
1094 if (pd_name)
1095 g_free(pd_name);
43e5747a 1096
445950d3 1097 return ret;
43e5747a
UH
1098}
1099
120f9ee7
BV
1100int setup_pd_stack(void)
1101{
1102 struct srd_decoder_inst *di_from, *di_to;
1103 int ret, i;
41e915a6 1104 char **pds, **ids;
120f9ee7
BV
1105
1106 /* Set up the protocol decoder stack. */
1107 pds = g_strsplit(opt_pds, ",", 0);
1108 if (g_strv_length(pds) > 1) {
1109 if (opt_pd_stack) {
1110 /* A stack setup was specified, use that. */
1111 g_strfreev(pds);
1112 pds = g_strsplit(opt_pd_stack, ",", 0);
1113 if (g_strv_length(pds) < 2) {
1114 g_strfreev(pds);
1115 g_critical("Specify at least two protocol decoders to stack.");
1116 return 1;
1117 }
1118 }
1119
41e915a6
BV
1120 /* First PD goes at the bottom of the stack. */
1121 ids = g_strsplit(pds[0], ":", 0);
1122 if (!(di_from = srd_inst_find_by_id(ids[0]))) {
1123 g_strfreev(ids);
120f9ee7
BV
1124 g_critical("Cannot stack protocol decoder '%s': "
1125 "instance not found.", pds[0]);
1126 return 1;
1127 }
41e915a6
BV
1128 g_strfreev(ids);
1129
1130 /* Every subsequent PD goes on top. */
120f9ee7 1131 for (i = 1; pds[i]; i++) {
41e915a6
BV
1132 ids = g_strsplit(pds[i], ":", 0);
1133 if (!(di_to = srd_inst_find_by_id(ids[0]))) {
1134 g_strfreev(ids);
120f9ee7
BV
1135 g_critical("Cannot stack protocol decoder '%s': "
1136 "instance not found.", pds[i]);
1137 return 1;
1138 }
41e915a6 1139 g_strfreev(ids);
120f9ee7
BV
1140 if ((ret = srd_inst_stack(di_from, di_to)) != SRD_OK)
1141 return 1;
1142
1143 /* Don't show annotation from this PD. Only the last PD in
1144 * the stack will be left on the annotation list (unless
1145 * the annotation list was specifically provided).
1146 */
478a782d 1147 if (!opt_pd_annotations)
b6bd032d
UH
1148 g_hash_table_remove(pd_ann_visible,
1149 di_from->inst_id);
120f9ee7
BV
1150
1151 di_from = di_to;
1152 }
1153 }
1154 g_strfreev(pds);
1155
1156 return 0;
1157}
1158
1159int setup_pd_annotations(void)
1160{
1161 GSList *l;
1162 struct srd_decoder *dec;
1163 int ann;
1164 char **pds, **pdtok, **keyval, **ann_descr;
1165
1166 /* Set up custom list of PDs and annotations to show. */
b6bd032d
UH
1167 if (opt_pd_annotations) {
1168 pds = g_strsplit(opt_pd_annotations, ",", 0);
120f9ee7
BV
1169 for (pdtok = pds; *pdtok && **pdtok; pdtok++) {
1170 ann = 0;
1171 keyval = g_strsplit(*pdtok, "=", 0);
1172 if (!(dec = srd_decoder_get_by_id(keyval[0]))) {
1173 g_critical("Protocol decoder '%s' not found.", keyval[0]);
1174 return 1;
1175 }
1176 if (!dec->annotations) {
1177 g_critical("Protocol decoder '%s' has no annotations.", keyval[0]);
1178 return 1;
1179 }
1180 if (g_strv_length(keyval) == 2) {
1181 for (l = dec->annotations; l; l = l->next, ann++) {
1182 ann_descr = l->data;
1183 if (!canon_cmp(ann_descr[0], keyval[1]))
1184 /* Found it. */
1185 break;
1186 }
1187 if (!l) {
1188 g_critical("Annotation '%s' not found "
1189 "for protocol decoder '%s'.", keyval[1], keyval[0]);
1190 return 1;
1191 }
1192 }
1193 g_debug("cli: showing protocol decoder annotation %d from '%s'", ann, keyval[0]);
1194 g_hash_table_insert(pd_ann_visible, g_strdup(keyval[0]), GINT_TO_POINTER(ann));
1195 g_strfreev(keyval);
1196 }
1197 g_strfreev(pds);
1198 }
1199
1200 return 0;
1201}
1202
de0a066e
BV
1203void show_pd_annotations(struct srd_proto_data *pdata, void *cb_data)
1204{
1205 int i;
1206 char **annotations;
1207 gpointer ann_format;
1208
1209 /* 'cb_data' is not used in this specific callback. */
1210 (void)cb_data;
1211
1212 if (!pd_ann_visible)
1213 return;
1214
1215 if (!g_hash_table_lookup_extended(pd_ann_visible, pdata->pdo->di->inst_id,
1216 NULL, &ann_format))
1217 /* Not in the list of PDs whose annotations we're showing. */
1218 return;
1219
1220 if (pdata->ann_format != GPOINTER_TO_INT(ann_format))
1221 /* We don't want this particular format from the PD. */
1222 return;
1223
1224 annotations = pdata->data;
1225 if (opt_loglevel > SR_LOG_WARN)
1226 printf("%"PRIu64"-%"PRIu64" ", pdata->start_sample, pdata->end_sample);
1227 printf("%s: ", pdata->pdo->proto_id);
1228 for (i = 0; annotations[i]; i++)
1229 printf("\"%s\" ", annotations[i]);
1230 printf("\n");
1231 fflush(stdout);
1232}
1233#endif
1234
ad2bc491
BV
1235int setup_output_format(void)
1236{
1237 GHashTable *fmtargs;
1238 GHashTableIter iter;
1239 gpointer key, value;
1240 struct sr_output_format **outputs;
1241 int i;
1242 char *fmtspec;
1243
1244 if (!opt_output_format) {
1245 opt_output_format = DEFAULT_OUTPUT_FORMAT;
1246 /* we'll need to remember this so when saving to a file
1247 * later, sigrok session format will be used.
1248 */
1249 default_output_format = TRUE;
1250 }
48f71481 1251
63bb454c 1252 fmtargs = parse_generic_arg(opt_output_format, TRUE);
ad2bc491
BV
1253 fmtspec = g_hash_table_lookup(fmtargs, "sigrok_key");
1254 if (!fmtspec) {
1255 g_critical("Invalid output format.");
1256 return 1;
1257 }
1258 outputs = sr_output_list();
1259 for (i = 0; outputs[i]; i++) {
1260 if (strcmp(outputs[i]->id, fmtspec))
1261 continue;
1262 g_hash_table_remove(fmtargs, "sigrok_key");
1263 output_format = outputs[i];
1264 g_hash_table_iter_init(&iter, fmtargs);
1265 while (g_hash_table_iter_next(&iter, &key, &value)) {
1266 /* only supporting one parameter per output module
1267 * for now, and only its value */
1268 output_format_param = g_strdup(value);
1269 break;
1270 }
1271 break;
1272 }
1273 if (!output_format) {
1274 g_critical("Invalid output format %s.", opt_output_format);
1275 return 1;
1276 }
1277 g_hash_table_destroy(fmtargs);
1278
1279 return 0;
1280}
1281
37d5ccc1 1282static int select_probes(struct sr_dev_inst *sdi)
43e5747a 1283{
497f5362
BV
1284 struct sr_probe *probe;
1285 GSList *selected_probes, *l;
43e5747a
UH
1286
1287 if (!opt_probes)
1288 return SR_OK;
1289
497f5362 1290 if (!(selected_probes = parse_probestring(sdi, opt_probes)))
43e5747a 1291 return SR_ERR;
43e5747a 1292
497f5362
BV
1293 for (l = sdi->probes; l; l = l->next) {
1294 probe = l->data;
1295 if (g_slist_find(selected_probes, probe))
1296 probe->enabled = TRUE;
1297 else
1298 probe->enabled = FALSE;
43e5747a 1299 }
497f5362 1300 g_slist_free(selected_probes);
43e5747a
UH
1301
1302 return SR_OK;
1303}
1304
1305/**
1306 * Return the input file format which the CLI tool should use.
1307 *
1308 * If the user specified -I / --input-format, use that one. Otherwise, try to
1309 * autodetect the format as good as possible. Failing that, return NULL.
1310 *
1311 * @param filename The filename of the input file. Must not be NULL.
1312 * @param opt The -I / --input-file option the user specified (or NULL).
1313 *
1314 * @return A pointer to the 'struct sr_input_format' that should be used,
1315 * or NULL if no input format was selected or auto-detected.
1316 */
1317static struct sr_input_format *determine_input_file_format(
1318 const char *filename, const char *opt)
1319{
1320 int i;
1321 struct sr_input_format **inputs;
1322
1323 /* If there are no input formats, return NULL right away. */
1324 inputs = sr_input_list();
1325 if (!inputs) {
8170b8ea 1326 g_critical("No supported input formats available.");
43e5747a
UH
1327 return NULL;
1328 }
1329
1330 /* If the user specified -I / --input-format, use that one. */
1331 if (opt) {
1332 for (i = 0; inputs[i]; i++) {
943d0c08 1333 if (strcasecmp(inputs[i]->id, opt))
43e5747a 1334 continue;
8170b8ea
BV
1335 g_debug("Using user-specified input file format '%s'.",
1336 inputs[i]->id);
43e5747a
UH
1337 return inputs[i];
1338 }
1339
1340 /* The user specified an unknown input format, return NULL. */
8170b8ea 1341 g_critical("Error: specified input file format '%s' is "
943d0c08 1342 "unknown.", opt);
43e5747a
UH
1343 return NULL;
1344 }
1345
1346 /* Otherwise, try to find an input module that can handle this file. */
1347 for (i = 0; inputs[i]; i++) {
1348 if (inputs[i]->format_match(filename))
1349 break;
1350 }
1351
1352 /* Return NULL if no input module wanted to touch this. */
1353 if (!inputs[i]) {
8170b8ea 1354 g_critical("Error: no matching input module found.");
43e5747a
UH
1355 return NULL;
1356 }
78912cc1
UH
1357
1358 g_debug("cli: Autodetected '%s' input format for file '%s'.",
1359 inputs[i]->id, filename);
43e5747a 1360
43e5747a
UH
1361 return inputs[i];
1362}
1363
1364static void load_input_file_format(void)
1365{
943d0c08 1366 GHashTable *fmtargs = NULL;
43e5747a
UH
1367 struct stat st;
1368 struct sr_input *in;
1369 struct sr_input_format *input_format;
943d0c08
TÅ 
1370 char *fmtspec = NULL;
1371
1372 if (opt_input_format) {
3e8e0c2d 1373 fmtargs = parse_generic_arg(opt_input_format, TRUE);
943d0c08
TÅ 
1374 fmtspec = g_hash_table_lookup(fmtargs, "sigrok_key");
1375 }
43e5747a 1376
8170b8ea 1377 if (!(input_format = determine_input_file_format(opt_input_file,
943d0c08 1378 fmtspec))) {
8170b8ea 1379 /* The exact cause was already logged. */
43e5747a 1380 return;
943d0c08 1381 }
3e8e0c2d 1382
943d0c08
TÅ 
1383 if (fmtargs)
1384 g_hash_table_remove(fmtargs, "sigrok_key");
43e5747a
UH
1385
1386 if (stat(opt_input_file, &st) == -1) {
8170b8ea 1387 g_critical("Failed to load %s: %s", opt_input_file,
43e5747a
UH
1388 strerror(errno));
1389 exit(1);
1390 }
1391
1392 /* Initialize the input module. */
c2c4a0de 1393 if (!(in = g_try_malloc(sizeof(struct sr_input)))) {
8170b8ea 1394 g_critical("Failed to allocate input module.");
43e5747a
UH
1395 exit(1);
1396 }
1397 in->format = input_format;
943d0c08 1398 in->param = fmtargs;
43e5747a 1399 if (in->format->init) {
41602d35 1400 if (in->format->init(in, opt_input_file) != SR_OK) {
8170b8ea 1401 g_critical("Input format init failed.");
43e5747a
UH
1402 exit(1);
1403 }
1404 }
1405
37d5ccc1 1406 if (select_probes(in->sdi) > 0)
c27450ea 1407 return;
43e5747a
UH
1408
1409 sr_session_new();
74f6195b 1410 sr_session_datafeed_callback_add(datafeed_in, NULL);
37d5ccc1 1411 if (sr_session_dev_add(in->sdi) != SR_OK) {
8170b8ea 1412 g_critical("Failed to use device.");
43e5747a
UH
1413 sr_session_destroy();
1414 return;
1415 }
1416
1417 input_format->loadfile(in, opt_input_file);
c27450ea 1418
43e5747a 1419 sr_session_destroy();
943d0c08
TÅ 
1420
1421 if (fmtargs)
1422 g_hash_table_destroy(fmtargs);
43e5747a
UH
1423}
1424
1425static void load_input_file(void)
1426{
1427
1428 if (sr_session_load(opt_input_file) == SR_OK) {
1429 /* sigrok session file */
74f6195b 1430 sr_session_datafeed_callback_add(datafeed_in, NULL);
43e5747a
UH
1431 sr_session_start();
1432 sr_session_run();
1433 sr_session_stop();
1434 }
1435 else {
1436 /* fall back on input modules */
1437 load_input_file_format();
1438 }
43e5747a
UH
1439}
1440
37d5ccc1 1441static int set_dev_options(struct sr_dev_inst *sdi, GHashTable *args)
43e5747a 1442{
995713f4 1443 const struct sr_config_info *srci;
43e5747a
UH
1444 GHashTableIter iter;
1445 gpointer key, value;
cfd3ec6e 1446 int ret;
2f50086c 1447 double tmp_double;
bd31fc3f 1448 uint64_t tmp_u64, p, q;
43e5747a 1449 gboolean tmp_bool;
bd31fc3f 1450 GVariant *val, *rational[2];
43e5747a
UH
1451
1452 g_hash_table_iter_init(&iter, args);
1453 while (g_hash_table_iter_next(&iter, &key, &value)) {
74b9bf0c 1454 if (!(srci = sr_config_info_name_get(key))) {
cfd3ec6e
BV
1455 g_critical("Unknown device option '%s'.", (char *) key);
1456 return SR_ERR;
1457 }
1458
1459 if ((value == NULL) &&
995713f4 1460 (srci->datatype != SR_T_BOOL)) {
cfd3ec6e
BV
1461 g_critical("Option '%s' needs a value.", (char *)key);
1462 return SR_ERR;
1463 }
1464 val = NULL;
995713f4 1465 switch (srci->datatype) {
cfd3ec6e
BV
1466 case SR_T_UINT64:
1467 ret = sr_parse_sizestring(value, &tmp_u64);
1468 if (ret != SR_OK)
8f3b8464 1469 break;
2f50086c 1470 val = g_variant_new_uint64(tmp_u64);
cfd3ec6e
BV
1471 break;
1472 case SR_T_CHAR:
2f50086c 1473 val = g_variant_new_string(value);
cfd3ec6e
BV
1474 break;
1475 case SR_T_BOOL:
1476 if (!value)
1477 tmp_bool = TRUE;
43e5747a 1478 else
cfd3ec6e 1479 tmp_bool = sr_parse_boolstring(value);
2f50086c 1480 val = g_variant_new_boolean(tmp_bool);
cfd3ec6e
BV
1481 break;
1482 case SR_T_FLOAT:
2f50086c
BV
1483 tmp_double = strtof(value, NULL);
1484 val = g_variant_new_double(tmp_double);
cfd3ec6e
BV
1485 break;
1486 case SR_T_RATIONAL_PERIOD:
bd31fc3f 1487 if ((ret = sr_parse_period(value, &p, &q)) != SR_OK)
cfd3ec6e 1488 break;
bd31fc3f
BV
1489 rational[0] = g_variant_new_uint64(p);
1490 rational[1] = g_variant_new_uint64(q);
1491 val = g_variant_new_tuple(rational, 2);
cfd3ec6e
BV
1492 break;
1493 case SR_T_RATIONAL_VOLT:
bd31fc3f 1494 if ((ret = sr_parse_voltage(value, &p, &q)) != SR_OK)
43e5747a 1495 break;
bd31fc3f
BV
1496 rational[0] = g_variant_new_uint64(p);
1497 rational[1] = g_variant_new_uint64(q);
1498 val = g_variant_new_tuple(rational, 2);
cfd3ec6e
BV
1499 break;
1500 default:
1501 ret = SR_ERR;
43e5747a 1502 }
cfd3ec6e 1503 if (val)
c0b9d56b 1504 ret = sr_config_set(sdi, srci->key, val);
cfd3ec6e
BV
1505 if (ret != SR_OK) {
1506 g_critical("Failed to set device option '%s'.", (char *)key);
1507 return ret;
43e5747a
UH
1508 }
1509 }
1510
1511 return SR_OK;
1512}
1513
2d73284e
BV
1514static void set_options(void)
1515{
1516 struct sr_dev_inst *sdi;
1517 GSList *devices;
1518 GHashTable *devargs;
1519
1520 if (!opt_dev) {
1521 g_critical("No setting specified.");
1522 return;
1523 }
1524
1525 if (!(devargs = parse_generic_arg(opt_dev, FALSE)))
1526 return;
1527
1528 if (!(devices = device_scan())) {
1529 g_critical("No devices found.");
1530 return;
1531 }
1532 sdi = devices->data;
1533
1534 sr_session_new();
1535 if (sr_session_dev_add(sdi) != SR_OK) {
1536 g_critical("Failed to use device.");
1537 return;
1538 }
1539
1540 set_dev_options(sdi, devargs);
1541
1542 sr_session_destroy();
1543 g_slist_free(devices);
1544 g_hash_table_destroy(devargs);
1545
1546}
1547
37d5ccc1 1548static int set_limit_time(const struct sr_dev_inst *sdi)
43e5747a 1549{
1616f663 1550 GVariant *gvar;
9c9c1080 1551 uint64_t time_msec;
1616f663 1552 uint64_t samplerate;
43e5747a 1553
43c062e6 1554 if (!(time_msec = sr_parse_timestring(opt_time))) {
37d5ccc1 1555 g_critical("Invalid time '%s'", opt_time);
37d5ccc1 1556 return SR_ERR;
43e5747a
UH
1557 }
1558
e4ffb9d6 1559 if (sr_dev_has_option(sdi, SR_CONF_LIMIT_MSEC)) {
1616f663
BV
1560 gvar = g_variant_new_uint64(time_msec);
1561 if (sr_config_set(sdi, SR_CONF_LIMIT_MSEC, gvar) != SR_OK) {
37d5ccc1 1562 g_critical("Failed to configure time limit.");
37d5ccc1 1563 return SR_ERR;
43e5747a 1564 }
43c062e6
BV
1565 } else if (sr_dev_has_option(sdi, SR_CONF_SAMPLERATE)) {
1566 /* Convert to samples based on the samplerate. */
1616f663
BV
1567 sr_config_get(sdi->driver, SR_CONF_SAMPLERATE, &gvar, sdi);
1568 samplerate = g_variant_get_uint64(gvar);
1569 g_variant_unref(gvar);
1570 limit_samples = (samplerate) * time_msec / (uint64_t)1000;
37d5ccc1
BV
1571 if (limit_samples == 0) {
1572 g_critical("Not enough time at this samplerate.");
37d5ccc1 1573 return SR_ERR;
43e5747a 1574 }
1616f663
BV
1575 gvar = g_variant_new_uint64(limit_samples);
1576 if (sr_config_set(sdi, SR_CONF_LIMIT_SAMPLES, gvar) != SR_OK) {
37d5ccc1 1577 g_critical("Failed to configure time-based sample limit.");
37d5ccc1 1578 return SR_ERR;
43e5747a 1579 }
43c062e6
BV
1580 } else {
1581 g_critical("This device does not support time limits.");
1582 return SR_ERR;
37d5ccc1 1583 }
43e5747a 1584
37d5ccc1
BV
1585 return SR_OK;
1586}
1587
1588static void run_session(void)
1589{
06a3fb10 1590 GSList *devices;
37d5ccc1 1591 GHashTable *devargs;
1616f663 1592 GVariant *gvar;
37d5ccc1
BV
1593 struct sr_dev_inst *sdi;
1594 int max_probes, i;
06a3fb10
BV
1595 char **triggerlist;
1596
1597 devices = device_scan();
1598 if (!devices) {
1599 g_critical("No devices found.");
1600 return;
1601 }
1602 if (g_slist_length(devices) > 1) {
1603 g_critical("sigrok-cli only supports one device for capturing.");
1604 return;
1605 }
1606 sdi = devices->data;
37d5ccc1
BV
1607
1608 sr_session_new();
74f6195b 1609 sr_session_datafeed_callback_add(datafeed_in, NULL);
37d5ccc1 1610
8ab6aafc
BV
1611 if (sr_session_dev_add(sdi) != SR_OK) {
1612 g_critical("Failed to use device.");
1613 sr_session_destroy();
1614 return;
1615 }
1616
06a3fb10
BV
1617 if (opt_dev) {
1618 if ((devargs = parse_generic_arg(opt_dev, FALSE))) {
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();
1e0f9ed9
UH
1774 else if (opt_list_devs)
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}