Rename 'probe group' to 'channel group' everywhere.
[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 "sigrok-cli.h"
21 #include "config.h"
22 #include <stdlib.h>
23 #include <glib.h>
24
25 struct sr_context *sr_ctx = NULL;
26 #ifdef HAVE_SRD
27 struct srd_session *srd_sess = NULL;
28 #endif
29
30 static gboolean opt_version = FALSE;
31 gint opt_loglevel = SR_LOG_WARN; /* Show errors+warnings by default. */
32 static gboolean opt_scan_devs = FALSE;
33 gboolean opt_wait_trigger = FALSE;
34 gchar *opt_input_file = NULL;
35 gchar *opt_output_file = NULL;
36 gchar *opt_drv = NULL;
37 gchar *opt_config = NULL;
38 static gchar *opt_probes = NULL;
39 gchar *opt_channel_group = NULL;
40 gchar *opt_triggers = NULL;
41 gchar *opt_pds = NULL;
42 #ifdef HAVE_SRD
43 static gchar *opt_pd_stack = NULL;
44 static gchar *opt_pd_annotations = NULL;
45 static gchar *opt_pd_meta = NULL;
46 static gchar *opt_pd_binary = NULL;
47 #endif
48 gchar *opt_input_format = NULL;
49 gchar *opt_output_format = NULL;
50 static gchar *opt_show = NULL;
51 gchar *opt_time = NULL;
52 gchar *opt_samples = NULL;
53 gchar *opt_frames = NULL;
54 gchar *opt_continuous = NULL;
55 static gchar *opt_set = NULL;
56
57 static GOptionEntry optargs[] = {
58         {"version", 'V', 0, G_OPTION_ARG_NONE, &opt_version,
59                         "Show version and support list", NULL},
60         {"loglevel", 'l', 0, G_OPTION_ARG_INT, &opt_loglevel,
61                         "Set loglevel (5 is most verbose)", NULL},
62         {"driver", 'd', 0, G_OPTION_ARG_STRING, &opt_drv,
63                         "The driver to use", NULL},
64         {"config", 'c', 0, G_OPTION_ARG_STRING, &opt_config,
65                         "Specify device configuration options", NULL},
66         {"input-file", 'i', 0, G_OPTION_ARG_FILENAME, &opt_input_file,
67                         "Load input from file", NULL},
68         {"input-format", 'I', 0, G_OPTION_ARG_STRING, &opt_input_format,
69                         "Input format", NULL},
70         {"output-file", 'o', 0, G_OPTION_ARG_FILENAME, &opt_output_file,
71                         "Save output to file", NULL},
72         {"output-format", 'O', 0, G_OPTION_ARG_STRING, &opt_output_format,
73                         "Output format", NULL},
74         {"probes", 'p', 0, G_OPTION_ARG_STRING, &opt_probes,
75                         "Probes to use", NULL},
76         {"channel-group", 'g', 0, G_OPTION_ARG_STRING, &opt_channel_group,
77                         "Channel groups", NULL},
78         {"triggers", 't', 0, G_OPTION_ARG_STRING, &opt_triggers,
79                         "Trigger configuration", NULL},
80         {"wait-trigger", 'w', 0, G_OPTION_ARG_NONE, &opt_wait_trigger,
81                         "Wait for trigger", NULL},
82 #ifdef HAVE_SRD
83         {"protocol-decoders", 'P', 0, G_OPTION_ARG_STRING, &opt_pds,
84                         "Protocol decoders to run", NULL},
85         {"protocol-decoder-stack", 'S', 0, G_OPTION_ARG_STRING, &opt_pd_stack,
86                         "Protocol decoder stack", NULL},
87         {"protocol-decoder-annotations", 'A', 0, G_OPTION_ARG_STRING, &opt_pd_annotations,
88                         "Protocol decoder annotation(s) to show", NULL},
89         {"protocol-decoder-meta", 'M', 0, G_OPTION_ARG_STRING, &opt_pd_meta,
90                         "Protocol decoder meta output to show", NULL},
91         {"protocol-decoder-binary", 'B', 0, G_OPTION_ARG_STRING, &opt_pd_binary,
92                         "Protocol decoder binary output to show", NULL},
93 #endif
94         {"scan", 0, 0, G_OPTION_ARG_NONE, &opt_scan_devs,
95                         "Scan for devices", NULL},
96         {"show", 0, 0, G_OPTION_ARG_NONE, &opt_show,
97                         "Show device detail", NULL},
98         {"time", 0, 0, G_OPTION_ARG_STRING, &opt_time,
99                         "How long to sample (ms)", NULL},
100         {"samples", 0, 0, G_OPTION_ARG_STRING, &opt_samples,
101                         "Number of samples to acquire", NULL},
102         {"frames", 0, 0, G_OPTION_ARG_STRING, &opt_frames,
103                         "Number of frames to acquire", NULL},
104         {"continuous", 0, 0, G_OPTION_ARG_NONE, &opt_continuous,
105                         "Sample continuously", NULL},
106         {"set", 0, 0, G_OPTION_ARG_NONE, &opt_set, "Set device options only", NULL},
107         {NULL, 0, 0, 0, NULL, NULL, NULL}
108 };
109
110
111 static void logger(const gchar *log_domain, GLogLevelFlags log_level,
112                    const gchar *message, gpointer cb_data)
113 {
114         (void)log_domain;
115         (void)cb_data;
116
117         /*
118          * All messages, warnings, errors etc. go to stderr (not stdout) in
119          * order to not mess up the CLI tool data output, e.g. VCD output.
120          */
121         if (log_level & (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING)
122                         || opt_loglevel > SR_LOG_WARN) {
123                 fprintf(stderr, "%s\n", message);
124                 fflush(stderr);
125         }
126
127         if (log_level & (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL))
128                 exit(1);
129
130 }
131
132 int select_probes(struct sr_dev_inst *sdi)
133 {
134         struct sr_probe *probe;
135         GSList *selected_probes, *l;
136
137         if (opt_probes) {
138                 if (!(selected_probes = parse_probestring(sdi, opt_probes)))
139                         return SR_ERR;
140
141                 for (l = sdi->probes; l; l = l->next) {
142                         probe = l->data;
143                         if (g_slist_find(selected_probes, probe))
144                                 probe->enabled = TRUE;
145                         else
146                                 probe->enabled = FALSE;
147                 }
148                 g_slist_free(selected_probes);
149         }
150 #ifdef HAVE_SRD
151         map_pd_probes(sdi);
152 #endif
153         return SR_OK;
154 }
155
156 static void set_options(void)
157 {
158         struct sr_dev_inst *sdi;
159         GSList *devices;
160         GHashTable *devargs;
161
162         if (!opt_config) {
163                 g_critical("No setting specified.");
164                 return;
165         }
166
167         if (!(devargs = parse_generic_arg(opt_config, FALSE)))
168                 return;
169
170         if (!(devices = device_scan())) {
171                 g_critical("No devices found.");
172                 return;
173         }
174         sdi = devices->data;
175
176         if (sr_dev_open(sdi) != SR_OK) {
177                 g_critical("Failed to open device.");
178                 return;
179         }
180
181         set_dev_options(sdi, devargs);
182
183         sr_dev_close(sdi);
184         g_slist_free(devices);
185         g_hash_table_destroy(devargs);
186
187 }
188
189 int main(int argc, char **argv)
190 {
191         GOptionContext *context;
192         GError *error;
193         int ret;
194         char *help;
195
196         g_log_set_default_handler(logger, NULL);
197
198         context = g_option_context_new(NULL);
199         g_option_context_add_main_entries(context, optargs, NULL);
200
201         ret = 1;
202         error = NULL;
203         if (!g_option_context_parse(context, &argc, &argv, &error)) {
204                 g_critical("%s", error->message);
205                 goto done;
206         }
207
208         /* Set the loglevel (amount of messages to output) for libsigrok. */
209         if (sr_log_loglevel_set(opt_loglevel) != SR_OK)
210                 goto done;
211
212         if (sr_init(&sr_ctx) != SR_OK)
213                 goto done;
214
215 #ifdef HAVE_SRD
216         /* Set the loglevel (amount of messages to output) for libsigrokdecode. */
217         if (srd_log_loglevel_set(opt_loglevel) != SRD_OK)
218                 goto done;
219
220         if (opt_pds) {
221                 if (srd_init(NULL) != SRD_OK)
222                         goto done;
223                 if (srd_session_new(&srd_sess) != SRD_OK) {
224                         g_critical("Failed to create new decode session.");
225                         goto done;
226                 }
227                 if (register_pds(opt_pds, opt_pd_annotations) != 0)
228                         goto done;
229                 if (setup_pd_stack(opt_pds, opt_pd_stack, opt_pd_annotations) != 0)
230                         goto done;
231
232                 /* Only one output type is ever shown. */
233                 if (opt_pd_binary) {
234                         if (setup_pd_binary(opt_pd_binary) != 0)
235                                 goto done;
236                         if (srd_pd_output_callback_add(srd_sess, SRD_OUTPUT_BINARY,
237                                         show_pd_binary, NULL) != SRD_OK)
238                                 goto done;
239                 } else if (opt_pd_meta) {
240                         if (setup_pd_meta(opt_pd_meta) != 0)
241                                 goto done;
242                         if (srd_pd_output_callback_add(srd_sess, SRD_OUTPUT_META,
243                                         show_pd_meta, NULL) != SRD_OK)
244                                 goto done;
245                 } else {
246                         if (opt_pd_annotations)
247                                 if (setup_pd_annotations(opt_pd_annotations) != 0)
248                                         goto done;
249                         if (srd_pd_output_callback_add(srd_sess, SRD_OUTPUT_ANN,
250                                         show_pd_annotations, NULL) != SRD_OK)
251                                 goto done;
252                 }
253         }
254 #endif
255
256         if (setup_output_format() != 0)
257                 goto done;
258
259         if (opt_version)
260                 show_version();
261         else if (opt_scan_devs)
262                 show_dev_list();
263 #ifdef HAVE_SRD
264         else if (opt_pds && opt_show)
265                 show_pd_detail();
266 #endif
267         else if (opt_show)
268                 show_dev_detail();
269         else if (opt_input_file)
270                 load_input_file();
271         else if (opt_set)
272                 set_options();
273         else if (opt_samples || opt_time || opt_frames || opt_continuous)
274                 run_session();
275         else {
276                 help = g_option_context_get_help(context, TRUE, NULL);
277                 printf("%s", help);
278                 g_free(help);
279         }
280
281 #ifdef HAVE_SRD
282         if (opt_pds)
283                 srd_exit();
284 #endif
285
286         ret = 0;
287
288 done:
289         if (sr_ctx)
290                 sr_exit(sr_ctx);
291
292         g_option_context_free(context);
293
294         return ret;
295 }