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