]>
Commit | Line | Data |
---|---|---|
43e5747a UH |
1 | /* |
2 | * This file is part of the sigrok project. | |
3 | * | |
4 | * Copyright (C) 2010 Bert Vermeulen <bert@biot.com> | |
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; | |
43e5747a UH |
47 | |
48 | static gboolean opt_version = FALSE; | |
49 | static gint opt_loglevel = SR_LOG_WARN; /* Show errors+warnings per default. */ | |
50 | static gboolean opt_list_devices = FALSE; | |
51 | static gboolean opt_wait_trigger = FALSE; | |
52 | static gchar *opt_input_file = NULL; | |
53 | static gchar *opt_output_file = NULL; | |
54 | static gchar *opt_device = NULL; | |
55 | static gchar *opt_probes = NULL; | |
56 | static gchar *opt_triggers = NULL; | |
57 | static gchar *opt_pds = NULL; | |
58 | static gchar *opt_input_format = NULL; | |
59 | static gchar *opt_format = NULL; | |
60 | static gchar *opt_time = NULL; | |
61 | static gchar *opt_samples = NULL; | |
62 | static gchar *opt_continuous = NULL; | |
63 | ||
64 | static GOptionEntry optargs[] = { | |
65 | {"version", 'V', 0, G_OPTION_ARG_NONE, &opt_version, "Show version and support list", NULL}, | |
66 | {"loglevel", 'l', 0, G_OPTION_ARG_INT, &opt_loglevel, "Select libsigrok loglevel", NULL}, | |
67 | {"list-devices", 'D', 0, G_OPTION_ARG_NONE, &opt_list_devices, "List devices", NULL}, | |
68 | {"input-file", 'i', 0, G_OPTION_ARG_FILENAME, &opt_input_file, "Load input from file", NULL}, | |
69 | {"output-file", 'o', 0, G_OPTION_ARG_FILENAME, &opt_output_file, "Save output to file", NULL}, | |
70 | {"device", 'd', 0, G_OPTION_ARG_STRING, &opt_device, "Use device ID", NULL}, | |
71 | {"probes", 'p', 0, G_OPTION_ARG_STRING, &opt_probes, "Probes to use", NULL}, | |
72 | {"triggers", 't', 0, G_OPTION_ARG_STRING, &opt_triggers, "Trigger configuration", NULL}, | |
73 | {"wait-trigger", 'w', 0, G_OPTION_ARG_NONE, &opt_wait_trigger, "Wait for trigger", NULL}, | |
74 | {"protocol-decoders", 'a', 0, G_OPTION_ARG_STRING, &opt_pds, "Protocol decoder sequence", NULL}, | |
75 | {"input-format", 'I', 0, G_OPTION_ARG_STRING, &opt_input_format, "Input format", NULL}, | |
76 | {"format", 'f', 0, G_OPTION_ARG_STRING, &opt_format, "Output format", NULL}, | |
77 | {"time", 0, 0, G_OPTION_ARG_STRING, &opt_time, "How long to sample (ms)", NULL}, | |
78 | {"samples", 0, 0, G_OPTION_ARG_STRING, &opt_samples, "Number of samples to acquire", NULL}, | |
79 | {"continuous", 0, 0, G_OPTION_ARG_NONE, &opt_continuous, "Sample continuously", NULL}, | |
80 | {NULL, 0, 0, 0, NULL, NULL, NULL} | |
81 | }; | |
82 | ||
83 | static void show_version(void) | |
84 | { | |
85 | GSList *plugins, *p, *l; | |
86 | struct sr_device_plugin *plugin; | |
87 | struct sr_input_format **inputs; | |
88 | struct sr_output_format **outputs; | |
89 | struct srd_decoder *dec; | |
90 | int i; | |
91 | ||
92 | printf("sigrok-cli %s\n\n", VERSION); | |
93 | printf("Supported hardware drivers:\n"); | |
94 | plugins = sr_list_hwplugins(); | |
95 | for (p = plugins; p; p = p->next) { | |
96 | plugin = p->data; | |
97 | printf(" %-20s %s\n", plugin->name, plugin->longname); | |
98 | } | |
99 | printf("\n"); | |
100 | ||
101 | printf("Supported input formats:\n"); | |
102 | inputs = sr_input_list(); | |
103 | for (i = 0; inputs[i]; i++) | |
104 | printf(" %-20s %s\n", inputs[i]->id, inputs[i]->description); | |
105 | printf("\n"); | |
106 | ||
107 | printf("Supported output formats:\n"); | |
108 | outputs = sr_output_list(); | |
109 | for (i = 0; outputs[i]; i++) | |
110 | printf(" %-20s %s\n", outputs[i]->id, outputs[i]->description); | |
111 | printf("\n"); | |
112 | ||
113 | /* TODO: Error handling. */ | |
114 | srd_init(); | |
115 | ||
116 | printf("Supported protocol decoders:\n"); | |
117 | for (l = srd_list_decoders(); l; l = l->next) { | |
118 | dec = l->data; | |
119 | printf(" %-20s %s\n", dec->id, dec->desc); | |
120 | } | |
121 | printf("\n"); | |
122 | ||
123 | srd_exit(); | |
124 | } | |
125 | ||
126 | static void print_device_line(struct sr_device *device) | |
127 | { | |
128 | struct sr_device_instance *sdi; | |
129 | ||
130 | sdi = device->plugin->get_device_info(device->plugin_index, SR_DI_INSTANCE); | |
131 | ||
132 | if (sdi->vendor && sdi->vendor[0]) | |
133 | printf("%s ", sdi->vendor); | |
134 | if (sdi->model && sdi->model[0]) | |
135 | printf("%s ", sdi->model); | |
136 | if (sdi->version && sdi->version[0]) | |
137 | printf("%s ", sdi->version); | |
138 | if (device->probes) | |
139 | printf("with %d probes", g_slist_length(device->probes)); | |
140 | printf("\n"); | |
141 | } | |
142 | ||
143 | static void show_device_list(void) | |
144 | { | |
145 | struct sr_device *device, *demo_device; | |
146 | GSList *devices, *l; | |
147 | int devcnt; | |
148 | ||
149 | devcnt = 0; | |
150 | devices = sr_device_list(); | |
151 | ||
152 | if (g_slist_length(devices) == 0) | |
153 | return; | |
154 | ||
155 | printf("The following devices were found:\nID Device\n"); | |
156 | demo_device = NULL; | |
157 | for (l = devices; l; l = l->next) { | |
158 | device = l->data; | |
159 | if (strstr(device->plugin->name, "demo")) { | |
160 | demo_device = device; | |
161 | continue; | |
162 | } | |
163 | printf("%-3d ", devcnt++); | |
164 | print_device_line(device); | |
165 | } | |
166 | if (demo_device) { | |
167 | printf("demo "); | |
168 | print_device_line(demo_device); | |
169 | } | |
170 | } | |
171 | ||
172 | static void show_device_detail(void) | |
173 | { | |
174 | struct sr_device *device; | |
175 | struct sr_hwcap_option *hwo; | |
176 | struct sr_samplerates *samplerates; | |
177 | int cap, *capabilities, i; | |
178 | char *s, *title, *charopts, **stropts; | |
179 | ||
180 | device = parse_devicestring(opt_device); | |
181 | if (!device) { | |
182 | printf("No such device. Use -D to list all devices.\n"); | |
183 | return; | |
184 | } | |
185 | ||
186 | print_device_line(device); | |
187 | ||
188 | if ((charopts = (char *)device->plugin->get_device_info( | |
189 | device->plugin_index, SR_DI_TRIGGER_TYPES))) { | |
190 | printf("Supported triggers: "); | |
191 | while (*charopts) { | |
192 | printf("%c ", *charopts); | |
193 | charopts++; | |
194 | } | |
195 | printf("\n"); | |
196 | } | |
197 | ||
198 | title = "Supported options:\n"; | |
199 | capabilities = device->plugin->get_capabilities(); | |
200 | for (cap = 0; capabilities[cap]; cap++) { | |
201 | if (!(hwo = sr_find_hwcap_option(capabilities[cap]))) | |
202 | continue; | |
203 | ||
204 | if (title) { | |
205 | printf("%s", title); | |
206 | title = NULL; | |
207 | } | |
208 | ||
209 | if (hwo->capability == SR_HWCAP_PATTERN_MODE) { | |
210 | printf(" %s", hwo->shortname); | |
211 | stropts = (char **)device->plugin->get_device_info( | |
212 | device->plugin_index, SR_DI_PATTERNMODES); | |
213 | if (!stropts) { | |
214 | printf("\n"); | |
215 | continue; | |
216 | } | |
217 | printf(" - supported modes:\n"); | |
218 | for (i = 0; stropts[i]; i++) | |
219 | printf(" %s\n", stropts[i]); | |
220 | } else if (hwo->capability == SR_HWCAP_SAMPLERATE) { | |
221 | printf(" %s", hwo->shortname); | |
222 | /* Supported samplerates */ | |
223 | samplerates = device->plugin->get_device_info( | |
224 | device->plugin_index, SR_DI_SAMPLERATES); | |
225 | if (!samplerates) { | |
226 | printf("\n"); | |
227 | continue; | |
228 | } | |
229 | ||
230 | if (samplerates->step) { | |
231 | /* low */ | |
232 | if (!(s = sr_samplerate_string(samplerates->low))) | |
233 | continue; | |
234 | printf(" (%s", s); | |
235 | free(s); | |
236 | /* high */ | |
237 | if (!(s = sr_samplerate_string(samplerates->high))) | |
238 | continue; | |
239 | printf(" - %s", s); | |
240 | free(s); | |
241 | /* step */ | |
242 | if (!(s = sr_samplerate_string(samplerates->step))) | |
243 | continue; | |
244 | printf(" in steps of %s)\n", s); | |
245 | free(s); | |
246 | } else { | |
247 | printf(" - supported samplerates:\n"); | |
248 | for (i = 0; samplerates->list[i]; i++) { | |
249 | printf(" %7s\n", sr_samplerate_string(samplerates->list[i])); | |
250 | } | |
251 | } | |
252 | } else { | |
253 | printf(" %s\n", hwo->shortname); | |
254 | } | |
255 | } | |
256 | } | |
257 | ||
258 | static void datafeed_in(struct sr_device *device, struct sr_datafeed_packet *packet) | |
259 | { | |
260 | static struct sr_output *o = NULL; | |
ee38f0be | 261 | static int probelist[SR_MAX_NUM_PROBES] = { 0 }; |
43e5747a UH |
262 | static uint64_t received_samples = 0; |
263 | static int unitsize = 0; | |
264 | static int triggered = 0; | |
265 | static FILE *outfile = NULL; | |
266 | struct sr_probe *probe; | |
267 | struct sr_datafeed_header *header; | |
268 | struct sr_datafeed_logic *logic; | |
269 | int num_enabled_probes, sample_size, ret, i; | |
270 | uint64_t output_len, filter_out_len; | |
271 | char *output_buf, *filter_out; | |
272 | ||
273 | /* If the first packet to come in isn't a header, don't even try. */ | |
274 | if (packet->type != SR_DF_HEADER && o == NULL) | |
275 | return; | |
276 | ||
277 | sample_size = -1; | |
278 | switch (packet->type) { | |
279 | case SR_DF_HEADER: | |
280 | g_message("cli: Received SR_DF_HEADER"); | |
281 | /* Initialize the output module. */ | |
282 | if (!(o = malloc(sizeof(struct sr_output)))) { | |
283 | printf("Output module malloc failed.\n"); | |
284 | exit(1); | |
285 | } | |
286 | o->format = output_format; | |
287 | o->device = device; | |
288 | o->param = output_format_param; | |
289 | if (o->format->init) { | |
290 | if (o->format->init(o) != SR_OK) { | |
291 | printf("Output format initialization failed.\n"); | |
292 | exit(1); | |
293 | } | |
294 | } | |
295 | ||
296 | header = packet->payload; | |
297 | num_enabled_probes = 0; | |
298 | for (i = 0; i < header->num_logic_probes; i++) { | |
299 | probe = g_slist_nth_data(device->probes, i); | |
300 | if (probe->enabled) | |
301 | probelist[num_enabled_probes++] = probe->index; | |
302 | } | |
303 | /* How many bytes we need to store num_enabled_probes bits */ | |
304 | unitsize = (num_enabled_probes + 7) / 8; | |
305 | ||
306 | outfile = stdout; | |
307 | if (opt_output_file) { | |
308 | if (default_output_format) { | |
309 | /* output file is in session format, which means we'll | |
310 | * dump everything in the datastore as it comes in, | |
311 | * and save from there after the session. */ | |
312 | outfile = NULL; | |
313 | ret = sr_datastore_new(unitsize, &(device->datastore)); | |
314 | if (ret != SR_OK) { | |
315 | printf("Failed to create datastore.\n"); | |
316 | exit(1); | |
317 | } | |
318 | } else { | |
319 | /* saving to a file in whatever format was set | |
320 | * with --format, so all we need is a filehandle */ | |
321 | outfile = g_fopen(opt_output_file, "wb"); | |
322 | } | |
323 | } | |
324 | if (opt_pds) | |
ee38f0be BV |
325 | srd_session_start(num_enabled_probes, unitsize, |
326 | header->samplerate); | |
43e5747a UH |
327 | break; |
328 | case SR_DF_END: | |
329 | g_message("cli: Received SR_DF_END"); | |
330 | if (!o) { | |
331 | g_message("cli: double end!"); | |
332 | break; | |
333 | } | |
334 | if (o->format->event) { | |
335 | o->format->event(o, SR_DF_END, &output_buf, &output_len); | |
336 | if (output_len) { | |
337 | if (outfile) | |
338 | fwrite(output_buf, 1, output_len, outfile); | |
339 | free(output_buf); | |
340 | output_len = 0; | |
341 | } | |
342 | } | |
343 | if (limit_samples && received_samples < limit_samples) | |
344 | printf("Device only sent %" PRIu64 " samples.\n", | |
345 | received_samples); | |
346 | if (opt_continuous) | |
347 | printf("Device stopped after %" PRIu64 " samples.\n", | |
348 | received_samples); | |
349 | sr_session_halt(); | |
350 | if (outfile && outfile != stdout) | |
351 | fclose(outfile); | |
352 | free(o); | |
353 | o = NULL; | |
354 | break; | |
355 | case SR_DF_TRIGGER: | |
356 | g_message("cli: received SR_DF_TRIGGER at %"PRIu64" ms", | |
357 | packet->timeoffset / 1000000); | |
358 | if (o->format->event) | |
359 | o->format->event(o, SR_DF_TRIGGER, &output_buf, | |
360 | &output_len); | |
361 | triggered = 1; | |
362 | break; | |
363 | case SR_DF_LOGIC: | |
364 | logic = packet->payload; | |
365 | sample_size = logic->unitsize; | |
366 | g_message("cli: received SR_DF_LOGIC at %f ms duration %f ms, %"PRIu64" bytes", | |
367 | packet->timeoffset / 1000000.0, packet->duration / 1000000.0, | |
368 | logic->length); | |
369 | break; | |
370 | case SR_DF_ANALOG: | |
371 | break; | |
372 | } | |
373 | ||
374 | /* not supporting anything but SR_DF_LOGIC for now */ | |
375 | ||
376 | if (sample_size == -1 || logic->length == 0) | |
377 | return; | |
378 | ||
379 | /* Don't store any samples until triggered. */ | |
380 | if (opt_wait_trigger && !triggered) | |
381 | return; | |
382 | ||
383 | if (limit_samples && received_samples >= limit_samples) | |
384 | return; | |
385 | ||
386 | /* TODO: filters only support SR_DF_LOGIC */ | |
387 | ret = sr_filter_probes(sample_size, unitsize, probelist, | |
388 | logic->data, logic->length, | |
389 | &filter_out, &filter_out_len); | |
390 | if (ret != SR_OK) | |
391 | return; | |
392 | ||
393 | /* what comes out of the filter is guaranteed to be packed into the | |
394 | * minimum size needed to support the number of samples at this sample | |
395 | * size. however, the driver may have submitted too much -- cut off | |
396 | * the buffer of the last packet according to the sample limit. | |
397 | */ | |
398 | if (limit_samples && (received_samples + logic->length / sample_size > | |
399 | limit_samples * sample_size)) | |
400 | filter_out_len = limit_samples * sample_size - received_samples; | |
401 | ||
402 | if (device->datastore) | |
403 | sr_datastore_put(device->datastore, filter_out, | |
404 | filter_out_len, sample_size, probelist); | |
405 | ||
406 | if (opt_output_file && default_output_format) | |
407 | /* saving to a session file, don't need to do anything else | |
408 | * to this data for now. */ | |
409 | goto cleanup; | |
410 | ||
411 | if (opt_pds) { | |
412 | if (srd_session_feed(packet->timeoffset, packet->duration, | |
413 | (uint8_t*)filter_out, filter_out_len) != SRD_OK) | |
414 | abort(); | |
415 | } else { | |
416 | output_len = 0; | |
417 | if (o->format->data && packet->type == o->format->df_type) | |
418 | o->format->data(o, filter_out, filter_out_len, &output_buf, &output_len); | |
419 | if (output_len) { | |
420 | fwrite(output_buf, 1, output_len, outfile); | |
421 | free(output_buf); | |
422 | } | |
423 | } | |
424 | ||
425 | cleanup: | |
426 | g_free(filter_out); | |
427 | received_samples += logic->length / sample_size; | |
428 | ||
429 | } | |
430 | ||
431 | /* Register the given PDs for this session. */ | |
432 | /* Accepts a string of the form: "spi:sck=3:sdata=4,spi:sck=3:sdata=5" | |
433 | * That will instantiate two SPI decoders on the clock but different data | |
434 | * lines. | |
435 | */ | |
436 | /* TODO: Support both serial PDs and nested PDs. Parallel PDs even? */ | |
437 | /* TODO: Only register here, run in streaming fashion later/elsewhere. */ | |
438 | static int register_pds(struct sr_device *device, const char *pdstring) | |
439 | { | |
440 | char **pdtokens, **pdtok; | |
441 | ||
442 | /* Avoid compiler warnings. */ | |
443 | (void)device; | |
444 | ||
445 | pdtokens = g_strsplit(pdstring, ",", -1); | |
446 | ||
447 | for (pdtok = pdtokens; *pdtok; pdtok++) { | |
448 | struct srd_decoder_instance *di; | |
449 | ||
450 | /* Configure probes from command line */ | |
451 | char **optokens, **optok; | |
452 | optokens = g_strsplit(*pdtok, ":", -1); | |
453 | di = srd_instance_new(optokens[0]); | |
454 | if(!di) { | |
455 | fprintf(stderr, "Failed to instantiate PD: %s\n", | |
456 | optokens[0]); | |
457 | g_strfreev(optokens); | |
458 | g_strfreev(pdtokens); | |
459 | return -1; | |
460 | } | |
461 | for (optok = optokens+1; *optok; optok++) { | |
462 | char probe[strlen(*optok)]; | |
463 | int num; | |
5aa4e6d9 UH |
464 | if (sscanf(*optok, "%[^=]=%d", probe, &num) == 2) { |
465 | printf("Setting probe '%s' to %d\n", probe, num); | |
43e5747a | 466 | srd_instance_set_probe(di, probe, num); |
5aa4e6d9 UH |
467 | } else { |
468 | fprintf(stderr, "Error: Couldn't parse decoder " | |
469 | "options correctly! Aborting.\n"); | |
470 | /* FIXME: Better error handling. */ | |
471 | exit(EXIT_FAILURE); | |
472 | } | |
43e5747a UH |
473 | } |
474 | g_strfreev(optokens); | |
475 | ||
476 | /* TODO: Handle errors. */ | |
477 | } | |
478 | ||
479 | g_strfreev(pdtokens); | |
480 | ||
481 | return 0; | |
482 | } | |
483 | ||
484 | static int select_probes(struct sr_device *device) | |
485 | { | |
486 | struct sr_probe *probe; | |
487 | char **probelist; | |
488 | int max_probes, i; | |
489 | ||
490 | if (!opt_probes) | |
491 | return SR_OK; | |
492 | ||
493 | /* | |
494 | * This only works because a device by default initializes | |
495 | * and enables all its probes. | |
496 | */ | |
497 | max_probes = g_slist_length(device->probes); | |
498 | probelist = parse_probestring(max_probes, opt_probes); | |
499 | if (!probelist) { | |
500 | return SR_ERR; | |
501 | } | |
502 | ||
503 | for (i = 0; i < max_probes; i++) { | |
504 | if (probelist[i]) { | |
505 | sr_device_probe_name(device, i + 1, probelist[i]); | |
506 | g_free(probelist[i]); | |
507 | } else { | |
508 | probe = sr_device_probe_find(device, i + 1); | |
509 | probe->enabled = FALSE; | |
510 | } | |
511 | } | |
512 | g_free(probelist); | |
513 | ||
514 | return SR_OK; | |
515 | } | |
516 | ||
517 | /** | |
518 | * Return the input file format which the CLI tool should use. | |
519 | * | |
520 | * If the user specified -I / --input-format, use that one. Otherwise, try to | |
521 | * autodetect the format as good as possible. Failing that, return NULL. | |
522 | * | |
523 | * @param filename The filename of the input file. Must not be NULL. | |
524 | * @param opt The -I / --input-file option the user specified (or NULL). | |
525 | * | |
526 | * @return A pointer to the 'struct sr_input_format' that should be used, | |
527 | * or NULL if no input format was selected or auto-detected. | |
528 | */ | |
529 | static struct sr_input_format *determine_input_file_format( | |
530 | const char *filename, const char *opt) | |
531 | { | |
532 | int i; | |
533 | struct sr_input_format **inputs; | |
534 | ||
535 | /* If there are no input formats, return NULL right away. */ | |
536 | inputs = sr_input_list(); | |
537 | if (!inputs) { | |
538 | fprintf(stderr, "cli: %s: no supported input formats " | |
539 | "available", __func__); | |
540 | return NULL; | |
541 | } | |
542 | ||
543 | /* If the user specified -I / --input-format, use that one. */ | |
544 | if (opt) { | |
545 | for (i = 0; inputs[i]; i++) { | |
546 | if (strcasecmp(inputs[i]->id, opt_input_format)) | |
547 | continue; | |
548 | printf("Using user-specified input file format" | |
549 | " '%s'.\n", inputs[i]->id); | |
550 | return inputs[i]; | |
551 | } | |
552 | ||
553 | /* The user specified an unknown input format, return NULL. */ | |
554 | fprintf(stderr, "Error: Specified input file format '%s' is " | |
555 | "unknown.\n", opt_input_format); | |
556 | return NULL; | |
557 | } | |
558 | ||
559 | /* Otherwise, try to find an input module that can handle this file. */ | |
560 | for (i = 0; inputs[i]; i++) { | |
561 | if (inputs[i]->format_match(filename)) | |
562 | break; | |
563 | } | |
564 | ||
565 | /* Return NULL if no input module wanted to touch this. */ | |
566 | if (!inputs[i]) { | |
567 | fprintf(stderr, "Error: No matching input module found.\n"); | |
568 | return NULL; | |
569 | } | |
570 | ||
571 | printf("Using input file format '%s'.\n", inputs[i]->id); | |
572 | return inputs[i]; | |
573 | } | |
574 | ||
575 | static void load_input_file_format(void) | |
576 | { | |
577 | struct stat st; | |
578 | struct sr_input *in; | |
579 | struct sr_input_format *input_format; | |
580 | ||
581 | input_format = determine_input_file_format(opt_input_file, | |
582 | opt_input_format); | |
583 | if (!input_format) { | |
584 | fprintf(stderr, "Error: Couldn't detect input file format.\n"); | |
585 | return; | |
586 | } | |
587 | ||
588 | if (stat(opt_input_file, &st) == -1) { | |
589 | printf("Failed to load %s: %s\n", opt_input_file, | |
590 | strerror(errno)); | |
591 | exit(1); | |
592 | } | |
593 | ||
594 | /* Initialize the input module. */ | |
595 | if (!(in = malloc(sizeof(struct sr_input)))) { | |
596 | printf("Failed to allocate input module.\n"); | |
597 | exit(1); | |
598 | } | |
599 | in->format = input_format; | |
600 | in->param = input_format_param; | |
601 | if (in->format->init) { | |
602 | if (in->format->init(in) != SR_OK) { | |
603 | printf("Input format init failed.\n"); | |
604 | exit(1); | |
605 | } | |
606 | } | |
607 | ||
608 | if (select_probes(in->vdevice) > 0) | |
609 | return; | |
610 | ||
611 | sr_session_new(); | |
612 | sr_session_datafeed_callback_add(datafeed_in); | |
613 | if (sr_session_device_add(in->vdevice) != SR_OK) { | |
614 | printf("Failed to use device.\n"); | |
615 | sr_session_destroy(); | |
616 | return; | |
617 | } | |
618 | ||
619 | input_format->loadfile(in, opt_input_file); | |
620 | if (opt_output_file && default_output_format) { | |
621 | if (sr_session_save(opt_output_file) != SR_OK) | |
622 | printf("Failed to save session.\n"); | |
623 | } | |
624 | sr_session_destroy(); | |
625 | ||
626 | } | |
627 | ||
628 | static void load_input_file(void) | |
629 | { | |
630 | ||
631 | if (sr_session_load(opt_input_file) == SR_OK) { | |
632 | /* sigrok session file */ | |
633 | sr_session_datafeed_callback_add(datafeed_in); | |
634 | sr_session_start(); | |
635 | sr_session_run(); | |
636 | sr_session_stop(); | |
637 | } | |
638 | else { | |
639 | /* fall back on input modules */ | |
640 | load_input_file_format(); | |
641 | } | |
642 | ||
643 | } | |
644 | ||
645 | int num_real_devices(void) | |
646 | { | |
647 | struct sr_device *device; | |
648 | GSList *devices, *l; | |
649 | int num_devices; | |
650 | ||
651 | num_devices = 0; | |
652 | devices = sr_device_list(); | |
653 | for (l = devices; l; l = l->next) { | |
654 | device = l->data; | |
655 | if (!strstr(device->plugin->name, "demo")) | |
656 | num_devices++; | |
657 | } | |
658 | ||
659 | return num_devices; | |
660 | } | |
661 | ||
1999ae06 | 662 | static int set_device_options(struct sr_device *device, GHashTable *args) |
43e5747a UH |
663 | { |
664 | GHashTableIter iter; | |
665 | gpointer key, value; | |
666 | int ret, i; | |
667 | uint64_t tmp_u64; | |
668 | gboolean found; | |
669 | gboolean tmp_bool; | |
670 | ||
671 | g_hash_table_iter_init(&iter, args); | |
672 | while (g_hash_table_iter_next(&iter, &key, &value)) { | |
673 | found = FALSE; | |
674 | for (i = 0; sr_hwcap_options[i].capability; i++) { | |
675 | if (strcmp(sr_hwcap_options[i].shortname, key)) | |
676 | continue; | |
677 | if ((value == NULL) && | |
678 | (sr_hwcap_options[i].type != SR_T_BOOL)) { | |
679 | printf("Option '%s' needs a value.\n", (char *)key); | |
680 | return SR_ERR; | |
681 | } | |
682 | found = TRUE; | |
683 | switch (sr_hwcap_options[i].type) { | |
684 | case SR_T_UINT64: | |
685 | ret = sr_parse_sizestring(value, &tmp_u64); | |
686 | if (ret != SR_OK) | |
687 | break; | |
688 | ret = device->plugin-> set_configuration(device-> plugin_index, | |
689 | sr_hwcap_options[i]. capability, &tmp_u64); | |
690 | break; | |
691 | case SR_T_CHAR: | |
692 | ret = device->plugin-> set_configuration(device-> plugin_index, | |
693 | sr_hwcap_options[i]. capability, value); | |
694 | break; | |
695 | case SR_T_BOOL: | |
696 | if (!value) | |
697 | tmp_bool = TRUE; | |
698 | else | |
699 | tmp_bool = sr_parse_boolstring(value); | |
700 | ret = device->plugin-> set_configuration(device-> plugin_index, | |
701 | sr_hwcap_options[i]. capability, | |
702 | GINT_TO_POINTER(tmp_bool)); | |
703 | break; | |
704 | default: | |
705 | ret = SR_ERR; | |
706 | } | |
707 | ||
708 | if (ret != SR_OK) { | |
709 | printf("Failed to set device option '%s'.\n", (char *)key); | |
710 | return ret; | |
711 | } | |
712 | else | |
713 | break; | |
714 | } | |
715 | if (!found) { | |
716 | printf("Unknown device option '%s'.\n", (char *) key); | |
717 | return SR_ERR; | |
718 | } | |
719 | } | |
720 | ||
721 | return SR_OK; | |
722 | } | |
723 | ||
724 | static void run_session(void) | |
725 | { | |
726 | struct sr_device *device; | |
727 | GHashTable *devargs; | |
728 | int num_devices, max_probes, *capabilities, i; | |
729 | uint64_t tmp_u64, time_msec; | |
730 | char **probelist, *devspec; | |
731 | ||
732 | devargs = NULL; | |
733 | if (opt_device) { | |
734 | devargs = parse_generic_arg(opt_device); | |
735 | devspec = g_hash_table_lookup(devargs, "sigrok_key"); | |
736 | device = parse_devicestring(devspec); | |
737 | if (!device) { | |
738 | g_warning("Device not found."); | |
739 | return; | |
740 | } | |
741 | g_hash_table_remove(devargs, "sigrok_key"); | |
742 | } else { | |
743 | num_devices = num_real_devices(); | |
744 | if (num_devices == 1) { | |
745 | /* No device specified, but there is only one. */ | |
746 | devargs = NULL; | |
747 | device = parse_devicestring("0"); | |
748 | } else if (num_devices == 0) { | |
749 | printf("No devices found.\n"); | |
750 | return; | |
751 | } else { | |
752 | printf("%d devices found, please select one.\n", num_devices); | |
753 | return; | |
754 | } | |
755 | } | |
756 | ||
757 | sr_session_new(); | |
758 | sr_session_datafeed_callback_add(datafeed_in); | |
759 | ||
760 | if (sr_session_device_add(device) != SR_OK) { | |
761 | printf("Failed to use device.\n"); | |
762 | sr_session_destroy(); | |
763 | return; | |
764 | } | |
765 | ||
766 | if (devargs) { | |
767 | if (set_device_options(device, devargs) != SR_OK) { | |
768 | sr_session_destroy(); | |
769 | return; | |
770 | } | |
771 | g_hash_table_destroy(devargs); | |
772 | } | |
773 | ||
774 | if (select_probes(device) != SR_OK) | |
775 | return; | |
776 | ||
777 | if (opt_continuous) { | |
778 | capabilities = device->plugin->get_capabilities(); | |
779 | if (!sr_find_hwcap(capabilities, SR_HWCAP_CONTINUOUS)) { | |
780 | printf("This device does not support continuous sampling."); | |
781 | sr_session_destroy(); | |
782 | return; | |
783 | } | |
784 | } | |
785 | ||
786 | if (opt_triggers) { | |
787 | probelist = sr_parse_triggerstring(device, opt_triggers); | |
788 | if (!probelist) { | |
789 | sr_session_destroy(); | |
790 | return; | |
791 | } | |
792 | ||
793 | max_probes = g_slist_length(device->probes); | |
794 | for (i = 0; i < max_probes; i++) { | |
795 | if (probelist[i]) { | |
796 | sr_device_trigger_set(device, i + 1, probelist[i]); | |
797 | g_free(probelist[i]); | |
798 | } | |
799 | } | |
800 | g_free(probelist); | |
801 | } | |
802 | ||
803 | if (opt_time) { | |
804 | time_msec = sr_parse_timestring(opt_time); | |
805 | if (time_msec == 0) { | |
806 | printf("Invalid time '%s'\n", opt_time); | |
807 | sr_session_destroy(); | |
808 | return; | |
809 | } | |
810 | ||
811 | capabilities = device->plugin->get_capabilities(); | |
812 | if (sr_find_hwcap(capabilities, SR_HWCAP_LIMIT_MSEC)) { | |
813 | if (device->plugin->set_configuration(device->plugin_index, | |
814 | SR_HWCAP_LIMIT_MSEC, &time_msec) != SR_OK) { | |
815 | printf("Failed to configure time limit.\n"); | |
816 | sr_session_destroy(); | |
817 | return; | |
818 | } | |
819 | } | |
820 | else { | |
821 | /* time limit set, but device doesn't support this... | |
822 | * convert to samples based on the samplerate. | |
823 | */ | |
824 | limit_samples = 0; | |
825 | if (sr_device_has_hwcap(device, SR_HWCAP_SAMPLERATE)) { | |
826 | tmp_u64 = *((uint64_t *) device->plugin->get_device_info( | |
827 | device->plugin_index, SR_DI_CUR_SAMPLERATE)); | |
828 | limit_samples = tmp_u64 * time_msec / (uint64_t) 1000; | |
829 | } | |
830 | if (limit_samples == 0) { | |
831 | printf("Not enough time at this samplerate.\n"); | |
832 | sr_session_destroy(); | |
833 | return; | |
834 | } | |
835 | ||
836 | if (device->plugin->set_configuration(device->plugin_index, | |
837 | SR_HWCAP_LIMIT_SAMPLES, &limit_samples) != SR_OK) { | |
838 | printf("Failed to configure time-based sample limit.\n"); | |
839 | sr_session_destroy(); | |
840 | return; | |
841 | } | |
842 | } | |
843 | } | |
844 | ||
845 | if (opt_samples) { | |
846 | if ((sr_parse_sizestring(opt_samples, &limit_samples) != SR_OK) | |
847 | || (device->plugin->set_configuration(device->plugin_index, | |
848 | SR_HWCAP_LIMIT_SAMPLES, &limit_samples) != SR_OK)) { | |
849 | printf("Failed to configure sample limit.\n"); | |
850 | sr_session_destroy(); | |
851 | return; | |
852 | } | |
853 | } | |
854 | ||
855 | if (device->plugin->set_configuration(device->plugin_index, | |
856 | SR_HWCAP_PROBECONFIG, (char *)device->probes) != SR_OK) { | |
857 | printf("Failed to configure probes.\n"); | |
858 | sr_session_destroy(); | |
859 | return; | |
860 | } | |
861 | ||
862 | if (sr_session_start() != SR_OK) { | |
863 | printf("Failed to start session.\n"); | |
864 | sr_session_destroy(); | |
865 | return; | |
866 | } | |
867 | ||
868 | if (opt_continuous) | |
869 | add_anykey(); | |
870 | ||
871 | sr_session_run(); | |
872 | ||
873 | if (opt_continuous) | |
874 | clear_anykey(); | |
875 | ||
876 | if (opt_output_file && default_output_format) { | |
877 | if (sr_session_save(opt_output_file) != SR_OK) | |
878 | printf("Failed to save session.\n"); | |
879 | } | |
880 | sr_session_destroy(); | |
881 | ||
882 | } | |
883 | ||
884 | static void logger(const gchar *log_domain, GLogLevelFlags log_level, | |
885 | const gchar *message, gpointer user_data) | |
886 | { | |
887 | /* Avoid compiler warnings. */ | |
888 | (void)log_domain; | |
889 | (void)user_data; | |
890 | ||
891 | /* | |
892 | * All messages, warnings, errors etc. go to stderr (not stdout) in | |
893 | * order to not mess up the CLI tool data output, e.g. VCD output. | |
894 | */ | |
895 | if (log_level & (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_WARNING)) { | |
896 | fprintf(stderr, "%s\n", message); | |
897 | fflush(stderr); | |
898 | } else { | |
899 | if ((log_level & G_LOG_LEVEL_MESSAGE && debug == 1) | |
900 | || debug == 2) { | |
901 | printf("%s\n", message); | |
902 | fflush(stderr); | |
903 | } | |
904 | } | |
905 | } | |
906 | ||
907 | int main(int argc, char **argv) | |
908 | { | |
909 | struct sr_output_format **outputs; | |
910 | GOptionContext *context; | |
911 | GError *error; | |
912 | GHashTable *fmtargs; | |
913 | GHashTableIter iter; | |
914 | gpointer key, value; | |
915 | int i; | |
916 | char *fmtspec; | |
917 | ||
918 | g_log_set_default_handler(logger, NULL); | |
919 | if (getenv("SIGROK_DEBUG")) | |
920 | debug = strtol(getenv("SIGROK_DEBUG"), NULL, 10); | |
921 | ||
922 | error = NULL; | |
923 | context = g_option_context_new(NULL); | |
924 | g_option_context_add_main_entries(context, optargs, NULL); | |
925 | ||
926 | if (!g_option_context_parse(context, &argc, &argv, &error)) { | |
927 | g_warning("%s", error->message); | |
928 | return 1; | |
929 | } | |
930 | ||
931 | /* Set the loglevel (amount of messages to output) for libsigrok. */ | |
932 | if (sr_set_loglevel(opt_loglevel) != SR_OK) { | |
933 | fprintf(stderr, "cli: %s: sr_set_loglevel(%d) failed\n", | |
934 | __func__, opt_loglevel); | |
935 | return 1; | |
936 | } | |
937 | ||
938 | if (sr_init() != SR_OK) | |
939 | return 1; | |
940 | ||
941 | if (opt_pds) { | |
942 | /* TODO: Error handling. */ | |
943 | srd_init(); | |
944 | register_pds(NULL, opt_pds); | |
945 | } | |
946 | ||
947 | if (!opt_format) { | |
948 | opt_format = DEFAULT_OUTPUT_FORMAT; | |
949 | /* we'll need to remember this, so when saving to an file | |
950 | * later, sigrok session format will be used. | |
951 | */ | |
952 | default_output_format = TRUE; | |
953 | } | |
954 | fmtargs = parse_generic_arg(opt_format); | |
955 | fmtspec = g_hash_table_lookup(fmtargs, "sigrok_key"); | |
956 | if (!fmtspec) { | |
957 | printf("Invalid output format.\n"); | |
958 | return 1; | |
959 | } | |
960 | outputs = sr_output_list(); | |
961 | for (i = 0; outputs[i]; i++) { | |
962 | if (strcmp(outputs[i]->id, fmtspec)) | |
963 | continue; | |
964 | g_hash_table_remove(fmtargs, "sigrok_key"); | |
965 | output_format = outputs[i]; | |
966 | g_hash_table_iter_init(&iter, fmtargs); | |
967 | while (g_hash_table_iter_next(&iter, &key, &value)) { | |
968 | /* only supporting one parameter per output module | |
969 | * for now, and only its value */ | |
970 | output_format_param = g_strdup(value); | |
971 | break; | |
972 | } | |
973 | break; | |
974 | } | |
975 | if (!output_format) { | |
976 | printf("invalid output format %s\n", opt_format); | |
977 | return 1; | |
978 | } | |
979 | ||
980 | if (opt_version) | |
981 | show_version(); | |
982 | else if (opt_list_devices) | |
983 | show_device_list(); | |
984 | else if (opt_input_file) | |
985 | load_input_file(); | |
986 | else if (opt_samples || opt_time || opt_continuous) | |
987 | run_session(); | |
988 | else if (opt_device) | |
989 | show_device_detail(); | |
990 | else | |
991 | printf("%s", g_option_context_get_help(context, TRUE, NULL)); | |
992 | ||
993 | if (opt_pds) | |
994 | srd_exit(); | |
995 | ||
996 | g_option_context_free(context); | |
997 | g_hash_table_destroy(fmtargs); | |
998 | sr_exit(); | |
999 | ||
1000 | return 0; | |
1001 | } |