]>
Commit | Line | Data |
---|---|---|
43e5747a UH |
1 | /* |
2 | * This file is part of the sigrok project. | |
3 | * | |
110aa72a | 4 | * Copyright (C) 2012 Bert Vermeulen <bert@biot.com> |
43e5747a UH |
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 | ||
de0a066e BV |
20 | #include "config.h" |
21 | #ifdef HAVE_SRD | |
43e5747a | 22 | #include <sigrokdecode.h> /* First, so we avoid a _POSIX_C_SOURCE warning. */ |
de0a066e | 23 | #endif |
43e5747a UH |
24 | #include <stdio.h> |
25 | #include <stdlib.h> | |
26 | #include <unistd.h> | |
27 | #include <string.h> | |
28 | #include <time.h> | |
29 | #include <sys/time.h> | |
30 | #include <inttypes.h> | |
31 | #include <sys/types.h> | |
32 | #include <sys/stat.h> | |
33 | #include <errno.h> | |
34 | #include <glib.h> | |
35 | #include <glib/gstdio.h> | |
60ea8937 | 36 | #include <libsigrok/libsigrok.h> |
43e5747a | 37 | #include "sigrok-cli.h" |
43e5747a UH |
38 | |
39 | #define DEFAULT_OUTPUT_FORMAT "bits:width=64" | |
40 | ||
e2fe62cc PS |
41 | static struct sr_context *sr_ctx = NULL; |
42 | ||
1999ae06 | 43 | static uint64_t limit_samples = 0; |
ce48d892 | 44 | static uint64_t limit_frames = 0; |
1999ae06 UH |
45 | static struct sr_output_format *output_format = NULL; |
46 | static int default_output_format = FALSE; | |
47 | static char *output_format_param = NULL; | |
de0a066e | 48 | #ifdef HAVE_SRD |
120f9ee7 | 49 | static GHashTable *pd_ann_visible = NULL; |
de0a066e | 50 | #endif |
c27450ea | 51 | static GByteArray *savebuf; |
43e5747a UH |
52 | |
53 | static gboolean opt_version = FALSE; | |
54 | static gint opt_loglevel = SR_LOG_WARN; /* Show errors+warnings per default. */ | |
1e0f9ed9 | 55 | static gboolean opt_list_devs = FALSE; |
43e5747a UH |
56 | static gboolean opt_wait_trigger = FALSE; |
57 | static gchar *opt_input_file = NULL; | |
58 | static gchar *opt_output_file = NULL; | |
9e2e414f | 59 | static gchar *opt_drv = NULL; |
1e0f9ed9 | 60 | static gchar *opt_dev = NULL; |
43e5747a UH |
61 | static gchar *opt_probes = NULL; |
62 | static gchar *opt_triggers = NULL; | |
63 | static gchar *opt_pds = NULL; | |
de0a066e | 64 | #ifdef HAVE_SRD |
9f4a898e | 65 | static gchar *opt_pd_stack = NULL; |
b6bd032d | 66 | static gchar *opt_pd_annotations = NULL; |
de0a066e | 67 | #endif |
43e5747a | 68 | static gchar *opt_input_format = NULL; |
76ae913d | 69 | static gchar *opt_output_format = NULL; |
22981b2c | 70 | static gchar *opt_show = NULL; |
43e5747a UH |
71 | static gchar *opt_time = NULL; |
72 | static gchar *opt_samples = NULL; | |
ce48d892 | 73 | static gchar *opt_frames = NULL; |
43e5747a | 74 | static gchar *opt_continuous = NULL; |
2d73284e | 75 | static gchar *opt_set = NULL; |
43e5747a UH |
76 | |
77 | static GOptionEntry optargs[] = { | |
03996962 BV |
78 | {"version", 'V', 0, G_OPTION_ARG_NONE, &opt_version, |
79 | "Show version and support list", NULL}, | |
80 | {"loglevel", 'l', 0, G_OPTION_ARG_INT, &opt_loglevel, | |
b64d15a3 | 81 | "Set libsigrok/libsigrokdecode loglevel", NULL}, |
03996962 BV |
82 | {"list-devices", 'D', 0, G_OPTION_ARG_NONE, &opt_list_devs, |
83 | "Scan for devices", NULL}, | |
9e2e414f BV |
84 | {"driver", 0, 0, G_OPTION_ARG_STRING, &opt_drv, |
85 | "Use only this driver", NULL}, | |
03996962 BV |
86 | {"device", 'd', 0, G_OPTION_ARG_STRING, &opt_dev, |
87 | "Use specified device", NULL}, | |
88 | {"input-file", 'i', 0, G_OPTION_ARG_FILENAME, &opt_input_file, | |
89 | "Load input from file", NULL}, | |
90 | {"input-format", 'I', 0, G_OPTION_ARG_STRING, &opt_input_format, | |
91 | "Input format", NULL}, | |
92 | {"output-file", 'o', 0, G_OPTION_ARG_FILENAME, &opt_output_file, | |
93 | "Save output to file", NULL}, | |
94 | {"output-format", 'O', 0, G_OPTION_ARG_STRING, &opt_output_format, | |
95 | "Output format", NULL}, | |
96 | {"probes", 'p', 0, G_OPTION_ARG_STRING, &opt_probes, | |
97 | "Probes to use", NULL}, | |
98 | {"triggers", 't', 0, G_OPTION_ARG_STRING, &opt_triggers, | |
99 | "Trigger configuration", NULL}, | |
100 | {"wait-trigger", 'w', 0, G_OPTION_ARG_NONE, &opt_wait_trigger, | |
101 | "Wait for trigger", NULL}, | |
de0a066e | 102 | #ifdef HAVE_SRD |
03996962 BV |
103 | {"protocol-decoders", 'a', 0, G_OPTION_ARG_STRING, &opt_pds, |
104 | "Protocol decoders to run", NULL}, | |
105 | {"protocol-decoder-stack", 's', 0, G_OPTION_ARG_STRING, &opt_pd_stack, | |
106 | "Protocol decoder stack", NULL}, | |
b6bd032d UH |
107 | {"protocol-decoder-annotations", 'A', 0, G_OPTION_ARG_STRING, &opt_pd_annotations, |
108 | "Protocol decoder annotation(s) to show", NULL}, | |
de0a066e | 109 | #endif |
22981b2c BV |
110 | {"show", 0, 0, G_OPTION_ARG_NONE, &opt_show, |
111 | "Show device detail", NULL}, | |
03996962 BV |
112 | {"time", 0, 0, G_OPTION_ARG_STRING, &opt_time, |
113 | "How long to sample (ms)", NULL}, | |
114 | {"samples", 0, 0, G_OPTION_ARG_STRING, &opt_samples, | |
115 | "Number of samples to acquire", NULL}, | |
ce48d892 BV |
116 | {"frames", 0, 0, G_OPTION_ARG_STRING, &opt_frames, |
117 | "Number of frames to acquire", NULL}, | |
03996962 BV |
118 | {"continuous", 0, 0, G_OPTION_ARG_NONE, &opt_continuous, |
119 | "Sample continuously", NULL}, | |
2d73284e | 120 | {"set", 0, 0, G_OPTION_ARG_NONE, &opt_set, "Set device options only", NULL}, |
43e5747a UH |
121 | {NULL, 0, 0, 0, NULL, NULL, NULL} |
122 | }; | |
123 | ||
9e2e414f | 124 | |
995713f4 | 125 | /* Convert driver options hash to GSList of struct sr_config. */ |
9e2e414f BV |
126 | static GSList *hash_to_hwopt(GHashTable *hash) |
127 | { | |
995713f4 BV |
128 | const struct sr_config_info *srci; |
129 | struct sr_config *src; | |
9e2e414f BV |
130 | GList *gl, *keys; |
131 | GSList *opts; | |
132 | char *key, *value; | |
133 | ||
134 | keys = g_hash_table_get_keys(hash); | |
135 | opts = NULL; | |
136 | for (gl = keys; gl; gl = gl->next) { | |
137 | key = gl->data; | |
74b9bf0c | 138 | if (!(srci = sr_config_info_name_get(key))) { |
9e2e414f BV |
139 | g_critical("Unknown option %s", key); |
140 | return NULL; | |
141 | } | |
995713f4 BV |
142 | src = g_try_malloc(sizeof(struct sr_config)); |
143 | src->key = srci->key; | |
cfd3ec6e | 144 | value = g_hash_table_lookup(hash, key); |
1616f663 | 145 | src->data = g_variant_new_string(value); |
995713f4 | 146 | opts = g_slist_append(opts, src); |
9e2e414f BV |
147 | } |
148 | g_list_free(keys); | |
149 | ||
150 | return opts; | |
151 | } | |
152 | ||
995713f4 | 153 | static void free_drvopts(struct sr_config *src) |
a2853311 | 154 | { |
1616f663 | 155 | g_variant_unref(src->data); |
995713f4 | 156 | g_free(src); |
a2853311 AG |
157 | } |
158 | ||
9e2e414f BV |
159 | static GSList *device_scan(void) |
160 | { | |
161 | struct sr_dev_driver **drivers, *driver; | |
162 | GHashTable *drvargs; | |
163 | GSList *drvopts, *devices, *tmpdevs, *l; | |
164 | int i; | |
165 | char *drvname; | |
166 | ||
167 | if (opt_drv) { | |
168 | drvargs = parse_generic_arg(opt_drv, TRUE); | |
169 | drvname = g_strdup(g_hash_table_lookup(drvargs, "sigrok_key")); | |
170 | g_hash_table_remove(drvargs, "sigrok_key"); | |
171 | driver = NULL; | |
172 | drivers = sr_driver_list(); | |
173 | for (i = 0; drivers[i]; i++) { | |
174 | if (strcmp(drivers[i]->name, drvname)) | |
175 | continue; | |
176 | driver = drivers[i]; | |
177 | } | |
178 | if (!driver) { | |
179 | g_critical("Driver %s not found.", drvname); | |
180 | return NULL; | |
181 | } | |
182 | g_free(drvname); | |
e2fe62cc | 183 | if (sr_driver_init(sr_ctx, driver) != SR_OK) { |
9e2e414f BV |
184 | g_critical("Failed to initialize driver."); |
185 | return NULL; | |
186 | } | |
187 | drvopts = NULL; | |
188 | if (g_hash_table_size(drvargs) > 0) | |
189 | if (!(drvopts = hash_to_hwopt(drvargs))) | |
190 | /* Unknown options, already logged. */ | |
191 | return NULL; | |
192 | devices = sr_driver_scan(driver, drvopts); | |
995713f4 | 193 | g_slist_free_full(drvopts, (GDestroyNotify)free_drvopts); |
9e2e414f BV |
194 | } else { |
195 | /* No driver specified, let them all scan on their own. */ | |
196 | devices = NULL; | |
197 | drivers = sr_driver_list(); | |
198 | for (i = 0; drivers[i]; i++) { | |
199 | driver = drivers[i]; | |
e2fe62cc | 200 | if (sr_driver_init(sr_ctx, driver) != SR_OK) { |
9e2e414f BV |
201 | g_critical("Failed to initialize driver."); |
202 | return NULL; | |
203 | } | |
204 | tmpdevs = sr_driver_scan(driver, NULL); | |
205 | for (l = tmpdevs; l; l = l->next) | |
206 | devices = g_slist_append(devices, l->data); | |
207 | g_slist_free(tmpdevs); | |
208 | } | |
209 | } | |
210 | ||
211 | return devices; | |
212 | } | |
213 | ||
43e5747a UH |
214 | static void show_version(void) |
215 | { | |
a210c4cc | 216 | struct sr_dev_driver **drivers; |
43e5747a UH |
217 | struct sr_input_format **inputs; |
218 | struct sr_output_format **outputs; | |
43e5747a | 219 | int i; |
de0a066e BV |
220 | #ifdef HAVE_SRD |
221 | struct srd_decoder *dec; | |
222 | const GSList *l; | |
223 | #endif | |
43e5747a UH |
224 | |
225 | printf("sigrok-cli %s\n\n", VERSION); | |
15d920a9 | 226 | |
4d167240 UH |
227 | printf("Using libsigrok %s (lib version %s).\n", |
228 | sr_package_version_string_get(), sr_lib_version_string_get()); | |
de0a066e | 229 | #ifdef HAVE_SRD |
4d167240 UH |
230 | printf("Using libsigrokdecode %s (lib version %s).\n\n", |
231 | srd_package_version_string_get(), srd_lib_version_string_get()); | |
de0a066e | 232 | #endif |
4d167240 | 233 | |
43e5747a | 234 | printf("Supported hardware drivers:\n"); |
a214a2eb | 235 | drivers = sr_driver_list(); |
a210c4cc UH |
236 | for (i = 0; drivers[i]; i++) { |
237 | printf(" %-20s %s\n", drivers[i]->name, drivers[i]->longname); | |
43e5747a UH |
238 | } |
239 | printf("\n"); | |
240 | ||
241 | printf("Supported input formats:\n"); | |
242 | inputs = sr_input_list(); | |
243 | for (i = 0; inputs[i]; i++) | |
244 | printf(" %-20s %s\n", inputs[i]->id, inputs[i]->description); | |
245 | printf("\n"); | |
246 | ||
247 | printf("Supported output formats:\n"); | |
248 | outputs = sr_output_list(); | |
249 | for (i = 0; outputs[i]; i++) | |
250 | printf(" %-20s %s\n", outputs[i]->id, outputs[i]->description); | |
251 | printf("\n"); | |
252 | ||
de0a066e | 253 | #ifdef HAVE_SRD |
15d920a9 BV |
254 | if (srd_init(NULL) == SRD_OK) { |
255 | printf("Supported protocol decoders:\n"); | |
1b34803d UH |
256 | srd_decoder_load_all(); |
257 | for (l = srd_decoder_list(); l; l = l->next) { | |
15d920a9 BV |
258 | dec = l->data; |
259 | printf(" %-20s %s\n", dec->id, dec->longname); | |
20622036 UH |
260 | /* Print protocol description upon "-l 3" or higher. */ |
261 | if (opt_loglevel >= SR_LOG_INFO) | |
262 | printf(" %-20s %s\n", "", dec->desc); | |
15d920a9 BV |
263 | } |
264 | srd_exit(); | |
43e5747a UH |
265 | } |
266 | printf("\n"); | |
de0a066e | 267 | #endif |
43e5747a UH |
268 | } |
269 | ||
22981b2c | 270 | static void print_dev_line(const struct sr_dev_inst *sdi) |
43e5747a | 271 | { |
11b62b6f BV |
272 | struct sr_probe *probe; |
273 | GSList *l; | |
43e5747a UH |
274 | |
275 | if (sdi->vendor && sdi->vendor[0]) | |
276 | printf("%s ", sdi->vendor); | |
277 | if (sdi->model && sdi->model[0]) | |
278 | printf("%s ", sdi->model); | |
279 | if (sdi->version && sdi->version[0]) | |
280 | printf("%s ", sdi->version); | |
11b62b6f BV |
281 | if (sdi->probes) { |
282 | if (g_slist_length(sdi->probes) == 1) { | |
283 | probe = sdi->probes->data; | |
4d326ca1 | 284 | printf("with 1 probe: %s", probe->name); |
11b62b6f | 285 | } else { |
4d326ca1 | 286 | printf("with %d probes:", g_slist_length(sdi->probes)); |
11b62b6f BV |
287 | for (l = sdi->probes; l; l = l->next) { |
288 | probe = l->data; | |
4d326ca1 | 289 | printf(" %s", probe->name); |
11b62b6f BV |
290 | } |
291 | } | |
4d326ca1 BV |
292 | } |
293 | printf("\n"); | |
43e5747a UH |
294 | } |
295 | ||
1e0f9ed9 | 296 | static void show_dev_list(void) |
43e5747a | 297 | { |
22981b2c BV |
298 | struct sr_dev_inst *sdi; |
299 | GSList *devices, *l; | |
43e5747a | 300 | |
22981b2c | 301 | if (!(devices = device_scan())) |
43e5747a UH |
302 | return; |
303 | ||
22981b2c BV |
304 | printf("The following devices were found:\n"); |
305 | for (l = devices; l; l = l->next) { | |
306 | sdi = l->data; | |
307 | print_dev_line(sdi); | |
43e5747a | 308 | } |
22981b2c BV |
309 | g_slist_free(devices); |
310 | ||
43e5747a UH |
311 | } |
312 | ||
1e0f9ed9 | 313 | static void show_dev_detail(void) |
43e5747a | 314 | { |
22981b2c | 315 | struct sr_dev_inst *sdi; |
995713f4 | 316 | const struct sr_config_info *srci; |
22981b2c | 317 | GSList *devices; |
1616f663 BV |
318 | GVariant *gvar_opts, *gvar_dict, *gvar_list, *gvar; |
319 | gsize num_opts, num_elements; | |
4e0db44d | 320 | const uint64_t *uint64, p, q; |
e07340ad | 321 | const int32_t *opts; |
1616f663 BV |
322 | unsigned int num_devices, tmp_bool, o, i; |
323 | char *s; | |
9c9c1080 | 324 | const char *charopts, **stropts; |
43e5747a | 325 | |
22981b2c BV |
326 | if (!(devices = device_scan())) { |
327 | g_critical("No devices found."); | |
43e5747a UH |
328 | return; |
329 | } | |
330 | ||
22981b2c BV |
331 | num_devices = g_slist_length(devices); |
332 | if (num_devices > 1) { | |
41d7fb23 BV |
333 | g_critical("%d devices found. Use --list-devices to show them, " |
334 | "and --device to select one.", num_devices); | |
335 | return; | |
336 | } | |
22981b2c | 337 | |
41d7fb23 | 338 | sdi = devices->data; |
22981b2c | 339 | print_dev_line(sdi); |
43e5747a | 340 | |
8e8827f4 BV |
341 | /* This properly opens and initializes the device, so we can get |
342 | * current settings. */ | |
343 | sr_session_new(); | |
344 | if (sr_session_dev_add(sdi) != SR_OK) { | |
345 | g_critical("Failed to use device."); | |
346 | return; | |
347 | } | |
348 | ||
1616f663 BV |
349 | if ((sr_config_list(sdi->driver, SR_CONF_SCAN_OPTIONS, &gvar_opts, |
350 | NULL) == SR_OK)) { | |
351 | opts = g_variant_get_fixed_array(gvar_opts, &num_elements, | |
352 | sizeof(int32_t)); | |
22981b2c | 353 | printf("Supported driver options:\n"); |
1616f663 BV |
354 | for (i = 0; i < num_elements; i++) { |
355 | if (!(srci = sr_config_info_get(opts[i]))) | |
22981b2c | 356 | continue; |
995713f4 | 357 | printf(" %s\n", srci->id); |
22981b2c | 358 | } |
1616f663 | 359 | g_variant_unref(gvar_opts); |
22981b2c BV |
360 | } |
361 | ||
1616f663 BV |
362 | if ((sr_config_list(sdi->driver, SR_CONF_DEVICE_OPTIONS, &gvar_opts, |
363 | sdi) != SR_OK)) | |
22981b2c BV |
364 | /* Driver supports no device instance options. */ |
365 | return; | |
366 | ||
1616f663 BV |
367 | printf("Supported device options:\n"); |
368 | opts = g_variant_get_fixed_array(gvar_opts, &num_opts, sizeof(int32_t)); | |
369 | for (o = 0; o < num_opts; o++) { | |
370 | if (!(srci = sr_config_info_get(opts[o]))) | |
43e5747a UH |
371 | continue; |
372 | ||
1616f663 BV |
373 | if (srci->key == SR_CONF_TRIGGER_TYPE) { |
374 | if (sr_config_list(sdi->driver, srci->key, &gvar, | |
375 | sdi) != SR_OK) { | |
376 | printf("\n"); | |
377 | continue; | |
378 | } | |
379 | charopts = g_variant_get_string(gvar, NULL); | |
380 | printf(" Supported triggers: "); | |
381 | while (*charopts) { | |
382 | printf("%c ", *charopts); | |
383 | charopts++; | |
384 | } | |
385 | printf("\n"); | |
386 | g_variant_unref(gvar); | |
43e5747a | 387 | |
1616f663 | 388 | } else if (srci->key == SR_CONF_PATTERN_MODE) { |
c8db7172 | 389 | /* Pattern generator modes */ |
995713f4 | 390 | printf(" %s", srci->id); |
1616f663 BV |
391 | if (sr_config_list(sdi->driver, srci->key, &gvar, |
392 | sdi) == SR_OK) { | |
0a56f4ec | 393 | printf(" - supported patterns:\n"); |
1e1316b3 BV |
394 | stropts = g_variant_get_strv(gvar, &num_elements); |
395 | for (i = 0; i < num_elements; i++) | |
9c9c1080 | 396 | printf(" %s\n", stropts[i]); |
1616f663 | 397 | g_variant_unref(gvar); |
9c9c1080 | 398 | } else { |
43e5747a | 399 | printf("\n"); |
43e5747a | 400 | } |
c8db7172 | 401 | |
b5c63de9 | 402 | } else if (srci->key == SR_CONF_SAMPLERATE) { |
43e5747a | 403 | /* Supported samplerates */ |
995713f4 | 404 | printf(" %s", srci->id); |
d2f671dc | 405 | if (sr_config_list(sdi->driver, SR_CONF_SAMPLERATE, |
1616f663 | 406 | &gvar_dict, sdi) != SR_OK) { |
43e5747a UH |
407 | printf("\n"); |
408 | continue; | |
409 | } | |
1616f663 BV |
410 | if ((gvar_list = g_variant_lookup_value(gvar_dict, |
411 | "samplerates", G_VARIANT_TYPE("at")))) { | |
4e0db44d | 412 | uint64 = g_variant_get_fixed_array(gvar_list, |
1616f663 BV |
413 | &num_elements, sizeof(uint64_t)); |
414 | printf(" - supported samplerates:\n"); | |
415 | for (i = 0; i < num_elements; i++) | |
4e0db44d | 416 | printf(" %s\n", sr_samplerate_string(uint64[i])); |
1616f663 BV |
417 | } if ((gvar_list = g_variant_lookup_value(gvar_dict, |
418 | "samplerate-steps", G_VARIANT_TYPE("at")))) { | |
4e0db44d | 419 | uint64 = g_variant_get_fixed_array(gvar_list, |
1616f663 | 420 | &num_elements, sizeof(uint64_t)); |
43e5747a | 421 | /* low */ |
4e0db44d | 422 | if (!(s = sr_samplerate_string(uint64[0]))) |
43e5747a UH |
423 | continue; |
424 | printf(" (%s", s); | |
c2c4a0de | 425 | g_free(s); |
43e5747a | 426 | /* high */ |
4e0db44d | 427 | if (!(s = sr_samplerate_string(uint64[1]))) |
43e5747a UH |
428 | continue; |
429 | printf(" - %s", s); | |
c2c4a0de | 430 | g_free(s); |
43e5747a | 431 | /* step */ |
4e0db44d | 432 | if (!(s = sr_samplerate_string(uint64[2]))) |
43e5747a UH |
433 | continue; |
434 | printf(" in steps of %s)\n", s); | |
c2c4a0de | 435 | g_free(s); |
1e1316b3 | 436 | g_variant_unref(gvar_list); |
43e5747a | 437 | } |
1616f663 | 438 | g_variant_unref(gvar_dict); |
c8db7172 | 439 | |
b5c63de9 | 440 | } else if (srci->key == SR_CONF_BUFFERSIZE) { |
c8db7172 | 441 | /* Supported buffer sizes */ |
995713f4 | 442 | printf(" %s", srci->id); |
2777cbd2 | 443 | if (sr_config_list(sdi->driver, SR_CONF_BUFFERSIZE, |
1e1316b3 | 444 | &gvar_list, sdi) != SR_OK) { |
c8db7172 BV |
445 | printf("\n"); |
446 | continue; | |
447 | } | |
4e0db44d | 448 | uint64 = g_variant_get_fixed_array(gvar_list, |
1e1316b3 | 449 | &num_elements, sizeof(uint64_t)); |
c8db7172 | 450 | printf(" - supported buffer sizes:\n"); |
1e1316b3 | 451 | for (i = 0; i < num_elements; i++) |
4e0db44d | 452 | printf(" %"PRIu64"\n", uint64[i]); |
1e1316b3 | 453 | g_variant_unref(gvar_list); |
c8db7172 | 454 | |
b5c63de9 | 455 | } else if (srci->key == SR_CONF_TIMEBASE) { |
c8db7172 | 456 | /* Supported time bases */ |
995713f4 | 457 | printf(" %s", srci->id); |
2bedb9bf | 458 | if (sr_config_list(sdi->driver, SR_CONF_TIMEBASE, |
1e1316b3 | 459 | &gvar_list, sdi) != SR_OK) { |
c8db7172 BV |
460 | printf("\n"); |
461 | continue; | |
462 | } | |
463 | printf(" - supported time bases:\n"); | |
e07340ad BV |
464 | num_elements = g_variant_n_children(gvar_list); |
465 | for (i = 0; i < num_elements; i++) { | |
466 | gvar = g_variant_get_child_value(gvar_list, i); | |
467 | g_variant_get(gvar, "(tt)", &p, &q); | |
468 | s = sr_period_string(p * q); | |
469 | printf(" %s\n", s); | |
470 | g_free(s); | |
471 | } | |
1e1316b3 | 472 | g_variant_unref(gvar_list); |
c8db7172 | 473 | |
b5c63de9 | 474 | } else if (srci->key == SR_CONF_TRIGGER_SOURCE) { |
c8db7172 | 475 | /* Supported trigger sources */ |
995713f4 | 476 | printf(" %s", srci->id); |
d3d7e30a | 477 | if (sr_config_list(sdi->driver, SR_CONF_TRIGGER_SOURCE, |
1e1316b3 | 478 | &gvar, sdi) != SR_OK) { |
c8db7172 BV |
479 | printf("\n"); |
480 | continue; | |
481 | } | |
482 | printf(" - supported trigger sources:\n"); | |
1e1316b3 BV |
483 | stropts = g_variant_get_strv(gvar, &num_elements); |
484 | for (i = 0; i < num_elements; i++) | |
c8db7172 | 485 | printf(" %s\n", stropts[i]); |
1e1316b3 | 486 | g_variant_unref(gvar); |
c8db7172 | 487 | |
b5c63de9 | 488 | } else if (srci->key == SR_CONF_FILTER) { |
e54290bd | 489 | /* Supported filters */ |
995713f4 | 490 | printf(" %s", srci->id); |
b4bd48e9 | 491 | if (sr_config_list(sdi->driver, SR_CONF_FILTER, |
1e1316b3 | 492 | &gvar, sdi) != SR_OK) { |
2850fca1 BV |
493 | printf("\n"); |
494 | continue; | |
495 | } | |
496 | printf(" - supported filter targets:\n"); | |
1e1316b3 BV |
497 | stropts = g_variant_get_strv(gvar, &num_elements); |
498 | for (i = 0; i < num_elements; i++) | |
2850fca1 | 499 | printf(" %s\n", stropts[i]); |
1e1316b3 | 500 | g_variant_unref(gvar); |
2850fca1 | 501 | |
b5c63de9 | 502 | } else if (srci->key == SR_CONF_VDIV) { |
8f3b8464 | 503 | /* Supported volts/div values */ |
995713f4 | 504 | printf(" %s", srci->id); |
9b0b39f6 | 505 | if (sr_config_list(sdi->driver, SR_CONF_VDIV, |
1e1316b3 | 506 | &gvar_list, sdi) != SR_OK) { |
8f3b8464 BV |
507 | printf("\n"); |
508 | continue; | |
509 | } | |
510 | printf(" - supported volts/div:\n"); | |
e07340ad BV |
511 | num_elements = g_variant_n_children(gvar_list); |
512 | for (i = 0; i < num_elements; i++) { | |
513 | gvar = g_variant_get_child_value(gvar_list, i); | |
514 | g_variant_get(gvar, "(tt)", &p, &q); | |
515 | s = sr_voltage_string(p, q); | |
516 | printf(" %s\n", s); | |
517 | g_free(s); | |
518 | } | |
1e1316b3 | 519 | g_variant_unref(gvar_list); |
8f3b8464 | 520 | |
b5c63de9 | 521 | } else if (srci->key == SR_CONF_COUPLING) { |
498f9167 | 522 | /* Supported coupling settings */ |
995713f4 | 523 | printf(" %s", srci->id); |
3ea086fc | 524 | if (sr_config_list(sdi->driver, SR_CONF_COUPLING, |
1e1316b3 | 525 | &gvar, sdi) != SR_OK) { |
498f9167 BV |
526 | printf("\n"); |
527 | continue; | |
528 | } | |
529 | printf(" - supported coupling options:\n"); | |
1e1316b3 BV |
530 | stropts = g_variant_get_strv(gvar, &num_elements); |
531 | for (i = 0; i < num_elements; i++) | |
498f9167 | 532 | printf(" %s\n", stropts[i]); |
1e1316b3 | 533 | g_variant_unref(gvar); |
498f9167 | 534 | |
8e8827f4 | 535 | } else if (srci->key == SR_CONF_DATALOG) { |
1e1316b3 | 536 | /* TODO test */ |
8e8827f4 BV |
537 | /* Turning on/off internal data logging. */ |
538 | printf(" %s\t(on/off", srci->id); | |
539 | if (sr_config_get(sdi->driver, SR_CONF_DATALOG, | |
1616f663 BV |
540 | &gvar, sdi) == SR_OK) { |
541 | tmp_bool = g_variant_get_boolean(gvar); | |
542 | printf(", currently %s", tmp_bool ? "on" : "off"); | |
1e1316b3 | 543 | g_variant_unref(gvar); |
8e8827f4 BV |
544 | } |
545 | printf(")\n"); | |
43e5747a | 546 | } else { |
c8db7172 | 547 | /* Everything else */ |
995713f4 | 548 | printf(" %s\n", srci->id); |
43e5747a UH |
549 | } |
550 | } | |
1616f663 | 551 | g_variant_unref(gvar_opts); |
22981b2c | 552 | |
8e8827f4 BV |
553 | sr_session_destroy(); |
554 | ||
43e5747a UH |
555 | } |
556 | ||
de0a066e | 557 | #ifdef HAVE_SRD |
71b1ea4e BV |
558 | static void show_pd_detail(void) |
559 | { | |
560 | GSList *l; | |
561 | struct srd_decoder *dec; | |
d142a3f7 BV |
562 | struct srd_decoder_option *o; |
563 | char **pdtokens, **pdtok, *optsep, **ann, *val, *doc; | |
e9ff6974 | 564 | struct srd_probe *p; |
71b1ea4e BV |
565 | |
566 | pdtokens = g_strsplit(opt_pds, ",", -1); | |
567 | for (pdtok = pdtokens; *pdtok; pdtok++) { | |
d142a3f7 BV |
568 | /* Strip options. */ |
569 | if ((optsep = strchr(*pdtok, ':'))) | |
570 | *optsep = '\0'; | |
2358775c | 571 | if (!(dec = srd_decoder_get_by_id(*pdtok))) { |
f8ccd825 | 572 | g_critical("Protocol decoder %s not found.", *pdtok); |
71b1ea4e BV |
573 | return; |
574 | } | |
575 | printf("ID: %s\nName: %s\nLong name: %s\nDescription: %s\n", | |
576 | dec->id, dec->name, dec->longname, dec->desc); | |
577 | printf("License: %s\n", dec->license); | |
e9ff6974 | 578 | printf("Annotations:\n"); |
71b1ea4e | 579 | if (dec->annotations) { |
71b1ea4e BV |
580 | for (l = dec->annotations; l; l = l->next) { |
581 | ann = l->data; | |
582 | printf("- %s\n %s\n", ann[0], ann[1]); | |
583 | } | |
e9ff6974 UH |
584 | } else { |
585 | printf("None.\n"); | |
586 | } | |
e9ff6974 UH |
587 | printf("Required probes:\n"); |
588 | if (dec->probes) { | |
589 | for (l = dec->probes; l; l = l->next) { | |
590 | p = l->data; | |
591 | printf("- %s (%s): %s\n", | |
592 | p->name, p->id, p->desc); | |
593 | } | |
594 | } else { | |
595 | printf("None.\n"); | |
596 | } | |
597 | printf("Optional probes:\n"); | |
598 | if (dec->opt_probes) { | |
599 | for (l = dec->opt_probes; l; l = l->next) { | |
600 | p = l->data; | |
601 | printf("- %s (%s): %s\n", | |
602 | p->name, p->id, p->desc); | |
603 | } | |
604 | } else { | |
605 | printf("None.\n"); | |
71b1ea4e | 606 | } |
d142a3f7 BV |
607 | if (dec->options) { |
608 | printf("Options:\n"); | |
609 | for (l = dec->options; l; l = l->next) { | |
610 | o = l->data; | |
611 | val = g_variant_print(o->def, FALSE); | |
612 | printf("- %s: %s (default %s)\n", o->id, o->desc, val); | |
613 | g_free(val); | |
614 | } | |
615 | } | |
4359a4da | 616 | if ((doc = srd_decoder_doc_get(dec))) { |
e9ff6974 UH |
617 | printf("Documentation:\n%s\n", |
618 | doc[0] == '\n' ? doc + 1 : doc); | |
71b1ea4e BV |
619 | g_free(doc); |
620 | } | |
621 | } | |
622 | ||
623 | g_strfreev(pdtokens); | |
71b1ea4e | 624 | } |
de0a066e | 625 | #endif |
71b1ea4e | 626 | |
c27450ea BV |
627 | static GArray *get_enabled_logic_probes(const struct sr_dev_inst *sdi) |
628 | { | |
629 | struct sr_probe *probe; | |
630 | GArray *probes; | |
631 | GSList *l; | |
632 | ||
633 | probes = g_array_new(FALSE, FALSE, sizeof(int)); | |
634 | for (l = sdi->probes; l; l = l->next) { | |
635 | probe = l->data; | |
636 | if (probe->type != SR_PROBE_LOGIC) | |
637 | continue; | |
638 | if (probe->enabled != TRUE) | |
639 | continue; | |
640 | g_array_append_val(probes, probe->index); | |
641 | } | |
642 | ||
643 | return probes; | |
644 | } | |
645 | ||
37d5ccc1 | 646 | static void datafeed_in(const struct sr_dev_inst *sdi, |
3d905bcc | 647 | const struct sr_datafeed_packet *packet) |
43e5747a | 648 | { |
7e97afa0 | 649 | const struct sr_datafeed_meta *meta; |
c27450ea BV |
650 | const struct sr_datafeed_logic *logic; |
651 | const struct sr_datafeed_analog *analog; | |
7e97afa0 | 652 | struct sr_config *src; |
43e5747a | 653 | static struct sr_output *o = NULL; |
c27450ea | 654 | static GArray *logic_probelist = NULL; |
43e5747a UH |
655 | static uint64_t received_samples = 0; |
656 | static int unitsize = 0; | |
657 | static int triggered = 0; | |
658 | static FILE *outfile = NULL; | |
1616f663 BV |
659 | GSList *l; |
660 | GString *out; | |
661 | GVariant *gvar; | |
c27450ea | 662 | int sample_size, ret; |
1616f663 | 663 | uint64_t samplerate, output_len, filter_out_len; |
8de01efe | 664 | uint8_t *output_buf, *filter_out; |
43e5747a UH |
665 | |
666 | /* If the first packet to come in isn't a header, don't even try. */ | |
667 | if (packet->type != SR_DF_HEADER && o == NULL) | |
668 | return; | |
669 | ||
670 | sample_size = -1; | |
671 | switch (packet->type) { | |
672 | case SR_DF_HEADER: | |
8170b8ea | 673 | g_debug("cli: Received SR_DF_HEADER"); |
43e5747a | 674 | /* Initialize the output module. */ |
c2c4a0de | 675 | if (!(o = g_try_malloc(sizeof(struct sr_output)))) { |
8170b8ea | 676 | g_critical("Output module malloc failed."); |
43e5747a UH |
677 | exit(1); |
678 | } | |
679 | o->format = output_format; | |
37d5ccc1 | 680 | o->sdi = (struct sr_dev_inst *)sdi; |
43e5747a UH |
681 | o->param = output_format_param; |
682 | if (o->format->init) { | |
683 | if (o->format->init(o) != SR_OK) { | |
8170b8ea | 684 | g_critical("Output format initialization failed."); |
43e5747a UH |
685 | exit(1); |
686 | } | |
687 | } | |
c27450ea BV |
688 | |
689 | /* Prepare non-stdout output. */ | |
690 | outfile = stdout; | |
691 | if (opt_output_file) { | |
692 | if (default_output_format) { | |
693 | /* output file is in session format, so we'll | |
694 | * keep a copy of everything as it comes in | |
695 | * and save from there after the session. */ | |
696 | outfile = NULL; | |
697 | savebuf = g_byte_array_new(); | |
698 | } else { | |
699 | /* saving to a file in whatever format was set | |
700 | * with --format, so all we need is a filehandle */ | |
701 | outfile = g_fopen(opt_output_file, "wb"); | |
702 | } | |
703 | } | |
704 | ||
705 | /* Prepare for logic data. */ | |
706 | logic_probelist = get_enabled_logic_probes(sdi); | |
707 | /* How many bytes we need to store the packed samples. */ | |
708 | unitsize = (logic_probelist->len + 7) / 8; | |
709 | ||
de0a066e | 710 | #ifdef HAVE_SRD |
c27450ea | 711 | if (opt_pds && logic_probelist->len) { |
9e0e651f | 712 | if (sr_config_get(sdi->driver, SR_CONF_SAMPLERATE, |
1616f663 | 713 | &gvar, sdi) != SR_OK) { |
c27450ea BV |
714 | g_critical("Unable to initialize protocol " |
715 | "decoders: no samplerate found."); | |
716 | break; | |
717 | } | |
1616f663 BV |
718 | samplerate = g_variant_get_uint64(gvar); |
719 | g_variant_unref(gvar); | |
720 | srd_session_start(logic_probelist->len, unitsize, samplerate); | |
c27450ea | 721 | } |
de0a066e | 722 | #endif |
43e5747a | 723 | break; |
53993299 | 724 | |
7e97afa0 BV |
725 | case SR_DF_META: |
726 | g_debug("cli: received SR_DF_META"); | |
727 | meta = packet->payload; | |
728 | for (l = meta->config; l; l = l->next) { | |
729 | src = l->data; | |
730 | switch (src->key) { | |
b5c63de9 | 731 | case SR_CONF_SAMPLERATE: |
1616f663 BV |
732 | samplerate = g_variant_get_uint64(src->data); |
733 | g_debug("cli: got samplerate %"PRIu64" Hz", samplerate); | |
5570e0f2 BV |
734 | break; |
735 | case SR_CONF_SAMPLE_INTERVAL: | |
1616f663 BV |
736 | samplerate = g_variant_get_uint64(src->data); |
737 | g_debug("cli: got sample interval %"PRIu64" ms", samplerate); | |
7e97afa0 BV |
738 | break; |
739 | default: | |
740 | /* Unknown metadata is not an error. */ | |
741 | break; | |
742 | } | |
743 | } | |
744 | break; | |
745 | ||
43e5747a | 746 | case SR_DF_TRIGGER: |
8170b8ea | 747 | g_debug("cli: received SR_DF_TRIGGER"); |
43e5747a UH |
748 | if (o->format->event) |
749 | o->format->event(o, SR_DF_TRIGGER, &output_buf, | |
750 | &output_len); | |
751 | triggered = 1; | |
752 | break; | |
53993299 | 753 | |
43e5747a UH |
754 | case SR_DF_LOGIC: |
755 | logic = packet->payload; | |
b46c4414 | 756 | g_message("cli: received SR_DF_LOGIC, %"PRIu64" bytes", logic->length); |
53993299 BV |
757 | sample_size = logic->unitsize; |
758 | if (logic->length == 0) | |
759 | break; | |
760 | ||
761 | /* Don't store any samples until triggered. */ | |
762 | if (opt_wait_trigger && !triggered) | |
763 | break; | |
43e5747a | 764 | |
53993299 BV |
765 | if (limit_samples && received_samples >= limit_samples) |
766 | break; | |
43e5747a | 767 | |
53993299 | 768 | ret = sr_filter_probes(sample_size, unitsize, logic_probelist, |
f8ccd825 BV |
769 | logic->data, logic->length, |
770 | &filter_out, &filter_out_len); | |
53993299 BV |
771 | if (ret != SR_OK) |
772 | break; | |
43e5747a | 773 | |
c27450ea | 774 | /* What comes out of the filter is guaranteed to be packed into the |
53993299 BV |
775 | * minimum size needed to support the number of samples at this sample |
776 | * size. however, the driver may have submitted too much -- cut off | |
777 | * the buffer of the last packet according to the sample limit. | |
778 | */ | |
779 | if (limit_samples && (received_samples + logic->length / sample_size > | |
780 | limit_samples * sample_size)) | |
781 | filter_out_len = limit_samples * sample_size - received_samples; | |
43e5747a | 782 | |
c27450ea BV |
783 | if (opt_output_file && default_output_format) { |
784 | /* Saving to a session file. */ | |
785 | g_byte_array_append(savebuf, filter_out, filter_out_len); | |
53993299 | 786 | } else { |
c27450ea | 787 | if (opt_pds) { |
de0a066e | 788 | #ifdef HAVE_SRD |
c27450ea BV |
789 | if (srd_session_send(received_samples, (uint8_t*)filter_out, |
790 | filter_out_len) != SRD_OK) | |
791 | sr_session_stop(); | |
de0a066e | 792 | #endif |
c27450ea BV |
793 | } else { |
794 | output_len = 0; | |
795 | if (o->format->data && packet->type == o->format->df_type) | |
796 | o->format->data(o, filter_out, filter_out_len, | |
797 | &output_buf, &output_len); | |
a172e2a0 | 798 | if (output_len) { |
c27450ea BV |
799 | fwrite(output_buf, 1, output_len, outfile); |
800 | fflush(outfile); | |
801 | g_free(output_buf); | |
802 | } | |
53993299 BV |
803 | } |
804 | } | |
53993299 | 805 | g_free(filter_out); |
43e5747a | 806 | |
c27450ea | 807 | received_samples += logic->length / sample_size; |
53993299 BV |
808 | break; |
809 | ||
810 | case SR_DF_ANALOG: | |
811 | analog = packet->payload; | |
812 | g_message("cli: received SR_DF_ANALOG, %d samples", analog->num_samples); | |
813 | if (analog->num_samples == 0) | |
814 | break; | |
815 | ||
816 | if (limit_samples && received_samples >= limit_samples) | |
817 | break; | |
818 | ||
48f71481 BV |
819 | if (o->format->data && packet->type == o->format->df_type) { |
820 | o->format->data(o, (const uint8_t *)analog->data, | |
821 | analog->num_samples * sizeof(float), | |
822 | &output_buf, &output_len); | |
823 | if (output_buf) { | |
824 | fwrite(output_buf, 1, output_len, outfile); | |
e09810e9 | 825 | fflush(outfile); |
48f71481 | 826 | g_free(output_buf); |
c8db7172 | 827 | } |
53993299 BV |
828 | } |
829 | ||
53993299 | 830 | received_samples += analog->num_samples; |
ce48d892 BV |
831 | break; |
832 | ||
833 | case SR_DF_FRAME_BEGIN: | |
48f71481 BV |
834 | g_debug("cli: received SR_DF_FRAME_BEGIN"); |
835 | if (o->format->event) { | |
836 | o->format->event(o, SR_DF_FRAME_BEGIN, &output_buf, | |
837 | &output_len); | |
838 | if (output_buf) { | |
839 | fwrite(output_buf, 1, output_len, outfile); | |
e09810e9 | 840 | fflush(outfile); |
48f71481 BV |
841 | g_free(output_buf); |
842 | } | |
843 | } | |
ce48d892 BV |
844 | break; |
845 | ||
846 | case SR_DF_FRAME_END: | |
48f71481 BV |
847 | g_debug("cli: received SR_DF_FRAME_END"); |
848 | if (o->format->event) { | |
849 | o->format->event(o, SR_DF_FRAME_END, &output_buf, | |
850 | &output_len); | |
851 | if (output_buf) { | |
852 | fwrite(output_buf, 1, output_len, outfile); | |
e09810e9 | 853 | fflush(outfile); |
48f71481 BV |
854 | g_free(output_buf); |
855 | } | |
856 | } | |
ce48d892 | 857 | break; |
53993299 BV |
858 | |
859 | default: | |
c27450ea | 860 | break; |
43e5747a UH |
861 | } |
862 | ||
05e7fa2c BV |
863 | if (o && o->format->recv) { |
864 | out = o->format->recv(o, sdi, packet); | |
865 | if (out && out->len) { | |
866 | fwrite(out->str, 1, out->len, outfile); | |
867 | fflush(outfile); | |
868 | } | |
869 | } | |
870 | ||
bea29a44 BV |
871 | /* SR_DF_END needs to be handled after the output module's recv() |
872 | * is called, so it can properly clean up that module etc. */ | |
873 | if (packet->type == SR_DF_END) { | |
874 | g_debug("cli: Received SR_DF_END"); | |
875 | ||
876 | if (o->format->event) { | |
877 | o->format->event(o, SR_DF_END, &output_buf, &output_len); | |
878 | if (output_buf) { | |
879 | if (outfile) | |
880 | fwrite(output_buf, 1, output_len, outfile); | |
881 | g_free(output_buf); | |
882 | output_len = 0; | |
883 | } | |
884 | } | |
885 | ||
886 | if (limit_samples && received_samples < limit_samples) | |
887 | g_warning("Device only sent %" PRIu64 " samples.", | |
888 | received_samples); | |
889 | ||
890 | if (opt_continuous) | |
891 | g_warning("Device stopped after %" PRIu64 " samples.", | |
892 | received_samples); | |
893 | ||
894 | g_array_free(logic_probelist, TRUE); | |
895 | ||
896 | if (o->format->cleanup) | |
897 | o->format->cleanup(o); | |
898 | g_free(o); | |
899 | o = NULL; | |
900 | ||
901 | if (outfile && outfile != stdout) | |
902 | fclose(outfile); | |
903 | ||
904 | if (opt_output_file && default_output_format) { | |
905 | if (sr_session_save(opt_output_file, sdi, savebuf->data, | |
906 | unitsize, savebuf->len / unitsize) != SR_OK) | |
907 | g_critical("Failed to save session."); | |
908 | g_byte_array_free(savebuf, FALSE); | |
909 | } | |
910 | } | |
911 | ||
43e5747a UH |
912 | } |
913 | ||
de0a066e | 914 | #ifdef HAVE_SRD |
d142a3f7 BV |
915 | static int opts_to_gvar(struct srd_decoder *dec, GHashTable *hash, |
916 | GHashTable **options) | |
917 | { | |
918 | struct srd_decoder_option *o; | |
919 | GSList *optl; | |
920 | GVariant *gvar; | |
921 | gint64 val_int; | |
922 | int ret; | |
923 | char *val_str, *conv; | |
924 | ||
925 | ret = TRUE; | |
926 | *options = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, | |
927 | (GDestroyNotify)g_variant_unref); | |
928 | ||
929 | for (optl = dec->options; optl; optl = optl->next) { | |
930 | o = optl->data; | |
931 | if (!(val_str = g_hash_table_lookup(hash, o->id))) | |
932 | /* Not specified. */ | |
933 | continue; | |
934 | if (g_variant_is_of_type(o->def, G_VARIANT_TYPE_STRING)) { | |
935 | gvar = g_variant_new_string(val_str); | |
936 | } else if (g_variant_is_of_type(o->def, G_VARIANT_TYPE_INT64)) { | |
937 | val_int = strtoll(val_str, &conv, 10); | |
938 | if (!conv || conv == val_str) { | |
939 | g_critical("Protocol decoder '%s' option '%s' " | |
940 | "requires a number.", dec->name, o->id); | |
941 | ret = FALSE; | |
942 | break; | |
943 | } | |
944 | gvar = g_variant_new_int64(val_int); | |
945 | } else { | |
946 | g_critical("Unsupported type for option '%s' (%s)", | |
947 | o->id, g_variant_get_type_string(o->def)); | |
948 | ret = FALSE; | |
949 | break; | |
950 | } | |
951 | g_variant_ref_sink(gvar); | |
952 | g_hash_table_insert(*options, g_strdup(o->id), gvar); | |
953 | g_hash_table_remove(hash, o->id); | |
954 | } | |
955 | ||
956 | return ret; | |
957 | } | |
958 | ||
959 | static int probes_to_gvar(struct srd_decoder *dec, GHashTable *hash, | |
960 | GHashTable **probes) | |
961 | { | |
962 | struct srd_probe *p; | |
963 | GSList *all_probes, *l; | |
964 | GVariant *gvar; | |
965 | gint32 val_int; | |
966 | int ret; | |
967 | char *val_str, *conv; | |
968 | ||
969 | ret = TRUE; | |
970 | *probes = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, | |
971 | (GDestroyNotify)g_variant_unref); | |
972 | ||
973 | all_probes = g_slist_copy(dec->probes); | |
974 | all_probes = g_slist_concat(all_probes, dec->opt_probes); | |
975 | for (l = all_probes; l; l = l->next) { | |
976 | p = l->data; | |
977 | if (!(val_str = g_hash_table_lookup(hash, p->id))) | |
978 | /* Not specified. */ | |
979 | continue; | |
980 | val_int = strtoll(val_str, &conv, 10); | |
981 | if (!conv || conv == val_str) { | |
982 | g_critical("Protocol decoder '%s' probes '%s' " | |
983 | "is not a number.", dec->name, p->id); | |
984 | ret = FALSE; | |
985 | break; | |
986 | } | |
987 | gvar = g_variant_new_int32(val_int); | |
988 | g_variant_ref_sink(gvar); | |
989 | g_hash_table_insert(*probes, g_strdup(p->id), gvar); | |
990 | g_hash_table_remove(hash, p->id); | |
991 | } | |
992 | g_slist_free(all_probes); | |
993 | ||
994 | return ret; | |
995 | } | |
996 | ||
9f4a898e BV |
997 | /* Register the given PDs for this session. |
998 | * Accepts a string of the form: "spi:sck=3:sdata=4,spi:sck=3:sdata=5" | |
43e5747a UH |
999 | * That will instantiate two SPI decoders on the clock but different data |
1000 | * lines. | |
1001 | */ | |
1e0f9ed9 | 1002 | static int register_pds(struct sr_dev *dev, const char *pdstring) |
43e5747a | 1003 | { |
d142a3f7 BV |
1004 | struct srd_decoder *dec; |
1005 | GHashTable *pd_opthash, *options, *probes; | |
1006 | GList *leftover, *l; | |
878e90d9 | 1007 | struct srd_decoder_inst *di; |
445950d3 | 1008 | int ret; |
a1418b73 | 1009 | char **pdtokens, **pdtok, *pd_name; |
43e5747a | 1010 | |
1e0f9ed9 | 1011 | (void)dev; |
43e5747a | 1012 | |
445950d3 | 1013 | ret = 0; |
120f9ee7 BV |
1014 | pd_ann_visible = g_hash_table_new_full(g_str_hash, g_int_equal, |
1015 | g_free, NULL); | |
a1418b73 | 1016 | pd_name = NULL; |
d142a3f7 | 1017 | pd_opthash = options = probes = NULL; |
120f9ee7 | 1018 | pdtokens = g_strsplit(pdstring, ",", 0); |
43e5747a | 1019 | for (pdtok = pdtokens; *pdtok; pdtok++) { |
63bb454c | 1020 | if (!(pd_opthash = parse_generic_arg(*pdtok, TRUE))) { |
8170b8ea | 1021 | g_critical("Invalid protocol decoder option '%s'.", *pdtok); |
d142a3f7 | 1022 | break; |
43e5747a | 1023 | } |
a1418b73 | 1024 | |
9720f23a BV |
1025 | pd_name = g_strdup(g_hash_table_lookup(pd_opthash, "sigrok_key")); |
1026 | g_hash_table_remove(pd_opthash, "sigrok_key"); | |
2358775c | 1027 | if (srd_decoder_load(pd_name) != SRD_OK) { |
8170b8ea | 1028 | g_critical("Failed to load protocol decoder %s.", pd_name); |
445950d3 | 1029 | ret = 1; |
d142a3f7 BV |
1030 | break; |
1031 | } | |
1032 | dec = srd_decoder_get_by_id(pd_name); | |
1033 | ||
1034 | /* Convert decoder option and probe values to GVariant. */ | |
1035 | if (!opts_to_gvar(dec, pd_opthash, &options)) { | |
1036 | ret = 1; | |
1037 | break; | |
1038 | } | |
1039 | if (!probes_to_gvar(dec, pd_opthash, &probes)) { | |
1040 | ret = 1; | |
1041 | break; | |
15d920a9 | 1042 | } |
d142a3f7 BV |
1043 | if (g_hash_table_size(pd_opthash) > 0) { |
1044 | leftover = g_hash_table_get_keys(pd_opthash); | |
1045 | for (l = leftover; l; l = l->next) | |
1046 | g_critical("Unknown option or probe '%s'", (char *)l->data); | |
1047 | g_list_free(leftover); | |
1048 | break; | |
1049 | } | |
1050 | ||
1051 | if (!(di = srd_inst_new(pd_name, options))) { | |
8170b8ea | 1052 | g_critical("Failed to instantiate protocol decoder %s.", pd_name); |
445950d3 | 1053 | ret = 1; |
d142a3f7 | 1054 | break; |
43e5747a | 1055 | } |
120f9ee7 BV |
1056 | |
1057 | /* If no annotation list was specified, add them all in now. | |
1058 | * This will be pared down later to leave only the last PD | |
1059 | * in the stack. | |
1060 | */ | |
b6bd032d UH |
1061 | if (!opt_pd_annotations) |
1062 | g_hash_table_insert(pd_ann_visible, | |
1063 | g_strdup(di->inst_id), NULL); | |
a1418b73 | 1064 | |
d142a3f7 BV |
1065 | /* Remap the probes if needed. */ |
1066 | if (srd_inst_probe_set_all(di, probes) != SRD_OK) { | |
445950d3 | 1067 | ret = 1; |
d142a3f7 | 1068 | break; |
445950d3 | 1069 | } |
67ce0d27 | 1070 | } |
43e5747a | 1071 | |
43e5747a | 1072 | g_strfreev(pdtokens); |
9720f23a BV |
1073 | if (pd_opthash) |
1074 | g_hash_table_destroy(pd_opthash); | |
d142a3f7 BV |
1075 | if (options) |
1076 | g_hash_table_destroy(options); | |
1077 | if (probes) | |
1078 | g_hash_table_destroy(probes); | |
a1418b73 BV |
1079 | if (pd_name) |
1080 | g_free(pd_name); | |
43e5747a | 1081 | |
445950d3 | 1082 | return ret; |
43e5747a UH |
1083 | } |
1084 | ||
120f9ee7 BV |
1085 | int setup_pd_stack(void) |
1086 | { | |
1087 | struct srd_decoder_inst *di_from, *di_to; | |
1088 | int ret, i; | |
41e915a6 | 1089 | char **pds, **ids; |
120f9ee7 BV |
1090 | |
1091 | /* Set up the protocol decoder stack. */ | |
1092 | pds = g_strsplit(opt_pds, ",", 0); | |
1093 | if (g_strv_length(pds) > 1) { | |
1094 | if (opt_pd_stack) { | |
1095 | /* A stack setup was specified, use that. */ | |
1096 | g_strfreev(pds); | |
1097 | pds = g_strsplit(opt_pd_stack, ",", 0); | |
1098 | if (g_strv_length(pds) < 2) { | |
1099 | g_strfreev(pds); | |
1100 | g_critical("Specify at least two protocol decoders to stack."); | |
1101 | return 1; | |
1102 | } | |
1103 | } | |
1104 | ||
41e915a6 BV |
1105 | /* First PD goes at the bottom of the stack. */ |
1106 | ids = g_strsplit(pds[0], ":", 0); | |
1107 | if (!(di_from = srd_inst_find_by_id(ids[0]))) { | |
1108 | g_strfreev(ids); | |
120f9ee7 BV |
1109 | g_critical("Cannot stack protocol decoder '%s': " |
1110 | "instance not found.", pds[0]); | |
1111 | return 1; | |
1112 | } | |
41e915a6 BV |
1113 | g_strfreev(ids); |
1114 | ||
1115 | /* Every subsequent PD goes on top. */ | |
120f9ee7 | 1116 | for (i = 1; pds[i]; i++) { |
41e915a6 BV |
1117 | ids = g_strsplit(pds[i], ":", 0); |
1118 | if (!(di_to = srd_inst_find_by_id(ids[0]))) { | |
1119 | g_strfreev(ids); | |
120f9ee7 BV |
1120 | g_critical("Cannot stack protocol decoder '%s': " |
1121 | "instance not found.", pds[i]); | |
1122 | return 1; | |
1123 | } | |
41e915a6 | 1124 | g_strfreev(ids); |
120f9ee7 BV |
1125 | if ((ret = srd_inst_stack(di_from, di_to)) != SRD_OK) |
1126 | return 1; | |
1127 | ||
1128 | /* Don't show annotation from this PD. Only the last PD in | |
1129 | * the stack will be left on the annotation list (unless | |
1130 | * the annotation list was specifically provided). | |
1131 | */ | |
478a782d | 1132 | if (!opt_pd_annotations) |
b6bd032d UH |
1133 | g_hash_table_remove(pd_ann_visible, |
1134 | di_from->inst_id); | |
120f9ee7 BV |
1135 | |
1136 | di_from = di_to; | |
1137 | } | |
1138 | } | |
1139 | g_strfreev(pds); | |
1140 | ||
1141 | return 0; | |
1142 | } | |
1143 | ||
1144 | int setup_pd_annotations(void) | |
1145 | { | |
1146 | GSList *l; | |
1147 | struct srd_decoder *dec; | |
1148 | int ann; | |
1149 | char **pds, **pdtok, **keyval, **ann_descr; | |
1150 | ||
1151 | /* Set up custom list of PDs and annotations to show. */ | |
b6bd032d UH |
1152 | if (opt_pd_annotations) { |
1153 | pds = g_strsplit(opt_pd_annotations, ",", 0); | |
120f9ee7 BV |
1154 | for (pdtok = pds; *pdtok && **pdtok; pdtok++) { |
1155 | ann = 0; | |
1156 | keyval = g_strsplit(*pdtok, "=", 0); | |
1157 | if (!(dec = srd_decoder_get_by_id(keyval[0]))) { | |
1158 | g_critical("Protocol decoder '%s' not found.", keyval[0]); | |
1159 | return 1; | |
1160 | } | |
1161 | if (!dec->annotations) { | |
1162 | g_critical("Protocol decoder '%s' has no annotations.", keyval[0]); | |
1163 | return 1; | |
1164 | } | |
1165 | if (g_strv_length(keyval) == 2) { | |
1166 | for (l = dec->annotations; l; l = l->next, ann++) { | |
1167 | ann_descr = l->data; | |
1168 | if (!canon_cmp(ann_descr[0], keyval[1])) | |
1169 | /* Found it. */ | |
1170 | break; | |
1171 | } | |
1172 | if (!l) { | |
1173 | g_critical("Annotation '%s' not found " | |
1174 | "for protocol decoder '%s'.", keyval[1], keyval[0]); | |
1175 | return 1; | |
1176 | } | |
1177 | } | |
1178 | g_debug("cli: showing protocol decoder annotation %d from '%s'", ann, keyval[0]); | |
1179 | g_hash_table_insert(pd_ann_visible, g_strdup(keyval[0]), GINT_TO_POINTER(ann)); | |
1180 | g_strfreev(keyval); | |
1181 | } | |
1182 | g_strfreev(pds); | |
1183 | } | |
1184 | ||
1185 | return 0; | |
1186 | } | |
1187 | ||
de0a066e BV |
1188 | void show_pd_annotations(struct srd_proto_data *pdata, void *cb_data) |
1189 | { | |
1190 | int i; | |
1191 | char **annotations; | |
1192 | gpointer ann_format; | |
1193 | ||
1194 | /* 'cb_data' is not used in this specific callback. */ | |
1195 | (void)cb_data; | |
1196 | ||
1197 | if (!pd_ann_visible) | |
1198 | return; | |
1199 | ||
1200 | if (!g_hash_table_lookup_extended(pd_ann_visible, pdata->pdo->di->inst_id, | |
1201 | NULL, &ann_format)) | |
1202 | /* Not in the list of PDs whose annotations we're showing. */ | |
1203 | return; | |
1204 | ||
1205 | if (pdata->ann_format != GPOINTER_TO_INT(ann_format)) | |
1206 | /* We don't want this particular format from the PD. */ | |
1207 | return; | |
1208 | ||
1209 | annotations = pdata->data; | |
1210 | if (opt_loglevel > SR_LOG_WARN) | |
1211 | printf("%"PRIu64"-%"PRIu64" ", pdata->start_sample, pdata->end_sample); | |
1212 | printf("%s: ", pdata->pdo->proto_id); | |
1213 | for (i = 0; annotations[i]; i++) | |
1214 | printf("\"%s\" ", annotations[i]); | |
1215 | printf("\n"); | |
1216 | fflush(stdout); | |
1217 | } | |
1218 | #endif | |
1219 | ||
ad2bc491 BV |
1220 | int setup_output_format(void) |
1221 | { | |
1222 | GHashTable *fmtargs; | |
1223 | GHashTableIter iter; | |
1224 | gpointer key, value; | |
1225 | struct sr_output_format **outputs; | |
1226 | int i; | |
1227 | char *fmtspec; | |
1228 | ||
1229 | if (!opt_output_format) { | |
1230 | opt_output_format = DEFAULT_OUTPUT_FORMAT; | |
1231 | /* we'll need to remember this so when saving to a file | |
1232 | * later, sigrok session format will be used. | |
1233 | */ | |
1234 | default_output_format = TRUE; | |
1235 | } | |
48f71481 | 1236 | |
63bb454c | 1237 | fmtargs = parse_generic_arg(opt_output_format, TRUE); |
ad2bc491 BV |
1238 | fmtspec = g_hash_table_lookup(fmtargs, "sigrok_key"); |
1239 | if (!fmtspec) { | |
1240 | g_critical("Invalid output format."); | |
1241 | return 1; | |
1242 | } | |
1243 | outputs = sr_output_list(); | |
1244 | for (i = 0; outputs[i]; i++) { | |
1245 | if (strcmp(outputs[i]->id, fmtspec)) | |
1246 | continue; | |
1247 | g_hash_table_remove(fmtargs, "sigrok_key"); | |
1248 | output_format = outputs[i]; | |
1249 | g_hash_table_iter_init(&iter, fmtargs); | |
1250 | while (g_hash_table_iter_next(&iter, &key, &value)) { | |
1251 | /* only supporting one parameter per output module | |
1252 | * for now, and only its value */ | |
1253 | output_format_param = g_strdup(value); | |
1254 | break; | |
1255 | } | |
1256 | break; | |
1257 | } | |
1258 | if (!output_format) { | |
1259 | g_critical("Invalid output format %s.", opt_output_format); | |
1260 | return 1; | |
1261 | } | |
1262 | g_hash_table_destroy(fmtargs); | |
1263 | ||
1264 | return 0; | |
1265 | } | |
1266 | ||
37d5ccc1 | 1267 | static int select_probes(struct sr_dev_inst *sdi) |
43e5747a | 1268 | { |
497f5362 BV |
1269 | struct sr_probe *probe; |
1270 | GSList *selected_probes, *l; | |
43e5747a UH |
1271 | |
1272 | if (!opt_probes) | |
1273 | return SR_OK; | |
1274 | ||
497f5362 | 1275 | if (!(selected_probes = parse_probestring(sdi, opt_probes))) |
43e5747a | 1276 | return SR_ERR; |
43e5747a | 1277 | |
497f5362 BV |
1278 | for (l = sdi->probes; l; l = l->next) { |
1279 | probe = l->data; | |
1280 | if (g_slist_find(selected_probes, probe)) | |
1281 | probe->enabled = TRUE; | |
1282 | else | |
1283 | probe->enabled = FALSE; | |
43e5747a | 1284 | } |
497f5362 | 1285 | g_slist_free(selected_probes); |
43e5747a UH |
1286 | |
1287 | return SR_OK; | |
1288 | } | |
1289 | ||
1290 | /** | |
1291 | * Return the input file format which the CLI tool should use. | |
1292 | * | |
1293 | * If the user specified -I / --input-format, use that one. Otherwise, try to | |
1294 | * autodetect the format as good as possible. Failing that, return NULL. | |
1295 | * | |
1296 | * @param filename The filename of the input file. Must not be NULL. | |
1297 | * @param opt The -I / --input-file option the user specified (or NULL). | |
1298 | * | |
1299 | * @return A pointer to the 'struct sr_input_format' that should be used, | |
1300 | * or NULL if no input format was selected or auto-detected. | |
1301 | */ | |
1302 | static struct sr_input_format *determine_input_file_format( | |
1303 | const char *filename, const char *opt) | |
1304 | { | |
1305 | int i; | |
1306 | struct sr_input_format **inputs; | |
1307 | ||
1308 | /* If there are no input formats, return NULL right away. */ | |
1309 | inputs = sr_input_list(); | |
1310 | if (!inputs) { | |
8170b8ea | 1311 | g_critical("No supported input formats available."); |
43e5747a UH |
1312 | return NULL; |
1313 | } | |
1314 | ||
1315 | /* If the user specified -I / --input-format, use that one. */ | |
1316 | if (opt) { | |
1317 | for (i = 0; inputs[i]; i++) { | |
943d0c08 | 1318 | if (strcasecmp(inputs[i]->id, opt)) |
43e5747a | 1319 | continue; |
8170b8ea BV |
1320 | g_debug("Using user-specified input file format '%s'.", |
1321 | inputs[i]->id); | |
43e5747a UH |
1322 | return inputs[i]; |
1323 | } | |
1324 | ||
1325 | /* The user specified an unknown input format, return NULL. */ | |
8170b8ea | 1326 | g_critical("Error: specified input file format '%s' is " |
943d0c08 | 1327 | "unknown.", opt); |
43e5747a UH |
1328 | return NULL; |
1329 | } | |
1330 | ||
1331 | /* Otherwise, try to find an input module that can handle this file. */ | |
1332 | for (i = 0; inputs[i]; i++) { | |
1333 | if (inputs[i]->format_match(filename)) | |
1334 | break; | |
1335 | } | |
1336 | ||
1337 | /* Return NULL if no input module wanted to touch this. */ | |
1338 | if (!inputs[i]) { | |
8170b8ea | 1339 | g_critical("Error: no matching input module found."); |
43e5747a UH |
1340 | return NULL; |
1341 | } | |
78912cc1 UH |
1342 | |
1343 | g_debug("cli: Autodetected '%s' input format for file '%s'.", | |
1344 | inputs[i]->id, filename); | |
43e5747a | 1345 | |
43e5747a UH |
1346 | return inputs[i]; |
1347 | } | |
1348 | ||
1349 | static void load_input_file_format(void) | |
1350 | { | |
943d0c08 | 1351 | GHashTable *fmtargs = NULL; |
43e5747a UH |
1352 | struct stat st; |
1353 | struct sr_input *in; | |
1354 | struct sr_input_format *input_format; | |
943d0c08 TÅ |
1355 | char *fmtspec = NULL; |
1356 | ||
1357 | if (opt_input_format) { | |
3e8e0c2d | 1358 | fmtargs = parse_generic_arg(opt_input_format, TRUE); |
943d0c08 TÅ |
1359 | fmtspec = g_hash_table_lookup(fmtargs, "sigrok_key"); |
1360 | } | |
43e5747a | 1361 | |
8170b8ea | 1362 | if (!(input_format = determine_input_file_format(opt_input_file, |
943d0c08 | 1363 | fmtspec))) { |
8170b8ea | 1364 | /* The exact cause was already logged. */ |
43e5747a | 1365 | return; |
943d0c08 | 1366 | } |
3e8e0c2d | 1367 | |
943d0c08 TÅ |
1368 | if (fmtargs) |
1369 | g_hash_table_remove(fmtargs, "sigrok_key"); | |
43e5747a UH |
1370 | |
1371 | if (stat(opt_input_file, &st) == -1) { | |
8170b8ea | 1372 | g_critical("Failed to load %s: %s", opt_input_file, |
43e5747a UH |
1373 | strerror(errno)); |
1374 | exit(1); | |
1375 | } | |
1376 | ||
1377 | /* Initialize the input module. */ | |
c2c4a0de | 1378 | if (!(in = g_try_malloc(sizeof(struct sr_input)))) { |
8170b8ea | 1379 | g_critical("Failed to allocate input module."); |
43e5747a UH |
1380 | exit(1); |
1381 | } | |
1382 | in->format = input_format; | |
943d0c08 | 1383 | in->param = fmtargs; |
43e5747a | 1384 | if (in->format->init) { |
41602d35 | 1385 | if (in->format->init(in, opt_input_file) != SR_OK) { |
8170b8ea | 1386 | g_critical("Input format init failed."); |
43e5747a UH |
1387 | exit(1); |
1388 | } | |
1389 | } | |
1390 | ||
37d5ccc1 | 1391 | if (select_probes(in->sdi) > 0) |
c27450ea | 1392 | return; |
43e5747a UH |
1393 | |
1394 | sr_session_new(); | |
1395 | sr_session_datafeed_callback_add(datafeed_in); | |
37d5ccc1 | 1396 | if (sr_session_dev_add(in->sdi) != SR_OK) { |
8170b8ea | 1397 | g_critical("Failed to use device."); |
43e5747a UH |
1398 | sr_session_destroy(); |
1399 | return; | |
1400 | } | |
1401 | ||
1402 | input_format->loadfile(in, opt_input_file); | |
c27450ea | 1403 | |
43e5747a | 1404 | sr_session_destroy(); |
943d0c08 TÅ |
1405 | |
1406 | if (fmtargs) | |
1407 | g_hash_table_destroy(fmtargs); | |
43e5747a UH |
1408 | } |
1409 | ||
1410 | static void load_input_file(void) | |
1411 | { | |
1412 | ||
1413 | if (sr_session_load(opt_input_file) == SR_OK) { | |
1414 | /* sigrok session file */ | |
1415 | sr_session_datafeed_callback_add(datafeed_in); | |
1416 | sr_session_start(); | |
1417 | sr_session_run(); | |
1418 | sr_session_stop(); | |
1419 | } | |
1420 | else { | |
1421 | /* fall back on input modules */ | |
1422 | load_input_file_format(); | |
1423 | } | |
43e5747a UH |
1424 | } |
1425 | ||
37d5ccc1 | 1426 | static int set_dev_options(struct sr_dev_inst *sdi, GHashTable *args) |
43e5747a | 1427 | { |
995713f4 | 1428 | const struct sr_config_info *srci; |
43e5747a UH |
1429 | GHashTableIter iter; |
1430 | gpointer key, value; | |
cfd3ec6e | 1431 | int ret; |
2f50086c | 1432 | double tmp_double; |
bd31fc3f | 1433 | uint64_t tmp_u64, p, q; |
43e5747a | 1434 | gboolean tmp_bool; |
bd31fc3f | 1435 | GVariant *val, *rational[2]; |
43e5747a UH |
1436 | |
1437 | g_hash_table_iter_init(&iter, args); | |
1438 | while (g_hash_table_iter_next(&iter, &key, &value)) { | |
74b9bf0c | 1439 | if (!(srci = sr_config_info_name_get(key))) { |
cfd3ec6e BV |
1440 | g_critical("Unknown device option '%s'.", (char *) key); |
1441 | return SR_ERR; | |
1442 | } | |
1443 | ||
1444 | if ((value == NULL) && | |
995713f4 | 1445 | (srci->datatype != SR_T_BOOL)) { |
cfd3ec6e BV |
1446 | g_critical("Option '%s' needs a value.", (char *)key); |
1447 | return SR_ERR; | |
1448 | } | |
1449 | val = NULL; | |
995713f4 | 1450 | switch (srci->datatype) { |
cfd3ec6e BV |
1451 | case SR_T_UINT64: |
1452 | ret = sr_parse_sizestring(value, &tmp_u64); | |
1453 | if (ret != SR_OK) | |
8f3b8464 | 1454 | break; |
2f50086c | 1455 | val = g_variant_new_uint64(tmp_u64); |
cfd3ec6e BV |
1456 | break; |
1457 | case SR_T_CHAR: | |
2f50086c | 1458 | val = g_variant_new_string(value); |
cfd3ec6e BV |
1459 | break; |
1460 | case SR_T_BOOL: | |
1461 | if (!value) | |
1462 | tmp_bool = TRUE; | |
43e5747a | 1463 | else |
cfd3ec6e | 1464 | tmp_bool = sr_parse_boolstring(value); |
2f50086c | 1465 | val = g_variant_new_boolean(tmp_bool); |
cfd3ec6e BV |
1466 | break; |
1467 | case SR_T_FLOAT: | |
2f50086c BV |
1468 | tmp_double = strtof(value, NULL); |
1469 | val = g_variant_new_double(tmp_double); | |
cfd3ec6e BV |
1470 | break; |
1471 | case SR_T_RATIONAL_PERIOD: | |
bd31fc3f | 1472 | if ((ret = sr_parse_period(value, &p, &q)) != SR_OK) |
cfd3ec6e | 1473 | break; |
bd31fc3f BV |
1474 | rational[0] = g_variant_new_uint64(p); |
1475 | rational[1] = g_variant_new_uint64(q); | |
1476 | val = g_variant_new_tuple(rational, 2); | |
cfd3ec6e BV |
1477 | break; |
1478 | case SR_T_RATIONAL_VOLT: | |
bd31fc3f | 1479 | if ((ret = sr_parse_voltage(value, &p, &q)) != SR_OK) |
43e5747a | 1480 | break; |
bd31fc3f BV |
1481 | rational[0] = g_variant_new_uint64(p); |
1482 | rational[1] = g_variant_new_uint64(q); | |
1483 | val = g_variant_new_tuple(rational, 2); | |
cfd3ec6e BV |
1484 | break; |
1485 | default: | |
1486 | ret = SR_ERR; | |
43e5747a | 1487 | } |
cfd3ec6e | 1488 | if (val) |
c0b9d56b | 1489 | ret = sr_config_set(sdi, srci->key, val); |
cfd3ec6e BV |
1490 | if (ret != SR_OK) { |
1491 | g_critical("Failed to set device option '%s'.", (char *)key); | |
1492 | return ret; | |
43e5747a UH |
1493 | } |
1494 | } | |
1495 | ||
1496 | return SR_OK; | |
1497 | } | |
1498 | ||
2d73284e BV |
1499 | static void set_options(void) |
1500 | { | |
1501 | struct sr_dev_inst *sdi; | |
1502 | GSList *devices; | |
1503 | GHashTable *devargs; | |
1504 | ||
1505 | if (!opt_dev) { | |
1506 | g_critical("No setting specified."); | |
1507 | return; | |
1508 | } | |
1509 | ||
1510 | if (!(devargs = parse_generic_arg(opt_dev, FALSE))) | |
1511 | return; | |
1512 | ||
1513 | if (!(devices = device_scan())) { | |
1514 | g_critical("No devices found."); | |
1515 | return; | |
1516 | } | |
1517 | sdi = devices->data; | |
1518 | ||
1519 | sr_session_new(); | |
1520 | if (sr_session_dev_add(sdi) != SR_OK) { | |
1521 | g_critical("Failed to use device."); | |
1522 | return; | |
1523 | } | |
1524 | ||
1525 | set_dev_options(sdi, devargs); | |
1526 | ||
1527 | sr_session_destroy(); | |
1528 | g_slist_free(devices); | |
1529 | g_hash_table_destroy(devargs); | |
1530 | ||
1531 | } | |
1532 | ||
37d5ccc1 | 1533 | static int set_limit_time(const struct sr_dev_inst *sdi) |
43e5747a | 1534 | { |
1616f663 | 1535 | GVariant *gvar; |
9c9c1080 | 1536 | uint64_t time_msec; |
1616f663 | 1537 | uint64_t samplerate; |
43e5747a | 1538 | |
43c062e6 | 1539 | if (!(time_msec = sr_parse_timestring(opt_time))) { |
37d5ccc1 | 1540 | g_critical("Invalid time '%s'", opt_time); |
37d5ccc1 | 1541 | return SR_ERR; |
43e5747a UH |
1542 | } |
1543 | ||
e4ffb9d6 | 1544 | if (sr_dev_has_option(sdi, SR_CONF_LIMIT_MSEC)) { |
1616f663 BV |
1545 | gvar = g_variant_new_uint64(time_msec); |
1546 | if (sr_config_set(sdi, SR_CONF_LIMIT_MSEC, gvar) != SR_OK) { | |
37d5ccc1 | 1547 | g_critical("Failed to configure time limit."); |
37d5ccc1 | 1548 | return SR_ERR; |
43e5747a | 1549 | } |
43c062e6 BV |
1550 | } else if (sr_dev_has_option(sdi, SR_CONF_SAMPLERATE)) { |
1551 | /* Convert to samples based on the samplerate. */ | |
1616f663 BV |
1552 | sr_config_get(sdi->driver, SR_CONF_SAMPLERATE, &gvar, sdi); |
1553 | samplerate = g_variant_get_uint64(gvar); | |
1554 | g_variant_unref(gvar); | |
1555 | limit_samples = (samplerate) * time_msec / (uint64_t)1000; | |
37d5ccc1 BV |
1556 | if (limit_samples == 0) { |
1557 | g_critical("Not enough time at this samplerate."); | |
37d5ccc1 | 1558 | return SR_ERR; |
43e5747a | 1559 | } |
1616f663 BV |
1560 | gvar = g_variant_new_uint64(limit_samples); |
1561 | if (sr_config_set(sdi, SR_CONF_LIMIT_SAMPLES, gvar) != SR_OK) { | |
37d5ccc1 | 1562 | g_critical("Failed to configure time-based sample limit."); |
37d5ccc1 | 1563 | return SR_ERR; |
43e5747a | 1564 | } |
43c062e6 BV |
1565 | } else { |
1566 | g_critical("This device does not support time limits."); | |
1567 | return SR_ERR; | |
37d5ccc1 | 1568 | } |
43e5747a | 1569 | |
37d5ccc1 BV |
1570 | return SR_OK; |
1571 | } | |
1572 | ||
1573 | static void run_session(void) | |
1574 | { | |
06a3fb10 | 1575 | GSList *devices; |
37d5ccc1 | 1576 | GHashTable *devargs; |
1616f663 | 1577 | GVariant *gvar; |
37d5ccc1 BV |
1578 | struct sr_dev_inst *sdi; |
1579 | int max_probes, i; | |
06a3fb10 BV |
1580 | char **triggerlist; |
1581 | ||
1582 | devices = device_scan(); | |
1583 | if (!devices) { | |
1584 | g_critical("No devices found."); | |
1585 | return; | |
1586 | } | |
1587 | if (g_slist_length(devices) > 1) { | |
1588 | g_critical("sigrok-cli only supports one device for capturing."); | |
1589 | return; | |
1590 | } | |
1591 | sdi = devices->data; | |
37d5ccc1 BV |
1592 | |
1593 | sr_session_new(); | |
1594 | sr_session_datafeed_callback_add(datafeed_in); | |
1595 | ||
8ab6aafc BV |
1596 | if (sr_session_dev_add(sdi) != SR_OK) { |
1597 | g_critical("Failed to use device."); | |
1598 | sr_session_destroy(); | |
1599 | return; | |
1600 | } | |
1601 | ||
06a3fb10 BV |
1602 | if (opt_dev) { |
1603 | if ((devargs = parse_generic_arg(opt_dev, FALSE))) { | |
1604 | if (set_dev_options(sdi, devargs) != SR_OK) | |
1605 | return; | |
1606 | g_hash_table_destroy(devargs); | |
43e5747a | 1607 | } |
06a3fb10 | 1608 | } |
43e5747a | 1609 | |
06a3fb10 BV |
1610 | if (select_probes(sdi) != SR_OK) { |
1611 | g_critical("Failed to set probes."); | |
1612 | sr_session_destroy(); | |
1613 | return; | |
1614 | } | |
1615 | ||
1616 | if (opt_triggers) { | |
1617 | if (!(triggerlist = sr_parse_triggerstring(sdi, opt_triggers))) { | |
43e5747a UH |
1618 | sr_session_destroy(); |
1619 | return; | |
1620 | } | |
06a3fb10 BV |
1621 | max_probes = g_slist_length(sdi->probes); |
1622 | for (i = 0; i < max_probes; i++) { | |
1623 | if (triggerlist[i]) { | |
1624 | sr_dev_trigger_set(sdi, i, triggerlist[i]); | |
1625 | g_free(triggerlist[i]); | |
43e5747a UH |
1626 | } |
1627 | } | |
06a3fb10 BV |
1628 | g_free(triggerlist); |
1629 | } | |
9c9c1080 | 1630 | |
06a3fb10 | 1631 | if (opt_continuous) { |
e4ffb9d6 | 1632 | if (!sr_dev_has_option(sdi, SR_CONF_CONTINUOUS)) { |
06a3fb10 | 1633 | g_critical("This device does not support continuous sampling."); |
37d5ccc1 BV |
1634 | sr_session_destroy(); |
1635 | return; | |
1636 | } | |
37d5ccc1 BV |
1637 | } |
1638 | ||
1639 | if (opt_time) { | |
1640 | if (set_limit_time(sdi) != SR_OK) { | |
1641 | sr_session_destroy(); | |
1642 | return; | |
43e5747a UH |
1643 | } |
1644 | } | |
1645 | ||
1646 | if (opt_samples) { | |
1616f663 BV |
1647 | if ((sr_parse_sizestring(opt_samples, &limit_samples) != SR_OK)) { |
1648 | g_critical("Invalid sample limit '%s'.", opt_samples); | |
1649 | sr_session_destroy(); | |
1650 | return; | |
1651 | } | |
1652 | gvar = g_variant_new_uint64(limit_samples); | |
1653 | if (sr_config_set(sdi, SR_CONF_LIMIT_SAMPLES, gvar) != SR_OK) { | |
8170b8ea | 1654 | g_critical("Failed to configure sample limit."); |
43e5747a UH |
1655 | sr_session_destroy(); |
1656 | return; | |
1657 | } | |
1658 | } | |
1659 | ||
ce48d892 | 1660 | if (opt_frames) { |
1616f663 BV |
1661 | if ((sr_parse_sizestring(opt_frames, &limit_frames) != SR_OK)) { |
1662 | g_critical("Invalid sample limit '%s'.", opt_samples); | |
1663 | sr_session_destroy(); | |
1664 | return; | |
1665 | } | |
2f50086c | 1666 | gvar = g_variant_new_uint64(limit_frames); |
1616f663 | 1667 | if (sr_config_set(sdi, SR_CONF_LIMIT_FRAMES, gvar) != SR_OK) { |
f8ccd825 | 1668 | g_critical("Failed to configure frame limit."); |
ce48d892 BV |
1669 | sr_session_destroy(); |
1670 | return; | |
1671 | } | |
1672 | } | |
1673 | ||
43e5747a | 1674 | if (sr_session_start() != SR_OK) { |
8170b8ea | 1675 | g_critical("Failed to start session."); |
43e5747a UH |
1676 | sr_session_destroy(); |
1677 | return; | |
1678 | } | |
1679 | ||
1680 | if (opt_continuous) | |
1681 | add_anykey(); | |
1682 | ||
1683 | sr_session_run(); | |
1684 | ||
1685 | if (opt_continuous) | |
1686 | clear_anykey(); | |
1687 | ||
43e5747a | 1688 | sr_session_destroy(); |
37d5ccc1 BV |
1689 | g_slist_free(devices); |
1690 | ||
43e5747a UH |
1691 | } |
1692 | ||
1693 | static void logger(const gchar *log_domain, GLogLevelFlags log_level, | |
68cdf174 | 1694 | const gchar *message, gpointer cb_data) |
43e5747a | 1695 | { |
43e5747a | 1696 | (void)log_domain; |
68cdf174 | 1697 | (void)cb_data; |
43e5747a UH |
1698 | |
1699 | /* | |
1700 | * All messages, warnings, errors etc. go to stderr (not stdout) in | |
1701 | * order to not mess up the CLI tool data output, e.g. VCD output. | |
1702 | */ | |
2d6ff326 | 1703 | if (log_level & (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING) |
d740e6ac | 1704 | || opt_loglevel > SR_LOG_WARN) { |
43e5747a UH |
1705 | fprintf(stderr, "%s\n", message); |
1706 | fflush(stderr); | |
43e5747a UH |
1707 | } |
1708 | } | |
1709 | ||
1710 | int main(int argc, char **argv) | |
1711 | { | |
198f4c6c | 1712 | int ret = 1; |
43e5747a UH |
1713 | GOptionContext *context; |
1714 | GError *error; | |
43e5747a UH |
1715 | |
1716 | g_log_set_default_handler(logger, NULL); | |
43e5747a UH |
1717 | |
1718 | error = NULL; | |
1719 | context = g_option_context_new(NULL); | |
1720 | g_option_context_add_main_entries(context, optargs, NULL); | |
1721 | ||
1722 | if (!g_option_context_parse(context, &argc, &argv, &error)) { | |
8170b8ea | 1723 | g_critical("%s", error->message); |
5acb7682 | 1724 | goto done; |
43e5747a UH |
1725 | } |
1726 | ||
1727 | /* Set the loglevel (amount of messages to output) for libsigrok. */ | |
120f9ee7 | 1728 | if (sr_log_loglevel_set(opt_loglevel) != SR_OK) |
5acb7682 | 1729 | goto done; |
43e5747a | 1730 | |
de0a066e | 1731 | if (sr_init(&sr_ctx) != SR_OK) |
5acb7682 | 1732 | goto done; |
6de7ec06 | 1733 | |
de0a066e BV |
1734 | #ifdef HAVE_SRD |
1735 | /* Set the loglevel (amount of messages to output) for libsigrokdecode. */ | |
1736 | if (srd_log_loglevel_set(opt_loglevel) != SRD_OK) | |
5acb7682 | 1737 | goto done; |
43e5747a UH |
1738 | |
1739 | if (opt_pds) { | |
120f9ee7 | 1740 | if (srd_init(NULL) != SRD_OK) |
198f4c6c | 1741 | goto done; |
120f9ee7 | 1742 | if (register_pds(NULL, opt_pds) != 0) |
198f4c6c | 1743 | goto done; |
4359a4da | 1744 | if (srd_pd_output_callback_add(SRD_OUTPUT_ANN, |
b6bd032d | 1745 | show_pd_annotations, NULL) != SRD_OK) |
198f4c6c | 1746 | goto done; |
120f9ee7 | 1747 | if (setup_pd_stack() != 0) |
198f4c6c | 1748 | goto done; |
120f9ee7 | 1749 | if (setup_pd_annotations() != 0) |
198f4c6c | 1750 | goto done; |
9f4a898e | 1751 | } |
de0a066e | 1752 | #endif |
9f4a898e | 1753 | |
ad2bc491 | 1754 | if (setup_output_format() != 0) |
198f4c6c | 1755 | goto done; |
43e5747a UH |
1756 | |
1757 | if (opt_version) | |
1758 | show_version(); | |
1e0f9ed9 UH |
1759 | else if (opt_list_devs) |
1760 | show_dev_list(); | |
de0a066e | 1761 | #ifdef HAVE_SRD |
f83fbc57 BV |
1762 | else if (opt_pds && opt_show) |
1763 | show_pd_detail(); | |
de0a066e | 1764 | #endif |
22981b2c BV |
1765 | else if (opt_show) |
1766 | show_dev_detail(); | |
43e5747a UH |
1767 | else if (opt_input_file) |
1768 | load_input_file(); | |
2d73284e BV |
1769 | else if (opt_set) |
1770 | set_options(); | |
ce48d892 | 1771 | else if (opt_samples || opt_time || opt_frames || opt_continuous) |
43e5747a | 1772 | run_session(); |
43e5747a UH |
1773 | else |
1774 | printf("%s", g_option_context_get_help(context, TRUE, NULL)); | |
1775 | ||
de0a066e | 1776 | #ifdef HAVE_SRD |
43e5747a UH |
1777 | if (opt_pds) |
1778 | srd_exit(); | |
de0a066e | 1779 | #endif |
43e5747a | 1780 | |
198f4c6c PS |
1781 | ret = 0; |
1782 | ||
1783 | done: | |
5acb7682 PS |
1784 | if (sr_ctx) |
1785 | sr_exit(sr_ctx); | |
198f4c6c | 1786 | |
8630d4a8 | 1787 | g_option_context_free(context); |
43e5747a | 1788 | |
198f4c6c | 1789 | return ret; |
43e5747a | 1790 | } |