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