]> sigrok.org Git - sigrok-cli.git/blob - main.c
Reduce reliance on globals
[sigrok-cli.git] / main.c
1 /*
2  * This file is part of the sigrok-cli project.
3  *
4  * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 #include "config.h"
21 #include <glib.h>
22 #include <libsigrok/libsigrok.h>
23 #ifdef HAVE_SRD
24 #include <libsigrokdecode/libsigrokdecode.h> /* First, so we avoid a _POSIX_C_SOURCE warning. */
25 #endif
26 #include "sigrok-cli.h"
27
28 struct sr_context *sr_ctx = NULL;
29 #ifdef HAVE_SRD
30 struct srd_session *srd_sess = NULL;
31 #endif
32
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;
45 #ifdef HAVE_SRD
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;
50 #endif
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;
59
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},
85 #ifdef HAVE_SRD
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},
96 #endif
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}
111 };
112
113
114 static void logger(const gchar *log_domain, GLogLevelFlags log_level,
115                    const gchar *message, gpointer cb_data)
116 {
117         (void)log_domain;
118         (void)cb_data;
119
120         /*
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.
123          */
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);
127                 fflush(stderr);
128         }
129
130         if (log_level & (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL))
131                 exit(1);
132
133 }
134
135 int select_probes(struct sr_dev_inst *sdi)
136 {
137         struct sr_probe *probe;
138         GSList *selected_probes, *l;
139
140         if (!opt_probes)
141                 return SR_OK;
142
143         if (!(selected_probes = parse_probestring(sdi, opt_probes)))
144                 return SR_ERR;
145
146         for (l = sdi->probes; l; l = l->next) {
147                 probe = l->data;
148                 if (g_slist_find(selected_probes, probe))
149                         probe->enabled = TRUE;
150                 else
151                         probe->enabled = FALSE;
152         }
153         g_slist_free(selected_probes);
154
155         return SR_OK;
156 }
157
158 static void set_options(void)
159 {
160         struct sr_dev_inst *sdi;
161         GSList *devices;
162         GHashTable *devargs;
163
164         if (!opt_config) {
165                 g_critical("No setting specified.");
166                 return;
167         }
168
169         if (!(devargs = parse_generic_arg(opt_config, FALSE)))
170                 return;
171
172         if (!(devices = device_scan())) {
173                 g_critical("No devices found.");
174                 return;
175         }
176         sdi = devices->data;
177
178         if (sr_dev_open(sdi) != SR_OK) {
179                 g_critical("Failed to open device.");
180                 return;
181         }
182
183         set_dev_options(sdi, devargs);
184
185         sr_dev_close(sdi);
186         g_slist_free(devices);
187         g_hash_table_destroy(devargs);
188
189 }
190
191 int main(int argc, char **argv)
192 {
193         GOptionContext *context;
194         GError *error;
195         int ret;
196         char *help;
197
198         g_log_set_default_handler(logger, NULL);
199
200         context = g_option_context_new(NULL);
201         g_option_context_add_main_entries(context, optargs, NULL);
202
203         ret = 1;
204         error = NULL;
205         if (!g_option_context_parse(context, &argc, &argv, &error)) {
206                 g_critical("%s", error->message);
207                 goto done;
208         }
209
210         /* Set the loglevel (amount of messages to output) for libsigrok. */
211         if (sr_log_loglevel_set(opt_loglevel) != SR_OK)
212                 goto done;
213
214         if (sr_init(&sr_ctx) != SR_OK)
215                 goto done;
216
217 #ifdef HAVE_SRD
218         /* Set the loglevel (amount of messages to output) for libsigrokdecode. */
219         if (srd_log_loglevel_set(opt_loglevel) != SRD_OK)
220                 goto done;
221
222         if (opt_pds) {
223                 if (srd_init(NULL) != SRD_OK)
224                         goto done;
225                 if (srd_session_new(&srd_sess) != SRD_OK) {
226                         g_critical("Failed to create new decode session.");
227                         goto done;
228                 }
229                 if (register_pds(opt_pds, opt_pd_annotations) != 0)
230                         goto done;
231                 if (setup_pd_stack(opt_pds, opt_pd_stack, opt_pd_annotations) != 0)
232                         goto done;
233
234                 /* Only one output type is ever shown. */
235                 if (opt_pd_binary) {
236                         if (setup_pd_binary(opt_pd_binary) != 0)
237                                 goto done;
238                         if (srd_pd_output_callback_add(srd_sess, SRD_OUTPUT_BINARY,
239                                         show_pd_binary, NULL) != SRD_OK)
240                                 goto done;
241                 } else if (opt_pd_meta) {
242                         if (setup_pd_meta(opt_pd_meta) != 0)
243                                 goto done;
244                         if (srd_pd_output_callback_add(srd_sess, SRD_OUTPUT_META,
245                                         show_pd_meta, NULL) != SRD_OK)
246                                 goto done;
247                 } else {
248                         if (opt_pd_annotations)
249                                 if (setup_pd_annotations(opt_pd_annotations) != 0)
250                                         goto done;
251                         if (srd_pd_output_callback_add(srd_sess, SRD_OUTPUT_ANN,
252                                         show_pd_annotations, NULL) != SRD_OK)
253                                 goto done;
254                 }
255         }
256 #endif
257
258         if (setup_output_format() != 0)
259                 goto done;
260
261         if (opt_version)
262                 show_version();
263         else if (opt_scan_devs)
264                 show_dev_list();
265 #ifdef HAVE_SRD
266         else if (opt_pds && opt_show)
267                 show_pd_detail();
268 #endif
269         else if (opt_show)
270                 show_dev_detail();
271         else if (opt_input_file)
272                 load_input_file();
273         else if (opt_set)
274                 set_options();
275         else if (opt_samples || opt_time || opt_frames || opt_continuous)
276                 run_session();
277         else {
278                 help = g_option_context_get_help(context, TRUE, NULL);
279                 printf("%s", help);
280                 g_free(help);
281         }
282
283 #ifdef HAVE_SRD
284         if (opt_pds)
285                 srd_exit();
286 #endif
287
288         ret = 0;
289
290 done:
291         if (sr_ctx)
292                 sr_exit(sr_ctx);
293
294         g_option_context_free(context);
295
296         return ret;
297 }