]>
Commit | Line | Data |
---|---|---|
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 | ||
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 | ||
caa4b2cc BV |
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 | ||
fbd226c3 BV |
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); | |
caa4b2cc | 115 | else if (debug) |
fbd226c3 BV |
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 | //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 | ||
139 | static 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 | ||
176 | static 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 | ||
223 | int 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 | ||
256 | static 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; | |
6b85745a | 266 | GSList *pdl, *l; |
fbd226c3 | 267 | int idx; |
6b85745a | 268 | char **decoder_class; |
fbd226c3 BV |
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) | |
6b85745a | 335 | l = dec->annotations; |
fbd226c3 | 336 | else if (op->type == SRD_OUTPUT_BINARY) |
6b85745a | 337 | l = dec->binary; |
fbd226c3 BV |
338 | else |
339 | /* Only annotations and binary for now. */ | |
340 | return FALSE; | |
341 | idx = 0; | |
6b85745a BV |
342 | while(l) { |
343 | decoder_class = l->data; | |
344 | if (!strcmp(decoder_class[0], op->class)) { | |
fbd226c3 BV |
345 | op->class_idx = idx; |
346 | break; | |
347 | } else | |
348 | idx++; | |
6b85745a | 349 | l = l->next; |
fbd226c3 BV |
350 | } |
351 | if (op->class_idx == -1) { | |
352 | ERR("Output class '%s' not found in decoder %s.", | |
353 | op->class, pd->name); | |
354 | return FALSE; | |
355 | } | |
356 | } | |
357 | ||
358 | sr_session_start(); | |
359 | sr_session_run(); | |
360 | sr_session_stop(); | |
361 | ||
362 | srd_session_destroy(sess); | |
363 | ||
364 | if (op->outfile) | |
365 | close(op->outfd); | |
366 | ||
367 | return TRUE; | |
368 | } | |
369 | ||
370 | int main(int argc, char **argv) | |
371 | { | |
372 | struct sr_context *ctx; | |
373 | GSList *pdlist; | |
374 | struct pd *pd; | |
375 | struct probe *probe; | |
376 | struct option *option; | |
377 | struct output *op; | |
932606db | 378 | int ret; |
fbd226c3 BV |
379 | char c, *opt_infile, **kv, **opstr; |
380 | ||
381 | op = malloc(sizeof(struct output)); | |
382 | op->pd = NULL; | |
383 | op->type = -1; | |
384 | op->class = NULL; | |
385 | op->class_idx = -1; | |
386 | op->outfd = 1; | |
387 | ||
388 | pdlist = NULL; | |
389 | opt_infile = NULL; | |
390 | pd = NULL; | |
391 | while((c = getopt(argc, argv, "dP:p:o:i:O:f:S")) != -1) { | |
392 | switch(c) { | |
393 | case 'd': | |
394 | debug = TRUE; | |
395 | break; | |
396 | case 'P': | |
397 | pd = g_malloc(sizeof(struct pd)); | |
398 | pd->name = g_strdup(optarg); | |
399 | pd->probes = pd->options = NULL; | |
400 | pdlist = g_slist_append(pdlist, pd); | |
401 | break; | |
402 | case 'p': | |
403 | case 'o': | |
404 | if (g_slist_length(pdlist) == 0) { | |
405 | /* No previous -P. */ | |
406 | ERR("Syntax error at '%s'", optarg); | |
407 | usage(NULL); | |
408 | } | |
409 | kv = g_strsplit(optarg, "=", 0); | |
410 | if (!kv[0] || (!kv[1] || kv[2])) { | |
411 | /* Need x=y. */ | |
412 | ERR("Syntax error at '%s'", optarg); | |
413 | g_strfreev(kv); | |
414 | usage(NULL); | |
415 | } | |
416 | if (c == 'p') { | |
417 | probe = malloc(sizeof(struct probe)); | |
418 | probe->name = g_strdup(kv[0]); | |
419 | probe->probe = strtoul(kv[1], 0, 10); | |
420 | /* Apply to last PD. */ | |
421 | pd->probes = g_slist_append(pd->probes, probe); | |
422 | } else { | |
423 | option = malloc(sizeof(struct option)); | |
424 | option->key = g_strdup(kv[0]); | |
425 | option->value = g_strdup(kv[1]); | |
426 | /* Apply to last PD. */ | |
427 | pd->options = g_slist_append(pd->options, option); | |
428 | } | |
429 | break; | |
430 | case 'i': | |
431 | opt_infile = optarg; | |
432 | break; | |
433 | case 'O': | |
434 | opstr = g_strsplit(optarg, ":", 0); | |
435 | if (!opstr[0] || !opstr[1]) { | |
436 | /* Need at least abc:def. */ | |
437 | ERR("Syntax error at '%s'", optarg); | |
438 | g_strfreev(opstr); | |
439 | usage(NULL); | |
440 | } | |
441 | op->pd = g_strdup(opstr[0]); | |
442 | if (!strcmp(opstr[1], "annotation")) | |
443 | op->type = SRD_OUTPUT_ANN; | |
444 | else if (!strcmp(opstr[1], "binary")) | |
445 | op->type = SRD_OUTPUT_BINARY; | |
446 | else if (!strcmp(opstr[1], "python")) | |
447 | op->type = SRD_OUTPUT_PYTHON; | |
448 | else { | |
449 | ERR("Unknown output type '%s'", opstr[1]); | |
450 | g_strfreev(opstr); | |
451 | usage(NULL); | |
452 | } | |
453 | if (opstr[2]) | |
454 | op->class = g_strdup(opstr[2]); | |
455 | g_strfreev(opstr); | |
456 | break; | |
457 | case 'f': | |
458 | op->outfile = g_strdup(optarg); | |
459 | op->outfd = -1; | |
460 | break; | |
461 | case 'S': | |
462 | statistics = TRUE; | |
463 | break; | |
464 | default: | |
465 | usage(NULL); | |
466 | } | |
467 | } | |
468 | if (argc > optind) | |
469 | usage(NULL); | |
470 | if (g_slist_length(pdlist) == 0) | |
471 | usage(NULL); | |
472 | if (!opt_infile) | |
473 | usage(NULL); | |
474 | if (!op->pd || op->type == -1) | |
475 | usage(NULL); | |
476 | ||
caa4b2cc | 477 | sr_log_callback_set(sr_log, NULL); |
fbd226c3 BV |
478 | if (sr_init(&ctx) != SR_OK) |
479 | return 1; | |
480 | ||
481 | srd_log_callback_set(srd_log, NULL); | |
b7482381 | 482 | if (srd_init(DECODERS_DIR) != SRD_OK) |
fbd226c3 BV |
483 | return 1; |
484 | ||
932606db BV |
485 | ret = 0; |
486 | if (!run_testcase(opt_infile, pdlist, op)) | |
487 | ret = 1; | |
fbd226c3 BV |
488 | |
489 | srd_exit(); | |
490 | sr_exit(ctx); | |
491 | ||
932606db | 492 | return ret; |
fbd226c3 BV |
493 | } |
494 | ||
495 |