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