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