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