]> sigrok.org Git - sigrok-cli.git/blame - main.c
Makefile.am: Shorten ChangeLog target a bit.
[sigrok-cli.git] / main.c
CommitLineData
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
d486cbdd 20#include <config.h>
8d52f788 21#include <stdlib.h>
2be182e6 22#include <glib.h>
662a1e27 23#include "sigrok-cli.h"
2be182e6
BV
24
25struct sr_context *sr_ctx = NULL;
26#ifdef HAVE_SRD
27struct srd_session *srd_sess = NULL;
28#endif
29
2be182e6
BV
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
029d73fe 51int select_channels(struct sr_dev_inst *sdi)
2be182e6 52{
029d73fe 53 struct sr_channel *ch;
abb03685 54 gboolean enabled;
c6fa2b2e
UH
55 GSList *selected_channels, *l, *channels;
56
57 channels = sr_dev_inst_channels_get(sdi);
2be182e6 58
3dfbfbc8
UH
59 if (opt_channels) {
60 if (!(selected_channels = parse_channelstring(sdi, opt_channels)))
39aedc34
DE
61 return SR_ERR;
62
c6fa2b2e 63 for (l = channels; l; l = l->next) {
029d73fe 64 ch = l->data;
abb03685
ML
65 enabled = (g_slist_find(selected_channels, ch) != NULL);
66 if (sr_dev_channel_enable(ch, enabled) != SR_OK)
67 return SR_ERR;
39aedc34 68 }
029d73fe 69 g_slist_free(selected_channels);
2be182e6 70 }
39aedc34 71#ifdef HAVE_SRD
3dfbfbc8 72 map_pd_channels(sdi);
39aedc34 73#endif
2be182e6
BV
74 return SR_OK;
75}
76
24bd9719
BV
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;
24278d3e 84 gboolean result;
24bd9719
BV
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));
24278d3e 91 result = FALSE;
24bd9719
BV
92 for (i = 0; i < num_opts; i++) {
93 if ((opts[i] & SR_CONF_MASK) == key) {
94 if ((opts[i] & capability) == capability)
24278d3e 95 result = TRUE;
24bd9719 96 else
24278d3e
ML
97 result = FALSE;
98 break;
24bd9719
BV
99 }
100 }
24278d3e 101 g_variant_unref(gvar_opts);
24bd9719 102
24278d3e 103 return result;
24bd9719
BV
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
62a64762
BV
136static void get_option(void)
137{
138 struct sr_dev_inst *sdi;
139 struct sr_channel_group *cg;
e4e4b472 140 const struct sr_key_info *ci;
62a64762
BV
141 GSList *devices;
142 GVariant *gvar;
88a195e4 143 GHashTable *devargs;
62a64762
BV
144 int ret;
145 char *s;
c6fa2b2e 146 struct sr_dev_driver *driver;
62a64762
BV
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
c6fa2b2e
UH
155 driver = sr_dev_inst_driver_get(sdi);
156
62a64762
BV
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);
e4e4b472 163 if (!(ci = sr_key_info_name_get(SR_KEY_CONFIG, opt_get)))
62a64762
BV
164 g_critical("Unknown option '%s'", opt_get);
165
88a195e4
BV
166 if ((devargs = parse_generic_arg(opt_config, FALSE)))
167 set_dev_options(sdi, devargs);
168 else devargs = NULL;
169
24bd9719 170 if ((ret = maybe_config_get(driver, sdi, cg, ci->key, &gvar)) != SR_OK)
62a64762
BV
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);
88a195e4
BV
178 if (devargs)
179 g_hash_table_destroy(devargs);
62a64762
BV
180}
181
2be182e6
BV
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;
b4eece7c 201 g_slist_free(devices);
2be182e6
BV
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);
2be182e6
BV
211 g_hash_table_destroy(devargs);
212
213}
214
215int main(int argc, char **argv)
216{
2be182e6
BV
217 g_log_set_default_handler(logger, NULL);
218
cd62e027
JS
219 if (parse_options(argc, argv)) {
220 return 1;
2be182e6
BV
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 }
26c63758 242 if (register_pds(opt_pds, opt_pd_annotations) != 0)
2be182e6 243 goto done;
26c63758 244 if (setup_pd_stack(opt_pds, opt_pd_stack, opt_pd_annotations) != 0)
2be182e6
BV
245 goto done;
246
247 /* Only one output type is ever shown. */
248 if (opt_pd_binary) {
26c63758 249 if (setup_pd_binary(opt_pd_binary) != 0)
2be182e6
BV
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) {
26c63758 255 if (setup_pd_meta(opt_pd_meta) != 0)
2be182e6
BV
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)
26c63758 262 if (setup_pd_annotations(opt_pd_annotations) != 0)
2be182e6
BV
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
2be182e6
BV
271 if (opt_version)
272 show_version();
a8b4041a
BV
273 else if (opt_input_format && opt_show)
274 show_input();
ad6520c4
BV
275 else if (opt_output_format && opt_show)
276 show_output();
6f7b4c5d
UH
277 else if (opt_transform_module && opt_show)
278 show_transform();
2be182e6
BV
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();
62a64762
BV
289 else if (opt_get)
290 get_option();
2be182e6
BV
291 else if (opt_set)
292 set_options();
293 else if (opt_samples || opt_time || opt_frames || opt_continuous)
294 run_session();
cd62e027
JS
295 else
296 show_help();
2be182e6
BV
297
298#ifdef HAVE_SRD
299 if (opt_pds)
300 srd_exit();
301#endif
302
2be182e6
BV
303done:
304 if (sr_ctx)
305 sr_exit(sr_ctx);
306
cd62e027 307 return 0;
2be182e6 308}