2 * This file is part of the sigrok-cli project.
4 * Copyright (C) 2013 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 <libsigrok/libsigrok.h>
24 #include <libsigrokdecode/libsigrokdecode.h> /* First, so we avoid a _POSIX_C_SOURCE warning. */
26 #include "sigrok-cli.h"
28 struct sr_context *sr_ctx = NULL;
30 struct srd_session *srd_sess = NULL;
33 static gboolean opt_version = FALSE;
34 gint opt_loglevel = SR_LOG_WARN; /* Show errors+warnings by default. */
35 static gboolean opt_scan_devs = FALSE;
36 gboolean opt_wait_trigger = FALSE;
37 gchar *opt_input_file = NULL;
38 gchar *opt_output_file = NULL;
39 gchar *opt_drv = NULL;
40 gchar *opt_config = NULL;
41 static gchar *opt_probes = NULL;
42 gchar *opt_probe_group = NULL;
43 gchar *opt_triggers = NULL;
44 gchar *opt_pds = NULL;
46 static gchar *opt_pd_stack = NULL;
47 static gchar *opt_pd_annotations = NULL;
48 static gchar *opt_pd_meta = NULL;
49 static gchar *opt_pd_binary = NULL;
51 gchar *opt_input_format = NULL;
52 gchar *opt_output_format = NULL;
53 static gchar *opt_show = NULL;
54 gchar *opt_time = NULL;
55 gchar *opt_samples = NULL;
56 gchar *opt_frames = NULL;
57 gchar *opt_continuous = NULL;
58 static gchar *opt_set = NULL;
60 static GOptionEntry optargs[] = {
61 {"version", 'V', 0, G_OPTION_ARG_NONE, &opt_version,
62 "Show version and support list", NULL},
63 {"loglevel", 'l', 0, G_OPTION_ARG_INT, &opt_loglevel,
64 "Set loglevel (5 is most verbose)", NULL},
65 {"driver", 'd', 0, G_OPTION_ARG_STRING, &opt_drv,
66 "The driver to use", NULL},
67 {"config", 'c', 0, G_OPTION_ARG_STRING, &opt_config,
68 "Specify device configuration options", NULL},
69 {"input-file", 'i', 0, G_OPTION_ARG_FILENAME, &opt_input_file,
70 "Load input from file", NULL},
71 {"input-format", 'I', 0, G_OPTION_ARG_STRING, &opt_input_format,
72 "Input format", NULL},
73 {"output-file", 'o', 0, G_OPTION_ARG_FILENAME, &opt_output_file,
74 "Save output to file", NULL},
75 {"output-format", 'O', 0, G_OPTION_ARG_STRING, &opt_output_format,
76 "Output format", NULL},
77 {"probes", 'p', 0, G_OPTION_ARG_STRING, &opt_probes,
78 "Probes to use", NULL},
79 {"probe-group", 'g', 0, G_OPTION_ARG_STRING, &opt_probe_group,
80 "Probe groups", NULL},
81 {"triggers", 't', 0, G_OPTION_ARG_STRING, &opt_triggers,
82 "Trigger configuration", NULL},
83 {"wait-trigger", 'w', 0, G_OPTION_ARG_NONE, &opt_wait_trigger,
84 "Wait for trigger", NULL},
86 {"protocol-decoders", 'P', 0, G_OPTION_ARG_STRING, &opt_pds,
87 "Protocol decoders to run", NULL},
88 {"protocol-decoder-stack", 'S', 0, G_OPTION_ARG_STRING, &opt_pd_stack,
89 "Protocol decoder stack", NULL},
90 {"protocol-decoder-annotations", 'A', 0, G_OPTION_ARG_STRING, &opt_pd_annotations,
91 "Protocol decoder annotation(s) to show", NULL},
92 {"protocol-decoder-meta", 'M', 0, G_OPTION_ARG_STRING, &opt_pd_meta,
93 "Protocol decoder meta output to show", NULL},
94 {"protocol-decoder-binary", 'B', 0, G_OPTION_ARG_STRING, &opt_pd_binary,
95 "Protocol decoder binary output to show", NULL},
97 {"scan", 0, 0, G_OPTION_ARG_NONE, &opt_scan_devs,
98 "Scan for devices", NULL},
99 {"show", 0, 0, G_OPTION_ARG_NONE, &opt_show,
100 "Show device detail", NULL},
101 {"time", 0, 0, G_OPTION_ARG_STRING, &opt_time,
102 "How long to sample (ms)", NULL},
103 {"samples", 0, 0, G_OPTION_ARG_STRING, &opt_samples,
104 "Number of samples to acquire", NULL},
105 {"frames", 0, 0, G_OPTION_ARG_STRING, &opt_frames,
106 "Number of frames to acquire", NULL},
107 {"continuous", 0, 0, G_OPTION_ARG_NONE, &opt_continuous,
108 "Sample continuously", NULL},
109 {"set", 0, 0, G_OPTION_ARG_NONE, &opt_set, "Set device options only", NULL},
110 {NULL, 0, 0, 0, NULL, NULL, NULL}
114 static void logger(const gchar *log_domain, GLogLevelFlags log_level,
115 const gchar *message, gpointer cb_data)
121 * All messages, warnings, errors etc. go to stderr (not stdout) in
122 * order to not mess up the CLI tool data output, e.g. VCD output.
124 if (log_level & (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING)
125 || opt_loglevel > SR_LOG_WARN) {
126 fprintf(stderr, "%s\n", message);
130 if (log_level & (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL))
135 int select_probes(struct sr_dev_inst *sdi)
137 struct sr_probe *probe;
138 GSList *selected_probes, *l;
143 if (!(selected_probes = parse_probestring(sdi, opt_probes)))
146 for (l = sdi->probes; l; l = l->next) {
148 if (g_slist_find(selected_probes, probe))
149 probe->enabled = TRUE;
151 probe->enabled = FALSE;
153 g_slist_free(selected_probes);
158 static void set_options(void)
160 struct sr_dev_inst *sdi;
165 g_critical("No setting specified.");
169 if (!(devargs = parse_generic_arg(opt_config, FALSE)))
172 if (!(devices = device_scan())) {
173 g_critical("No devices found.");
178 if (sr_dev_open(sdi) != SR_OK) {
179 g_critical("Failed to open device.");
183 set_dev_options(sdi, devargs);
186 g_slist_free(devices);
187 g_hash_table_destroy(devargs);
191 int main(int argc, char **argv)
193 GOptionContext *context;
198 g_log_set_default_handler(logger, NULL);
200 context = g_option_context_new(NULL);
201 g_option_context_add_main_entries(context, optargs, NULL);
205 if (!g_option_context_parse(context, &argc, &argv, &error)) {
206 g_critical("%s", error->message);
210 /* Set the loglevel (amount of messages to output) for libsigrok. */
211 if (sr_log_loglevel_set(opt_loglevel) != SR_OK)
214 if (sr_init(&sr_ctx) != SR_OK)
218 /* Set the loglevel (amount of messages to output) for libsigrokdecode. */
219 if (srd_log_loglevel_set(opt_loglevel) != SRD_OK)
223 if (srd_init(NULL) != SRD_OK)
225 if (srd_session_new(&srd_sess) != SRD_OK) {
226 g_critical("Failed to create new decode session.");
229 if (register_pds(opt_pds, opt_pd_annotations) != 0)
231 if (setup_pd_stack(opt_pds, opt_pd_stack, opt_pd_annotations) != 0)
234 /* Only one output type is ever shown. */
236 if (setup_pd_binary(opt_pd_binary) != 0)
238 if (srd_pd_output_callback_add(srd_sess, SRD_OUTPUT_BINARY,
239 show_pd_binary, NULL) != SRD_OK)
241 } else if (opt_pd_meta) {
242 if (setup_pd_meta(opt_pd_meta) != 0)
244 if (srd_pd_output_callback_add(srd_sess, SRD_OUTPUT_META,
245 show_pd_meta, NULL) != SRD_OK)
248 if (opt_pd_annotations)
249 if (setup_pd_annotations(opt_pd_annotations) != 0)
251 if (srd_pd_output_callback_add(srd_sess, SRD_OUTPUT_ANN,
252 show_pd_annotations, NULL) != SRD_OK)
258 if (setup_output_format() != 0)
263 else if (opt_scan_devs)
266 else if (opt_pds && opt_show)
271 else if (opt_input_file)
275 else if (opt_samples || opt_time || opt_frames || opt_continuous)
278 help = g_option_context_get_help(context, TRUE, NULL);
294 g_option_context_free(context);