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"
40 int statistics = FALSE;
68 static void logmsg(char *prefix, FILE *out, const char *format, va_list args)
71 fprintf(out, "%s", prefix);
72 vfprintf(out, format, args);
76 static void DBG(const char *format, ...)
82 va_start(args, format);
83 logmsg("DBG: ", stdout, format, args);
87 static void ERR(const char *format, ...)
91 va_start(args, format);
92 logmsg("Error: ", stderr, format, args);
96 static int sr_log(void *cb_data, int loglevel, const char *format, va_list args)
100 if (loglevel == SR_LOG_ERR || loglevel == SR_LOG_WARN)
101 logmsg("Error: sr: ", stderr, format, args);
103 logmsg("DBG: sr: ", stdout, format, args);
108 static int srd_log(void *cb_data, int loglevel, const char *format, va_list args)
112 if (loglevel == SRD_LOG_ERR || loglevel == SRD_LOG_WARN)
113 logmsg("Error: srd: ", stderr, format, args);
115 logmsg("DBG: srd: ", stdout, format, args);
120 static void usage(char *msg)
123 fprintf(stderr, "%s\n", msg);
125 printf("Usage: runtc [-dPpoiOf]\n");
126 printf(" -d Debug\n");
127 printf(" -P <protocol decoder>\n");
128 printf(" -p <probename=probenum> (optional)\n");
129 printf(" -o <probeoption=value> (optional)\n");
130 printf(" -i <input file>\n");
131 printf(" -O <output-pd:output-type[:output-class]>\n");
132 printf(" -f <output file> (optional)\n");
137 /* This is a neutered version of libsigrokdecode's py_str_as_str(). It
138 * does no error checking, but then the only strings it processes are
139 * generated by Python's repr(), so are known good. */
140 static char *py_str_as_str(const PyObject *py_str)
145 py_encstr = PyUnicode_AsEncodedString((PyObject *)py_str, "utf-8", NULL);
146 str = PyBytes_AS_STRING(py_encstr);
147 outstr = g_strdup(str);
148 Py_DecRef(py_encstr);
153 static void srd_cb_py(struct srd_proto_data *pdata, void *cb_data)
156 PyObject *pydata, *pyrepr;
160 DBG("Python output from %s", pdata->pdo->di->inst_id);
162 pydata = pdata->data;
163 DBG("ptr %p", pydata);
165 if (strcmp(pdata->pdo->di->inst_id, op->pd))
166 /* This is not the PD selected for output. */
169 if (!(pyrepr = PyObject_Repr(pydata))) {
170 ERR("Invalid Python object.");
173 s = py_str_as_str(pyrepr);
176 /* Output format for testing is '<ss>-<es> <inst-id>: <repr>\n' */
177 out = g_string_sized_new(128);
178 g_string_printf(out, "%"PRIu64"-%"PRIu64" %s: %s\n",
179 pdata->start_sample, pdata->end_sample,
180 pdata->pdo->di->inst_id, s);
182 if (write(op->outfd, out->str, out->len) == -1)
183 ERR("SRD_OUTPUT_PYTHON callback write failure!");
184 DBG("wrote '%s'", out->str);
185 g_string_free(out, TRUE);
189 static void srd_cb_bin(struct srd_proto_data *pdata, void *cb_data)
191 struct srd_proto_data_binary *pdb;
196 DBG("Binary output from %s", pdata->pdo->di->inst_id);
200 if (strcmp(pdata->pdo->di->inst_id, op->pd))
201 /* This is not the PD selected for output. */
204 if (op->class_idx != -1 && op->class_idx != pdb->bin_class)
206 * This output takes a specific binary class,
207 * but not the one that just came in.
211 out = g_string_sized_new(128);
212 g_string_printf(out, "%"PRIu64"-%"PRIu64" %s:",
213 pdata->start_sample, pdata->end_sample,
214 pdata->pdo->di->inst_id);
215 for (i = 0; i < pdb->size; i++) {
216 g_string_append_printf(out, " %.2x", pdb->data[i]);
218 g_string_append(out, "\n");
219 if (write(op->outfd, out->str, out->len) == -1)
220 ERR("SRD_OUTPUT_BINARY callback write failure!");
224 static void srd_cb_ann(struct srd_proto_data *pdata, void *cb_data)
226 struct srd_decoder *dec;
227 struct srd_proto_data_annotation *pda;
233 DBG("Annotation output from %s", pdata->pdo->di->inst_id);
236 dec = pdata->pdo->di->decoder;
237 if (strcmp(pdata->pdo->di->inst_id, op->pd))
238 /* This is not the PD selected for output. */
241 if (op->class_idx != -1 && op->class_idx != pda->ann_format)
243 * This output takes a specific annotation class,
244 * but not the one that just came in.
248 dec_ann = g_slist_nth_data(dec->annotations, pda->ann_format);
249 line = g_string_sized_new(256);
250 g_string_printf(line, "%"PRIu64"-%"PRIu64" %s: %s:",
251 pdata->start_sample, pdata->end_sample,
252 pdata->pdo->di->inst_id, dec_ann[0]);
253 for (i = 0; pda->ann_text[i]; i++)
254 g_string_append_printf(line, " \"%s\"", pda->ann_text[i]);
255 g_string_append(line, "\n");
256 if (write(op->outfd, line->str, line->len) == -1)
257 ERR("SRD_OUTPUT_ANN callback write failure!");
258 g_string_free(line, TRUE);
262 static void sr_cb(const struct sr_dev_inst *sdi,
263 const struct sr_datafeed_packet *packet, void *cb_data)
265 const struct sr_datafeed_logic *logic;
266 struct srd_session *sess;
270 static int samplecnt = 0;
274 switch (packet->type) {
276 DBG("Received SR_DF_HEADER");
277 if (sr_config_get(sdi->driver, sdi, NULL, SR_CONF_SAMPLERATE,
279 ERR("Getting samplerate failed");
282 samplerate = g_variant_get_uint64(gvar);
283 g_variant_unref(gvar);
284 if (srd_session_metadata_set(sess, SRD_CONF_SAMPLERATE,
285 g_variant_new_uint64(samplerate)) != SRD_OK) {
286 ERR("Setting samplerate failed");
289 if (srd_session_start(sess) != SRD_OK) {
290 ERR("Session start failed");
295 logic = packet->payload;
296 num_samples = logic->length / logic->unitsize;
297 DBG("Received SR_DF_LOGIC: %d samples", num_samples);
298 srd_session_send(sess, samplecnt, samplecnt + num_samples,
299 logic->data, logic->length);
300 samplecnt += logic->length / logic->unitsize;
303 DBG("Received SR_DF_END");
309 static int run_testcase(char *infile, GSList *pdlist, struct output *op)
311 struct srd_session *sess;
312 struct srd_decoder *dec;
313 struct srd_decoder_inst *di, *prev_di;
314 srd_pd_output_callback_t cb;
317 struct option *option;
319 GHashTable *probes, *opts;
322 char **decoder_class;
325 if ((op->outfd = open(op->outfile, O_CREAT|O_WRONLY, 0600)) == -1) {
326 ERR("Unable to open %s for writing: %s", op->outfile,
332 if (sr_session_load(infile) != SR_OK)
335 if (srd_session_new(&sess) != SRD_OK)
337 sr_session_datafeed_callback_add(sr_cb, sess);
342 case SRD_OUTPUT_BINARY:
345 case SRD_OUTPUT_PYTHON:
351 srd_pd_output_callback_add(sess, op->type, cb, op);
355 for (pdl = pdlist; pdl; pdl = pdl->next) {
357 if (srd_decoder_load(pd->name) != SRD_OK)
360 /* Instantiate decoder and pass in options. */
361 opts = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
362 (GDestroyNotify)g_variant_unref);
363 for (l = pd->options; l; l = l->next) {
365 g_hash_table_insert(opts, option->key, option->value);
367 if (!(di = srd_inst_new(sess, pd->name, opts)))
369 g_hash_table_destroy(opts);
373 probes = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
374 (GDestroyNotify)g_variant_unref);
375 for (l = pd->probes; l; l = l->next) {
377 gvar = g_variant_new_int32(probe->probe);
378 g_variant_ref_sink(gvar);
379 g_hash_table_insert(probes, probe->name, gvar);
381 if (srd_inst_probe_set_all(di, probes) != SRD_OK)
383 g_hash_table_destroy(probes);
386 /* If this is not the first decoder in the list, stack it
387 * on top of the previous one. */
389 if (srd_inst_stack(sess, prev_di, di) != SRD_OK) {
390 ERR("Failed to stack decoder instances.");
397 /* Resolve top decoder's class index, so we can match. */
398 dec = srd_decoder_get_by_id(pd->name);
400 if (op->type == SRD_OUTPUT_ANN)
401 l = dec->annotations;
402 else if (op->type == SRD_OUTPUT_BINARY)
405 /* Only annotations and binary can have a class. */
409 decoder_class = l->data;
410 if (!strcmp(decoder_class[0], op->class)) {
417 if (op->class_idx == -1) {
418 ERR("Output class '%s' not found in decoder %s.",
419 op->class, pd->name);
422 DBG("Class %s index is %d", op->class, op->class_idx);
429 srd_session_destroy(sess);
437 int main(int argc, char **argv)
439 struct sr_context *ctx;
443 struct option *option;
446 char c, *opt_infile, **kv, **opstr;
448 op = malloc(sizeof(struct output));
458 while ((c = getopt(argc, argv, "dP:p:o:i:O:f:S")) != -1) {
464 pd = g_malloc(sizeof(struct pd));
465 pd->name = g_strdup(optarg);
466 pd->probes = pd->options = NULL;
467 pdlist = g_slist_append(pdlist, pd);
471 if (g_slist_length(pdlist) == 0) {
472 /* No previous -P. */
473 ERR("Syntax error at '%s'", optarg);
476 kv = g_strsplit(optarg, "=", 0);
477 if (!kv[0] || (!kv[1] || kv[2])) {
479 ERR("Syntax error at '%s'", optarg);
484 probe = malloc(sizeof(struct probe));
485 probe->name = g_strdup(kv[0]);
486 probe->probe = strtoul(kv[1], 0, 10);
487 /* Apply to last PD. */
488 pd->probes = g_slist_append(pd->probes, probe);
490 option = malloc(sizeof(struct option));
491 option->key = g_strdup(kv[0]);
492 option->value = g_strdup(kv[1]);
493 /* Apply to last PD. */
494 pd->options = g_slist_append(pd->options, option);
501 opstr = g_strsplit(optarg, ":", 0);
502 if (!opstr[0] || !opstr[1]) {
503 /* Need at least abc:def. */
504 ERR("Syntax error at '%s'", optarg);
508 op->pd = g_strdup(opstr[0]);
509 if (!strcmp(opstr[1], "annotation"))
510 op->type = SRD_OUTPUT_ANN;
511 else if (!strcmp(opstr[1], "binary"))
512 op->type = SRD_OUTPUT_BINARY;
513 else if (!strcmp(opstr[1], "python"))
514 op->type = SRD_OUTPUT_PYTHON;
516 ERR("Unknown output type '%s'", opstr[1]);
521 op->class = g_strdup(opstr[2]);
525 op->outfile = g_strdup(optarg);
537 if (g_slist_length(pdlist) == 0)
541 if (!op->pd || op->type == -1)
544 sr_log_callback_set(sr_log, NULL);
545 if (sr_init(&ctx) != SR_OK)
548 srd_log_callback_set(srd_log, NULL);
549 if (srd_init(DECODERS_DIR) != SRD_OK)
553 if (!run_testcase(opt_infile, pdlist, op))