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