]> sigrok.org Git - sigrok-cli.git/blame - main.c
show: use getter result to determine "current" presence (voltage range)
[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
323368a2
GS
109static void get_option(struct sr_dev_inst *sdi,
110 struct sr_channel_group *cg, const char *opt)
62a64762 111{
323368a2 112 struct sr_dev_driver *driver;
e4e4b472 113 const struct sr_key_info *ci;
62a64762 114 int ret;
323368a2 115 GVariant *gvar;
0bdb08fd
GS
116 const struct sr_key_info *srci, *srmqi, *srmqfi;
117 uint32_t mq;
118 uint64_t mask, mqflags;
119 unsigned int j;
323368a2 120 char *s;
62a64762 121
c6fa2b2e
UH
122 driver = sr_dev_inst_driver_get(sdi);
123
323368a2
GS
124 ci = sr_key_info_name_get(SR_KEY_CONFIG, opt);
125 if (!ci)
126 g_critical("Unknown option '%s'", opt);
62a64762 127
323368a2
GS
128 ret = maybe_config_get(driver, sdi, cg, ci->key, &gvar);
129 if (ret != SR_OK)
130 g_critical("Failed to get '%s': %s", opt, sr_strerror(ret));
62a64762 131
0bdb08fd
GS
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);
137 else
138 printf("%d", mq);
139 for (j = 0, mask = 1; j < 32; j++, mask <<= 1) {
140 if (!(mqflags & mask))
141 continue;
142 if ((srmqfi = sr_key_info_get(SR_KEY_MQFLAGS, mqflags & mask)))
143 printf("/%s", srmqfi->id);
144 else
145 printf("/%" PRIu64, mqflags & mask);
146 }
147 printf("\n");
148 } else {
149 s = g_variant_print(gvar, FALSE);
150 printf("%s\n", s);
151 g_free(s);
152 }
62a64762
BV
153
154 g_variant_unref(gvar);
323368a2
GS
155}
156
157static void get_options(void)
158{
159 GSList *devices;
160 struct sr_dev_inst *sdi;
161 size_t get_idx;
162 char *get_text, *cg_name;
163 GHashTable *args;
164 GHashTableIter iter;
165 gpointer key, value;
166 struct sr_channel_group *cg;
167
168 /* Lookup and open the device. */
169 devices = device_scan();
170 if (!devices) {
171 g_critical("No devices found.");
172 return;
173 }
174 sdi = devices->data;
175 g_slist_free(devices);
176
177 if (sr_dev_open(sdi) != SR_OK) {
178 g_critical("Failed to open device.");
179 return;
180 }
181
182 /* Set configuration values when -c was specified. */
183 set_dev_options_array(sdi, opt_configs);
184
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");
188 if (!args)
189 continue;
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)
195 continue;
196 get_option(sdi, cg, key);
197 }
198 }
199
200 /* Close the device. */
62a64762
BV
201 sr_dev_close(sdi);
202}
203
2be182e6
BV
204static void set_options(void)
205{
206 struct sr_dev_inst *sdi;
207 GSList *devices;
2be182e6 208
6c94f0c1 209 if (!opt_configs) {
2be182e6
BV
210 g_critical("No setting specified.");
211 return;
212 }
213
2be182e6
BV
214 if (!(devices = device_scan())) {
215 g_critical("No devices found.");
216 return;
217 }
218 sdi = devices->data;
b4eece7c 219 g_slist_free(devices);
2be182e6
BV
220
221 if (sr_dev_open(sdi) != SR_OK) {
222 g_critical("Failed to open device.");
223 return;
224 }
225
6c94f0c1 226 set_dev_options_array(sdi, opt_configs);
2be182e6
BV
227
228 sr_dev_close(sdi);
2be182e6
BV
229}
230
231int main(int argc, char **argv)
232{
2be182e6
BV
233 g_log_set_default_handler(logger, NULL);
234
cd62e027
JS
235 if (parse_options(argc, argv)) {
236 return 1;
2be182e6
BV
237 }
238
239 /* Set the loglevel (amount of messages to output) for libsigrok. */
240 if (sr_log_loglevel_set(opt_loglevel) != SR_OK)
241 goto done;
242
243 if (sr_init(&sr_ctx) != SR_OK)
244 goto done;
245
246#ifdef HAVE_SRD
eaa5c966
GS
247 if (opt_pd_binary && !opt_pds) {
248 g_critical("Option -B will not take effect in the absence of -P.");
249 goto done;
250 }
251
2be182e6
BV
252 /* Set the loglevel (amount of messages to output) for libsigrokdecode. */
253 if (srd_log_loglevel_set(opt_loglevel) != SRD_OK)
254 goto done;
255
256 if (opt_pds) {
257 if (srd_init(NULL) != SRD_OK)
258 goto done;
259 if (srd_session_new(&srd_sess) != SRD_OK) {
260 g_critical("Failed to create new decode session.");
261 goto done;
262 }
26c63758 263 if (register_pds(opt_pds, opt_pd_annotations) != 0)
2be182e6 264 goto done;
2be182e6
BV
265
266 /* Only one output type is ever shown. */
267 if (opt_pd_binary) {
26c63758 268 if (setup_pd_binary(opt_pd_binary) != 0)
2be182e6 269 goto done;
e8a9eb8d
DL
270 if (setup_binary_stdout() != 0)
271 goto done;
2be182e6
BV
272 if (srd_pd_output_callback_add(srd_sess, SRD_OUTPUT_BINARY,
273 show_pd_binary, NULL) != SRD_OK)
274 goto done;
275 } else if (opt_pd_meta) {
26c63758 276 if (setup_pd_meta(opt_pd_meta) != 0)
2be182e6
BV
277 goto done;
278 if (srd_pd_output_callback_add(srd_sess, SRD_OUTPUT_META,
279 show_pd_meta, NULL) != SRD_OK)
280 goto done;
281 } else {
282 if (opt_pd_annotations)
26c63758 283 if (setup_pd_annotations(opt_pd_annotations) != 0)
2be182e6
BV
284 goto done;
285 if (srd_pd_output_callback_add(srd_sess, SRD_OUTPUT_ANN,
286 show_pd_annotations, NULL) != SRD_OK)
287 goto done;
288 }
54614916 289 show_pd_prepare();
2be182e6
BV
290 }
291#endif
292
2be182e6
BV
293 if (opt_version)
294 show_version();
6293db8a
UH
295 else if (opt_list_supported)
296 show_supported();
c45dd41c
UH
297 else if (opt_list_supported_wiki)
298 show_supported_wiki();
1c950633
GS
299 else if (opt_input_file && opt_show)
300 load_input_file(TRUE);
a8b4041a
BV
301 else if (opt_input_format && opt_show)
302 show_input();
ad6520c4
BV
303 else if (opt_output_format && opt_show)
304 show_output();
6f7b4c5d
UH
305 else if (opt_transform_module && opt_show)
306 show_transform();
2be182e6
BV
307 else if (opt_scan_devs)
308 show_dev_list();
309#ifdef HAVE_SRD
310 else if (opt_pds && opt_show)
311 show_pd_detail();
312#endif
313 else if (opt_show)
314 show_dev_detail();
315 else if (opt_input_file)
1c950633 316 load_input_file(FALSE);
323368a2
GS
317 else if (opt_gets)
318 get_options();
2be182e6
BV
319 else if (opt_set)
320 set_options();
321 else if (opt_samples || opt_time || opt_frames || opt_continuous)
322 run_session();
56fc0d6d
GS
323 else if (opt_list_serial)
324 show_serial_ports();
cd62e027
JS
325 else
326 show_help();
2be182e6
BV
327
328#ifdef HAVE_SRD
54614916
GS
329 if (opt_pds)
330 show_pd_close();
2be182e6
BV
331 if (opt_pds)
332 srd_exit();
333#endif
334
2be182e6
BV
335done:
336 if (sr_ctx)
337 sr_exit(sr_ctx);
338
cd62e027 339 return 0;
2be182e6 340}