device: rename channel group lookup routine
[sigrok-cli.git] / main.c
blob2c1dcfed1f901f78d0056bdb85ef06dbfdcf1407
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;
120 const struct sr_key_info *srci, *srmqi, *srmqfi;
121 uint32_t mq;
122 uint64_t mask, mqflags;
123 unsigned int j;
125 if (!(devices = device_scan())) {
126 g_critical("No devices found.");
127 return;
129 sdi = devices->data;
130 g_slist_free(devices);
132 driver = sr_dev_inst_driver_get(sdi);
134 if (sr_dev_open(sdi) != SR_OK) {
135 g_critical("Failed to open device.");
136 return;
139 cg = lookup_channel_group(sdi);
140 if (!(ci = sr_key_info_name_get(SR_KEY_CONFIG, opt_get)))
141 g_critical("Unknown option '%s'", opt_get);
143 if ((devargs = parse_generic_arg(opt_config, FALSE)))
144 set_dev_options(sdi, devargs);
145 else
146 devargs = NULL;
148 if ((ret = maybe_config_get(driver, sdi, cg, ci->key, &gvar)) != SR_OK)
149 g_critical("Failed to get '%s': %s", opt_get, sr_strerror(ret));
150 srci = sr_key_info_get(SR_KEY_CONFIG, ci->key);
151 if (srci && srci->datatype == SR_T_MQ) {
152 g_variant_get(gvar, "(ut)", &mq, &mqflags);
153 if ((srmqi = sr_key_info_get(SR_KEY_MQ, mq)))
154 printf("%s", srmqi->id);
155 else
156 printf("%d", mq);
157 for (j = 0, mask = 1; j < 32; j++, mask <<= 1) {
158 if (!(mqflags & mask))
159 continue;
160 if ((srmqfi = sr_key_info_get(SR_KEY_MQFLAGS, mqflags & mask)))
161 printf("/%s", srmqfi->id);
162 else
163 printf("/%" PRIu64, mqflags & mask);
165 printf("\n");
166 } else {
167 s = g_variant_print(gvar, FALSE);
168 printf("%s\n", s);
169 g_free(s);
172 g_variant_unref(gvar);
173 sr_dev_close(sdi);
174 if (devargs)
175 g_hash_table_destroy(devargs);
178 static void set_options(void)
180 struct sr_dev_inst *sdi;
181 GSList *devices;
182 GHashTable *devargs;
184 if (!opt_config) {
185 g_critical("No setting specified.");
186 return;
189 if (!(devargs = parse_generic_arg(opt_config, FALSE)))
190 return;
192 if (!(devices = device_scan())) {
193 g_critical("No devices found.");
194 return;
196 sdi = devices->data;
197 g_slist_free(devices);
199 if (sr_dev_open(sdi) != SR_OK) {
200 g_critical("Failed to open device.");
201 return;
204 set_dev_options(sdi, devargs);
206 sr_dev_close(sdi);
207 g_hash_table_destroy(devargs);
211 int main(int argc, char **argv)
213 g_log_set_default_handler(logger, NULL);
215 if (parse_options(argc, argv)) {
216 return 1;
219 /* Set the loglevel (amount of messages to output) for libsigrok. */
220 if (sr_log_loglevel_set(opt_loglevel) != SR_OK)
221 goto done;
223 if (sr_init(&sr_ctx) != SR_OK)
224 goto done;
226 #ifdef HAVE_SRD
227 /* Set the loglevel (amount of messages to output) for libsigrokdecode. */
228 if (srd_log_loglevel_set(opt_loglevel) != SRD_OK)
229 goto done;
231 if (opt_pds) {
232 if (srd_init(NULL) != SRD_OK)
233 goto done;
234 if (srd_session_new(&srd_sess) != SRD_OK) {
235 g_critical("Failed to create new decode session.");
236 goto done;
238 if (register_pds(opt_pds, opt_pd_annotations) != 0)
239 goto done;
241 /* Only one output type is ever shown. */
242 if (opt_pd_binary) {
243 if (setup_pd_binary(opt_pd_binary) != 0)
244 goto done;
245 if (setup_binary_stdout() != 0)
246 goto done;
247 if (srd_pd_output_callback_add(srd_sess, SRD_OUTPUT_BINARY,
248 show_pd_binary, NULL) != SRD_OK)
249 goto done;
250 } else if (opt_pd_meta) {
251 if (setup_pd_meta(opt_pd_meta) != 0)
252 goto done;
253 if (srd_pd_output_callback_add(srd_sess, SRD_OUTPUT_META,
254 show_pd_meta, NULL) != SRD_OK)
255 goto done;
256 } else {
257 if (opt_pd_annotations)
258 if (setup_pd_annotations(opt_pd_annotations) != 0)
259 goto done;
260 if (srd_pd_output_callback_add(srd_sess, SRD_OUTPUT_ANN,
261 show_pd_annotations, NULL) != SRD_OK)
262 goto done;
264 show_pd_prepare();
266 #endif
268 if (opt_version)
269 show_version();
270 else if (opt_list_supported)
271 show_supported();
272 else if (opt_list_supported_wiki)
273 show_supported_wiki();
274 else if (opt_input_file && opt_show)
275 load_input_file(TRUE);
276 else if (opt_input_format && opt_show)
277 show_input();
278 else if (opt_output_format && opt_show)
279 show_output();
280 else if (opt_transform_module && opt_show)
281 show_transform();
282 else if (opt_scan_devs)
283 show_dev_list();
284 #ifdef HAVE_SRD
285 else if (opt_pds && opt_show)
286 show_pd_detail();
287 #endif
288 else if (opt_show)
289 show_dev_detail();
290 else if (opt_input_file)
291 load_input_file(FALSE);
292 else if (opt_get)
293 get_option();
294 else if (opt_set)
295 set_options();
296 else if (opt_samples || opt_time || opt_frames || opt_continuous)
297 run_session();
298 else if (opt_list_serial)
299 show_serial_ports();
300 else
301 show_help();
303 #ifdef HAVE_SRD
304 if (opt_pds)
305 show_pd_close();
306 if (opt_pds)
307 srd_exit();
308 #endif
310 done:
311 if (sr_ctx)
312 sr_exit(sr_ctx);
314 return 0;