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