]>
Commit | Line | Data |
---|---|---|
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 | ||
20fb52e0 | 20 | #include "sigrok-cli.h" |
8d52f788 | 21 | #include <stdlib.h> |
2be182e6 | 22 | #include <glib.h> |
2be182e6 BV |
23 | |
24 | struct sr_context *sr_ctx = NULL; | |
25 | #ifdef HAVE_SRD | |
26 | struct srd_session *srd_sess = NULL; | |
27 | #endif | |
28 | ||
2be182e6 BV |
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 | ||
029d73fe | 50 | int select_channels(struct sr_dev_inst *sdi) |
2be182e6 | 51 | { |
029d73fe UH |
52 | struct sr_channel *ch; |
53 | GSList *selected_channels, *l; | |
2be182e6 | 54 | |
3dfbfbc8 UH |
55 | if (opt_channels) { |
56 | if (!(selected_channels = parse_channelstring(sdi, opt_channels))) | |
39aedc34 DE |
57 | return SR_ERR; |
58 | ||
029d73fe UH |
59 | for (l = sdi->channels; l; l = l->next) { |
60 | ch = l->data; | |
61 | if (g_slist_find(selected_channels, ch)) | |
62 | ch->enabled = TRUE; | |
39aedc34 | 63 | else |
029d73fe | 64 | ch->enabled = FALSE; |
39aedc34 | 65 | } |
029d73fe | 66 | g_slist_free(selected_channels); |
2be182e6 | 67 | } |
39aedc34 | 68 | #ifdef HAVE_SRD |
3dfbfbc8 | 69 | map_pd_channels(sdi); |
39aedc34 | 70 | #endif |
2be182e6 BV |
71 | return SR_OK; |
72 | } | |
73 | ||
62a64762 BV |
74 | static void get_option(void) |
75 | { | |
76 | struct sr_dev_inst *sdi; | |
77 | struct sr_channel_group *cg; | |
78 | const struct sr_config_info *ci; | |
79 | GSList *devices; | |
80 | GVariant *gvar; | |
88a195e4 | 81 | GHashTable *devargs; |
62a64762 BV |
82 | int ret; |
83 | char *s; | |
84 | ||
85 | if (!(devices = device_scan())) { | |
86 | g_critical("No devices found."); | |
87 | return; | |
88 | } | |
89 | sdi = devices->data; | |
90 | g_slist_free(devices); | |
91 | ||
92 | if (sr_dev_open(sdi) != SR_OK) { | |
93 | g_critical("Failed to open device."); | |
94 | return; | |
95 | } | |
96 | ||
97 | cg = select_channel_group(sdi); | |
98 | if (!(ci = sr_config_info_name_get(opt_get))) | |
99 | g_critical("Unknown option '%s'", opt_get); | |
100 | ||
88a195e4 BV |
101 | if ((devargs = parse_generic_arg(opt_config, FALSE))) |
102 | set_dev_options(sdi, devargs); | |
103 | else devargs = NULL; | |
104 | ||
62a64762 BV |
105 | if ((ret = sr_config_get(sdi->driver, sdi, cg, ci->key, &gvar)) != SR_OK) |
106 | g_critical("Failed to get '%s': %s", opt_get, sr_strerror(ret)); | |
107 | s = g_variant_print(gvar, FALSE); | |
108 | printf("%s\n", s); | |
109 | g_free(s); | |
110 | ||
111 | g_variant_unref(gvar); | |
112 | sr_dev_close(sdi); | |
88a195e4 BV |
113 | if (devargs) |
114 | g_hash_table_destroy(devargs); | |
62a64762 BV |
115 | } |
116 | ||
2be182e6 BV |
117 | static void set_options(void) |
118 | { | |
119 | struct sr_dev_inst *sdi; | |
120 | GSList *devices; | |
121 | GHashTable *devargs; | |
122 | ||
123 | if (!opt_config) { | |
124 | g_critical("No setting specified."); | |
125 | return; | |
126 | } | |
127 | ||
128 | if (!(devargs = parse_generic_arg(opt_config, FALSE))) | |
129 | return; | |
130 | ||
131 | if (!(devices = device_scan())) { | |
132 | g_critical("No devices found."); | |
133 | return; | |
134 | } | |
135 | sdi = devices->data; | |
b4eece7c | 136 | g_slist_free(devices); |
2be182e6 BV |
137 | |
138 | if (sr_dev_open(sdi) != SR_OK) { | |
139 | g_critical("Failed to open device."); | |
140 | return; | |
141 | } | |
142 | ||
143 | set_dev_options(sdi, devargs); | |
144 | ||
145 | sr_dev_close(sdi); | |
2be182e6 BV |
146 | g_hash_table_destroy(devargs); |
147 | ||
148 | } | |
149 | ||
150 | int main(int argc, char **argv) | |
151 | { | |
2be182e6 BV |
152 | g_log_set_default_handler(logger, NULL); |
153 | ||
cd62e027 JS |
154 | if (parse_options(argc, argv)) { |
155 | return 1; | |
2be182e6 BV |
156 | } |
157 | ||
158 | /* Set the loglevel (amount of messages to output) for libsigrok. */ | |
159 | if (sr_log_loglevel_set(opt_loglevel) != SR_OK) | |
160 | goto done; | |
161 | ||
162 | if (sr_init(&sr_ctx) != SR_OK) | |
163 | goto done; | |
164 | ||
165 | #ifdef HAVE_SRD | |
166 | /* Set the loglevel (amount of messages to output) for libsigrokdecode. */ | |
167 | if (srd_log_loglevel_set(opt_loglevel) != SRD_OK) | |
168 | goto done; | |
169 | ||
170 | if (opt_pds) { | |
171 | if (srd_init(NULL) != SRD_OK) | |
172 | goto done; | |
173 | if (srd_session_new(&srd_sess) != SRD_OK) { | |
174 | g_critical("Failed to create new decode session."); | |
175 | goto done; | |
176 | } | |
26c63758 | 177 | if (register_pds(opt_pds, opt_pd_annotations) != 0) |
2be182e6 | 178 | goto done; |
26c63758 | 179 | if (setup_pd_stack(opt_pds, opt_pd_stack, opt_pd_annotations) != 0) |
2be182e6 BV |
180 | goto done; |
181 | ||
182 | /* Only one output type is ever shown. */ | |
183 | if (opt_pd_binary) { | |
26c63758 | 184 | if (setup_pd_binary(opt_pd_binary) != 0) |
2be182e6 BV |
185 | goto done; |
186 | if (srd_pd_output_callback_add(srd_sess, SRD_OUTPUT_BINARY, | |
187 | show_pd_binary, NULL) != SRD_OK) | |
188 | goto done; | |
189 | } else if (opt_pd_meta) { | |
26c63758 | 190 | if (setup_pd_meta(opt_pd_meta) != 0) |
2be182e6 BV |
191 | goto done; |
192 | if (srd_pd_output_callback_add(srd_sess, SRD_OUTPUT_META, | |
193 | show_pd_meta, NULL) != SRD_OK) | |
194 | goto done; | |
195 | } else { | |
196 | if (opt_pd_annotations) | |
26c63758 | 197 | if (setup_pd_annotations(opt_pd_annotations) != 0) |
2be182e6 BV |
198 | goto done; |
199 | if (srd_pd_output_callback_add(srd_sess, SRD_OUTPUT_ANN, | |
200 | show_pd_annotations, NULL) != SRD_OK) | |
201 | goto done; | |
202 | } | |
203 | } | |
204 | #endif | |
205 | ||
2be182e6 BV |
206 | if (opt_version) |
207 | show_version(); | |
a8b4041a BV |
208 | else if (opt_input_format && opt_show) |
209 | show_input(); | |
ad6520c4 BV |
210 | else if (opt_output_format && opt_show) |
211 | show_output(); | |
2be182e6 BV |
212 | else if (opt_scan_devs) |
213 | show_dev_list(); | |
214 | #ifdef HAVE_SRD | |
215 | else if (opt_pds && opt_show) | |
216 | show_pd_detail(); | |
217 | #endif | |
218 | else if (opt_show) | |
219 | show_dev_detail(); | |
220 | else if (opt_input_file) | |
221 | load_input_file(); | |
62a64762 BV |
222 | else if (opt_get) |
223 | get_option(); | |
2be182e6 BV |
224 | else if (opt_set) |
225 | set_options(); | |
226 | else if (opt_samples || opt_time || opt_frames || opt_continuous) | |
227 | run_session(); | |
cd62e027 JS |
228 | else |
229 | show_help(); | |
2be182e6 BV |
230 | |
231 | #ifdef HAVE_SRD | |
232 | if (opt_pds) | |
233 | srd_exit(); | |
234 | #endif | |
235 | ||
2be182e6 BV |
236 | done: |
237 | if (sr_ctx) | |
238 | sr_exit(sr_ctx); | |
239 | ||
cd62e027 | 240 | return 0; |
2be182e6 | 241 | } |