2 * This file is part of the sigrok-cli project.
4 * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
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.
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.
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/>.
22 #include <libsigrokdecode/libsigrokdecode.h> /* First, so we avoid a _POSIX_C_SOURCE warning. */
31 #include <sys/types.h>
35 #include <glib/gstdio.h>
36 #include <libsigrok/libsigrok.h>
37 #include "sigrok-cli.h"
39 #define DEFAULT_OUTPUT_FORMAT "bits:width=64"
41 static struct sr_context *sr_ctx = NULL;
43 static uint64_t limit_samples = 0;
44 static uint64_t limit_frames = 0;
45 static struct sr_output_format *output_format = NULL;
46 static int default_output_format = FALSE;
47 static char *output_format_param = NULL;
49 static struct srd_session *srd_sess = NULL;
50 static GHashTable *pd_ann_visible = NULL;
52 static GByteArray *savebuf;
54 static gboolean opt_version = FALSE;
55 static gint opt_loglevel = SR_LOG_WARN; /* Show errors+warnings per default. */
56 static gboolean opt_scan_devs = FALSE;
57 static gboolean opt_wait_trigger = FALSE;
58 static gchar *opt_input_file = NULL;
59 static gchar *opt_output_file = NULL;
60 static gchar *opt_drv = NULL;
61 static gchar *opt_config = NULL;
62 static gchar *opt_probes = NULL;
63 static gchar *opt_probe_group = NULL;
64 static gchar *opt_triggers = NULL;
65 static gchar *opt_pds = NULL;
67 static gchar *opt_pd_stack = NULL;
68 static gchar *opt_pd_annotations = NULL;
70 static gchar *opt_input_format = NULL;
71 static gchar *opt_output_format = NULL;
72 static gchar *opt_show = NULL;
73 static gchar *opt_time = NULL;
74 static gchar *opt_samples = NULL;
75 static gchar *opt_frames = NULL;
76 static gchar *opt_continuous = NULL;
77 static gchar *opt_set = NULL;
79 static GOptionEntry optargs[] = {
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,
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},
88 {"input-file", 'i', 0, G_OPTION_ARG_FILENAME, &opt_input_file,
89 "Load input from file", NULL},
90 {"input-format", 'I', 0, G_OPTION_ARG_STRING, &opt_input_format,
91 "Input format", NULL},
92 {"output-file", 'o', 0, G_OPTION_ARG_FILENAME, &opt_output_file,
93 "Save output to file", NULL},
94 {"output-format", 'O', 0, G_OPTION_ARG_STRING, &opt_output_format,
95 "Output format", NULL},
96 {"probes", 'p', 0, G_OPTION_ARG_STRING, &opt_probes,
97 "Probes to use", NULL},
98 {"probe-group", 'g', 0, G_OPTION_ARG_STRING, &opt_probe_group,
99 "Probe groups", NULL},
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},
105 {"protocol-decoders", 'P', 0, G_OPTION_ARG_STRING, &opt_pds,
106 "Protocol decoders to run", NULL},
107 {"protocol-decoder-stack", 'S', 0, G_OPTION_ARG_STRING, &opt_pd_stack,
108 "Protocol decoder stack", NULL},
109 {"protocol-decoder-annotations", 'A', 0, G_OPTION_ARG_STRING, &opt_pd_annotations,
110 "Protocol decoder annotation(s) to show", NULL},
112 {"scan", 0, 0, G_OPTION_ARG_NONE, &opt_scan_devs,
113 "Scan for devices", NULL},
114 {"show", 0, 0, G_OPTION_ARG_NONE, &opt_show,
115 "Show device detail", NULL},
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},
120 {"frames", 0, 0, G_OPTION_ARG_STRING, &opt_frames,
121 "Number of frames to acquire", NULL},
122 {"continuous", 0, 0, G_OPTION_ARG_NONE, &opt_continuous,
123 "Sample continuously", NULL},
124 {"set", 0, 0, G_OPTION_ARG_NONE, &opt_set, "Set device options only", NULL},
125 {NULL, 0, 0, 0, NULL, NULL, NULL}
129 /* Convert driver options hash to GSList of struct sr_config. */
130 static GSList *hash_to_hwopt(GHashTable *hash)
132 const struct sr_config_info *srci;
133 struct sr_config *src;
138 keys = g_hash_table_get_keys(hash);
140 for (gl = keys; gl; gl = gl->next) {
142 if (!(srci = sr_config_info_name_get(key))) {
143 g_critical("Unknown option %s", key);
146 src = g_try_malloc(sizeof(struct sr_config));
147 src->key = srci->key;
148 value = g_hash_table_lookup(hash, key);
149 src->data = g_variant_new_string(value);
150 opts = g_slist_append(opts, src);
157 static struct sr_probe_group *select_probe_group(struct sr_dev_inst *sdi)
159 struct sr_probe_group *pg;
162 if (!opt_probe_group)
165 if (!sdi->probe_groups) {
166 g_critical("This device does not have any probe groups.");
170 for (l = sdi->probe_groups; l; l = l->next) {
172 if (!strcasecmp(opt_probe_group, pg->name)) {
180 static void free_drvopts(struct sr_config *src)
182 g_variant_unref(src->data);
186 static GSList *device_scan(void)
188 struct sr_dev_driver **drivers, *driver;
190 GSList *drvopts, *devices, *tmpdevs, *l;
195 drvargs = parse_generic_arg(opt_drv, TRUE);
196 drvname = g_strdup(g_hash_table_lookup(drvargs, "sigrok_key"));
197 g_hash_table_remove(drvargs, "sigrok_key");
199 drivers = sr_driver_list();
200 for (i = 0; drivers[i]; i++) {
201 if (strcmp(drivers[i]->name, drvname))
206 g_critical("Driver %s not found.", drvname);
207 g_hash_table_destroy(drvargs);
212 if (sr_driver_init(sr_ctx, driver) != SR_OK) {
213 g_critical("Failed to initialize driver.");
214 g_hash_table_destroy(drvargs);
218 if (g_hash_table_size(drvargs) > 0) {
219 if (!(drvopts = hash_to_hwopt(drvargs))) {
220 /* Unknown options, already logged. */
221 g_hash_table_destroy(drvargs);
225 g_hash_table_destroy(drvargs);
226 devices = sr_driver_scan(driver, drvopts);
227 g_slist_free_full(drvopts, (GDestroyNotify)free_drvopts);
229 /* No driver specified, let them all scan on their own. */
231 drivers = sr_driver_list();
232 for (i = 0; drivers[i]; i++) {
234 if (sr_driver_init(sr_ctx, driver) != SR_OK) {
235 g_critical("Failed to initialize driver.");
238 tmpdevs = sr_driver_scan(driver, NULL);
239 for (l = tmpdevs; l; l = l->next)
240 devices = g_slist_append(devices, l->data);
241 g_slist_free(tmpdevs);
248 static void show_version(void)
250 struct sr_dev_driver **drivers;
251 struct sr_input_format **inputs;
252 struct sr_output_format **outputs;
255 struct srd_decoder *dec;
259 printf("sigrok-cli %s\n\n", VERSION);
261 printf("Using libsigrok %s (lib version %s).\n",
262 sr_package_version_string_get(), sr_lib_version_string_get());
264 printf("Using libsigrokdecode %s (lib version %s).\n\n",
265 srd_package_version_string_get(), srd_lib_version_string_get());
268 printf("Supported hardware drivers:\n");
269 drivers = sr_driver_list();
270 for (i = 0; drivers[i]; i++) {
271 printf(" %-20s %s\n", drivers[i]->name, drivers[i]->longname);
275 printf("Supported input formats:\n");
276 inputs = sr_input_list();
277 for (i = 0; inputs[i]; i++)
278 printf(" %-20s %s\n", inputs[i]->id, inputs[i]->description);
281 printf("Supported output formats:\n");
282 outputs = sr_output_list();
283 for (i = 0; outputs[i]; i++)
284 printf(" %-20s %s\n", outputs[i]->id, outputs[i]->description);
285 printf(" %-20s %s\n", "sigrok", "Default file output format");
289 if (srd_init(NULL) == SRD_OK) {
290 printf("Supported protocol decoders:\n");
291 srd_decoder_load_all();
292 for (l = srd_decoder_list(); l; l = l->next) {
294 printf(" %-20s %s\n", dec->id, dec->longname);
295 /* Print protocol description upon "-l 3" or higher. */
296 if (opt_loglevel >= SR_LOG_INFO)
297 printf(" %-20s %s\n", "", dec->desc);
305 static void print_dev_line(const struct sr_dev_inst *sdi)
307 struct sr_probe *probe;
312 s = g_string_sized_new(128);
313 g_string_assign(s, sdi->driver->name);
314 if (sr_config_get(sdi->driver, sdi, NULL, SR_CONF_CONN, &gvar) == SR_OK) {
315 g_string_append(s, ":conn=");
316 g_string_append(s, g_variant_get_string(gvar, NULL));
317 g_variant_unref(gvar);
319 g_string_append(s, " - ");
320 if (sdi->vendor && sdi->vendor[0])
321 g_string_append_printf(s, "%s ", sdi->vendor);
322 if (sdi->model && sdi->model[0])
323 g_string_append_printf(s, "%s ", sdi->model);
324 if (sdi->version && sdi->version[0])
325 g_string_append_printf(s, "%s ", sdi->version);
327 if (g_slist_length(sdi->probes) == 1) {
328 probe = sdi->probes->data;
329 g_string_append_printf(s, "with 1 probe: %s", probe->name);
331 g_string_append_printf(s, "with %d probes:", g_slist_length(sdi->probes));
332 for (l = sdi->probes; l; l = l->next) {
334 g_string_append_printf(s, " %s", probe->name);
338 g_string_append_printf(s, "\n");
339 printf("%s", s->str);
340 g_string_free(s, TRUE);
344 static void show_dev_list(void)
346 struct sr_dev_inst *sdi;
349 if (!(devices = device_scan()))
352 printf("The following devices were found:\n");
353 for (l = devices; l; l = l->next) {
357 g_slist_free(devices);
361 static void show_dev_detail(void)
363 struct sr_dev_inst *sdi;
364 const struct sr_config_info *srci;
365 struct sr_probe *probe;
366 struct sr_probe_group *probe_group, *pg;
367 GSList *devices, *pgl, *prl;
368 GVariant *gvar_opts, *gvar_dict, *gvar_list, *gvar;
369 gsize num_opts, num_elements;
370 const uint64_t *uint64, p, q, low, high;
371 uint64_t cur_low, cur_high;
373 unsigned int num_devices, o, i;
376 const char *charopts, **stropts;
378 if (!(devices = device_scan())) {
379 g_critical("No devices found.");
383 num_devices = g_slist_length(devices);
384 if (num_devices > 1) {
385 g_critical("%d devices found. Use --scan to show them, "
386 "and select one to show.", num_devices);
393 if (sr_dev_open(sdi) != SR_OK) {
394 g_critical("Failed to open device.");
398 if ((sr_config_list(sdi->driver, NULL, NULL, SR_CONF_SCAN_OPTIONS,
399 &gvar_opts) == SR_OK)) {
400 opts = g_variant_get_fixed_array(gvar_opts, &num_elements,
402 printf("Supported driver options:\n");
403 for (i = 0; i < num_elements; i++) {
404 if (!(srci = sr_config_info_get(opts[i])))
406 printf(" %s\n", srci->id);
408 g_variant_unref(gvar_opts);
411 probe_group = select_probe_group(sdi);
412 if ((sr_config_list(sdi->driver, sdi, probe_group, SR_CONF_DEVICE_OPTIONS,
413 &gvar_opts)) != SR_OK)
414 /* Driver supports no device instance options. */
417 if (sdi->probe_groups) {
418 printf("Probe groups:\n");
419 for (pgl = sdi->probe_groups; pgl; pgl = pgl->next) {
421 printf(" %s: channel%s", pg->name,
422 g_slist_length(pg->probes) > 1 ? "s" : "");
423 for (prl = pg->probes; prl; prl = prl->next) {
425 printf(" %s", probe->name);
431 printf("Supported configuration options");
432 if (sdi->probe_groups) {
434 printf(" across all probe groups");
436 printf(" on probe group %s", probe_group->name);
439 opts = g_variant_get_fixed_array(gvar_opts, &num_opts, sizeof(int32_t));
440 for (o = 0; o < num_opts; o++) {
441 if (!(srci = sr_config_info_get(opts[o])))
444 if (srci->key == SR_CONF_TRIGGER_TYPE) {
445 if (sr_config_list(sdi->driver, sdi, probe_group, srci->key,
450 charopts = g_variant_get_string(gvar, NULL);
451 printf(" Supported triggers: ");
453 printf("%c ", *charopts);
457 g_variant_unref(gvar);
459 } else if (srci->key == SR_CONF_PATTERN_MODE) {
460 /* Pattern generator modes */
461 printf(" %s", srci->id);
462 if (sr_config_list(sdi->driver, sdi, probe_group, srci->key,
464 printf(" - supported patterns:\n");
465 stropts = g_variant_get_strv(gvar, &num_elements);
466 for (i = 0; i < num_elements; i++)
467 printf(" %s\n", stropts[i]);
468 g_variant_unref(gvar);
473 } else if (srci->key == SR_CONF_SAMPLERATE) {
474 /* Supported samplerates */
475 printf(" %s", srci->id);
476 if (sr_config_list(sdi->driver, sdi, probe_group, SR_CONF_SAMPLERATE,
477 &gvar_dict) != SR_OK) {
481 if ((gvar_list = g_variant_lookup_value(gvar_dict,
482 "samplerates", G_VARIANT_TYPE("at")))) {
483 uint64 = g_variant_get_fixed_array(gvar_list,
484 &num_elements, sizeof(uint64_t));
485 printf(" - supported samplerates:\n");
486 for (i = 0; i < num_elements; i++) {
487 if (!(s = sr_samplerate_string(uint64[i])))
492 g_variant_unref(gvar_list);
493 } else if ((gvar_list = g_variant_lookup_value(gvar_dict,
494 "samplerate-steps", G_VARIANT_TYPE("at")))) {
495 uint64 = g_variant_get_fixed_array(gvar_list,
496 &num_elements, sizeof(uint64_t));
498 if (!(s = sr_samplerate_string(uint64[0])))
503 if (!(s = sr_samplerate_string(uint64[1])))
508 if (!(s = sr_samplerate_string(uint64[2])))
510 printf(" in steps of %s)\n", s);
512 g_variant_unref(gvar_list);
514 g_variant_unref(gvar_dict);
516 } else if (srci->key == SR_CONF_BUFFERSIZE) {
517 /* Supported buffer sizes */
518 printf(" %s", srci->id);
519 if (sr_config_list(sdi->driver, sdi, probe_group,
520 SR_CONF_BUFFERSIZE, &gvar_list) != SR_OK) {
524 uint64 = g_variant_get_fixed_array(gvar_list,
525 &num_elements, sizeof(uint64_t));
526 printf(" - supported buffer sizes:\n");
527 for (i = 0; i < num_elements; i++)
528 printf(" %"PRIu64"\n", uint64[i]);
529 g_variant_unref(gvar_list);
531 } else if (srci->key == SR_CONF_TIMEBASE) {
532 /* Supported time bases */
533 printf(" %s", srci->id);
534 if (sr_config_list(sdi->driver, sdi, probe_group,
535 SR_CONF_TIMEBASE, &gvar_list) != SR_OK) {
539 printf(" - supported time bases:\n");
540 num_elements = g_variant_n_children(gvar_list);
541 for (i = 0; i < num_elements; i++) {
542 gvar = g_variant_get_child_value(gvar_list, i);
543 g_variant_get(gvar, "(tt)", &p, &q);
544 s = sr_period_string(p * q);
548 g_variant_unref(gvar_list);
550 } else if (srci->key == SR_CONF_VDIV) {
551 /* Supported volts/div values */
552 printf(" %s", srci->id);
553 if (sr_config_list(sdi->driver, sdi, probe_group,
554 SR_CONF_VDIV, &gvar_list) != SR_OK) {
558 printf(" - supported volts/div:\n");
559 num_elements = g_variant_n_children(gvar_list);
560 for (i = 0; i < num_elements; i++) {
561 gvar = g_variant_get_child_value(gvar_list, i);
562 g_variant_get(gvar, "(tt)", &p, &q);
563 s = sr_voltage_string(p, q);
567 g_variant_unref(gvar_list);
569 } else if (srci->datatype == SR_T_CHAR) {
570 printf(" %s: ", srci->id);
571 if (sr_config_get(sdi->driver, sdi, probe_group, srci->key,
573 tmp_str = g_strdup(g_variant_get_string(gvar, NULL));
574 g_variant_unref(gvar);
578 if (sr_config_list(sdi->driver, sdi, probe_group, srci->key,
584 stropts = g_variant_get_strv(gvar, &num_elements);
585 for (i = 0; i < num_elements; i++) {
588 printf("%s", stropts[i]);
589 if (tmp_str && !strcmp(tmp_str, stropts[i]))
590 printf(" (current)");
595 g_variant_unref(gvar);
597 } else if (srci->datatype == SR_T_UINT64_RANGE) {
598 printf(" %s: ", srci->id);
599 if (sr_config_list(sdi->driver, sdi, probe_group, srci->key,
600 &gvar_list) != SR_OK) {
605 if (sr_config_get(sdi->driver, sdi, NULL, srci->key, &gvar) == SR_OK) {
606 g_variant_get(gvar, "(tt)", &cur_low, &cur_high);
607 g_variant_unref(gvar);
613 num_elements = g_variant_n_children(gvar_list);
614 for (i = 0; i < num_elements; i++) {
615 gvar = g_variant_get_child_value(gvar_list, i);
616 g_variant_get(gvar, "(tt)", &low, &high);
617 g_variant_unref(gvar);
620 printf("%"PRIu64"-%"PRIu64, low, high);
621 if (low == cur_low && high == cur_high)
622 printf(" (current)");
625 g_variant_unref(gvar_list);
627 } else if (srci->datatype == SR_T_BOOL) {
628 printf(" %s: ", srci->id);
629 if (sr_config_get(sdi->driver, sdi, NULL, srci->key,
631 if (g_variant_get_boolean(gvar))
632 printf("on (current), off\n");
634 printf("on, off (current)\n");
635 g_variant_unref(gvar);
641 /* Everything else */
642 printf(" %s\n", srci->id);
645 g_variant_unref(gvar_opts);
648 g_slist_free(devices);
653 static void show_pd_detail(void)
656 struct srd_decoder *dec;
657 struct srd_decoder_option *o;
658 char **pdtokens, **pdtok, *optsep, **ann, *val, *doc;
661 pdtokens = g_strsplit(opt_pds, ",", -1);
662 for (pdtok = pdtokens; *pdtok; pdtok++) {
664 if ((optsep = strchr(*pdtok, ':')))
666 if (!(dec = srd_decoder_get_by_id(*pdtok))) {
667 g_critical("Protocol decoder %s not found.", *pdtok);
670 printf("ID: %s\nName: %s\nLong name: %s\nDescription: %s\n",
671 dec->id, dec->name, dec->longname, dec->desc);
672 printf("License: %s\n", dec->license);
673 printf("Annotations:\n");
674 if (dec->annotations) {
675 for (l = dec->annotations; l; l = l->next) {
677 printf("- %s\n %s\n", ann[0], ann[1]);
682 printf("Required probes:\n");
684 for (l = dec->probes; l; l = l->next) {
686 printf("- %s (%s): %s\n",
687 p->name, p->id, p->desc);
692 printf("Optional probes:\n");
693 if (dec->opt_probes) {
694 for (l = dec->opt_probes; l; l = l->next) {
696 printf("- %s (%s): %s\n",
697 p->name, p->id, p->desc);
703 printf("Options:\n");
704 for (l = dec->options; l; l = l->next) {
706 val = g_variant_print(o->def, FALSE);
707 printf("- %s: %s (default %s)\n", o->id, o->desc, val);
711 if ((doc = srd_decoder_doc_get(dec))) {
712 printf("Documentation:\n%s\n",
713 doc[0] == '\n' ? doc + 1 : doc);
718 g_strfreev(pdtokens);
722 static GArray *get_enabled_logic_probes(const struct sr_dev_inst *sdi)
724 struct sr_probe *probe;
728 probes = g_array_new(FALSE, FALSE, sizeof(int));
729 for (l = sdi->probes; l; l = l->next) {
731 if (probe->type != SR_PROBE_LOGIC)
733 if (probe->enabled != TRUE)
735 g_array_append_val(probes, probe->index);
741 static void datafeed_in(const struct sr_dev_inst *sdi,
742 const struct sr_datafeed_packet *packet, void *cb_data)
744 const struct sr_datafeed_meta *meta;
745 const struct sr_datafeed_logic *logic;
746 const struct sr_datafeed_analog *analog;
747 struct sr_config *src;
748 static struct sr_output *o = NULL;
749 static GArray *logic_probelist = NULL;
750 static uint64_t received_samples = 0;
751 static int unitsize = 0;
752 static int triggered = 0;
753 static FILE *outfile = NULL;
756 int sample_size, ret;
757 uint64_t samplerate, output_len, filter_out_len, end_sample;
758 uint8_t *output_buf, *filter_out;
762 /* If the first packet to come in isn't a header, don't even try. */
763 if (packet->type != SR_DF_HEADER && o == NULL)
767 switch (packet->type) {
769 g_debug("cli: Received SR_DF_HEADER");
770 /* Initialize the output module. */
771 if (!(o = g_try_malloc(sizeof(struct sr_output)))) {
772 g_critical("Output module malloc failed.");
775 o->format = output_format;
776 o->sdi = (struct sr_dev_inst *)sdi;
777 o->param = output_format_param;
778 if (o->format->init) {
779 if (o->format->init(o) != SR_OK) {
780 g_critical("Output format initialization failed.");
785 /* Prepare non-stdout output. */
787 if (opt_output_file) {
788 if (default_output_format) {
789 /* output file is in session format, so we'll
790 * keep a copy of everything as it comes in
791 * and save from there after the session. */
793 savebuf = g_byte_array_new();
795 /* saving to a file in whatever format was set
796 * with --format, so all we need is a filehandle */
797 outfile = g_fopen(opt_output_file, "wb");
801 /* Prepare for logic data. */
802 logic_probelist = get_enabled_logic_probes(sdi);
803 /* How many bytes we need to store the packed samples. */
804 unitsize = (logic_probelist->len + 7) / 8;
808 if (opt_pds && logic_probelist->len) {
809 if (sr_config_get(sdi->driver, sdi, NULL, SR_CONF_SAMPLERATE,
811 samplerate = g_variant_get_uint64(gvar);
812 g_variant_unref(gvar);
813 if (srd_session_metadata_set(srd_sess, SRD_CONF_SAMPLERATE,
814 g_variant_new_uint64(samplerate)) != SRD_OK) {
815 g_critical("Failed to configure decode session.");
819 if (srd_session_start(srd_sess) != SRD_OK) {
820 g_critical("Failed to start decode session.");
828 g_debug("cli: received SR_DF_META");
829 meta = packet->payload;
830 for (l = meta->config; l; l = l->next) {
833 case SR_CONF_SAMPLERATE:
834 samplerate = g_variant_get_uint64(src->data);
835 g_debug("cli: got samplerate %"PRIu64" Hz", samplerate);
838 if (srd_session_metadata_set(srd_sess, SRD_CONF_SAMPLERATE,
839 g_variant_new_uint64(samplerate)) != SRD_OK) {
840 g_critical("Failed to pass samplerate to decoder.");
845 case SR_CONF_SAMPLE_INTERVAL:
846 samplerate = g_variant_get_uint64(src->data);
847 g_debug("cli: got sample interval %"PRIu64" ms", samplerate);
850 /* Unknown metadata is not an error. */
857 g_debug("cli: received SR_DF_TRIGGER");
858 if (o->format->event)
859 o->format->event(o, SR_DF_TRIGGER, &output_buf,
865 logic = packet->payload;
866 g_message("cli: received SR_DF_LOGIC, %"PRIu64" bytes", logic->length);
867 sample_size = logic->unitsize;
868 if (logic->length == 0)
871 /* Don't store any samples until triggered. */
872 if (opt_wait_trigger && !triggered)
875 if (limit_samples && received_samples >= limit_samples)
878 ret = sr_filter_probes(sample_size, unitsize, logic_probelist,
879 logic->data, logic->length,
880 &filter_out, &filter_out_len);
885 * What comes out of the filter is guaranteed to be packed into the
886 * minimum size needed to support the number of samples at this sample
887 * size. however, the driver may have submitted too much. Cut off
888 * the buffer of the last packet according to the sample limit.
890 if (limit_samples && (received_samples + logic->length / sample_size >
891 limit_samples * sample_size))
892 filter_out_len = limit_samples * sample_size - received_samples;
894 if (opt_output_file && default_output_format) {
895 /* Saving to a session file. */
896 g_byte_array_append(savebuf, filter_out, filter_out_len);
900 end_sample = received_samples + filter_out_len / unitsize;
901 if (srd_session_send(srd_sess, received_samples, end_sample,
902 (uint8_t*)filter_out, filter_out_len) != SRD_OK)
907 if (o->format->data && packet->type == o->format->df_type)
908 o->format->data(o, filter_out, filter_out_len,
909 &output_buf, &output_len);
911 fwrite(output_buf, 1, output_len, outfile);
919 received_samples += logic->length / sample_size;
923 analog = packet->payload;
924 g_message("cli: received SR_DF_ANALOG, %d samples", analog->num_samples);
925 if (analog->num_samples == 0)
928 if (limit_samples && received_samples >= limit_samples)
931 if (o->format->data && packet->type == o->format->df_type) {
932 o->format->data(o, (const uint8_t *)analog->data,
933 analog->num_samples * sizeof(float),
934 &output_buf, &output_len);
936 fwrite(output_buf, 1, output_len, outfile);
942 received_samples += analog->num_samples;
945 case SR_DF_FRAME_BEGIN:
946 g_debug("cli: received SR_DF_FRAME_BEGIN");
947 if (o->format->event) {
948 o->format->event(o, SR_DF_FRAME_BEGIN, &output_buf,
951 fwrite(output_buf, 1, output_len, outfile);
958 case SR_DF_FRAME_END:
959 g_debug("cli: received SR_DF_FRAME_END");
960 if (o->format->event) {
961 o->format->event(o, SR_DF_FRAME_END, &output_buf,
964 fwrite(output_buf, 1, output_len, outfile);
975 if (o && o->format->receive) {
976 if (o->format->receive(o, sdi, packet, &out) == SR_OK && out) {
977 fwrite(out->str, 1, out->len, outfile);
979 g_string_free(out, TRUE);
983 /* SR_DF_END needs to be handled after the output module's receive()
984 * is called, so it can properly clean up that module etc. */
985 if (packet->type == SR_DF_END) {
986 g_debug("cli: Received SR_DF_END");
988 if (o->format->event) {
989 o->format->event(o, SR_DF_END, &output_buf, &output_len);
992 fwrite(output_buf, 1, output_len, outfile);
998 if (limit_samples && received_samples < limit_samples)
999 g_warning("Device only sent %" PRIu64 " samples.",
1003 g_warning("Device stopped after %" PRIu64 " samples.",
1006 g_array_free(logic_probelist, TRUE);
1008 if (o->format->cleanup)
1009 o->format->cleanup(o);
1013 if (outfile && outfile != stdout)
1016 if (opt_output_file && default_output_format && savebuf->len) {
1017 if (sr_session_save(opt_output_file, sdi, savebuf->data,
1018 unitsize, savebuf->len / unitsize) != SR_OK)
1019 g_critical("Failed to save session.");
1020 g_byte_array_free(savebuf, FALSE);
1027 static int opts_to_gvar(struct srd_decoder *dec, GHashTable *hash,
1028 GHashTable **options)
1030 struct srd_decoder_option *o;
1035 char *val_str, *conv;
1038 *options = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
1039 (GDestroyNotify)g_variant_unref);
1041 for (optl = dec->options; optl; optl = optl->next) {
1043 if (!(val_str = g_hash_table_lookup(hash, o->id)))
1044 /* Not specified. */
1046 if (g_variant_is_of_type(o->def, G_VARIANT_TYPE_STRING)) {
1047 gvar = g_variant_new_string(val_str);
1048 } else if (g_variant_is_of_type(o->def, G_VARIANT_TYPE_INT64)) {
1049 val_int = strtoll(val_str, &conv, 0);
1050 if (!conv || conv == val_str) {
1051 g_critical("Protocol decoder '%s' option '%s' "
1052 "requires a number.", dec->name, o->id);
1056 gvar = g_variant_new_int64(val_int);
1058 g_critical("Unsupported type for option '%s' (%s)",
1059 o->id, g_variant_get_type_string(o->def));
1063 g_variant_ref_sink(gvar);
1064 g_hash_table_insert(*options, g_strdup(o->id), gvar);
1065 g_hash_table_remove(hash, o->id);
1071 static int probes_to_gvar(struct srd_decoder *dec, GHashTable *hash,
1072 GHashTable **probes)
1074 struct srd_probe *p;
1075 GSList *all_probes, *l;
1079 char *val_str, *conv;
1082 *probes = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
1083 (GDestroyNotify)g_variant_unref);
1085 all_probes = g_slist_copy(dec->probes);
1086 all_probes = g_slist_concat(all_probes, g_slist_copy(dec->opt_probes));
1087 for (l = all_probes; l; l = l->next) {
1089 if (!(val_str = g_hash_table_lookup(hash, p->id)))
1090 /* Not specified. */
1092 val_int = strtoll(val_str, &conv, 10);
1093 if (!conv || conv == val_str) {
1094 g_critical("Protocol decoder '%s' probes '%s' "
1095 "is not a number.", dec->name, p->id);
1099 gvar = g_variant_new_int32(val_int);
1100 g_variant_ref_sink(gvar);
1101 g_hash_table_insert(*probes, g_strdup(p->id), gvar);
1102 g_hash_table_remove(hash, p->id);
1104 g_slist_free(all_probes);
1109 /* Register the given PDs for this session.
1110 * Accepts a string of the form: "spi:sck=3:sdata=4,spi:sck=3:sdata=5"
1111 * That will instantiate two SPI decoders on the clock but different data
1114 static int register_pds(struct sr_dev *dev, const char *pdstring)
1116 struct srd_decoder *dec;
1117 GHashTable *pd_opthash, *options, *probes;
1118 GList *leftover, *l;
1119 struct srd_decoder_inst *di;
1121 char **pdtokens, **pdtok, *pd_name;
1126 pd_ann_visible = g_hash_table_new_full(g_str_hash, g_int_equal,
1129 pd_opthash = options = probes = NULL;
1130 pdtokens = g_strsplit(pdstring, ",", 0);
1131 for (pdtok = pdtokens; *pdtok; pdtok++) {
1132 if (!(pd_opthash = parse_generic_arg(*pdtok, TRUE))) {
1133 g_critical("Invalid protocol decoder option '%s'.", *pdtok);
1137 pd_name = g_strdup(g_hash_table_lookup(pd_opthash, "sigrok_key"));
1138 g_hash_table_remove(pd_opthash, "sigrok_key");
1139 if (srd_decoder_load(pd_name) != SRD_OK) {
1140 g_critical("Failed to load protocol decoder %s.", pd_name);
1144 dec = srd_decoder_get_by_id(pd_name);
1146 /* Convert decoder option and probe values to GVariant. */
1147 if (!opts_to_gvar(dec, pd_opthash, &options)) {
1151 if (!probes_to_gvar(dec, pd_opthash, &probes)) {
1155 if (g_hash_table_size(pd_opthash) > 0) {
1156 leftover = g_hash_table_get_keys(pd_opthash);
1157 for (l = leftover; l; l = l->next)
1158 g_critical("Unknown option or probe '%s'", (char *)l->data);
1159 g_list_free(leftover);
1163 if (!(di = srd_inst_new(srd_sess, pd_name, options))) {
1164 g_critical("Failed to instantiate protocol decoder %s.", pd_name);
1169 /* If no annotation list was specified, add them all in now.
1170 * This will be pared down later to leave only the last PD
1173 if (!opt_pd_annotations)
1174 g_hash_table_insert(pd_ann_visible,
1175 g_strdup(di->inst_id), NULL);
1177 /* Remap the probes if needed. */
1178 if (srd_inst_probe_set_all(di, probes) != SRD_OK) {
1184 g_strfreev(pdtokens);
1186 g_hash_table_destroy(pd_opthash);
1188 g_hash_table_destroy(options);
1190 g_hash_table_destroy(probes);
1197 int setup_pd_stack(void)
1199 struct srd_decoder_inst *di_from, *di_to;
1203 /* Set up the protocol decoder stack. */
1204 pds = g_strsplit(opt_pds, ",", 0);
1205 if (g_strv_length(pds) > 1) {
1207 /* A stack setup was specified, use that. */
1209 pds = g_strsplit(opt_pd_stack, ",", 0);
1210 if (g_strv_length(pds) < 2) {
1212 g_critical("Specify at least two protocol decoders to stack.");
1217 /* First PD goes at the bottom of the stack. */
1218 ids = g_strsplit(pds[0], ":", 0);
1219 if (!(di_from = srd_inst_find_by_id(srd_sess, ids[0]))) {
1221 g_critical("Cannot stack protocol decoder '%s': "
1222 "instance not found.", pds[0]);
1227 /* Every subsequent PD goes on top. */
1228 for (i = 1; pds[i]; i++) {
1229 ids = g_strsplit(pds[i], ":", 0);
1230 if (!(di_to = srd_inst_find_by_id(srd_sess, ids[0]))) {
1232 g_critical("Cannot stack protocol decoder '%s': "
1233 "instance not found.", pds[i]);
1237 if ((ret = srd_inst_stack(srd_sess, di_from, di_to)) != SRD_OK)
1240 /* Don't show annotation from this PD. Only the last PD in
1241 * the stack will be left on the annotation list (unless
1242 * the annotation list was specifically provided).
1244 if (!opt_pd_annotations)
1245 g_hash_table_remove(pd_ann_visible,
1256 int setup_pd_annotations(void)
1259 struct srd_decoder *dec;
1261 char **pds, **pdtok, **keyval, **ann_descr;
1263 /* Set up custom list of PDs and annotations to show. */
1264 if (opt_pd_annotations) {
1265 pds = g_strsplit(opt_pd_annotations, ",", 0);
1266 for (pdtok = pds; *pdtok && **pdtok; pdtok++) {
1268 keyval = g_strsplit(*pdtok, "=", 0);
1269 if (!(dec = srd_decoder_get_by_id(keyval[0]))) {
1270 g_critical("Protocol decoder '%s' not found.", keyval[0]);
1273 if (!dec->annotations) {
1274 g_critical("Protocol decoder '%s' has no annotations.", keyval[0]);
1277 if (g_strv_length(keyval) == 2) {
1278 for (l = dec->annotations; l; l = l->next, ann++) {
1279 ann_descr = l->data;
1280 if (!canon_cmp(ann_descr[0], keyval[1]))
1285 g_critical("Annotation '%s' not found "
1286 "for protocol decoder '%s'.", keyval[1], keyval[0]);
1290 g_debug("cli: showing protocol decoder annotation %d from '%s'", ann, keyval[0]);
1291 g_hash_table_insert(pd_ann_visible, g_strdup(keyval[0]), GINT_TO_POINTER(ann));
1300 void show_pd_annotations(struct srd_proto_data *pdata, void *cb_data)
1302 struct srd_proto_data_annotation *pda;
1303 gpointer ann_format;
1306 /* 'cb_data' is not used in this specific callback. */
1309 if (!pd_ann_visible)
1312 if (!g_hash_table_lookup_extended(pd_ann_visible, pdata->pdo->di->inst_id,
1314 /* Not in the list of PDs whose annotations we're showing. */
1318 if (pda->ann_format != GPOINTER_TO_INT(ann_format))
1319 /* We don't want this particular format from the PD. */
1322 if (opt_loglevel > SR_LOG_WARN)
1323 printf("%"PRIu64"-%"PRIu64" ", pdata->start_sample, pdata->end_sample);
1324 printf("%s: ", pdata->pdo->proto_id);
1325 for (i = 0; pda->ann_text[i]; i++)
1326 printf("\"%s\" ", pda->ann_text[i]);
1332 int setup_output_format(void)
1334 GHashTable *fmtargs;
1335 GHashTableIter iter;
1336 gpointer key, value;
1337 struct sr_output_format **outputs;
1341 if (opt_output_format && !strcmp(opt_output_format, "sigrok")) {
1342 /* Doesn't really exist as an output module - this is
1343 * the session save mode. */
1344 g_free(opt_output_format);
1345 opt_output_format = NULL;
1348 if (!opt_output_format) {
1349 opt_output_format = DEFAULT_OUTPUT_FORMAT;
1350 /* we'll need to remember this so when saving to a file
1351 * later, sigrok session format will be used.
1353 default_output_format = TRUE;
1356 fmtargs = parse_generic_arg(opt_output_format, TRUE);
1357 fmtspec = g_hash_table_lookup(fmtargs, "sigrok_key");
1359 g_critical("Invalid output format.");
1362 outputs = sr_output_list();
1363 for (i = 0; outputs[i]; i++) {
1364 if (strcmp(outputs[i]->id, fmtspec))
1366 g_hash_table_remove(fmtargs, "sigrok_key");
1367 output_format = outputs[i];
1368 g_hash_table_iter_init(&iter, fmtargs);
1369 while (g_hash_table_iter_next(&iter, &key, &value)) {
1370 /* only supporting one parameter per output module
1371 * for now, and only its value */
1372 output_format_param = g_strdup(value);
1377 if (!output_format) {
1378 g_critical("Invalid output format %s.", opt_output_format);
1381 g_hash_table_destroy(fmtargs);
1386 static int select_probes(struct sr_dev_inst *sdi)
1388 struct sr_probe *probe;
1389 GSList *selected_probes, *l;
1394 if (!(selected_probes = parse_probestring(sdi, opt_probes)))
1397 for (l = sdi->probes; l; l = l->next) {
1399 if (g_slist_find(selected_probes, probe))
1400 probe->enabled = TRUE;
1402 probe->enabled = FALSE;
1404 g_slist_free(selected_probes);
1410 * Return the input file format which the CLI tool should use.
1412 * If the user specified -I / --input-format, use that one. Otherwise, try to
1413 * autodetect the format as good as possible. Failing that, return NULL.
1415 * @param filename The filename of the input file. Must not be NULL.
1416 * @param opt The -I / --input-file option the user specified (or NULL).
1418 * @return A pointer to the 'struct sr_input_format' that should be used,
1419 * or NULL if no input format was selected or auto-detected.
1421 static struct sr_input_format *determine_input_file_format(
1422 const char *filename, const char *opt)
1425 struct sr_input_format **inputs;
1427 /* If there are no input formats, return NULL right away. */
1428 inputs = sr_input_list();
1430 g_critical("No supported input formats available.");
1434 /* If the user specified -I / --input-format, use that one. */
1436 for (i = 0; inputs[i]; i++) {
1437 if (strcasecmp(inputs[i]->id, opt))
1439 g_debug("Using user-specified input file format '%s'.",
1444 /* The user specified an unknown input format, return NULL. */
1445 g_critical("Error: specified input file format '%s' is "
1450 /* Otherwise, try to find an input module that can handle this file. */
1451 for (i = 0; inputs[i]; i++) {
1452 if (inputs[i]->format_match(filename))
1456 /* Return NULL if no input module wanted to touch this. */
1458 g_critical("Error: no matching input module found.");
1462 g_debug("cli: Autodetected '%s' input format for file '%s'.",
1463 inputs[i]->id, filename);
1468 static void load_input_file_format(void)
1470 GHashTable *fmtargs = NULL;
1472 struct sr_input *in;
1473 struct sr_input_format *input_format;
1474 char *fmtspec = NULL;
1476 if (opt_input_format) {
1477 fmtargs = parse_generic_arg(opt_input_format, TRUE);
1478 fmtspec = g_hash_table_lookup(fmtargs, "sigrok_key");
1481 if (!(input_format = determine_input_file_format(opt_input_file,
1483 /* The exact cause was already logged. */
1488 g_hash_table_remove(fmtargs, "sigrok_key");
1490 if (stat(opt_input_file, &st) == -1) {
1491 g_critical("Failed to load %s: %s", opt_input_file,
1496 /* Initialize the input module. */
1497 if (!(in = g_try_malloc(sizeof(struct sr_input)))) {
1498 g_critical("Failed to allocate input module.");
1501 in->format = input_format;
1502 in->param = fmtargs;
1503 if (in->format->init) {
1504 if (in->format->init(in, opt_input_file) != SR_OK) {
1505 g_critical("Input format init failed.");
1510 if (select_probes(in->sdi) > 0)
1514 sr_session_datafeed_callback_add(datafeed_in, NULL);
1515 if (sr_session_dev_add(in->sdi) != SR_OK) {
1516 g_critical("Failed to use device.");
1517 sr_session_destroy();
1521 input_format->loadfile(in, opt_input_file);
1523 sr_session_destroy();
1526 g_hash_table_destroy(fmtargs);
1529 static void load_input_file(void)
1532 if (sr_session_load(opt_input_file) == SR_OK) {
1533 /* sigrok session file */
1534 sr_session_datafeed_callback_add(datafeed_in, NULL);
1540 /* fall back on input modules */
1541 load_input_file_format();
1545 static int set_dev_options(struct sr_dev_inst *sdi, GHashTable *args)
1547 const struct sr_config_info *srci;
1548 struct sr_probe_group *pg;
1549 GHashTableIter iter;
1550 gpointer key, value;
1553 uint64_t tmp_u64, p, q, low, high;
1555 GVariant *val, *rational[2], *range[2];
1557 g_hash_table_iter_init(&iter, args);
1558 while (g_hash_table_iter_next(&iter, &key, &value)) {
1559 if (!(srci = sr_config_info_name_get(key))) {
1560 g_critical("Unknown device option '%s'.", (char *) key);
1564 if ((value == NULL) &&
1565 (srci->datatype != SR_T_BOOL)) {
1566 g_critical("Option '%s' needs a value.", (char *)key);
1570 switch (srci->datatype) {
1572 ret = sr_parse_sizestring(value, &tmp_u64);
1575 val = g_variant_new_uint64(tmp_u64);
1578 val = g_variant_new_string(value);
1584 tmp_bool = sr_parse_boolstring(value);
1585 val = g_variant_new_boolean(tmp_bool);
1588 tmp_double = strtof(value, NULL);
1589 val = g_variant_new_double(tmp_double);
1591 case SR_T_RATIONAL_PERIOD:
1592 if ((ret = sr_parse_period(value, &p, &q)) != SR_OK)
1594 rational[0] = g_variant_new_uint64(p);
1595 rational[1] = g_variant_new_uint64(q);
1596 val = g_variant_new_tuple(rational, 2);
1598 case SR_T_RATIONAL_VOLT:
1599 if ((ret = sr_parse_voltage(value, &p, &q)) != SR_OK)
1601 rational[0] = g_variant_new_uint64(p);
1602 rational[1] = g_variant_new_uint64(q);
1603 val = g_variant_new_tuple(rational, 2);
1605 case SR_T_UINT64_RANGE:
1606 if (sscanf(value, "%"PRIu64"-%"PRIu64, &low, &high) != 2) {
1610 range[0] = g_variant_new_uint64(low);
1611 range[1] = g_variant_new_uint64(high);
1612 val = g_variant_new_tuple(range, 2);
1619 pg = select_probe_group(sdi);
1620 ret = sr_config_set(sdi, pg, srci->key, val);
1623 g_critical("Failed to set device option '%s'.", (char *)key);
1631 static void set_options(void)
1633 struct sr_dev_inst *sdi;
1635 GHashTable *devargs;
1638 g_critical("No setting specified.");
1642 if (!(devargs = parse_generic_arg(opt_config, FALSE)))
1645 if (!(devices = device_scan())) {
1646 g_critical("No devices found.");
1649 sdi = devices->data;
1651 if (sr_dev_open(sdi) != SR_OK) {
1652 g_critical("Failed to open device.");
1656 set_dev_options(sdi, devargs);
1659 g_slist_free(devices);
1660 g_hash_table_destroy(devargs);
1664 static int set_limit_time(const struct sr_dev_inst *sdi)
1668 uint64_t samplerate;
1670 if (!(time_msec = sr_parse_timestring(opt_time))) {
1671 g_critical("Invalid time '%s'", opt_time);
1675 if (sr_dev_has_option(sdi, SR_CONF_LIMIT_MSEC)) {
1676 gvar = g_variant_new_uint64(time_msec);
1677 if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_MSEC, gvar) != SR_OK) {
1678 g_critical("Failed to configure time limit.");
1681 } else if (sr_dev_has_option(sdi, SR_CONF_SAMPLERATE)) {
1682 /* Convert to samples based on the samplerate. */
1683 sr_config_get(sdi->driver, sdi, NULL, SR_CONF_SAMPLERATE, &gvar);
1684 samplerate = g_variant_get_uint64(gvar);
1685 g_variant_unref(gvar);
1686 limit_samples = (samplerate) * time_msec / (uint64_t)1000;
1687 if (limit_samples == 0) {
1688 g_critical("Not enough time at this samplerate.");
1691 gvar = g_variant_new_uint64(limit_samples);
1692 if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_SAMPLES, gvar) != SR_OK) {
1693 g_critical("Failed to configure time-based sample limit.");
1697 g_critical("This device does not support time limits.");
1704 static void run_session(void)
1707 GHashTable *devargs;
1709 struct sr_dev_inst *sdi;
1713 devices = device_scan();
1715 g_critical("No devices found.");
1718 if (g_slist_length(devices) > 1) {
1719 g_critical("sigrok-cli only supports one device for capturing.");
1722 sdi = devices->data;
1725 sr_session_datafeed_callback_add(datafeed_in, NULL);
1727 if (sr_dev_open(sdi) != SR_OK) {
1728 g_critical("Failed to open device.");
1732 if (sr_session_dev_add(sdi) != SR_OK) {
1733 g_critical("Failed to add device to session.");
1734 sr_session_destroy();
1739 if ((devargs = parse_generic_arg(opt_config, FALSE))) {
1740 if (set_dev_options(sdi, devargs) != SR_OK)
1742 g_hash_table_destroy(devargs);
1746 if (select_probes(sdi) != SR_OK) {
1747 g_critical("Failed to set probes.");
1748 sr_session_destroy();
1753 if (!(triggerlist = sr_parse_triggerstring(sdi, opt_triggers))) {
1754 sr_session_destroy();
1757 max_probes = g_slist_length(sdi->probes);
1758 for (i = 0; i < max_probes; i++) {
1759 if (triggerlist[i]) {
1760 sr_dev_trigger_set(sdi, i, triggerlist[i]);
1761 g_free(triggerlist[i]);
1764 g_free(triggerlist);
1767 if (opt_continuous) {
1768 if (!sr_dev_has_option(sdi, SR_CONF_CONTINUOUS)) {
1769 g_critical("This device does not support continuous sampling.");
1770 sr_session_destroy();
1776 if (set_limit_time(sdi) != SR_OK) {
1777 sr_session_destroy();
1783 if ((sr_parse_sizestring(opt_samples, &limit_samples) != SR_OK)) {
1784 g_critical("Invalid sample limit '%s'.", opt_samples);
1785 sr_session_destroy();
1788 gvar = g_variant_new_uint64(limit_samples);
1789 if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_SAMPLES, gvar) != SR_OK) {
1790 g_critical("Failed to configure sample limit.");
1791 sr_session_destroy();
1797 if ((sr_parse_sizestring(opt_frames, &limit_frames) != SR_OK)) {
1798 g_critical("Invalid sample limit '%s'.", opt_samples);
1799 sr_session_destroy();
1802 gvar = g_variant_new_uint64(limit_frames);
1803 if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_FRAMES, gvar) != SR_OK) {
1804 g_critical("Failed to configure frame limit.");
1805 sr_session_destroy();
1810 if (sr_session_start() != SR_OK) {
1811 g_critical("Failed to start session.");
1812 sr_session_destroy();
1824 sr_session_datafeed_callback_remove_all();
1825 sr_session_destroy();
1826 g_slist_free(devices);
1830 static void logger(const gchar *log_domain, GLogLevelFlags log_level,
1831 const gchar *message, gpointer cb_data)
1837 * All messages, warnings, errors etc. go to stderr (not stdout) in
1838 * order to not mess up the CLI tool data output, e.g. VCD output.
1840 if (log_level & (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING)
1841 || opt_loglevel > SR_LOG_WARN) {
1842 fprintf(stderr, "%s\n", message);
1846 if (log_level & (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL))
1851 int main(int argc, char **argv)
1853 GOptionContext *context;
1858 g_log_set_default_handler(logger, NULL);
1860 context = g_option_context_new(NULL);
1861 g_option_context_add_main_entries(context, optargs, NULL);
1865 if (!g_option_context_parse(context, &argc, &argv, &error)) {
1866 g_critical("%s", error->message);
1870 /* Set the loglevel (amount of messages to output) for libsigrok. */
1871 if (sr_log_loglevel_set(opt_loglevel) != SR_OK)
1874 if (sr_init(&sr_ctx) != SR_OK)
1878 /* Set the loglevel (amount of messages to output) for libsigrokdecode. */
1879 if (srd_log_loglevel_set(opt_loglevel) != SRD_OK)
1883 if (srd_init(NULL) != SRD_OK)
1885 if (srd_session_new(&srd_sess) != SRD_OK) {
1886 g_critical("Failed to create new decode session.");
1889 if (register_pds(NULL, opt_pds) != 0)
1891 if (srd_pd_output_callback_add(srd_sess, SRD_OUTPUT_ANN,
1892 show_pd_annotations, NULL) != SRD_OK)
1894 if (setup_pd_stack() != 0)
1896 if (setup_pd_annotations() != 0)
1901 if (setup_output_format() != 0)
1906 else if (opt_scan_devs)
1909 else if (opt_pds && opt_show)
1914 else if (opt_input_file)
1918 else if (opt_samples || opt_time || opt_frames || opt_continuous)
1921 help = g_option_context_get_help(context, TRUE, NULL);
1937 g_option_context_free(context);