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 "sigrok-cli.h"
24 struct sr_context *sr_ctx = NULL;
26 struct srd_session *srd_sess = NULL;
29 static void logger(const gchar *log_domain, GLogLevelFlags log_level,
30 const gchar *message, gpointer cb_data)
36 * All messages, warnings, errors etc. go to stderr (not stdout) in
37 * order to not mess up the CLI tool data output, e.g. VCD output.
39 if (log_level & (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING)
40 || opt_loglevel > SR_LOG_WARN) {
41 fprintf(stderr, "%s\n", message);
45 if (log_level & (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL))
50 int select_channels(struct sr_dev_inst *sdi)
52 struct sr_channel *ch;
54 GSList *selected_channels, *l, *channels;
56 channels = sr_dev_inst_channels_get(sdi);
59 if (!(selected_channels = parse_channelstring(sdi, opt_channels)))
62 for (l = channels; l; l = l->next) {
64 enabled = (g_slist_find(selected_channels, ch) != NULL);
65 if (sr_dev_channel_enable(ch, enabled) != SR_OK)
68 g_slist_free(selected_channels);
76 gboolean config_key_has_cap(struct sr_dev_driver *driver,
77 const struct sr_dev_inst *sdi, struct sr_channel_group *cg,
78 uint32_t key, uint32_t capability)
85 if (sr_config_list(driver, sdi, cg, SR_CONF_DEVICE_OPTIONS,
89 opts = g_variant_get_fixed_array(gvar_opts, &num_opts, sizeof(uint32_t));
91 for (i = 0; i < num_opts; i++) {
92 if ((opts[i] & SR_CONF_MASK) == key) {
93 if ((opts[i] & capability) == capability)
100 g_variant_unref(gvar_opts);
105 int maybe_config_get(struct sr_dev_driver *driver,
106 const struct sr_dev_inst *sdi, struct sr_channel_group *cg,
107 uint32_t key, GVariant **gvar)
109 if (config_key_has_cap(driver, sdi, cg, key, SR_CONF_GET))
110 return sr_config_get(driver, sdi, cg, key, gvar);
115 int maybe_config_set(struct sr_dev_driver *driver,
116 const struct sr_dev_inst *sdi, struct sr_channel_group *cg,
117 uint32_t key, GVariant *gvar)
119 if (config_key_has_cap(driver, sdi, cg, key, SR_CONF_SET))
120 return sr_config_set(sdi, cg, key, gvar);
125 int maybe_config_list(struct sr_dev_driver *driver,
126 const struct sr_dev_inst *sdi, struct sr_channel_group *cg,
127 uint32_t key, GVariant **gvar)
129 if (config_key_has_cap(driver, sdi, cg, key, SR_CONF_LIST))
130 return sr_config_list(driver, sdi, cg, key, gvar);
135 static void get_option(void)
137 struct sr_dev_inst *sdi;
138 struct sr_channel_group *cg;
139 const struct sr_config_info *ci;
145 struct sr_dev_driver *driver;
147 if (!(devices = device_scan())) {
148 g_critical("No devices found.");
152 g_slist_free(devices);
154 driver = sr_dev_inst_driver_get(sdi);
156 if (sr_dev_open(sdi) != SR_OK) {
157 g_critical("Failed to open device.");
161 cg = select_channel_group(sdi);
162 if (!(ci = sr_config_info_name_get(opt_get)))
163 g_critical("Unknown option '%s'", opt_get);
165 if ((devargs = parse_generic_arg(opt_config, FALSE)))
166 set_dev_options(sdi, devargs);
169 if ((ret = maybe_config_get(driver, sdi, cg, ci->key, &gvar)) != SR_OK)
170 g_critical("Failed to get '%s': %s", opt_get, sr_strerror(ret));
171 s = g_variant_print(gvar, FALSE);
175 g_variant_unref(gvar);
178 g_hash_table_destroy(devargs);
181 static void set_options(void)
183 struct sr_dev_inst *sdi;
188 g_critical("No setting specified.");
192 if (!(devargs = parse_generic_arg(opt_config, FALSE)))
195 if (!(devices = device_scan())) {
196 g_critical("No devices found.");
200 g_slist_free(devices);
202 if (sr_dev_open(sdi) != SR_OK) {
203 g_critical("Failed to open device.");
207 set_dev_options(sdi, devargs);
210 g_hash_table_destroy(devargs);
214 int main(int argc, char **argv)
216 g_log_set_default_handler(logger, NULL);
218 if (parse_options(argc, argv)) {
222 /* Set the loglevel (amount of messages to output) for libsigrok. */
223 if (sr_log_loglevel_set(opt_loglevel) != SR_OK)
226 if (sr_init(&sr_ctx) != SR_OK)
230 /* Set the loglevel (amount of messages to output) for libsigrokdecode. */
231 if (srd_log_loglevel_set(opt_loglevel) != SRD_OK)
235 if (srd_init(NULL) != SRD_OK)
237 if (srd_session_new(&srd_sess) != SRD_OK) {
238 g_critical("Failed to create new decode session.");
241 if (register_pds(opt_pds, opt_pd_annotations) != 0)
243 if (setup_pd_stack(opt_pds, opt_pd_stack, opt_pd_annotations) != 0)
246 /* Only one output type is ever shown. */
248 if (setup_pd_binary(opt_pd_binary) != 0)
250 if (srd_pd_output_callback_add(srd_sess, SRD_OUTPUT_BINARY,
251 show_pd_binary, NULL) != SRD_OK)
253 } else if (opt_pd_meta) {
254 if (setup_pd_meta(opt_pd_meta) != 0)
256 if (srd_pd_output_callback_add(srd_sess, SRD_OUTPUT_META,
257 show_pd_meta, NULL) != SRD_OK)
260 if (opt_pd_annotations)
261 if (setup_pd_annotations(opt_pd_annotations) != 0)
263 if (srd_pd_output_callback_add(srd_sess, SRD_OUTPUT_ANN,
264 show_pd_annotations, NULL) != SRD_OK)
272 else if (opt_input_format && opt_show)
274 else if (opt_output_format && opt_show)
276 else if (opt_transform_module && opt_show)
278 else if (opt_scan_devs)
281 else if (opt_pds && opt_show)
286 else if (opt_input_file)
292 else if (opt_samples || opt_time || opt_frames || opt_continuous)