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