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_triggers = NULL;
64 static gchar *opt_pds = NULL;
66 static gchar *opt_pd_stack = NULL;
67 static gchar *opt_pd_annotations = NULL;
69 static gchar *opt_input_format = NULL;
70 static gchar *opt_output_format = NULL;
71 static gchar *opt_show = NULL;
72 static gchar *opt_time = NULL;
73 static gchar *opt_samples = NULL;
74 static gchar *opt_frames = NULL;
75 static gchar *opt_continuous = NULL;
76 static gchar *opt_set = NULL;
78 static GOptionEntry optargs[] = {
79 {"version", 'V', 0, G_OPTION_ARG_NONE, &opt_version,
80 "Show version and support list", NULL},
81 {"loglevel", 'l', 0, G_OPTION_ARG_INT, &opt_loglevel,
82 "Set loglevel (5 is most verbose)", NULL},
83 {"driver", 'd', 0, G_OPTION_ARG_STRING, &opt_drv,
84 "The driver to use", NULL},
85 {"config", 'c', 0, G_OPTION_ARG_STRING, &opt_config,
86 "Specify device configuration options", NULL},
87 {"input-file", 'i', 0, G_OPTION_ARG_FILENAME, &opt_input_file,
88 "Load input from file", NULL},
89 {"input-format", 'I', 0, G_OPTION_ARG_STRING, &opt_input_format,
90 "Input format", NULL},
91 {"output-file", 'o', 0, G_OPTION_ARG_FILENAME, &opt_output_file,
92 "Save output to file", NULL},
93 {"output-format", 'O', 0, G_OPTION_ARG_STRING, &opt_output_format,
94 "Output format", NULL},
95 {"probes", 'p', 0, G_OPTION_ARG_STRING, &opt_probes,
96 "Probes to use", NULL},
97 {"triggers", 't', 0, G_OPTION_ARG_STRING, &opt_triggers,
98 "Trigger configuration", NULL},
99 {"wait-trigger", 'w', 0, G_OPTION_ARG_NONE, &opt_wait_trigger,
100 "Wait for trigger", NULL},
102 {"protocol-decoders", 'P', 0, G_OPTION_ARG_STRING, &opt_pds,
103 "Protocol decoders to run", NULL},
104 {"protocol-decoder-stack", 'S', 0, G_OPTION_ARG_STRING, &opt_pd_stack,
105 "Protocol decoder stack", NULL},
106 {"protocol-decoder-annotations", 'A', 0, G_OPTION_ARG_STRING, &opt_pd_annotations,
107 "Protocol decoder annotation(s) to show", NULL},
109 {"scan", 0, 0, G_OPTION_ARG_NONE, &opt_scan_devs,
110 "Scan for devices", NULL},
111 {"show", 0, 0, G_OPTION_ARG_NONE, &opt_show,
112 "Show device detail", NULL},
113 {"time", 0, 0, G_OPTION_ARG_STRING, &opt_time,
114 "How long to sample (ms)", NULL},
115 {"samples", 0, 0, G_OPTION_ARG_STRING, &opt_samples,
116 "Number of samples to acquire", NULL},
117 {"frames", 0, 0, G_OPTION_ARG_STRING, &opt_frames,
118 "Number of frames to acquire", NULL},
119 {"continuous", 0, 0, G_OPTION_ARG_NONE, &opt_continuous,
120 "Sample continuously", NULL},
121 {"set", 0, 0, G_OPTION_ARG_NONE, &opt_set, "Set device options only", NULL},
122 {NULL, 0, 0, 0, NULL, NULL, NULL}
126 /* Convert driver options hash to GSList of struct sr_config. */
127 static GSList *hash_to_hwopt(GHashTable *hash)
129 const struct sr_config_info *srci;
130 struct sr_config *src;
135 keys = g_hash_table_get_keys(hash);
137 for (gl = keys; gl; gl = gl->next) {
139 if (!(srci = sr_config_info_name_get(key))) {
140 g_critical("Unknown option %s", key);
143 src = g_try_malloc(sizeof(struct sr_config));
144 src->key = srci->key;
145 value = g_hash_table_lookup(hash, key);
146 src->data = g_variant_new_string(value);
147 opts = g_slist_append(opts, src);
154 static void free_drvopts(struct sr_config *src)
156 g_variant_unref(src->data);
160 static GSList *device_scan(void)
162 struct sr_dev_driver **drivers, *driver;
164 GSList *drvopts, *devices, *tmpdevs, *l;
169 drvargs = parse_generic_arg(opt_drv, TRUE);
170 drvname = g_strdup(g_hash_table_lookup(drvargs, "sigrok_key"));
171 g_hash_table_remove(drvargs, "sigrok_key");
173 drivers = sr_driver_list();
174 for (i = 0; drivers[i]; i++) {
175 if (strcmp(drivers[i]->name, drvname))
180 g_critical("Driver %s not found.", drvname);
184 if (sr_driver_init(sr_ctx, driver) != SR_OK) {
185 g_critical("Failed to initialize driver.");
189 if (g_hash_table_size(drvargs) > 0) {
190 if (!(drvopts = hash_to_hwopt(drvargs))) {
191 /* Unknown options, already logged. */
192 g_hash_table_destroy(drvargs);
196 g_hash_table_destroy(drvargs);
197 devices = sr_driver_scan(driver, drvopts);
198 g_slist_free_full(drvopts, (GDestroyNotify)free_drvopts);
200 /* No driver specified, let them all scan on their own. */
202 drivers = sr_driver_list();
203 for (i = 0; drivers[i]; i++) {
205 if (sr_driver_init(sr_ctx, driver) != SR_OK) {
206 g_critical("Failed to initialize driver.");
209 tmpdevs = sr_driver_scan(driver, NULL);
210 for (l = tmpdevs; l; l = l->next)
211 devices = g_slist_append(devices, l->data);
212 g_slist_free(tmpdevs);
219 static void show_version(void)
221 struct sr_dev_driver **drivers;
222 struct sr_input_format **inputs;
223 struct sr_output_format **outputs;
226 struct srd_decoder *dec;
230 printf("sigrok-cli %s\n\n", VERSION);
232 printf("Using libsigrok %s (lib version %s).\n",
233 sr_package_version_string_get(), sr_lib_version_string_get());
235 printf("Using libsigrokdecode %s (lib version %s).\n\n",
236 srd_package_version_string_get(), srd_lib_version_string_get());
239 printf("Supported hardware drivers:\n");
240 drivers = sr_driver_list();
241 for (i = 0; drivers[i]; i++) {
242 printf(" %-20s %s\n", drivers[i]->name, drivers[i]->longname);
246 printf("Supported input formats:\n");
247 inputs = sr_input_list();
248 for (i = 0; inputs[i]; i++)
249 printf(" %-20s %s\n", inputs[i]->id, inputs[i]->description);
252 printf("Supported output formats:\n");
253 outputs = sr_output_list();
254 for (i = 0; outputs[i]; i++)
255 printf(" %-20s %s\n", outputs[i]->id, outputs[i]->description);
256 printf(" %-20s %s\n", "sigrok", "Default file output format");
260 if (srd_init(NULL) == SRD_OK) {
261 printf("Supported protocol decoders:\n");
262 srd_decoder_load_all();
263 for (l = srd_decoder_list(); l; l = l->next) {
265 printf(" %-20s %s\n", dec->id, dec->longname);
266 /* Print protocol description upon "-l 3" or higher. */
267 if (opt_loglevel >= SR_LOG_INFO)
268 printf(" %-20s %s\n", "", dec->desc);
276 static void print_dev_line(const struct sr_dev_inst *sdi)
278 struct sr_probe *probe;
283 s = g_string_sized_new(128);
284 g_string_assign(s, sdi->driver->name);
285 if (sr_config_get(sdi->driver, SR_CONF_CONN, &gvar, sdi) == SR_OK) {
286 g_string_append(s, ":conn=");
287 g_string_append(s, g_variant_get_string(gvar, NULL));
288 g_variant_unref(gvar);
290 g_string_append(s, " - ");
291 if (sdi->vendor && sdi->vendor[0])
292 g_string_append_printf(s, "%s ", sdi->vendor);
293 if (sdi->model && sdi->model[0])
294 g_string_append_printf(s, "%s ", sdi->model);
295 if (sdi->version && sdi->version[0])
296 g_string_append_printf(s, "%s ", sdi->version);
298 if (g_slist_length(sdi->probes) == 1) {
299 probe = sdi->probes->data;
300 g_string_append_printf(s, "with 1 probe: %s", probe->name);
302 g_string_append_printf(s, "with %d probes:", g_slist_length(sdi->probes));
303 for (l = sdi->probes; l; l = l->next) {
305 g_string_append_printf(s, " %s", probe->name);
309 g_string_append_printf(s, "\n");
310 printf("%s", s->str);
311 g_string_free(s, TRUE);
315 static void show_dev_list(void)
317 struct sr_dev_inst *sdi;
320 if (!(devices = device_scan()))
323 printf("The following devices were found:\n");
324 for (l = devices; l; l = l->next) {
328 g_slist_free(devices);
332 static void show_dev_detail(void)
334 struct sr_dev_inst *sdi;
335 const struct sr_config_info *srci;
337 GVariant *gvar_opts, *gvar_dict, *gvar_list, *gvar;
338 gsize num_opts, num_elements;
339 const uint64_t *uint64, p, q, low, high;
340 uint64_t cur_low, cur_high;
342 unsigned int num_devices, o, i;
345 const char *charopts, **stropts;
347 if (!(devices = device_scan())) {
348 g_critical("No devices found.");
352 num_devices = g_slist_length(devices);
353 if (num_devices > 1) {
354 g_critical("%d devices found. Use --scan to show them, "
355 "and select one to show.", num_devices);
362 if (sr_dev_open(sdi) != SR_OK) {
363 g_critical("Failed to open device.");
367 if ((sr_config_list(sdi->driver, SR_CONF_SCAN_OPTIONS, &gvar_opts,
369 opts = g_variant_get_fixed_array(gvar_opts, &num_elements,
371 printf("Supported driver options:\n");
372 for (i = 0; i < num_elements; i++) {
373 if (!(srci = sr_config_info_get(opts[i])))
375 printf(" %s\n", srci->id);
377 g_variant_unref(gvar_opts);
380 if ((sr_config_list(sdi->driver, SR_CONF_DEVICE_OPTIONS, &gvar_opts,
382 /* Driver supports no device instance options. */
385 printf("Supported configuration options:\n");
386 opts = g_variant_get_fixed_array(gvar_opts, &num_opts, sizeof(int32_t));
387 for (o = 0; o < num_opts; o++) {
388 if (!(srci = sr_config_info_get(opts[o])))
391 if (srci->key == SR_CONF_TRIGGER_TYPE) {
392 if (sr_config_list(sdi->driver, srci->key, &gvar,
397 charopts = g_variant_get_string(gvar, NULL);
398 printf(" Supported triggers: ");
400 printf("%c ", *charopts);
404 g_variant_unref(gvar);
406 } else if (srci->key == SR_CONF_PATTERN_MODE) {
407 /* Pattern generator modes */
408 printf(" %s", srci->id);
409 if (sr_config_list(sdi->driver, srci->key, &gvar,
411 printf(" - supported patterns:\n");
412 stropts = g_variant_get_strv(gvar, &num_elements);
413 for (i = 0; i < num_elements; i++)
414 printf(" %s\n", stropts[i]);
415 g_variant_unref(gvar);
420 } else if (srci->key == SR_CONF_SAMPLERATE) {
421 /* Supported samplerates */
422 printf(" %s", srci->id);
423 if (sr_config_list(sdi->driver, SR_CONF_SAMPLERATE,
424 &gvar_dict, sdi) != SR_OK) {
428 if ((gvar_list = g_variant_lookup_value(gvar_dict,
429 "samplerates", G_VARIANT_TYPE("at")))) {
430 uint64 = g_variant_get_fixed_array(gvar_list,
431 &num_elements, sizeof(uint64_t));
432 printf(" - supported samplerates:\n");
433 for (i = 0; i < num_elements; i++) {
434 if (!(s = sr_samplerate_string(uint64[i])))
439 g_variant_unref(gvar_list);
440 } else if ((gvar_list = g_variant_lookup_value(gvar_dict,
441 "samplerate-steps", G_VARIANT_TYPE("at")))) {
442 uint64 = g_variant_get_fixed_array(gvar_list,
443 &num_elements, sizeof(uint64_t));
445 if (!(s = sr_samplerate_string(uint64[0])))
450 if (!(s = sr_samplerate_string(uint64[1])))
455 if (!(s = sr_samplerate_string(uint64[2])))
457 printf(" in steps of %s)\n", s);
459 g_variant_unref(gvar_list);
461 g_variant_unref(gvar_dict);
463 } else if (srci->key == SR_CONF_BUFFERSIZE) {
464 /* Supported buffer sizes */
465 printf(" %s", srci->id);
466 if (sr_config_list(sdi->driver, SR_CONF_BUFFERSIZE,
467 &gvar_list, sdi) != SR_OK) {
471 uint64 = g_variant_get_fixed_array(gvar_list,
472 &num_elements, sizeof(uint64_t));
473 printf(" - supported buffer sizes:\n");
474 for (i = 0; i < num_elements; i++)
475 printf(" %"PRIu64"\n", uint64[i]);
476 g_variant_unref(gvar_list);
478 } else if (srci->key == SR_CONF_TIMEBASE) {
479 /* Supported time bases */
480 printf(" %s", srci->id);
481 if (sr_config_list(sdi->driver, SR_CONF_TIMEBASE,
482 &gvar_list, sdi) != SR_OK) {
486 printf(" - supported time bases:\n");
487 num_elements = g_variant_n_children(gvar_list);
488 for (i = 0; i < num_elements; i++) {
489 gvar = g_variant_get_child_value(gvar_list, i);
490 g_variant_get(gvar, "(tt)", &p, &q);
491 s = sr_period_string(p * q);
495 g_variant_unref(gvar_list);
497 } else if (srci->key == SR_CONF_VDIV) {
498 /* Supported volts/div values */
499 printf(" %s", srci->id);
500 if (sr_config_list(sdi->driver, SR_CONF_VDIV,
501 &gvar_list, sdi) != SR_OK) {
505 printf(" - supported volts/div:\n");
506 num_elements = g_variant_n_children(gvar_list);
507 for (i = 0; i < num_elements; i++) {
508 gvar = g_variant_get_child_value(gvar_list, i);
509 g_variant_get(gvar, "(tt)", &p, &q);
510 s = sr_voltage_string(p, q);
514 g_variant_unref(gvar_list);
516 } else if (srci->datatype == SR_T_CHAR) {
517 printf(" %s: ", srci->id);
518 if (sr_config_get(sdi->driver, srci->key,
519 &gvar, sdi) == SR_OK) {
520 tmp_str = g_strdup(g_variant_get_string(gvar, NULL));
521 g_variant_unref(gvar);
525 if (sr_config_list(sdi->driver, srci->key,
526 &gvar, sdi) != SR_OK) {
531 stropts = g_variant_get_strv(gvar, &num_elements);
532 for (i = 0; i < num_elements; i++) {
535 printf("%s", stropts[i]);
536 if (tmp_str && !strcmp(tmp_str, stropts[i]))
537 printf(" (current)");
542 g_variant_unref(gvar);
544 } else if (srci->datatype == SR_T_UINT64_RANGE) {
545 printf(" %s: ", srci->id);
546 if (sr_config_list(sdi->driver, srci->key,
547 &gvar_list, sdi) != SR_OK) {
552 if (sr_config_get(sdi->driver, srci->key, &gvar, sdi) == SR_OK) {
553 g_variant_get(gvar, "(tt)", &cur_low, &cur_high);
554 g_variant_unref(gvar);
560 num_elements = g_variant_n_children(gvar_list);
561 for (i = 0; i < num_elements; i++) {
562 gvar = g_variant_get_child_value(gvar_list, i);
563 g_variant_get(gvar, "(tt)", &low, &high);
564 g_variant_unref(gvar);
567 printf("%"PRIu64"-%"PRIu64, low, high);
568 if (low == cur_low && high == cur_high)
569 printf(" (current)");
572 g_variant_unref(gvar_list);
574 } else if (srci->datatype == SR_T_BOOL) {
575 printf(" %s: ", srci->id);
576 if (sr_config_get(sdi->driver, srci->key,
577 &gvar, sdi) == SR_OK) {
578 if (g_variant_get_boolean(gvar))
579 printf("on (current), off\n");
581 printf("on, off (current)\n");
582 g_variant_unref(gvar);
588 /* Everything else */
589 printf(" %s\n", srci->id);
592 g_variant_unref(gvar_opts);
595 g_slist_free(devices);
600 static void show_pd_detail(void)
603 struct srd_decoder *dec;
604 struct srd_decoder_option *o;
605 char **pdtokens, **pdtok, *optsep, **ann, *val, *doc;
608 pdtokens = g_strsplit(opt_pds, ",", -1);
609 for (pdtok = pdtokens; *pdtok; pdtok++) {
611 if ((optsep = strchr(*pdtok, ':')))
613 if (!(dec = srd_decoder_get_by_id(*pdtok))) {
614 g_critical("Protocol decoder %s not found.", *pdtok);
617 printf("ID: %s\nName: %s\nLong name: %s\nDescription: %s\n",
618 dec->id, dec->name, dec->longname, dec->desc);
619 printf("License: %s\n", dec->license);
620 printf("Annotations:\n");
621 if (dec->annotations) {
622 for (l = dec->annotations; l; l = l->next) {
624 printf("- %s\n %s\n", ann[0], ann[1]);
629 printf("Required probes:\n");
631 for (l = dec->probes; l; l = l->next) {
633 printf("- %s (%s): %s\n",
634 p->name, p->id, p->desc);
639 printf("Optional probes:\n");
640 if (dec->opt_probes) {
641 for (l = dec->opt_probes; l; l = l->next) {
643 printf("- %s (%s): %s\n",
644 p->name, p->id, p->desc);
650 printf("Options:\n");
651 for (l = dec->options; l; l = l->next) {
653 val = g_variant_print(o->def, FALSE);
654 printf("- %s: %s (default %s)\n", o->id, o->desc, val);
658 if ((doc = srd_decoder_doc_get(dec))) {
659 printf("Documentation:\n%s\n",
660 doc[0] == '\n' ? doc + 1 : doc);
665 g_strfreev(pdtokens);
669 static GArray *get_enabled_logic_probes(const struct sr_dev_inst *sdi)
671 struct sr_probe *probe;
675 probes = g_array_new(FALSE, FALSE, sizeof(int));
676 for (l = sdi->probes; l; l = l->next) {
678 if (probe->type != SR_PROBE_LOGIC)
680 if (probe->enabled != TRUE)
682 g_array_append_val(probes, probe->index);
688 static void datafeed_in(const struct sr_dev_inst *sdi,
689 const struct sr_datafeed_packet *packet, void *cb_data)
691 const struct sr_datafeed_meta *meta;
692 const struct sr_datafeed_logic *logic;
693 const struct sr_datafeed_analog *analog;
694 struct sr_config *src;
695 static struct sr_output *o = NULL;
696 static GArray *logic_probelist = NULL;
697 static uint64_t received_samples = 0;
698 static int unitsize = 0;
699 static int triggered = 0;
700 static FILE *outfile = NULL;
703 int sample_size, ret;
704 uint64_t samplerate, output_len, filter_out_len;
705 uint8_t *output_buf, *filter_out;
709 /* If the first packet to come in isn't a header, don't even try. */
710 if (packet->type != SR_DF_HEADER && o == NULL)
714 switch (packet->type) {
716 g_debug("cli: Received SR_DF_HEADER");
717 /* Initialize the output module. */
718 if (!(o = g_try_malloc(sizeof(struct sr_output)))) {
719 g_critical("Output module malloc failed.");
722 o->format = output_format;
723 o->sdi = (struct sr_dev_inst *)sdi;
724 o->param = output_format_param;
725 if (o->format->init) {
726 if (o->format->init(o) != SR_OK) {
727 g_critical("Output format initialization failed.");
732 /* Prepare non-stdout output. */
734 if (opt_output_file) {
735 if (default_output_format) {
736 /* output file is in session format, so we'll
737 * keep a copy of everything as it comes in
738 * and save from there after the session. */
740 savebuf = g_byte_array_new();
742 /* saving to a file in whatever format was set
743 * with --format, so all we need is a filehandle */
744 outfile = g_fopen(opt_output_file, "wb");
748 /* Prepare for logic data. */
749 logic_probelist = get_enabled_logic_probes(sdi);
750 /* How many bytes we need to store the packed samples. */
751 unitsize = (logic_probelist->len + 7) / 8;
755 if (opt_pds && logic_probelist->len) {
756 if (sr_config_get(sdi->driver, SR_CONF_SAMPLERATE,
757 &gvar, sdi) != SR_OK) {
758 g_critical("Unable to initialize protocol "
759 "decoders: no samplerate found.");
762 samplerate = g_variant_get_uint64(gvar);
763 g_variant_unref(gvar);
764 if (srd_session_config_set(srd_sess, SRD_CONF_NUM_PROBES,
765 g_variant_new_uint64(logic_probelist->len)) != SRD_OK) {
766 g_critical("Failed to configure decode session.");
769 if (srd_session_config_set(srd_sess, SRD_CONF_UNITSIZE,
770 g_variant_new_uint64(unitsize)) != SRD_OK) {
771 g_critical("Failed to configure decode session.");
774 if (srd_session_config_set(srd_sess, SRD_CONF_SAMPLERATE,
775 g_variant_new_uint64(samplerate)) != SRD_OK) {
776 g_critical("Failed to configure decode session.");
779 if (srd_session_start(srd_sess) != SRD_OK) {
780 g_critical("Failed to start decode session.");
788 g_debug("cli: received SR_DF_META");
789 meta = packet->payload;
790 for (l = meta->config; l; l = l->next) {
793 case SR_CONF_SAMPLERATE:
794 samplerate = g_variant_get_uint64(src->data);
795 g_debug("cli: got samplerate %"PRIu64" Hz", samplerate);
797 case SR_CONF_SAMPLE_INTERVAL:
798 samplerate = g_variant_get_uint64(src->data);
799 g_debug("cli: got sample interval %"PRIu64" ms", samplerate);
802 /* Unknown metadata is not an error. */
809 g_debug("cli: received SR_DF_TRIGGER");
810 if (o->format->event)
811 o->format->event(o, SR_DF_TRIGGER, &output_buf,
817 logic = packet->payload;
818 g_message("cli: received SR_DF_LOGIC, %"PRIu64" bytes", logic->length);
819 sample_size = logic->unitsize;
820 if (logic->length == 0)
823 /* Don't store any samples until triggered. */
824 if (opt_wait_trigger && !triggered)
827 if (limit_samples && received_samples >= limit_samples)
830 ret = sr_filter_probes(sample_size, unitsize, logic_probelist,
831 logic->data, logic->length,
832 &filter_out, &filter_out_len);
836 /* What comes out of the filter is guaranteed to be packed into the
837 * minimum size needed to support the number of samples at this sample
838 * size. however, the driver may have submitted too much -- cut off
839 * the buffer of the last packet according to the sample limit.
841 if (limit_samples && (received_samples + logic->length / sample_size >
842 limit_samples * sample_size))
843 filter_out_len = limit_samples * sample_size - received_samples;
845 if (opt_output_file && default_output_format) {
846 /* Saving to a session file. */
847 g_byte_array_append(savebuf, filter_out, filter_out_len);
851 if (srd_session_send(srd_sess, received_samples,
852 (uint8_t*)filter_out, filter_out_len) != SRD_OK)
857 if (o->format->data && packet->type == o->format->df_type)
858 o->format->data(o, filter_out, filter_out_len,
859 &output_buf, &output_len);
861 fwrite(output_buf, 1, output_len, outfile);
869 received_samples += logic->length / sample_size;
873 analog = packet->payload;
874 g_message("cli: received SR_DF_ANALOG, %d samples", analog->num_samples);
875 if (analog->num_samples == 0)
878 if (limit_samples && received_samples >= limit_samples)
881 if (o->format->data && packet->type == o->format->df_type) {
882 o->format->data(o, (const uint8_t *)analog->data,
883 analog->num_samples * sizeof(float),
884 &output_buf, &output_len);
886 fwrite(output_buf, 1, output_len, outfile);
892 received_samples += analog->num_samples;
895 case SR_DF_FRAME_BEGIN:
896 g_debug("cli: received SR_DF_FRAME_BEGIN");
897 if (o->format->event) {
898 o->format->event(o, SR_DF_FRAME_BEGIN, &output_buf,
901 fwrite(output_buf, 1, output_len, outfile);
908 case SR_DF_FRAME_END:
909 g_debug("cli: received SR_DF_FRAME_END");
910 if (o->format->event) {
911 o->format->event(o, SR_DF_FRAME_END, &output_buf,
914 fwrite(output_buf, 1, output_len, outfile);
925 if (o && o->format->receive) {
926 if (o->format->receive(o, sdi, packet, &out) == SR_OK && out) {
927 fwrite(out->str, 1, out->len, outfile);
929 g_string_free(out, TRUE);
933 /* SR_DF_END needs to be handled after the output module's receive()
934 * is called, so it can properly clean up that module etc. */
935 if (packet->type == SR_DF_END) {
936 g_debug("cli: Received SR_DF_END");
938 if (o->format->event) {
939 o->format->event(o, SR_DF_END, &output_buf, &output_len);
942 fwrite(output_buf, 1, output_len, outfile);
948 if (limit_samples && received_samples < limit_samples)
949 g_warning("Device only sent %" PRIu64 " samples.",
953 g_warning("Device stopped after %" PRIu64 " samples.",
956 g_array_free(logic_probelist, TRUE);
958 if (o->format->cleanup)
959 o->format->cleanup(o);
963 if (outfile && outfile != stdout)
966 if (opt_output_file && default_output_format && savebuf->len) {
967 if (sr_session_save(opt_output_file, sdi, savebuf->data,
968 unitsize, savebuf->len / unitsize) != SR_OK)
969 g_critical("Failed to save session.");
970 g_byte_array_free(savebuf, FALSE);
977 static int opts_to_gvar(struct srd_decoder *dec, GHashTable *hash,
978 GHashTable **options)
980 struct srd_decoder_option *o;
985 char *val_str, *conv;
988 *options = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
989 (GDestroyNotify)g_variant_unref);
991 for (optl = dec->options; optl; optl = optl->next) {
993 if (!(val_str = g_hash_table_lookup(hash, o->id)))
996 if (g_variant_is_of_type(o->def, G_VARIANT_TYPE_STRING)) {
997 gvar = g_variant_new_string(val_str);
998 } else if (g_variant_is_of_type(o->def, G_VARIANT_TYPE_INT64)) {
999 val_int = strtoll(val_str, &conv, 0);
1000 if (!conv || conv == val_str) {
1001 g_critical("Protocol decoder '%s' option '%s' "
1002 "requires a number.", dec->name, o->id);
1006 gvar = g_variant_new_int64(val_int);
1008 g_critical("Unsupported type for option '%s' (%s)",
1009 o->id, g_variant_get_type_string(o->def));
1013 g_variant_ref_sink(gvar);
1014 g_hash_table_insert(*options, g_strdup(o->id), gvar);
1015 g_hash_table_remove(hash, o->id);
1021 static int probes_to_gvar(struct srd_decoder *dec, GHashTable *hash,
1022 GHashTable **probes)
1024 struct srd_probe *p;
1025 GSList *all_probes, *l;
1029 char *val_str, *conv;
1032 *probes = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
1033 (GDestroyNotify)g_variant_unref);
1035 all_probes = g_slist_copy(dec->probes);
1036 all_probes = g_slist_concat(all_probes, g_slist_copy(dec->opt_probes));
1037 for (l = all_probes; l; l = l->next) {
1039 if (!(val_str = g_hash_table_lookup(hash, p->id)))
1040 /* Not specified. */
1042 val_int = strtoll(val_str, &conv, 10);
1043 if (!conv || conv == val_str) {
1044 g_critical("Protocol decoder '%s' probes '%s' "
1045 "is not a number.", dec->name, p->id);
1049 gvar = g_variant_new_int32(val_int);
1050 g_variant_ref_sink(gvar);
1051 g_hash_table_insert(*probes, g_strdup(p->id), gvar);
1052 g_hash_table_remove(hash, p->id);
1054 g_slist_free(all_probes);
1059 /* Register the given PDs for this session.
1060 * Accepts a string of the form: "spi:sck=3:sdata=4,spi:sck=3:sdata=5"
1061 * That will instantiate two SPI decoders on the clock but different data
1064 static int register_pds(struct sr_dev *dev, const char *pdstring)
1066 struct srd_decoder *dec;
1067 GHashTable *pd_opthash, *options, *probes;
1068 GList *leftover, *l;
1069 struct srd_decoder_inst *di;
1071 char **pdtokens, **pdtok, *pd_name;
1076 pd_ann_visible = g_hash_table_new_full(g_str_hash, g_int_equal,
1079 pd_opthash = options = probes = NULL;
1080 pdtokens = g_strsplit(pdstring, ",", 0);
1081 for (pdtok = pdtokens; *pdtok; pdtok++) {
1082 if (!(pd_opthash = parse_generic_arg(*pdtok, TRUE))) {
1083 g_critical("Invalid protocol decoder option '%s'.", *pdtok);
1087 pd_name = g_strdup(g_hash_table_lookup(pd_opthash, "sigrok_key"));
1088 g_hash_table_remove(pd_opthash, "sigrok_key");
1089 if (srd_decoder_load(pd_name) != SRD_OK) {
1090 g_critical("Failed to load protocol decoder %s.", pd_name);
1094 dec = srd_decoder_get_by_id(pd_name);
1096 /* Convert decoder option and probe values to GVariant. */
1097 if (!opts_to_gvar(dec, pd_opthash, &options)) {
1101 if (!probes_to_gvar(dec, pd_opthash, &probes)) {
1105 if (g_hash_table_size(pd_opthash) > 0) {
1106 leftover = g_hash_table_get_keys(pd_opthash);
1107 for (l = leftover; l; l = l->next)
1108 g_critical("Unknown option or probe '%s'", (char *)l->data);
1109 g_list_free(leftover);
1113 if (!(di = srd_inst_new(srd_sess, pd_name, options))) {
1114 g_critical("Failed to instantiate protocol decoder %s.", pd_name);
1119 /* If no annotation list was specified, add them all in now.
1120 * This will be pared down later to leave only the last PD
1123 if (!opt_pd_annotations)
1124 g_hash_table_insert(pd_ann_visible,
1125 g_strdup(di->inst_id), NULL);
1127 /* Remap the probes if needed. */
1128 if (srd_inst_probe_set_all(di, probes) != SRD_OK) {
1134 g_strfreev(pdtokens);
1136 g_hash_table_destroy(pd_opthash);
1138 g_hash_table_destroy(options);
1140 g_hash_table_destroy(probes);
1147 int setup_pd_stack(void)
1149 struct srd_decoder_inst *di_from, *di_to;
1153 /* Set up the protocol decoder stack. */
1154 pds = g_strsplit(opt_pds, ",", 0);
1155 if (g_strv_length(pds) > 1) {
1157 /* A stack setup was specified, use that. */
1159 pds = g_strsplit(opt_pd_stack, ",", 0);
1160 if (g_strv_length(pds) < 2) {
1162 g_critical("Specify at least two protocol decoders to stack.");
1167 /* First PD goes at the bottom of the stack. */
1168 ids = g_strsplit(pds[0], ":", 0);
1169 if (!(di_from = srd_inst_find_by_id(srd_sess, ids[0]))) {
1171 g_critical("Cannot stack protocol decoder '%s': "
1172 "instance not found.", pds[0]);
1177 /* Every subsequent PD goes on top. */
1178 for (i = 1; pds[i]; i++) {
1179 ids = g_strsplit(pds[i], ":", 0);
1180 if (!(di_to = srd_inst_find_by_id(srd_sess, ids[0]))) {
1182 g_critical("Cannot stack protocol decoder '%s': "
1183 "instance not found.", pds[i]);
1187 if ((ret = srd_inst_stack(srd_sess, di_from, di_to)) != SRD_OK)
1190 /* Don't show annotation from this PD. Only the last PD in
1191 * the stack will be left on the annotation list (unless
1192 * the annotation list was specifically provided).
1194 if (!opt_pd_annotations)
1195 g_hash_table_remove(pd_ann_visible,
1206 int setup_pd_annotations(void)
1209 struct srd_decoder *dec;
1211 char **pds, **pdtok, **keyval, **ann_descr;
1213 /* Set up custom list of PDs and annotations to show. */
1214 if (opt_pd_annotations) {
1215 pds = g_strsplit(opt_pd_annotations, ",", 0);
1216 for (pdtok = pds; *pdtok && **pdtok; pdtok++) {
1218 keyval = g_strsplit(*pdtok, "=", 0);
1219 if (!(dec = srd_decoder_get_by_id(keyval[0]))) {
1220 g_critical("Protocol decoder '%s' not found.", keyval[0]);
1223 if (!dec->annotations) {
1224 g_critical("Protocol decoder '%s' has no annotations.", keyval[0]);
1227 if (g_strv_length(keyval) == 2) {
1228 for (l = dec->annotations; l; l = l->next, ann++) {
1229 ann_descr = l->data;
1230 if (!canon_cmp(ann_descr[0], keyval[1]))
1235 g_critical("Annotation '%s' not found "
1236 "for protocol decoder '%s'.", keyval[1], keyval[0]);
1240 g_debug("cli: showing protocol decoder annotation %d from '%s'", ann, keyval[0]);
1241 g_hash_table_insert(pd_ann_visible, g_strdup(keyval[0]), GINT_TO_POINTER(ann));
1250 void show_pd_annotations(struct srd_proto_data *pdata, void *cb_data)
1254 gpointer ann_format;
1256 /* 'cb_data' is not used in this specific callback. */
1259 if (!pd_ann_visible)
1262 if (!g_hash_table_lookup_extended(pd_ann_visible, pdata->pdo->di->inst_id,
1264 /* Not in the list of PDs whose annotations we're showing. */
1267 if (pdata->ann_format != GPOINTER_TO_INT(ann_format))
1268 /* We don't want this particular format from the PD. */
1271 annotations = pdata->data;
1272 if (opt_loglevel > SR_LOG_WARN)
1273 printf("%"PRIu64"-%"PRIu64" ", pdata->start_sample, pdata->end_sample);
1274 printf("%s: ", pdata->pdo->proto_id);
1275 for (i = 0; annotations[i]; i++)
1276 printf("\"%s\" ", annotations[i]);
1282 int setup_output_format(void)
1284 GHashTable *fmtargs;
1285 GHashTableIter iter;
1286 gpointer key, value;
1287 struct sr_output_format **outputs;
1291 if (opt_output_format && !strcmp(opt_output_format, "sigrok")) {
1292 /* Doesn't really exist as an output module - this is
1293 * the session save mode. */
1294 g_free(opt_output_format);
1295 opt_output_format = NULL;
1298 if (!opt_output_format) {
1299 opt_output_format = DEFAULT_OUTPUT_FORMAT;
1300 /* we'll need to remember this so when saving to a file
1301 * later, sigrok session format will be used.
1303 default_output_format = TRUE;
1306 fmtargs = parse_generic_arg(opt_output_format, TRUE);
1307 fmtspec = g_hash_table_lookup(fmtargs, "sigrok_key");
1309 g_critical("Invalid output format.");
1312 outputs = sr_output_list();
1313 for (i = 0; outputs[i]; i++) {
1314 if (strcmp(outputs[i]->id, fmtspec))
1316 g_hash_table_remove(fmtargs, "sigrok_key");
1317 output_format = outputs[i];
1318 g_hash_table_iter_init(&iter, fmtargs);
1319 while (g_hash_table_iter_next(&iter, &key, &value)) {
1320 /* only supporting one parameter per output module
1321 * for now, and only its value */
1322 output_format_param = g_strdup(value);
1327 if (!output_format) {
1328 g_critical("Invalid output format %s.", opt_output_format);
1331 g_hash_table_destroy(fmtargs);
1336 static int select_probes(struct sr_dev_inst *sdi)
1338 struct sr_probe *probe;
1339 GSList *selected_probes, *l;
1344 if (!(selected_probes = parse_probestring(sdi, opt_probes)))
1347 for (l = sdi->probes; l; l = l->next) {
1349 if (g_slist_find(selected_probes, probe))
1350 probe->enabled = TRUE;
1352 probe->enabled = FALSE;
1354 g_slist_free(selected_probes);
1360 * Return the input file format which the CLI tool should use.
1362 * If the user specified -I / --input-format, use that one. Otherwise, try to
1363 * autodetect the format as good as possible. Failing that, return NULL.
1365 * @param filename The filename of the input file. Must not be NULL.
1366 * @param opt The -I / --input-file option the user specified (or NULL).
1368 * @return A pointer to the 'struct sr_input_format' that should be used,
1369 * or NULL if no input format was selected or auto-detected.
1371 static struct sr_input_format *determine_input_file_format(
1372 const char *filename, const char *opt)
1375 struct sr_input_format **inputs;
1377 /* If there are no input formats, return NULL right away. */
1378 inputs = sr_input_list();
1380 g_critical("No supported input formats available.");
1384 /* If the user specified -I / --input-format, use that one. */
1386 for (i = 0; inputs[i]; i++) {
1387 if (strcasecmp(inputs[i]->id, opt))
1389 g_debug("Using user-specified input file format '%s'.",
1394 /* The user specified an unknown input format, return NULL. */
1395 g_critical("Error: specified input file format '%s' is "
1400 /* Otherwise, try to find an input module that can handle this file. */
1401 for (i = 0; inputs[i]; i++) {
1402 if (inputs[i]->format_match(filename))
1406 /* Return NULL if no input module wanted to touch this. */
1408 g_critical("Error: no matching input module found.");
1412 g_debug("cli: Autodetected '%s' input format for file '%s'.",
1413 inputs[i]->id, filename);
1418 static void load_input_file_format(void)
1420 GHashTable *fmtargs = NULL;
1422 struct sr_input *in;
1423 struct sr_input_format *input_format;
1424 char *fmtspec = NULL;
1426 if (opt_input_format) {
1427 fmtargs = parse_generic_arg(opt_input_format, TRUE);
1428 fmtspec = g_hash_table_lookup(fmtargs, "sigrok_key");
1431 if (!(input_format = determine_input_file_format(opt_input_file,
1433 /* The exact cause was already logged. */
1438 g_hash_table_remove(fmtargs, "sigrok_key");
1440 if (stat(opt_input_file, &st) == -1) {
1441 g_critical("Failed to load %s: %s", opt_input_file,
1446 /* Initialize the input module. */
1447 if (!(in = g_try_malloc(sizeof(struct sr_input)))) {
1448 g_critical("Failed to allocate input module.");
1451 in->format = input_format;
1452 in->param = fmtargs;
1453 if (in->format->init) {
1454 if (in->format->init(in, opt_input_file) != SR_OK) {
1455 g_critical("Input format init failed.");
1460 if (select_probes(in->sdi) > 0)
1464 sr_session_datafeed_callback_add(datafeed_in, NULL);
1465 if (sr_session_dev_add(in->sdi) != SR_OK) {
1466 g_critical("Failed to use device.");
1467 sr_session_destroy();
1471 input_format->loadfile(in, opt_input_file);
1473 sr_session_destroy();
1476 g_hash_table_destroy(fmtargs);
1479 static void load_input_file(void)
1482 if (sr_session_load(opt_input_file) == SR_OK) {
1483 /* sigrok session file */
1484 sr_session_datafeed_callback_add(datafeed_in, NULL);
1490 /* fall back on input modules */
1491 load_input_file_format();
1495 static int set_dev_options(struct sr_dev_inst *sdi, GHashTable *args)
1497 const struct sr_config_info *srci;
1498 GHashTableIter iter;
1499 gpointer key, value;
1502 uint64_t tmp_u64, p, q, low, high;
1504 GVariant *val, *rational[2], *range[2];
1506 g_hash_table_iter_init(&iter, args);
1507 while (g_hash_table_iter_next(&iter, &key, &value)) {
1508 if (!(srci = sr_config_info_name_get(key))) {
1509 g_critical("Unknown device option '%s'.", (char *) key);
1513 if ((value == NULL) &&
1514 (srci->datatype != SR_T_BOOL)) {
1515 g_critical("Option '%s' needs a value.", (char *)key);
1519 switch (srci->datatype) {
1521 ret = sr_parse_sizestring(value, &tmp_u64);
1524 val = g_variant_new_uint64(tmp_u64);
1527 val = g_variant_new_string(value);
1533 tmp_bool = sr_parse_boolstring(value);
1534 val = g_variant_new_boolean(tmp_bool);
1537 tmp_double = strtof(value, NULL);
1538 val = g_variant_new_double(tmp_double);
1540 case SR_T_RATIONAL_PERIOD:
1541 if ((ret = sr_parse_period(value, &p, &q)) != SR_OK)
1543 rational[0] = g_variant_new_uint64(p);
1544 rational[1] = g_variant_new_uint64(q);
1545 val = g_variant_new_tuple(rational, 2);
1547 case SR_T_RATIONAL_VOLT:
1548 if ((ret = sr_parse_voltage(value, &p, &q)) != SR_OK)
1550 rational[0] = g_variant_new_uint64(p);
1551 rational[1] = g_variant_new_uint64(q);
1552 val = g_variant_new_tuple(rational, 2);
1554 case SR_T_UINT64_RANGE:
1555 if (sscanf(value, "%"PRIu64"-%"PRIu64, &low, &high) != 2) {
1559 range[0] = g_variant_new_uint64(low);
1560 range[1] = g_variant_new_uint64(high);
1561 val = g_variant_new_tuple(range, 2);
1568 ret = sr_config_set(sdi, srci->key, val);
1570 g_critical("Failed to set device option '%s'.", (char *)key);
1578 static void set_options(void)
1580 struct sr_dev_inst *sdi;
1582 GHashTable *devargs;
1585 g_critical("No setting specified.");
1589 if (!(devargs = parse_generic_arg(opt_config, FALSE)))
1592 if (!(devices = device_scan())) {
1593 g_critical("No devices found.");
1596 sdi = devices->data;
1598 if (sr_dev_open(sdi) != SR_OK) {
1599 g_critical("Failed to open device.");
1603 set_dev_options(sdi, devargs);
1606 g_slist_free(devices);
1607 g_hash_table_destroy(devargs);
1611 static int set_limit_time(const struct sr_dev_inst *sdi)
1615 uint64_t samplerate;
1617 if (!(time_msec = sr_parse_timestring(opt_time))) {
1618 g_critical("Invalid time '%s'", opt_time);
1622 if (sr_dev_has_option(sdi, SR_CONF_LIMIT_MSEC)) {
1623 gvar = g_variant_new_uint64(time_msec);
1624 if (sr_config_set(sdi, SR_CONF_LIMIT_MSEC, gvar) != SR_OK) {
1625 g_critical("Failed to configure time limit.");
1628 } else if (sr_dev_has_option(sdi, SR_CONF_SAMPLERATE)) {
1629 /* Convert to samples based on the samplerate. */
1630 sr_config_get(sdi->driver, SR_CONF_SAMPLERATE, &gvar, sdi);
1631 samplerate = g_variant_get_uint64(gvar);
1632 g_variant_unref(gvar);
1633 limit_samples = (samplerate) * time_msec / (uint64_t)1000;
1634 if (limit_samples == 0) {
1635 g_critical("Not enough time at this samplerate.");
1638 gvar = g_variant_new_uint64(limit_samples);
1639 if (sr_config_set(sdi, SR_CONF_LIMIT_SAMPLES, gvar) != SR_OK) {
1640 g_critical("Failed to configure time-based sample limit.");
1644 g_critical("This device does not support time limits.");
1651 static void run_session(void)
1654 GHashTable *devargs;
1656 struct sr_dev_inst *sdi;
1660 devices = device_scan();
1662 g_critical("No devices found.");
1665 if (g_slist_length(devices) > 1) {
1666 g_critical("sigrok-cli only supports one device for capturing.");
1669 sdi = devices->data;
1672 sr_session_datafeed_callback_add(datafeed_in, NULL);
1674 if (sr_dev_open(sdi) != SR_OK) {
1675 g_critical("Failed to open device.");
1679 if (sr_session_dev_add(sdi) != SR_OK) {
1680 g_critical("Failed to add device to session.");
1681 sr_session_destroy();
1686 if ((devargs = parse_generic_arg(opt_config, FALSE))) {
1687 if (set_dev_options(sdi, devargs) != SR_OK)
1689 g_hash_table_destroy(devargs);
1693 if (select_probes(sdi) != SR_OK) {
1694 g_critical("Failed to set probes.");
1695 sr_session_destroy();
1700 if (!(triggerlist = sr_parse_triggerstring(sdi, opt_triggers))) {
1701 sr_session_destroy();
1704 max_probes = g_slist_length(sdi->probes);
1705 for (i = 0; i < max_probes; i++) {
1706 if (triggerlist[i]) {
1707 sr_dev_trigger_set(sdi, i, triggerlist[i]);
1708 g_free(triggerlist[i]);
1711 g_free(triggerlist);
1714 if (opt_continuous) {
1715 if (!sr_dev_has_option(sdi, SR_CONF_CONTINUOUS)) {
1716 g_critical("This device does not support continuous sampling.");
1717 sr_session_destroy();
1723 if (set_limit_time(sdi) != SR_OK) {
1724 sr_session_destroy();
1730 if ((sr_parse_sizestring(opt_samples, &limit_samples) != SR_OK)) {
1731 g_critical("Invalid sample limit '%s'.", opt_samples);
1732 sr_session_destroy();
1735 gvar = g_variant_new_uint64(limit_samples);
1736 if (sr_config_set(sdi, SR_CONF_LIMIT_SAMPLES, gvar) != SR_OK) {
1737 g_critical("Failed to configure sample limit.");
1738 sr_session_destroy();
1744 if ((sr_parse_sizestring(opt_frames, &limit_frames) != SR_OK)) {
1745 g_critical("Invalid sample limit '%s'.", opt_samples);
1746 sr_session_destroy();
1749 gvar = g_variant_new_uint64(limit_frames);
1750 if (sr_config_set(sdi, SR_CONF_LIMIT_FRAMES, gvar) != SR_OK) {
1751 g_critical("Failed to configure frame limit.");
1752 sr_session_destroy();
1757 if (sr_session_start() != SR_OK) {
1758 g_critical("Failed to start session.");
1759 sr_session_destroy();
1771 sr_session_datafeed_callback_remove_all();
1772 sr_session_destroy();
1773 g_slist_free(devices);
1777 static void logger(const gchar *log_domain, GLogLevelFlags log_level,
1778 const gchar *message, gpointer cb_data)
1784 * All messages, warnings, errors etc. go to stderr (not stdout) in
1785 * order to not mess up the CLI tool data output, e.g. VCD output.
1787 if (log_level & (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING)
1788 || opt_loglevel > SR_LOG_WARN) {
1789 fprintf(stderr, "%s\n", message);
1794 int main(int argc, char **argv)
1796 GOptionContext *context;
1801 g_log_set_default_handler(logger, NULL);
1803 context = g_option_context_new(NULL);
1804 g_option_context_add_main_entries(context, optargs, NULL);
1808 if (!g_option_context_parse(context, &argc, &argv, &error)) {
1809 g_critical("%s", error->message);
1813 /* Set the loglevel (amount of messages to output) for libsigrok. */
1814 if (sr_log_loglevel_set(opt_loglevel) != SR_OK)
1817 if (sr_init(&sr_ctx) != SR_OK)
1821 /* Set the loglevel (amount of messages to output) for libsigrokdecode. */
1822 if (srd_log_loglevel_set(opt_loglevel) != SRD_OK)
1826 if (srd_init(NULL) != SRD_OK)
1828 if (srd_session_new(&srd_sess) != SRD_OK) {
1829 g_critical("Failed to create new decode session.");
1832 if (register_pds(NULL, opt_pds) != 0)
1834 if (srd_pd_output_callback_add(srd_sess, SRD_OUTPUT_ANN,
1835 show_pd_annotations, NULL) != SRD_OK)
1837 if (setup_pd_stack() != 0)
1839 if (setup_pd_annotations() != 0)
1844 if (setup_output_format() != 0)
1849 else if (opt_scan_devs)
1852 else if (opt_pds && opt_show)
1857 else if (opt_input_file)
1861 else if (opt_samples || opt_time || opt_frames || opt_continuous)
1864 help = g_option_context_get_help(context, TRUE, NULL);
1880 g_option_context_free(context);