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