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