aboutsummaryrefslogtreecommitdiffstats
path: root/adsb_gen.c
blob: 93b363f6699bc51f70bc4130ef5a574c389b5239 (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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/* (C) 2014 by Harald Welte <laforge@gnumonks.org>
 * All Rights Reserved
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <stdint.h>
#include <fcntl.h>

#include <sys/types.h>
#include <sys/stat.h>

#include <osmocom/core/bits.h>
#include <osmocom/core/utils.h>

#define PREAMBLE_LEN	8
#define	SHORT_LEN	56
#define LONG_LEN	112

#define OUTBUF_SIZE	((LONG_LEN+PREAMBLE_LEN)*2)

#define SPACE	0xff

static const ubit_t preamble[PREAMBLE_LEN] = { 1, 1, SPACE, 0, 0, SPACE, SPACE, SPACE };

/* encode Pulse Position Modulation */
static int ppm_encode(ubit_t *out, const ubit_t *in, unsigned int num_in)
{
	int i;
	int j = 0;

	for (i = 0; i < num_in; i++) {
		switch (in[i]) {
		case 0:
			out[j++] = 0;
			out[j++] = 1;
			break;
		case 1:
			out[j++] = 1;
			out[j++] = 0;
			break;
		case SPACE:
			out[j++] = 0;
			out[j++] = 0;
			break;
		default:
			return -1;
		}
	}
	return 0;
}

int modes_encode_from_bin(ubit_t *out, const pbit_t *bin, unsigned int num_bits)
{
	ubit_t bitbuf[PREAMBLE_LEN+LONG_LEN];
	int rc, out_len;

	printf("from_bin(num_bits=%u)\n", num_bits);

	/* assemble the un-encoded bits of the entire message */
	memcpy(bitbuf, preamble, sizeof(preamble));
	rc = osmo_pbit2ubit(bitbuf+sizeof(preamble), bin, num_bits);

	printf("sym=%s\n", osmo_ubit_dump(bitbuf, num_bits+sizeof(preamble)));

	/* encode using pulse-position modulation */
	rc = ppm_encode(out, bitbuf, num_bits+sizeof(preamble));
	if (rc < 0) {
		fprintf(stderr, "Error during ppm_encode\n");
		return rc;
	}

	out_len = 2*(num_bits + sizeof(preamble));

	printf("out=%s\n", osmo_ubit_dump(out, out_len));

	return out_len;
}

int modes_encode_from_ascii(ubit_t *out, const char *in)
{
	char ascbuf[LONG_LEN*2];
	ubit_t binbuf[LONG_LEN];
	int str_len = strlen(in);
	int num_bytes = (str_len - 2) / 2;
	int rc;

	/* sanity checking */
	if (str_len > LONG_LEN*2 + 2) {
		fprintf(stderr, "String too long!\n");
		return -1;
	}

	if (in[0] != '*' || in[strlen(in)-1] != ';') {
		fprintf(stderr, "string `%s' not in correct format\n", in);
		return -2;
	}

	/* copy string, skipping heading '*' and trailing ';' */
	memset(ascbuf, 0, sizeof(ascbuf));
	strncpy(ascbuf, in+1, str_len-2);

	/* convert from hex to binary */
	rc = osmo_hexparse(ascbuf, binbuf, sizeof(binbuf));
	if (rc < 0) {
		fprintf(stderr, "error during hexparse of `%s'\n", ascbuf);
		return rc;
	}

	/* encode the actual binary */
	return modes_encode_from_bin(out, binbuf, num_bytes*8);
}

static int outfd;

/* write a frame to the output file */
static int put_frame(const ubit_t *frame, unsigned int num_bits)
{
	return write(outfd, frame, num_bits);
}

/* write a pause to the output file (given duration in usec) */
static int put_pause(unsigned int usec)
{
	uint8_t buf[100000];
	unsigned int i, num_samples = usec * 2;
	unsigned int written = 0;

	memset(buf, 0, sizeof(buf));

	for (i = 0; i < num_samples; i += written) {
		unsigned int pending = num_samples - i;
		int rc;

		written = pending;
		if (written > sizeof(buf))
			written = sizeof(buf);

		rc = write(outfd, buf, written);
		if (rc < written)
			return -1;
	}

	return 0;
}

int main(int argc, char **argv)
{
	int rc;
	char line[255];
	uint8_t outbuf[OUTBUF_SIZE];
	char *infname, *outfname;
	FILE *infile;
	int opt, pause_usec = 1000000;

	while ((opt = getopt(argc, argv, "p:")) != -1) {
		switch (opt) {
		case 'p':
			pause_usec = atoi(optarg);
			break;
		default:
			exit(2);
		}
	}

	if (argc <= optind+1) {
		fprintf(stderr, "Usage: %s [-p pause_usec] <infname> <outfname>\n", argv[0]);
		exit(2);
	}

	infname = argv[optind];
	outfname = argv[optind+1];

	infile = fopen(infname, "r");
	if (!infile)
		exit(2);

	outfd = open(outfname, O_CREAT|O_WRONLY|O_TRUNC, 0660);
	if (outfd < 0)
		exit(2);

	while (fgets(line, sizeof(line), infile)) {
		if (!strlen(line))
			continue;
		if (line[strlen(line)-1] == '\n')
			line[strlen(line)-1] = '\0';
		printf("IN: %s\n", line);

		rc = modes_encode_from_ascii(outbuf, line);
		if (rc < 0)
			exit(-rc);
		put_frame(outbuf, rc);

		put_pause(pause_usec);
	}

	fclose(infile);
	close(outfd);

	exit(0);
}