diff options
Diffstat (limited to 'src/bin2tts.c')
-rw-r--r-- | src/bin2tts.c | 159 |
1 files changed, 159 insertions, 0 deletions
diff --git a/src/bin2tts.c b/src/bin2tts.c new file mode 100644 index 0000000..dc04620 --- /dev/null +++ b/src/bin2tts.c @@ -0,0 +1,159 @@ +/* + * bin2tts.c + * + * Convert binary file to TTS (transisition/timestamp) format. + * + * Copyright (C) 2016 Daniel Thompson <daniel.thompson@linaro.org> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + */ + +#include <errno.h> +#include <getopt.h> +#include <stdbool.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +struct ctx { + int bits; + int sample_rate; + unsigned int timeout; +}; + +void timestamp(struct ctx *ctx, FILE *f) +{ + size_t sz = (ctx->bits + 7) / 8; + uint64_t count = 0; + unsigned int timeout = 0; + bool hot = false; + void *p[2]; + + p[0] = malloc(2 * sz); + p[1] = (char *) p[0] + sz; + + printf("["); + + while (!feof(f)) { + if (1 != fread(p[hot], ctx->bits / 8, 1, f)) + break; + + if (0 != memcmp(p[hot], p[!hot], sz)) { + printf("%5d.%09d] 0x%02x\n[", + (int)(count / ctx->sample_rate), + (int)(((count % ctx->sample_rate) * 1000000000) / + ctx->sample_rate), + *((unsigned char *)p[hot])); + timeout = ctx->timeout; + } + + if (timeout-- == 0) { + printf("%5d.%09d] 0x%02x\n[", + (int)(count / ctx->sample_rate), + (int)(((count % ctx->sample_rate) * 1000000000) / + ctx->sample_rate), + *((unsigned char *)p[hot])); + fflush(stdout); + + } + + hot = !hot; + count++; + } + + free(p[0]); +} + +void show_usage(bool full) +{ + FILE *f = full ? stdout : stderr; + + fprintf(f, "Usage: bin2tts [OPTION]... [FILE]...\n"); + + if (!full) { + fprintf(f, "Try --help for more information.\n"); + return; + } + + fprintf(f, +"\n" +"Mandatory arguments to long options are mandatory for short options too.\n" +" -b, --bits=BITS number of bits in each data sample\n" +" -h, --help show this help, then exit\n" +" -r, --sample-rate=X nominal sample rate of incoming data\n" +" -t, --timeout=C issue an extra 'timeout' packet after C samples\n" + ); +} + +int main(int argc, char *argv[]) +{ + int c; + struct ctx ctx = { + .bits = 8, + .sample_rate = 1000000, + .timeout = -1 + }; + + const struct option opts[] = { + { "bits", required_argument, 0, 'b' }, + { "help", no_argument, 0, 'h' }, + { "sample-rate", required_argument, 0, 'r' }, + { "timeout", required_argument, 0, 't' }, + { 0 }, + }; + + while ((c = getopt_long(argc, argv, "b:r:ht:", opts, NULL)) != -1) { + switch (c) { + case 'b': + ctx.bits = strtol(optarg, NULL, 0); + break; + case 'h': + show_usage(true); + return 0; + case 'r': + ctx.sample_rate = strtol(optarg, NULL, 0); + break; + case 't': + ctx.timeout = strtol(optarg, NULL, 0); + break; + default: + fprintf(stderr, "Try --help for more information.\n"); + return 1; + } + } + + /* + * If we don't have a timeout set then we need to force line + * buffering. This tool is often used in pipelines and + * terminated by ^C. Line buffering will reduce performance + * slightly. To avoid this penalty set a timeout and the + * buffers will be flushed whenever the timeout expires. + */ + if (ctx.timeout == -1) + setvbuf(stdout, NULL, _IOLBF, 0); + + if (optind >= argc) { + timestamp(&ctx, stdin); + } else while (optind < argc) { + FILE *f = fopen(argv[optind], "rb"); + + if (!f) { + fprintf(stderr, "Cannot open '%s' (%s)\n", argv[optind], + strerror(errno)); + return 10; + } + + timestamp(&ctx, f); + + fclose(f); + optind++; + } + + return 0; +} + |