aboutsummaryrefslogtreecommitdiff
path: root/src/bin2tts.c
blob: dc04620c9284bb2df9c8224b67aeef06a6e7b2c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
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;
}