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