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