]> sigrok.org Git - libsigrokdecode.git/blob - tests/runtc.c
902ca8e1dd4aff2b1295cbe822dc620c00d9f632
[libsigrokdecode.git] / tests / runtc.c
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
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
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);
115         else if (debug)
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
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
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)
155 {
156         struct output *op;
157         PyObject *pydata, *pyrepr;
158         GString *out;
159         char *s;
160
161         DBG("Python output from %s", pdata->pdo->di->inst_id);
162         op = cb_data;
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.");
172                 return;
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;
194
195         DBG("Binary output from %s", pdata->pdo->di->inst_id);
196         op = cb_data;
197         pdb = pdata->data;
198
199         if (strcmp(pdata->pdo->di->inst_id, op->pd))
200                 /* This is not the PD selected for output. */
201                 return;
202
203         if (op->class_idx != -1 && op->class_idx != pdb->bin_class)
204                 /*
205                  * This output takes a specific binary class,
206                  * but not the one that just came in.
207                  */
208                 return;
209
210         if (write(op->outfd, pdb->data, pdb->size) == -1)
211                 ERR("SRD_OUTPUT_BINARY callback write failure!");
212
213 }
214
215 static void srd_cb_ann(struct srd_proto_data *pdata, void *cb_data)
216 {
217         struct srd_decoder *dec;
218         struct srd_proto_data_annotation *pda;
219         struct output *op;
220         GString *line;
221         int i;
222         char **dec_ann;
223
224         DBG("Annotation output from %s", pdata->pdo->di->inst_id);
225         op = cb_data;
226         pda = pdata->data;
227         dec = pdata->pdo->di->decoder;
228         if (strcmp(pdata->pdo->di->inst_id, op->pd))
229                 /* This is not the PD selected for output. */
230                 return;
231
232         if (op->class_idx != -1 && op->class_idx != pda->ann_format)
233                 /*
234                  * This output takes a specific annotation class,
235                  * but not the one that just came in.
236                  */
237                 return;
238
239         dec_ann = g_slist_nth_data(dec->annotations, pda->ann_format);
240         line = g_string_sized_new(256);
241         g_string_printf(line, "%"PRIu64"-%"PRIu64" %s: %s:",
242                         pdata->start_sample, pdata->end_sample,
243                         pdata->pdo->di->inst_id, dec_ann[0]);
244         for (i = 0; pda->ann_text[i]; i++)
245                 g_string_append_printf(line, " \"%s\"", pda->ann_text[i]);
246         g_string_append(line, "\n");
247         if (write(op->outfd, line->str, line->len) == -1)
248                 ERR("SRD_OUTPUT_ANN callback write failure!");
249         g_string_free(line, TRUE);
250
251 }
252
253 static void sr_cb(const struct sr_dev_inst *sdi,
254                 const struct sr_datafeed_packet *packet, void *cb_data)
255 {
256         const struct sr_datafeed_logic *logic;
257         struct srd_session *sess;
258         GVariant *gvar;
259         uint64_t samplerate;
260         int num_samples;
261         static int samplecnt = 0;
262
263         sess = cb_data;
264
265         switch (packet->type) {
266         case SR_DF_HEADER:
267                 DBG("Received SR_DF_HEADER");
268                 if (sr_config_get(sdi->driver, sdi, NULL, SR_CONF_SAMPLERATE,
269                                 &gvar) != SR_OK) {
270                         ERR("Getting samplerate failed");
271                         break;
272                 }
273                 samplerate = g_variant_get_uint64(gvar);
274                 g_variant_unref(gvar);
275                 if (srd_session_metadata_set(sess, SRD_CONF_SAMPLERATE,
276                                 g_variant_new_uint64(samplerate)) != SRD_OK) {
277                         ERR("Setting samplerate failed");
278                         break;
279                 }
280                 if (srd_session_start(sess) != SRD_OK) {
281                         ERR("Session start failed");
282                         break;
283                 }
284                 break;
285         case SR_DF_LOGIC:
286                 logic = packet->payload;
287                 num_samples = logic->length / logic->unitsize;
288                 DBG("Received SR_DF_LOGIC: %d samples", num_samples);
289                 srd_session_send(sess, samplecnt, samplecnt + num_samples,
290                                 logic->data, logic->length);
291                 samplecnt += logic->length / logic->unitsize;
292                 break;
293         case SR_DF_END:
294                 DBG("Received SR_DF_END");
295                 break;
296         }
297
298 }
299
300 int get_stats(int stats[2])
301 {
302         FILE *f;
303         size_t len;
304         int tmp;
305         char *buf;
306
307         stats[0] = stats[1] = -1;
308         if (!(f = fopen("/proc/self/status", "r")))
309                 return FALSE;
310         len = 128;
311         buf = malloc(len);
312         while (getline(&buf, &len, f) != -1) {
313                 if (strcasestr(buf, "vmpeak:")) {
314                         stats[0] = strtoul(buf + 10, NULL, 10);
315                 } else if (strcasestr(buf, "vmsize:")) {
316                         tmp = strtoul(buf + 10, NULL, 10);
317                         if (tmp > stats[0])
318                                 stats[0] = tmp;
319                 } else if (strcasestr(buf, "vmhwm:")) {
320                         stats[1] = strtoul(buf + 6, NULL, 10);
321                 } else if (strcasestr(buf, "vmrss:")) {
322                         tmp = strtoul(buf + 10, NULL, 10);
323                         if (tmp > stats[0])
324                                 stats[0] = tmp;
325                 }
326         }
327         free(buf);
328         fclose(f);
329
330         return TRUE;
331 }
332
333 static int run_testcase(char *infile, GSList *pdlist, struct output *op)
334 {
335         struct srd_session *sess;
336         struct srd_decoder *dec;
337         struct srd_decoder_inst *di, *prev_di;
338         srd_pd_output_callback_t cb;
339         struct pd *pd;
340         struct probe *probe;
341         struct option *option;
342         GVariant *gvar;
343         GHashTable *probes, *opts;
344         GSList *pdl, *l;
345         int idx;
346         char **decoder_class;
347
348         if (op->outfile) {
349                 if ((op->outfd = open(op->outfile, O_CREAT|O_WRONLY, 0600)) == -1) {
350                         ERR("Unable to open %s for writing: %s", op->outfile,
351                                         strerror(errno));
352                         return FALSE;
353                 }
354         }
355
356         if (sr_session_load(infile) != SR_OK)
357                 return FALSE;
358
359         if (srd_session_new(&sess) != SRD_OK)
360                 return FALSE;
361         sr_session_datafeed_callback_add(sr_cb, sess);
362         switch (op->type) {
363         case SRD_OUTPUT_ANN:
364                 cb = srd_cb_ann;
365                 break;
366         case SRD_OUTPUT_BINARY:
367                 cb = srd_cb_bin;
368                 break;
369         case SRD_OUTPUT_PYTHON:
370                 cb = srd_cb_py;
371                 break;
372         default:
373                 return FALSE;
374         }
375         srd_pd_output_callback_add(sess, op->type, cb, op);
376
377         prev_di = NULL;
378         pd = NULL;
379         for (pdl = pdlist; pdl; pdl = pdl->next) {
380                 pd = pdl->data;
381                 if (srd_decoder_load(pd->name) != SRD_OK)
382                         return FALSE;
383
384                 /* Instantiate decoder and pass in options. */
385                 opts = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
386                                 (GDestroyNotify)g_variant_unref);
387                 for (l = pd->options; l; l = l->next) {
388                         option = l->data;
389                         g_hash_table_insert(opts, option->key, option->value);
390                 }
391                 if (!(di = srd_inst_new(sess, pd->name, opts)))
392                         return FALSE;
393                 g_hash_table_destroy(opts);
394
395                 /* Map probes. */
396                 if (pd->probes) {
397                         probes = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
398                                         (GDestroyNotify)g_variant_unref);
399                         for (l = pd->probes; l; l = l->next) {
400                                 probe = l->data;
401                                 gvar = g_variant_new_int32(probe->probe);
402                                 g_variant_ref_sink(gvar);
403                                 g_hash_table_insert(probes, probe->name, gvar);
404                         }
405                         if (srd_inst_probe_set_all(di, probes) != SRD_OK)
406                                 return FALSE;
407                         g_hash_table_destroy(probes);
408                 }
409
410                 /* If this is not the first decoder in the list, stack it
411                  * on top of the previous one. */
412                 if (prev_di) {
413                         if (srd_inst_stack(sess, prev_di, di) != SRD_OK) {
414                                 ERR("Failed to stack decoder instances.");
415                                 return FALSE;
416                         }
417                 }
418                 prev_di = di;
419         }
420
421         /* Resolve top decoder's class index, so we can match. */
422         dec = srd_decoder_get_by_id(pd->name);
423         if (op->class) {
424                 if (op->type == SRD_OUTPUT_ANN)
425                         l = dec->annotations;
426                 else if (op->type == SRD_OUTPUT_BINARY)
427                         l = dec->binary;
428                 else
429                         /* Only annotations and binary can have a class. */
430                         return FALSE;
431                 idx = 0;
432                 while(l) {
433                         decoder_class = l->data;
434                         if (!strcmp(decoder_class[0], op->class)) {
435                                 op->class_idx = idx;
436                                 break;
437                         } else
438                                 idx++;
439                         l = l->next;
440                 }
441                 if (op->class_idx == -1) {
442                         ERR("Output class '%s' not found in decoder %s.",
443                                         op->class, pd->name);
444                         return FALSE;
445                 } else
446                         DBG("Class %s index is %d", op->class, op->class_idx);
447         }
448
449         sr_session_start();
450         sr_session_run();
451         sr_session_stop();
452
453         srd_session_destroy(sess);
454
455         if (op->outfile)
456                 close(op->outfd);
457
458         return TRUE;
459 }
460
461 int main(int argc, char **argv)
462 {
463         struct sr_context *ctx;
464         GSList *pdlist;
465         struct pd *pd;
466         struct probe *probe;
467         struct option *option;
468         struct output *op;
469         int ret;
470         char c, *opt_infile, **kv, **opstr;
471
472         op = malloc(sizeof(struct output));
473         op->pd = NULL;
474         op->type = -1;
475         op->class = NULL;
476         op->class_idx = -1;
477         op->outfd = 1;
478
479         pdlist = NULL;
480         opt_infile = NULL;
481         pd = NULL;
482         while((c = getopt(argc, argv, "dP:p:o:i:O:f:S")) != -1) {
483                 switch(c) {
484                 case 'd':
485                         debug = TRUE;
486                         break;
487                 case 'P':
488                         pd = g_malloc(sizeof(struct pd));
489                         pd->name = g_strdup(optarg);
490                         pd->probes = pd->options = NULL;
491                         pdlist = g_slist_append(pdlist, pd);
492                         break;
493                 case 'p':
494                 case 'o':
495                         if (g_slist_length(pdlist) == 0) {
496                                 /* No previous -P. */
497                                 ERR("Syntax error at '%s'", optarg);
498                                 usage(NULL);
499                         }
500                         kv = g_strsplit(optarg, "=", 0);
501                         if (!kv[0] || (!kv[1] || kv[2])) {
502                                 /* Need x=y. */
503                                 ERR("Syntax error at '%s'", optarg);
504                                 g_strfreev(kv);
505                                 usage(NULL);
506                         }
507                         if (c == 'p') {
508                                 probe = malloc(sizeof(struct probe));
509                                 probe->name = g_strdup(kv[0]);
510                                 probe->probe = strtoul(kv[1], 0, 10);
511                                 /* Apply to last PD. */
512                                 pd->probes = g_slist_append(pd->probes, probe);
513                         } else {
514                                 option = malloc(sizeof(struct option));
515                                 option->key = g_strdup(kv[0]);
516                                 option->value = g_strdup(kv[1]);
517                                 /* Apply to last PD. */
518                                 pd->options = g_slist_append(pd->options, option);
519                         }
520                         break;
521                 case 'i':
522                         opt_infile = optarg;
523                         break;
524                 case 'O':
525                         opstr = g_strsplit(optarg, ":", 0);
526                         if (!opstr[0] || !opstr[1]) {
527                                 /* Need at least abc:def. */
528                                 ERR("Syntax error at '%s'", optarg);
529                                 g_strfreev(opstr);
530                                 usage(NULL);
531                         }
532                         op->pd = g_strdup(opstr[0]);
533                         if (!strcmp(opstr[1], "annotation"))
534                                 op->type = SRD_OUTPUT_ANN;
535                         else if (!strcmp(opstr[1], "binary"))
536                                 op->type = SRD_OUTPUT_BINARY;
537                         else if (!strcmp(opstr[1], "python"))
538                                 op->type = SRD_OUTPUT_PYTHON;
539                         else {
540                                 ERR("Unknown output type '%s'", opstr[1]);
541                                 g_strfreev(opstr);
542                                 usage(NULL);
543                         }
544                         if (opstr[2])
545                                 op->class = g_strdup(opstr[2]);
546                         g_strfreev(opstr);
547                         break;
548                 case 'f':
549                         op->outfile = g_strdup(optarg);
550                         op->outfd = -1;
551                         break;
552                 case 'S':
553                         statistics = TRUE;
554                         break;
555                 default:
556                         usage(NULL);
557                 }
558         }
559         if (argc > optind)
560                 usage(NULL);
561         if (g_slist_length(pdlist) == 0)
562                 usage(NULL);
563         if (!opt_infile)
564                 usage(NULL);
565         if (!op->pd || op->type == -1)
566                 usage(NULL);
567
568         sr_log_callback_set(sr_log, NULL);
569         if (sr_init(&ctx) != SR_OK)
570                 return 1;
571
572         srd_log_callback_set(srd_log, NULL);
573         if (srd_init(DECODERS_DIR) != SRD_OK)
574                 return 1;
575
576         ret = 0;
577         if (!run_testcase(opt_infile, pdlist, op))
578                 ret = 1;
579
580         srd_exit();
581         sr_exit(ctx);
582
583         return ret;
584 }
585
586