]> sigrok.org Git - sigrok-cli.git/blob - main.c
1de4b16e8ea0adbaeff9a2e2aebde7daeb1570e5
[sigrok-cli.git] / main.c
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
20 #include <config.h>
21 #include <stdlib.h>
22 #include <glib.h>
23 #include "sigrok-cli.h"
24
25 struct sr_context *sr_ctx = NULL;
26 #ifdef HAVE_SRD
27 struct srd_session *srd_sess = NULL;
28 #endif
29
30 static 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
51 int select_channels(struct sr_dev_inst *sdi)
52 {
53         struct sr_channel *ch;
54         gboolean enabled;
55         GSList *selected_channels, *l, *channels;
56
57         channels = sr_dev_inst_channels_get(sdi);
58
59         if (opt_channels) {
60                 if (!(selected_channels = parse_channelstring(sdi, opt_channels)))
61                         return SR_ERR;
62
63                 for (l = channels; l; l = l->next) {
64                         ch = l->data;
65                         enabled = (g_slist_find(selected_channels, ch) != NULL);
66                         if (sr_dev_channel_enable(ch, enabled) != SR_OK)
67                                 return SR_ERR;
68                 }
69                 g_slist_free(selected_channels);
70         }
71 #ifdef HAVE_SRD
72         map_pd_channels(sdi);
73 #endif
74         return SR_OK;
75 }
76
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)
80 {
81         if (sr_dev_config_capabilities_list(sdi, cg, key) & SR_CONF_GET)
82                 return sr_config_get(driver, sdi, cg, key, gvar);
83
84         return SR_ERR_NA;
85 }
86
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)
90 {
91         (void)driver;
92
93         if (sr_dev_config_capabilities_list(sdi, cg, key) & SR_CONF_SET)
94                 return sr_config_set(sdi, cg, key, gvar);
95
96         return SR_ERR_NA;
97 }
98
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)
102 {
103         if (sr_dev_config_capabilities_list(sdi, cg, key) & SR_CONF_LIST)
104                 return sr_config_list(driver, sdi, cg, key, gvar);
105
106         return SR_ERR_NA;
107 }
108
109 static void get_option(void)
110 {
111         struct sr_dev_inst *sdi;
112         struct sr_channel_group *cg;
113         const struct sr_key_info *ci;
114         GSList *devices;
115         GVariant *gvar;
116         int ret;
117         char *s;
118         struct sr_dev_driver *driver;
119         const struct sr_key_info *srci, *srmqi, *srmqfi;
120         uint32_t mq;
121         uint64_t mask, mqflags;
122         unsigned int j;
123
124         if (!(devices = device_scan())) {
125                 g_critical("No devices found.");
126                 return;
127         }
128         sdi = devices->data;
129         g_slist_free(devices);
130
131         driver = sr_dev_inst_driver_get(sdi);
132
133         if (sr_dev_open(sdi) != SR_OK) {
134                 g_critical("Failed to open device.");
135                 return;
136         }
137
138         cg = lookup_channel_group(sdi, NULL);
139         if (!(ci = sr_key_info_name_get(SR_KEY_CONFIG, opt_get)))
140                 g_critical("Unknown option '%s'", opt_get);
141
142         set_dev_options_array(sdi, opt_configs);
143
144         if ((ret = maybe_config_get(driver, sdi, cg, ci->key, &gvar)) != SR_OK)
145                 g_critical("Failed to get '%s': %s", opt_get, sr_strerror(ret));
146         srci = sr_key_info_get(SR_KEY_CONFIG, ci->key);
147         if (srci && srci->datatype == SR_T_MQ) {
148                 g_variant_get(gvar, "(ut)", &mq, &mqflags);
149                 if ((srmqi = sr_key_info_get(SR_KEY_MQ, mq)))
150                         printf("%s", srmqi->id);
151                 else
152                         printf("%d", mq);
153                 for (j = 0, mask = 1; j < 32; j++, mask <<= 1) {
154                         if (!(mqflags & mask))
155                                 continue;
156                         if ((srmqfi = sr_key_info_get(SR_KEY_MQFLAGS, mqflags & mask)))
157                                 printf("/%s", srmqfi->id);
158                         else
159                                 printf("/%" PRIu64, mqflags & mask);
160                 }
161                 printf("\n");
162         } else {
163                 s = g_variant_print(gvar, FALSE);
164                 printf("%s\n", s);
165                 g_free(s);
166         }
167
168         g_variant_unref(gvar);
169         sr_dev_close(sdi);
170 }
171
172 static void set_options(void)
173 {
174         struct sr_dev_inst *sdi;
175         GSList *devices;
176
177         if (!opt_configs) {
178                 g_critical("No setting specified.");
179                 return;
180         }
181
182         if (!(devices = device_scan())) {
183                 g_critical("No devices found.");
184                 return;
185         }
186         sdi = devices->data;
187         g_slist_free(devices);
188
189         if (sr_dev_open(sdi) != SR_OK) {
190                 g_critical("Failed to open device.");
191                 return;
192         }
193
194         set_dev_options_array(sdi, opt_configs);
195
196         sr_dev_close(sdi);
197 }
198
199 int main(int argc, char **argv)
200 {
201         g_log_set_default_handler(logger, NULL);
202
203         if (parse_options(argc, argv)) {
204                 return 1;
205         }
206
207         /* Set the loglevel (amount of messages to output) for libsigrok. */
208         if (sr_log_loglevel_set(opt_loglevel) != SR_OK)
209                 goto done;
210
211         if (sr_init(&sr_ctx) != SR_OK)
212                 goto done;
213
214 #ifdef HAVE_SRD
215         if (opt_pd_binary && !opt_pds) {
216                 g_critical("Option -B will not take effect in the absence of -P.");
217                 goto done;
218         }
219
220         /* Set the loglevel (amount of messages to output) for libsigrokdecode. */
221         if (srd_log_loglevel_set(opt_loglevel) != SRD_OK)
222                 goto done;
223
224         if (opt_pds) {
225                 if (srd_init(NULL) != SRD_OK)
226                         goto done;
227                 if (srd_session_new(&srd_sess) != SRD_OK) {
228                         g_critical("Failed to create new decode session.");
229                         goto done;
230                 }
231                 if (register_pds(opt_pds, opt_pd_annotations) != 0)
232                         goto done;
233
234                 /* Only one output type is ever shown. */
235                 if (opt_pd_binary) {
236                         if (setup_pd_binary(opt_pd_binary) != 0)
237                                 goto done;
238                         if (setup_binary_stdout() != 0)
239                                 goto done;
240                         if (srd_pd_output_callback_add(srd_sess, SRD_OUTPUT_BINARY,
241                                         show_pd_binary, NULL) != SRD_OK)
242                                 goto done;
243                 } else if (opt_pd_meta) {
244                         if (setup_pd_meta(opt_pd_meta) != 0)
245                                 goto done;
246                         if (srd_pd_output_callback_add(srd_sess, SRD_OUTPUT_META,
247                                         show_pd_meta, NULL) != SRD_OK)
248                                 goto done;
249                 } else {
250                         if (opt_pd_annotations)
251                                 if (setup_pd_annotations(opt_pd_annotations) != 0)
252                                         goto done;
253                         if (srd_pd_output_callback_add(srd_sess, SRD_OUTPUT_ANN,
254                                         show_pd_annotations, NULL) != SRD_OK)
255                                 goto done;
256                 }
257                 show_pd_prepare();
258         }
259 #endif
260
261         if (opt_version)
262                 show_version();
263         else if (opt_list_supported)
264                 show_supported();
265         else if (opt_list_supported_wiki)
266                 show_supported_wiki();
267         else if (opt_input_file && opt_show)
268                 load_input_file(TRUE);
269         else if (opt_input_format && opt_show)
270                 show_input();
271         else if (opt_output_format && opt_show)
272                 show_output();
273         else if (opt_transform_module && opt_show)
274                 show_transform();
275         else if (opt_scan_devs)
276                 show_dev_list();
277 #ifdef HAVE_SRD
278         else if (opt_pds && opt_show)
279                 show_pd_detail();
280 #endif
281         else if (opt_show)
282                 show_dev_detail();
283         else if (opt_input_file)
284                 load_input_file(FALSE);
285         else if (opt_get)
286                 get_option();
287         else if (opt_set)
288                 set_options();
289         else if (opt_samples || opt_time || opt_frames || opt_continuous)
290                 run_session();
291         else if (opt_list_serial)
292                 show_serial_ports();
293         else
294                 show_help();
295
296 #ifdef HAVE_SRD
297         if (opt_pds)
298                 show_pd_close();
299         if (opt_pds)
300                 srd_exit();
301 #endif
302
303 done:
304         if (sr_ctx)
305                 sr_exit(sr_ctx);
306
307         return 0;
308 }