]> sigrok.org Git - sigrok-test.git/blame - decoder/runtc.c
pwm: Update files, add a missing file.
[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;
285
286 sess = cb_data;
287
288 switch (packet->type) {
289 case SR_DF_HEADER:
290 DBG("Received SR_DF_HEADER");
291 if (sr_config_get(sdi->driver, sdi, NULL, SR_CONF_SAMPLERATE,
292 &gvar) != SR_OK) {
293 ERR("Getting samplerate failed");
294 break;
295 }
296 samplerate = g_variant_get_uint64(gvar);
297 g_variant_unref(gvar);
298 if (srd_session_metadata_set(sess, SRD_CONF_SAMPLERATE,
299 g_variant_new_uint64(samplerate)) != SRD_OK) {
300 ERR("Setting samplerate failed");
301 break;
302 }
303 if (srd_session_start(sess) != SRD_OK) {
304 ERR("Session start failed");
305 break;
306 }
307 break;
308 case SR_DF_LOGIC:
309 logic = packet->payload;
310 num_samples = logic->length / logic->unitsize;
311 DBG("Received SR_DF_LOGIC: %d samples", num_samples);
312 srd_session_send(sess, samplecnt, samplecnt + num_samples,
313 logic->data, logic->length);
314 samplecnt += logic->length / logic->unitsize;
315 break;
316 case SR_DF_END:
317 DBG("Received SR_DF_END");
318 break;
319 }
320
321}
322
323static int run_testcase(char *infile, GSList *pdlist, struct output *op)
324{
325 struct srd_session *sess;
326 struct srd_decoder *dec;
327 struct srd_decoder_inst *di, *prev_di;
328 srd_pd_output_callback cb;
329 struct pd *pd;
330 struct channel *channel;
331 struct option *option;
332 GVariant *gvar;
333 GHashTable *channels, *opts;
334 GSList *pdl, *l;
9cb2e89a 335 int idx, i;
dd37a782
UH
336 int max_channel;
337 char **decoder_class;
338 struct sr_session *sr_sess;
9cb2e89a
UH
339 gboolean is_number;
340 const char *s;
dd37a782
UH
341
342 if (op->outfile) {
343 if ((op->outfd = open(op->outfile, O_CREAT|O_WRONLY, 0600)) == -1) {
344 ERR("Unable to open %s for writing: %s", op->outfile,
345 strerror(errno));
346 return FALSE;
347 }
348 }
349
350 if (sr_session_load(infile, &sr_sess) != SR_OK)
351 return FALSE;
352
353 if (srd_session_new(&sess) != SRD_OK)
354 return FALSE;
355 sr_session_datafeed_callback_add(sr_sess, sr_cb, sess);
356 switch (op->type) {
357 case SRD_OUTPUT_ANN:
358 cb = srd_cb_ann;
359 break;
360 case SRD_OUTPUT_BINARY:
361 cb = srd_cb_bin;
362 break;
363 case SRD_OUTPUT_PYTHON:
364 cb = srd_cb_py;
365 break;
366 default:
367 return FALSE;
368 }
369 srd_pd_output_callback_add(sess, op->type, cb, op);
370
371 prev_di = NULL;
372 pd = NULL;
373 for (pdl = pdlist; pdl; pdl = pdl->next) {
374 pd = pdl->data;
375 if (srd_decoder_load(pd->name) != SRD_OK)
376 return FALSE;
377
378 /* Instantiate decoder and pass in options. */
379 opts = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
380 (GDestroyNotify)g_variant_unref);
381 for (l = pd->options; l; l = l->next) {
382 option = l->data;
9cb2e89a
UH
383
384 is_number = TRUE;
385 s = g_variant_get_string(option->value, NULL);
386 for (i = 0; i < (int)strlen(s); i++) {
387 if (!isdigit(s[i]))
388 is_number = FALSE;
389 }
390
391 if (is_number) {
392 /* Integer option value */
393 g_hash_table_insert(opts, option->key,
394 g_variant_new_int64(strtoull(s, NULL, 10)));
395 } else {
396 /* String option value */
397 g_hash_table_insert(opts, option->key, option->value);
398 }
dd37a782
UH
399 }
400 if (!(di = srd_inst_new(sess, pd->name, opts)))
401 return FALSE;
402 g_hash_table_destroy(opts);
403
404 /* Map channels. */
405 if (pd->channels) {
406 channels = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
407 (GDestroyNotify)g_variant_unref);
408 max_channel = 0;
409 for (l = pd->channels; l; l = l->next) {
410 channel = l->data;
411 if (channel->channel > max_channel)
412 max_channel = channel->channel;
413 gvar = g_variant_new_int32(channel->channel);
414 g_variant_ref_sink(gvar);
415 g_hash_table_insert(channels, channel->name, gvar);
416 }
417 if (srd_inst_channel_set_all(di, channels,
418 (max_channel + 8) / 8) != SRD_OK)
419 return FALSE;
420 g_hash_table_destroy(channels);
421 }
422
423 /* If this is not the first decoder in the list, stack it
424 * on top of the previous one. */
425 if (prev_di) {
426 if (srd_inst_stack(sess, prev_di, di) != SRD_OK) {
427 ERR("Failed to stack decoder instances.");
428 return FALSE;
429 }
430 }
431 prev_di = di;
432 }
433
434 /* Resolve top decoder's class index, so we can match. */
435 dec = srd_decoder_get_by_id(pd->name);
436 if (op->class) {
437 if (op->type == SRD_OUTPUT_ANN)
438 l = dec->annotations;
439 else if (op->type == SRD_OUTPUT_BINARY)
440 l = dec->binary;
441 else
442 /* Only annotations and binary can have a class. */
443 return FALSE;
444 idx = 0;
445 while (l) {
446 decoder_class = l->data;
447 if (!strcmp(decoder_class[0], op->class)) {
448 op->class_idx = idx;
449 break;
450 } else
451 idx++;
452 l = l->next;
453 }
454 if (op->class_idx == -1) {
455 ERR("Output class '%s' not found in decoder %s.",
456 op->class, pd->name);
457 return FALSE;
458 } else
459 DBG("Class %s index is %d", op->class, op->class_idx);
460 }
461
462 sr_session_start(sr_sess);
463 sr_session_run(sr_sess);
464 sr_session_stop(sr_sess);
465
466 srd_session_destroy(sess);
467
468 if (op->outfile)
469 close(op->outfd);
470
471 return TRUE;
472}
473
474static PyObject *start_coverage(GSList *pdlist)
475{
476 PyObject *py_mod, *py_pdlist, *py_pd, *py_func, *py_args, *py_kwargs, *py_cov;
477 GSList *l;
478 struct pd *pd;
479
480 DBG("Starting coverage.");
481
482 if (!(py_mod = PyImport_ImportModule("coverage")))
483 return NULL;
484
485 if (!(py_pdlist = PyList_New(0)))
486 return NULL;
487 for (l = pdlist; l; l = l->next) {
488 pd = l->data;
489 py_pd = PyUnicode_FromFormat("*/%s/*.py", pd->name);
490 if (PyList_Append(py_pdlist, py_pd) < 0)
491 return NULL;
492 Py_DecRef(py_pd);
493 }
494 if (!(py_func = PyObject_GetAttrString(py_mod, "coverage")))
495 return NULL;
496 if (!(py_args = PyTuple_New(0)))
497 return NULL;
498 if (!(py_kwargs = Py_BuildValue("{sO}", "include", py_pdlist)))
499 return NULL;
500 if (!(py_cov = PyObject_Call(py_func, py_args, py_kwargs)))
501 return NULL;
502 if (!(PyObject_CallMethod(py_cov, "start", NULL)))
503 return NULL;
504 Py_DecRef(py_pdlist);
505 Py_DecRef(py_args);
506 Py_DecRef(py_kwargs);
507 Py_DecRef(py_func);
508
509 return py_cov;
510}
511
512struct cvg *get_mod_cov(PyObject *py_cov, char *module_name)
513{
514 PyObject *py_mod, *py_pathlist, *py_path, *py_func, *py_pd;
515 PyObject *py_result, *py_missed, *py_item;
516 DIR *d;
517 struct dirent *de;
518 struct cvg *cvg_mod;
519 int num_lines, num_missed, linenum, i, j;
520 char *path, *linespec;
521
522 if (!(py_mod = PyImport_ImportModule(module_name)))
523 return NULL;
524
525 cvg_mod = NULL;
526 py_pathlist = PyObject_GetAttrString(py_mod, "__path__");
527 for (i = 0; i < PyList_Size(py_pathlist); i++) {
528 py_path = PyList_GetItem(py_pathlist, i);
529 PyUnicode_FSConverter(PyList_GetItem(py_pathlist, i), &py_path);
530 path = PyBytes_AS_STRING(py_path);
531 if (!(d = opendir(path))) {
532 ERR("Invalid module path '%s'", path);
533 return NULL;
534 }
535 while ((de = readdir(d))) {
536 if (strncmp(de->d_name + strlen(de->d_name) - 3, ".py", 3))
537 continue;
538
539 if (!(py_func = PyObject_GetAttrString(py_cov, "analysis2")))
540 return NULL;
541 if (!(py_pd = PyUnicode_FromFormat("%s/%s", path, de->d_name)))
542 return NULL;
543 if (!(py_result = PyObject_CallFunction(py_func, "O", py_pd)))
544 return NULL;
545 Py_DecRef(py_pd);
546 Py_DecRef(py_func);
547
548 if (!cvg_mod)
549 cvg_mod = cvg_new();
550 if (PyTuple_Size(py_result) != 5) {
551 ERR("Invalid result from coverage of '%s/%s'", path, de->d_name);
552 return NULL;
553 }
554 num_lines = PyList_Size(PyTuple_GetItem(py_result, 1));
555 py_missed = PyTuple_GetItem(py_result, 3);
556 num_missed = PyList_Size(py_missed);
557 cvg_mod->num_lines += num_lines;
558 cvg_mod->num_missed += num_missed;
559 for (j = 0; j < num_missed; j++) {
560 py_item = PyList_GetItem(py_missed, j);
561 linenum = PyLong_AsLong(py_item);
562 linespec = g_strdup_printf("%s/%s:%d", module_name,
563 de->d_name, linenum);
564 cvg_mod->missed_lines = g_slist_append(cvg_mod->missed_lines, linespec);
565 }
566 DBG("Coverage for %s/%s: %d lines, %d missed.",
567 module_name, de->d_name, num_lines, num_missed);
568 Py_DecRef(py_result);
569 }
570 }
571 if (cvg_mod->num_lines)
572 cvg_mod->coverage = 100 - ((float)cvg_mod->num_missed / (float)cvg_mod->num_lines * 100);
573
574 Py_DecRef(py_mod);
575 Py_DecRef(py_path);
576
577 return cvg_mod;
578}
579
580struct cvg *cvg_new(void)
581{
582 struct cvg *cvg;
583
584 cvg = calloc(1, sizeof(struct cvg));
585
586 return cvg;
587}
588
589gboolean find_missed_line(struct cvg *cvg, char *linespec)
590{
591 GSList *l;
592
593 for (l = cvg->missed_lines; l; l = l->next)
594 if (!strcmp(l->data, linespec))
595 return TRUE;
596
597 return FALSE;
598}
599
600void cvg_add(struct cvg *dst, struct cvg *src)
601{
602 GSList *l;
603 char *linespec;
604
605 dst->num_lines += src->num_lines;
606 dst->num_missed += src->num_missed;
607 for (l = src->missed_lines; l; l = l->next) {
608 linespec = l->data;
609 if (!find_missed_line(dst, linespec))
610 dst->missed_lines = g_slist_append(dst->missed_lines, linespec);
611 }
612
613}
614
615static int report_coverage(PyObject *py_cov, GSList *pdlist)
616{
617 PyObject *py_func, *py_mod, *py_args, *py_kwargs, *py_outfile, *py_pct;
618 GSList *l, *ml;
619 struct pd *pd;
620 struct cvg *cvg_mod, *cvg_all;
621 float total_coverage;
622 int lines, missed, cnt;
623
624 DBG("Making coverage report.");
625
626 /* Get coverage for each module in the stack. */
627 lines = missed = 0;
628 cvg_all = cvg_new();
629 for (cnt = 0, l = pdlist; l; l = l->next, cnt++) {
630 pd = l->data;
631 if (!(cvg_mod = get_mod_cov(py_cov, pd->name)))
632 return FALSE;
633 printf("coverage: scope=%s coverage=%.0f%% lines=%d missed=%d "
634 "missed_lines=", pd->name, cvg_mod->coverage,
635 cvg_mod->num_lines, cvg_mod->num_missed);
636 for (ml = cvg_mod->missed_lines; ml; ml = ml->next) {
637 if (ml != cvg_mod->missed_lines)
638 printf(",");
639 printf("%s", (char *)ml->data);
640 }
641 printf("\n");
642 lines += cvg_mod->num_lines;
643 missed += cvg_mod->num_missed;
644 cvg_add(cvg_all, cvg_mod);
645 DBG("Coverage for module %s: %d lines, %d missed", pd->name,
646 cvg_mod->num_lines, cvg_mod->num_missed);
647 }
648 lines /= cnt;
649 missed /= cnt;
650 total_coverage = 100 - ((float)missed / (float)lines * 100);
651
652 /* Machine-readable stats on stdout. */
653 printf("coverage: scope=all coverage=%.0f%% lines=%d missed=%d\n",
654 total_coverage, cvg_all->num_lines, cvg_all->num_missed);
655
656 /* Write text report to file. */
657 /* io.open(coverage_report, "w") */
658 if (!(py_mod = PyImport_ImportModule("io")))
659 return FALSE;
660 if (!(py_func = PyObject_GetAttrString(py_mod, "open")))
661 return FALSE;
662 if (!(py_args = PyTuple_New(0)))
663 return FALSE;
664 if (!(py_kwargs = Py_BuildValue("{ssss}", "file", coverage_report,
665 "mode", "w")))
666 return FALSE;
667 if (!(py_outfile = PyObject_Call(py_func, py_args, py_kwargs)))
668 return FALSE;
669 Py_DecRef(py_kwargs);
670 Py_DecRef(py_func);
671
672 /* py_cov.report(file=py_outfile) */
673 if (!(py_func = PyObject_GetAttrString(py_cov, "report")))
674 return FALSE;
675 if (!(py_kwargs = Py_BuildValue("{sO}", "file", py_outfile)))
676 return FALSE;
677 if (!(py_pct = PyObject_Call(py_func, py_args, py_kwargs)))
678 return FALSE;
679 Py_DecRef(py_pct);
680 Py_DecRef(py_kwargs);
681 Py_DecRef(py_func);
682
683 /* py_outfile.close() */
684 if (!(py_func = PyObject_GetAttrString(py_outfile, "close")))
685 return FALSE;
686 if (!PyObject_Call(py_func, py_args, NULL))
687 return FALSE;
688 Py_DecRef(py_outfile);
689 Py_DecRef(py_func);
690 Py_DecRef(py_args);
691 Py_DecRef(py_mod);
692
693 return TRUE;
694}
695
696int main(int argc, char **argv)
697{
698 struct sr_context *ctx;
699 PyObject *coverage;
700 GSList *pdlist;
701 struct pd *pd;
702 struct channel *channel;
703 struct option *option;
704 struct output *op;
705 int ret, c;
706 char *opt_infile, **kv, **opstr;
707
708 op = malloc(sizeof(struct output));
709 op->pd = NULL;
710 op->type = -1;
711 op->class = NULL;
712 op->class_idx = -1;
713 op->outfd = 1;
714
715 pdlist = NULL;
716 opt_infile = NULL;
717 pd = NULL;
718 coverage = NULL;
719 while ((c = getopt(argc, argv, "dP:p:o:i:O:f:c:S")) != -1) {
720 switch (c) {
721 case 'd':
722 debug = TRUE;
723 break;
724 case 'P':
725 pd = g_malloc(sizeof(struct pd));
726 pd->name = g_strdup(optarg);
727 pd->channels = pd->options = NULL;
728 pdlist = g_slist_append(pdlist, pd);
729 break;
730 case 'p':
731 case 'o':
732 if (g_slist_length(pdlist) == 0) {
733 /* No previous -P. */
734 ERR("Syntax error at '%s'", optarg);
735 usage(NULL);
736 }
737 kv = g_strsplit(optarg, "=", 0);
738 if (!kv[0] || (!kv[1] || kv[2])) {
739 /* Need x=y. */
740 ERR("Syntax error at '%s'", optarg);
741 g_strfreev(kv);
742 usage(NULL);
743 }
744 if (c == 'p') {
745 channel = malloc(sizeof(struct channel));
746 channel->name = g_strdup(kv[0]);
747 channel->channel = strtoul(kv[1], 0, 10);
748 /* Apply to last PD. */
749 pd->channels = g_slist_append(pd->channels, channel);
750 } else {
751 option = malloc(sizeof(struct option));
752 option->key = g_strdup(kv[0]);
753 option->value = g_variant_new_string(kv[1]);
754 g_variant_ref_sink(option->value);
755 /* Apply to last PD. */
756 pd->options = g_slist_append(pd->options, option);
757 }
758 break;
759 case 'i':
760 opt_infile = optarg;
761 break;
762 case 'O':
763 opstr = g_strsplit(optarg, ":", 0);
764 if (!opstr[0] || !opstr[1]) {
765 /* Need at least abc:def. */
766 ERR("Syntax error at '%s'", optarg);
767 g_strfreev(opstr);
768 usage(NULL);
769 }
770 op->pd = g_strdup(opstr[0]);
771 if (!strcmp(opstr[1], "annotation"))
772 op->type = SRD_OUTPUT_ANN;
773 else if (!strcmp(opstr[1], "binary"))
774 op->type = SRD_OUTPUT_BINARY;
775 else if (!strcmp(opstr[1], "python"))
776 op->type = SRD_OUTPUT_PYTHON;
777 else if (!strcmp(opstr[1], "exception"))
778 /* Doesn't matter, we just need it to bomb out. */
779 op->type = SRD_OUTPUT_PYTHON;
780 else {
781 ERR("Unknown output type '%s'", opstr[1]);
782 g_strfreev(opstr);
783 usage(NULL);
784 }
785 if (opstr[2])
786 op->class = g_strdup(opstr[2]);
787 g_strfreev(opstr);
788 break;
789 case 'f':
790 op->outfile = g_strdup(optarg);
791 op->outfd = -1;
792 break;
793 case 'c':
794 coverage_report = optarg;
795 break;
796 case 'S':
797 statistics = TRUE;
798 break;
799 default:
800 usage(NULL);
801 }
802 }
803 if (argc > optind)
804 usage(NULL);
805 if (g_slist_length(pdlist) == 0)
806 usage(NULL);
807 if (!opt_infile)
808 usage(NULL);
809 if (!op->pd || op->type == -1)
810 usage(NULL);
811
812 sr_log_callback_set(sr_log, NULL);
813 if (sr_init(&ctx) != SR_OK)
814 return 1;
815
816 srd_log_callback_set(srd_log, NULL);
817 if (srd_init(DECODERS_DIR) != SRD_OK)
818 return 1;
819
820 if (coverage_report) {
821 if (!(coverage = start_coverage(pdlist))) {
822 DBG("Failed to start coverage.");
823 if (PyErr_Occurred()) {
824 PyErr_PrintEx(0);
825 PyErr_Clear();
826 }
827 }
828 }
829
830 ret = 0;
831 if (!run_testcase(opt_infile, pdlist, op))
832 ret = 1;
833
834 if (coverage) {
835 DBG("Stopping coverage.");
836
837 if (!(PyObject_CallMethod(coverage, "stop", NULL)))
838 ERR("Failed to stop coverage.");
839 else if (!(report_coverage(coverage, pdlist)))
840 ERR("Failed to make coverage report.");
841 else
842 DBG("Coverage report in %s", coverage_report);
843
844 if (PyErr_Occurred()) {
845 PyErr_PrintEx(0);
846 PyErr_Clear();
847 }
848 Py_DecRef(coverage);
849 }
850
851 srd_exit();
852 sr_exit(ctx);
853
854 return ret;
855}