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 <sigrokdecode.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 GHashTable
*pd_ann_visible
= NULL
;
51 static GByteArray
*savebuf
;
53 static gboolean opt_version
= FALSE
;
54 static gint opt_loglevel
= SR_LOG_WARN
; /* Show errors+warnings per default. */
55 static gboolean opt_scan_devs
= FALSE
;
56 static gboolean opt_wait_trigger
= FALSE
;
57 static gchar
*opt_input_file
= NULL
;
58 static gchar
*opt_output_file
= NULL
;
59 static gchar
*opt_drv
= NULL
;
60 static gchar
*opt_config
= NULL
;
61 static gchar
*opt_probes
= NULL
;
62 static gchar
*opt_triggers
= NULL
;
63 static gchar
*opt_pds
= NULL
;
65 static gchar
*opt_pd_stack
= NULL
;
66 static gchar
*opt_pd_annotations
= NULL
;
68 static gchar
*opt_input_format
= NULL
;
69 static gchar
*opt_output_format
= NULL
;
70 static gchar
*opt_show
= NULL
;
71 static gchar
*opt_time
= NULL
;
72 static gchar
*opt_samples
= NULL
;
73 static gchar
*opt_frames
= NULL
;
74 static gchar
*opt_continuous
= NULL
;
75 static gchar
*opt_set
= NULL
;
77 static GOptionEntry optargs
[] = {
78 {"version", 'V', 0, G_OPTION_ARG_NONE
, &opt_version
,
79 "Show version and support list", NULL
},
80 {"loglevel", 'l', 0, G_OPTION_ARG_INT
, &opt_loglevel
,
81 "Set loglevel (5 is most verbose)", NULL
},
82 {"driver", 'd', 0, G_OPTION_ARG_STRING
, &opt_drv
,
83 "The driver to use", NULL
},
84 {"config", 'c', 0, G_OPTION_ARG_STRING
, &opt_config
,
85 "Specify device configuration options", NULL
},
86 {"input-file", 'i', 0, G_OPTION_ARG_FILENAME
, &opt_input_file
,
87 "Load input from file", NULL
},
88 {"input-format", 'I', 0, G_OPTION_ARG_STRING
, &opt_input_format
,
89 "Input format", NULL
},
90 {"output-file", 'o', 0, G_OPTION_ARG_FILENAME
, &opt_output_file
,
91 "Save output to file", NULL
},
92 {"output-format", 'O', 0, G_OPTION_ARG_STRING
, &opt_output_format
,
93 "Output format", NULL
},
94 {"probes", 'p', 0, G_OPTION_ARG_STRING
, &opt_probes
,
95 "Probes to use", NULL
},
96 {"triggers", 't', 0, G_OPTION_ARG_STRING
, &opt_triggers
,
97 "Trigger configuration", NULL
},
98 {"wait-trigger", 'w', 0, G_OPTION_ARG_NONE
, &opt_wait_trigger
,
99 "Wait for trigger", NULL
},
101 {"protocol-decoders", 'P', 0, G_OPTION_ARG_STRING
, &opt_pds
,
102 "Protocol decoders to run", NULL
},
103 {"protocol-decoder-stack", 'S', 0, G_OPTION_ARG_STRING
, &opt_pd_stack
,
104 "Protocol decoder stack", NULL
},
105 {"protocol-decoder-annotations", 'A', 0, G_OPTION_ARG_STRING
, &opt_pd_annotations
,
106 "Protocol decoder annotation(s) to show", NULL
},
108 {"scan", 0, 0, G_OPTION_ARG_NONE
, &opt_scan_devs
,
109 "Scan for devices", NULL
},
110 {"show", 0, 0, G_OPTION_ARG_NONE
, &opt_show
,
111 "Show device detail", NULL
},
112 {"time", 0, 0, G_OPTION_ARG_STRING
, &opt_time
,
113 "How long to sample (ms)", NULL
},
114 {"samples", 0, 0, G_OPTION_ARG_STRING
, &opt_samples
,
115 "Number of samples to acquire", NULL
},
116 {"frames", 0, 0, G_OPTION_ARG_STRING
, &opt_frames
,
117 "Number of frames to acquire", NULL
},
118 {"continuous", 0, 0, G_OPTION_ARG_NONE
, &opt_continuous
,
119 "Sample continuously", NULL
},
120 {"set", 0, 0, G_OPTION_ARG_NONE
, &opt_set
, "Set device options only", NULL
},
121 {NULL
, 0, 0, 0, NULL
, NULL
, NULL
}
125 /* Convert driver options hash to GSList of struct sr_config. */
126 static GSList
*hash_to_hwopt(GHashTable
*hash
)
128 const struct sr_config_info
*srci
;
129 struct sr_config
*src
;
134 keys
= g_hash_table_get_keys(hash
);
136 for (gl
= keys
; gl
; gl
= gl
->next
) {
138 if (!(srci
= sr_config_info_name_get(key
))) {
139 g_critical("Unknown option %s", key
);
142 src
= g_try_malloc(sizeof(struct sr_config
));
143 src
->key
= srci
->key
;
144 value
= g_hash_table_lookup(hash
, key
);
145 src
->data
= g_variant_new_string(value
);
146 opts
= g_slist_append(opts
, src
);
153 static void free_drvopts(struct sr_config
*src
)
155 g_variant_unref(src
->data
);
159 static GSList
*device_scan(void)
161 struct sr_dev_driver
**drivers
, *driver
;
163 GSList
*drvopts
, *devices
, *tmpdevs
, *l
;
168 drvargs
= parse_generic_arg(opt_drv
, TRUE
);
169 drvname
= g_strdup(g_hash_table_lookup(drvargs
, "sigrok_key"));
170 g_hash_table_remove(drvargs
, "sigrok_key");
172 drivers
= sr_driver_list();
173 for (i
= 0; drivers
[i
]; i
++) {
174 if (strcmp(drivers
[i
]->name
, drvname
))
179 g_critical("Driver %s not found.", drvname
);
183 if (sr_driver_init(sr_ctx
, driver
) != SR_OK
) {
184 g_critical("Failed to initialize driver.");
188 if (g_hash_table_size(drvargs
) > 0)
189 if (!(drvopts
= hash_to_hwopt(drvargs
)))
190 /* Unknown options, already logged. */
192 devices
= sr_driver_scan(driver
, drvopts
);
193 g_slist_free_full(drvopts
, (GDestroyNotify
)free_drvopts
);
195 /* No driver specified, let them all scan on their own. */
197 drivers
= sr_driver_list();
198 for (i
= 0; drivers
[i
]; i
++) {
200 if (sr_driver_init(sr_ctx
, driver
) != SR_OK
) {
201 g_critical("Failed to initialize driver.");
204 tmpdevs
= sr_driver_scan(driver
, NULL
);
205 for (l
= tmpdevs
; l
; l
= l
->next
)
206 devices
= g_slist_append(devices
, l
->data
);
207 g_slist_free(tmpdevs
);
214 static void show_version(void)
216 struct sr_dev_driver
**drivers
;
217 struct sr_input_format
**inputs
;
218 struct sr_output_format
**outputs
;
221 struct srd_decoder
*dec
;
225 printf("sigrok-cli %s\n\n", VERSION
);
227 printf("Using libsigrok %s (lib version %s).\n",
228 sr_package_version_string_get(), sr_lib_version_string_get());
230 printf("Using libsigrokdecode %s (lib version %s).\n\n",
231 srd_package_version_string_get(), srd_lib_version_string_get());
234 printf("Supported hardware drivers:\n");
235 drivers
= sr_driver_list();
236 for (i
= 0; drivers
[i
]; i
++) {
237 printf(" %-20s %s\n", drivers
[i
]->name
, drivers
[i
]->longname
);
241 printf("Supported input formats:\n");
242 inputs
= sr_input_list();
243 for (i
= 0; inputs
[i
]; i
++)
244 printf(" %-20s %s\n", inputs
[i
]->id
, inputs
[i
]->description
);
247 printf("Supported output formats:\n");
248 outputs
= sr_output_list();
249 for (i
= 0; outputs
[i
]; i
++)
250 printf(" %-20s %s\n", outputs
[i
]->id
, outputs
[i
]->description
);
254 if (srd_init(NULL
) == SRD_OK
) {
255 printf("Supported protocol decoders:\n");
256 srd_decoder_load_all();
257 for (l
= srd_decoder_list(); l
; l
= l
->next
) {
259 printf(" %-20s %s\n", dec
->id
, dec
->longname
);
260 /* Print protocol description upon "-l 3" or higher. */
261 if (opt_loglevel
>= SR_LOG_INFO
)
262 printf(" %-20s %s\n", "", dec
->desc
);
270 static void print_dev_line(const struct sr_dev_inst
*sdi
)
272 struct sr_probe
*probe
;
277 s
= g_string_sized_new(128);
278 g_string_assign(s
, sdi
->driver
->name
);
279 if (sr_config_get(sdi
->driver
, SR_CONF_CONN
, &gvar
, sdi
) == SR_OK
) {
280 g_string_append(s
, ":conn=");
281 g_string_append(s
, g_variant_get_string(gvar
, NULL
));
282 g_variant_unref(gvar
);
284 g_string_append(s
, " - ");
285 if (sdi
->vendor
&& sdi
->vendor
[0])
286 g_string_append_printf(s
, "%s ", sdi
->vendor
);
287 if (sdi
->model
&& sdi
->model
[0])
288 g_string_append_printf(s
, "%s ", sdi
->model
);
289 if (sdi
->version
&& sdi
->version
[0])
290 g_string_append_printf(s
, "%s ", sdi
->version
);
292 if (g_slist_length(sdi
->probes
) == 1) {
293 probe
= sdi
->probes
->data
;
294 g_string_append_printf(s
, "with 1 probe: %s", probe
->name
);
296 g_string_append_printf(s
, "with %d probes:", g_slist_length(sdi
->probes
));
297 for (l
= sdi
->probes
; l
; l
= l
->next
) {
299 g_string_append_printf(s
, " %s", probe
->name
);
303 g_string_append_printf(s
, "\n");
304 printf("%s", s
->str
);
305 g_string_free(s
, TRUE
);
309 static void show_dev_list(void)
311 struct sr_dev_inst
*sdi
;
314 if (!(devices
= device_scan()))
317 printf("The following devices were found:\n");
318 for (l
= devices
; l
; l
= l
->next
) {
322 g_slist_free(devices
);
326 static void show_dev_detail(void)
328 struct sr_dev_inst
*sdi
;
329 const struct sr_config_info
*srci
;
331 GVariant
*gvar_opts
, *gvar_dict
, *gvar_list
, *gvar
;
332 gsize num_opts
, num_elements
;
333 const uint64_t *uint64
, p
, q
;
335 unsigned int num_devices
, tmp_bool
, o
, i
;
337 const char *charopts
, **stropts
;
339 if (!(devices
= device_scan())) {
340 g_critical("No devices found.");
344 num_devices
= g_slist_length(devices
);
345 if (num_devices
> 1) {
346 g_critical("%d devices found. Use --list-devices to show them, "
347 "and select one to show.", num_devices
);
354 if (sr_dev_open(sdi
) != SR_OK
) {
355 g_critical("Failed to open device.");
359 if ((sr_config_list(sdi
->driver
, SR_CONF_SCAN_OPTIONS
, &gvar_opts
,
361 opts
= g_variant_get_fixed_array(gvar_opts
, &num_elements
,
363 printf("Supported driver options:\n");
364 for (i
= 0; i
< num_elements
; i
++) {
365 if (!(srci
= sr_config_info_get(opts
[i
])))
367 printf(" %s\n", srci
->id
);
369 g_variant_unref(gvar_opts
);
372 if ((sr_config_list(sdi
->driver
, SR_CONF_DEVICE_OPTIONS
, &gvar_opts
,
374 /* Driver supports no device instance options. */
377 printf("Supported device options:\n");
378 opts
= g_variant_get_fixed_array(gvar_opts
, &num_opts
, sizeof(int32_t));
379 for (o
= 0; o
< num_opts
; o
++) {
380 if (!(srci
= sr_config_info_get(opts
[o
])))
383 if (srci
->key
== SR_CONF_TRIGGER_TYPE
) {
384 if (sr_config_list(sdi
->driver
, srci
->key
, &gvar
,
389 charopts
= g_variant_get_string(gvar
, NULL
);
390 printf(" Supported triggers: ");
392 printf("%c ", *charopts
);
396 g_variant_unref(gvar
);
398 } else if (srci
->key
== SR_CONF_PATTERN_MODE
) {
399 /* Pattern generator modes */
400 printf(" %s", srci
->id
);
401 if (sr_config_list(sdi
->driver
, srci
->key
, &gvar
,
403 printf(" - supported patterns:\n");
404 stropts
= g_variant_get_strv(gvar
, &num_elements
);
405 for (i
= 0; i
< num_elements
; i
++)
406 printf(" %s\n", stropts
[i
]);
407 g_variant_unref(gvar
);
412 } else if (srci
->key
== SR_CONF_SAMPLERATE
) {
413 /* Supported samplerates */
414 printf(" %s", srci
->id
);
415 if (sr_config_list(sdi
->driver
, SR_CONF_SAMPLERATE
,
416 &gvar_dict
, sdi
) != SR_OK
) {
420 if ((gvar_list
= g_variant_lookup_value(gvar_dict
,
421 "samplerates", G_VARIANT_TYPE("at")))) {
422 uint64
= g_variant_get_fixed_array(gvar_list
,
423 &num_elements
, sizeof(uint64_t));
424 printf(" - supported samplerates:\n");
425 for (i
= 0; i
< num_elements
; i
++)
426 printf(" %s\n", sr_samplerate_string(uint64
[i
]));
427 } if ((gvar_list
= g_variant_lookup_value(gvar_dict
,
428 "samplerate-steps", G_VARIANT_TYPE("at")))) {
429 uint64
= g_variant_get_fixed_array(gvar_list
,
430 &num_elements
, sizeof(uint64_t));
432 if (!(s
= sr_samplerate_string(uint64
[0])))
437 if (!(s
= sr_samplerate_string(uint64
[1])))
442 if (!(s
= sr_samplerate_string(uint64
[2])))
444 printf(" in steps of %s)\n", s
);
446 g_variant_unref(gvar_list
);
448 g_variant_unref(gvar_dict
);
450 } else if (srci
->key
== SR_CONF_BUFFERSIZE
) {
451 /* Supported buffer sizes */
452 printf(" %s", srci
->id
);
453 if (sr_config_list(sdi
->driver
, SR_CONF_BUFFERSIZE
,
454 &gvar_list
, sdi
) != SR_OK
) {
458 uint64
= g_variant_get_fixed_array(gvar_list
,
459 &num_elements
, sizeof(uint64_t));
460 printf(" - supported buffer sizes:\n");
461 for (i
= 0; i
< num_elements
; i
++)
462 printf(" %"PRIu64
"\n", uint64
[i
]);
463 g_variant_unref(gvar_list
);
465 } else if (srci
->key
== SR_CONF_TIMEBASE
) {
466 /* Supported time bases */
467 printf(" %s", srci
->id
);
468 if (sr_config_list(sdi
->driver
, SR_CONF_TIMEBASE
,
469 &gvar_list
, sdi
) != SR_OK
) {
473 printf(" - supported time bases:\n");
474 num_elements
= g_variant_n_children(gvar_list
);
475 for (i
= 0; i
< num_elements
; i
++) {
476 gvar
= g_variant_get_child_value(gvar_list
, i
);
477 g_variant_get(gvar
, "(tt)", &p
, &q
);
478 s
= sr_period_string(p
* q
);
482 g_variant_unref(gvar_list
);
484 } else if (srci
->key
== SR_CONF_TRIGGER_SOURCE
) {
485 /* Supported trigger sources */
486 printf(" %s", srci
->id
);
487 if (sr_config_list(sdi
->driver
, SR_CONF_TRIGGER_SOURCE
,
488 &gvar
, sdi
) != SR_OK
) {
492 printf(" - supported trigger sources:\n");
493 stropts
= g_variant_get_strv(gvar
, &num_elements
);
494 for (i
= 0; i
< num_elements
; i
++)
495 printf(" %s\n", stropts
[i
]);
496 g_variant_unref(gvar
);
498 } else if (srci
->key
== SR_CONF_FILTER
) {
499 /* Supported filters */
500 printf(" %s", srci
->id
);
501 if (sr_config_list(sdi
->driver
, SR_CONF_FILTER
,
502 &gvar
, sdi
) != SR_OK
) {
506 printf(" - supported filter targets:\n");
507 stropts
= g_variant_get_strv(gvar
, &num_elements
);
508 for (i
= 0; i
< num_elements
; i
++)
509 printf(" %s\n", stropts
[i
]);
510 g_variant_unref(gvar
);
512 } else if (srci
->key
== SR_CONF_VDIV
) {
513 /* Supported volts/div values */
514 printf(" %s", srci
->id
);
515 if (sr_config_list(sdi
->driver
, SR_CONF_VDIV
,
516 &gvar_list
, sdi
) != SR_OK
) {
520 printf(" - supported volts/div:\n");
521 num_elements
= g_variant_n_children(gvar_list
);
522 for (i
= 0; i
< num_elements
; i
++) {
523 gvar
= g_variant_get_child_value(gvar_list
, i
);
524 g_variant_get(gvar
, "(tt)", &p
, &q
);
525 s
= sr_voltage_string(p
, q
);
529 g_variant_unref(gvar_list
);
531 } else if (srci
->key
== SR_CONF_COUPLING
) {
532 /* Supported coupling settings */
533 printf(" %s", srci
->id
);
534 if (sr_config_list(sdi
->driver
, SR_CONF_COUPLING
,
535 &gvar
, sdi
) != SR_OK
) {
539 printf(" - supported coupling options:\n");
540 stropts
= g_variant_get_strv(gvar
, &num_elements
);
541 for (i
= 0; i
< num_elements
; i
++)
542 printf(" %s\n", stropts
[i
]);
543 g_variant_unref(gvar
);
545 } else if (srci
->key
== SR_CONF_DATALOG
) {
546 /* Turning on/off internal data logging. */
547 printf(" %s\t(on/off", srci
->id
);
548 if (sr_config_get(sdi
->driver
, SR_CONF_DATALOG
,
549 &gvar
, sdi
) == SR_OK
) {
550 tmp_bool
= g_variant_get_boolean(gvar
);
551 printf(", currently %s", tmp_bool
? "on" : "off");
552 g_variant_unref(gvar
);
556 /* Everything else */
557 printf(" %s\n", srci
->id
);
560 g_variant_unref(gvar_opts
);
567 static void show_pd_detail(void)
570 struct srd_decoder
*dec
;
571 struct srd_decoder_option
*o
;
572 char **pdtokens
, **pdtok
, *optsep
, **ann
, *val
, *doc
;
575 pdtokens
= g_strsplit(opt_pds
, ",", -1);
576 for (pdtok
= pdtokens
; *pdtok
; pdtok
++) {
578 if ((optsep
= strchr(*pdtok
, ':')))
580 if (!(dec
= srd_decoder_get_by_id(*pdtok
))) {
581 g_critical("Protocol decoder %s not found.", *pdtok
);
584 printf("ID: %s\nName: %s\nLong name: %s\nDescription: %s\n",
585 dec
->id
, dec
->name
, dec
->longname
, dec
->desc
);
586 printf("License: %s\n", dec
->license
);
587 printf("Annotations:\n");
588 if (dec
->annotations
) {
589 for (l
= dec
->annotations
; l
; l
= l
->next
) {
591 printf("- %s\n %s\n", ann
[0], ann
[1]);
596 printf("Required probes:\n");
598 for (l
= dec
->probes
; l
; l
= l
->next
) {
600 printf("- %s (%s): %s\n",
601 p
->name
, p
->id
, p
->desc
);
606 printf("Optional probes:\n");
607 if (dec
->opt_probes
) {
608 for (l
= dec
->opt_probes
; l
; l
= l
->next
) {
610 printf("- %s (%s): %s\n",
611 p
->name
, p
->id
, p
->desc
);
617 printf("Options:\n");
618 for (l
= dec
->options
; l
; l
= l
->next
) {
620 val
= g_variant_print(o
->def
, FALSE
);
621 printf("- %s: %s (default %s)\n", o
->id
, o
->desc
, val
);
625 if ((doc
= srd_decoder_doc_get(dec
))) {
626 printf("Documentation:\n%s\n",
627 doc
[0] == '\n' ? doc
+ 1 : doc
);
632 g_strfreev(pdtokens
);
636 static GArray
*get_enabled_logic_probes(const struct sr_dev_inst
*sdi
)
638 struct sr_probe
*probe
;
642 probes
= g_array_new(FALSE
, FALSE
, sizeof(int));
643 for (l
= sdi
->probes
; l
; l
= l
->next
) {
645 if (probe
->type
!= SR_PROBE_LOGIC
)
647 if (probe
->enabled
!= TRUE
)
649 g_array_append_val(probes
, probe
->index
);
655 static void datafeed_in(const struct sr_dev_inst
*sdi
,
656 const struct sr_datafeed_packet
*packet
, void *cb_data
)
658 const struct sr_datafeed_meta
*meta
;
659 const struct sr_datafeed_logic
*logic
;
660 const struct sr_datafeed_analog
*analog
;
661 struct sr_config
*src
;
662 static struct sr_output
*o
= NULL
;
663 static GArray
*logic_probelist
= NULL
;
664 static uint64_t received_samples
= 0;
665 static int unitsize
= 0;
666 static int triggered
= 0;
667 static FILE *outfile
= NULL
;
671 int sample_size
, ret
;
672 uint64_t samplerate
, output_len
, filter_out_len
;
673 uint8_t *output_buf
, *filter_out
;
677 /* If the first packet to come in isn't a header, don't even try. */
678 if (packet
->type
!= SR_DF_HEADER
&& o
== NULL
)
682 switch (packet
->type
) {
684 g_debug("cli: Received SR_DF_HEADER");
685 /* Initialize the output module. */
686 if (!(o
= g_try_malloc(sizeof(struct sr_output
)))) {
687 g_critical("Output module malloc failed.");
690 o
->format
= output_format
;
691 o
->sdi
= (struct sr_dev_inst
*)sdi
;
692 o
->param
= output_format_param
;
693 if (o
->format
->init
) {
694 if (o
->format
->init(o
) != SR_OK
) {
695 g_critical("Output format initialization failed.");
700 /* Prepare non-stdout output. */
702 if (opt_output_file
) {
703 if (default_output_format
) {
704 /* output file is in session format, so we'll
705 * keep a copy of everything as it comes in
706 * and save from there after the session. */
708 savebuf
= g_byte_array_new();
710 /* saving to a file in whatever format was set
711 * with --format, so all we need is a filehandle */
712 outfile
= g_fopen(opt_output_file
, "wb");
716 /* Prepare for logic data. */
717 logic_probelist
= get_enabled_logic_probes(sdi
);
718 /* How many bytes we need to store the packed samples. */
719 unitsize
= (logic_probelist
->len
+ 7) / 8;
722 if (opt_pds
&& logic_probelist
->len
) {
723 if (sr_config_get(sdi
->driver
, SR_CONF_SAMPLERATE
,
724 &gvar
, sdi
) != SR_OK
) {
725 g_critical("Unable to initialize protocol "
726 "decoders: no samplerate found.");
729 samplerate
= g_variant_get_uint64(gvar
);
730 g_variant_unref(gvar
);
731 srd_session_start(logic_probelist
->len
, unitsize
, samplerate
);
737 g_debug("cli: received SR_DF_META");
738 meta
= packet
->payload
;
739 for (l
= meta
->config
; l
; l
= l
->next
) {
742 case SR_CONF_SAMPLERATE
:
743 samplerate
= g_variant_get_uint64(src
->data
);
744 g_debug("cli: got samplerate %"PRIu64
" Hz", samplerate
);
746 case SR_CONF_SAMPLE_INTERVAL
:
747 samplerate
= g_variant_get_uint64(src
->data
);
748 g_debug("cli: got sample interval %"PRIu64
" ms", samplerate
);
751 /* Unknown metadata is not an error. */
758 g_debug("cli: received SR_DF_TRIGGER");
759 if (o
->format
->event
)
760 o
->format
->event(o
, SR_DF_TRIGGER
, &output_buf
,
766 logic
= packet
->payload
;
767 g_message("cli: received SR_DF_LOGIC, %"PRIu64
" bytes", logic
->length
);
768 sample_size
= logic
->unitsize
;
769 if (logic
->length
== 0)
772 /* Don't store any samples until triggered. */
773 if (opt_wait_trigger
&& !triggered
)
776 if (limit_samples
&& received_samples
>= limit_samples
)
779 ret
= sr_filter_probes(sample_size
, unitsize
, logic_probelist
,
780 logic
->data
, logic
->length
,
781 &filter_out
, &filter_out_len
);
785 /* What comes out of the filter is guaranteed to be packed into the
786 * minimum size needed to support the number of samples at this sample
787 * size. however, the driver may have submitted too much -- cut off
788 * the buffer of the last packet according to the sample limit.
790 if (limit_samples
&& (received_samples
+ logic
->length
/ sample_size
>
791 limit_samples
* sample_size
))
792 filter_out_len
= limit_samples
* sample_size
- received_samples
;
794 if (opt_output_file
&& default_output_format
) {
795 /* Saving to a session file. */
796 g_byte_array_append(savebuf
, filter_out
, filter_out_len
);
800 if (srd_session_send(received_samples
, (uint8_t*)filter_out
,
801 filter_out_len
) != SRD_OK
)
806 if (o
->format
->data
&& packet
->type
== o
->format
->df_type
)
807 o
->format
->data(o
, filter_out
, filter_out_len
,
808 &output_buf
, &output_len
);
810 fwrite(output_buf
, 1, output_len
, outfile
);
818 received_samples
+= logic
->length
/ sample_size
;
822 analog
= packet
->payload
;
823 g_message("cli: received SR_DF_ANALOG, %d samples", analog
->num_samples
);
824 if (analog
->num_samples
== 0)
827 if (limit_samples
&& received_samples
>= limit_samples
)
830 if (o
->format
->data
&& packet
->type
== o
->format
->df_type
) {
831 o
->format
->data(o
, (const uint8_t *)analog
->data
,
832 analog
->num_samples
* sizeof(float),
833 &output_buf
, &output_len
);
835 fwrite(output_buf
, 1, output_len
, outfile
);
841 received_samples
+= analog
->num_samples
;
844 case SR_DF_FRAME_BEGIN
:
845 g_debug("cli: received SR_DF_FRAME_BEGIN");
846 if (o
->format
->event
) {
847 o
->format
->event(o
, SR_DF_FRAME_BEGIN
, &output_buf
,
850 fwrite(output_buf
, 1, output_len
, outfile
);
857 case SR_DF_FRAME_END
:
858 g_debug("cli: received SR_DF_FRAME_END");
859 if (o
->format
->event
) {
860 o
->format
->event(o
, SR_DF_FRAME_END
, &output_buf
,
863 fwrite(output_buf
, 1, output_len
, outfile
);
874 if (o
&& o
->format
->receive
) {
875 if (o
->format
->receive(o
, sdi
, packet
, &out
) == SR_OK
&& out
) {
876 fwrite(out
->str
, 1, out
->len
, outfile
);
878 g_string_free(out
, TRUE
);
882 /* SR_DF_END needs to be handled after the output module's receive()
883 * is called, so it can properly clean up that module etc. */
884 if (packet
->type
== SR_DF_END
) {
885 g_debug("cli: Received SR_DF_END");
887 if (o
->format
->event
) {
888 o
->format
->event(o
, SR_DF_END
, &output_buf
, &output_len
);
891 fwrite(output_buf
, 1, output_len
, outfile
);
897 if (limit_samples
&& received_samples
< limit_samples
)
898 g_warning("Device only sent %" PRIu64
" samples.",
902 g_warning("Device stopped after %" PRIu64
" samples.",
905 g_array_free(logic_probelist
, TRUE
);
907 if (o
->format
->cleanup
)
908 o
->format
->cleanup(o
);
912 if (outfile
&& outfile
!= stdout
)
915 if (opt_output_file
&& default_output_format
) {
916 if (sr_session_save(opt_output_file
, sdi
, savebuf
->data
,
917 unitsize
, savebuf
->len
/ unitsize
) != SR_OK
)
918 g_critical("Failed to save session.");
919 g_byte_array_free(savebuf
, FALSE
);
926 static int opts_to_gvar(struct srd_decoder
*dec
, GHashTable
*hash
,
927 GHashTable
**options
)
929 struct srd_decoder_option
*o
;
934 char *val_str
, *conv
;
937 *options
= g_hash_table_new_full(g_str_hash
, g_str_equal
, g_free
,
938 (GDestroyNotify
)g_variant_unref
);
940 for (optl
= dec
->options
; optl
; optl
= optl
->next
) {
942 if (!(val_str
= g_hash_table_lookup(hash
, o
->id
)))
945 if (g_variant_is_of_type(o
->def
, G_VARIANT_TYPE_STRING
)) {
946 gvar
= g_variant_new_string(val_str
);
947 } else if (g_variant_is_of_type(o
->def
, G_VARIANT_TYPE_INT64
)) {
948 val_int
= strtoll(val_str
, &conv
, 10);
949 if (!conv
|| conv
== val_str
) {
950 g_critical("Protocol decoder '%s' option '%s' "
951 "requires a number.", dec
->name
, o
->id
);
955 gvar
= g_variant_new_int64(val_int
);
957 g_critical("Unsupported type for option '%s' (%s)",
958 o
->id
, g_variant_get_type_string(o
->def
));
962 g_variant_ref_sink(gvar
);
963 g_hash_table_insert(*options
, g_strdup(o
->id
), gvar
);
964 g_hash_table_remove(hash
, o
->id
);
970 static int probes_to_gvar(struct srd_decoder
*dec
, GHashTable
*hash
,
974 GSList
*all_probes
, *l
;
978 char *val_str
, *conv
;
981 *probes
= g_hash_table_new_full(g_str_hash
, g_str_equal
, g_free
,
982 (GDestroyNotify
)g_variant_unref
);
984 all_probes
= g_slist_copy(dec
->probes
);
985 all_probes
= g_slist_concat(all_probes
, dec
->opt_probes
);
986 for (l
= all_probes
; l
; l
= l
->next
) {
988 if (!(val_str
= g_hash_table_lookup(hash
, p
->id
)))
991 val_int
= strtoll(val_str
, &conv
, 10);
992 if (!conv
|| conv
== val_str
) {
993 g_critical("Protocol decoder '%s' probes '%s' "
994 "is not a number.", dec
->name
, p
->id
);
998 gvar
= g_variant_new_int32(val_int
);
999 g_variant_ref_sink(gvar
);
1000 g_hash_table_insert(*probes
, g_strdup(p
->id
), gvar
);
1001 g_hash_table_remove(hash
, p
->id
);
1003 g_slist_free(all_probes
);
1008 /* Register the given PDs for this session.
1009 * Accepts a string of the form: "spi:sck=3:sdata=4,spi:sck=3:sdata=5"
1010 * That will instantiate two SPI decoders on the clock but different data
1013 static int register_pds(struct sr_dev
*dev
, const char *pdstring
)
1015 struct srd_decoder
*dec
;
1016 GHashTable
*pd_opthash
, *options
, *probes
;
1017 GList
*leftover
, *l
;
1018 struct srd_decoder_inst
*di
;
1020 char **pdtokens
, **pdtok
, *pd_name
;
1025 pd_ann_visible
= g_hash_table_new_full(g_str_hash
, g_int_equal
,
1028 pd_opthash
= options
= probes
= NULL
;
1029 pdtokens
= g_strsplit(pdstring
, ",", 0);
1030 for (pdtok
= pdtokens
; *pdtok
; pdtok
++) {
1031 if (!(pd_opthash
= parse_generic_arg(*pdtok
, TRUE
))) {
1032 g_critical("Invalid protocol decoder option '%s'.", *pdtok
);
1036 pd_name
= g_strdup(g_hash_table_lookup(pd_opthash
, "sigrok_key"));
1037 g_hash_table_remove(pd_opthash
, "sigrok_key");
1038 if (srd_decoder_load(pd_name
) != SRD_OK
) {
1039 g_critical("Failed to load protocol decoder %s.", pd_name
);
1043 dec
= srd_decoder_get_by_id(pd_name
);
1045 /* Convert decoder option and probe values to GVariant. */
1046 if (!opts_to_gvar(dec
, pd_opthash
, &options
)) {
1050 if (!probes_to_gvar(dec
, pd_opthash
, &probes
)) {
1054 if (g_hash_table_size(pd_opthash
) > 0) {
1055 leftover
= g_hash_table_get_keys(pd_opthash
);
1056 for (l
= leftover
; l
; l
= l
->next
)
1057 g_critical("Unknown option or probe '%s'", (char *)l
->data
);
1058 g_list_free(leftover
);
1062 if (!(di
= srd_inst_new(pd_name
, options
))) {
1063 g_critical("Failed to instantiate protocol decoder %s.", pd_name
);
1068 /* If no annotation list was specified, add them all in now.
1069 * This will be pared down later to leave only the last PD
1072 if (!opt_pd_annotations
)
1073 g_hash_table_insert(pd_ann_visible
,
1074 g_strdup(di
->inst_id
), NULL
);
1076 /* Remap the probes if needed. */
1077 if (srd_inst_probe_set_all(di
, probes
) != SRD_OK
) {
1083 g_strfreev(pdtokens
);
1085 g_hash_table_destroy(pd_opthash
);
1087 g_hash_table_destroy(options
);
1089 g_hash_table_destroy(probes
);
1096 int setup_pd_stack(void)
1098 struct srd_decoder_inst
*di_from
, *di_to
;
1102 /* Set up the protocol decoder stack. */
1103 pds
= g_strsplit(opt_pds
, ",", 0);
1104 if (g_strv_length(pds
) > 1) {
1106 /* A stack setup was specified, use that. */
1108 pds
= g_strsplit(opt_pd_stack
, ",", 0);
1109 if (g_strv_length(pds
) < 2) {
1111 g_critical("Specify at least two protocol decoders to stack.");
1116 /* First PD goes at the bottom of the stack. */
1117 ids
= g_strsplit(pds
[0], ":", 0);
1118 if (!(di_from
= srd_inst_find_by_id(ids
[0]))) {
1120 g_critical("Cannot stack protocol decoder '%s': "
1121 "instance not found.", pds
[0]);
1126 /* Every subsequent PD goes on top. */
1127 for (i
= 1; pds
[i
]; i
++) {
1128 ids
= g_strsplit(pds
[i
], ":", 0);
1129 if (!(di_to
= srd_inst_find_by_id(ids
[0]))) {
1131 g_critical("Cannot stack protocol decoder '%s': "
1132 "instance not found.", pds
[i
]);
1136 if ((ret
= srd_inst_stack(di_from
, di_to
)) != SRD_OK
)
1139 /* Don't show annotation from this PD. Only the last PD in
1140 * the stack will be left on the annotation list (unless
1141 * the annotation list was specifically provided).
1143 if (!opt_pd_annotations
)
1144 g_hash_table_remove(pd_ann_visible
,
1155 int setup_pd_annotations(void)
1158 struct srd_decoder
*dec
;
1160 char **pds
, **pdtok
, **keyval
, **ann_descr
;
1162 /* Set up custom list of PDs and annotations to show. */
1163 if (opt_pd_annotations
) {
1164 pds
= g_strsplit(opt_pd_annotations
, ",", 0);
1165 for (pdtok
= pds
; *pdtok
&& **pdtok
; pdtok
++) {
1167 keyval
= g_strsplit(*pdtok
, "=", 0);
1168 if (!(dec
= srd_decoder_get_by_id(keyval
[0]))) {
1169 g_critical("Protocol decoder '%s' not found.", keyval
[0]);
1172 if (!dec
->annotations
) {
1173 g_critical("Protocol decoder '%s' has no annotations.", keyval
[0]);
1176 if (g_strv_length(keyval
) == 2) {
1177 for (l
= dec
->annotations
; l
; l
= l
->next
, ann
++) {
1178 ann_descr
= l
->data
;
1179 if (!canon_cmp(ann_descr
[0], keyval
[1]))
1184 g_critical("Annotation '%s' not found "
1185 "for protocol decoder '%s'.", keyval
[1], keyval
[0]);
1189 g_debug("cli: showing protocol decoder annotation %d from '%s'", ann
, keyval
[0]);
1190 g_hash_table_insert(pd_ann_visible
, g_strdup(keyval
[0]), GINT_TO_POINTER(ann
));
1199 void show_pd_annotations(struct srd_proto_data
*pdata
, void *cb_data
)
1203 gpointer ann_format
;
1205 /* 'cb_data' is not used in this specific callback. */
1208 if (!pd_ann_visible
)
1211 if (!g_hash_table_lookup_extended(pd_ann_visible
, pdata
->pdo
->di
->inst_id
,
1213 /* Not in the list of PDs whose annotations we're showing. */
1216 if (pdata
->ann_format
!= GPOINTER_TO_INT(ann_format
))
1217 /* We don't want this particular format from the PD. */
1220 annotations
= pdata
->data
;
1221 if (opt_loglevel
> SR_LOG_WARN
)
1222 printf("%"PRIu64
"-%"PRIu64
" ", pdata
->start_sample
, pdata
->end_sample
);
1223 printf("%s: ", pdata
->pdo
->proto_id
);
1224 for (i
= 0; annotations
[i
]; i
++)
1225 printf("\"%s\" ", annotations
[i
]);
1231 int setup_output_format(void)
1233 GHashTable
*fmtargs
;
1234 GHashTableIter iter
;
1235 gpointer key
, value
;
1236 struct sr_output_format
**outputs
;
1240 if (!opt_output_format
) {
1241 opt_output_format
= DEFAULT_OUTPUT_FORMAT
;
1242 /* we'll need to remember this so when saving to a file
1243 * later, sigrok session format will be used.
1245 default_output_format
= TRUE
;
1248 fmtargs
= parse_generic_arg(opt_output_format
, TRUE
);
1249 fmtspec
= g_hash_table_lookup(fmtargs
, "sigrok_key");
1251 g_critical("Invalid output format.");
1254 outputs
= sr_output_list();
1255 for (i
= 0; outputs
[i
]; i
++) {
1256 if (strcmp(outputs
[i
]->id
, fmtspec
))
1258 g_hash_table_remove(fmtargs
, "sigrok_key");
1259 output_format
= outputs
[i
];
1260 g_hash_table_iter_init(&iter
, fmtargs
);
1261 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
1262 /* only supporting one parameter per output module
1263 * for now, and only its value */
1264 output_format_param
= g_strdup(value
);
1269 if (!output_format
) {
1270 g_critical("Invalid output format %s.", opt_output_format
);
1273 g_hash_table_destroy(fmtargs
);
1278 static int select_probes(struct sr_dev_inst
*sdi
)
1280 struct sr_probe
*probe
;
1281 GSList
*selected_probes
, *l
;
1286 if (!(selected_probes
= parse_probestring(sdi
, opt_probes
)))
1289 for (l
= sdi
->probes
; l
; l
= l
->next
) {
1291 if (g_slist_find(selected_probes
, probe
))
1292 probe
->enabled
= TRUE
;
1294 probe
->enabled
= FALSE
;
1296 g_slist_free(selected_probes
);
1302 * Return the input file format which the CLI tool should use.
1304 * If the user specified -I / --input-format, use that one. Otherwise, try to
1305 * autodetect the format as good as possible. Failing that, return NULL.
1307 * @param filename The filename of the input file. Must not be NULL.
1308 * @param opt The -I / --input-file option the user specified (or NULL).
1310 * @return A pointer to the 'struct sr_input_format' that should be used,
1311 * or NULL if no input format was selected or auto-detected.
1313 static struct sr_input_format
*determine_input_file_format(
1314 const char *filename
, const char *opt
)
1317 struct sr_input_format
**inputs
;
1319 /* If there are no input formats, return NULL right away. */
1320 inputs
= sr_input_list();
1322 g_critical("No supported input formats available.");
1326 /* If the user specified -I / --input-format, use that one. */
1328 for (i
= 0; inputs
[i
]; i
++) {
1329 if (strcasecmp(inputs
[i
]->id
, opt
))
1331 g_debug("Using user-specified input file format '%s'.",
1336 /* The user specified an unknown input format, return NULL. */
1337 g_critical("Error: specified input file format '%s' is "
1342 /* Otherwise, try to find an input module that can handle this file. */
1343 for (i
= 0; inputs
[i
]; i
++) {
1344 if (inputs
[i
]->format_match(filename
))
1348 /* Return NULL if no input module wanted to touch this. */
1350 g_critical("Error: no matching input module found.");
1354 g_debug("cli: Autodetected '%s' input format for file '%s'.",
1355 inputs
[i
]->id
, filename
);
1360 static void load_input_file_format(void)
1362 GHashTable
*fmtargs
= NULL
;
1364 struct sr_input
*in
;
1365 struct sr_input_format
*input_format
;
1366 char *fmtspec
= NULL
;
1368 if (opt_input_format
) {
1369 fmtargs
= parse_generic_arg(opt_input_format
, TRUE
);
1370 fmtspec
= g_hash_table_lookup(fmtargs
, "sigrok_key");
1373 if (!(input_format
= determine_input_file_format(opt_input_file
,
1375 /* The exact cause was already logged. */
1380 g_hash_table_remove(fmtargs
, "sigrok_key");
1382 if (stat(opt_input_file
, &st
) == -1) {
1383 g_critical("Failed to load %s: %s", opt_input_file
,
1388 /* Initialize the input module. */
1389 if (!(in
= g_try_malloc(sizeof(struct sr_input
)))) {
1390 g_critical("Failed to allocate input module.");
1393 in
->format
= input_format
;
1394 in
->param
= fmtargs
;
1395 if (in
->format
->init
) {
1396 if (in
->format
->init(in
, opt_input_file
) != SR_OK
) {
1397 g_critical("Input format init failed.");
1402 if (select_probes(in
->sdi
) > 0)
1406 sr_session_datafeed_callback_add(datafeed_in
, NULL
);
1407 if (sr_session_dev_add(in
->sdi
) != SR_OK
) {
1408 g_critical("Failed to use device.");
1409 sr_session_destroy();
1413 input_format
->loadfile(in
, opt_input_file
);
1415 sr_session_destroy();
1418 g_hash_table_destroy(fmtargs
);
1421 static void load_input_file(void)
1424 if (sr_session_load(opt_input_file
) == SR_OK
) {
1425 /* sigrok session file */
1426 sr_session_datafeed_callback_add(datafeed_in
, NULL
);
1432 /* fall back on input modules */
1433 load_input_file_format();
1437 static int set_dev_options(struct sr_dev_inst
*sdi
, GHashTable
*args
)
1439 const struct sr_config_info
*srci
;
1440 GHashTableIter iter
;
1441 gpointer key
, value
;
1444 uint64_t tmp_u64
, p
, q
;
1446 GVariant
*val
, *rational
[2];
1448 g_hash_table_iter_init(&iter
, args
);
1449 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
1450 if (!(srci
= sr_config_info_name_get(key
))) {
1451 g_critical("Unknown device option '%s'.", (char *) key
);
1455 if ((value
== NULL
) &&
1456 (srci
->datatype
!= SR_T_BOOL
)) {
1457 g_critical("Option '%s' needs a value.", (char *)key
);
1461 switch (srci
->datatype
) {
1463 ret
= sr_parse_sizestring(value
, &tmp_u64
);
1466 val
= g_variant_new_uint64(tmp_u64
);
1469 val
= g_variant_new_string(value
);
1475 tmp_bool
= sr_parse_boolstring(value
);
1476 val
= g_variant_new_boolean(tmp_bool
);
1479 tmp_double
= strtof(value
, NULL
);
1480 val
= g_variant_new_double(tmp_double
);
1482 case SR_T_RATIONAL_PERIOD
:
1483 if ((ret
= sr_parse_period(value
, &p
, &q
)) != SR_OK
)
1485 rational
[0] = g_variant_new_uint64(p
);
1486 rational
[1] = g_variant_new_uint64(q
);
1487 val
= g_variant_new_tuple(rational
, 2);
1489 case SR_T_RATIONAL_VOLT
:
1490 if ((ret
= sr_parse_voltage(value
, &p
, &q
)) != SR_OK
)
1492 rational
[0] = g_variant_new_uint64(p
);
1493 rational
[1] = g_variant_new_uint64(q
);
1494 val
= g_variant_new_tuple(rational
, 2);
1500 ret
= sr_config_set(sdi
, srci
->key
, val
);
1502 g_critical("Failed to set device option '%s'.", (char *)key
);
1510 static void set_options(void)
1512 struct sr_dev_inst
*sdi
;
1514 GHashTable
*devargs
;
1517 g_critical("No setting specified.");
1521 if (!(devargs
= parse_generic_arg(opt_config
, FALSE
)))
1524 if (!(devices
= device_scan())) {
1525 g_critical("No devices found.");
1528 sdi
= devices
->data
;
1530 if (sr_dev_open(sdi
) != SR_OK
) {
1531 g_critical("Failed to open device.");
1535 set_dev_options(sdi
, devargs
);
1538 g_slist_free(devices
);
1539 g_hash_table_destroy(devargs
);
1543 static int set_limit_time(const struct sr_dev_inst
*sdi
)
1547 uint64_t samplerate
;
1549 if (!(time_msec
= sr_parse_timestring(opt_time
))) {
1550 g_critical("Invalid time '%s'", opt_time
);
1554 if (sr_dev_has_option(sdi
, SR_CONF_LIMIT_MSEC
)) {
1555 gvar
= g_variant_new_uint64(time_msec
);
1556 if (sr_config_set(sdi
, SR_CONF_LIMIT_MSEC
, gvar
) != SR_OK
) {
1557 g_critical("Failed to configure time limit.");
1560 } else if (sr_dev_has_option(sdi
, SR_CONF_SAMPLERATE
)) {
1561 /* Convert to samples based on the samplerate. */
1562 sr_config_get(sdi
->driver
, SR_CONF_SAMPLERATE
, &gvar
, sdi
);
1563 samplerate
= g_variant_get_uint64(gvar
);
1564 g_variant_unref(gvar
);
1565 limit_samples
= (samplerate
) * time_msec
/ (uint64_t)1000;
1566 if (limit_samples
== 0) {
1567 g_critical("Not enough time at this samplerate.");
1570 gvar
= g_variant_new_uint64(limit_samples
);
1571 if (sr_config_set(sdi
, SR_CONF_LIMIT_SAMPLES
, gvar
) != SR_OK
) {
1572 g_critical("Failed to configure time-based sample limit.");
1576 g_critical("This device does not support time limits.");
1583 static void run_session(void)
1586 GHashTable
*devargs
;
1588 struct sr_dev_inst
*sdi
;
1592 devices
= device_scan();
1594 g_critical("No devices found.");
1597 if (g_slist_length(devices
) > 1) {
1598 g_critical("sigrok-cli only supports one device for capturing.");
1601 sdi
= devices
->data
;
1604 sr_session_datafeed_callback_add(datafeed_in
, NULL
);
1606 if (sr_dev_open(sdi
) != SR_OK
) {
1607 g_critical("Failed to open device.");
1611 if (sr_session_dev_add(sdi
) != SR_OK
) {
1612 g_critical("Failed to add device to session.");
1613 sr_session_destroy();
1618 if ((devargs
= parse_generic_arg(opt_config
, FALSE
))) {
1619 if (set_dev_options(sdi
, devargs
) != SR_OK
)
1621 g_hash_table_destroy(devargs
);
1625 if (select_probes(sdi
) != SR_OK
) {
1626 g_critical("Failed to set probes.");
1627 sr_session_destroy();
1632 if (!(triggerlist
= sr_parse_triggerstring(sdi
, opt_triggers
))) {
1633 sr_session_destroy();
1636 max_probes
= g_slist_length(sdi
->probes
);
1637 for (i
= 0; i
< max_probes
; i
++) {
1638 if (triggerlist
[i
]) {
1639 sr_dev_trigger_set(sdi
, i
, triggerlist
[i
]);
1640 g_free(triggerlist
[i
]);
1643 g_free(triggerlist
);
1646 if (opt_continuous
) {
1647 if (!sr_dev_has_option(sdi
, SR_CONF_CONTINUOUS
)) {
1648 g_critical("This device does not support continuous sampling.");
1649 sr_session_destroy();
1655 if (set_limit_time(sdi
) != SR_OK
) {
1656 sr_session_destroy();
1662 if ((sr_parse_sizestring(opt_samples
, &limit_samples
) != SR_OK
)) {
1663 g_critical("Invalid sample limit '%s'.", opt_samples
);
1664 sr_session_destroy();
1667 gvar
= g_variant_new_uint64(limit_samples
);
1668 if (sr_config_set(sdi
, SR_CONF_LIMIT_SAMPLES
, gvar
) != SR_OK
) {
1669 g_critical("Failed to configure sample limit.");
1670 sr_session_destroy();
1676 if ((sr_parse_sizestring(opt_frames
, &limit_frames
) != SR_OK
)) {
1677 g_critical("Invalid sample limit '%s'.", opt_samples
);
1678 sr_session_destroy();
1681 gvar
= g_variant_new_uint64(limit_frames
);
1682 if (sr_config_set(sdi
, SR_CONF_LIMIT_FRAMES
, gvar
) != SR_OK
) {
1683 g_critical("Failed to configure frame limit.");
1684 sr_session_destroy();
1689 if (sr_session_start() != SR_OK
) {
1690 g_critical("Failed to start session.");
1691 sr_session_destroy();
1703 sr_session_destroy();
1704 g_slist_free(devices
);
1708 static void logger(const gchar
*log_domain
, GLogLevelFlags log_level
,
1709 const gchar
*message
, gpointer cb_data
)
1715 * All messages, warnings, errors etc. go to stderr (not stdout) in
1716 * order to not mess up the CLI tool data output, e.g. VCD output.
1718 if (log_level
& (G_LOG_LEVEL_ERROR
| G_LOG_LEVEL_CRITICAL
| G_LOG_LEVEL_WARNING
)
1719 || opt_loglevel
> SR_LOG_WARN
) {
1720 fprintf(stderr
, "%s\n", message
);
1725 int main(int argc
, char **argv
)
1728 GOptionContext
*context
;
1731 g_log_set_default_handler(logger
, NULL
);
1734 context
= g_option_context_new(NULL
);
1735 g_option_context_add_main_entries(context
, optargs
, NULL
);
1737 if (!g_option_context_parse(context
, &argc
, &argv
, &error
)) {
1738 g_critical("%s", error
->message
);
1742 /* Set the loglevel (amount of messages to output) for libsigrok. */
1743 if (sr_log_loglevel_set(opt_loglevel
) != SR_OK
)
1746 if (sr_init(&sr_ctx
) != SR_OK
)
1750 /* Set the loglevel (amount of messages to output) for libsigrokdecode. */
1751 if (srd_log_loglevel_set(opt_loglevel
) != SRD_OK
)
1755 if (srd_init(NULL
) != SRD_OK
)
1757 if (register_pds(NULL
, opt_pds
) != 0)
1759 if (srd_pd_output_callback_add(SRD_OUTPUT_ANN
,
1760 show_pd_annotations
, NULL
) != SRD_OK
)
1762 if (setup_pd_stack() != 0)
1764 if (setup_pd_annotations() != 0)
1769 if (setup_output_format() != 0)
1774 else if (opt_scan_devs
)
1777 else if (opt_pds
&& opt_show
)
1782 else if (opt_input_file
)
1786 else if (opt_samples
|| opt_time
|| opt_frames
|| opt_continuous
)
1789 printf("%s", g_option_context_get_help(context
, TRUE
, NULL
));
1802 g_option_context_free(context
);