forked from TLeconte/acarsdec
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathacars.c
418 lines (363 loc) · 10.1 KB
/
acars.c
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/*
* Copyright (c) 2015 Thierry Leconte
* Copyright (c) 2024 Thibaut VARENE
*
*
* This code is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License version 2
* published by the Free Software Foundation.
*
* 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 Library General Public License for more details.
*
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <pthread.h>
#include "acarsdec.h"
#include "output.h"
#include "statsd.h"
// ACARS is LSb first, 7-bit ASCII range. MSb is odd parity bit.
// include parity MSb
#define SYN 0x16
#define SOH 0x01
#define STX 0x02
#define ETX 0x83 // 0x03|0x80
#define ETB 0x97 // 0x17|0x80
#define DEL 0x7f
// mode + address + TA + lbl + BI + SoT + TXT + suffix + CRC + DEL
#define TXTMAXLEN (sizeof(struct txtdata_s))
// mode + address + TA + lbl + BI + SoT + (no txt+suffix / no crc / no DEL)
#define TXTMINLEN (sizeof(struct txtdata_s) - sizeof(((struct txtdata_s *)0)->text))
/* message queue */
static pthread_mutex_t blkq_mtx = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t blkq_wcd = PTHREAD_COND_INITIALIZER;
static msgblk_t *blkq_h = NULL;
static pthread_t blkth_id;
static int acars_shutdown = 1;
#include "syndrom.h"
static int fixprerr(msgblk_t *blk, const uint16_t crc, uint8_t *pr, uint8_t pn)
{
int i;
if (pn > 0) {
/* try to recursievly fix parity error */
for (i = 0; i < 8; i++) {
if (fixprerr(blk, crc ^ syndrom[i + 8 * (blk->txtlen - *pr + 1)], pr + 1, pn - 1)) {
blk->txt.raw[*pr] ^= (1 << i);
return 1;
}
}
return 0;
} else {
/* end of recursion : no more parity error */
if (crc == 0)
return 1;
/* test remainding error in crc */
for (i = 0; i < 2 * 8; i++)
if (syndrom[i] == crc) {
return 1;
}
return 0;
}
}
static int fixdberr(msgblk_t *blk, const uint16_t crc)
{
int i, j, k;
/* test remainding error in crc */
for (i = 0; i < 2 * 8; i++)
if (syndrom[i] == crc) {
return 1;
}
/* test double error in bytes */
for (k = 0; k < blk->txtlen; k++) {
int bo = 8 * (blk->txtlen - k + 1);
for (i = 0; i < 8; i++)
for (j = 0; j < 8; j++) {
if (i == j)
continue;
if ((crc ^ syndrom[i + bo] ^ syndrom[j + bo]) == 0) {
blk->txt.raw[k] ^= (1 << i);
blk->txt.raw[k] ^= (1 << j);
return 1;
}
}
}
return 0;
}
#define MAXPERR 3
static void *blk_thread(void *arg)
{
do {
msgblk_t *blk;
uint8_t i, pn, chn;
uint16_t crc;
uint8_t pr[MAXPERR];
vprerr("blk_starting\n");
/* pop a message */
pthread_mutex_lock(&blkq_mtx);
while ((blkq_h == NULL) && !acars_shutdown)
pthread_cond_wait(&blkq_wcd, &blkq_mtx);
if (acars_shutdown) {
pthread_mutex_unlock(&blkq_mtx);
break;
}
blk = blkq_h;
blkq_h = blk->prev;
pthread_mutex_unlock(&blkq_mtx);
chn = blk->chn;
vprerr("get message #%d\n", chn+1);
if (R.statsd)
statsd_inc_per_channel(chn, "decoder.msg.count");
/* handle message */
if (blk->txtlen < TXTMINLEN) {
vprerr("#%d too short: %d\n", chn+1, blk->txtlen);
if (R.statsd)
statsd_inc_per_channel(chn, "decoder.errors.too_short");
goto fail;
}
/* force STX/ETX */
blk->txt.d.sot &= (ETX | STX);
blk->txt.d.sot |= (ETX & STX);
/* parity check */
pn = 0;
for (i = 0; i < blk->txtlen; i++) {
if (parity8(blk->txt.raw[i]) == 0) {
if (pn < MAXPERR) {
pr[pn] = i;
}
pn++;
}
}
if (pn > MAXPERR) {
vprerr("#%d too many parity errors: %d\n", chn+1, pn);
if (R.statsd)
statsd_inc_per_channel(chn, "decoder.errors.parity_excess");
goto fail;
}
/* crc check */
crc = 0;
for (i = 0; i < blk->txtlen; i++)
crc = update_crc16(crc, blk->txt.raw[i]);
crc = update_crc16(crc, blk->crc[0]);
crc = update_crc16(crc, blk->crc[1]);
/* try to fix errors: parity or crc */
if (pn) {
vprerr("#%d parity error(s): %d\n", chn+1, pn);
if (fixprerr(blk, crc, pr, pn) == 0) {
vprerr("#%d not able to fix errors\n", chn+1);
goto fail;
}
vprerr("#%d errors fixed\n", chn+1);
}
else if (crc) {
vprerr("#%d crc error\n", chn+1);
if (R.statsd)
statsd_inc_per_channel(chn, "decoder.errors.crc");
if (fixdberr(blk, crc) == 0) {
vprerr("#%d not able to fix errors\n", chn+1);
goto fail;
}
vprerr("#%d errors fixed\n", chn+1);
}
blk->err = pn;
/* redo parity checking and removing */
bool pfail = 0;
for (i = 0; i < blk->txtlen; i++) { // vectorizable (in clang at least)
pfail |= !parity8(blk->txt.raw[i]);
blk->txt.raw[i] &= 0x7f;
}
if (pfail) {
vprerr("#%d parity check failed\n", chn+1);
goto fail;
}
if (R.statsd) {
char pfx[16];
statsd_metric_t metrics[] = {
{ .type = STATSD_UCOUNTER, .name = "decoder.msg.good", .value.u = 1 },
{ .type = STATSD_LGAUGE, .name = "decoder.msg.errs", .value.l = blk->err },
{ .type = STATSD_FGAUGE, .name = "decoder.msg.lvl", .value.f = blk->lvl },
{ .type = STATSD_LGAUGE, .name = "decoder.msg.len", .value.l = blk->txtlen },
};
// use the frequency if available, else the channel number
snprintf(pfx, sizeof(pfx), "%u.", R.channels[chn].Fr ? R.channels[chn].Fr : chn+1);
statsd_update(pfx, metrics, ARRAY_SIZE(metrics));
}
outputmsg(blk);
fail:
free(blk);
} while (1);
return NULL;
}
static void resetAcars(channel_t *ch)
{
ch->Acarsstate = PREKEY;
ch->MskDf = 0;
ch->nbits = 8;
ch->count = 0;
}
int initAcars(channel_t *ch)
{
if (acars_shutdown) {
acars_shutdown = 0;
pthread_create(&blkth_id, NULL, blk_thread, NULL);
}
resetAcars(ch);
ch->blk = NULL;
return 0;
}
void decodeAcars(channel_t *ch)
{
uint8_t r = ch->outbits;
//vprerr("#%d r: %x, count: %d, st: %d\n", ch->chn+1, r, ch->count, ch->Acarsstate);
ch->nbits = 8; // by default we'll read another byte next
switch (ch->Acarsstate) {
case PREKEY:
if (ch->count >= 12 && 0xFF != r) { // we have our first non-0xFF byte after a sequence - XXX REVISIT: expect at least 16: adjust count depending on how fast the MSK PLL locks
uint8_t q = ~r; // avoid type promotion in calling ffs(~r)
int l = ffs(q); // find the first (LSb) 0 in r
vprerr("#%d synced, count: %d, r: %x, fz: %d, lvl: %5.1f\n", ch->chn+1, ch->count, r, l, 10 * log10(ch->MskLvl));
ch->count = 0;
ch->Acarsstate = SYNC;
// after the 0xFF sequence we expect a possibly shifted '+'|0x80, aka 0xAB: 10101011
if (l < 3) {
/*
if the first zero is in position 1 or 2, assume we have already eaten into the '+', attempt sync on next symbol.
NB we could check if we could reconstruct a '+' by shifting left, but that's too much effort for too little gain ;P
3 possible cases: 0x.5 (0xAB>>1): shift 7 more bits; 0x.A (0xAB>>2): shift 6 more bits; else error.
can't check entire byte as high nibble contains bits from next one. error unhandled, will be caught by SYNC:
ch->nbits = ((r & 0xF) == 0x5) ? 7 : 6;
in fact, this can be rewritten without branch simply by considering the value of the first bit:
*/
ch->nbits = 6 | (r & 0x01); // 6 or 7
ch->count = 1; // skip '+', check '*'
}
else if (l > 3)
ch->nbits = l-3; // shift enough bits to leave 2 '1' before the first zero
else // it looks like we've stopped dead on '+', jump into next state
goto synced;
}
else { // otherwise eat bytes until we get a sequence of more than 16 consecutive 0xFF or 0x00
switch (r) {
case 0x00:
if (--ch->count <= -10) {
// we really are hearing 0xFF, only inverted
vprerr("#%d inverting polarity\n", ch->chn+1);
ch->MskS ^= 2; // inverted polarity
ch->count = -ch->count;
}
break;
case 0xFF:
ch->count++;
break;
default:
ch->count = 0;
break;
}
}
return;
case SYNC:
synced:
switch (ch->count) {
case 0: // expect '+' with parity bit set
if (unlikely(('+'|0x80) != r)) {
vprerr("#%d didn't get '+': %x\n", ch->chn+1, r);
goto fail;
}
break;
case 1: // expect '*'
if (unlikely('*' != r)) {
vprerr("#%d didn't get '*': %x\n", ch->chn+1, r);
goto fail;
}
break;
case 3: // expect SYN
ch->Acarsstate = SOH1;
// fallthrough
case 2: // expect SYN
if (unlikely(SYN != r)) {
vprerr("#%d didn't get SYN: %x\n", ch->chn+1, r);
goto fail;
}
break;
default: // cannot happen
goto fail;
}
ch->count++;
return;
case SOH1:
if (likely(r == SOH)) {
if (ch->blk == NULL) {
ch->blk = malloc(sizeof(*ch->blk));
if (unlikely(ch->blk == NULL)) {
perror(NULL);
break; // fail
}
}
gettimeofday(&(ch->blk->tv), NULL);
ch->Acarsstate = TXT;
ch->blk->chn = ch->chn;
ch->blk->txtlen = 0;
ch->blk->err = 0;
return;
}
vprerr("#%d didn't get SOH: %x\n", ch->chn+1, r);
break; // else fail
case TXT:
if (unlikely(ch->blk->txtlen > TXTMAXLEN)) {
vprerr("#%d too long\n", ch->chn + 1);
break; // fail
}
ch->blk->txt.raw[ch->blk->txtlen++] = r;
if (r == ETX || r == ETB) {
ch->Acarsstate = CRC1;
return;
}
if (unlikely(r == DEL && ch->blk->txtlen >= TXTMINLEN + 3)) {
vprerr("#%d missed txt end\n", ch->chn + 1);
ch->blk->txtlen -= 3;
ch->blk->crc[0] = ch->blk->txt.raw[ch->blk->txtlen];
ch->blk->crc[1] = ch->blk->txt.raw[ch->blk->txtlen + 1];
ch->Acarsstate = END;
goto putmsg_lbl;
}
return;
case CRC1:
ch->blk->crc[0] = r;
ch->Acarsstate = CRC2;
return;
case CRC2:
ch->blk->crc[1] = r;
ch->Acarsstate = END;
return;
case END:
if (unlikely(r != DEL))
vprerr("#%d didn't get DEL: %x\n", ch->chn+1, r); // ignored
putmsg_lbl:
ch->blk->lvl = 10 * log10(ch->MskLvl);
vprerr("put message #%d\n", ch->chn + 1);
pthread_mutex_lock(&blkq_mtx);
ch->blk->prev = blkq_h;
blkq_h = ch->blk;
pthread_cond_signal(&blkq_wcd);
pthread_mutex_unlock(&blkq_mtx);
ch->blk = NULL;
break; // reset
}
fail:
resetAcars(ch);
return;
}
int deinitAcars(void)
{
pthread_mutex_lock(&blkq_mtx);
acars_shutdown = 1;
pthread_mutex_unlock(&blkq_mtx);
pthread_cond_signal(&blkq_wcd);
pthread_join(blkth_id, NULL);
return 0;
}