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