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