2 * This file is part of the libsigrokdecode project.
4 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
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.
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.
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/>.
20 #include "../libsigrokdecode.h"
21 #include <libsigrok/libsigrok.h>
27 #include <sys/types.h>
32 #include <sys/resource.h>
37 #include "../config.h"
41 int statistics = FALSE;
69 void logmsg(char *prefix, FILE *out, const char *format, va_list args)
72 fprintf(out, "%s", prefix);
73 vfprintf(out, format, args);
77 void DBG(const char *format, ...)
83 va_start(args, format);
84 logmsg("DBG: ", stdout, format, args);
88 void ERR(const char *format, ...)
92 va_start(args, format);
93 logmsg("Error: ", stderr, format, args);
97 int sr_log(void *cb_data, int loglevel, const char *format, va_list args)
101 if (loglevel == SR_LOG_ERR || loglevel == SR_LOG_WARN)
102 logmsg("Error: sr: ", stderr, format, args);
104 logmsg("DBG: sr: ", stdout, format, args);
109 int srd_log(void *cb_data, int loglevel, const char *format, va_list args)
113 if (loglevel == SRD_LOG_ERR || loglevel == SRD_LOG_WARN)
114 logmsg("Error: srd: ", stderr, format, args);
116 logmsg("DBG: srd: ", stdout, format, args);
121 void usage(char *msg)
124 fprintf(stderr, "%s\n", msg);
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");
139 static void srd_cb_ann(struct srd_proto_data *pdata, void *cb_data)
141 struct srd_decoder *dec;
142 struct srd_proto_data_annotation *pda;
148 DBG("Annotation from %s", pdata->pdo->di->inst_id);
151 dec = pdata->pdo->di->decoder;
153 if (strcmp(pdata->pdo->di->inst_id, op->pd))
154 /* This is not the PD selected for output. */
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. */
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)
172 g_string_free(line, TRUE);
176 static void sr_cb(const struct sr_dev_inst *sdi,
177 const struct sr_datafeed_packet *packet, void *cb_data)
179 const struct sr_datafeed_logic *logic;
180 struct srd_session *sess;
184 static int samplecnt = 0;
188 switch (packet->type) {
190 DBG("Received SR_DF_HEADER");
191 if (sr_config_get(sdi->driver, sdi, NULL, SR_CONF_SAMPLERATE,
193 ERR("Getting samplerate failed");
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");
203 if (srd_session_start(sess) != SRD_OK) {
204 ERR("Session start failed");
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;
217 DBG("Received SR_DF_END");
223 int get_stats(int stats[2])
230 stats[0] = stats[1] = -1;
231 if (!(f = fopen("/proc/self/status", "r")))
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);
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);
256 static int run_testcase(char *infile, GSList *pdlist, struct output *op)
258 struct srd_session *sess;
259 struct srd_decoder *dec;
260 struct srd_decoder_inst *di, *prev_di;
263 struct option *option;
265 GHashTable *probes, *opts;
266 GSList *pdl, *l, *annl;
271 if ((op->outfd = open(op->outfile, O_CREAT|O_WRONLY, 0600)) == -1) {
272 ERR("Unable to open %s for writing: %s", op->outfile,
278 if (sr_session_load(infile) != SR_OK)
281 if (srd_session_new(&sess) != SRD_OK)
283 sr_session_datafeed_callback_add(sr_cb, sess);
284 srd_pd_output_callback_add(sess, SRD_OUTPUT_ANN, srd_cb_ann, op);
288 for (pdl = pdlist; pdl; pdl = pdl->next) {
290 if (srd_decoder_load(pd->name) != SRD_OK)
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) {
298 g_hash_table_insert(opts, option->key, option->value);
300 if (!(di = srd_inst_new(sess, pd->name, opts)))
302 g_hash_table_destroy(opts);
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) {
310 gvar = g_variant_new_int32(probe->probe);
311 g_variant_ref_sink(gvar);
312 g_hash_table_insert(probes, probe->name, gvar);
314 if (srd_inst_probe_set_all(di, probes) != SRD_OK)
316 g_hash_table_destroy(probes);
320 /* If this is not the first decoder in the list, stack it
321 * on top of the previous one. */
323 if (srd_inst_stack(sess, prev_di, di) != SRD_OK) {
324 ERR("Failed to stack decoder instances.");
331 /* Resolve top decoder's class index, so we can match. */
332 dec = srd_decoder_get_by_id(pd->name);
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)
341 /* Only annotations and binary for now. */
345 dec_ann = annl->data;
346 /* TODO can't dereference this for binary yet */
347 if (!strcmp(dec_ann[0], op->class)) {
354 if (op->class_idx == -1) {
355 ERR("Output class '%s' not found in decoder %s.",
356 op->class, pd->name);
365 srd_session_destroy(sess);
373 int main(int argc, char **argv)
375 struct sr_context *ctx;
379 struct option *option;
381 char c, *opt_infile, **kv, **opstr;
383 op = malloc(sizeof(struct output));
393 while((c = getopt(argc, argv, "dP:p:o:i:O:f:S")) != -1) {
399 pd = g_malloc(sizeof(struct pd));
400 pd->name = g_strdup(optarg);
401 pd->probes = pd->options = NULL;
402 pdlist = g_slist_append(pdlist, pd);
406 if (g_slist_length(pdlist) == 0) {
407 /* No previous -P. */
408 ERR("Syntax error at '%s'", optarg);
411 kv = g_strsplit(optarg, "=", 0);
412 if (!kv[0] || (!kv[1] || kv[2])) {
414 ERR("Syntax error at '%s'", optarg);
419 probe = malloc(sizeof(struct probe));
420 probe->name = g_strdup(kv[0]);
421 probe->probe = strtoul(kv[1], 0, 10);
422 /* Apply to last PD. */
423 pd->probes = g_slist_append(pd->probes, probe);
425 option = malloc(sizeof(struct option));
426 option->key = g_strdup(kv[0]);
427 option->value = g_strdup(kv[1]);
428 /* Apply to last PD. */
429 pd->options = g_slist_append(pd->options, option);
436 opstr = g_strsplit(optarg, ":", 0);
437 if (!opstr[0] || !opstr[1]) {
438 /* Need at least abc:def. */
439 ERR("Syntax error at '%s'", optarg);
443 op->pd = g_strdup(opstr[0]);
444 if (!strcmp(opstr[1], "annotation"))
445 op->type = SRD_OUTPUT_ANN;
446 else if (!strcmp(opstr[1], "binary"))
447 op->type = SRD_OUTPUT_BINARY;
448 else if (!strcmp(opstr[1], "python"))
449 op->type = SRD_OUTPUT_PYTHON;
451 ERR("Unknown output type '%s'", opstr[1]);
456 op->class = g_strdup(opstr[2]);
460 op->outfile = g_strdup(optarg);
472 if (g_slist_length(pdlist) == 0)
476 if (!op->pd || op->type == -1)
479 sr_log_callback_set(sr_log, NULL);
480 if (sr_init(&ctx) != SR_OK)
483 srd_log_callback_set(srd_log, NULL);
484 if (srd_init(DECODERS_DIR) != SRD_OK)
487 run_testcase(opt_infile, pdlist, op);