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