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