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/>.
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 uint64_t limit_samples = 0;
42 static struct sr_output_format *output_format = NULL;
43 static int default_output_format = FALSE;
44 static char *output_format_param = NULL;
45 static char *input_format_param = NULL;
46 static GData *pd_ann_visible = NULL;
48 static gboolean opt_version = FALSE;
49 static gint opt_loglevel = SR_LOG_WARN; /* Show errors+warnings per default. */
50 static gboolean opt_list_devs = FALSE;
51 static gboolean opt_wait_trigger = FALSE;
52 static gchar *opt_input_file = NULL;
53 static gchar *opt_output_file = NULL;
54 static gchar *opt_dev = NULL;
55 static gchar *opt_probes = NULL;
56 static gchar *opt_triggers = NULL;
57 static gchar *opt_pds = NULL;
58 static gchar *opt_pd_stack = NULL;
59 static gchar *opt_input_format = NULL;
60 static gchar *opt_output_format = NULL;
61 static gchar *opt_time = NULL;
62 static gchar *opt_samples = NULL;
63 static gchar *opt_continuous = NULL;
65 static GOptionEntry optargs[] = {
66 {"version", 'V', 0, G_OPTION_ARG_NONE, &opt_version, "Show version and support list", NULL},
67 {"loglevel", 'l', 0, G_OPTION_ARG_INT, &opt_loglevel, "Select libsigrok/libsigrokdecode loglevel", NULL},
68 {"list-devices", 'D', 0, G_OPTION_ARG_NONE, &opt_list_devs, "Scan for devices", NULL},
69 {"device", 'd', 0, G_OPTION_ARG_STRING, &opt_dev, "Use specified device", NULL},
70 {"input-file", 'i', 0, G_OPTION_ARG_FILENAME, &opt_input_file, "Load input from file", NULL},
71 {"input-format", 'I', 0, G_OPTION_ARG_STRING, &opt_input_format, "Input format", NULL},
72 {"output-file", 'o', 0, G_OPTION_ARG_FILENAME, &opt_output_file, "Save output to file", NULL},
73 {"output-format", 'O', 0, G_OPTION_ARG_STRING, &opt_output_format, "Output format", NULL},
74 {"probes", 'p', 0, G_OPTION_ARG_STRING, &opt_probes, "Probes to use", NULL},
75 {"triggers", 't', 0, G_OPTION_ARG_STRING, &opt_triggers, "Trigger configuration", NULL},
76 {"wait-trigger", 'w', 0, G_OPTION_ARG_NONE, &opt_wait_trigger, "Wait for trigger", NULL},
77 {"protocol-decoders", 'a', 0, G_OPTION_ARG_STRING, &opt_pds, "Protocol decoders to run", NULL},
78 {"protocol-decoder-stack", 's', 0, G_OPTION_ARG_STRING, &opt_pd_stack, "Protocol decoder stack", NULL},
79 {"time", 0, 0, G_OPTION_ARG_STRING, &opt_time, "How long to sample (ms)", NULL},
80 {"samples", 0, 0, G_OPTION_ARG_STRING, &opt_samples, "Number of samples to acquire", NULL},
81 {"continuous", 0, 0, G_OPTION_ARG_NONE, &opt_continuous, "Sample continuously", NULL},
82 {NULL, 0, 0, 0, NULL, NULL, NULL}
85 static void show_version(void)
88 struct sr_dev_driver **drivers;
89 struct sr_input_format **inputs;
90 struct sr_output_format **outputs;
91 struct srd_decoder *dec;
94 printf("sigrok-cli %s\n\n", VERSION);
96 printf("Using libsigrok %s (lib version %s).\n",
97 sr_package_version_string_get(), sr_lib_version_string_get());
98 printf("Using libsigrokdecode %s (lib version %s).\n\n",
99 srd_package_version_string_get(), srd_lib_version_string_get());
101 printf("Supported hardware drivers:\n");
102 drivers = sr_driver_list();
103 for (i = 0; drivers[i]; i++) {
104 printf(" %-20s %s\n", drivers[i]->name, drivers[i]->longname);
108 printf("Supported input formats:\n");
109 inputs = sr_input_list();
110 for (i = 0; inputs[i]; i++)
111 printf(" %-20s %s\n", inputs[i]->id, inputs[i]->description);
114 printf("Supported output formats:\n");
115 outputs = sr_output_list();
116 for (i = 0; outputs[i]; i++)
117 printf(" %-20s %s\n", outputs[i]->id, outputs[i]->description);
120 if (srd_init(NULL) == SRD_OK) {
121 printf("Supported protocol decoders:\n");
122 srd_decoder_load_all();
123 for (l = srd_decoder_list(); l; l = l->next) {
125 printf(" %-20s %s\n", dec->id, dec->longname);
126 /* Print protocol description upon "-l 3" or higher. */
127 if (opt_loglevel >= SR_LOG_INFO)
128 printf(" %-20s %s\n", "", dec->desc);
135 static void print_dev_line(const struct sr_dev *dev)
137 const struct sr_dev_inst *sdi;
139 sr_dev_info_get(dev, SR_DI_INST, (const void **)&sdi);
141 if (sdi->vendor && sdi->vendor[0])
142 printf("%s ", sdi->vendor);
143 if (sdi->model && sdi->model[0])
144 printf("%s ", sdi->model);
145 if (sdi->version && sdi->version[0])
146 printf("%s ", sdi->version);
148 printf("with %d probes", g_slist_length(dev->probes));
152 static void show_dev_list(void)
154 struct sr_dev *dev, *demo_dev;
159 devs = sr_dev_list();
161 if (g_slist_length(devs) == 0)
164 printf("The following devices were found:\nID Device\n");
166 for (l = devs; l; l = l->next) {
168 if (sr_dev_has_hwcap(dev, SR_HWCAP_DEMO_DEV)) {
172 printf("%-3d ", devcnt++);
177 print_dev_line(demo_dev);
181 static void show_dev_detail(void)
184 struct sr_hwcap_option *hwo;
185 const struct sr_samplerates *samplerates;
188 const char *charopts, **stropts;
190 dev = parse_devstring(opt_dev);
192 printf("No such device. Use -D to list all devices.\n");
198 if (sr_dev_info_get(dev, SR_DI_TRIGGER_TYPES,
199 (const void **)&charopts) == SR_OK) {
200 printf("Supported triggers: ");
202 printf("%c ", *charopts);
208 title = "Supported options:\n";
209 hwcaps = dev->driver->hwcap_get_all();
210 for (cap = 0; hwcaps[cap]; cap++) {
211 if (!(hwo = sr_hw_hwcap_get(hwcaps[cap])))
219 if (hwo->hwcap == SR_HWCAP_PATTERN_MODE) {
220 printf(" %s", hwo->shortname);
221 if (sr_dev_info_get(dev, SR_DI_PATTERNS,
222 (const void **)&stropts) == SR_OK) {
223 printf(" - supported patterns:\n");
224 for (i = 0; stropts[i]; i++)
225 printf(" %s\n", stropts[i]);
229 } else if (hwo->hwcap == SR_HWCAP_SAMPLERATE) {
230 printf(" %s", hwo->shortname);
231 /* Supported samplerates */
232 if (sr_dev_info_get(dev, SR_DI_SAMPLERATES,
233 (const void **)&samplerates) != SR_OK) {
238 if (samplerates->step) {
240 if (!(s = sr_samplerate_string(samplerates->low)))
245 if (!(s = sr_samplerate_string(samplerates->high)))
250 if (!(s = sr_samplerate_string(samplerates->step)))
252 printf(" in steps of %s)\n", s);
255 printf(" - supported samplerates:\n");
256 for (i = 0; samplerates->list[i]; i++) {
257 printf(" %s\n", sr_samplerate_string(samplerates->list[i]));
261 printf(" %s\n", hwo->shortname);
266 static void show_pd_detail(void)
269 struct srd_decoder *dec;
270 char **pdtokens, **pdtok, **ann, *doc;
273 pdtokens = g_strsplit(opt_pds, ",", -1);
274 for (pdtok = pdtokens; *pdtok; pdtok++) {
275 if (!(dec = srd_decoder_get_by_id(*pdtok))) {
276 printf("Protocol decoder %s not found.", *pdtok);
279 printf("ID: %s\nName: %s\nLong name: %s\nDescription: %s\n",
280 dec->id, dec->name, dec->longname, dec->desc);
281 printf("License: %s\n", dec->license);
282 printf("Annotations:\n");
283 if (dec->annotations) {
284 for (l = dec->annotations; l; l = l->next) {
286 printf("- %s\n %s\n", ann[0], ann[1]);
291 /* TODO: Print supported decoder options. */
292 printf("Required probes:\n");
294 for (l = dec->probes; l; l = l->next) {
296 printf("- %s (%s): %s\n",
297 p->name, p->id, p->desc);
302 printf("Optional probes:\n");
303 if (dec->opt_probes) {
304 for (l = dec->opt_probes; l; l = l->next) {
306 printf("- %s (%s): %s\n",
307 p->name, p->id, p->desc);
312 if ((doc = srd_decoder_doc_get(dec))) {
313 printf("Documentation:\n%s\n",
314 doc[0] == '\n' ? doc + 1 : doc);
319 g_strfreev(pdtokens);
322 static void datafeed_in(struct sr_dev *dev, struct sr_datafeed_packet *packet)
324 static struct sr_output *o = NULL;
325 static int probelist[SR_MAX_NUM_PROBES + 1] = { 0 };
326 static uint64_t received_samples = 0;
327 static int unitsize = 0;
328 static int triggered = 0;
329 static FILE *outfile = NULL;
330 struct sr_probe *probe;
331 struct sr_datafeed_header *header;
332 struct sr_datafeed_logic *logic;
333 int num_enabled_probes, sample_size, ret, i;
334 uint64_t output_len, filter_out_len;
335 uint8_t *output_buf, *filter_out;
337 /* If the first packet to come in isn't a header, don't even try. */
338 if (packet->type != SR_DF_HEADER && o == NULL)
342 switch (packet->type) {
344 g_debug("cli: Received SR_DF_HEADER");
345 /* Initialize the output module. */
346 if (!(o = g_try_malloc(sizeof(struct sr_output)))) {
347 g_critical("Output module malloc failed.");
350 o->format = output_format;
352 o->param = output_format_param;
353 if (o->format->init) {
354 if (o->format->init(o) != SR_OK) {
355 g_critical("Output format initialization failed.");
360 header = packet->payload;
361 num_enabled_probes = 0;
362 for (i = 0; i < header->num_logic_probes; i++) {
363 probe = g_slist_nth_data(dev->probes, i);
365 probelist[num_enabled_probes++] = probe->index;
367 /* How many bytes we need to store num_enabled_probes bits */
368 unitsize = (num_enabled_probes + 7) / 8;
371 if (opt_output_file) {
372 if (default_output_format) {
373 /* output file is in session format, which means we'll
374 * dump everything in the datastore as it comes in,
375 * and save from there after the session. */
377 ret = sr_datastore_new(unitsize, &(dev->datastore));
379 g_critical("Failed to create datastore.");
383 /* saving to a file in whatever format was set
384 * with -O, so all we need is a filehandle */
385 outfile = g_fopen(opt_output_file, "wb");
389 srd_session_start(num_enabled_probes, unitsize,
393 g_debug("cli: Received SR_DF_END");
395 g_debug("cli: double end!");
398 if (o->format->event) {
399 o->format->event(o, SR_DF_END, &output_buf, &output_len);
402 fwrite(output_buf, 1, output_len, outfile);
407 if (limit_samples && received_samples < limit_samples)
408 g_warning("Device only sent %" PRIu64 " samples.",
411 g_warning("Device stopped after %" PRIu64 " samples.",
414 if (outfile && outfile != stdout)
420 g_debug("cli: received SR_DF_TRIGGER");
421 if (o->format->event)
422 o->format->event(o, SR_DF_TRIGGER, &output_buf,
427 logic = packet->payload;
428 sample_size = logic->unitsize;
429 g_message("cli: received SR_DF_LOGIC, %"PRIu64" bytes", logic->length);
433 /* not supporting anything but SR_DF_LOGIC for now */
435 if (sample_size == -1 || logic->length == 0)
438 /* Don't store any samples until triggered. */
439 if (opt_wait_trigger && !triggered)
442 if (limit_samples && received_samples >= limit_samples)
445 /* TODO: filters only support SR_DF_LOGIC */
446 ret = sr_filter_probes(sample_size, unitsize, probelist,
447 logic->data, logic->length,
448 &filter_out, &filter_out_len);
452 /* what comes out of the filter is guaranteed to be packed into the
453 * minimum size needed to support the number of samples at this sample
454 * size. however, the driver may have submitted too much -- cut off
455 * the buffer of the last packet according to the sample limit.
457 if (limit_samples && (received_samples + logic->length / sample_size >
458 limit_samples * sample_size))
459 filter_out_len = limit_samples * sample_size - received_samples;
462 sr_datastore_put(dev->datastore, filter_out,
463 filter_out_len, sample_size, probelist);
465 if (opt_output_file && default_output_format)
466 /* saving to a session file, don't need to do anything else
467 * to this data for now. */
471 if (srd_session_send(received_samples, (uint8_t*)filter_out,
472 filter_out_len) != SRD_OK)
476 if (o->format->data && packet->type == o->format->df_type)
477 o->format->data(o, filter_out, filter_out_len, &output_buf, &output_len);
479 fwrite(output_buf, 1, output_len, outfile);
486 received_samples += logic->length / sample_size;
489 /* Register the given PDs for this session.
490 * Accepts a string of the form: "spi:sck=3:sdata=4,spi:sck=3:sdata=5"
491 * That will instantiate two SPI decoders on the clock but different data
494 static int register_pds(struct sr_dev *dev, const char *pdstring)
496 GHashTable *pd_opthash;
497 struct srd_decoder_inst *di;
498 char **pdtokens, **pdtok, *pd_name;
500 /* Avoid compiler warnings. */
503 g_datalist_init(&pd_ann_visible);
504 pdtokens = g_strsplit(pdstring, ",", -1);
508 for (pdtok = pdtokens; *pdtok; pdtok++) {
509 if (!(pd_opthash = parse_generic_arg(*pdtok))) {
510 g_critical("Invalid protocol decoder option '%s'.", *pdtok);
514 pd_name = g_strdup(g_hash_table_lookup(pd_opthash, "sigrok_key"));
515 g_hash_table_remove(pd_opthash, "sigrok_key");
516 if (srd_decoder_load(pd_name) != SRD_OK) {
517 g_critical("Failed to load protocol decoder %s.", pd_name);
520 if (!(di = srd_inst_new(pd_name, pd_opthash))) {
521 g_critical("Failed to instantiate protocol decoder %s.", pd_name);
524 g_datalist_set_data(&pd_ann_visible, di->inst_id, pd_name);
526 /* Any keys left in the options hash are probes, where the key
527 * is the probe name as specified in the decoder class, and the
528 * value is the probe number i.e. the order in which the PD's
529 * incoming samples are arranged. */
530 if (srd_inst_probe_set_all(di, pd_opthash) != SRD_OK)
532 g_hash_table_destroy(pd_opthash);
537 g_strfreev(pdtokens);
539 g_hash_table_destroy(pd_opthash);
546 void show_pd_annotation(struct srd_proto_data *pdata, void *cb_data)
551 /* 'cb_data' is not used in this specific callback. */
554 if (pdata->ann_format != 0) {
555 /* CLI only shows the default annotation format. */
559 if (!g_datalist_get_data(&pd_ann_visible, pdata->pdo->di->inst_id)) {
560 /* Not in the list of PDs whose annotations we're showing. */
564 annotations = pdata->data;
565 if (opt_loglevel > SR_LOG_WARN)
566 printf("%"PRIu64"-%"PRIu64" ", pdata->start_sample, pdata->end_sample);
567 printf("%s: ", pdata->pdo->proto_id);
568 for (i = 0; annotations[i]; i++)
569 printf("\"%s\" ", annotations[i]);
573 static int select_probes(struct sr_dev *dev)
575 struct sr_probe *probe;
583 * This only works because a device by default initializes
584 * and enables all its probes.
586 max_probes = g_slist_length(dev->probes);
587 probelist = parse_probestring(max_probes, opt_probes);
592 for (i = 0; i < max_probes; i++) {
594 sr_dev_probe_name_set(dev, i + 1, probelist[i]);
595 g_free(probelist[i]);
597 probe = sr_dev_probe_find(dev, i + 1);
598 probe->enabled = FALSE;
607 * Return the input file format which the CLI tool should use.
609 * If the user specified -I / --input-format, use that one. Otherwise, try to
610 * autodetect the format as good as possible. Failing that, return NULL.
612 * @param filename The filename of the input file. Must not be NULL.
613 * @param opt The -I / --input-file option the user specified (or NULL).
615 * @return A pointer to the 'struct sr_input_format' that should be used,
616 * or NULL if no input format was selected or auto-detected.
618 static struct sr_input_format *determine_input_file_format(
619 const char *filename, const char *opt)
622 struct sr_input_format **inputs;
624 /* If there are no input formats, return NULL right away. */
625 inputs = sr_input_list();
627 g_critical("No supported input formats available.");
631 /* If the user specified -I / --input-format, use that one. */
633 for (i = 0; inputs[i]; i++) {
634 if (strcasecmp(inputs[i]->id, opt_input_format))
636 g_debug("Using user-specified input file format '%s'.",
641 /* The user specified an unknown input format, return NULL. */
642 g_critical("Error: specified input file format '%s' is "
643 "unknown.", opt_input_format);
647 /* Otherwise, try to find an input module that can handle this file. */
648 for (i = 0; inputs[i]; i++) {
649 if (inputs[i]->format_match(filename))
653 /* Return NULL if no input module wanted to touch this. */
655 g_critical("Error: no matching input module found.");
662 static void load_input_file_format(void)
666 struct sr_input_format *input_format;
668 if (!(input_format = determine_input_file_format(opt_input_file,
670 /* The exact cause was already logged. */
673 if (stat(opt_input_file, &st) == -1) {
674 g_critical("Failed to load %s: %s", opt_input_file,
679 /* Initialize the input module. */
680 if (!(in = g_try_malloc(sizeof(struct sr_input)))) {
681 g_critical("Failed to allocate input module.");
684 in->format = input_format;
685 in->param = input_format_param;
686 if (in->format->init) {
687 if (in->format->init(in) != SR_OK) {
688 g_critical("Input format init failed.");
693 if (select_probes(in->vdev) > 0)
697 sr_session_datafeed_callback_add(datafeed_in);
698 if (sr_session_dev_add(in->vdev) != SR_OK) {
699 g_critical("Failed to use device.");
700 sr_session_destroy();
704 input_format->loadfile(in, opt_input_file);
705 if (opt_output_file && default_output_format) {
706 if (sr_session_save(opt_output_file) != SR_OK)
707 g_critical("Failed to save session.");
709 sr_session_destroy();
712 static void load_input_file(void)
715 if (sr_session_load(opt_input_file) == SR_OK) {
716 /* sigrok session file */
717 sr_session_datafeed_callback_add(datafeed_in);
723 /* fall back on input modules */
724 load_input_file_format();
728 int num_real_devs(void)
735 devs = sr_dev_list();
736 for (l = devs; l; l = l->next) {
738 if (!sr_dev_has_hwcap(dev, SR_HWCAP_DEMO_DEV))
745 static int set_dev_options(struct sr_dev *dev, GHashTable *args)
754 g_hash_table_iter_init(&iter, args);
755 while (g_hash_table_iter_next(&iter, &key, &value)) {
757 for (i = 0; sr_hwcap_options[i].hwcap; i++) {
758 if (strcmp(sr_hwcap_options[i].shortname, key))
760 if ((value == NULL) &&
761 (sr_hwcap_options[i].type != SR_T_BOOL)) {
762 g_critical("Option '%s' needs a value.", (char *)key);
766 switch (sr_hwcap_options[i].type) {
768 ret = sr_parse_sizestring(value, &tmp_u64);
771 ret = dev->driver->dev_config_set(dev->driver_index,
772 sr_hwcap_options[i].hwcap, &tmp_u64);
775 ret = dev->driver->dev_config_set(dev->driver_index,
776 sr_hwcap_options[i].hwcap, value);
782 tmp_bool = sr_parse_boolstring(value);
783 ret = dev->driver->dev_config_set(dev->driver_index,
784 sr_hwcap_options[i].hwcap,
785 GINT_TO_POINTER(tmp_bool));
792 g_critical("Failed to set device option '%s'.", (char *)key);
799 g_critical("Unknown device option '%s'.", (char *) key);
807 static void run_session(void)
811 int num_devs, max_probes, i;
813 char **probelist, *devspec;
817 devargs = parse_generic_arg(opt_dev);
818 devspec = g_hash_table_lookup(devargs, "sigrok_key");
819 dev = parse_devstring(devspec);
821 g_critical("Device not found.");
824 g_hash_table_remove(devargs, "sigrok_key");
826 num_devs = num_real_devs();
828 /* No device specified, but there is only one. */
830 dev = parse_devstring("0");
831 } else if (num_devs == 0) {
832 g_critical("No devices found.");
835 g_critical("%d devices found, please select one.", num_devs);
841 sr_session_datafeed_callback_add(datafeed_in);
843 if (sr_session_dev_add(dev) != SR_OK) {
844 g_critical("Failed to use device.");
845 sr_session_destroy();
850 if (set_dev_options(dev, devargs) != SR_OK) {
851 sr_session_destroy();
854 g_hash_table_destroy(devargs);
857 if (select_probes(dev) != SR_OK)
860 if (opt_continuous) {
861 if (!sr_driver_hwcap_exists(dev->driver, SR_HWCAP_CONTINUOUS)) {
862 g_critical("This device does not support continuous sampling.");
863 sr_session_destroy();
869 probelist = sr_parse_triggerstring(dev, opt_triggers);
871 sr_session_destroy();
875 max_probes = g_slist_length(dev->probes);
876 for (i = 0; i < max_probes; i++) {
878 sr_dev_trigger_set(dev, i + 1, probelist[i]);
879 g_free(probelist[i]);
886 time_msec = sr_parse_timestring(opt_time);
887 if (time_msec == 0) {
888 g_critical("Invalid time '%s'", opt_time);
889 sr_session_destroy();
893 if (sr_driver_hwcap_exists(dev->driver, SR_HWCAP_LIMIT_MSEC)) {
894 if (dev->driver->dev_config_set(dev->driver_index,
895 SR_HWCAP_LIMIT_MSEC, &time_msec) != SR_OK) {
896 g_critical("Failed to configure time limit.");
897 sr_session_destroy();
902 /* time limit set, but device doesn't support this...
903 * convert to samples based on the samplerate.
906 if (sr_dev_has_hwcap(dev, SR_HWCAP_SAMPLERATE)) {
907 const uint64_t *samplerate;
909 sr_dev_info_get(dev, SR_DI_CUR_SAMPLERATE,
910 (const void **)&samplerate);
911 limit_samples = (*samplerate) * time_msec / (uint64_t)1000;
913 if (limit_samples == 0) {
914 g_critical("Not enough time at this samplerate.");
915 sr_session_destroy();
919 if (dev->driver->dev_config_set(dev->driver_index,
920 SR_HWCAP_LIMIT_SAMPLES, &limit_samples) != SR_OK) {
921 g_critical("Failed to configure time-based sample limit.");
922 sr_session_destroy();
929 if ((sr_parse_sizestring(opt_samples, &limit_samples) != SR_OK)
930 || (dev->driver->dev_config_set(dev->driver_index,
931 SR_HWCAP_LIMIT_SAMPLES, &limit_samples) != SR_OK)) {
932 g_critical("Failed to configure sample limit.");
933 sr_session_destroy();
938 if (dev->driver->dev_config_set(dev->driver_index,
939 SR_HWCAP_PROBECONFIG, (char *)dev->probes) != SR_OK) {
940 g_critical("Failed to configure probes.");
941 sr_session_destroy();
945 if (sr_session_start() != SR_OK) {
946 g_critical("Failed to start session.");
947 sr_session_destroy();
959 if (opt_output_file && default_output_format) {
960 if (sr_session_save(opt_output_file) != SR_OK)
961 g_critical("Failed to save session.");
963 sr_session_destroy();
966 static void logger(const gchar *log_domain, GLogLevelFlags log_level,
967 const gchar *message, gpointer cb_data)
969 /* Avoid compiler warnings. */
974 * All messages, warnings, errors etc. go to stderr (not stdout) in
975 * order to not mess up the CLI tool data output, e.g. VCD output.
977 if (log_level & (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_WARNING)
978 || opt_loglevel > SR_LOG_WARN) {
979 fprintf(stderr, "%s\n", message);
984 int main(int argc, char **argv)
986 GOptionContext *context;
991 struct sr_output_format **outputs;
992 struct srd_decoder_inst *di_from, *di_to;
994 char *fmtspec, **pds;
996 g_log_set_default_handler(logger, NULL);
999 context = g_option_context_new(NULL);
1000 g_option_context_add_main_entries(context, optargs, NULL);
1002 if (!g_option_context_parse(context, &argc, &argv, &error)) {
1003 g_critical("%s", error->message);
1007 /* Set the loglevel (amount of messages to output) for libsigrok. */
1008 if (sr_log_loglevel_set(opt_loglevel) != SR_OK) {
1009 g_critical("sr_log_loglevel_set(%d) failed.", opt_loglevel);
1013 /* Set the loglevel (amount of messages to output) for libsigrokdecode. */
1014 if (srd_log_loglevel_set(opt_loglevel) != SRD_OK) {
1015 g_critical("srd_log_loglevel_set(%d) failed.", opt_loglevel);
1019 if (sr_init() != SR_OK)
1023 if (srd_init(NULL) != SRD_OK) {
1024 g_critical("Failed to initialize sigrokdecode.");
1027 if (register_pds(NULL, opt_pds) != 0) {
1028 g_critical("Failed to register protocol decoders.");
1031 if (srd_pd_output_callback_add(SRD_OUTPUT_ANN,
1032 show_pd_annotation, NULL) != SRD_OK) {
1033 g_critical("Failed to register protocol decoder callback.");
1037 pds = g_strsplit(opt_pds, ",", 0);
1038 if (g_strv_length(pds) > 1) {
1040 /* A stack setup was specified, use that. */
1042 pds = g_strsplit(opt_pd_stack, ",", 0);
1043 if (g_strv_length(pds) < 2) {
1045 g_critical("Specify at least two protocol decoders to stack.");
1050 if (!(di_from = srd_inst_find_by_id(pds[0]))) {
1051 g_critical("Cannot stack protocol decoder '%s': "
1052 "instance not found.", pds[0]);
1055 for (i = 1; pds[i]; i++) {
1056 if (!(di_to = srd_inst_find_by_id(pds[i]))) {
1057 g_critical("Cannot stack protocol decoder '%s': "
1058 "instance not found.", pds[i]);
1061 if ((ret = srd_inst_stack(di_from, di_to)) != SRD_OK)
1064 /* Don't show annotation from this PD. Only the last PD in
1065 * the stack will be left on the annotation list.
1067 g_datalist_remove_data(&pd_ann_visible, di_from->inst_id);
1075 if (!opt_output_format) {
1076 opt_output_format = DEFAULT_OUTPUT_FORMAT;
1077 /* we'll need to remember this so when saving to a file
1078 * later, sigrok session format will be used.
1080 default_output_format = TRUE;
1082 fmtargs = parse_generic_arg(opt_output_format);
1083 fmtspec = g_hash_table_lookup(fmtargs, "sigrok_key");
1085 g_critical("Invalid output format.");
1088 outputs = sr_output_list();
1089 for (i = 0; outputs[i]; i++) {
1090 if (strcmp(outputs[i]->id, fmtspec))
1092 g_hash_table_remove(fmtargs, "sigrok_key");
1093 output_format = outputs[i];
1094 g_hash_table_iter_init(&iter, fmtargs);
1095 while (g_hash_table_iter_next(&iter, &key, &value)) {
1096 /* only supporting one parameter per output module
1097 * for now, and only its value */
1098 output_format_param = g_strdup(value);
1103 if (!output_format) {
1104 g_critical("Invalid output format %s.", opt_output_format);
1110 else if (opt_list_devs)
1112 else if (opt_input_file)
1114 else if (opt_samples || opt_time || opt_continuous)
1121 printf("%s", g_option_context_get_help(context, TRUE, NULL));
1126 g_option_context_free(context);
1127 g_hash_table_destroy(fmtargs);