]>
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 | ||
2be182e6 BV |
29 | static void logger(const gchar *log_domain, GLogLevelFlags log_level, |
30 | const gchar *message, gpointer cb_data) | |
31 | { | |
32 | (void)log_domain; | |
33 | (void)cb_data; | |
34 | ||
35 | /* | |
36 | * All messages, warnings, errors etc. go to stderr (not stdout) in | |
37 | * order to not mess up the CLI tool data output, e.g. VCD output. | |
38 | */ | |
39 | if (log_level & (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING) | |
40 | || opt_loglevel > SR_LOG_WARN) { | |
41 | fprintf(stderr, "%s\n", message); | |
42 | fflush(stderr); | |
43 | } | |
44 | ||
45 | if (log_level & (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL)) | |
46 | exit(1); | |
47 | ||
48 | } | |
49 | ||
029d73fe | 50 | int select_channels(struct sr_dev_inst *sdi) |
2be182e6 | 51 | { |
029d73fe | 52 | struct sr_channel *ch; |
c6fa2b2e UH |
53 | GSList *selected_channels, *l, *channels; |
54 | ||
55 | channels = sr_dev_inst_channels_get(sdi); | |
2be182e6 | 56 | |
3dfbfbc8 UH |
57 | if (opt_channels) { |
58 | if (!(selected_channels = parse_channelstring(sdi, opt_channels))) | |
39aedc34 DE |
59 | return SR_ERR; |
60 | ||
c6fa2b2e | 61 | for (l = channels; l; l = l->next) { |
029d73fe UH |
62 | ch = l->data; |
63 | if (g_slist_find(selected_channels, ch)) | |
64 | ch->enabled = TRUE; | |
39aedc34 | 65 | else |
029d73fe | 66 | ch->enabled = FALSE; |
39aedc34 | 67 | } |
029d73fe | 68 | g_slist_free(selected_channels); |
2be182e6 | 69 | } |
39aedc34 | 70 | #ifdef HAVE_SRD |
3dfbfbc8 | 71 | map_pd_channels(sdi); |
39aedc34 | 72 | #endif |
2be182e6 BV |
73 | return SR_OK; |
74 | } | |
75 | ||
24bd9719 BV |
76 | gboolean config_key_has_cap(struct sr_dev_driver *driver, |
77 | const struct sr_dev_inst *sdi, struct sr_channel_group *cg, | |
78 | uint32_t key, uint32_t capability) | |
79 | { | |
80 | GVariant *gvar_opts; | |
81 | const uint32_t *opts; | |
82 | gsize num_opts, i; | |
83 | ||
84 | if (sr_config_list(driver, sdi, cg, SR_CONF_DEVICE_OPTIONS, | |
85 | &gvar_opts) != SR_OK) | |
86 | return FALSE; | |
87 | ||
88 | opts = g_variant_get_fixed_array(gvar_opts, &num_opts, sizeof(uint32_t)); | |
89 | for (i = 0; i < num_opts; i++) { | |
90 | if ((opts[i] & SR_CONF_MASK) == key) { | |
91 | if ((opts[i] & capability) == capability) | |
92 | return TRUE; | |
93 | else | |
94 | return FALSE; | |
95 | } | |
96 | } | |
97 | ||
98 | return FALSE; | |
99 | } | |
100 | ||
101 | int maybe_config_get(struct sr_dev_driver *driver, | |
102 | const struct sr_dev_inst *sdi, struct sr_channel_group *cg, | |
103 | uint32_t key, GVariant **gvar) | |
104 | { | |
105 | if (config_key_has_cap(driver, sdi, cg, key, SR_CONF_GET)) | |
106 | return sr_config_get(driver, sdi, cg, key, gvar); | |
107 | ||
108 | return SR_ERR_NA; | |
109 | } | |
110 | ||
111 | int maybe_config_set(struct sr_dev_driver *driver, | |
112 | const struct sr_dev_inst *sdi, struct sr_channel_group *cg, | |
113 | uint32_t key, GVariant *gvar) | |
114 | { | |
115 | if (config_key_has_cap(driver, sdi, cg, key, SR_CONF_SET)) | |
116 | return sr_config_set(sdi, cg, key, gvar); | |
117 | ||
118 | return SR_ERR_NA; | |
119 | } | |
120 | ||
121 | int maybe_config_list(struct sr_dev_driver *driver, | |
122 | const struct sr_dev_inst *sdi, struct sr_channel_group *cg, | |
123 | uint32_t key, GVariant **gvar) | |
124 | { | |
125 | if (config_key_has_cap(driver, sdi, cg, key, SR_CONF_LIST)) | |
126 | return sr_config_list(driver, sdi, cg, key, gvar); | |
127 | ||
128 | return SR_ERR_NA; | |
129 | } | |
130 | ||
62a64762 BV |
131 | static void get_option(void) |
132 | { | |
133 | struct sr_dev_inst *sdi; | |
134 | struct sr_channel_group *cg; | |
135 | const struct sr_config_info *ci; | |
136 | GSList *devices; | |
137 | GVariant *gvar; | |
88a195e4 | 138 | GHashTable *devargs; |
62a64762 BV |
139 | int ret; |
140 | char *s; | |
c6fa2b2e | 141 | struct sr_dev_driver *driver; |
62a64762 BV |
142 | |
143 | if (!(devices = device_scan())) { | |
144 | g_critical("No devices found."); | |
145 | return; | |
146 | } | |
147 | sdi = devices->data; | |
148 | g_slist_free(devices); | |
149 | ||
c6fa2b2e UH |
150 | driver = sr_dev_inst_driver_get(sdi); |
151 | ||
62a64762 BV |
152 | if (sr_dev_open(sdi) != SR_OK) { |
153 | g_critical("Failed to open device."); | |
154 | return; | |
155 | } | |
156 | ||
157 | cg = select_channel_group(sdi); | |
158 | if (!(ci = sr_config_info_name_get(opt_get))) | |
159 | g_critical("Unknown option '%s'", opt_get); | |
160 | ||
88a195e4 BV |
161 | if ((devargs = parse_generic_arg(opt_config, FALSE))) |
162 | set_dev_options(sdi, devargs); | |
163 | else devargs = NULL; | |
164 | ||
24bd9719 | 165 | if ((ret = maybe_config_get(driver, sdi, cg, ci->key, &gvar)) != SR_OK) |
62a64762 BV |
166 | g_critical("Failed to get '%s': %s", opt_get, sr_strerror(ret)); |
167 | s = g_variant_print(gvar, FALSE); | |
168 | printf("%s\n", s); | |
169 | g_free(s); | |
170 | ||
171 | g_variant_unref(gvar); | |
172 | sr_dev_close(sdi); | |
88a195e4 BV |
173 | if (devargs) |
174 | g_hash_table_destroy(devargs); | |
62a64762 BV |
175 | } |
176 | ||
2be182e6 BV |
177 | static void set_options(void) |
178 | { | |
179 | struct sr_dev_inst *sdi; | |
180 | GSList *devices; | |
181 | GHashTable *devargs; | |
182 | ||
183 | if (!opt_config) { | |
184 | g_critical("No setting specified."); | |
185 | return; | |
186 | } | |
187 | ||
188 | if (!(devargs = parse_generic_arg(opt_config, FALSE))) | |
189 | return; | |
190 | ||
191 | if (!(devices = device_scan())) { | |
192 | g_critical("No devices found."); | |
193 | return; | |
194 | } | |
195 | sdi = devices->data; | |
b4eece7c | 196 | g_slist_free(devices); |
2be182e6 BV |
197 | |
198 | if (sr_dev_open(sdi) != SR_OK) { | |
199 | g_critical("Failed to open device."); | |
200 | return; | |
201 | } | |
202 | ||
203 | set_dev_options(sdi, devargs); | |
204 | ||
205 | sr_dev_close(sdi); | |
2be182e6 BV |
206 | g_hash_table_destroy(devargs); |
207 | ||
208 | } | |
209 | ||
210 | int main(int argc, char **argv) | |
211 | { | |
2be182e6 BV |
212 | g_log_set_default_handler(logger, NULL); |
213 | ||
cd62e027 JS |
214 | if (parse_options(argc, argv)) { |
215 | return 1; | |
2be182e6 BV |
216 | } |
217 | ||
218 | /* Set the loglevel (amount of messages to output) for libsigrok. */ | |
219 | if (sr_log_loglevel_set(opt_loglevel) != SR_OK) | |
220 | goto done; | |
221 | ||
222 | if (sr_init(&sr_ctx) != SR_OK) | |
223 | goto done; | |
224 | ||
225 | #ifdef HAVE_SRD | |
226 | /* Set the loglevel (amount of messages to output) for libsigrokdecode. */ | |
227 | if (srd_log_loglevel_set(opt_loglevel) != SRD_OK) | |
228 | goto done; | |
229 | ||
230 | if (opt_pds) { | |
231 | if (srd_init(NULL) != SRD_OK) | |
232 | goto done; | |
233 | if (srd_session_new(&srd_sess) != SRD_OK) { | |
234 | g_critical("Failed to create new decode session."); | |
235 | goto done; | |
236 | } | |
26c63758 | 237 | if (register_pds(opt_pds, opt_pd_annotations) != 0) |
2be182e6 | 238 | goto done; |
26c63758 | 239 | if (setup_pd_stack(opt_pds, opt_pd_stack, opt_pd_annotations) != 0) |
2be182e6 BV |
240 | goto done; |
241 | ||
242 | /* Only one output type is ever shown. */ | |
243 | if (opt_pd_binary) { | |
26c63758 | 244 | if (setup_pd_binary(opt_pd_binary) != 0) |
2be182e6 BV |
245 | goto done; |
246 | if (srd_pd_output_callback_add(srd_sess, SRD_OUTPUT_BINARY, | |
247 | show_pd_binary, NULL) != SRD_OK) | |
248 | goto done; | |
249 | } else if (opt_pd_meta) { | |
26c63758 | 250 | if (setup_pd_meta(opt_pd_meta) != 0) |
2be182e6 BV |
251 | goto done; |
252 | if (srd_pd_output_callback_add(srd_sess, SRD_OUTPUT_META, | |
253 | show_pd_meta, NULL) != SRD_OK) | |
254 | goto done; | |
255 | } else { | |
256 | if (opt_pd_annotations) | |
26c63758 | 257 | if (setup_pd_annotations(opt_pd_annotations) != 0) |
2be182e6 BV |
258 | goto done; |
259 | if (srd_pd_output_callback_add(srd_sess, SRD_OUTPUT_ANN, | |
260 | show_pd_annotations, NULL) != SRD_OK) | |
261 | goto done; | |
262 | } | |
263 | } | |
264 | #endif | |
265 | ||
2be182e6 BV |
266 | if (opt_version) |
267 | show_version(); | |
a8b4041a BV |
268 | else if (opt_input_format && opt_show) |
269 | show_input(); | |
ad6520c4 BV |
270 | else if (opt_output_format && opt_show) |
271 | show_output(); | |
6f7b4c5d UH |
272 | else if (opt_transform_module && opt_show) |
273 | show_transform(); | |
2be182e6 BV |
274 | else if (opt_scan_devs) |
275 | show_dev_list(); | |
276 | #ifdef HAVE_SRD | |
277 | else if (opt_pds && opt_show) | |
278 | show_pd_detail(); | |
279 | #endif | |
280 | else if (opt_show) | |
281 | show_dev_detail(); | |
282 | else if (opt_input_file) | |
283 | load_input_file(); | |
62a64762 BV |
284 | else if (opt_get) |
285 | get_option(); | |
2be182e6 BV |
286 | else if (opt_set) |
287 | set_options(); | |
288 | else if (opt_samples || opt_time || opt_frames || opt_continuous) | |
289 | run_session(); | |
cd62e027 JS |
290 | else |
291 | show_help(); | |
2be182e6 BV |
292 | |
293 | #ifdef HAVE_SRD | |
294 | if (opt_pds) | |
295 | srd_exit(); | |
296 | #endif | |
297 | ||
2be182e6 BV |
298 | done: |
299 | if (sr_ctx) | |
300 | sr_exit(sr_ctx); | |
301 | ||
cd62e027 | 302 | return 0; |
2be182e6 | 303 | } |