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/>.
23 #include "sigrok-cli.h"
25 struct sr_context *sr_ctx = NULL;
27 struct srd_session *srd_sess = NULL;
30 static void logger(const gchar *log_domain, GLogLevelFlags log_level,
31 const gchar *message, gpointer 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);
46 if (log_level & (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL))
51 int select_channels(struct sr_dev_inst *sdi)
53 struct sr_channel *ch;
55 GSList *selected_channels, *l, *channels;
57 channels = sr_dev_inst_channels_get(sdi);
60 if (!(selected_channels = parse_channelstring(sdi, opt_channels)))
63 for (l = channels; l; l = l->next) {
65 enabled = (g_slist_find(selected_channels, ch) != NULL);
66 if (sr_dev_channel_enable(ch, enabled) != SR_OK)
69 g_slist_free(selected_channels);
77 gboolean 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)
86 if (sr_config_list(driver, sdi, cg, SR_CONF_DEVICE_OPTIONS,
90 opts = g_variant_get_fixed_array(gvar_opts, &num_opts, sizeof(uint32_t));
92 for (i = 0; i < num_opts; i++) {
93 if ((opts[i] & SR_CONF_MASK) == key) {
94 if ((opts[i] & capability) == capability)
101 g_variant_unref(gvar_opts);
106 int 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)
110 if (config_key_has_cap(driver, sdi, cg, key, SR_CONF_GET))
111 return sr_config_get(driver, sdi, cg, key, gvar);
116 int 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)
120 if (config_key_has_cap(driver, sdi, cg, key, SR_CONF_SET))
121 return sr_config_set(sdi, cg, key, gvar);
126 int 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)
130 if (config_key_has_cap(driver, sdi, cg, key, SR_CONF_LIST))
131 return sr_config_list(driver, sdi, cg, key, gvar);
136 static void get_option(void)
138 struct sr_dev_inst *sdi;
139 struct sr_channel_group *cg;
140 const struct sr_key_info *ci;
146 struct sr_dev_driver *driver;
148 if (!(devices = device_scan())) {
149 g_critical("No devices found.");
153 g_slist_free(devices);
155 driver = sr_dev_inst_driver_get(sdi);
157 if (sr_dev_open(sdi) != SR_OK) {
158 g_critical("Failed to open device.");
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);
166 if ((devargs = parse_generic_arg(opt_config, FALSE)))
167 set_dev_options(sdi, devargs);
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);
176 g_variant_unref(gvar);
179 g_hash_table_destroy(devargs);
182 static void set_options(void)
184 struct sr_dev_inst *sdi;
189 g_critical("No setting specified.");
193 if (!(devargs = parse_generic_arg(opt_config, FALSE)))
196 if (!(devices = device_scan())) {
197 g_critical("No devices found.");
201 g_slist_free(devices);
203 if (sr_dev_open(sdi) != SR_OK) {
204 g_critical("Failed to open device.");
208 set_dev_options(sdi, devargs);
211 g_hash_table_destroy(devargs);
215 int main(int argc, char **argv)
217 g_log_set_default_handler(logger, NULL);
219 if (parse_options(argc, argv)) {
223 /* Set the loglevel (amount of messages to output) for libsigrok. */
224 if (sr_log_loglevel_set(opt_loglevel) != SR_OK)
227 if (sr_init(&sr_ctx) != SR_OK)
231 /* Set the loglevel (amount of messages to output) for libsigrokdecode. */
232 if (srd_log_loglevel_set(opt_loglevel) != SRD_OK)
236 if (srd_init(NULL) != SRD_OK)
238 if (srd_session_new(&srd_sess) != SRD_OK) {
239 g_critical("Failed to create new decode session.");
242 if (register_pds(opt_pds, opt_pd_annotations) != 0)
244 if (setup_pd_stack(opt_pds, opt_pd_stack, opt_pd_annotations) != 0)
247 /* Only one output type is ever shown. */
249 if (setup_pd_binary(opt_pd_binary) != 0)
251 if (srd_pd_output_callback_add(srd_sess, SRD_OUTPUT_BINARY,
252 show_pd_binary, NULL) != SRD_OK)
254 } else if (opt_pd_meta) {
255 if (setup_pd_meta(opt_pd_meta) != 0)
257 if (srd_pd_output_callback_add(srd_sess, SRD_OUTPUT_META,
258 show_pd_meta, NULL) != SRD_OK)
261 if (opt_pd_annotations)
262 if (setup_pd_annotations(opt_pd_annotations) != 0)
264 if (srd_pd_output_callback_add(srd_sess, SRD_OUTPUT_ANN,
265 show_pd_annotations, NULL) != SRD_OK)
273 else if (opt_input_format && opt_show)
275 else if (opt_output_format && opt_show)
277 else if (opt_transform_module && opt_show)
279 else if (opt_scan_devs)
282 else if (opt_pds && opt_show)
287 else if (opt_input_file)
293 else if (opt_samples || opt_time || opt_frames || opt_continuous)