doc: minor update to -O and -g options in the manpage
[sigrok-cli.git] / main.c
blob689c2d57496ba5b4506698165318a88459ea7e2e
1 /*
2 * This file is part of the sigrok-cli project.
4 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
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.
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.
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/>.
20 #include <config.h>
21 #include <stdlib.h>
22 #include <glib.h>
23 #include "sigrok-cli.h"
25 struct sr_context *sr_ctx = NULL;
26 #ifdef HAVE_SRD
27 struct srd_session *srd_sess = NULL;
28 #endif
30 static void logger(const gchar *log_domain, GLogLevelFlags log_level,
31 const gchar *message, gpointer cb_data)
33 (void)log_domain;
34 (void)cb_data;
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.
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);
46 if (log_level & (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL))
47 exit(1);
51 int select_channels(struct sr_dev_inst *sdi)
53 struct sr_channel *ch;
54 gboolean enabled;
55 GSList *selected_channels, *l, *channels;
57 channels = sr_dev_inst_channels_get(sdi);
59 if (opt_channels) {
60 if (!(selected_channels = parse_channelstring(sdi, opt_channels)))
61 return SR_ERR;
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;
69 g_slist_free(selected_channels);
71 #ifdef HAVE_SRD
72 map_pd_channels(sdi);
73 #endif
74 return SR_OK;
77 int maybe_config_get(struct sr_dev_driver *driver,
78 const struct sr_dev_inst *sdi, struct sr_channel_group *cg,
79 uint32_t key, GVariant **gvar)
81 if (sr_dev_config_capabilities_list(sdi, cg, key) & SR_CONF_GET)
82 return sr_config_get(driver, sdi, cg, key, gvar);
84 return SR_ERR_NA;
87 int maybe_config_set(struct sr_dev_driver *driver,
88 const struct sr_dev_inst *sdi, struct sr_channel_group *cg,
89 uint32_t key, GVariant *gvar)
91 (void)driver;
93 if (sr_dev_config_capabilities_list(sdi, cg, key) & SR_CONF_SET)
94 return sr_config_set(sdi, cg, key, gvar);
96 return SR_ERR_NA;
99 int maybe_config_list(struct sr_dev_driver *driver,
100 const struct sr_dev_inst *sdi, struct sr_channel_group *cg,
101 uint32_t key, GVariant **gvar)
103 if (sr_dev_config_capabilities_list(sdi, cg, key) & SR_CONF_LIST)
104 return sr_config_list(driver, sdi, cg, key, gvar);
106 return SR_ERR_NA;
109 static void get_option(void)
111 struct sr_dev_inst *sdi;
112 struct sr_channel_group *cg;
113 const struct sr_key_info *ci;
114 GSList *devices;
115 GVariant *gvar;
116 GHashTable *devargs;
117 int ret;
118 char *s;
119 struct sr_dev_driver *driver;
121 if (!(devices = device_scan())) {
122 g_critical("No devices found.");
123 return;
125 sdi = devices->data;
126 g_slist_free(devices);
128 driver = sr_dev_inst_driver_get(sdi);
130 if (sr_dev_open(sdi) != SR_OK) {
131 g_critical("Failed to open device.");
132 return;
135 cg = select_channel_group(sdi);
136 if (!(ci = sr_key_info_name_get(SR_KEY_CONFIG, opt_get)))
137 g_critical("Unknown option '%s'", opt_get);
139 if ((devargs = parse_generic_arg(opt_config, FALSE)))
140 set_dev_options(sdi, devargs);
141 else
142 devargs = NULL;
144 if ((ret = maybe_config_get(driver, sdi, cg, ci->key, &gvar)) != SR_OK)
145 g_critical("Failed to get '%s': %s", opt_get, sr_strerror(ret));
146 s = g_variant_print(gvar, FALSE);
147 printf("%s\n", s);
148 g_free(s);
150 g_variant_unref(gvar);
151 sr_dev_close(sdi);
152 if (devargs)
153 g_hash_table_destroy(devargs);
156 static void set_options(void)
158 struct sr_dev_inst *sdi;
159 GSList *devices;
160 GHashTable *devargs;
162 if (!opt_config) {
163 g_critical("No setting specified.");
164 return;
167 if (!(devargs = parse_generic_arg(opt_config, FALSE)))
168 return;
170 if (!(devices = device_scan())) {
171 g_critical("No devices found.");
172 return;
174 sdi = devices->data;
175 g_slist_free(devices);
177 if (sr_dev_open(sdi) != SR_OK) {
178 g_critical("Failed to open device.");
179 return;
182 set_dev_options(sdi, devargs);
184 sr_dev_close(sdi);
185 g_hash_table_destroy(devargs);
189 int main(int argc, char **argv)
191 g_log_set_default_handler(logger, NULL);
193 if (parse_options(argc, argv)) {
194 return 1;
197 /* Set the loglevel (amount of messages to output) for libsigrok. */
198 if (sr_log_loglevel_set(opt_loglevel) != SR_OK)
199 goto done;
201 if (sr_init(&sr_ctx) != SR_OK)
202 goto done;
204 #ifdef HAVE_SRD
205 /* Set the loglevel (amount of messages to output) for libsigrokdecode. */
206 if (srd_log_loglevel_set(opt_loglevel) != SRD_OK)
207 goto done;
209 if (opt_pds) {
210 if (srd_init(NULL) != SRD_OK)
211 goto done;
212 if (srd_session_new(&srd_sess) != SRD_OK) {
213 g_critical("Failed to create new decode session.");
214 goto done;
216 if (register_pds(opt_pds, opt_pd_annotations) != 0)
217 goto done;
219 /* Only one output type is ever shown. */
220 if (opt_pd_binary) {
221 if (setup_pd_binary(opt_pd_binary) != 0)
222 goto done;
223 if (srd_pd_output_callback_add(srd_sess, SRD_OUTPUT_BINARY,
224 show_pd_binary, NULL) != SRD_OK)
225 goto done;
226 } else if (opt_pd_meta) {
227 if (setup_pd_meta(opt_pd_meta) != 0)
228 goto done;
229 if (srd_pd_output_callback_add(srd_sess, SRD_OUTPUT_META,
230 show_pd_meta, NULL) != SRD_OK)
231 goto done;
232 } else {
233 if (opt_pd_annotations)
234 if (setup_pd_annotations(opt_pd_annotations) != 0)
235 goto done;
236 if (srd_pd_output_callback_add(srd_sess, SRD_OUTPUT_ANN,
237 show_pd_annotations, NULL) != SRD_OK)
238 goto done;
241 #endif
243 if (opt_version)
244 show_version();
245 else if (opt_list_supported)
246 show_supported();
247 else if (opt_input_format && opt_show)
248 show_input();
249 else if (opt_output_format && opt_show)
250 show_output();
251 else if (opt_transform_module && opt_show)
252 show_transform();
253 else if (opt_scan_devs)
254 show_dev_list();
255 #ifdef HAVE_SRD
256 else if (opt_pds && opt_show)
257 show_pd_detail();
258 #endif
259 else if (opt_show)
260 show_dev_detail();
261 else if (opt_input_file)
262 load_input_file();
263 else if (opt_get)
264 get_option();
265 else if (opt_set)
266 set_options();
267 else if (opt_samples || opt_time || opt_frames || opt_continuous)
268 run_session();
269 else
270 show_help();
272 #ifdef HAVE_SRD
273 if (opt_pds)
274 srd_exit();
275 #endif
277 done:
278 if (sr_ctx)
279 sr_exit(sr_ctx);
281 return 0;