]> sigrok.org Git - libsigrokdecode.git/blame - tests/runtc.c
Easier access to sequences of strings, not just lists.
[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
caa4b2cc
BV
97int 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
fbd226c3
BV
109int 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);
caa4b2cc 115 else if (debug)
fbd226c3
BV
116 logmsg("DBG: srd: ", stdout, format, args);
117
118 return SRD_OK;
119}
120
121void usage(char *msg)
122{
123 if (msg)
124 fprintf(stderr, "%s\n", msg);
125
126 //while((c = getopt(argc, argv, "dP:p:o:i:O:f:S")) != -1) {
127 printf("Usage: runtc [-dPpoiOf]\n");
128 printf(" -d Debug\n");
129 printf(" -P <protocol decoder>\n");
130 printf(" -p <probename=probenum> (optional)\n");
131 printf(" -o <probeoption=value> (optional)\n");
132 printf(" -i <input file>\n");
133 printf(" -O <output-pd:output-type[:output-class]>\n");
134 printf(" -f <output file> (optional)\n");
135 exit(msg ? 1 : 0);
136
137}
138
139static void srd_cb_ann(struct srd_proto_data *pdata, void *cb_data)
140{
141 struct srd_decoder *dec;
142 struct srd_proto_data_annotation *pda;
143 struct output *op;
144 GString *line;
145 int i;
146 char **dec_ann;
147
148 DBG("Annotation from %s", pdata->pdo->di->inst_id);
149 op = cb_data;
150 pda = pdata->data;
151 dec = pdata->pdo->di->decoder;
152
153 if (strcmp(pdata->pdo->di->inst_id, op->pd))
154 /* This is not the PD selected for output. */
155 return;
156
157 if (op->class_idx != -1 && op->class_idx != pda->ann_format)
158 /* This output takes a specific annotation class,
159 * but not the one that just came in. */
160 return;
161
162 dec_ann = g_slist_nth_data(dec->annotations, pda->ann_format);
163 line = g_string_sized_new(256);
164 g_string_printf(line, "%"PRIu64"-%"PRIu64" %s: %s:",
165 pdata->start_sample, pdata->end_sample,
166 pdata->pdo->di->inst_id, dec_ann[0]);
167 for (i = 0; pda->ann_text[i]; i++)
168 g_string_append_printf(line, " \"%s\"", pda->ann_text[i]);
169 g_string_append(line, "\n");
170 if (write(op->outfd, line->str, line->len) == -1)
171 ERR("Oops!");
172 g_string_free(line, TRUE);
173
174}
175
176static void sr_cb(const struct sr_dev_inst *sdi,
177 const struct sr_datafeed_packet *packet, void *cb_data)
178{
179 const struct sr_datafeed_logic *logic;
180 struct srd_session *sess;
181 GVariant *gvar;
182 uint64_t samplerate;
183 int num_samples;
184 static int samplecnt = 0;
185
186 sess = cb_data;
187
188 switch (packet->type) {
189 case SR_DF_HEADER:
190 DBG("Received SR_DF_HEADER");
191 if (sr_config_get(sdi->driver, sdi, NULL, SR_CONF_SAMPLERATE,
192 &gvar) != SR_OK) {
193 ERR("Getting samplerate failed");
194 break;
195 }
196 samplerate = g_variant_get_uint64(gvar);
197 g_variant_unref(gvar);
198 if (srd_session_metadata_set(sess, SRD_CONF_SAMPLERATE,
199 g_variant_new_uint64(samplerate)) != SRD_OK) {
200 ERR("Setting samplerate failed");
201 break;
202 }
203 if (srd_session_start(sess) != SRD_OK) {
204 ERR("Session start failed");
205 break;
206 }
207 break;
208 case SR_DF_LOGIC:
209 logic = packet->payload;
210 num_samples = logic->length / logic->unitsize;
211 DBG("Received SR_DF_LOGIC: %d samples", num_samples);
212 srd_session_send(sess, samplecnt, samplecnt + num_samples,
213 logic->data, logic->length);
214 samplecnt += logic->length / logic->unitsize;
215 break;
216 case SR_DF_END:
217 DBG("Received SR_DF_END");
218 break;
219 }
220
221}
222
223int get_stats(int stats[2])
224{
225 FILE *f;
226 size_t len;
227 int tmp;
228 char *buf;
229
230 stats[0] = stats[1] = -1;
231 if (!(f = fopen("/proc/self/status", "r")))
232 return FALSE;
233 len = 128;
234 buf = malloc(len);
235 while (getline(&buf, &len, f) != -1) {
236 if (strcasestr(buf, "vmpeak:")) {
237 stats[0] = strtoul(buf + 10, NULL, 10);
238 } else if (strcasestr(buf, "vmsize:")) {
239 tmp = strtoul(buf + 10, NULL, 10);
240 if (tmp > stats[0])
241 stats[0] = tmp;
242 } else if (strcasestr(buf, "vmhwm:")) {
243 stats[1] = strtoul(buf + 6, NULL, 10);
244 } else if (strcasestr(buf, "vmrss:")) {
245 tmp = strtoul(buf + 10, NULL, 10);
246 if (tmp > stats[0])
247 stats[0] = tmp;
248 }
249 }
250 free(buf);
251 fclose(f);
252
253 return TRUE;
254}
255
256static int run_testcase(char *infile, GSList *pdlist, struct output *op)
257{
258 struct srd_session *sess;
259 struct srd_decoder *dec;
260 struct srd_decoder_inst *di, *prev_di;
261 struct pd *pd;
262 struct probe *probe;
263 struct option *option;
264 GVariant *gvar;
265 GHashTable *probes, *opts;
266 GSList *pdl, *l, *annl;
267 int idx;
268 char **dec_ann;
269
270 if (op->outfile) {
271 if ((op->outfd = open(op->outfile, O_CREAT|O_WRONLY, 0600)) == -1) {
272 ERR("Unable to open %s for writing: %s", op->outfile,
273 strerror(errno));
274 return FALSE;
275 }
276 }
277
278 if (sr_session_load(infile) != SR_OK)
279 return FALSE;
280
281 if (srd_session_new(&sess) != SRD_OK)
282 return FALSE;
283 sr_session_datafeed_callback_add(sr_cb, sess);
284 srd_pd_output_callback_add(sess, SRD_OUTPUT_ANN, srd_cb_ann, op);
285
286 prev_di = NULL;
287 pd = NULL;
288 for (pdl = pdlist; pdl; pdl = pdl->next) {
289 pd = pdl->data;
290 if (srd_decoder_load(pd->name) != SRD_OK)
291 return FALSE;
292
293 /* Instantiate decoder and pass in options. */
294 opts = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
295 (GDestroyNotify)g_variant_unref);
296 for (l = pd->options; l; l = l->next) {
297 option = l->data;
298 g_hash_table_insert(opts, option->key, option->value);
299 }
300 if (!(di = srd_inst_new(sess, pd->name, opts)))
301 return FALSE;
302 g_hash_table_destroy(opts);
303
304 /* Map probes. */
305 if (pd->probes) {
306 probes = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
307 (GDestroyNotify)g_variant_unref);
308 for (l = pd->probes; l; l = l->next) {
309 probe = l->data;
310 gvar = g_variant_new_int32(probe->probe);
311 g_variant_ref_sink(gvar);
312 g_hash_table_insert(probes, probe->name, gvar);
313 }
314 if (srd_inst_probe_set_all(di, probes) != SRD_OK)
315 return FALSE;
316 g_hash_table_destroy(probes);
317 }
318
319
320 /* If this is not the first decoder in the list, stack it
321 * on top of the previous one. */
322 if (prev_di) {
323 if (srd_inst_stack(sess, prev_di, di) != SRD_OK) {
324 ERR("Failed to stack decoder instances.");
325 return FALSE;
326 }
327 }
328 prev_di = di;
329 }
330
331 /* Resolve top decoder's class index, so we can match. */
332 dec = srd_decoder_get_by_id(pd->name);
333 if (op->class) {
334 if (op->type == SRD_OUTPUT_ANN)
335 annl = dec->annotations;
336 /* TODO can't dereference this for binary yet
337 else if (op->type == SRD_OUTPUT_BINARY)
338 annl = dec->binary;
339 */
340 else
341 /* Only annotations and binary for now. */
342 return FALSE;
343 idx = 0;
344 while(annl) {
345 dec_ann = annl->data;
346 /* TODO can't dereference this for binary yet */
347 if (!strcmp(dec_ann[0], op->class)) {
348 op->class_idx = idx;
349 break;
350 } else
351 idx++;
352 annl = annl->next;
353 }
354 if (op->class_idx == -1) {
355 ERR("Output class '%s' not found in decoder %s.",
356 op->class, pd->name);
357 return FALSE;
358 }
359 }
360
361 sr_session_start();
362 sr_session_run();
363 sr_session_stop();
364
365 srd_session_destroy(sess);
366
367 if (op->outfile)
368 close(op->outfd);
369
370 return TRUE;
371}
372
373int main(int argc, char **argv)
374{
375 struct sr_context *ctx;
376 GSList *pdlist;
377 struct pd *pd;
378 struct probe *probe;
379 struct option *option;
380 struct output *op;
932606db 381 int ret;
fbd226c3
BV
382 char c, *opt_infile, **kv, **opstr;
383
384 op = malloc(sizeof(struct output));
385 op->pd = NULL;
386 op->type = -1;
387 op->class = NULL;
388 op->class_idx = -1;
389 op->outfd = 1;
390
391 pdlist = NULL;
392 opt_infile = NULL;
393 pd = NULL;
394 while((c = getopt(argc, argv, "dP:p:o:i:O:f:S")) != -1) {
395 switch(c) {
396 case 'd':
397 debug = TRUE;
398 break;
399 case 'P':
400 pd = g_malloc(sizeof(struct pd));
401 pd->name = g_strdup(optarg);
402 pd->probes = pd->options = NULL;
403 pdlist = g_slist_append(pdlist, pd);
404 break;
405 case 'p':
406 case 'o':
407 if (g_slist_length(pdlist) == 0) {
408 /* No previous -P. */
409 ERR("Syntax error at '%s'", optarg);
410 usage(NULL);
411 }
412 kv = g_strsplit(optarg, "=", 0);
413 if (!kv[0] || (!kv[1] || kv[2])) {
414 /* Need x=y. */
415 ERR("Syntax error at '%s'", optarg);
416 g_strfreev(kv);
417 usage(NULL);
418 }
419 if (c == 'p') {
420 probe = malloc(sizeof(struct probe));
421 probe->name = g_strdup(kv[0]);
422 probe->probe = strtoul(kv[1], 0, 10);
423 /* Apply to last PD. */
424 pd->probes = g_slist_append(pd->probes, probe);
425 } else {
426 option = malloc(sizeof(struct option));
427 option->key = g_strdup(kv[0]);
428 option->value = g_strdup(kv[1]);
429 /* Apply to last PD. */
430 pd->options = g_slist_append(pd->options, option);
431 }
432 break;
433 case 'i':
434 opt_infile = optarg;
435 break;
436 case 'O':
437 opstr = g_strsplit(optarg, ":", 0);
438 if (!opstr[0] || !opstr[1]) {
439 /* Need at least abc:def. */
440 ERR("Syntax error at '%s'", optarg);
441 g_strfreev(opstr);
442 usage(NULL);
443 }
444 op->pd = g_strdup(opstr[0]);
445 if (!strcmp(opstr[1], "annotation"))
446 op->type = SRD_OUTPUT_ANN;
447 else if (!strcmp(opstr[1], "binary"))
448 op->type = SRD_OUTPUT_BINARY;
449 else if (!strcmp(opstr[1], "python"))
450 op->type = SRD_OUTPUT_PYTHON;
451 else {
452 ERR("Unknown output type '%s'", opstr[1]);
453 g_strfreev(opstr);
454 usage(NULL);
455 }
456 if (opstr[2])
457 op->class = g_strdup(opstr[2]);
458 g_strfreev(opstr);
459 break;
460 case 'f':
461 op->outfile = g_strdup(optarg);
462 op->outfd = -1;
463 break;
464 case 'S':
465 statistics = TRUE;
466 break;
467 default:
468 usage(NULL);
469 }
470 }
471 if (argc > optind)
472 usage(NULL);
473 if (g_slist_length(pdlist) == 0)
474 usage(NULL);
475 if (!opt_infile)
476 usage(NULL);
477 if (!op->pd || op->type == -1)
478 usage(NULL);
479
caa4b2cc 480 sr_log_callback_set(sr_log, NULL);
fbd226c3
BV
481 if (sr_init(&ctx) != SR_OK)
482 return 1;
483
484 srd_log_callback_set(srd_log, NULL);
b7482381 485 if (srd_init(DECODERS_DIR) != SRD_OK)
fbd226c3
BV
486 return 1;
487
932606db
BV
488 ret = 0;
489 if (!run_testcase(opt_infile, pdlist, op))
490 ret = 1;
fbd226c3
BV
491
492 srd_exit();
493 sr_exit(ctx);
494
932606db 495 return ret;
fbd226c3
BV
496}
497
498