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