]>
Commit | Line | Data |
---|---|---|
fbd226c3 BV |
1 | /* |
2 | * This file is part of the libsigrokdecode project. | |
3 | * | |
4 | * Copyright (C) 2013 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 "../libsigrokdecode.h" | |
21 | #include <libsigrok/libsigrok.h> | |
22 | #include <stdlib.h> | |
23 | #include <stdio.h> | |
24 | #include <stdarg.h> | |
25 | #include <unistd.h> | |
26 | #include <errno.h> | |
27 | #include <sys/types.h> | |
28 | #include <sys/stat.h> | |
29 | #include <fcntl.h> | |
30 | #include <time.h> | |
31 | #include <sys/time.h> | |
32 | #include <sys/resource.h> | |
33 | #include <glib.h> | |
34 | #ifdef __LINUX__ | |
35 | #include <sched.h> | |
36 | #endif | |
37 | #include "../config.h" | |
38 | ||
39 | ||
40 | int debug = FALSE; | |
41 | int statistics = FALSE; | |
42 | ||
43 | struct probe { | |
44 | char *name; | |
45 | int probe; | |
46 | }; | |
47 | ||
48 | struct option { | |
49 | char *key; | |
50 | char *value; | |
51 | }; | |
52 | ||
53 | struct pd { | |
54 | char *name; | |
55 | GSList *probes; | |
56 | GSList *options; | |
57 | }; | |
58 | ||
59 | struct output { | |
60 | char *pd; | |
61 | int type; | |
62 | char *class; | |
63 | int class_idx; | |
64 | char *outfile; | |
65 | int outfd; | |
66 | }; | |
67 | ||
68 | ||
69 | void logmsg(char *prefix, FILE *out, const char *format, va_list args) | |
70 | { | |
71 | if (prefix) | |
72 | fprintf(out, "%s", prefix); | |
73 | vfprintf(out, format, args); | |
74 | fprintf(out, "\n"); | |
75 | } | |
76 | ||
77 | void DBG(const char *format, ...) | |
78 | { | |
79 | va_list args; | |
80 | ||
81 | if (!debug) | |
82 | return; | |
83 | va_start(args, format); | |
84 | logmsg("DBG: ", stdout, format, args); | |
85 | va_end(args); | |
86 | } | |
87 | ||
88 | void ERR(const char *format, ...) | |
89 | { | |
90 | va_list args; | |
91 | ||
92 | va_start(args, format); | |
93 | logmsg("Error: ", stderr, format, args); | |
94 | va_end(args); | |
95 | } | |
96 | ||
caa4b2cc BV |
97 | int sr_log(void *cb_data, int loglevel, const char *format, va_list args) |
98 | { | |
99 | (void)cb_data; | |
100 | ||
101 | if (loglevel == SR_LOG_ERR || loglevel == SR_LOG_WARN) | |
102 | logmsg("Error: sr: ", stderr, format, args); | |
103 | else if (debug) | |
104 | logmsg("DBG: sr: ", stdout, format, args); | |
105 | ||
106 | return SRD_OK; | |
107 | } | |
108 | ||
fbd226c3 BV |
109 | int srd_log(void *cb_data, int loglevel, const char *format, va_list args) |
110 | { | |
111 | (void)cb_data; | |
112 | ||
113 | if (loglevel == SRD_LOG_ERR || loglevel == SRD_LOG_WARN) | |
114 | logmsg("Error: srd: ", stderr, format, args); | |
caa4b2cc | 115 | else if (debug) |
fbd226c3 BV |
116 | logmsg("DBG: srd: ", stdout, format, args); |
117 | ||
118 | return SRD_OK; | |
119 | } | |
120 | ||
121 | void usage(char *msg) | |
122 | { | |
123 | if (msg) | |
124 | fprintf(stderr, "%s\n", msg); | |
125 | ||
fbd226c3 BV |
126 | printf("Usage: runtc [-dPpoiOf]\n"); |
127 | printf(" -d Debug\n"); | |
128 | printf(" -P <protocol decoder>\n"); | |
129 | printf(" -p <probename=probenum> (optional)\n"); | |
130 | printf(" -o <probeoption=value> (optional)\n"); | |
131 | printf(" -i <input file>\n"); | |
132 | printf(" -O <output-pd:output-type[:output-class]>\n"); | |
133 | printf(" -f <output file> (optional)\n"); | |
134 | exit(msg ? 1 : 0); | |
135 | ||
136 | } | |
137 | ||
b7e15e0e BV |
138 | /* This is a neutered version of libsigrokdecode's py_str_as_str(). It |
139 | * does no error checking, but then the only strings it processes are | |
140 | * generated by Python's repr(), so are known good. */ | |
141 | char *py_str_as_str(const PyObject *py_str) | |
142 | { | |
143 | PyObject *py_encstr; | |
144 | char *str, *outstr; | |
145 | ||
146 | py_encstr = PyUnicode_AsEncodedString((PyObject *)py_str, "utf-8", NULL); | |
147 | str = PyBytes_AS_STRING(py_encstr); | |
148 | outstr = g_strdup(str); | |
149 | Py_DecRef(py_encstr); | |
150 | ||
151 | return outstr; | |
152 | } | |
153 | ||
154 | static void srd_cb_py(struct srd_proto_data *pdata, void *cb_data) | |
d7d693b5 | 155 | { |
d7d693b5 | 156 | struct output *op; |
b7e15e0e BV |
157 | PyObject *pydata, *pyrepr; |
158 | GString *out; | |
159 | char *s; | |
d7d693b5 | 160 | |
b7e15e0e | 161 | DBG("Python output from %s", pdata->pdo->di->inst_id); |
d7d693b5 | 162 | op = cb_data; |
b7e15e0e BV |
163 | pydata = pdata->data; |
164 | DBG("ptr %p", pydata); | |
165 | ||
166 | if (strcmp(pdata->pdo->di->inst_id, op->pd)) | |
167 | /* This is not the PD selected for output. */ | |
168 | return; | |
169 | ||
170 | if (!(pyrepr = PyObject_Repr(pydata))) { | |
171 | ERR("Invalid Python object."); | |
d7d693b5 | 172 | return; |
b7e15e0e BV |
173 | } |
174 | s = py_str_as_str(pyrepr); | |
175 | Py_DecRef(pyrepr); | |
176 | ||
177 | /* Output format for testing is '<ss>-<es> <inst-id>: <repr>\n' */ | |
178 | out = g_string_sized_new(128); | |
179 | g_string_printf(out, "%"PRIu64"-%"PRIu64" %s: %s\n", | |
180 | pdata->start_sample, pdata->end_sample, | |
181 | pdata->pdo->di->inst_id, s); | |
182 | g_free(s); | |
183 | if (write(op->outfd, out->str, out->len) == -1) | |
184 | ERR("SRD_OUTPUT_PYTHON callback write failure!"); | |
185 | DBG("wrote '%s'", out->str); | |
186 | g_string_free(out, TRUE); | |
187 | ||
188 | } | |
189 | ||
190 | static void srd_cb_bin(struct srd_proto_data *pdata, void *cb_data) | |
191 | { | |
192 | struct srd_proto_data_binary *pdb; | |
193 | struct output *op; | |
97578cb1 BV |
194 | GString *out; |
195 | unsigned int i; | |
d7d693b5 BV |
196 | |
197 | DBG("Binary output from %s", pdata->pdo->di->inst_id); | |
b7e15e0e | 198 | op = cb_data; |
d7d693b5 BV |
199 | pdb = pdata->data; |
200 | ||
201 | if (strcmp(pdata->pdo->di->inst_id, op->pd)) | |
202 | /* This is not the PD selected for output. */ | |
203 | return; | |
204 | ||
205 | if (op->class_idx != -1 && op->class_idx != pdb->bin_class) | |
206 | /* | |
207 | * This output takes a specific binary class, | |
208 | * but not the one that just came in. | |
209 | */ | |
210 | return; | |
211 | ||
97578cb1 BV |
212 | out = g_string_sized_new(128); |
213 | g_string_printf(out, "%"PRIu64"-%"PRIu64" %s:", | |
214 | pdata->start_sample, pdata->end_sample, | |
215 | pdata->pdo->di->inst_id); | |
216 | for (i = 0; i < pdb->size; i++) { | |
217 | g_string_append_printf(out, " %.2x", pdb->data[i]); | |
218 | } | |
219 | g_string_append(out, "\n"); | |
220 | if (write(op->outfd, out->str, out->len) == -1) | |
b7e15e0e | 221 | ERR("SRD_OUTPUT_BINARY callback write failure!"); |
d7d693b5 BV |
222 | |
223 | } | |
224 | ||
fbd226c3 BV |
225 | static void srd_cb_ann(struct srd_proto_data *pdata, void *cb_data) |
226 | { | |
227 | struct srd_decoder *dec; | |
228 | struct srd_proto_data_annotation *pda; | |
229 | struct output *op; | |
230 | GString *line; | |
231 | int i; | |
232 | char **dec_ann; | |
233 | ||
d7d693b5 | 234 | DBG("Annotation output from %s", pdata->pdo->di->inst_id); |
b7e15e0e | 235 | op = cb_data; |
fbd226c3 BV |
236 | pda = pdata->data; |
237 | dec = pdata->pdo->di->decoder; | |
fbd226c3 BV |
238 | if (strcmp(pdata->pdo->di->inst_id, op->pd)) |
239 | /* This is not the PD selected for output. */ | |
240 | return; | |
241 | ||
242 | if (op->class_idx != -1 && op->class_idx != pda->ann_format) | |
d7d693b5 BV |
243 | /* |
244 | * This output takes a specific annotation class, | |
245 | * but not the one that just came in. | |
246 | */ | |
fbd226c3 BV |
247 | return; |
248 | ||
249 | dec_ann = g_slist_nth_data(dec->annotations, pda->ann_format); | |
250 | line = g_string_sized_new(256); | |
251 | g_string_printf(line, "%"PRIu64"-%"PRIu64" %s: %s:", | |
252 | pdata->start_sample, pdata->end_sample, | |
253 | pdata->pdo->di->inst_id, dec_ann[0]); | |
254 | for (i = 0; pda->ann_text[i]; i++) | |
255 | g_string_append_printf(line, " \"%s\"", pda->ann_text[i]); | |
256 | g_string_append(line, "\n"); | |
257 | if (write(op->outfd, line->str, line->len) == -1) | |
b7e15e0e | 258 | ERR("SRD_OUTPUT_ANN callback write failure!"); |
fbd226c3 BV |
259 | g_string_free(line, TRUE); |
260 | ||
261 | } | |
262 | ||
263 | static void sr_cb(const struct sr_dev_inst *sdi, | |
264 | const struct sr_datafeed_packet *packet, void *cb_data) | |
265 | { | |
266 | const struct sr_datafeed_logic *logic; | |
267 | struct srd_session *sess; | |
268 | GVariant *gvar; | |
269 | uint64_t samplerate; | |
270 | int num_samples; | |
271 | static int samplecnt = 0; | |
272 | ||
273 | sess = cb_data; | |
274 | ||
275 | switch (packet->type) { | |
276 | case SR_DF_HEADER: | |
277 | DBG("Received SR_DF_HEADER"); | |
278 | if (sr_config_get(sdi->driver, sdi, NULL, SR_CONF_SAMPLERATE, | |
279 | &gvar) != SR_OK) { | |
280 | ERR("Getting samplerate failed"); | |
281 | break; | |
282 | } | |
283 | samplerate = g_variant_get_uint64(gvar); | |
284 | g_variant_unref(gvar); | |
285 | if (srd_session_metadata_set(sess, SRD_CONF_SAMPLERATE, | |
286 | g_variant_new_uint64(samplerate)) != SRD_OK) { | |
287 | ERR("Setting samplerate failed"); | |
288 | break; | |
289 | } | |
290 | if (srd_session_start(sess) != SRD_OK) { | |
291 | ERR("Session start failed"); | |
292 | break; | |
293 | } | |
294 | break; | |
295 | case SR_DF_LOGIC: | |
296 | logic = packet->payload; | |
297 | num_samples = logic->length / logic->unitsize; | |
298 | DBG("Received SR_DF_LOGIC: %d samples", num_samples); | |
299 | srd_session_send(sess, samplecnt, samplecnt + num_samples, | |
300 | logic->data, logic->length); | |
301 | samplecnt += logic->length / logic->unitsize; | |
302 | break; | |
303 | case SR_DF_END: | |
304 | DBG("Received SR_DF_END"); | |
305 | break; | |
306 | } | |
307 | ||
308 | } | |
309 | ||
fbd226c3 BV |
310 | static int run_testcase(char *infile, GSList *pdlist, struct output *op) |
311 | { | |
312 | struct srd_session *sess; | |
313 | struct srd_decoder *dec; | |
314 | struct srd_decoder_inst *di, *prev_di; | |
b7e15e0e | 315 | srd_pd_output_callback_t cb; |
fbd226c3 BV |
316 | struct pd *pd; |
317 | struct probe *probe; | |
318 | struct option *option; | |
319 | GVariant *gvar; | |
320 | GHashTable *probes, *opts; | |
6b85745a | 321 | GSList *pdl, *l; |
fbd226c3 | 322 | int idx; |
6b85745a | 323 | char **decoder_class; |
fbd226c3 BV |
324 | |
325 | if (op->outfile) { | |
326 | if ((op->outfd = open(op->outfile, O_CREAT|O_WRONLY, 0600)) == -1) { | |
327 | ERR("Unable to open %s for writing: %s", op->outfile, | |
328 | strerror(errno)); | |
329 | return FALSE; | |
330 | } | |
331 | } | |
332 | ||
333 | if (sr_session_load(infile) != SR_OK) | |
334 | return FALSE; | |
335 | ||
336 | if (srd_session_new(&sess) != SRD_OK) | |
337 | return FALSE; | |
338 | sr_session_datafeed_callback_add(sr_cb, sess); | |
b7e15e0e BV |
339 | switch (op->type) { |
340 | case SRD_OUTPUT_ANN: | |
341 | cb = srd_cb_ann; | |
342 | break; | |
343 | case SRD_OUTPUT_BINARY: | |
344 | cb = srd_cb_bin; | |
345 | break; | |
346 | case SRD_OUTPUT_PYTHON: | |
347 | cb = srd_cb_py; | |
348 | break; | |
349 | default: | |
350 | return FALSE; | |
351 | } | |
352 | srd_pd_output_callback_add(sess, op->type, cb, op); | |
fbd226c3 BV |
353 | |
354 | prev_di = NULL; | |
355 | pd = NULL; | |
356 | for (pdl = pdlist; pdl; pdl = pdl->next) { | |
357 | pd = pdl->data; | |
358 | if (srd_decoder_load(pd->name) != SRD_OK) | |
359 | return FALSE; | |
360 | ||
361 | /* Instantiate decoder and pass in options. */ | |
362 | opts = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, | |
363 | (GDestroyNotify)g_variant_unref); | |
364 | for (l = pd->options; l; l = l->next) { | |
365 | option = l->data; | |
366 | g_hash_table_insert(opts, option->key, option->value); | |
367 | } | |
368 | if (!(di = srd_inst_new(sess, pd->name, opts))) | |
369 | return FALSE; | |
370 | g_hash_table_destroy(opts); | |
371 | ||
372 | /* Map probes. */ | |
373 | if (pd->probes) { | |
374 | probes = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, | |
375 | (GDestroyNotify)g_variant_unref); | |
376 | for (l = pd->probes; l; l = l->next) { | |
377 | probe = l->data; | |
378 | gvar = g_variant_new_int32(probe->probe); | |
379 | g_variant_ref_sink(gvar); | |
380 | g_hash_table_insert(probes, probe->name, gvar); | |
381 | } | |
382 | if (srd_inst_probe_set_all(di, probes) != SRD_OK) | |
383 | return FALSE; | |
384 | g_hash_table_destroy(probes); | |
385 | } | |
386 | ||
fbd226c3 BV |
387 | /* If this is not the first decoder in the list, stack it |
388 | * on top of the previous one. */ | |
389 | if (prev_di) { | |
390 | if (srd_inst_stack(sess, prev_di, di) != SRD_OK) { | |
391 | ERR("Failed to stack decoder instances."); | |
392 | return FALSE; | |
393 | } | |
394 | } | |
395 | prev_di = di; | |
396 | } | |
397 | ||
398 | /* Resolve top decoder's class index, so we can match. */ | |
399 | dec = srd_decoder_get_by_id(pd->name); | |
400 | if (op->class) { | |
401 | if (op->type == SRD_OUTPUT_ANN) | |
6b85745a | 402 | l = dec->annotations; |
fbd226c3 | 403 | else if (op->type == SRD_OUTPUT_BINARY) |
6b85745a | 404 | l = dec->binary; |
fbd226c3 | 405 | else |
b7e15e0e | 406 | /* Only annotations and binary can have a class. */ |
fbd226c3 BV |
407 | return FALSE; |
408 | idx = 0; | |
6b85745a BV |
409 | while(l) { |
410 | decoder_class = l->data; | |
411 | if (!strcmp(decoder_class[0], op->class)) { | |
fbd226c3 BV |
412 | op->class_idx = idx; |
413 | break; | |
414 | } else | |
415 | idx++; | |
6b85745a | 416 | l = l->next; |
fbd226c3 BV |
417 | } |
418 | if (op->class_idx == -1) { | |
419 | ERR("Output class '%s' not found in decoder %s.", | |
420 | op->class, pd->name); | |
421 | return FALSE; | |
d7d693b5 BV |
422 | } else |
423 | DBG("Class %s index is %d", op->class, op->class_idx); | |
fbd226c3 BV |
424 | } |
425 | ||
426 | sr_session_start(); | |
427 | sr_session_run(); | |
428 | sr_session_stop(); | |
429 | ||
430 | srd_session_destroy(sess); | |
431 | ||
432 | if (op->outfile) | |
433 | close(op->outfd); | |
434 | ||
435 | return TRUE; | |
436 | } | |
437 | ||
438 | int main(int argc, char **argv) | |
439 | { | |
440 | struct sr_context *ctx; | |
441 | GSList *pdlist; | |
442 | struct pd *pd; | |
443 | struct probe *probe; | |
444 | struct option *option; | |
445 | struct output *op; | |
932606db | 446 | int ret; |
fbd226c3 BV |
447 | char c, *opt_infile, **kv, **opstr; |
448 | ||
449 | op = malloc(sizeof(struct output)); | |
450 | op->pd = NULL; | |
451 | op->type = -1; | |
452 | op->class = NULL; | |
453 | op->class_idx = -1; | |
454 | op->outfd = 1; | |
455 | ||
456 | pdlist = NULL; | |
457 | opt_infile = NULL; | |
458 | pd = NULL; | |
459 | while((c = getopt(argc, argv, "dP:p:o:i:O:f:S")) != -1) { | |
460 | switch(c) { | |
461 | case 'd': | |
462 | debug = TRUE; | |
463 | break; | |
464 | case 'P': | |
465 | pd = g_malloc(sizeof(struct pd)); | |
466 | pd->name = g_strdup(optarg); | |
467 | pd->probes = pd->options = NULL; | |
468 | pdlist = g_slist_append(pdlist, pd); | |
469 | break; | |
470 | case 'p': | |
471 | case 'o': | |
472 | if (g_slist_length(pdlist) == 0) { | |
473 | /* No previous -P. */ | |
474 | ERR("Syntax error at '%s'", optarg); | |
475 | usage(NULL); | |
476 | } | |
477 | kv = g_strsplit(optarg, "=", 0); | |
478 | if (!kv[0] || (!kv[1] || kv[2])) { | |
479 | /* Need x=y. */ | |
480 | ERR("Syntax error at '%s'", optarg); | |
481 | g_strfreev(kv); | |
482 | usage(NULL); | |
483 | } | |
484 | if (c == 'p') { | |
485 | probe = malloc(sizeof(struct probe)); | |
486 | probe->name = g_strdup(kv[0]); | |
487 | probe->probe = strtoul(kv[1], 0, 10); | |
488 | /* Apply to last PD. */ | |
489 | pd->probes = g_slist_append(pd->probes, probe); | |
490 | } else { | |
491 | option = malloc(sizeof(struct option)); | |
492 | option->key = g_strdup(kv[0]); | |
493 | option->value = g_strdup(kv[1]); | |
494 | /* Apply to last PD. */ | |
495 | pd->options = g_slist_append(pd->options, option); | |
496 | } | |
497 | break; | |
498 | case 'i': | |
499 | opt_infile = optarg; | |
500 | break; | |
501 | case 'O': | |
502 | opstr = g_strsplit(optarg, ":", 0); | |
503 | if (!opstr[0] || !opstr[1]) { | |
504 | /* Need at least abc:def. */ | |
505 | ERR("Syntax error at '%s'", optarg); | |
506 | g_strfreev(opstr); | |
507 | usage(NULL); | |
508 | } | |
509 | op->pd = g_strdup(opstr[0]); | |
510 | if (!strcmp(opstr[1], "annotation")) | |
511 | op->type = SRD_OUTPUT_ANN; | |
512 | else if (!strcmp(opstr[1], "binary")) | |
513 | op->type = SRD_OUTPUT_BINARY; | |
514 | else if (!strcmp(opstr[1], "python")) | |
515 | op->type = SRD_OUTPUT_PYTHON; | |
516 | else { | |
517 | ERR("Unknown output type '%s'", opstr[1]); | |
518 | g_strfreev(opstr); | |
519 | usage(NULL); | |
520 | } | |
521 | if (opstr[2]) | |
522 | op->class = g_strdup(opstr[2]); | |
523 | g_strfreev(opstr); | |
524 | break; | |
525 | case 'f': | |
526 | op->outfile = g_strdup(optarg); | |
527 | op->outfd = -1; | |
528 | break; | |
529 | case 'S': | |
530 | statistics = TRUE; | |
531 | break; | |
532 | default: | |
533 | usage(NULL); | |
534 | } | |
535 | } | |
536 | if (argc > optind) | |
537 | usage(NULL); | |
538 | if (g_slist_length(pdlist) == 0) | |
539 | usage(NULL); | |
540 | if (!opt_infile) | |
541 | usage(NULL); | |
542 | if (!op->pd || op->type == -1) | |
543 | usage(NULL); | |
544 | ||
caa4b2cc | 545 | sr_log_callback_set(sr_log, NULL); |
fbd226c3 BV |
546 | if (sr_init(&ctx) != SR_OK) |
547 | return 1; | |
548 | ||
549 | srd_log_callback_set(srd_log, NULL); | |
b7482381 | 550 | if (srd_init(DECODERS_DIR) != SRD_OK) |
fbd226c3 BV |
551 | return 1; |
552 | ||
932606db BV |
553 | ret = 0; |
554 | if (!run_testcase(opt_infile, pdlist, op)) | |
555 | ret = 1; | |
fbd226c3 BV |
556 | |
557 | srd_exit(); | |
558 | sr_exit(ctx); | |
559 | ||
932606db | 560 | return ret; |
fbd226c3 BV |
561 | } |
562 | ||
563 |