2 * This file is part of the sigrok project.
4 * Copyright (C) 2010 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/>.
20 #include <sigrokdecode.h> /* First, so we avoid a _POSIX_C_SOURCE warning. */
28 #include <sys/types.h>
32 #include <glib/gstdio.h>
34 #include "sigrok-cli.h"
37 #define DEFAULT_OUTPUT_FORMAT "bits:width=64"
39 extern struct sr_hwcap_option sr_hwcap_options
[];
41 static gboolean debug
= 0;
42 static uint64_t limit_samples
= 0;
43 static struct sr_output_format
*output_format
= NULL
;
44 static int default_output_format
= FALSE
;
45 static char *output_format_param
= NULL
;
46 static char *input_format_param
= NULL
;
47 static GData
*pd_ann_visible
= NULL
;
49 static gboolean opt_version
= FALSE
;
50 static gint opt_loglevel
= SR_LOG_WARN
; /* Show errors+warnings per default. */
51 static gboolean opt_list_devices
= FALSE
;
52 static gboolean opt_wait_trigger
= FALSE
;
53 static gchar
*opt_input_file
= NULL
;
54 static gchar
*opt_output_file
= NULL
;
55 static gchar
*opt_device
= NULL
;
56 static gchar
*opt_probes
= NULL
;
57 static gchar
*opt_triggers
= NULL
;
58 static gchar
*opt_pds
= NULL
;
59 static gchar
*opt_pd_stack
= NULL
;
60 static gchar
*opt_input_format
= NULL
;
61 static gchar
*opt_format
= NULL
;
62 static gchar
*opt_time
= NULL
;
63 static gchar
*opt_samples
= NULL
;
64 static gchar
*opt_continuous
= NULL
;
66 static GOptionEntry optargs
[] = {
67 {"version", 'V', 0, G_OPTION_ARG_NONE
, &opt_version
, "Show version and support list", NULL
},
68 {"loglevel", 'l', 0, G_OPTION_ARG_INT
, &opt_loglevel
, "Select libsigrok loglevel", NULL
},
69 {"list-devices", 'D', 0, G_OPTION_ARG_NONE
, &opt_list_devices
, "List devices", NULL
},
70 {"input-file", 'i', 0, G_OPTION_ARG_FILENAME
, &opt_input_file
, "Load input from file", NULL
},
71 {"output-file", 'o', 0, G_OPTION_ARG_FILENAME
, &opt_output_file
, "Save output to file", NULL
},
72 {"device", 'd', 0, G_OPTION_ARG_STRING
, &opt_device
, "Use device ID", NULL
},
73 {"probes", 'p', 0, G_OPTION_ARG_STRING
, &opt_probes
, "Probes to use", NULL
},
74 {"triggers", 't', 0, G_OPTION_ARG_STRING
, &opt_triggers
, "Trigger configuration", NULL
},
75 {"wait-trigger", 'w', 0, G_OPTION_ARG_NONE
, &opt_wait_trigger
, "Wait for trigger", NULL
},
76 {"protocol-decoders", 'a', 0, G_OPTION_ARG_STRING
, &opt_pds
, "Protocol decoder sequence", NULL
},
77 {"protocol-decoder-stack", 's', 0, G_OPTION_ARG_STRING
, &opt_pd_stack
, "Protocol decoder stack", NULL
},
78 {"input-format", 'I', 0, G_OPTION_ARG_STRING
, &opt_input_format
, "Input format", NULL
},
79 {"format", 'f', 0, G_OPTION_ARG_STRING
, &opt_format
, "Output format", NULL
},
80 {"time", 0, 0, G_OPTION_ARG_STRING
, &opt_time
, "How long to sample (ms)", NULL
},
81 {"samples", 0, 0, G_OPTION_ARG_STRING
, &opt_samples
, "Number of samples to acquire", NULL
},
82 {"continuous", 0, 0, G_OPTION_ARG_NONE
, &opt_continuous
, "Sample continuously", NULL
},
83 {NULL
, 0, 0, 0, NULL
, NULL
, NULL
}
86 static void show_version(void)
88 GSList
*plugins
, *p
, *l
;
89 struct sr_device_plugin
*plugin
;
90 struct sr_input_format
**inputs
;
91 struct sr_output_format
**outputs
;
92 struct srd_decoder
*dec
;
95 printf("sigrok-cli %s\n\n", VERSION
);
96 printf("Supported hardware drivers:\n");
97 plugins
= sr_list_hwplugins();
98 for (p
= plugins
; p
; p
= p
->next
) {
100 printf(" %-20s %s\n", plugin
->name
, plugin
->longname
);
104 printf("Supported input formats:\n");
105 inputs
= sr_input_list();
106 for (i
= 0; inputs
[i
]; i
++)
107 printf(" %-20s %s\n", inputs
[i
]->id
, inputs
[i
]->description
);
110 printf("Supported output formats:\n");
111 outputs
= sr_output_list();
112 for (i
= 0; outputs
[i
]; i
++)
113 printf(" %-20s %s\n", outputs
[i
]->id
, outputs
[i
]->description
);
116 /* TODO: Error handling. */
119 printf("Supported protocol decoders:\n");
120 for (l
= srd_list_decoders(); l
; l
= l
->next
) {
122 printf(" %-20s %s\n", dec
->id
, dec
->longname
);
129 static void print_device_line(struct sr_device
*device
)
131 struct sr_device_instance
*sdi
;
133 sdi
= device
->plugin
->get_device_info(device
->plugin_index
, SR_DI_INSTANCE
);
135 if (sdi
->vendor
&& sdi
->vendor
[0])
136 printf("%s ", sdi
->vendor
);
137 if (sdi
->model
&& sdi
->model
[0])
138 printf("%s ", sdi
->model
);
139 if (sdi
->version
&& sdi
->version
[0])
140 printf("%s ", sdi
->version
);
142 printf("with %d probes", g_slist_length(device
->probes
));
146 static void show_device_list(void)
148 struct sr_device
*device
, *demo_device
;
153 devices
= sr_device_list();
155 if (g_slist_length(devices
) == 0)
158 printf("The following devices were found:\nID Device\n");
160 for (l
= devices
; l
; l
= l
->next
) {
162 if (strstr(device
->plugin
->name
, "demo")) {
163 demo_device
= device
;
166 printf("%-3d ", devcnt
++);
167 print_device_line(device
);
171 print_device_line(demo_device
);
175 static void show_device_detail(void)
177 struct sr_device
*device
;
178 struct sr_hwcap_option
*hwo
;
179 struct sr_samplerates
*samplerates
;
180 int cap
, *capabilities
, i
;
181 char *s
, *title
, *charopts
, **stropts
;
183 device
= parse_devicestring(opt_device
);
185 printf("No such device. Use -D to list all devices.\n");
189 print_device_line(device
);
191 if ((charopts
= (char *)device
->plugin
->get_device_info(
192 device
->plugin_index
, SR_DI_TRIGGER_TYPES
))) {
193 printf("Supported triggers: ");
195 printf("%c ", *charopts
);
201 title
= "Supported options:\n";
202 capabilities
= device
->plugin
->get_capabilities();
203 for (cap
= 0; capabilities
[cap
]; cap
++) {
204 if (!(hwo
= sr_find_hwcap_option(capabilities
[cap
])))
212 if (hwo
->capability
== SR_HWCAP_PATTERN_MODE
) {
213 printf(" %s", hwo
->shortname
);
214 stropts
= (char **)device
->plugin
->get_device_info(
215 device
->plugin_index
, SR_DI_PATTERNMODES
);
220 printf(" - supported modes:\n");
221 for (i
= 0; stropts
[i
]; i
++)
222 printf(" %s\n", stropts
[i
]);
223 } else if (hwo
->capability
== SR_HWCAP_SAMPLERATE
) {
224 printf(" %s", hwo
->shortname
);
225 /* Supported samplerates */
226 samplerates
= device
->plugin
->get_device_info(
227 device
->plugin_index
, SR_DI_SAMPLERATES
);
233 if (samplerates
->step
) {
235 if (!(s
= sr_samplerate_string(samplerates
->low
)))
240 if (!(s
= sr_samplerate_string(samplerates
->high
)))
245 if (!(s
= sr_samplerate_string(samplerates
->step
)))
247 printf(" in steps of %s)\n", s
);
250 printf(" - supported samplerates:\n");
251 for (i
= 0; samplerates
->list
[i
]; i
++) {
252 printf(" %7s\n", sr_samplerate_string(samplerates
->list
[i
]));
256 printf(" %s\n", hwo
->shortname
);
261 static void show_pd_detail(void)
264 struct srd_decoder
*dec
;
265 char **pdtokens
, **pdtok
, **ann
, *doc
;
267 pdtokens
= g_strsplit(opt_pds
, ",", -1);
268 for (pdtok
= pdtokens
; *pdtok
; pdtok
++) {
269 if (!(dec
= srd_get_decoder_by_id(*pdtok
))) {
270 printf("Protocol decoder %s not found.", *pdtok
);
273 printf("ID: %s\nName: %s\nLong name: %s\nDescription: %s\n",
274 dec
->id
, dec
->name
, dec
->longname
, dec
->desc
);
275 printf("License: %s\n", dec
->license
);
276 if (dec
->annotations
) {
277 printf("Annotations:\n");
278 for (l
= dec
->annotations
; l
; l
= l
->next
) {
280 printf("- %s\n %s\n", ann
[0], ann
[1]);
283 if ((doc
= srd_decoder_doc(dec
))) {
284 printf("Documentation:\n%s\n", doc
[0] == '\n' ? doc
+1 : doc
);
289 g_strfreev(pdtokens
);
293 static void datafeed_in(struct sr_device
*device
, struct sr_datafeed_packet
*packet
)
295 static struct sr_output
*o
= NULL
;
296 static int probelist
[SR_MAX_NUM_PROBES
] = { 0 };
297 static uint64_t received_samples
= 0;
298 static int unitsize
= 0;
299 static int triggered
= 0;
300 static FILE *outfile
= NULL
;
301 struct sr_probe
*probe
;
302 struct sr_datafeed_header
*header
;
303 struct sr_datafeed_logic
*logic
;
304 int num_enabled_probes
, sample_size
, ret
, i
;
305 uint64_t output_len
, filter_out_len
;
306 char *output_buf
, *filter_out
;
308 /* If the first packet to come in isn't a header, don't even try. */
309 if (packet
->type
!= SR_DF_HEADER
&& o
== NULL
)
313 switch (packet
->type
) {
315 g_message("cli: Received SR_DF_HEADER");
316 /* Initialize the output module. */
317 if (!(o
= malloc(sizeof(struct sr_output
)))) {
318 printf("Output module malloc failed.\n");
321 o
->format
= output_format
;
323 o
->param
= output_format_param
;
324 if (o
->format
->init
) {
325 if (o
->format
->init(o
) != SR_OK
) {
326 printf("Output format initialization failed.\n");
331 header
= packet
->payload
;
332 num_enabled_probes
= 0;
333 for (i
= 0; i
< header
->num_logic_probes
; i
++) {
334 probe
= g_slist_nth_data(device
->probes
, i
);
336 probelist
[num_enabled_probes
++] = probe
->index
;
338 /* How many bytes we need to store num_enabled_probes bits */
339 unitsize
= (num_enabled_probes
+ 7) / 8;
342 if (opt_output_file
) {
343 if (default_output_format
) {
344 /* output file is in session format, which means we'll
345 * dump everything in the datastore as it comes in,
346 * and save from there after the session. */
348 ret
= sr_datastore_new(unitsize
, &(device
->datastore
));
350 printf("Failed to create datastore.\n");
354 /* saving to a file in whatever format was set
355 * with --format, so all we need is a filehandle */
356 outfile
= g_fopen(opt_output_file
, "wb");
360 srd_session_start(num_enabled_probes
, unitsize
,
364 g_message("cli: Received SR_DF_END");
366 g_message("cli: double end!");
369 if (o
->format
->event
) {
370 o
->format
->event(o
, SR_DF_END
, &output_buf
, &output_len
);
373 fwrite(output_buf
, 1, output_len
, outfile
);
378 if (limit_samples
&& received_samples
< limit_samples
)
379 printf("Device only sent %" PRIu64
" samples.\n",
382 printf("Device stopped after %" PRIu64
" samples.\n",
385 if (outfile
&& outfile
!= stdout
)
391 g_message("cli: received SR_DF_TRIGGER at %"PRIu64
" ms",
392 packet
->timeoffset
/ 1000000);
393 if (o
->format
->event
)
394 o
->format
->event(o
, SR_DF_TRIGGER
, &output_buf
,
399 logic
= packet
->payload
;
400 sample_size
= logic
->unitsize
;
401 g_message("cli: received SR_DF_LOGIC at %f ms duration %f ms, %"PRIu64
" bytes",
402 packet
->timeoffset
/ 1000000.0, packet
->duration
/ 1000000.0,
409 /* not supporting anything but SR_DF_LOGIC for now */
411 if (sample_size
== -1 || logic
->length
== 0)
414 /* Don't store any samples until triggered. */
415 if (opt_wait_trigger
&& !triggered
)
418 if (limit_samples
&& received_samples
>= limit_samples
)
421 /* TODO: filters only support SR_DF_LOGIC */
422 ret
= sr_filter_probes(sample_size
, unitsize
, probelist
,
423 logic
->data
, logic
->length
,
424 &filter_out
, &filter_out_len
);
428 /* what comes out of the filter is guaranteed to be packed into the
429 * minimum size needed to support the number of samples at this sample
430 * size. however, the driver may have submitted too much -- cut off
431 * the buffer of the last packet according to the sample limit.
433 if (limit_samples
&& (received_samples
+ logic
->length
/ sample_size
>
434 limit_samples
* sample_size
))
435 filter_out_len
= limit_samples
* sample_size
- received_samples
;
437 if (device
->datastore
)
438 sr_datastore_put(device
->datastore
, filter_out
,
439 filter_out_len
, sample_size
, probelist
);
441 if (opt_output_file
&& default_output_format
)
442 /* saving to a session file, don't need to do anything else
443 * to this data for now. */
447 if (srd_session_feed(packet
->timeoffset
, packet
->duration
,
448 (uint8_t*)filter_out
, filter_out_len
) != SRD_OK
)
452 if (o
->format
->data
&& packet
->type
== o
->format
->df_type
)
453 o
->format
->data(o
, filter_out
, filter_out_len
, &output_buf
, &output_len
);
455 fwrite(output_buf
, 1, output_len
, outfile
);
462 received_samples
+= logic
->length
/ sample_size
;
466 /* Register the given PDs for this session.
467 * Accepts a string of the form: "spi:sck=3:sdata=4,spi:sck=3:sdata=5"
468 * That will instantiate two SPI decoders on the clock but different data
471 static int register_pds(struct sr_device
*device
, const char *pdstring
)
474 char **pdtokens
, **pdtok
;
476 /* Avoid compiler warnings. */
479 g_datalist_init(&pd_ann_visible
);
480 pdtokens
= g_strsplit(pdstring
, ",", -1);
482 /* anything, but not NULL */
483 dummy
= register_pds
;
484 for (pdtok
= pdtokens
; *pdtok
; pdtok
++) {
485 struct srd_decoder_instance
*di
;
487 /* Configure probes from command line */
488 char **optokens
, **optok
;
489 optokens
= g_strsplit(*pdtok
, ":", -1);
490 di
= srd_instance_new(optokens
[0], NULL
);
492 fprintf(stderr
, "Failed to instantiate PD: %s\n",
494 g_strfreev(optokens
);
495 g_strfreev(pdtokens
);
498 g_datalist_set_data(&pd_ann_visible
, optokens
[0], dummy
);
499 for (optok
= optokens
+1; *optok
; optok
++) {
500 char probe
[strlen(*optok
)];
502 if (sscanf(*optok
, "%[^=]=%d", probe
, &num
) == 2) {
503 printf("Setting probe '%s' to %d\n", probe
, num
);
504 srd_instance_set_probe(di
, probe
, num
);
506 fprintf(stderr
, "Error: Couldn't parse decoder "
507 "options correctly! Aborting.\n");
508 /* FIXME: Better error handling. */
512 g_strfreev(optokens
);
514 /* TODO: Handle errors. */
517 g_strfreev(pdtokens
);
522 void show_pd_annotation(struct srd_proto_data
*pdata
)
527 annotations
= pdata
->data
;
528 if (pdata
->ann_format
!= 0) {
529 /* CLI only shows the default annotation format */
533 if (!g_datalist_get_data(&pd_ann_visible
, pdata
->pdo
->proto_id
)) {
534 /* not in the list of PDs whose annotations we're showing */
538 printf("%s: ", pdata
->pdo
->proto_id
);
539 for (i
= 0; annotations
[i
]; i
++)
540 printf("\"%s\" ", annotations
[i
]);
545 static int select_probes(struct sr_device
*device
)
547 struct sr_probe
*probe
;
555 * This only works because a device by default initializes
556 * and enables all its probes.
558 max_probes
= g_slist_length(device
->probes
);
559 probelist
= parse_probestring(max_probes
, opt_probes
);
564 for (i
= 0; i
< max_probes
; i
++) {
566 sr_device_probe_name(device
, i
+ 1, probelist
[i
]);
567 g_free(probelist
[i
]);
569 probe
= sr_device_probe_find(device
, i
+ 1);
570 probe
->enabled
= FALSE
;
579 * Return the input file format which the CLI tool should use.
581 * If the user specified -I / --input-format, use that one. Otherwise, try to
582 * autodetect the format as good as possible. Failing that, return NULL.
584 * @param filename The filename of the input file. Must not be NULL.
585 * @param opt The -I / --input-file option the user specified (or NULL).
587 * @return A pointer to the 'struct sr_input_format' that should be used,
588 * or NULL if no input format was selected or auto-detected.
590 static struct sr_input_format
*determine_input_file_format(
591 const char *filename
, const char *opt
)
594 struct sr_input_format
**inputs
;
596 /* If there are no input formats, return NULL right away. */
597 inputs
= sr_input_list();
599 fprintf(stderr
, "cli: %s: no supported input formats "
600 "available", __func__
);
604 /* If the user specified -I / --input-format, use that one. */
606 for (i
= 0; inputs
[i
]; i
++) {
607 if (strcasecmp(inputs
[i
]->id
, opt_input_format
))
609 printf("Using user-specified input file format"
610 " '%s'.\n", inputs
[i
]->id
);
614 /* The user specified an unknown input format, return NULL. */
615 fprintf(stderr
, "Error: Specified input file format '%s' is "
616 "unknown.\n", opt_input_format
);
620 /* Otherwise, try to find an input module that can handle this file. */
621 for (i
= 0; inputs
[i
]; i
++) {
622 if (inputs
[i
]->format_match(filename
))
626 /* Return NULL if no input module wanted to touch this. */
628 fprintf(stderr
, "Error: No matching input module found.\n");
632 printf("Using input file format '%s'.\n", inputs
[i
]->id
);
636 static void load_input_file_format(void)
640 struct sr_input_format
*input_format
;
642 input_format
= determine_input_file_format(opt_input_file
,
645 fprintf(stderr
, "Error: Couldn't detect input file format.\n");
649 if (stat(opt_input_file
, &st
) == -1) {
650 printf("Failed to load %s: %s\n", opt_input_file
,
655 /* Initialize the input module. */
656 if (!(in
= malloc(sizeof(struct sr_input
)))) {
657 printf("Failed to allocate input module.\n");
660 in
->format
= input_format
;
661 in
->param
= input_format_param
;
662 if (in
->format
->init
) {
663 if (in
->format
->init(in
) != SR_OK
) {
664 printf("Input format init failed.\n");
669 if (select_probes(in
->vdevice
) > 0)
673 sr_session_datafeed_callback_add(datafeed_in
);
674 if (sr_session_device_add(in
->vdevice
) != SR_OK
) {
675 printf("Failed to use device.\n");
676 sr_session_destroy();
680 input_format
->loadfile(in
, opt_input_file
);
681 if (opt_output_file
&& default_output_format
) {
682 if (sr_session_save(opt_output_file
) != SR_OK
)
683 printf("Failed to save session.\n");
685 sr_session_destroy();
689 static void load_input_file(void)
692 if (sr_session_load(opt_input_file
) == SR_OK
) {
693 /* sigrok session file */
694 sr_session_datafeed_callback_add(datafeed_in
);
700 /* fall back on input modules */
701 load_input_file_format();
706 int num_real_devices(void)
708 struct sr_device
*device
;
713 devices
= sr_device_list();
714 for (l
= devices
; l
; l
= l
->next
) {
716 if (!strstr(device
->plugin
->name
, "demo"))
723 static int set_device_options(struct sr_device
*device
, GHashTable
*args
)
732 g_hash_table_iter_init(&iter
, args
);
733 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
735 for (i
= 0; sr_hwcap_options
[i
].capability
; i
++) {
736 if (strcmp(sr_hwcap_options
[i
].shortname
, key
))
738 if ((value
== NULL
) &&
739 (sr_hwcap_options
[i
].type
!= SR_T_BOOL
)) {
740 printf("Option '%s' needs a value.\n", (char *)key
);
744 switch (sr_hwcap_options
[i
].type
) {
746 ret
= sr_parse_sizestring(value
, &tmp_u64
);
749 ret
= device
->plugin
-> set_configuration(device
-> plugin_index
,
750 sr_hwcap_options
[i
]. capability
, &tmp_u64
);
753 ret
= device
->plugin
-> set_configuration(device
-> plugin_index
,
754 sr_hwcap_options
[i
]. capability
, value
);
760 tmp_bool
= sr_parse_boolstring(value
);
761 ret
= device
->plugin
-> set_configuration(device
-> plugin_index
,
762 sr_hwcap_options
[i
]. capability
,
763 GINT_TO_POINTER(tmp_bool
));
770 printf("Failed to set device option '%s'.\n", (char *)key
);
777 printf("Unknown device option '%s'.\n", (char *) key
);
785 static void run_session(void)
787 struct sr_device
*device
;
789 int num_devices
, max_probes
, *capabilities
, i
;
790 uint64_t tmp_u64
, time_msec
;
791 char **probelist
, *devspec
;
795 devargs
= parse_generic_arg(opt_device
);
796 devspec
= g_hash_table_lookup(devargs
, "sigrok_key");
797 device
= parse_devicestring(devspec
);
799 g_warning("Device not found.");
802 g_hash_table_remove(devargs
, "sigrok_key");
804 num_devices
= num_real_devices();
805 if (num_devices
== 1) {
806 /* No device specified, but there is only one. */
808 device
= parse_devicestring("0");
809 } else if (num_devices
== 0) {
810 printf("No devices found.\n");
813 printf("%d devices found, please select one.\n", num_devices
);
819 sr_session_datafeed_callback_add(datafeed_in
);
821 if (sr_session_device_add(device
) != SR_OK
) {
822 printf("Failed to use device.\n");
823 sr_session_destroy();
828 if (set_device_options(device
, devargs
) != SR_OK
) {
829 sr_session_destroy();
832 g_hash_table_destroy(devargs
);
835 if (select_probes(device
) != SR_OK
)
838 if (opt_continuous
) {
839 capabilities
= device
->plugin
->get_capabilities();
840 if (!sr_find_hwcap(capabilities
, SR_HWCAP_CONTINUOUS
)) {
841 printf("This device does not support continuous sampling.");
842 sr_session_destroy();
848 probelist
= sr_parse_triggerstring(device
, opt_triggers
);
850 sr_session_destroy();
854 max_probes
= g_slist_length(device
->probes
);
855 for (i
= 0; i
< max_probes
; i
++) {
857 sr_device_trigger_set(device
, i
+ 1, probelist
[i
]);
858 g_free(probelist
[i
]);
865 time_msec
= sr_parse_timestring(opt_time
);
866 if (time_msec
== 0) {
867 printf("Invalid time '%s'\n", opt_time
);
868 sr_session_destroy();
872 capabilities
= device
->plugin
->get_capabilities();
873 if (sr_find_hwcap(capabilities
, SR_HWCAP_LIMIT_MSEC
)) {
874 if (device
->plugin
->set_configuration(device
->plugin_index
,
875 SR_HWCAP_LIMIT_MSEC
, &time_msec
) != SR_OK
) {
876 printf("Failed to configure time limit.\n");
877 sr_session_destroy();
882 /* time limit set, but device doesn't support this...
883 * convert to samples based on the samplerate.
886 if (sr_device_has_hwcap(device
, SR_HWCAP_SAMPLERATE
)) {
887 tmp_u64
= *((uint64_t *) device
->plugin
->get_device_info(
888 device
->plugin_index
, SR_DI_CUR_SAMPLERATE
));
889 limit_samples
= tmp_u64
* time_msec
/ (uint64_t) 1000;
891 if (limit_samples
== 0) {
892 printf("Not enough time at this samplerate.\n");
893 sr_session_destroy();
897 if (device
->plugin
->set_configuration(device
->plugin_index
,
898 SR_HWCAP_LIMIT_SAMPLES
, &limit_samples
) != SR_OK
) {
899 printf("Failed to configure time-based sample limit.\n");
900 sr_session_destroy();
907 if ((sr_parse_sizestring(opt_samples
, &limit_samples
) != SR_OK
)
908 || (device
->plugin
->set_configuration(device
->plugin_index
,
909 SR_HWCAP_LIMIT_SAMPLES
, &limit_samples
) != SR_OK
)) {
910 printf("Failed to configure sample limit.\n");
911 sr_session_destroy();
916 if (device
->plugin
->set_configuration(device
->plugin_index
,
917 SR_HWCAP_PROBECONFIG
, (char *)device
->probes
) != SR_OK
) {
918 printf("Failed to configure probes.\n");
919 sr_session_destroy();
923 if (sr_session_start() != SR_OK
) {
924 printf("Failed to start session.\n");
925 sr_session_destroy();
937 if (opt_output_file
&& default_output_format
) {
938 if (sr_session_save(opt_output_file
) != SR_OK
)
939 printf("Failed to save session.\n");
941 sr_session_destroy();
945 static void logger(const gchar
*log_domain
, GLogLevelFlags log_level
,
946 const gchar
*message
, gpointer user_data
)
948 /* Avoid compiler warnings. */
953 * All messages, warnings, errors etc. go to stderr (not stdout) in
954 * order to not mess up the CLI tool data output, e.g. VCD output.
956 if (log_level
& (G_LOG_LEVEL_ERROR
| G_LOG_LEVEL_WARNING
)) {
957 fprintf(stderr
, "%s\n", message
);
960 if ((log_level
& G_LOG_LEVEL_MESSAGE
&& debug
== 1)
962 printf("%s\n", message
);
968 int main(int argc
, char **argv
)
970 GOptionContext
*context
;
975 struct sr_output_format
**outputs
;
976 struct srd_decoder_instance
*di_from
, *di_to
;
978 char *fmtspec
, **pds
;
980 g_log_set_default_handler(logger
, NULL
);
981 if (getenv("SIGROK_DEBUG"))
982 debug
= strtol(getenv("SIGROK_DEBUG"), NULL
, 10);
985 context
= g_option_context_new(NULL
);
986 g_option_context_add_main_entries(context
, optargs
, NULL
);
988 if (!g_option_context_parse(context
, &argc
, &argv
, &error
)) {
989 g_warning("%s", error
->message
);
993 /* Set the loglevel (amount of messages to output) for libsigrok. */
994 if (sr_set_loglevel(opt_loglevel
) != SR_OK
) {
995 fprintf(stderr
, "cli: %s: sr_set_loglevel(%d) failed\n",
996 __func__
, opt_loglevel
);
1000 /* Set the loglevel (amount of messages to output) for libsigrokdecode. */
1001 if (srd_set_loglevel(opt_loglevel
) != SRD_OK
) {
1002 fprintf(stderr
, "cli: %s: srd_set_loglevel(%d) failed\n",
1003 __func__
, opt_loglevel
);
1007 if (sr_init() != SR_OK
)
1011 if (srd_init() != SRD_OK
) {
1012 printf("Failed to initialize sigrokdecode\n");
1015 if (register_pds(NULL
, opt_pds
) != 0) {
1016 printf("Failed to register protocol decoders\n");
1019 if (srd_register_callback(SRD_OUTPUT_ANN
,
1020 show_pd_annotation
) != SRD_OK
) {
1021 printf("Failed to register protocol decoder callback\n");
1027 pds
= g_strsplit(opt_pd_stack
, ":", 0);
1028 if (g_strv_length(pds
) < 2) {
1029 printf("Specify at least two protocol decoders to stack.\n");
1033 if (!(di_from
= srd_instance_find(pds
[0]))) {
1034 printf("Cannot stack protocol decoder '%s': instance not found.\n", pds
[0]);
1037 for (i
= 1; pds
[i
]; i
++) {
1038 if (!(di_to
= srd_instance_find(pds
[i
]))) {
1039 printf("Cannot stack protocol decoder '%s': instance not found.\n", pds
[i
]);
1042 if ((ret
= srd_instance_stack(di_from
, di_to
) != SRD_OK
))
1045 /* Don't show annotation from this PD. Only the last PD in
1046 * the stack will be left on the annotation list.
1048 g_datalist_remove_data(&pd_ann_visible
, di_from
->instance_id
);
1056 opt_format
= DEFAULT_OUTPUT_FORMAT
;
1057 /* we'll need to remember this so when saving to a file
1058 * later, sigrok session format will be used.
1060 default_output_format
= TRUE
;
1062 fmtargs
= parse_generic_arg(opt_format
);
1063 fmtspec
= g_hash_table_lookup(fmtargs
, "sigrok_key");
1065 printf("Invalid output format.\n");
1068 outputs
= sr_output_list();
1069 for (i
= 0; outputs
[i
]; i
++) {
1070 if (strcmp(outputs
[i
]->id
, fmtspec
))
1072 g_hash_table_remove(fmtargs
, "sigrok_key");
1073 output_format
= outputs
[i
];
1074 g_hash_table_iter_init(&iter
, fmtargs
);
1075 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
1076 /* only supporting one parameter per output module
1077 * for now, and only its value */
1078 output_format_param
= g_strdup(value
);
1083 if (!output_format
) {
1084 printf("invalid output format %s\n", opt_format
);
1090 else if (opt_list_devices
)
1092 else if (opt_input_file
)
1094 else if (opt_samples
|| opt_time
|| opt_continuous
)
1096 else if (opt_device
)
1097 show_device_detail();
1101 printf("%s", g_option_context_get_help(context
, TRUE
, NULL
));
1106 g_option_context_free(context
);
1107 g_hash_table_destroy(fmtargs
);