]> sigrok.org Git - sigrok-test.git/blame - decoder/runtc.c
runtc: fixup coding style (conditionals, braces)
[sigrok-test.git] / decoder / runtc.c
CommitLineData
dd37a782
UH
1/*
2 * This file is part of the sigrok-test 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 <Python.h>
21#include <libsigrokdecode/libsigrokdecode.h>
22#include <libsigrok/libsigrok.h>
23#include <stdlib.h>
24#include <stdio.h>
25#include <stdarg.h>
26#include <unistd.h>
27#include <errno.h>
28#include <sys/types.h>
29#include <sys/stat.h>
30#include <fcntl.h>
31#include <time.h>
32#include <sys/time.h>
33#include <sys/resource.h>
34#include <dirent.h>
35#include <glib.h>
36#ifdef __LINUX__
37#include <sched.h>
38#endif
39
6463feb2
UH
40static int debug = FALSE;
41static int statistics = FALSE;
42static char *coverage_report;
43static struct sr_context *ctx;
dd37a782
UH
44
45struct channel {
46 char *name;
47 int channel;
48};
49
50struct option {
51 char *key;
52 GVariant *value;
53};
54
55struct pd {
d116194c 56 const char *name;
dd37a782
UH
57 GSList *channels;
58 GSList *options;
59};
60
61struct output {
d116194c 62 const char *pd;
dd37a782 63 int type;
d116194c 64 const char *class;
dd37a782 65 int class_idx;
d116194c 66 const char *outfile;
dd37a782
UH
67 int outfd;
68};
69
70struct cvg {
71 int num_lines;
72 int num_missed;
73 float coverage;
74 GSList *missed_lines;
75};
76
d116194c
UH
77static struct cvg *get_mod_cov(PyObject *py_cov, const char *module_name);
78static void cvg_add(struct cvg *dst, const struct cvg *src);
6463feb2 79static struct cvg *cvg_new(void);
d116194c 80static gboolean find_missed_line(struct cvg *cvg, const char *linespec);
dd37a782 81
d116194c 82static void logmsg(const char *prefix, FILE *out, const char *format, va_list args)
dd37a782
UH
83{
84 if (prefix)
85 fprintf(out, "%s", prefix);
86 vfprintf(out, format, args);
87 fprintf(out, "\n");
88}
89
90static void DBG(const char *format, ...)
91{
92 va_list args;
93
94 if (!debug)
95 return;
96 va_start(args, format);
97 logmsg("DBG: runtc: ", stdout, format, args);
98 va_end(args);
99}
100
101static void ERR(const char *format, ...)
102{
103 va_list args;
104
105 va_start(args, format);
106 logmsg("Error: ", stderr, format, args);
107 va_end(args);
108}
109
110static int sr_log(void *cb_data, int loglevel, const char *format, va_list args)
111{
112 (void)cb_data;
113
114 if (loglevel == SR_LOG_ERR || loglevel == SR_LOG_WARN)
115 logmsg("Error: sr: ", stderr, format, args);
116 else if (debug)
117 logmsg("DBG: sr: ", stdout, format, args);
118
119 return SRD_OK;
120}
121
122static int srd_log(void *cb_data, int loglevel, const char *format, va_list args)
123{
124 (void)cb_data;
125
126 if (loglevel == SRD_LOG_ERR || loglevel == SRD_LOG_WARN)
127 logmsg("Error: srd: ", stderr, format, args);
128 else if (debug)
129 logmsg("DBG: srd: ", stdout, format, args);
130
131 return SRD_OK;
132}
133
d116194c 134static void usage(const char *msg)
dd37a782
UH
135{
136 if (msg)
137 fprintf(stderr, "%s\n", msg);
138
987819ad
GS
139 printf("Usage: runtc [-dPpoiOfcS]\n");
140 printf(" -d (enables debug output)\n");
141 printf(" -P <protocol decoder>\n");
142 printf(" -p <channelname=channelnum> (optional)\n");
143 printf(" -o <channeloption=value> (optional)\n");
dd37a782
UH
144 printf(" -i <input file>\n");
145 printf(" -O <output-pd:output-type[:output-class]>\n");
146 printf(" -f <output file> (optional)\n");
147 printf(" -c <coverage report> (optional)\n");
987819ad 148 printf(" -S (enables statistics)\n");
dd37a782
UH
149 exit(msg ? 1 : 0);
150
151}
152
95418690
UH
153/*
154 * This is a neutered version of libsigrokdecode's py_str_as_str(). It
dd37a782 155 * does no error checking, but then the only strings it processes are
95418690
UH
156 * generated by Python's repr(), so are known good.
157 */
dd37a782
UH
158static char *py_str_as_str(const PyObject *py_str)
159{
160 PyObject *py_encstr;
161 char *str, *outstr;
162
163 py_encstr = PyUnicode_AsEncodedString((PyObject *)py_str, "utf-8", NULL);
164 str = PyBytes_AS_STRING(py_encstr);
165 outstr = g_strdup(str);
166 Py_DecRef(py_encstr);
167
168 return outstr;
169}
170
171static void srd_cb_py(struct srd_proto_data *pdata, void *cb_data)
172{
173 struct output *op;
174 PyObject *pydata, *pyrepr;
175 GString *out;
176 char *s;
177
914b125b 178 DBG("Python output from %s", pdata->pdo->di->decoder->id);
dd37a782
UH
179 op = cb_data;
180 pydata = pdata->data;
181 DBG("ptr %p", pydata);
182
914b125b 183 if (strcmp(pdata->pdo->di->decoder->id, op->pd))
dd37a782
UH
184 /* This is not the PD selected for output. */
185 return;
186
187 if (!(pyrepr = PyObject_Repr(pydata))) {
188 ERR("Invalid Python object.");
189 return;
190 }
191 s = py_str_as_str(pyrepr);
192 Py_DecRef(pyrepr);
193
194 /* Output format for testing is '<ss>-<es> <inst-id>: <repr>\n'. */
195 out = g_string_sized_new(128);
196 g_string_printf(out, "%" PRIu64 "-%" PRIu64 " %s: %s\n",
197 pdata->start_sample, pdata->end_sample,
914b125b 198 pdata->pdo->di->decoder->id, s);
dd37a782
UH
199 g_free(s);
200 if (write(op->outfd, out->str, out->len) == -1)
201 ERR("SRD_OUTPUT_PYTHON callback write failure!");
202 DBG("wrote '%s'", out->str);
203 g_string_free(out, TRUE);
204
205}
206
207static void srd_cb_bin(struct srd_proto_data *pdata, void *cb_data)
208{
209 struct srd_proto_data_binary *pdb;
210 struct output *op;
211 GString *out;
212 unsigned int i;
213
914b125b 214 DBG("Binary output from %s", pdata->pdo->di->decoder->id);
dd37a782
UH
215 op = cb_data;
216 pdb = pdata->data;
217
914b125b 218 if (strcmp(pdata->pdo->di->decoder->id, op->pd))
dd37a782
UH
219 /* This is not the PD selected for output. */
220 return;
221
222 if (op->class_idx != -1 && op->class_idx != pdb->bin_class)
223 /*
224 * This output takes a specific binary class,
225 * but not the one that just came in.
226 */
227 return;
228
229 out = g_string_sized_new(128);
230 g_string_printf(out, "%" PRIu64 "-%" PRIu64 " %s:",
231 pdata->start_sample, pdata->end_sample,
914b125b 232 pdata->pdo->di->decoder->id);
dd37a782
UH
233 for (i = 0; i < pdb->size; i++) {
234 g_string_append_printf(out, " %.2x", pdb->data[i]);
235 }
236 g_string_append(out, "\n");
237 if (write(op->outfd, out->str, out->len) == -1)
238 ERR("SRD_OUTPUT_BINARY callback write failure!");
239
240}
241
242static void srd_cb_ann(struct srd_proto_data *pdata, void *cb_data)
243{
244 struct srd_decoder *dec;
245 struct srd_proto_data_annotation *pda;
246 struct output *op;
247 GString *line;
248 int i;
249 char **dec_ann;
250
914b125b 251 DBG("Annotation output from %s", pdata->pdo->di->decoder->id);
dd37a782
UH
252 op = cb_data;
253 pda = pdata->data;
254 dec = pdata->pdo->di->decoder;
914b125b 255 if (strcmp(pdata->pdo->di->decoder->id, op->pd))
dd37a782
UH
256 /* This is not the PD selected for output. */
257 return;
258
ec3de18f 259 if (op->class_idx != -1 && op->class_idx != pda->ann_class)
dd37a782
UH
260 /*
261 * This output takes a specific annotation class,
262 * but not the one that just came in.
263 */
264 return;
265
ec3de18f 266 dec_ann = g_slist_nth_data(dec->annotations, pda->ann_class);
dd37a782
UH
267 line = g_string_sized_new(256);
268 g_string_printf(line, "%" PRIu64 "-%" PRIu64 " %s: %s:",
269 pdata->start_sample, pdata->end_sample,
914b125b 270 pdata->pdo->di->decoder->id, dec_ann[0]);
dd37a782
UH
271 for (i = 0; pda->ann_text[i]; i++)
272 g_string_append_printf(line, " \"%s\"", pda->ann_text[i]);
273 g_string_append(line, "\n");
274 if (write(op->outfd, line->str, line->len) == -1)
275 ERR("SRD_OUTPUT_ANN callback write failure!");
276 g_string_free(line, TRUE);
277
278}
279
280static void sr_cb(const struct sr_dev_inst *sdi,
281 const struct sr_datafeed_packet *packet, void *cb_data)
282{
6688fad4 283 static int samplecnt = 0;
dd37a782
UH
284 const struct sr_datafeed_logic *logic;
285 struct srd_session *sess;
286 GVariant *gvar;
287 uint64_t samplerate;
288 int num_samples;
02192227 289 struct sr_dev_driver *driver;
dd37a782
UH
290
291 sess = cb_data;
292
02192227
UH
293 driver = sr_dev_inst_driver_get(sdi);
294
dd37a782
UH
295 switch (packet->type) {
296 case SR_DF_HEADER:
297 DBG("Received SR_DF_HEADER");
02192227 298 if (sr_config_get(driver, sdi, NULL, SR_CONF_SAMPLERATE,
dd37a782
UH
299 &gvar) != SR_OK) {
300 ERR("Getting samplerate failed");
301 break;
302 }
303 samplerate = g_variant_get_uint64(gvar);
304 g_variant_unref(gvar);
305 if (srd_session_metadata_set(sess, SRD_CONF_SAMPLERATE,
306 g_variant_new_uint64(samplerate)) != SRD_OK) {
307 ERR("Setting samplerate failed");
308 break;
309 }
310 if (srd_session_start(sess) != SRD_OK) {
311 ERR("Session start failed");
312 break;
313 }
314 break;
315 case SR_DF_LOGIC:
316 logic = packet->payload;
317 num_samples = logic->length / logic->unitsize;
47de7a66
UH
318 DBG("Received SR_DF_LOGIC (%"PRIu64" bytes, unitsize = %d).",
319 logic->length, logic->unitsize);
dd37a782 320 srd_session_send(sess, samplecnt, samplecnt + num_samples,
cb5b23ce 321 logic->data, logic->length, logic->unitsize);
6688fad4 322 samplecnt += num_samples;
dd37a782
UH
323 break;
324 case SR_DF_END:
325 DBG("Received SR_DF_END");
326 break;
327 }
328
329}
330
d116194c 331static int run_testcase(const char *infile, GSList *pdlist, struct output *op)
dd37a782
UH
332{
333 struct srd_session *sess;
334 struct srd_decoder *dec;
335 struct srd_decoder_inst *di, *prev_di;
336 srd_pd_output_callback cb;
337 struct pd *pd;
338 struct channel *channel;
339 struct option *option;
340 GVariant *gvar;
341 GHashTable *channels, *opts;
47de7a66 342 GSList *pdl, *l, *devices;
9cb2e89a 343 int idx, i;
dd37a782
UH
344 int max_channel;
345 char **decoder_class;
346 struct sr_session *sr_sess;
9cb2e89a
UH
347 gboolean is_number;
348 const char *s;
dd37a782
UH
349
350 if (op->outfile) {
351 if ((op->outfd = open(op->outfile, O_CREAT|O_WRONLY, 0600)) == -1) {
352 ERR("Unable to open %s for writing: %s", op->outfile,
8a8c92bd 353 g_strerror(errno));
dd37a782
UH
354 return FALSE;
355 }
356 }
357
25a196ad 358 if (sr_session_load(ctx, infile, &sr_sess) != SR_OK)
dd37a782
UH
359 return FALSE;
360
47de7a66 361 sr_session_dev_list(sr_sess, &devices);
47de7a66 362
dd37a782
UH
363 if (srd_session_new(&sess) != SRD_OK)
364 return FALSE;
365 sr_session_datafeed_callback_add(sr_sess, sr_cb, sess);
366 switch (op->type) {
367 case SRD_OUTPUT_ANN:
368 cb = srd_cb_ann;
369 break;
370 case SRD_OUTPUT_BINARY:
371 cb = srd_cb_bin;
372 break;
373 case SRD_OUTPUT_PYTHON:
374 cb = srd_cb_py;
375 break;
376 default:
377 return FALSE;
378 }
379 srd_pd_output_callback_add(sess, op->type, cb, op);
380
381 prev_di = NULL;
382 pd = NULL;
383 for (pdl = pdlist; pdl; pdl = pdl->next) {
384 pd = pdl->data;
385 if (srd_decoder_load(pd->name) != SRD_OK)
386 return FALSE;
387
388 /* Instantiate decoder and pass in options. */
389 opts = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
390 (GDestroyNotify)g_variant_unref);
391 for (l = pd->options; l; l = l->next) {
392 option = l->data;
9cb2e89a
UH
393
394 is_number = TRUE;
395 s = g_variant_get_string(option->value, NULL);
396 for (i = 0; i < (int)strlen(s); i++) {
397 if (!isdigit(s[i]))
398 is_number = FALSE;
399 }
400
401 if (is_number) {
402 /* Integer option value */
403 g_hash_table_insert(opts, option->key,
404 g_variant_new_int64(strtoull(s, NULL, 10)));
405 } else {
406 /* String option value */
407 g_hash_table_insert(opts, option->key, option->value);
408 }
dd37a782
UH
409 }
410 if (!(di = srd_inst_new(sess, pd->name, opts)))
411 return FALSE;
412 g_hash_table_destroy(opts);
413
414 /* Map channels. */
415 if (pd->channels) {
416 channels = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
417 (GDestroyNotify)g_variant_unref);
418 max_channel = 0;
419 for (l = pd->channels; l; l = l->next) {
420 channel = l->data;
421 if (channel->channel > max_channel)
422 max_channel = channel->channel;
423 gvar = g_variant_new_int32(channel->channel);
424 g_variant_ref_sink(gvar);
425 g_hash_table_insert(channels, channel->name, gvar);
426 }
47de7a66 427
cb5b23ce 428 if (srd_inst_channel_set_all(di, channels) != SRD_OK)
dd37a782
UH
429 return FALSE;
430 g_hash_table_destroy(channels);
431 }
432
95418690
UH
433 /*
434 * If this is not the first decoder in the list, stack it
435 * on top of the previous one.
436 */
dd37a782
UH
437 if (prev_di) {
438 if (srd_inst_stack(sess, prev_di, di) != SRD_OK) {
439 ERR("Failed to stack decoder instances.");
440 return FALSE;
441 }
442 }
443 prev_di = di;
444 }
445
446 /* Resolve top decoder's class index, so we can match. */
447 dec = srd_decoder_get_by_id(pd->name);
448 if (op->class) {
449 if (op->type == SRD_OUTPUT_ANN)
450 l = dec->annotations;
451 else if (op->type == SRD_OUTPUT_BINARY)
452 l = dec->binary;
453 else
454 /* Only annotations and binary can have a class. */
455 return FALSE;
456 idx = 0;
457 while (l) {
458 decoder_class = l->data;
459 if (!strcmp(decoder_class[0], op->class)) {
460 op->class_idx = idx;
461 break;
59060ffc
GS
462 }
463 idx++;
dd37a782
UH
464 l = l->next;
465 }
466 if (op->class_idx == -1) {
467 ERR("Output class '%s' not found in decoder %s.",
468 op->class, pd->name);
469 return FALSE;
59060ffc
GS
470 }
471 DBG("Class %s index is %d", op->class, op->class_idx);
dd37a782
UH
472 }
473
474 sr_session_start(sr_sess);
475 sr_session_run(sr_sess);
476 sr_session_stop(sr_sess);
477
478 srd_session_destroy(sess);
479
480 if (op->outfile)
481 close(op->outfd);
482
483 return TRUE;
484}
485
486static PyObject *start_coverage(GSList *pdlist)
487{
488 PyObject *py_mod, *py_pdlist, *py_pd, *py_func, *py_args, *py_kwargs, *py_cov;
489 GSList *l;
490 struct pd *pd;
491
492 DBG("Starting coverage.");
493
494 if (!(py_mod = PyImport_ImportModule("coverage")))
495 return NULL;
496
497 if (!(py_pdlist = PyList_New(0)))
498 return NULL;
499 for (l = pdlist; l; l = l->next) {
500 pd = l->data;
501 py_pd = PyUnicode_FromFormat("*/%s/*.py", pd->name);
502 if (PyList_Append(py_pdlist, py_pd) < 0)
503 return NULL;
504 Py_DecRef(py_pd);
505 }
506 if (!(py_func = PyObject_GetAttrString(py_mod, "coverage")))
507 return NULL;
508 if (!(py_args = PyTuple_New(0)))
509 return NULL;
510 if (!(py_kwargs = Py_BuildValue("{sO}", "include", py_pdlist)))
511 return NULL;
512 if (!(py_cov = PyObject_Call(py_func, py_args, py_kwargs)))
513 return NULL;
514 if (!(PyObject_CallMethod(py_cov, "start", NULL)))
515 return NULL;
516 Py_DecRef(py_pdlist);
517 Py_DecRef(py_args);
518 Py_DecRef(py_kwargs);
519 Py_DecRef(py_func);
520
521 return py_cov;
522}
523
d116194c 524static struct cvg *get_mod_cov(PyObject *py_cov, const char *module_name)
dd37a782
UH
525{
526 PyObject *py_mod, *py_pathlist, *py_path, *py_func, *py_pd;
527 PyObject *py_result, *py_missed, *py_item;
528 DIR *d;
529 struct dirent *de;
530 struct cvg *cvg_mod;
531 int num_lines, num_missed, linenum, i, j;
532 char *path, *linespec;
533
534 if (!(py_mod = PyImport_ImportModule(module_name)))
535 return NULL;
536
537 cvg_mod = NULL;
538 py_pathlist = PyObject_GetAttrString(py_mod, "__path__");
539 for (i = 0; i < PyList_Size(py_pathlist); i++) {
540 py_path = PyList_GetItem(py_pathlist, i);
95418690 541 PyUnicode_FSConverter(PyList_GetItem(py_pathlist, i), &py_path);
dd37a782
UH
542 path = PyBytes_AS_STRING(py_path);
543 if (!(d = opendir(path))) {
544 ERR("Invalid module path '%s'", path);
545 return NULL;
546 }
547 while ((de = readdir(d))) {
548 if (strncmp(de->d_name + strlen(de->d_name) - 3, ".py", 3))
549 continue;
550
551 if (!(py_func = PyObject_GetAttrString(py_cov, "analysis2")))
552 return NULL;
553 if (!(py_pd = PyUnicode_FromFormat("%s/%s", path, de->d_name)))
554 return NULL;
555 if (!(py_result = PyObject_CallFunction(py_func, "O", py_pd)))
556 return NULL;
557 Py_DecRef(py_pd);
558 Py_DecRef(py_func);
559
560 if (!cvg_mod)
561 cvg_mod = cvg_new();
562 if (PyTuple_Size(py_result) != 5) {
563 ERR("Invalid result from coverage of '%s/%s'", path, de->d_name);
564 return NULL;
565 }
566 num_lines = PyList_Size(PyTuple_GetItem(py_result, 1));
567 py_missed = PyTuple_GetItem(py_result, 3);
568 num_missed = PyList_Size(py_missed);
569 cvg_mod->num_lines += num_lines;
570 cvg_mod->num_missed += num_missed;
571 for (j = 0; j < num_missed; j++) {
572 py_item = PyList_GetItem(py_missed, j);
573 linenum = PyLong_AsLong(py_item);
574 linespec = g_strdup_printf("%s/%s:%d", module_name,
575 de->d_name, linenum);
576 cvg_mod->missed_lines = g_slist_append(cvg_mod->missed_lines, linespec);
577 }
578 DBG("Coverage for %s/%s: %d lines, %d missed.",
579 module_name, de->d_name, num_lines, num_missed);
580 Py_DecRef(py_result);
581 }
582 }
583 if (cvg_mod->num_lines)
584 cvg_mod->coverage = 100 - ((float)cvg_mod->num_missed / (float)cvg_mod->num_lines * 100);
585
586 Py_DecRef(py_mod);
587 Py_DecRef(py_path);
588
589 return cvg_mod;
590}
591
6463feb2 592static struct cvg *cvg_new(void)
dd37a782
UH
593{
594 struct cvg *cvg;
595
596 cvg = calloc(1, sizeof(struct cvg));
597
598 return cvg;
599}
600
d116194c 601static gboolean find_missed_line(struct cvg *cvg, const char *linespec)
dd37a782
UH
602{
603 GSList *l;
604
605 for (l = cvg->missed_lines; l; l = l->next)
606 if (!strcmp(l->data, linespec))
607 return TRUE;
608
609 return FALSE;
610}
611
d116194c 612static void cvg_add(struct cvg *dst, const struct cvg *src)
dd37a782
UH
613{
614 GSList *l;
615 char *linespec;
616
617 dst->num_lines += src->num_lines;
618 dst->num_missed += src->num_missed;
619 for (l = src->missed_lines; l; l = l->next) {
620 linespec = l->data;
621 if (!find_missed_line(dst, linespec))
622 dst->missed_lines = g_slist_append(dst->missed_lines, linespec);
623 }
624
625}
626
627static int report_coverage(PyObject *py_cov, GSList *pdlist)
628{
629 PyObject *py_func, *py_mod, *py_args, *py_kwargs, *py_outfile, *py_pct;
630 GSList *l, *ml;
631 struct pd *pd;
632 struct cvg *cvg_mod, *cvg_all;
633 float total_coverage;
634 int lines, missed, cnt;
635
636 DBG("Making coverage report.");
637
638 /* Get coverage for each module in the stack. */
639 lines = missed = 0;
640 cvg_all = cvg_new();
641 for (cnt = 0, l = pdlist; l; l = l->next, cnt++) {
642 pd = l->data;
643 if (!(cvg_mod = get_mod_cov(py_cov, pd->name)))
644 return FALSE;
645 printf("coverage: scope=%s coverage=%.0f%% lines=%d missed=%d "
646 "missed_lines=", pd->name, cvg_mod->coverage,
647 cvg_mod->num_lines, cvg_mod->num_missed);
648 for (ml = cvg_mod->missed_lines; ml; ml = ml->next) {
649 if (ml != cvg_mod->missed_lines)
650 printf(",");
651 printf("%s", (char *)ml->data);
652 }
653 printf("\n");
654 lines += cvg_mod->num_lines;
655 missed += cvg_mod->num_missed;
656 cvg_add(cvg_all, cvg_mod);
657 DBG("Coverage for module %s: %d lines, %d missed", pd->name,
658 cvg_mod->num_lines, cvg_mod->num_missed);
659 }
660 lines /= cnt;
661 missed /= cnt;
662 total_coverage = 100 - ((float)missed / (float)lines * 100);
663
664 /* Machine-readable stats on stdout. */
665 printf("coverage: scope=all coverage=%.0f%% lines=%d missed=%d\n",
666 total_coverage, cvg_all->num_lines, cvg_all->num_missed);
667
668 /* Write text report to file. */
669 /* io.open(coverage_report, "w") */
670 if (!(py_mod = PyImport_ImportModule("io")))
671 return FALSE;
672 if (!(py_func = PyObject_GetAttrString(py_mod, "open")))
673 return FALSE;
674 if (!(py_args = PyTuple_New(0)))
675 return FALSE;
676 if (!(py_kwargs = Py_BuildValue("{ssss}", "file", coverage_report,
677 "mode", "w")))
678 return FALSE;
679 if (!(py_outfile = PyObject_Call(py_func, py_args, py_kwargs)))
680 return FALSE;
681 Py_DecRef(py_kwargs);
682 Py_DecRef(py_func);
683
684 /* py_cov.report(file=py_outfile) */
685 if (!(py_func = PyObject_GetAttrString(py_cov, "report")))
686 return FALSE;
687 if (!(py_kwargs = Py_BuildValue("{sO}", "file", py_outfile)))
688 return FALSE;
689 if (!(py_pct = PyObject_Call(py_func, py_args, py_kwargs)))
690 return FALSE;
691 Py_DecRef(py_pct);
692 Py_DecRef(py_kwargs);
693 Py_DecRef(py_func);
694
695 /* py_outfile.close() */
696 if (!(py_func = PyObject_GetAttrString(py_outfile, "close")))
697 return FALSE;
698 if (!PyObject_Call(py_func, py_args, NULL))
699 return FALSE;
700 Py_DecRef(py_outfile);
701 Py_DecRef(py_func);
702 Py_DecRef(py_args);
703 Py_DecRef(py_mod);
704
705 return TRUE;
706}
707
708int main(int argc, char **argv)
709{
dd37a782
UH
710 PyObject *coverage;
711 GSList *pdlist;
712 struct pd *pd;
713 struct channel *channel;
714 struct option *option;
715 struct output *op;
716 int ret, c;
717 char *opt_infile, **kv, **opstr;
718
719 op = malloc(sizeof(struct output));
720 op->pd = NULL;
721 op->type = -1;
722 op->class = NULL;
723 op->class_idx = -1;
724 op->outfd = 1;
725
726 pdlist = NULL;
727 opt_infile = NULL;
728 pd = NULL;
729 coverage = NULL;
730 while ((c = getopt(argc, argv, "dP:p:o:i:O:f:c:S")) != -1) {
731 switch (c) {
732 case 'd':
733 debug = TRUE;
734 break;
735 case 'P':
736 pd = g_malloc(sizeof(struct pd));
737 pd->name = g_strdup(optarg);
738 pd->channels = pd->options = NULL;
739 pdlist = g_slist_append(pdlist, pd);
740 break;
741 case 'p':
742 case 'o':
743 if (g_slist_length(pdlist) == 0) {
744 /* No previous -P. */
745 ERR("Syntax error at '%s'", optarg);
746 usage(NULL);
747 }
748 kv = g_strsplit(optarg, "=", 0);
749 if (!kv[0] || (!kv[1] || kv[2])) {
750 /* Need x=y. */
751 ERR("Syntax error at '%s'", optarg);
752 g_strfreev(kv);
753 usage(NULL);
754 }
755 if (c == 'p') {
756 channel = malloc(sizeof(struct channel));
757 channel->name = g_strdup(kv[0]);
ecec131b 758 channel->channel = strtoul(kv[1], NULL, 10);
dd37a782
UH
759 /* Apply to last PD. */
760 pd->channels = g_slist_append(pd->channels, channel);
761 } else {
762 option = malloc(sizeof(struct option));
763 option->key = g_strdup(kv[0]);
764 option->value = g_variant_new_string(kv[1]);
ecec131b 765 g_variant_ref_sink(option->value);
dd37a782
UH
766 /* Apply to last PD. */
767 pd->options = g_slist_append(pd->options, option);
768 }
769 break;
770 case 'i':
771 opt_infile = optarg;
772 break;
773 case 'O':
774 opstr = g_strsplit(optarg, ":", 0);
775 if (!opstr[0] || !opstr[1]) {
776 /* Need at least abc:def. */
777 ERR("Syntax error at '%s'", optarg);
778 g_strfreev(opstr);
779 usage(NULL);
780 }
781 op->pd = g_strdup(opstr[0]);
782 if (!strcmp(opstr[1], "annotation"))
783 op->type = SRD_OUTPUT_ANN;
784 else if (!strcmp(opstr[1], "binary"))
785 op->type = SRD_OUTPUT_BINARY;
786 else if (!strcmp(opstr[1], "python"))
787 op->type = SRD_OUTPUT_PYTHON;
788 else if (!strcmp(opstr[1], "exception"))
789 /* Doesn't matter, we just need it to bomb out. */
790 op->type = SRD_OUTPUT_PYTHON;
791 else {
792 ERR("Unknown output type '%s'", opstr[1]);
793 g_strfreev(opstr);
794 usage(NULL);
795 }
796 if (opstr[2])
797 op->class = g_strdup(opstr[2]);
798 g_strfreev(opstr);
799 break;
800 case 'f':
801 op->outfile = g_strdup(optarg);
802 op->outfd = -1;
803 break;
804 case 'c':
805 coverage_report = optarg;
806 break;
807 case 'S':
808 statistics = TRUE;
809 break;
810 default:
811 usage(NULL);
812 }
813 }
814 if (argc > optind)
815 usage(NULL);
816 if (g_slist_length(pdlist) == 0)
817 usage(NULL);
818 if (!opt_infile)
819 usage(NULL);
820 if (!op->pd || op->type == -1)
821 usage(NULL);
822
823 sr_log_callback_set(sr_log, NULL);
824 if (sr_init(&ctx) != SR_OK)
825 return 1;
826
827 srd_log_callback_set(srd_log, NULL);
828 if (srd_init(DECODERS_DIR) != SRD_OK)
829 return 1;
830
831 if (coverage_report) {
832 if (!(coverage = start_coverage(pdlist))) {
833 DBG("Failed to start coverage.");
834 if (PyErr_Occurred()) {
835 PyErr_PrintEx(0);
836 PyErr_Clear();
837 }
838 }
839 }
840
841 ret = 0;
842 if (!run_testcase(opt_infile, pdlist, op))
843 ret = 1;
844
845 if (coverage) {
846 DBG("Stopping coverage.");
847
848 if (!(PyObject_CallMethod(coverage, "stop", NULL)))
849 ERR("Failed to stop coverage.");
850 else if (!(report_coverage(coverage, pdlist)))
851 ERR("Failed to make coverage report.");
852 else
853 DBG("Coverage report in %s", coverage_report);
854
855 if (PyErr_Occurred()) {
856 PyErr_PrintEx(0);
857 PyErr_Clear();
858 }
859 Py_DecRef(coverage);
860 }
861
862 srd_exit();
863 sr_exit(ctx);
864
865 return ret;
866}