]>
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 UH |
41 | static uint64_t limit_samples = 0; |
42 | static struct sr_output_format *output_format = NULL; | |
43 | static int default_output_format = FALSE; | |
44 | static char *output_format_param = NULL; | |
45 | static char *input_format_param = NULL; | |
9f4a898e | 46 | static GData *pd_ann_visible = NULL; |
43e5747a UH |
47 | |
48 | static gboolean opt_version = FALSE; | |
49 | static gint opt_loglevel = SR_LOG_WARN; /* Show errors+warnings per default. */ | |
1e0f9ed9 | 50 | static gboolean opt_list_devs = FALSE; |
43e5747a UH |
51 | static gboolean opt_wait_trigger = FALSE; |
52 | static gchar *opt_input_file = NULL; | |
53 | static gchar *opt_output_file = NULL; | |
1e0f9ed9 | 54 | static gchar *opt_dev = NULL; |
43e5747a UH |
55 | static gchar *opt_probes = NULL; |
56 | static gchar *opt_triggers = NULL; | |
57 | static gchar *opt_pds = NULL; | |
9f4a898e | 58 | static gchar *opt_pd_stack = NULL; |
43e5747a | 59 | static gchar *opt_input_format = NULL; |
76ae913d | 60 | static gchar *opt_output_format = NULL; |
43e5747a UH |
61 | static gchar *opt_time = NULL; |
62 | static gchar *opt_samples = NULL; | |
63 | static gchar *opt_continuous = NULL; | |
64 | ||
65 | static GOptionEntry optargs[] = { | |
66 | {"version", 'V', 0, G_OPTION_ARG_NONE, &opt_version, "Show version and support list", NULL}, | |
30f1c93f UH |
67 | {"loglevel", 'l', 0, G_OPTION_ARG_INT, &opt_loglevel, "Select libsigrok/libsigrokdecode loglevel", NULL}, |
68 | {"list-devices", 'D', 0, G_OPTION_ARG_NONE, &opt_list_devs, "Scan for devices", NULL}, | |
1b4b6a7c | 69 | {"device", 'd', 0, G_OPTION_ARG_STRING, &opt_dev, "Use specified device", NULL}, |
43e5747a | 70 | {"input-file", 'i', 0, G_OPTION_ARG_FILENAME, &opt_input_file, "Load input from file", NULL}, |
7949dca0 | 71 | {"input-format", 'I', 0, G_OPTION_ARG_STRING, &opt_input_format, "Input format", NULL}, |
43e5747a | 72 | {"output-file", 'o', 0, G_OPTION_ARG_FILENAME, &opt_output_file, "Save output to file", NULL}, |
7949dca0 | 73 | {"output-format", 'O', 0, G_OPTION_ARG_STRING, &opt_output_format, "Output format", NULL}, |
43e5747a UH |
74 | {"probes", 'p', 0, G_OPTION_ARG_STRING, &opt_probes, "Probes to use", NULL}, |
75 | {"triggers", 't', 0, G_OPTION_ARG_STRING, &opt_triggers, "Trigger configuration", NULL}, | |
76 | {"wait-trigger", 'w', 0, G_OPTION_ARG_NONE, &opt_wait_trigger, "Wait for trigger", NULL}, | |
1b4b6a7c | 77 | {"protocol-decoders", 'a', 0, G_OPTION_ARG_STRING, &opt_pds, "Protocol decoders to run", NULL}, |
9f4a898e | 78 | {"protocol-decoder-stack", 's', 0, G_OPTION_ARG_STRING, &opt_pd_stack, "Protocol decoder stack", NULL}, |
43e5747a UH |
79 | {"time", 0, 0, G_OPTION_ARG_STRING, &opt_time, "How long to sample (ms)", NULL}, |
80 | {"samples", 0, 0, G_OPTION_ARG_STRING, &opt_samples, "Number of samples to acquire", NULL}, | |
81 | {"continuous", 0, 0, G_OPTION_ARG_NONE, &opt_continuous, "Sample continuously", NULL}, | |
82 | {NULL, 0, 0, 0, NULL, NULL, NULL} | |
83 | }; | |
84 | ||
85 | static void show_version(void) | |
86 | { | |
bc8e2400 | 87 | GSList *l; |
a210c4cc | 88 | struct sr_dev_driver **drivers; |
43e5747a UH |
89 | struct sr_input_format **inputs; |
90 | struct sr_output_format **outputs; | |
91 | struct srd_decoder *dec; | |
92 | int i; | |
93 | ||
94 | printf("sigrok-cli %s\n\n", VERSION); | |
15d920a9 | 95 | |
4d167240 UH |
96 | printf("Using libsigrok %s (lib version %s).\n", |
97 | sr_package_version_string_get(), sr_lib_version_string_get()); | |
98 | printf("Using libsigrokdecode %s (lib version %s).\n\n", | |
99 | srd_package_version_string_get(), srd_lib_version_string_get()); | |
100 | ||
43e5747a | 101 | printf("Supported hardware drivers:\n"); |
a214a2eb | 102 | drivers = sr_driver_list(); |
a210c4cc UH |
103 | for (i = 0; drivers[i]; i++) { |
104 | printf(" %-20s %s\n", drivers[i]->name, drivers[i]->longname); | |
43e5747a UH |
105 | } |
106 | printf("\n"); | |
107 | ||
108 | printf("Supported input formats:\n"); | |
109 | inputs = sr_input_list(); | |
110 | for (i = 0; inputs[i]; i++) | |
111 | printf(" %-20s %s\n", inputs[i]->id, inputs[i]->description); | |
112 | printf("\n"); | |
113 | ||
114 | printf("Supported output formats:\n"); | |
115 | outputs = sr_output_list(); | |
116 | for (i = 0; outputs[i]; i++) | |
117 | printf(" %-20s %s\n", outputs[i]->id, outputs[i]->description); | |
118 | printf("\n"); | |
119 | ||
15d920a9 BV |
120 | if (srd_init(NULL) == SRD_OK) { |
121 | printf("Supported protocol decoders:\n"); | |
1b34803d UH |
122 | srd_decoder_load_all(); |
123 | for (l = srd_decoder_list(); l; l = l->next) { | |
15d920a9 BV |
124 | dec = l->data; |
125 | printf(" %-20s %s\n", dec->id, dec->longname); | |
20622036 UH |
126 | /* Print protocol description upon "-l 3" or higher. */ |
127 | if (opt_loglevel >= SR_LOG_INFO) | |
128 | printf(" %-20s %s\n", "", dec->desc); | |
15d920a9 BV |
129 | } |
130 | srd_exit(); | |
43e5747a UH |
131 | } |
132 | printf("\n"); | |
43e5747a UH |
133 | } |
134 | ||
1e0f9ed9 | 135 | static void print_dev_line(const struct sr_dev *dev) |
43e5747a | 136 | { |
1e0f9ed9 | 137 | const struct sr_dev_inst *sdi; |
43e5747a | 138 | |
b9210275 | 139 | sr_dev_info_get(dev, SR_DI_INST, (const void **)&sdi); |
43e5747a UH |
140 | |
141 | if (sdi->vendor && sdi->vendor[0]) | |
142 | printf("%s ", sdi->vendor); | |
143 | if (sdi->model && sdi->model[0]) | |
144 | printf("%s ", sdi->model); | |
145 | if (sdi->version && sdi->version[0]) | |
146 | printf("%s ", sdi->version); | |
1e0f9ed9 UH |
147 | if (dev->probes) |
148 | printf("with %d probes", g_slist_length(dev->probes)); | |
43e5747a UH |
149 | printf("\n"); |
150 | } | |
151 | ||
1e0f9ed9 | 152 | static void show_dev_list(void) |
43e5747a | 153 | { |
1e0f9ed9 UH |
154 | struct sr_dev *dev, *demo_dev; |
155 | GSList *devs, *l; | |
43e5747a UH |
156 | int devcnt; |
157 | ||
158 | devcnt = 0; | |
1e0f9ed9 | 159 | devs = sr_dev_list(); |
43e5747a | 160 | |
1e0f9ed9 | 161 | if (g_slist_length(devs) == 0) |
43e5747a UH |
162 | return; |
163 | ||
164 | printf("The following devices were found:\nID Device\n"); | |
1e0f9ed9 UH |
165 | demo_dev = NULL; |
166 | for (l = devs; l; l = l->next) { | |
167 | dev = l->data; | |
168 | if (sr_dev_has_hwcap(dev, SR_HWCAP_DEMO_DEV)) { | |
169 | demo_dev = dev; | |
43e5747a UH |
170 | continue; |
171 | } | |
172 | printf("%-3d ", devcnt++); | |
1e0f9ed9 | 173 | print_dev_line(dev); |
43e5747a | 174 | } |
1e0f9ed9 | 175 | if (demo_dev) { |
43e5747a | 176 | printf("demo "); |
1e0f9ed9 | 177 | print_dev_line(demo_dev); |
43e5747a UH |
178 | } |
179 | } | |
180 | ||
1e0f9ed9 | 181 | static void show_dev_detail(void) |
43e5747a | 182 | { |
1e0f9ed9 | 183 | struct sr_dev *dev; |
43e5747a | 184 | struct sr_hwcap_option *hwo; |
9c9c1080 | 185 | const struct sr_samplerates *samplerates; |
edd79b3f | 186 | int cap, *hwcaps, i; |
9c9c1080 AS |
187 | char *s, *title; |
188 | const char *charopts, **stropts; | |
43e5747a | 189 | |
1e0f9ed9 UH |
190 | dev = parse_devstring(opt_dev); |
191 | if (!dev) { | |
43e5747a UH |
192 | printf("No such device. Use -D to list all devices.\n"); |
193 | return; | |
194 | } | |
195 | ||
1e0f9ed9 | 196 | print_dev_line(dev); |
43e5747a | 197 | |
1e0f9ed9 | 198 | if (sr_dev_info_get(dev, SR_DI_TRIGGER_TYPES, |
fa230beb | 199 | (const void **)&charopts) == SR_OK) { |
43e5747a UH |
200 | printf("Supported triggers: "); |
201 | while (*charopts) { | |
202 | printf("%c ", *charopts); | |
203 | charopts++; | |
204 | } | |
205 | printf("\n"); | |
206 | } | |
207 | ||
208 | title = "Supported options:\n"; | |
a210c4cc | 209 | hwcaps = dev->driver->hwcap_get_all(); |
edd79b3f UH |
210 | for (cap = 0; hwcaps[cap]; cap++) { |
211 | if (!(hwo = sr_hw_hwcap_get(hwcaps[cap]))) | |
43e5747a UH |
212 | continue; |
213 | ||
214 | if (title) { | |
215 | printf("%s", title); | |
216 | title = NULL; | |
217 | } | |
218 | ||
edd79b3f | 219 | if (hwo->hwcap == SR_HWCAP_PATTERN_MODE) { |
43e5747a | 220 | printf(" %s", hwo->shortname); |
0a56f4ec | 221 | if (sr_dev_info_get(dev, SR_DI_PATTERNS, |
fa230beb | 222 | (const void **)&stropts) == SR_OK) { |
0a56f4ec | 223 | printf(" - supported patterns:\n"); |
9c9c1080 AS |
224 | for (i = 0; stropts[i]; i++) |
225 | printf(" %s\n", stropts[i]); | |
226 | } else { | |
43e5747a | 227 | printf("\n"); |
43e5747a | 228 | } |
edd79b3f | 229 | } else if (hwo->hwcap == SR_HWCAP_SAMPLERATE) { |
43e5747a UH |
230 | printf(" %s", hwo->shortname); |
231 | /* Supported samplerates */ | |
1e0f9ed9 | 232 | if (sr_dev_info_get(dev, SR_DI_SAMPLERATES, |
fa230beb | 233 | (const void **)&samplerates) != SR_OK) { |
43e5747a UH |
234 | printf("\n"); |
235 | continue; | |
236 | } | |
237 | ||
238 | if (samplerates->step) { | |
239 | /* low */ | |
240 | if (!(s = sr_samplerate_string(samplerates->low))) | |
241 | continue; | |
242 | printf(" (%s", s); | |
c2c4a0de | 243 | g_free(s); |
43e5747a UH |
244 | /* high */ |
245 | if (!(s = sr_samplerate_string(samplerates->high))) | |
246 | continue; | |
247 | printf(" - %s", s); | |
c2c4a0de | 248 | g_free(s); |
43e5747a UH |
249 | /* step */ |
250 | if (!(s = sr_samplerate_string(samplerates->step))) | |
251 | continue; | |
252 | printf(" in steps of %s)\n", s); | |
c2c4a0de | 253 | g_free(s); |
43e5747a UH |
254 | } else { |
255 | printf(" - supported samplerates:\n"); | |
256 | for (i = 0; samplerates->list[i]; i++) { | |
9da0b05b | 257 | printf(" %s\n", sr_samplerate_string(samplerates->list[i])); |
43e5747a UH |
258 | } |
259 | } | |
260 | } else { | |
261 | printf(" %s\n", hwo->shortname); | |
262 | } | |
263 | } | |
264 | } | |
265 | ||
71b1ea4e BV |
266 | static void show_pd_detail(void) |
267 | { | |
268 | GSList *l; | |
269 | struct srd_decoder *dec; | |
270 | char **pdtokens, **pdtok, **ann, *doc; | |
e9ff6974 | 271 | struct srd_probe *p; |
71b1ea4e BV |
272 | |
273 | pdtokens = g_strsplit(opt_pds, ",", -1); | |
274 | for (pdtok = pdtokens; *pdtok; pdtok++) { | |
2358775c | 275 | if (!(dec = srd_decoder_get_by_id(*pdtok))) { |
71b1ea4e BV |
276 | printf("Protocol decoder %s not found.", *pdtok); |
277 | return; | |
278 | } | |
279 | printf("ID: %s\nName: %s\nLong name: %s\nDescription: %s\n", | |
280 | dec->id, dec->name, dec->longname, dec->desc); | |
281 | printf("License: %s\n", dec->license); | |
e9ff6974 | 282 | printf("Annotations:\n"); |
71b1ea4e | 283 | if (dec->annotations) { |
71b1ea4e BV |
284 | for (l = dec->annotations; l; l = l->next) { |
285 | ann = l->data; | |
286 | printf("- %s\n %s\n", ann[0], ann[1]); | |
287 | } | |
e9ff6974 UH |
288 | } else { |
289 | printf("None.\n"); | |
290 | } | |
291 | /* TODO: Print supported decoder options. */ | |
292 | printf("Required probes:\n"); | |
293 | if (dec->probes) { | |
294 | for (l = dec->probes; l; l = l->next) { | |
295 | p = l->data; | |
296 | printf("- %s (%s): %s\n", | |
297 | p->name, p->id, p->desc); | |
298 | } | |
299 | } else { | |
300 | printf("None.\n"); | |
301 | } | |
302 | printf("Optional probes:\n"); | |
303 | if (dec->opt_probes) { | |
304 | for (l = dec->opt_probes; l; l = l->next) { | |
305 | p = l->data; | |
306 | printf("- %s (%s): %s\n", | |
307 | p->name, p->id, p->desc); | |
308 | } | |
309 | } else { | |
310 | printf("None.\n"); | |
71b1ea4e | 311 | } |
4359a4da | 312 | if ((doc = srd_decoder_doc_get(dec))) { |
e9ff6974 UH |
313 | printf("Documentation:\n%s\n", |
314 | doc[0] == '\n' ? doc + 1 : doc); | |
71b1ea4e BV |
315 | g_free(doc); |
316 | } | |
317 | } | |
318 | ||
319 | g_strfreev(pdtokens); | |
71b1ea4e BV |
320 | } |
321 | ||
1e0f9ed9 | 322 | static void datafeed_in(struct sr_dev *dev, struct sr_datafeed_packet *packet) |
43e5747a UH |
323 | { |
324 | static struct sr_output *o = NULL; | |
8364461d | 325 | static int probelist[SR_MAX_NUM_PROBES + 1] = { 0 }; |
43e5747a UH |
326 | static uint64_t received_samples = 0; |
327 | static int unitsize = 0; | |
328 | static int triggered = 0; | |
329 | static FILE *outfile = NULL; | |
330 | struct sr_probe *probe; | |
331 | struct sr_datafeed_header *header; | |
332 | struct sr_datafeed_logic *logic; | |
333 | int num_enabled_probes, sample_size, ret, i; | |
334 | uint64_t output_len, filter_out_len; | |
8de01efe | 335 | uint8_t *output_buf, *filter_out; |
43e5747a UH |
336 | |
337 | /* If the first packet to come in isn't a header, don't even try. */ | |
338 | if (packet->type != SR_DF_HEADER && o == NULL) | |
339 | return; | |
340 | ||
341 | sample_size = -1; | |
342 | switch (packet->type) { | |
343 | case SR_DF_HEADER: | |
8170b8ea | 344 | g_debug("cli: Received SR_DF_HEADER"); |
43e5747a | 345 | /* Initialize the output module. */ |
c2c4a0de | 346 | if (!(o = g_try_malloc(sizeof(struct sr_output)))) { |
8170b8ea | 347 | g_critical("Output module malloc failed."); |
43e5747a UH |
348 | exit(1); |
349 | } | |
350 | o->format = output_format; | |
1e0f9ed9 | 351 | o->dev = dev; |
43e5747a UH |
352 | o->param = output_format_param; |
353 | if (o->format->init) { | |
354 | if (o->format->init(o) != SR_OK) { | |
8170b8ea | 355 | g_critical("Output format initialization failed."); |
43e5747a UH |
356 | exit(1); |
357 | } | |
358 | } | |
359 | ||
360 | header = packet->payload; | |
361 | num_enabled_probes = 0; | |
362 | for (i = 0; i < header->num_logic_probes; i++) { | |
1e0f9ed9 | 363 | probe = g_slist_nth_data(dev->probes, i); |
43e5747a UH |
364 | if (probe->enabled) |
365 | probelist[num_enabled_probes++] = probe->index; | |
366 | } | |
367 | /* How many bytes we need to store num_enabled_probes bits */ | |
368 | unitsize = (num_enabled_probes + 7) / 8; | |
369 | ||
370 | outfile = stdout; | |
371 | if (opt_output_file) { | |
372 | if (default_output_format) { | |
373 | /* output file is in session format, which means we'll | |
374 | * dump everything in the datastore as it comes in, | |
375 | * and save from there after the session. */ | |
376 | outfile = NULL; | |
1e0f9ed9 | 377 | ret = sr_datastore_new(unitsize, &(dev->datastore)); |
43e5747a | 378 | if (ret != SR_OK) { |
8170b8ea | 379 | g_critical("Failed to create datastore."); |
43e5747a UH |
380 | exit(1); |
381 | } | |
382 | } else { | |
383 | /* saving to a file in whatever format was set | |
76ae913d | 384 | * with -O, so all we need is a filehandle */ |
43e5747a UH |
385 | outfile = g_fopen(opt_output_file, "wb"); |
386 | } | |
387 | } | |
3a999920 BV |
388 | if (opt_pds) { |
389 | if (srd_session_start(num_enabled_probes, unitsize, | |
390 | header->samplerate) != SRD_OK) | |
391 | sr_session_halt(); | |
392 | } | |
43e5747a UH |
393 | break; |
394 | case SR_DF_END: | |
8170b8ea | 395 | g_debug("cli: Received SR_DF_END"); |
43e5747a | 396 | if (!o) { |
8170b8ea | 397 | g_debug("cli: double end!"); |
43e5747a UH |
398 | break; |
399 | } | |
400 | if (o->format->event) { | |
401 | o->format->event(o, SR_DF_END, &output_buf, &output_len); | |
402 | if (output_len) { | |
403 | if (outfile) | |
404 | fwrite(output_buf, 1, output_len, outfile); | |
c2c4a0de | 405 | g_free(output_buf); |
43e5747a UH |
406 | output_len = 0; |
407 | } | |
408 | } | |
409 | if (limit_samples && received_samples < limit_samples) | |
8170b8ea | 410 | g_warning("Device only sent %" PRIu64 " samples.", |
43e5747a UH |
411 | received_samples); |
412 | if (opt_continuous) | |
8170b8ea | 413 | g_warning("Device stopped after %" PRIu64 " samples.", |
43e5747a UH |
414 | received_samples); |
415 | sr_session_halt(); | |
416 | if (outfile && outfile != stdout) | |
417 | fclose(outfile); | |
c2c4a0de | 418 | g_free(o); |
43e5747a UH |
419 | o = NULL; |
420 | break; | |
421 | case SR_DF_TRIGGER: | |
8170b8ea | 422 | g_debug("cli: received SR_DF_TRIGGER"); |
43e5747a UH |
423 | if (o->format->event) |
424 | o->format->event(o, SR_DF_TRIGGER, &output_buf, | |
425 | &output_len); | |
426 | triggered = 1; | |
427 | break; | |
428 | case SR_DF_LOGIC: | |
429 | logic = packet->payload; | |
430 | sample_size = logic->unitsize; | |
b46c4414 | 431 | g_message("cli: received SR_DF_LOGIC, %"PRIu64" bytes", logic->length); |
43e5747a | 432 | break; |
43e5747a UH |
433 | } |
434 | ||
435 | /* not supporting anything but SR_DF_LOGIC for now */ | |
436 | ||
437 | if (sample_size == -1 || logic->length == 0) | |
438 | return; | |
439 | ||
440 | /* Don't store any samples until triggered. */ | |
441 | if (opt_wait_trigger && !triggered) | |
442 | return; | |
443 | ||
444 | if (limit_samples && received_samples >= limit_samples) | |
445 | return; | |
446 | ||
447 | /* TODO: filters only support SR_DF_LOGIC */ | |
448 | ret = sr_filter_probes(sample_size, unitsize, probelist, | |
449 | logic->data, logic->length, | |
450 | &filter_out, &filter_out_len); | |
451 | if (ret != SR_OK) | |
452 | return; | |
453 | ||
454 | /* what comes out of the filter is guaranteed to be packed into the | |
455 | * minimum size needed to support the number of samples at this sample | |
456 | * size. however, the driver may have submitted too much -- cut off | |
457 | * the buffer of the last packet according to the sample limit. | |
458 | */ | |
459 | if (limit_samples && (received_samples + logic->length / sample_size > | |
460 | limit_samples * sample_size)) | |
461 | filter_out_len = limit_samples * sample_size - received_samples; | |
462 | ||
1e0f9ed9 UH |
463 | if (dev->datastore) |
464 | sr_datastore_put(dev->datastore, filter_out, | |
43e5747a UH |
465 | filter_out_len, sample_size, probelist); |
466 | ||
467 | if (opt_output_file && default_output_format) | |
468 | /* saving to a session file, don't need to do anything else | |
469 | * to this data for now. */ | |
470 | goto cleanup; | |
471 | ||
472 | if (opt_pds) { | |
37aeb8c5 | 473 | if (srd_session_send(received_samples, (uint8_t*)filter_out, |
838285aa | 474 | filter_out_len) != SRD_OK) |
305661ad | 475 | sr_session_halt(); |
43e5747a UH |
476 | } else { |
477 | output_len = 0; | |
478 | if (o->format->data && packet->type == o->format->df_type) | |
479 | o->format->data(o, filter_out, filter_out_len, &output_buf, &output_len); | |
480 | if (output_len) { | |
481 | fwrite(output_buf, 1, output_len, outfile); | |
c2c4a0de | 482 | g_free(output_buf); |
43e5747a UH |
483 | } |
484 | } | |
485 | ||
486 | cleanup: | |
487 | g_free(filter_out); | |
488 | received_samples += logic->length / sample_size; | |
43e5747a UH |
489 | } |
490 | ||
9f4a898e BV |
491 | /* Register the given PDs for this session. |
492 | * Accepts a string of the form: "spi:sck=3:sdata=4,spi:sck=3:sdata=5" | |
43e5747a UH |
493 | * That will instantiate two SPI decoders on the clock but different data |
494 | * lines. | |
495 | */ | |
1e0f9ed9 | 496 | static int register_pds(struct sr_dev *dev, const char *pdstring) |
43e5747a | 497 | { |
9720f23a | 498 | GHashTable *pd_opthash; |
878e90d9 | 499 | struct srd_decoder_inst *di; |
a1418b73 | 500 | char **pdtokens, **pdtok, *pd_name; |
43e5747a UH |
501 | |
502 | /* Avoid compiler warnings. */ | |
1e0f9ed9 | 503 | (void)dev; |
43e5747a | 504 | |
9f4a898e | 505 | g_datalist_init(&pd_ann_visible); |
43e5747a | 506 | pdtokens = g_strsplit(pdstring, ",", -1); |
9720f23a | 507 | pd_opthash = NULL; |
a1418b73 | 508 | pd_name = NULL; |
43e5747a UH |
509 | |
510 | for (pdtok = pdtokens; *pdtok; pdtok++) { | |
9720f23a | 511 | if (!(pd_opthash = parse_generic_arg(*pdtok))) { |
8170b8ea | 512 | g_critical("Invalid protocol decoder option '%s'.", *pdtok); |
a1418b73 | 513 | goto err_out; |
43e5747a | 514 | } |
a1418b73 | 515 | |
9720f23a BV |
516 | pd_name = g_strdup(g_hash_table_lookup(pd_opthash, "sigrok_key")); |
517 | g_hash_table_remove(pd_opthash, "sigrok_key"); | |
2358775c | 518 | if (srd_decoder_load(pd_name) != SRD_OK) { |
8170b8ea | 519 | g_critical("Failed to load protocol decoder %s.", pd_name); |
15d920a9 BV |
520 | goto err_out; |
521 | } | |
878e90d9 | 522 | if (!(di = srd_inst_new(pd_name, pd_opthash))) { |
8170b8ea | 523 | g_critical("Failed to instantiate protocol decoder %s.", pd_name); |
a1418b73 | 524 | goto err_out; |
43e5747a | 525 | } |
878e90d9 | 526 | g_datalist_set_data(&pd_ann_visible, di->inst_id, pd_name); |
a1418b73 | 527 | |
67ce0d27 BV |
528 | /* Any keys left in the options hash are probes, where the key |
529 | * is the probe name as specified in the decoder class, and the | |
530 | * value is the probe number i.e. the order in which the PD's | |
531 | * incoming samples are arranged. */ | |
4359a4da | 532 | if (srd_inst_probe_set_all(di, pd_opthash) != SRD_OK) |
67ce0d27 BV |
533 | goto err_out; |
534 | g_hash_table_destroy(pd_opthash); | |
535 | pd_opthash = NULL; | |
536 | } | |
43e5747a | 537 | |
a1418b73 | 538 | err_out: |
43e5747a | 539 | g_strfreev(pdtokens); |
9720f23a BV |
540 | if (pd_opthash) |
541 | g_hash_table_destroy(pd_opthash); | |
a1418b73 BV |
542 | if (pd_name) |
543 | g_free(pd_name); | |
43e5747a UH |
544 | |
545 | return 0; | |
546 | } | |
547 | ||
68cdf174 | 548 | void show_pd_annotation(struct srd_proto_data *pdata, void *cb_data) |
213c6cea BV |
549 | { |
550 | int i; | |
ca07c711 | 551 | char **annotations; |
213c6cea | 552 | |
68cdf174 UH |
553 | /* 'cb_data' is not used in this specific callback. */ |
554 | (void)cb_data; | |
7cb9889f | 555 | |
ca07c711 | 556 | if (pdata->ann_format != 0) { |
9720f23a | 557 | /* CLI only shows the default annotation format. */ |
213c6cea BV |
558 | return; |
559 | } | |
560 | ||
878e90d9 | 561 | if (!g_datalist_get_data(&pd_ann_visible, pdata->pdo->di->inst_id)) { |
9720f23a | 562 | /* Not in the list of PDs whose annotations we're showing. */ |
9f4a898e BV |
563 | return; |
564 | } | |
565 | ||
9720f23a | 566 | annotations = pdata->data; |
41bc9e4f BV |
567 | if (opt_loglevel > SR_LOG_WARN) |
568 | printf("%"PRIu64"-%"PRIu64" ", pdata->start_sample, pdata->end_sample); | |
ca07c711 UH |
569 | printf("%s: ", pdata->pdo->proto_id); |
570 | for (i = 0; annotations[i]; i++) | |
571 | printf("\"%s\" ", annotations[i]); | |
213c6cea | 572 | printf("\n"); |
213c6cea BV |
573 | } |
574 | ||
1e0f9ed9 | 575 | static int select_probes(struct sr_dev *dev) |
43e5747a UH |
576 | { |
577 | struct sr_probe *probe; | |
578 | char **probelist; | |
579 | int max_probes, i; | |
580 | ||
581 | if (!opt_probes) | |
582 | return SR_OK; | |
583 | ||
584 | /* | |
585 | * This only works because a device by default initializes | |
586 | * and enables all its probes. | |
587 | */ | |
1e0f9ed9 | 588 | max_probes = g_slist_length(dev->probes); |
43e5747a UH |
589 | probelist = parse_probestring(max_probes, opt_probes); |
590 | if (!probelist) { | |
591 | return SR_ERR; | |
592 | } | |
593 | ||
594 | for (i = 0; i < max_probes; i++) { | |
595 | if (probelist[i]) { | |
2df65e9f | 596 | sr_dev_probe_name_set(dev, i + 1, probelist[i]); |
43e5747a UH |
597 | g_free(probelist[i]); |
598 | } else { | |
1e0f9ed9 | 599 | probe = sr_dev_probe_find(dev, i + 1); |
43e5747a UH |
600 | probe->enabled = FALSE; |
601 | } | |
602 | } | |
603 | g_free(probelist); | |
604 | ||
605 | return SR_OK; | |
606 | } | |
607 | ||
608 | /** | |
609 | * Return the input file format which the CLI tool should use. | |
610 | * | |
611 | * If the user specified -I / --input-format, use that one. Otherwise, try to | |
612 | * autodetect the format as good as possible. Failing that, return NULL. | |
613 | * | |
614 | * @param filename The filename of the input file. Must not be NULL. | |
615 | * @param opt The -I / --input-file option the user specified (or NULL). | |
616 | * | |
617 | * @return A pointer to the 'struct sr_input_format' that should be used, | |
618 | * or NULL if no input format was selected or auto-detected. | |
619 | */ | |
620 | static struct sr_input_format *determine_input_file_format( | |
621 | const char *filename, const char *opt) | |
622 | { | |
623 | int i; | |
624 | struct sr_input_format **inputs; | |
625 | ||
626 | /* If there are no input formats, return NULL right away. */ | |
627 | inputs = sr_input_list(); | |
628 | if (!inputs) { | |
8170b8ea | 629 | g_critical("No supported input formats available."); |
43e5747a UH |
630 | return NULL; |
631 | } | |
632 | ||
633 | /* If the user specified -I / --input-format, use that one. */ | |
634 | if (opt) { | |
635 | for (i = 0; inputs[i]; i++) { | |
636 | if (strcasecmp(inputs[i]->id, opt_input_format)) | |
637 | continue; | |
8170b8ea BV |
638 | g_debug("Using user-specified input file format '%s'.", |
639 | inputs[i]->id); | |
43e5747a UH |
640 | return inputs[i]; |
641 | } | |
642 | ||
643 | /* The user specified an unknown input format, return NULL. */ | |
8170b8ea BV |
644 | g_critical("Error: specified input file format '%s' is " |
645 | "unknown.", opt_input_format); | |
43e5747a UH |
646 | return NULL; |
647 | } | |
648 | ||
649 | /* Otherwise, try to find an input module that can handle this file. */ | |
650 | for (i = 0; inputs[i]; i++) { | |
651 | if (inputs[i]->format_match(filename)) | |
652 | break; | |
653 | } | |
654 | ||
655 | /* Return NULL if no input module wanted to touch this. */ | |
656 | if (!inputs[i]) { | |
8170b8ea | 657 | g_critical("Error: no matching input module found."); |
43e5747a UH |
658 | return NULL; |
659 | } | |
660 | ||
43e5747a UH |
661 | return inputs[i]; |
662 | } | |
663 | ||
664 | static void load_input_file_format(void) | |
665 | { | |
666 | struct stat st; | |
667 | struct sr_input *in; | |
668 | struct sr_input_format *input_format; | |
669 | ||
8170b8ea BV |
670 | if (!(input_format = determine_input_file_format(opt_input_file, |
671 | opt_input_format))) | |
672 | /* The exact cause was already logged. */ | |
43e5747a | 673 | return; |
43e5747a UH |
674 | |
675 | if (stat(opt_input_file, &st) == -1) { | |
8170b8ea | 676 | g_critical("Failed to load %s: %s", opt_input_file, |
43e5747a UH |
677 | strerror(errno)); |
678 | exit(1); | |
679 | } | |
680 | ||
681 | /* Initialize the input module. */ | |
c2c4a0de | 682 | if (!(in = g_try_malloc(sizeof(struct sr_input)))) { |
8170b8ea | 683 | g_critical("Failed to allocate input module."); |
43e5747a UH |
684 | exit(1); |
685 | } | |
686 | in->format = input_format; | |
687 | in->param = input_format_param; | |
688 | if (in->format->init) { | |
689 | if (in->format->init(in) != SR_OK) { | |
8170b8ea | 690 | g_critical("Input format init failed."); |
43e5747a UH |
691 | exit(1); |
692 | } | |
693 | } | |
694 | ||
1e0f9ed9 | 695 | if (select_probes(in->vdev) > 0) |
43e5747a UH |
696 | return; |
697 | ||
698 | sr_session_new(); | |
699 | sr_session_datafeed_callback_add(datafeed_in); | |
1e0f9ed9 | 700 | if (sr_session_dev_add(in->vdev) != SR_OK) { |
8170b8ea | 701 | g_critical("Failed to use device."); |
43e5747a UH |
702 | sr_session_destroy(); |
703 | return; | |
704 | } | |
705 | ||
706 | input_format->loadfile(in, opt_input_file); | |
707 | if (opt_output_file && default_output_format) { | |
708 | if (sr_session_save(opt_output_file) != SR_OK) | |
8170b8ea | 709 | g_critical("Failed to save session."); |
43e5747a UH |
710 | } |
711 | sr_session_destroy(); | |
43e5747a UH |
712 | } |
713 | ||
714 | static void load_input_file(void) | |
715 | { | |
716 | ||
717 | if (sr_session_load(opt_input_file) == SR_OK) { | |
718 | /* sigrok session file */ | |
719 | sr_session_datafeed_callback_add(datafeed_in); | |
720 | sr_session_start(); | |
721 | sr_session_run(); | |
722 | sr_session_stop(); | |
723 | } | |
724 | else { | |
725 | /* fall back on input modules */ | |
726 | load_input_file_format(); | |
727 | } | |
43e5747a UH |
728 | } |
729 | ||
1e0f9ed9 | 730 | int num_real_devs(void) |
43e5747a | 731 | { |
1e0f9ed9 UH |
732 | struct sr_dev *dev; |
733 | GSList *devs, *l; | |
734 | int num_devs; | |
735 | ||
736 | num_devs = 0; | |
737 | devs = sr_dev_list(); | |
738 | for (l = devs; l; l = l->next) { | |
739 | dev = l->data; | |
740 | if (!sr_dev_has_hwcap(dev, SR_HWCAP_DEMO_DEV)) | |
741 | num_devs++; | |
43e5747a UH |
742 | } |
743 | ||
1e0f9ed9 | 744 | return num_devs; |
43e5747a UH |
745 | } |
746 | ||
1e0f9ed9 | 747 | static int set_dev_options(struct sr_dev *dev, GHashTable *args) |
43e5747a UH |
748 | { |
749 | GHashTableIter iter; | |
750 | gpointer key, value; | |
751 | int ret, i; | |
752 | uint64_t tmp_u64; | |
753 | gboolean found; | |
754 | gboolean tmp_bool; | |
755 | ||
756 | g_hash_table_iter_init(&iter, args); | |
757 | while (g_hash_table_iter_next(&iter, &key, &value)) { | |
758 | found = FALSE; | |
edd79b3f | 759 | for (i = 0; sr_hwcap_options[i].hwcap; i++) { |
43e5747a UH |
760 | if (strcmp(sr_hwcap_options[i].shortname, key)) |
761 | continue; | |
762 | if ((value == NULL) && | |
763 | (sr_hwcap_options[i].type != SR_T_BOOL)) { | |
8170b8ea | 764 | g_critical("Option '%s' needs a value.", (char *)key); |
43e5747a UH |
765 | return SR_ERR; |
766 | } | |
767 | found = TRUE; | |
768 | switch (sr_hwcap_options[i].type) { | |
769 | case SR_T_UINT64: | |
770 | ret = sr_parse_sizestring(value, &tmp_u64); | |
771 | if (ret != SR_OK) | |
772 | break; | |
a210c4cc | 773 | ret = dev->driver->dev_config_set(dev->driver_index, |
8d145d46 | 774 | sr_hwcap_options[i].hwcap, &tmp_u64); |
43e5747a UH |
775 | break; |
776 | case SR_T_CHAR: | |
a210c4cc | 777 | ret = dev->driver->dev_config_set(dev->driver_index, |
8d145d46 | 778 | sr_hwcap_options[i].hwcap, value); |
43e5747a UH |
779 | break; |
780 | case SR_T_BOOL: | |
781 | if (!value) | |
782 | tmp_bool = TRUE; | |
783 | else | |
784 | tmp_bool = sr_parse_boolstring(value); | |
a210c4cc | 785 | ret = dev->driver->dev_config_set(dev->driver_index, |
edd79b3f | 786 | sr_hwcap_options[i].hwcap, |
43e5747a UH |
787 | GINT_TO_POINTER(tmp_bool)); |
788 | break; | |
789 | default: | |
790 | ret = SR_ERR; | |
791 | } | |
792 | ||
793 | if (ret != SR_OK) { | |
8170b8ea | 794 | g_critical("Failed to set device option '%s'.", (char *)key); |
43e5747a UH |
795 | return ret; |
796 | } | |
797 | else | |
798 | break; | |
799 | } | |
800 | if (!found) { | |
8170b8ea | 801 | g_critical("Unknown device option '%s'.", (char *) key); |
43e5747a UH |
802 | return SR_ERR; |
803 | } | |
804 | } | |
805 | ||
806 | return SR_OK; | |
807 | } | |
808 | ||
809 | static void run_session(void) | |
810 | { | |
1e0f9ed9 | 811 | struct sr_dev *dev; |
43e5747a | 812 | GHashTable *devargs; |
1e0f9ed9 | 813 | int num_devs, max_probes, i; |
9c9c1080 | 814 | uint64_t time_msec; |
43e5747a UH |
815 | char **probelist, *devspec; |
816 | ||
817 | devargs = NULL; | |
1e0f9ed9 UH |
818 | if (opt_dev) { |
819 | devargs = parse_generic_arg(opt_dev); | |
43e5747a | 820 | devspec = g_hash_table_lookup(devargs, "sigrok_key"); |
1e0f9ed9 UH |
821 | dev = parse_devstring(devspec); |
822 | if (!dev) { | |
8170b8ea | 823 | g_critical("Device not found."); |
43e5747a UH |
824 | return; |
825 | } | |
826 | g_hash_table_remove(devargs, "sigrok_key"); | |
827 | } else { | |
1e0f9ed9 UH |
828 | num_devs = num_real_devs(); |
829 | if (num_devs == 1) { | |
43e5747a UH |
830 | /* No device specified, but there is only one. */ |
831 | devargs = NULL; | |
1e0f9ed9 UH |
832 | dev = parse_devstring("0"); |
833 | } else if (num_devs == 0) { | |
8170b8ea | 834 | g_critical("No devices found."); |
43e5747a UH |
835 | return; |
836 | } else { | |
8170b8ea | 837 | g_critical("%d devices found, please select one.", num_devs); |
43e5747a UH |
838 | return; |
839 | } | |
840 | } | |
841 | ||
842 | sr_session_new(); | |
843 | sr_session_datafeed_callback_add(datafeed_in); | |
844 | ||
1e0f9ed9 | 845 | if (sr_session_dev_add(dev) != SR_OK) { |
8170b8ea | 846 | g_critical("Failed to use device."); |
43e5747a UH |
847 | sr_session_destroy(); |
848 | return; | |
849 | } | |
850 | ||
851 | if (devargs) { | |
1e0f9ed9 | 852 | if (set_dev_options(dev, devargs) != SR_OK) { |
43e5747a UH |
853 | sr_session_destroy(); |
854 | return; | |
855 | } | |
856 | g_hash_table_destroy(devargs); | |
857 | } | |
858 | ||
1e0f9ed9 | 859 | if (select_probes(dev) != SR_OK) |
43e5747a UH |
860 | return; |
861 | ||
862 | if (opt_continuous) { | |
a214a2eb | 863 | if (!sr_driver_hwcap_exists(dev->driver, SR_HWCAP_CONTINUOUS)) { |
8170b8ea | 864 | g_critical("This device does not support continuous sampling."); |
43e5747a UH |
865 | sr_session_destroy(); |
866 | return; | |
867 | } | |
868 | } | |
869 | ||
870 | if (opt_triggers) { | |
1e0f9ed9 | 871 | probelist = sr_parse_triggerstring(dev, opt_triggers); |
43e5747a UH |
872 | if (!probelist) { |
873 | sr_session_destroy(); | |
874 | return; | |
875 | } | |
876 | ||
1e0f9ed9 | 877 | max_probes = g_slist_length(dev->probes); |
43e5747a UH |
878 | for (i = 0; i < max_probes; i++) { |
879 | if (probelist[i]) { | |
1e0f9ed9 | 880 | sr_dev_trigger_set(dev, i + 1, probelist[i]); |
43e5747a UH |
881 | g_free(probelist[i]); |
882 | } | |
883 | } | |
884 | g_free(probelist); | |
885 | } | |
886 | ||
887 | if (opt_time) { | |
888 | time_msec = sr_parse_timestring(opt_time); | |
889 | if (time_msec == 0) { | |
8170b8ea | 890 | g_critical("Invalid time '%s'", opt_time); |
43e5747a UH |
891 | sr_session_destroy(); |
892 | return; | |
893 | } | |
894 | ||
a214a2eb | 895 | if (sr_driver_hwcap_exists(dev->driver, SR_HWCAP_LIMIT_MSEC)) { |
a210c4cc | 896 | if (dev->driver->dev_config_set(dev->driver_index, |
8d145d46 | 897 | SR_HWCAP_LIMIT_MSEC, &time_msec) != SR_OK) { |
8170b8ea | 898 | g_critical("Failed to configure time limit."); |
43e5747a UH |
899 | sr_session_destroy(); |
900 | return; | |
901 | } | |
902 | } | |
903 | else { | |
904 | /* time limit set, but device doesn't support this... | |
905 | * convert to samples based on the samplerate. | |
906 | */ | |
907 | limit_samples = 0; | |
1e0f9ed9 | 908 | if (sr_dev_has_hwcap(dev, SR_HWCAP_SAMPLERATE)) { |
9c9c1080 AS |
909 | const uint64_t *samplerate; |
910 | ||
1e0f9ed9 | 911 | sr_dev_info_get(dev, SR_DI_CUR_SAMPLERATE, |
fa230beb UH |
912 | (const void **)&samplerate); |
913 | limit_samples = (*samplerate) * time_msec / (uint64_t)1000; | |
43e5747a UH |
914 | } |
915 | if (limit_samples == 0) { | |
8170b8ea | 916 | g_critical("Not enough time at this samplerate."); |
43e5747a UH |
917 | sr_session_destroy(); |
918 | return; | |
919 | } | |
920 | ||
a210c4cc | 921 | if (dev->driver->dev_config_set(dev->driver_index, |
8d145d46 | 922 | SR_HWCAP_LIMIT_SAMPLES, &limit_samples) != SR_OK) { |
8170b8ea | 923 | g_critical("Failed to configure time-based sample limit."); |
43e5747a UH |
924 | sr_session_destroy(); |
925 | return; | |
926 | } | |
927 | } | |
928 | } | |
929 | ||
930 | if (opt_samples) { | |
931 | if ((sr_parse_sizestring(opt_samples, &limit_samples) != SR_OK) | |
a210c4cc | 932 | || (dev->driver->dev_config_set(dev->driver_index, |
8d145d46 | 933 | SR_HWCAP_LIMIT_SAMPLES, &limit_samples) != SR_OK)) { |
8170b8ea | 934 | g_critical("Failed to configure sample limit."); |
43e5747a UH |
935 | sr_session_destroy(); |
936 | return; | |
937 | } | |
938 | } | |
939 | ||
a210c4cc | 940 | if (dev->driver->dev_config_set(dev->driver_index, |
1e0f9ed9 | 941 | SR_HWCAP_PROBECONFIG, (char *)dev->probes) != SR_OK) { |
8170b8ea | 942 | g_critical("Failed to configure probes."); |
43e5747a UH |
943 | sr_session_destroy(); |
944 | return; | |
945 | } | |
946 | ||
947 | if (sr_session_start() != SR_OK) { | |
8170b8ea | 948 | g_critical("Failed to start session."); |
43e5747a UH |
949 | sr_session_destroy(); |
950 | return; | |
951 | } | |
952 | ||
953 | if (opt_continuous) | |
954 | add_anykey(); | |
955 | ||
956 | sr_session_run(); | |
957 | ||
958 | if (opt_continuous) | |
959 | clear_anykey(); | |
960 | ||
961 | if (opt_output_file && default_output_format) { | |
962 | if (sr_session_save(opt_output_file) != SR_OK) | |
8170b8ea | 963 | g_critical("Failed to save session."); |
43e5747a UH |
964 | } |
965 | sr_session_destroy(); | |
43e5747a UH |
966 | } |
967 | ||
968 | static void logger(const gchar *log_domain, GLogLevelFlags log_level, | |
68cdf174 | 969 | const gchar *message, gpointer cb_data) |
43e5747a UH |
970 | { |
971 | /* Avoid compiler warnings. */ | |
972 | (void)log_domain; | |
68cdf174 | 973 | (void)cb_data; |
43e5747a UH |
974 | |
975 | /* | |
976 | * All messages, warnings, errors etc. go to stderr (not stdout) in | |
977 | * order to not mess up the CLI tool data output, e.g. VCD output. | |
978 | */ | |
d740e6ac BV |
979 | if (log_level & (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_WARNING) |
980 | || opt_loglevel > SR_LOG_WARN) { | |
43e5747a UH |
981 | fprintf(stderr, "%s\n", message); |
982 | fflush(stderr); | |
43e5747a UH |
983 | } |
984 | } | |
985 | ||
986 | int main(int argc, char **argv) | |
987 | { | |
43e5747a UH |
988 | GOptionContext *context; |
989 | GError *error; | |
990 | GHashTable *fmtargs; | |
991 | GHashTableIter iter; | |
992 | gpointer key, value; | |
9f4a898e | 993 | struct sr_output_format **outputs; |
878e90d9 | 994 | struct srd_decoder_inst *di_from, *di_to; |
9f4a898e BV |
995 | int i, ret; |
996 | char *fmtspec, **pds; | |
43e5747a UH |
997 | |
998 | g_log_set_default_handler(logger, NULL); | |
43e5747a UH |
999 | |
1000 | error = NULL; | |
1001 | context = g_option_context_new(NULL); | |
1002 | g_option_context_add_main_entries(context, optargs, NULL); | |
1003 | ||
1004 | if (!g_option_context_parse(context, &argc, &argv, &error)) { | |
8170b8ea | 1005 | g_critical("%s", error->message); |
43e5747a UH |
1006 | return 1; |
1007 | } | |
1008 | ||
1009 | /* Set the loglevel (amount of messages to output) for libsigrok. */ | |
bed50525 | 1010 | if (sr_log_loglevel_set(opt_loglevel) != SR_OK) { |
8170b8ea | 1011 | g_critical("sr_log_loglevel_set(%d) failed.", opt_loglevel); |
43e5747a UH |
1012 | return 1; |
1013 | } | |
1014 | ||
6de7ec06 | 1015 | /* Set the loglevel (amount of messages to output) for libsigrokdecode. */ |
f84f2fe0 | 1016 | if (srd_log_loglevel_set(opt_loglevel) != SRD_OK) { |
8170b8ea | 1017 | g_critical("srd_log_loglevel_set(%d) failed.", opt_loglevel); |
6de7ec06 BV |
1018 | return 1; |
1019 | } | |
1020 | ||
43e5747a UH |
1021 | if (sr_init() != SR_OK) |
1022 | return 1; | |
1023 | ||
1024 | if (opt_pds) { | |
d3a574f8 | 1025 | if (srd_init(NULL) != SRD_OK) { |
8170b8ea | 1026 | g_critical("Failed to initialize sigrokdecode."); |
213c6cea BV |
1027 | return 1; |
1028 | } | |
1029 | if (register_pds(NULL, opt_pds) != 0) { | |
8170b8ea | 1030 | g_critical("Failed to register protocol decoders."); |
213c6cea BV |
1031 | return 1; |
1032 | } | |
4359a4da | 1033 | if (srd_pd_output_callback_add(SRD_OUTPUT_ANN, |
7cb9889f | 1034 | show_pd_annotation, NULL) != SRD_OK) { |
8170b8ea | 1035 | g_critical("Failed to register protocol decoder callback."); |
213c6cea BV |
1036 | return 1; |
1037 | } | |
43e5747a | 1038 | |
b0b05654 BV |
1039 | pds = g_strsplit(opt_pds, ",", 0); |
1040 | if (g_strv_length(pds) > 1) { | |
1041 | if (opt_pd_stack) { | |
1042 | /* A stack setup was specified, use that. */ | |
1043 | g_strfreev(pds); | |
1044 | pds = g_strsplit(opt_pd_stack, ",", 0); | |
1045 | if (g_strv_length(pds) < 2) { | |
1046 | g_strfreev(pds); | |
8170b8ea | 1047 | g_critical("Specify at least two protocol decoders to stack."); |
b0b05654 BV |
1048 | return 1; |
1049 | } | |
1050 | } | |
9f4a898e | 1051 | |
b0b05654 | 1052 | if (!(di_from = srd_inst_find_by_id(pds[0]))) { |
8170b8ea BV |
1053 | g_critical("Cannot stack protocol decoder '%s': " |
1054 | "instance not found.", pds[0]); | |
9f4a898e BV |
1055 | return 1; |
1056 | } | |
b0b05654 BV |
1057 | for (i = 1; pds[i]; i++) { |
1058 | if (!(di_to = srd_inst_find_by_id(pds[i]))) { | |
8170b8ea BV |
1059 | g_critical("Cannot stack protocol decoder '%s': " |
1060 | "instance not found.", pds[i]); | |
b0b05654 BV |
1061 | return 1; |
1062 | } | |
1063 | if ((ret = srd_inst_stack(di_from, di_to)) != SRD_OK) | |
1064 | return ret; | |
9f4a898e | 1065 | |
b0b05654 BV |
1066 | /* Don't show annotation from this PD. Only the last PD in |
1067 | * the stack will be left on the annotation list. | |
1068 | */ | |
1069 | g_datalist_remove_data(&pd_ann_visible, di_from->inst_id); | |
9f4a898e | 1070 | |
b0b05654 BV |
1071 | di_from = di_to; |
1072 | } | |
9f4a898e BV |
1073 | } |
1074 | g_strfreev(pds); | |
1075 | } | |
1076 | ||
76ae913d UH |
1077 | if (!opt_output_format) { |
1078 | opt_output_format = DEFAULT_OUTPUT_FORMAT; | |
9f4a898e | 1079 | /* we'll need to remember this so when saving to a file |
43e5747a UH |
1080 | * later, sigrok session format will be used. |
1081 | */ | |
1082 | default_output_format = TRUE; | |
1083 | } | |
76ae913d | 1084 | fmtargs = parse_generic_arg(opt_output_format); |
43e5747a UH |
1085 | fmtspec = g_hash_table_lookup(fmtargs, "sigrok_key"); |
1086 | if (!fmtspec) { | |
8170b8ea | 1087 | g_critical("Invalid output format."); |
43e5747a UH |
1088 | return 1; |
1089 | } | |
1090 | outputs = sr_output_list(); | |
1091 | for (i = 0; outputs[i]; i++) { | |
1092 | if (strcmp(outputs[i]->id, fmtspec)) | |
1093 | continue; | |
1094 | g_hash_table_remove(fmtargs, "sigrok_key"); | |
1095 | output_format = outputs[i]; | |
1096 | g_hash_table_iter_init(&iter, fmtargs); | |
1097 | while (g_hash_table_iter_next(&iter, &key, &value)) { | |
1098 | /* only supporting one parameter per output module | |
1099 | * for now, and only its value */ | |
1100 | output_format_param = g_strdup(value); | |
1101 | break; | |
1102 | } | |
1103 | break; | |
1104 | } | |
1105 | if (!output_format) { | |
8170b8ea | 1106 | g_critical("Invalid output format %s.", opt_output_format); |
43e5747a UH |
1107 | return 1; |
1108 | } | |
1109 | ||
1110 | if (opt_version) | |
1111 | show_version(); | |
1e0f9ed9 UH |
1112 | else if (opt_list_devs) |
1113 | show_dev_list(); | |
43e5747a UH |
1114 | else if (opt_input_file) |
1115 | load_input_file(); | |
1116 | else if (opt_samples || opt_time || opt_continuous) | |
1117 | run_session(); | |
1e0f9ed9 UH |
1118 | else if (opt_dev) |
1119 | show_dev_detail(); | |
71b1ea4e BV |
1120 | else if (opt_pds) |
1121 | show_pd_detail(); | |
43e5747a UH |
1122 | else |
1123 | printf("%s", g_option_context_get_help(context, TRUE, NULL)); | |
1124 | ||
1125 | if (opt_pds) | |
1126 | srd_exit(); | |
1127 | ||
1128 | g_option_context_free(context); | |
1129 | g_hash_table_destroy(fmtargs); | |
1130 | sr_exit(); | |
1131 | ||
1132 | return 0; | |
1133 | } |