]> sigrok.org Git - libsigrokdecode.git/blame - tests/runtc.c
runtc: Make sure to test this set of decoders, not the installed ones.
[libsigrokdecode.git] / tests / runtc.c
CommitLineData
fbd226c3
BV
1/*
2 * This file is part of the libsigrokdecode project.
3 *
4 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "../libsigrokdecode.h"
21#include <libsigrok/libsigrok.h>
22#include <stdlib.h>
23#include <stdio.h>
24#include <stdarg.h>
25#include <unistd.h>
26#include <errno.h>
27#include <sys/types.h>
28#include <sys/stat.h>
29#include <fcntl.h>
30#include <time.h>
31#include <sys/time.h>
32#include <sys/resource.h>
33#include <glib.h>
34#ifdef __LINUX__
35#include <sched.h>
36#endif
37#include "../config.h"
38
39
40int debug = FALSE;
41int statistics = FALSE;
42
43struct probe {
44 char *name;
45 int probe;
46};
47
48struct option {
49 char *key;
50 char *value;
51};
52
53struct pd {
54 char *name;
55 GSList *probes;
56 GSList *options;
57};
58
59struct output {
60 char *pd;
61 int type;
62 char *class;
63 int class_idx;
64 char *outfile;
65 int outfd;
66};
67
68
69void 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
77void 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
88void 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
97int srd_log(void *cb_data, int loglevel, const char *format, va_list args)
98{
99 (void)cb_data;
100
101 if (loglevel == SRD_LOG_ERR || loglevel == SRD_LOG_WARN)
102 logmsg("Error: srd: ", stderr, format, args);
103 else if (loglevel >= SRD_LOG_DBG && debug)
104 logmsg("DBG: srd: ", stdout, format, args);
105
106 return SRD_OK;
107}
108
109void usage(char *msg)
110{
111 if (msg)
112 fprintf(stderr, "%s\n", msg);
113
114 //while((c = getopt(argc, argv, "dP:p:o:i:O:f:S")) != -1) {
115 printf("Usage: runtc [-dPpoiOf]\n");
116 printf(" -d Debug\n");
117 printf(" -P <protocol decoder>\n");
118 printf(" -p <probename=probenum> (optional)\n");
119 printf(" -o <probeoption=value> (optional)\n");
120 printf(" -i <input file>\n");
121 printf(" -O <output-pd:output-type[:output-class]>\n");
122 printf(" -f <output file> (optional)\n");
123 exit(msg ? 1 : 0);
124
125}
126
127static void srd_cb_ann(struct srd_proto_data *pdata, void *cb_data)
128{
129 struct srd_decoder *dec;
130 struct srd_proto_data_annotation *pda;
131 struct output *op;
132 GString *line;
133 int i;
134 char **dec_ann;
135
136 DBG("Annotation from %s", pdata->pdo->di->inst_id);
137 op = cb_data;
138 pda = pdata->data;
139 dec = pdata->pdo->di->decoder;
140
141 if (strcmp(pdata->pdo->di->inst_id, op->pd))
142 /* This is not the PD selected for output. */
143 return;
144
145 if (op->class_idx != -1 && op->class_idx != pda->ann_format)
146 /* This output takes a specific annotation class,
147 * but not the one that just came in. */
148 return;
149
150 dec_ann = g_slist_nth_data(dec->annotations, pda->ann_format);
151 line = g_string_sized_new(256);
152 g_string_printf(line, "%"PRIu64"-%"PRIu64" %s: %s:",
153 pdata->start_sample, pdata->end_sample,
154 pdata->pdo->di->inst_id, dec_ann[0]);
155 for (i = 0; pda->ann_text[i]; i++)
156 g_string_append_printf(line, " \"%s\"", pda->ann_text[i]);
157 g_string_append(line, "\n");
158 if (write(op->outfd, line->str, line->len) == -1)
159 ERR("Oops!");
160 g_string_free(line, TRUE);
161
162}
163
164static void sr_cb(const struct sr_dev_inst *sdi,
165 const struct sr_datafeed_packet *packet, void *cb_data)
166{
167 const struct sr_datafeed_logic *logic;
168 struct srd_session *sess;
169 GVariant *gvar;
170 uint64_t samplerate;
171 int num_samples;
172 static int samplecnt = 0;
173
174 sess = cb_data;
175
176 switch (packet->type) {
177 case SR_DF_HEADER:
178 DBG("Received SR_DF_HEADER");
179 if (sr_config_get(sdi->driver, sdi, NULL, SR_CONF_SAMPLERATE,
180 &gvar) != SR_OK) {
181 ERR("Getting samplerate failed");
182 break;
183 }
184 samplerate = g_variant_get_uint64(gvar);
185 g_variant_unref(gvar);
186 if (srd_session_metadata_set(sess, SRD_CONF_SAMPLERATE,
187 g_variant_new_uint64(samplerate)) != SRD_OK) {
188 ERR("Setting samplerate failed");
189 break;
190 }
191 if (srd_session_start(sess) != SRD_OK) {
192 ERR("Session start failed");
193 break;
194 }
195 break;
196 case SR_DF_LOGIC:
197 logic = packet->payload;
198 num_samples = logic->length / logic->unitsize;
199 DBG("Received SR_DF_LOGIC: %d samples", num_samples);
200 srd_session_send(sess, samplecnt, samplecnt + num_samples,
201 logic->data, logic->length);
202 samplecnt += logic->length / logic->unitsize;
203 break;
204 case SR_DF_END:
205 DBG("Received SR_DF_END");
206 break;
207 }
208
209}
210
211int get_stats(int stats[2])
212{
213 FILE *f;
214 size_t len;
215 int tmp;
216 char *buf;
217
218 stats[0] = stats[1] = -1;
219 if (!(f = fopen("/proc/self/status", "r")))
220 return FALSE;
221 len = 128;
222 buf = malloc(len);
223 while (getline(&buf, &len, f) != -1) {
224 if (strcasestr(buf, "vmpeak:")) {
225 stats[0] = strtoul(buf + 10, NULL, 10);
226 } else if (strcasestr(buf, "vmsize:")) {
227 tmp = strtoul(buf + 10, NULL, 10);
228 if (tmp > stats[0])
229 stats[0] = tmp;
230 } else if (strcasestr(buf, "vmhwm:")) {
231 stats[1] = strtoul(buf + 6, NULL, 10);
232 } else if (strcasestr(buf, "vmrss:")) {
233 tmp = strtoul(buf + 10, NULL, 10);
234 if (tmp > stats[0])
235 stats[0] = tmp;
236 }
237 }
238 free(buf);
239 fclose(f);
240
241 return TRUE;
242}
243
244static int run_testcase(char *infile, GSList *pdlist, struct output *op)
245{
246 struct srd_session *sess;
247 struct srd_decoder *dec;
248 struct srd_decoder_inst *di, *prev_di;
249 struct pd *pd;
250 struct probe *probe;
251 struct option *option;
252 GVariant *gvar;
253 GHashTable *probes, *opts;
254 GSList *pdl, *l, *annl;
255 int idx;
256 char **dec_ann;
257
258 if (op->outfile) {
259 if ((op->outfd = open(op->outfile, O_CREAT|O_WRONLY, 0600)) == -1) {
260 ERR("Unable to open %s for writing: %s", op->outfile,
261 strerror(errno));
262 return FALSE;
263 }
264 }
265
266 if (sr_session_load(infile) != SR_OK)
267 return FALSE;
268
269 if (srd_session_new(&sess) != SRD_OK)
270 return FALSE;
271 sr_session_datafeed_callback_add(sr_cb, sess);
272 srd_pd_output_callback_add(sess, SRD_OUTPUT_ANN, srd_cb_ann, op);
273
274 prev_di = NULL;
275 pd = NULL;
276 for (pdl = pdlist; pdl; pdl = pdl->next) {
277 pd = pdl->data;
278 if (srd_decoder_load(pd->name) != SRD_OK)
279 return FALSE;
280
281 /* Instantiate decoder and pass in options. */
282 opts = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
283 (GDestroyNotify)g_variant_unref);
284 for (l = pd->options; l; l = l->next) {
285 option = l->data;
286 g_hash_table_insert(opts, option->key, option->value);
287 }
288 if (!(di = srd_inst_new(sess, pd->name, opts)))
289 return FALSE;
290 g_hash_table_destroy(opts);
291
292 /* Map probes. */
293 if (pd->probes) {
294 probes = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
295 (GDestroyNotify)g_variant_unref);
296 for (l = pd->probes; l; l = l->next) {
297 probe = l->data;
298 gvar = g_variant_new_int32(probe->probe);
299 g_variant_ref_sink(gvar);
300 g_hash_table_insert(probes, probe->name, gvar);
301 }
302 if (srd_inst_probe_set_all(di, probes) != SRD_OK)
303 return FALSE;
304 g_hash_table_destroy(probes);
305 }
306
307
308 /* If this is not the first decoder in the list, stack it
309 * on top of the previous one. */
310 if (prev_di) {
311 if (srd_inst_stack(sess, prev_di, di) != SRD_OK) {
312 ERR("Failed to stack decoder instances.");
313 return FALSE;
314 }
315 }
316 prev_di = di;
317 }
318
319 /* Resolve top decoder's class index, so we can match. */
320 dec = srd_decoder_get_by_id(pd->name);
321 if (op->class) {
322 if (op->type == SRD_OUTPUT_ANN)
323 annl = dec->annotations;
324 /* TODO can't dereference this for binary yet
325 else if (op->type == SRD_OUTPUT_BINARY)
326 annl = dec->binary;
327 */
328 else
329 /* Only annotations and binary for now. */
330 return FALSE;
331 idx = 0;
332 while(annl) {
333 dec_ann = annl->data;
334 /* TODO can't dereference this for binary yet */
335 if (!strcmp(dec_ann[0], op->class)) {
336 op->class_idx = idx;
337 break;
338 } else
339 idx++;
340 annl = annl->next;
341 }
342 if (op->class_idx == -1) {
343 ERR("Output class '%s' not found in decoder %s.",
344 op->class, pd->name);
345 return FALSE;
346 }
347 }
348
349 sr_session_start();
350 sr_session_run();
351 sr_session_stop();
352
353 srd_session_destroy(sess);
354
355 if (op->outfile)
356 close(op->outfd);
357
358 return TRUE;
359}
360
361int main(int argc, char **argv)
362{
363 struct sr_context *ctx;
364 GSList *pdlist;
365 struct pd *pd;
366 struct probe *probe;
367 struct option *option;
368 struct output *op;
369 char c, *opt_infile, **kv, **opstr;
370
371 op = malloc(sizeof(struct output));
372 op->pd = NULL;
373 op->type = -1;
374 op->class = NULL;
375 op->class_idx = -1;
376 op->outfd = 1;
377
378 pdlist = NULL;
379 opt_infile = NULL;
380 pd = NULL;
381 while((c = getopt(argc, argv, "dP:p:o:i:O:f:S")) != -1) {
382 switch(c) {
383 case 'd':
384 debug = TRUE;
385 break;
386 case 'P':
387 pd = g_malloc(sizeof(struct pd));
388 pd->name = g_strdup(optarg);
389 pd->probes = pd->options = NULL;
390 pdlist = g_slist_append(pdlist, pd);
391 break;
392 case 'p':
393 case 'o':
394 if (g_slist_length(pdlist) == 0) {
395 /* No previous -P. */
396 ERR("Syntax error at '%s'", optarg);
397 usage(NULL);
398 }
399 kv = g_strsplit(optarg, "=", 0);
400 if (!kv[0] || (!kv[1] || kv[2])) {
401 /* Need x=y. */
402 ERR("Syntax error at '%s'", optarg);
403 g_strfreev(kv);
404 usage(NULL);
405 }
406 if (c == 'p') {
407 probe = malloc(sizeof(struct probe));
408 probe->name = g_strdup(kv[0]);
409 probe->probe = strtoul(kv[1], 0, 10);
410 /* Apply to last PD. */
411 pd->probes = g_slist_append(pd->probes, probe);
412 } else {
413 option = malloc(sizeof(struct option));
414 option->key = g_strdup(kv[0]);
415 option->value = g_strdup(kv[1]);
416 /* Apply to last PD. */
417 pd->options = g_slist_append(pd->options, option);
418 }
419 break;
420 case 'i':
421 opt_infile = optarg;
422 break;
423 case 'O':
424 opstr = g_strsplit(optarg, ":", 0);
425 if (!opstr[0] || !opstr[1]) {
426 /* Need at least abc:def. */
427 ERR("Syntax error at '%s'", optarg);
428 g_strfreev(opstr);
429 usage(NULL);
430 }
431 op->pd = g_strdup(opstr[0]);
432 if (!strcmp(opstr[1], "annotation"))
433 op->type = SRD_OUTPUT_ANN;
434 else if (!strcmp(opstr[1], "binary"))
435 op->type = SRD_OUTPUT_BINARY;
436 else if (!strcmp(opstr[1], "python"))
437 op->type = SRD_OUTPUT_PYTHON;
438 else {
439 ERR("Unknown output type '%s'", opstr[1]);
440 g_strfreev(opstr);
441 usage(NULL);
442 }
443 if (opstr[2])
444 op->class = g_strdup(opstr[2]);
445 g_strfreev(opstr);
446 break;
447 case 'f':
448 op->outfile = g_strdup(optarg);
449 op->outfd = -1;
450 break;
451 case 'S':
452 statistics = TRUE;
453 break;
454 default:
455 usage(NULL);
456 }
457 }
458 if (argc > optind)
459 usage(NULL);
460 if (g_slist_length(pdlist) == 0)
461 usage(NULL);
462 if (!opt_infile)
463 usage(NULL);
464 if (!op->pd || op->type == -1)
465 usage(NULL);
466
467 if (sr_init(&ctx) != SR_OK)
468 return 1;
469
470 srd_log_callback_set(srd_log, NULL);
b7482381 471 if (srd_init(DECODERS_DIR) != SRD_OK)
fbd226c3
BV
472 return 1;
473
474 run_testcase(opt_infile, pdlist, op);
475
476 srd_exit();
477 sr_exit(ctx);
478
479 return 0;
480}
481
482