2 * This file is part of the sigrok 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_list_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_dev
= 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 libsigrok/libsigrokdecode loglevel", NULL
},
82 {"list-devices", 'D', 0, G_OPTION_ARG_NONE
, &opt_list_devs
,
83 "Scan for devices", NULL
},
84 {"driver", 0, 0, G_OPTION_ARG_STRING
, &opt_drv
,
85 "Use only this driver", NULL
},
86 {"device", 'd', 0, G_OPTION_ARG_STRING
, &opt_dev
,
87 "Use specified device", 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 {"triggers", 't', 0, G_OPTION_ARG_STRING
, &opt_triggers
,
99 "Trigger configuration", NULL
},
100 {"wait-trigger", 'w', 0, G_OPTION_ARG_NONE
, &opt_wait_trigger
,
101 "Wait for trigger", NULL
},
103 {"protocol-decoders", 'a', 0, G_OPTION_ARG_STRING
, &opt_pds
,
104 "Protocol decoders to run", NULL
},
105 {"protocol-decoder-stack", 's', 0, G_OPTION_ARG_STRING
, &opt_pd_stack
,
106 "Protocol decoder stack", NULL
},
107 {"protocol-decoder-annotations", 'A', 0, G_OPTION_ARG_STRING
, &opt_pd_annotations
,
108 "Protocol decoder annotation(s) to show", 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 --device to select one.", num_devices
);
354 /* This properly opens and initializes the device, so we can get
355 * current settings. */
357 if (sr_session_dev_add(sdi
) != SR_OK
) {
358 g_critical("Failed to use device.");
362 if ((sr_config_list(sdi
->driver
, SR_CONF_SCAN_OPTIONS
, &gvar_opts
,
364 opts
= g_variant_get_fixed_array(gvar_opts
, &num_elements
,
366 printf("Supported driver options:\n");
367 for (i
= 0; i
< num_elements
; i
++) {
368 if (!(srci
= sr_config_info_get(opts
[i
])))
370 printf(" %s\n", srci
->id
);
372 g_variant_unref(gvar_opts
);
375 if ((sr_config_list(sdi
->driver
, SR_CONF_DEVICE_OPTIONS
, &gvar_opts
,
377 /* Driver supports no device instance options. */
380 printf("Supported device options:\n");
381 opts
= g_variant_get_fixed_array(gvar_opts
, &num_opts
, sizeof(int32_t));
382 for (o
= 0; o
< num_opts
; o
++) {
383 if (!(srci
= sr_config_info_get(opts
[o
])))
386 if (srci
->key
== SR_CONF_TRIGGER_TYPE
) {
387 if (sr_config_list(sdi
->driver
, srci
->key
, &gvar
,
392 charopts
= g_variant_get_string(gvar
, NULL
);
393 printf(" Supported triggers: ");
395 printf("%c ", *charopts
);
399 g_variant_unref(gvar
);
401 } else if (srci
->key
== SR_CONF_PATTERN_MODE
) {
402 /* Pattern generator modes */
403 printf(" %s", srci
->id
);
404 if (sr_config_list(sdi
->driver
, srci
->key
, &gvar
,
406 printf(" - supported patterns:\n");
407 stropts
= g_variant_get_strv(gvar
, &num_elements
);
408 for (i
= 0; i
< num_elements
; i
++)
409 printf(" %s\n", stropts
[i
]);
410 g_variant_unref(gvar
);
415 } else if (srci
->key
== SR_CONF_SAMPLERATE
) {
416 /* Supported samplerates */
417 printf(" %s", srci
->id
);
418 if (sr_config_list(sdi
->driver
, SR_CONF_SAMPLERATE
,
419 &gvar_dict
, sdi
) != SR_OK
) {
423 if ((gvar_list
= g_variant_lookup_value(gvar_dict
,
424 "samplerates", G_VARIANT_TYPE("at")))) {
425 uint64
= g_variant_get_fixed_array(gvar_list
,
426 &num_elements
, sizeof(uint64_t));
427 printf(" - supported samplerates:\n");
428 for (i
= 0; i
< num_elements
; i
++)
429 printf(" %s\n", sr_samplerate_string(uint64
[i
]));
430 } if ((gvar_list
= g_variant_lookup_value(gvar_dict
,
431 "samplerate-steps", G_VARIANT_TYPE("at")))) {
432 uint64
= g_variant_get_fixed_array(gvar_list
,
433 &num_elements
, sizeof(uint64_t));
435 if (!(s
= sr_samplerate_string(uint64
[0])))
440 if (!(s
= sr_samplerate_string(uint64
[1])))
445 if (!(s
= sr_samplerate_string(uint64
[2])))
447 printf(" in steps of %s)\n", s
);
449 g_variant_unref(gvar_list
);
451 g_variant_unref(gvar_dict
);
453 } else if (srci
->key
== SR_CONF_BUFFERSIZE
) {
454 /* Supported buffer sizes */
455 printf(" %s", srci
->id
);
456 if (sr_config_list(sdi
->driver
, SR_CONF_BUFFERSIZE
,
457 &gvar_list
, sdi
) != SR_OK
) {
461 uint64
= g_variant_get_fixed_array(gvar_list
,
462 &num_elements
, sizeof(uint64_t));
463 printf(" - supported buffer sizes:\n");
464 for (i
= 0; i
< num_elements
; i
++)
465 printf(" %"PRIu64
"\n", uint64
[i
]);
466 g_variant_unref(gvar_list
);
468 } else if (srci
->key
== SR_CONF_TIMEBASE
) {
469 /* Supported time bases */
470 printf(" %s", srci
->id
);
471 if (sr_config_list(sdi
->driver
, SR_CONF_TIMEBASE
,
472 &gvar_list
, sdi
) != SR_OK
) {
476 printf(" - supported time bases:\n");
477 num_elements
= g_variant_n_children(gvar_list
);
478 for (i
= 0; i
< num_elements
; i
++) {
479 gvar
= g_variant_get_child_value(gvar_list
, i
);
480 g_variant_get(gvar
, "(tt)", &p
, &q
);
481 s
= sr_period_string(p
* q
);
485 g_variant_unref(gvar_list
);
487 } else if (srci
->key
== SR_CONF_TRIGGER_SOURCE
) {
488 /* Supported trigger sources */
489 printf(" %s", srci
->id
);
490 if (sr_config_list(sdi
->driver
, SR_CONF_TRIGGER_SOURCE
,
491 &gvar
, sdi
) != SR_OK
) {
495 printf(" - supported trigger sources:\n");
496 stropts
= g_variant_get_strv(gvar
, &num_elements
);
497 for (i
= 0; i
< num_elements
; i
++)
498 printf(" %s\n", stropts
[i
]);
499 g_variant_unref(gvar
);
501 } else if (srci
->key
== SR_CONF_FILTER
) {
502 /* Supported filters */
503 printf(" %s", srci
->id
);
504 if (sr_config_list(sdi
->driver
, SR_CONF_FILTER
,
505 &gvar
, sdi
) != SR_OK
) {
509 printf(" - supported filter targets:\n");
510 stropts
= g_variant_get_strv(gvar
, &num_elements
);
511 for (i
= 0; i
< num_elements
; i
++)
512 printf(" %s\n", stropts
[i
]);
513 g_variant_unref(gvar
);
515 } else if (srci
->key
== SR_CONF_VDIV
) {
516 /* Supported volts/div values */
517 printf(" %s", srci
->id
);
518 if (sr_config_list(sdi
->driver
, SR_CONF_VDIV
,
519 &gvar_list
, sdi
) != SR_OK
) {
523 printf(" - supported volts/div:\n");
524 num_elements
= g_variant_n_children(gvar_list
);
525 for (i
= 0; i
< num_elements
; i
++) {
526 gvar
= g_variant_get_child_value(gvar_list
, i
);
527 g_variant_get(gvar
, "(tt)", &p
, &q
);
528 s
= sr_voltage_string(p
, q
);
532 g_variant_unref(gvar_list
);
534 } else if (srci
->key
== SR_CONF_COUPLING
) {
535 /* Supported coupling settings */
536 printf(" %s", srci
->id
);
537 if (sr_config_list(sdi
->driver
, SR_CONF_COUPLING
,
538 &gvar
, sdi
) != SR_OK
) {
542 printf(" - supported coupling options:\n");
543 stropts
= g_variant_get_strv(gvar
, &num_elements
);
544 for (i
= 0; i
< num_elements
; i
++)
545 printf(" %s\n", stropts
[i
]);
546 g_variant_unref(gvar
);
548 } else if (srci
->key
== SR_CONF_DATALOG
) {
550 /* Turning on/off internal data logging. */
551 printf(" %s\t(on/off", srci
->id
);
552 if (sr_config_get(sdi
->driver
, SR_CONF_DATALOG
,
553 &gvar
, sdi
) == SR_OK
) {
554 tmp_bool
= g_variant_get_boolean(gvar
);
555 printf(", currently %s", tmp_bool
? "on" : "off");
556 g_variant_unref(gvar
);
560 /* Everything else */
561 printf(" %s\n", srci
->id
);
564 g_variant_unref(gvar_opts
);
566 sr_session_destroy();
571 static void show_pd_detail(void)
574 struct srd_decoder
*dec
;
575 struct srd_decoder_option
*o
;
576 char **pdtokens
, **pdtok
, *optsep
, **ann
, *val
, *doc
;
579 pdtokens
= g_strsplit(opt_pds
, ",", -1);
580 for (pdtok
= pdtokens
; *pdtok
; pdtok
++) {
582 if ((optsep
= strchr(*pdtok
, ':')))
584 if (!(dec
= srd_decoder_get_by_id(*pdtok
))) {
585 g_critical("Protocol decoder %s not found.", *pdtok
);
588 printf("ID: %s\nName: %s\nLong name: %s\nDescription: %s\n",
589 dec
->id
, dec
->name
, dec
->longname
, dec
->desc
);
590 printf("License: %s\n", dec
->license
);
591 printf("Annotations:\n");
592 if (dec
->annotations
) {
593 for (l
= dec
->annotations
; l
; l
= l
->next
) {
595 printf("- %s\n %s\n", ann
[0], ann
[1]);
600 printf("Required probes:\n");
602 for (l
= dec
->probes
; l
; l
= l
->next
) {
604 printf("- %s (%s): %s\n",
605 p
->name
, p
->id
, p
->desc
);
610 printf("Optional probes:\n");
611 if (dec
->opt_probes
) {
612 for (l
= dec
->opt_probes
; l
; l
= l
->next
) {
614 printf("- %s (%s): %s\n",
615 p
->name
, p
->id
, p
->desc
);
621 printf("Options:\n");
622 for (l
= dec
->options
; l
; l
= l
->next
) {
624 val
= g_variant_print(o
->def
, FALSE
);
625 printf("- %s: %s (default %s)\n", o
->id
, o
->desc
, val
);
629 if ((doc
= srd_decoder_doc_get(dec
))) {
630 printf("Documentation:\n%s\n",
631 doc
[0] == '\n' ? doc
+ 1 : doc
);
636 g_strfreev(pdtokens
);
640 static GArray
*get_enabled_logic_probes(const struct sr_dev_inst
*sdi
)
642 struct sr_probe
*probe
;
646 probes
= g_array_new(FALSE
, FALSE
, sizeof(int));
647 for (l
= sdi
->probes
; l
; l
= l
->next
) {
649 if (probe
->type
!= SR_PROBE_LOGIC
)
651 if (probe
->enabled
!= TRUE
)
653 g_array_append_val(probes
, probe
->index
);
659 static void datafeed_in(const struct sr_dev_inst
*sdi
,
660 const struct sr_datafeed_packet
*packet
, void *cb_data
)
662 const struct sr_datafeed_meta
*meta
;
663 const struct sr_datafeed_logic
*logic
;
664 const struct sr_datafeed_analog
*analog
;
665 struct sr_config
*src
;
666 static struct sr_output
*o
= NULL
;
667 static GArray
*logic_probelist
= NULL
;
668 static uint64_t received_samples
= 0;
669 static int unitsize
= 0;
670 static int triggered
= 0;
671 static FILE *outfile
= NULL
;
675 int sample_size
, ret
;
676 uint64_t samplerate
, output_len
, filter_out_len
;
677 uint8_t *output_buf
, *filter_out
;
681 /* If the first packet to come in isn't a header, don't even try. */
682 if (packet
->type
!= SR_DF_HEADER
&& o
== NULL
)
686 switch (packet
->type
) {
688 g_debug("cli: Received SR_DF_HEADER");
689 /* Initialize the output module. */
690 if (!(o
= g_try_malloc(sizeof(struct sr_output
)))) {
691 g_critical("Output module malloc failed.");
694 o
->format
= output_format
;
695 o
->sdi
= (struct sr_dev_inst
*)sdi
;
696 o
->param
= output_format_param
;
697 if (o
->format
->init
) {
698 if (o
->format
->init(o
) != SR_OK
) {
699 g_critical("Output format initialization failed.");
704 /* Prepare non-stdout output. */
706 if (opt_output_file
) {
707 if (default_output_format
) {
708 /* output file is in session format, so we'll
709 * keep a copy of everything as it comes in
710 * and save from there after the session. */
712 savebuf
= g_byte_array_new();
714 /* saving to a file in whatever format was set
715 * with --format, so all we need is a filehandle */
716 outfile
= g_fopen(opt_output_file
, "wb");
720 /* Prepare for logic data. */
721 logic_probelist
= get_enabled_logic_probes(sdi
);
722 /* How many bytes we need to store the packed samples. */
723 unitsize
= (logic_probelist
->len
+ 7) / 8;
726 if (opt_pds
&& logic_probelist
->len
) {
727 if (sr_config_get(sdi
->driver
, SR_CONF_SAMPLERATE
,
728 &gvar
, sdi
) != SR_OK
) {
729 g_critical("Unable to initialize protocol "
730 "decoders: no samplerate found.");
733 samplerate
= g_variant_get_uint64(gvar
);
734 g_variant_unref(gvar
);
735 srd_session_start(logic_probelist
->len
, unitsize
, samplerate
);
741 g_debug("cli: received SR_DF_META");
742 meta
= packet
->payload
;
743 for (l
= meta
->config
; l
; l
= l
->next
) {
746 case SR_CONF_SAMPLERATE
:
747 samplerate
= g_variant_get_uint64(src
->data
);
748 g_debug("cli: got samplerate %"PRIu64
" Hz", samplerate
);
750 case SR_CONF_SAMPLE_INTERVAL
:
751 samplerate
= g_variant_get_uint64(src
->data
);
752 g_debug("cli: got sample interval %"PRIu64
" ms", samplerate
);
755 /* Unknown metadata is not an error. */
762 g_debug("cli: received SR_DF_TRIGGER");
763 if (o
->format
->event
)
764 o
->format
->event(o
, SR_DF_TRIGGER
, &output_buf
,
770 logic
= packet
->payload
;
771 g_message("cli: received SR_DF_LOGIC, %"PRIu64
" bytes", logic
->length
);
772 sample_size
= logic
->unitsize
;
773 if (logic
->length
== 0)
776 /* Don't store any samples until triggered. */
777 if (opt_wait_trigger
&& !triggered
)
780 if (limit_samples
&& received_samples
>= limit_samples
)
783 ret
= sr_filter_probes(sample_size
, unitsize
, logic_probelist
,
784 logic
->data
, logic
->length
,
785 &filter_out
, &filter_out_len
);
789 /* What comes out of the filter is guaranteed to be packed into the
790 * minimum size needed to support the number of samples at this sample
791 * size. however, the driver may have submitted too much -- cut off
792 * the buffer of the last packet according to the sample limit.
794 if (limit_samples
&& (received_samples
+ logic
->length
/ sample_size
>
795 limit_samples
* sample_size
))
796 filter_out_len
= limit_samples
* sample_size
- received_samples
;
798 if (opt_output_file
&& default_output_format
) {
799 /* Saving to a session file. */
800 g_byte_array_append(savebuf
, filter_out
, filter_out_len
);
804 if (srd_session_send(received_samples
, (uint8_t*)filter_out
,
805 filter_out_len
) != SRD_OK
)
810 if (o
->format
->data
&& packet
->type
== o
->format
->df_type
)
811 o
->format
->data(o
, filter_out
, filter_out_len
,
812 &output_buf
, &output_len
);
814 fwrite(output_buf
, 1, output_len
, outfile
);
822 received_samples
+= logic
->length
/ sample_size
;
826 analog
= packet
->payload
;
827 g_message("cli: received SR_DF_ANALOG, %d samples", analog
->num_samples
);
828 if (analog
->num_samples
== 0)
831 if (limit_samples
&& received_samples
>= limit_samples
)
834 if (o
->format
->data
&& packet
->type
== o
->format
->df_type
) {
835 o
->format
->data(o
, (const uint8_t *)analog
->data
,
836 analog
->num_samples
* sizeof(float),
837 &output_buf
, &output_len
);
839 fwrite(output_buf
, 1, output_len
, outfile
);
845 received_samples
+= analog
->num_samples
;
848 case SR_DF_FRAME_BEGIN
:
849 g_debug("cli: received SR_DF_FRAME_BEGIN");
850 if (o
->format
->event
) {
851 o
->format
->event(o
, SR_DF_FRAME_BEGIN
, &output_buf
,
854 fwrite(output_buf
, 1, output_len
, outfile
);
861 case SR_DF_FRAME_END
:
862 g_debug("cli: received SR_DF_FRAME_END");
863 if (o
->format
->event
) {
864 o
->format
->event(o
, SR_DF_FRAME_END
, &output_buf
,
867 fwrite(output_buf
, 1, output_len
, outfile
);
878 if (o
&& o
->format
->recv
) {
879 out
= o
->format
->recv(o
, sdi
, packet
);
880 if (out
&& out
->len
) {
881 fwrite(out
->str
, 1, out
->len
, outfile
);
886 /* SR_DF_END needs to be handled after the output module's recv()
887 * is called, so it can properly clean up that module etc. */
888 if (packet
->type
== SR_DF_END
) {
889 g_debug("cli: Received SR_DF_END");
891 if (o
->format
->event
) {
892 o
->format
->event(o
, SR_DF_END
, &output_buf
, &output_len
);
895 fwrite(output_buf
, 1, output_len
, outfile
);
901 if (limit_samples
&& received_samples
< limit_samples
)
902 g_warning("Device only sent %" PRIu64
" samples.",
906 g_warning("Device stopped after %" PRIu64
" samples.",
909 g_array_free(logic_probelist
, TRUE
);
911 if (o
->format
->cleanup
)
912 o
->format
->cleanup(o
);
916 if (outfile
&& outfile
!= stdout
)
919 if (opt_output_file
&& default_output_format
) {
920 if (sr_session_save(opt_output_file
, sdi
, savebuf
->data
,
921 unitsize
, savebuf
->len
/ unitsize
) != SR_OK
)
922 g_critical("Failed to save session.");
923 g_byte_array_free(savebuf
, FALSE
);
930 static int opts_to_gvar(struct srd_decoder
*dec
, GHashTable
*hash
,
931 GHashTable
**options
)
933 struct srd_decoder_option
*o
;
938 char *val_str
, *conv
;
941 *options
= g_hash_table_new_full(g_str_hash
, g_str_equal
, g_free
,
942 (GDestroyNotify
)g_variant_unref
);
944 for (optl
= dec
->options
; optl
; optl
= optl
->next
) {
946 if (!(val_str
= g_hash_table_lookup(hash
, o
->id
)))
949 if (g_variant_is_of_type(o
->def
, G_VARIANT_TYPE_STRING
)) {
950 gvar
= g_variant_new_string(val_str
);
951 } else if (g_variant_is_of_type(o
->def
, G_VARIANT_TYPE_INT64
)) {
952 val_int
= strtoll(val_str
, &conv
, 10);
953 if (!conv
|| conv
== val_str
) {
954 g_critical("Protocol decoder '%s' option '%s' "
955 "requires a number.", dec
->name
, o
->id
);
959 gvar
= g_variant_new_int64(val_int
);
961 g_critical("Unsupported type for option '%s' (%s)",
962 o
->id
, g_variant_get_type_string(o
->def
));
966 g_variant_ref_sink(gvar
);
967 g_hash_table_insert(*options
, g_strdup(o
->id
), gvar
);
968 g_hash_table_remove(hash
, o
->id
);
974 static int probes_to_gvar(struct srd_decoder
*dec
, GHashTable
*hash
,
978 GSList
*all_probes
, *l
;
982 char *val_str
, *conv
;
985 *probes
= g_hash_table_new_full(g_str_hash
, g_str_equal
, g_free
,
986 (GDestroyNotify
)g_variant_unref
);
988 all_probes
= g_slist_copy(dec
->probes
);
989 all_probes
= g_slist_concat(all_probes
, dec
->opt_probes
);
990 for (l
= all_probes
; l
; l
= l
->next
) {
992 if (!(val_str
= g_hash_table_lookup(hash
, p
->id
)))
995 val_int
= strtoll(val_str
, &conv
, 10);
996 if (!conv
|| conv
== val_str
) {
997 g_critical("Protocol decoder '%s' probes '%s' "
998 "is not a number.", dec
->name
, p
->id
);
1002 gvar
= g_variant_new_int32(val_int
);
1003 g_variant_ref_sink(gvar
);
1004 g_hash_table_insert(*probes
, g_strdup(p
->id
), gvar
);
1005 g_hash_table_remove(hash
, p
->id
);
1007 g_slist_free(all_probes
);
1012 /* Register the given PDs for this session.
1013 * Accepts a string of the form: "spi:sck=3:sdata=4,spi:sck=3:sdata=5"
1014 * That will instantiate two SPI decoders on the clock but different data
1017 static int register_pds(struct sr_dev
*dev
, const char *pdstring
)
1019 struct srd_decoder
*dec
;
1020 GHashTable
*pd_opthash
, *options
, *probes
;
1021 GList
*leftover
, *l
;
1022 struct srd_decoder_inst
*di
;
1024 char **pdtokens
, **pdtok
, *pd_name
;
1029 pd_ann_visible
= g_hash_table_new_full(g_str_hash
, g_int_equal
,
1032 pd_opthash
= options
= probes
= NULL
;
1033 pdtokens
= g_strsplit(pdstring
, ",", 0);
1034 for (pdtok
= pdtokens
; *pdtok
; pdtok
++) {
1035 if (!(pd_opthash
= parse_generic_arg(*pdtok
, TRUE
))) {
1036 g_critical("Invalid protocol decoder option '%s'.", *pdtok
);
1040 pd_name
= g_strdup(g_hash_table_lookup(pd_opthash
, "sigrok_key"));
1041 g_hash_table_remove(pd_opthash
, "sigrok_key");
1042 if (srd_decoder_load(pd_name
) != SRD_OK
) {
1043 g_critical("Failed to load protocol decoder %s.", pd_name
);
1047 dec
= srd_decoder_get_by_id(pd_name
);
1049 /* Convert decoder option and probe values to GVariant. */
1050 if (!opts_to_gvar(dec
, pd_opthash
, &options
)) {
1054 if (!probes_to_gvar(dec
, pd_opthash
, &probes
)) {
1058 if (g_hash_table_size(pd_opthash
) > 0) {
1059 leftover
= g_hash_table_get_keys(pd_opthash
);
1060 for (l
= leftover
; l
; l
= l
->next
)
1061 g_critical("Unknown option or probe '%s'", (char *)l
->data
);
1062 g_list_free(leftover
);
1066 if (!(di
= srd_inst_new(pd_name
, options
))) {
1067 g_critical("Failed to instantiate protocol decoder %s.", pd_name
);
1072 /* If no annotation list was specified, add them all in now.
1073 * This will be pared down later to leave only the last PD
1076 if (!opt_pd_annotations
)
1077 g_hash_table_insert(pd_ann_visible
,
1078 g_strdup(di
->inst_id
), NULL
);
1080 /* Remap the probes if needed. */
1081 if (srd_inst_probe_set_all(di
, probes
) != SRD_OK
) {
1087 g_strfreev(pdtokens
);
1089 g_hash_table_destroy(pd_opthash
);
1091 g_hash_table_destroy(options
);
1093 g_hash_table_destroy(probes
);
1100 int setup_pd_stack(void)
1102 struct srd_decoder_inst
*di_from
, *di_to
;
1106 /* Set up the protocol decoder stack. */
1107 pds
= g_strsplit(opt_pds
, ",", 0);
1108 if (g_strv_length(pds
) > 1) {
1110 /* A stack setup was specified, use that. */
1112 pds
= g_strsplit(opt_pd_stack
, ",", 0);
1113 if (g_strv_length(pds
) < 2) {
1115 g_critical("Specify at least two protocol decoders to stack.");
1120 /* First PD goes at the bottom of the stack. */
1121 ids
= g_strsplit(pds
[0], ":", 0);
1122 if (!(di_from
= srd_inst_find_by_id(ids
[0]))) {
1124 g_critical("Cannot stack protocol decoder '%s': "
1125 "instance not found.", pds
[0]);
1130 /* Every subsequent PD goes on top. */
1131 for (i
= 1; pds
[i
]; i
++) {
1132 ids
= g_strsplit(pds
[i
], ":", 0);
1133 if (!(di_to
= srd_inst_find_by_id(ids
[0]))) {
1135 g_critical("Cannot stack protocol decoder '%s': "
1136 "instance not found.", pds
[i
]);
1140 if ((ret
= srd_inst_stack(di_from
, di_to
)) != SRD_OK
)
1143 /* Don't show annotation from this PD. Only the last PD in
1144 * the stack will be left on the annotation list (unless
1145 * the annotation list was specifically provided).
1147 if (!opt_pd_annotations
)
1148 g_hash_table_remove(pd_ann_visible
,
1159 int setup_pd_annotations(void)
1162 struct srd_decoder
*dec
;
1164 char **pds
, **pdtok
, **keyval
, **ann_descr
;
1166 /* Set up custom list of PDs and annotations to show. */
1167 if (opt_pd_annotations
) {
1168 pds
= g_strsplit(opt_pd_annotations
, ",", 0);
1169 for (pdtok
= pds
; *pdtok
&& **pdtok
; pdtok
++) {
1171 keyval
= g_strsplit(*pdtok
, "=", 0);
1172 if (!(dec
= srd_decoder_get_by_id(keyval
[0]))) {
1173 g_critical("Protocol decoder '%s' not found.", keyval
[0]);
1176 if (!dec
->annotations
) {
1177 g_critical("Protocol decoder '%s' has no annotations.", keyval
[0]);
1180 if (g_strv_length(keyval
) == 2) {
1181 for (l
= dec
->annotations
; l
; l
= l
->next
, ann
++) {
1182 ann_descr
= l
->data
;
1183 if (!canon_cmp(ann_descr
[0], keyval
[1]))
1188 g_critical("Annotation '%s' not found "
1189 "for protocol decoder '%s'.", keyval
[1], keyval
[0]);
1193 g_debug("cli: showing protocol decoder annotation %d from '%s'", ann
, keyval
[0]);
1194 g_hash_table_insert(pd_ann_visible
, g_strdup(keyval
[0]), GINT_TO_POINTER(ann
));
1203 void show_pd_annotations(struct srd_proto_data
*pdata
, void *cb_data
)
1207 gpointer ann_format
;
1209 /* 'cb_data' is not used in this specific callback. */
1212 if (!pd_ann_visible
)
1215 if (!g_hash_table_lookup_extended(pd_ann_visible
, pdata
->pdo
->di
->inst_id
,
1217 /* Not in the list of PDs whose annotations we're showing. */
1220 if (pdata
->ann_format
!= GPOINTER_TO_INT(ann_format
))
1221 /* We don't want this particular format from the PD. */
1224 annotations
= pdata
->data
;
1225 if (opt_loglevel
> SR_LOG_WARN
)
1226 printf("%"PRIu64
"-%"PRIu64
" ", pdata
->start_sample
, pdata
->end_sample
);
1227 printf("%s: ", pdata
->pdo
->proto_id
);
1228 for (i
= 0; annotations
[i
]; i
++)
1229 printf("\"%s\" ", annotations
[i
]);
1235 int setup_output_format(void)
1237 GHashTable
*fmtargs
;
1238 GHashTableIter iter
;
1239 gpointer key
, value
;
1240 struct sr_output_format
**outputs
;
1244 if (!opt_output_format
) {
1245 opt_output_format
= DEFAULT_OUTPUT_FORMAT
;
1246 /* we'll need to remember this so when saving to a file
1247 * later, sigrok session format will be used.
1249 default_output_format
= TRUE
;
1252 fmtargs
= parse_generic_arg(opt_output_format
, TRUE
);
1253 fmtspec
= g_hash_table_lookup(fmtargs
, "sigrok_key");
1255 g_critical("Invalid output format.");
1258 outputs
= sr_output_list();
1259 for (i
= 0; outputs
[i
]; i
++) {
1260 if (strcmp(outputs
[i
]->id
, fmtspec
))
1262 g_hash_table_remove(fmtargs
, "sigrok_key");
1263 output_format
= outputs
[i
];
1264 g_hash_table_iter_init(&iter
, fmtargs
);
1265 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
1266 /* only supporting one parameter per output module
1267 * for now, and only its value */
1268 output_format_param
= g_strdup(value
);
1273 if (!output_format
) {
1274 g_critical("Invalid output format %s.", opt_output_format
);
1277 g_hash_table_destroy(fmtargs
);
1282 static int select_probes(struct sr_dev_inst
*sdi
)
1284 struct sr_probe
*probe
;
1285 GSList
*selected_probes
, *l
;
1290 if (!(selected_probes
= parse_probestring(sdi
, opt_probes
)))
1293 for (l
= sdi
->probes
; l
; l
= l
->next
) {
1295 if (g_slist_find(selected_probes
, probe
))
1296 probe
->enabled
= TRUE
;
1298 probe
->enabled
= FALSE
;
1300 g_slist_free(selected_probes
);
1306 * Return the input file format which the CLI tool should use.
1308 * If the user specified -I / --input-format, use that one. Otherwise, try to
1309 * autodetect the format as good as possible. Failing that, return NULL.
1311 * @param filename The filename of the input file. Must not be NULL.
1312 * @param opt The -I / --input-file option the user specified (or NULL).
1314 * @return A pointer to the 'struct sr_input_format' that should be used,
1315 * or NULL if no input format was selected or auto-detected.
1317 static struct sr_input_format
*determine_input_file_format(
1318 const char *filename
, const char *opt
)
1321 struct sr_input_format
**inputs
;
1323 /* If there are no input formats, return NULL right away. */
1324 inputs
= sr_input_list();
1326 g_critical("No supported input formats available.");
1330 /* If the user specified -I / --input-format, use that one. */
1332 for (i
= 0; inputs
[i
]; i
++) {
1333 if (strcasecmp(inputs
[i
]->id
, opt
))
1335 g_debug("Using user-specified input file format '%s'.",
1340 /* The user specified an unknown input format, return NULL. */
1341 g_critical("Error: specified input file format '%s' is "
1346 /* Otherwise, try to find an input module that can handle this file. */
1347 for (i
= 0; inputs
[i
]; i
++) {
1348 if (inputs
[i
]->format_match(filename
))
1352 /* Return NULL if no input module wanted to touch this. */
1354 g_critical("Error: no matching input module found.");
1358 g_debug("cli: Autodetected '%s' input format for file '%s'.",
1359 inputs
[i
]->id
, filename
);
1364 static void load_input_file_format(void)
1366 GHashTable
*fmtargs
= NULL
;
1368 struct sr_input
*in
;
1369 struct sr_input_format
*input_format
;
1370 char *fmtspec
= NULL
;
1372 if (opt_input_format
) {
1373 fmtargs
= parse_generic_arg(opt_input_format
, TRUE
);
1374 fmtspec
= g_hash_table_lookup(fmtargs
, "sigrok_key");
1377 if (!(input_format
= determine_input_file_format(opt_input_file
,
1379 /* The exact cause was already logged. */
1384 g_hash_table_remove(fmtargs
, "sigrok_key");
1386 if (stat(opt_input_file
, &st
) == -1) {
1387 g_critical("Failed to load %s: %s", opt_input_file
,
1392 /* Initialize the input module. */
1393 if (!(in
= g_try_malloc(sizeof(struct sr_input
)))) {
1394 g_critical("Failed to allocate input module.");
1397 in
->format
= input_format
;
1398 in
->param
= fmtargs
;
1399 if (in
->format
->init
) {
1400 if (in
->format
->init(in
, opt_input_file
) != SR_OK
) {
1401 g_critical("Input format init failed.");
1406 if (select_probes(in
->sdi
) > 0)
1410 sr_session_datafeed_callback_add(datafeed_in
, NULL
);
1411 if (sr_session_dev_add(in
->sdi
) != SR_OK
) {
1412 g_critical("Failed to use device.");
1413 sr_session_destroy();
1417 input_format
->loadfile(in
, opt_input_file
);
1419 sr_session_destroy();
1422 g_hash_table_destroy(fmtargs
);
1425 static void load_input_file(void)
1428 if (sr_session_load(opt_input_file
) == SR_OK
) {
1429 /* sigrok session file */
1430 sr_session_datafeed_callback_add(datafeed_in
, NULL
);
1436 /* fall back on input modules */
1437 load_input_file_format();
1441 static int set_dev_options(struct sr_dev_inst
*sdi
, GHashTable
*args
)
1443 const struct sr_config_info
*srci
;
1444 GHashTableIter iter
;
1445 gpointer key
, value
;
1448 uint64_t tmp_u64
, p
, q
;
1450 GVariant
*val
, *rational
[2];
1452 g_hash_table_iter_init(&iter
, args
);
1453 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
1454 if (!(srci
= sr_config_info_name_get(key
))) {
1455 g_critical("Unknown device option '%s'.", (char *) key
);
1459 if ((value
== NULL
) &&
1460 (srci
->datatype
!= SR_T_BOOL
)) {
1461 g_critical("Option '%s' needs a value.", (char *)key
);
1465 switch (srci
->datatype
) {
1467 ret
= sr_parse_sizestring(value
, &tmp_u64
);
1470 val
= g_variant_new_uint64(tmp_u64
);
1473 val
= g_variant_new_string(value
);
1479 tmp_bool
= sr_parse_boolstring(value
);
1480 val
= g_variant_new_boolean(tmp_bool
);
1483 tmp_double
= strtof(value
, NULL
);
1484 val
= g_variant_new_double(tmp_double
);
1486 case SR_T_RATIONAL_PERIOD
:
1487 if ((ret
= sr_parse_period(value
, &p
, &q
)) != SR_OK
)
1489 rational
[0] = g_variant_new_uint64(p
);
1490 rational
[1] = g_variant_new_uint64(q
);
1491 val
= g_variant_new_tuple(rational
, 2);
1493 case SR_T_RATIONAL_VOLT
:
1494 if ((ret
= sr_parse_voltage(value
, &p
, &q
)) != SR_OK
)
1496 rational
[0] = g_variant_new_uint64(p
);
1497 rational
[1] = g_variant_new_uint64(q
);
1498 val
= g_variant_new_tuple(rational
, 2);
1504 ret
= sr_config_set(sdi
, srci
->key
, val
);
1506 g_critical("Failed to set device option '%s'.", (char *)key
);
1514 static void set_options(void)
1516 struct sr_dev_inst
*sdi
;
1518 GHashTable
*devargs
;
1521 g_critical("No setting specified.");
1525 if (!(devargs
= parse_generic_arg(opt_dev
, FALSE
)))
1528 if (!(devices
= device_scan())) {
1529 g_critical("No devices found.");
1532 sdi
= devices
->data
;
1535 if (sr_session_dev_add(sdi
) != SR_OK
) {
1536 g_critical("Failed to use device.");
1540 set_dev_options(sdi
, devargs
);
1542 sr_session_destroy();
1543 g_slist_free(devices
);
1544 g_hash_table_destroy(devargs
);
1548 static int set_limit_time(const struct sr_dev_inst
*sdi
)
1552 uint64_t samplerate
;
1554 if (!(time_msec
= sr_parse_timestring(opt_time
))) {
1555 g_critical("Invalid time '%s'", opt_time
);
1559 if (sr_dev_has_option(sdi
, SR_CONF_LIMIT_MSEC
)) {
1560 gvar
= g_variant_new_uint64(time_msec
);
1561 if (sr_config_set(sdi
, SR_CONF_LIMIT_MSEC
, gvar
) != SR_OK
) {
1562 g_critical("Failed to configure time limit.");
1565 } else if (sr_dev_has_option(sdi
, SR_CONF_SAMPLERATE
)) {
1566 /* Convert to samples based on the samplerate. */
1567 sr_config_get(sdi
->driver
, SR_CONF_SAMPLERATE
, &gvar
, sdi
);
1568 samplerate
= g_variant_get_uint64(gvar
);
1569 g_variant_unref(gvar
);
1570 limit_samples
= (samplerate
) * time_msec
/ (uint64_t)1000;
1571 if (limit_samples
== 0) {
1572 g_critical("Not enough time at this samplerate.");
1575 gvar
= g_variant_new_uint64(limit_samples
);
1576 if (sr_config_set(sdi
, SR_CONF_LIMIT_SAMPLES
, gvar
) != SR_OK
) {
1577 g_critical("Failed to configure time-based sample limit.");
1581 g_critical("This device does not support time limits.");
1588 static void run_session(void)
1591 GHashTable
*devargs
;
1593 struct sr_dev_inst
*sdi
;
1597 devices
= device_scan();
1599 g_critical("No devices found.");
1602 if (g_slist_length(devices
) > 1) {
1603 g_critical("sigrok-cli only supports one device for capturing.");
1606 sdi
= devices
->data
;
1609 sr_session_datafeed_callback_add(datafeed_in
, NULL
);
1611 if (sr_session_dev_add(sdi
) != SR_OK
) {
1612 g_critical("Failed to use device.");
1613 sr_session_destroy();
1618 if ((devargs
= parse_generic_arg(opt_dev
, 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_list_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
);