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 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);
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)
93 if (sr_dev_config_capabilities_list(sdi, cg, key) & SR_CONF_SET)
94 return sr_config_set(sdi, cg, key, gvar);
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);
109 static void get_option(struct sr_dev_inst *sdi,
110 struct sr_channel_group *cg, const char *opt)
112 struct sr_dev_driver *driver;
113 const struct sr_key_info *ci;
116 const struct sr_key_info *srci, *srmqi, *srmqfi;
118 uint64_t mask, mqflags;
122 driver = sr_dev_inst_driver_get(sdi);
124 ci = sr_key_info_name_get(SR_KEY_CONFIG, opt);
126 g_critical("Unknown option '%s'", opt);
128 ret = maybe_config_get(driver, sdi, cg, ci->key, &gvar);
130 g_critical("Failed to get '%s': %s", opt, sr_strerror(ret));
132 srci = sr_key_info_get(SR_KEY_CONFIG, ci->key);
133 if (srci && srci->datatype == SR_T_MQ) {
134 g_variant_get(gvar, "(ut)", &mq, &mqflags);
135 if ((srmqi = sr_key_info_get(SR_KEY_MQ, mq)))
136 printf("%s", srmqi->id);
139 for (j = 0, mask = 1; j < 32; j++, mask <<= 1) {
140 if (!(mqflags & mask))
142 if ((srmqfi = sr_key_info_get(SR_KEY_MQFLAGS, mqflags & mask)))
143 printf("/%s", srmqfi->id);
145 printf("/%" PRIu64, mqflags & mask);
149 s = g_variant_print(gvar, FALSE);
154 g_variant_unref(gvar);
157 static void get_options(void)
160 struct sr_dev_inst *sdi;
162 char *get_text, *cg_name;
166 struct sr_channel_group *cg;
168 /* Lookup and open the device. */
169 devices = device_scan();
171 g_critical("No devices found.");
175 g_slist_free(devices);
177 if (sr_dev_open(sdi) != SR_OK) {
178 g_critical("Failed to open device.");
182 /* Set configuration values when -c was specified. */
183 set_dev_options_array(sdi, opt_configs);
185 /* Get configuration values which were specified by --get. */
186 for (get_idx = 0; (get_text = opt_gets[get_idx]); get_idx++) {
187 args = parse_generic_arg(get_text, FALSE, "channel_group");
190 cg_name = g_hash_table_lookup(args, "sigrok_key");
191 cg = lookup_channel_group(sdi, cg_name);
192 g_hash_table_iter_init(&iter, args);
193 while (g_hash_table_iter_next(&iter, &key, &value)) {
194 if (g_ascii_strcasecmp(key, "sigrok_key") == 0)
196 get_option(sdi, cg, key);
200 /* Close the device. */
204 static void set_options(void)
206 struct sr_dev_inst *sdi;
210 g_critical("No setting specified.");
214 if (!(devices = device_scan())) {
215 g_critical("No devices found.");
219 g_slist_free(devices);
221 if (sr_dev_open(sdi) != SR_OK) {
222 g_critical("Failed to open device.");
226 set_dev_options_array(sdi, opt_configs);
231 int main(int argc, char **argv)
233 g_log_set_default_handler(logger, NULL);
235 if (parse_options(argc, argv)) {
239 /* Set the loglevel (amount of messages to output) for libsigrok. */
240 if (sr_log_loglevel_set(opt_loglevel) != SR_OK)
243 if (sr_init(&sr_ctx) != SR_OK)
247 if (opt_pd_binary && !opt_pds) {
248 g_critical("Option -B will not take effect in the absence of -P.");
252 /* Set the loglevel (amount of messages to output) for libsigrokdecode. */
253 if (srd_log_loglevel_set(opt_loglevel) != SRD_OK)
257 if (srd_init(NULL) != SRD_OK)
259 if (srd_session_new(&srd_sess) != SRD_OK) {
260 g_critical("Failed to create new decode session.");
263 if (register_pds(opt_pds, opt_pd_annotations) != 0)
266 /* Only one output type is ever shown. */
268 if (setup_pd_binary(opt_pd_binary) != 0)
270 if (setup_binary_stdout() != 0)
272 if (srd_pd_output_callback_add(srd_sess, SRD_OUTPUT_BINARY,
273 show_pd_binary, NULL) != SRD_OK)
275 } else if (opt_pd_meta) {
276 if (setup_pd_meta(opt_pd_meta) != 0)
278 if (srd_pd_output_callback_add(srd_sess, SRD_OUTPUT_META,
279 show_pd_meta, NULL) != SRD_OK)
282 if (opt_pd_annotations)
283 if (setup_pd_annotations(opt_pd_annotations) != 0)
285 if (srd_pd_output_callback_add(srd_sess, SRD_OUTPUT_ANN,
286 show_pd_annotations, NULL) != SRD_OK)
295 else if (opt_list_supported)
297 else if (opt_list_supported_wiki)
298 show_supported_wiki();
299 else if (opt_input_file && opt_show)
300 load_input_file(TRUE);
301 else if (opt_input_format && opt_show)
303 else if (opt_output_format && opt_show)
305 else if (opt_transform_module && opt_show)
307 else if (opt_scan_devs)
310 else if (opt_pds && opt_show)
315 else if (opt_input_file)
316 load_input_file(FALSE);
321 else if (opt_samples || opt_time || opt_frames || opt_continuous)
323 else if (opt_list_serial)