]> sigrok.org Git - sigrok-cli.git/blame - main.c
Force backup analog traces only if needed
[sigrok-cli.git] / main.c
CommitLineData
2be182e6
BV
1/*
2 * This file is part of the sigrok-cli project.
3 *
4 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
d486cbdd 20#include <config.h>
8d52f788 21#include <stdlib.h>
2be182e6 22#include <glib.h>
662a1e27 23#include "sigrok-cli.h"
2be182e6
BV
24
25struct sr_context *sr_ctx = NULL;
26#ifdef HAVE_SRD
27struct srd_session *srd_sess = NULL;
28#endif
29
2be182e6
BV
30static void logger(const gchar *log_domain, GLogLevelFlags log_level,
31 const gchar *message, gpointer cb_data)
32{
33 (void)log_domain;
34 (void)cb_data;
35
36 /*
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.
39 */
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);
44 }
45
46 if (log_level & (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL))
47 exit(1);
48
49}
50
029d73fe 51int select_channels(struct sr_dev_inst *sdi)
2be182e6 52{
029d73fe 53 struct sr_channel *ch;
abb03685 54 gboolean enabled;
c6fa2b2e
UH
55 GSList *selected_channels, *l, *channels;
56
57 channels = sr_dev_inst_channels_get(sdi);
2be182e6 58
3dfbfbc8
UH
59 if (opt_channels) {
60 if (!(selected_channels = parse_channelstring(sdi, opt_channels)))
39aedc34
DE
61 return SR_ERR;
62
c6fa2b2e 63 for (l = channels; l; l = l->next) {
029d73fe 64 ch = l->data;
abb03685
ML
65 enabled = (g_slist_find(selected_channels, ch) != NULL);
66 if (sr_dev_channel_enable(ch, enabled) != SR_OK)
67 return SR_ERR;
39aedc34 68 }
029d73fe 69 g_slist_free(selected_channels);
2be182e6 70 }
39aedc34 71#ifdef HAVE_SRD
3dfbfbc8 72 map_pd_channels(sdi);
39aedc34 73#endif
2be182e6
BV
74 return SR_OK;
75}
76
24bd9719
BV
77int 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)
80{
09fc39da 81 if (sr_dev_config_capabilities_list(sdi, cg, key) & SR_CONF_GET)
24bd9719
BV
82 return sr_config_get(driver, sdi, cg, key, gvar);
83
84 return SR_ERR_NA;
85}
86
87int 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)
90{
c7639c1d
ML
91 (void)driver;
92
09fc39da 93 if (sr_dev_config_capabilities_list(sdi, cg, key) & SR_CONF_SET)
24bd9719
BV
94 return sr_config_set(sdi, cg, key, gvar);
95
96 return SR_ERR_NA;
97}
98
99int 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)
102{
09fc39da 103 if (sr_dev_config_capabilities_list(sdi, cg, key) & SR_CONF_LIST)
24bd9719
BV
104 return sr_config_list(driver, sdi, cg, key, gvar);
105
106 return SR_ERR_NA;
107}
108
62a64762
BV
109static void get_option(void)
110{
111 struct sr_dev_inst *sdi;
112 struct sr_channel_group *cg;
e4e4b472 113 const struct sr_key_info *ci;
62a64762
BV
114 GSList *devices;
115 GVariant *gvar;
88a195e4 116 GHashTable *devargs;
62a64762
BV
117 int ret;
118 char *s;
c6fa2b2e 119 struct sr_dev_driver *driver;
62a64762
BV
120
121 if (!(devices = device_scan())) {
122 g_critical("No devices found.");
123 return;
124 }
125 sdi = devices->data;
126 g_slist_free(devices);
127
c6fa2b2e
UH
128 driver = sr_dev_inst_driver_get(sdi);
129
62a64762
BV
130 if (sr_dev_open(sdi) != SR_OK) {
131 g_critical("Failed to open device.");
132 return;
133 }
134
135 cg = select_channel_group(sdi);
e4e4b472 136 if (!(ci = sr_key_info_name_get(SR_KEY_CONFIG, opt_get)))
62a64762
BV
137 g_critical("Unknown option '%s'", opt_get);
138
88a195e4
BV
139 if ((devargs = parse_generic_arg(opt_config, FALSE)))
140 set_dev_options(sdi, devargs);
141 else devargs = NULL;
142
24bd9719 143 if ((ret = maybe_config_get(driver, sdi, cg, ci->key, &gvar)) != SR_OK)
62a64762
BV
144 g_critical("Failed to get '%s': %s", opt_get, sr_strerror(ret));
145 s = g_variant_print(gvar, FALSE);
146 printf("%s\n", s);
147 g_free(s);
148
149 g_variant_unref(gvar);
150 sr_dev_close(sdi);
88a195e4
BV
151 if (devargs)
152 g_hash_table_destroy(devargs);
62a64762
BV
153}
154
2be182e6
BV
155static void set_options(void)
156{
157 struct sr_dev_inst *sdi;
158 GSList *devices;
159 GHashTable *devargs;
160
161 if (!opt_config) {
162 g_critical("No setting specified.");
163 return;
164 }
165
166 if (!(devargs = parse_generic_arg(opt_config, FALSE)))
167 return;
168
169 if (!(devices = device_scan())) {
170 g_critical("No devices found.");
171 return;
172 }
173 sdi = devices->data;
b4eece7c 174 g_slist_free(devices);
2be182e6
BV
175
176 if (sr_dev_open(sdi) != SR_OK) {
177 g_critical("Failed to open device.");
178 return;
179 }
180
181 set_dev_options(sdi, devargs);
182
183 sr_dev_close(sdi);
2be182e6
BV
184 g_hash_table_destroy(devargs);
185
186}
187
188int main(int argc, char **argv)
189{
2be182e6
BV
190 g_log_set_default_handler(logger, NULL);
191
cd62e027
JS
192 if (parse_options(argc, argv)) {
193 return 1;
2be182e6
BV
194 }
195
196 /* Set the loglevel (amount of messages to output) for libsigrok. */
197 if (sr_log_loglevel_set(opt_loglevel) != SR_OK)
198 goto done;
199
200 if (sr_init(&sr_ctx) != SR_OK)
201 goto done;
202
203#ifdef HAVE_SRD
204 /* Set the loglevel (amount of messages to output) for libsigrokdecode. */
205 if (srd_log_loglevel_set(opt_loglevel) != SRD_OK)
206 goto done;
207
208 if (opt_pds) {
209 if (srd_init(NULL) != SRD_OK)
210 goto done;
211 if (srd_session_new(&srd_sess) != SRD_OK) {
212 g_critical("Failed to create new decode session.");
213 goto done;
214 }
26c63758 215 if (register_pds(opt_pds, opt_pd_annotations) != 0)
2be182e6 216 goto done;
2be182e6
BV
217
218 /* Only one output type is ever shown. */
219 if (opt_pd_binary) {
26c63758 220 if (setup_pd_binary(opt_pd_binary) != 0)
2be182e6
BV
221 goto done;
222 if (srd_pd_output_callback_add(srd_sess, SRD_OUTPUT_BINARY,
223 show_pd_binary, NULL) != SRD_OK)
224 goto done;
225 } else if (opt_pd_meta) {
26c63758 226 if (setup_pd_meta(opt_pd_meta) != 0)
2be182e6
BV
227 goto done;
228 if (srd_pd_output_callback_add(srd_sess, SRD_OUTPUT_META,
229 show_pd_meta, NULL) != SRD_OK)
230 goto done;
231 } else {
232 if (opt_pd_annotations)
26c63758 233 if (setup_pd_annotations(opt_pd_annotations) != 0)
2be182e6
BV
234 goto done;
235 if (srd_pd_output_callback_add(srd_sess, SRD_OUTPUT_ANN,
236 show_pd_annotations, NULL) != SRD_OK)
237 goto done;
238 }
239 }
240#endif
241
2be182e6
BV
242 if (opt_version)
243 show_version();
6293db8a
UH
244 else if (opt_list_supported)
245 show_supported();
a8b4041a
BV
246 else if (opt_input_format && opt_show)
247 show_input();
ad6520c4
BV
248 else if (opt_output_format && opt_show)
249 show_output();
6f7b4c5d
UH
250 else if (opt_transform_module && opt_show)
251 show_transform();
2be182e6
BV
252 else if (opt_scan_devs)
253 show_dev_list();
254#ifdef HAVE_SRD
255 else if (opt_pds && opt_show)
256 show_pd_detail();
257#endif
258 else if (opt_show)
259 show_dev_detail();
260 else if (opt_input_file)
261 load_input_file();
62a64762
BV
262 else if (opt_get)
263 get_option();
2be182e6
BV
264 else if (opt_set)
265 set_options();
266 else if (opt_samples || opt_time || opt_frames || opt_continuous)
267 run_session();
cd62e027
JS
268 else
269 show_help();
2be182e6
BV
270
271#ifdef HAVE_SRD
272 if (opt_pds)
273 srd_exit();
274#endif
275
2be182e6
BV
276done:
277 if (sr_ctx)
278 sr_exit(sr_ctx);
279
cd62e027 280 return 0;
2be182e6 281}