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