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;
53 GSList *selected_channels, *l;
56 if (!(selected_channels = parse_channelstring(sdi, opt_channels)))
59 for (l = sdi->channels; l; l = l->next) {
61 if (g_slist_find(selected_channels, ch))
66 g_slist_free(selected_channels);
74 static void get_option(void)
76 struct sr_dev_inst *sdi;
77 struct sr_channel_group *cg;
78 const struct sr_config_info *ci;
84 if (!(devices = device_scan())) {
85 g_critical("No devices found.");
89 g_slist_free(devices);
91 if (sr_dev_open(sdi) != SR_OK) {
92 g_critical("Failed to open device.");
96 cg = select_channel_group(sdi);
97 if (!(ci = sr_config_info_name_get(opt_get)))
98 g_critical("Unknown option '%s'", opt_get);
100 if ((ret = sr_config_get(sdi->driver, sdi, cg, ci->key, &gvar)) != SR_OK)
101 g_critical("Failed to get '%s': %s", opt_get, sr_strerror(ret));
102 s = g_variant_print(gvar, FALSE);
106 g_variant_unref(gvar);
110 static void set_options(void)
112 struct sr_dev_inst *sdi;
117 g_critical("No setting specified.");
121 if (!(devargs = parse_generic_arg(opt_config, FALSE)))
124 if (!(devices = device_scan())) {
125 g_critical("No devices found.");
129 g_slist_free(devices);
131 if (sr_dev_open(sdi) != SR_OK) {
132 g_critical("Failed to open device.");
136 set_dev_options(sdi, devargs);
139 g_hash_table_destroy(devargs);
143 int main(int argc, char **argv)
145 g_log_set_default_handler(logger, NULL);
147 if (parse_options(argc, argv)) {
151 /* Set the loglevel (amount of messages to output) for libsigrok. */
152 if (sr_log_loglevel_set(opt_loglevel) != SR_OK)
155 if (sr_init(&sr_ctx) != SR_OK)
159 /* Set the loglevel (amount of messages to output) for libsigrokdecode. */
160 if (srd_log_loglevel_set(opt_loglevel) != SRD_OK)
164 if (srd_init(NULL) != SRD_OK)
166 if (srd_session_new(&srd_sess) != SRD_OK) {
167 g_critical("Failed to create new decode session.");
170 if (register_pds(opt_pds, opt_pd_annotations) != 0)
172 if (setup_pd_stack(opt_pds, opt_pd_stack, opt_pd_annotations) != 0)
175 /* Only one output type is ever shown. */
177 if (setup_pd_binary(opt_pd_binary) != 0)
179 if (srd_pd_output_callback_add(srd_sess, SRD_OUTPUT_BINARY,
180 show_pd_binary, NULL) != SRD_OK)
182 } else if (opt_pd_meta) {
183 if (setup_pd_meta(opt_pd_meta) != 0)
185 if (srd_pd_output_callback_add(srd_sess, SRD_OUTPUT_META,
186 show_pd_meta, NULL) != SRD_OK)
189 if (opt_pd_annotations)
190 if (setup_pd_annotations(opt_pd_annotations) != 0)
192 if (srd_pd_output_callback_add(srd_sess, SRD_OUTPUT_ANN,
193 show_pd_annotations, NULL) != SRD_OK)
201 else if (opt_input_format && opt_show)
203 else if (opt_output_format && opt_show)
205 else if (opt_scan_devs)
208 else if (opt_pds && opt_show)
213 else if (opt_input_file)
219 else if (opt_samples || opt_time || opt_frames || opt_continuous)