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