-
Notifications
You must be signed in to change notification settings - Fork 89
/
acars.c
388 lines (337 loc) · 7.33 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
/*
* Copyright (c) 2015 Thierry Leconte
*
*
* 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 "acarsdec.h"
#define SYN 0x16
#define SOH 0x01
#define STX 0x02
#define ETX 0x83
#define ETB 0x97
#define DLE 0x7f
/* message queue */
static pthread_mutex_t blkq_mtx;
static pthread_cond_t blkq_wcd;
static msgblk_t *blkq_s,*blkq_e;
static pthread_t blkth_id;
static int acars_shutdown;
#include "syndrom.h"
static int fixprerr(msgblk_t * blk, const unsigned short crc, int *pr, int 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->len - *pr + 1)], pr + 1, pn - 1)) {
blk->txt[*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 unsigned short 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->len ; k++) {
int bo=8*(blk->len-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[k] ^= (1 << i);
blk->txt[k] ^= (1 << j);
return 1;
}
}
}
return 0;
}
#define MAXPERR 3
static void *blk_thread(void *arg)
{
do {
msgblk_t *blk;
int i, pn;
unsigned short crc;
int pr[MAXPERR];
if (verbose)
fprintf(stderr, "blk_starting\n");
/* get a message */
pthread_mutex_lock(&blkq_mtx);
while ((blkq_e == NULL) && !acars_shutdown)
pthread_cond_wait(&blkq_wcd, &blkq_mtx);
if (acars_shutdown) {
pthread_mutex_unlock(&blkq_mtx);
break;
}
blk = blkq_e;
blkq_e = blk->prev;
if (blkq_e == NULL)
blkq_s = NULL;
pthread_mutex_unlock(&blkq_mtx);
if (verbose)
fprintf(stderr, "get message #%d\n", blk->chn + 1);
/* handle message */
if (blk->len < 13) {
if (verbose)
fprintf(stderr, "#%d too short\n", blk->chn + 1);
free(blk);
continue;
}
/* force STX/ETX */
blk->txt[12] &= (ETX | STX);
blk->txt[12] |= (ETX & STX);
/* parity check */
pn = 0;
for (i = 0; i < blk->len; i++) {
if ((numbits[(unsigned char)(blk->txt[i])] & 1) == 0) {
if (pn < MAXPERR) {
pr[pn] = i;
}
pn++;
}
}
if (pn > MAXPERR) {
if (verbose)
fprintf(stderr,
"#%d too many parity errors: %d\n",
blk->chn + 1, pn);
free(blk);
continue;
}
if (pn > 0 && verbose)
fprintf(stderr, "#%d parity error(s): %d\n",
blk->chn + 1, pn);
blk->err = pn;
/* crc check */
crc = 0;
for (i = 0; i < blk->len; i++) {
update_crc(crc, blk->txt[i]);
}
update_crc(crc, blk->crc[0]);
update_crc(crc, blk->crc[1]);
if (crc && verbose)
fprintf(stderr, "#%d crc error\n", blk->chn + 1);
/* try to fix error */
if(pn) {
if (fixprerr(blk, crc, pr, pn) == 0) {
if (verbose)
fprintf(stderr, "#%d not able to fix errors\n", blk->chn + 1);
free(blk);
continue;
}
if (verbose)
fprintf(stderr, "#%d errors fixed\n", blk->chn + 1);
} else {
if (crc) {
if(fixdberr(blk, crc) == 0) {
if (verbose)
fprintf(stderr, "#%d not able to fix errors\n", blk->chn + 1);
free(blk);
continue;
}
if (verbose)
fprintf(stderr, "#%d errors fixed\n", blk->chn + 1);
}
}
/* redo parity checking and removing */
pn = 0;
for (i = 0; i < blk->len; i++) {
if ((numbits[(unsigned char)(blk->txt[i])] & 1) == 0) {
pn++;
}
blk->txt[i] &= 0x7f;
}
if (pn) {
fprintf(stderr, "#%d parity check problem\n",
blk->chn + 1);
free(blk);
continue;
}
outputmsg(blk);
free(blk);
} while (1);
return NULL;
}
int initAcars(channel_t * ch)
{
if(ch->chn==0) {
/* init global message queue */
pthread_mutex_init(&blkq_mtx, NULL);
pthread_cond_init(&blkq_wcd, NULL);
blkq_e=blkq_s=NULL;
pthread_create(&blkth_id , NULL, blk_thread, NULL);
acars_shutdown = 0;
}
ch->outbits = 0;
ch->nbits = 8;
ch->Acarsstate = WSYN;
ch->blk = NULL;
return 0;
}
static void resetAcars(channel_t * ch)
{
ch->Acarsstate = WSYN;
ch->MskDf = 0;
ch->nbits = 1;
}
void decodeAcars(channel_t * ch)
{
unsigned char r = ch->outbits;
switch (ch->Acarsstate) {
case WSYN:
if (r == SYN) {
ch->Acarsstate = SYN2;
ch->nbits = 8;
return;
}
if (r == (unsigned char)~SYN) {
ch->MskS ^= 2;
ch->Acarsstate = SYN2;
ch->nbits = 8;
return;
}
ch->nbits = 1;
return;
case SYN2:
if (r == SYN) {
ch->Acarsstate = SOH1;
ch->nbits = 8;
return;
}
if (r == (unsigned char)~SYN) {
ch->MskS ^= 2;
ch->nbits = 8;
return;
}
resetAcars(ch);
return;
case SOH1:
if (r == SOH) {
if(ch->blk == NULL) {
ch->blk = malloc(sizeof(msgblk_t));
if(ch->blk == NULL) {
resetAcars(ch);
return;
}
}
gettimeofday(&(ch->blk->tv), NULL);
ch->Acarsstate = TXT;
ch->blk->chn = ch->chn;
ch->blk->len = 0;
ch->blk->err = 0;
ch->nbits = 8;
ch->MskLvlSum = 0;
ch->MskBitCount = 0;
return;
}
resetAcars(ch);
return;
case TXT:
ch->blk->txt[ch->blk->len] = r;
ch->blk->len++;
if ((numbits[(unsigned char)r] & 1) == 0) {
ch->blk->err++;
if (ch->blk->err > MAXPERR + 1) {
if (verbose)
fprintf(stderr,
"#%d too many parity errors\n",
ch->chn + 1);
resetAcars(ch);
return;
}
}
if (r == ETX || r == ETB) {
ch->Acarsstate = CRC1;
ch->nbits = 8;
return;
}
if (ch->blk->len > 20 && r == DLE) {
if (verbose)
fprintf(stderr, "#%d miss txt end\n",
ch->chn + 1);
ch->blk->len -= 3;
ch->blk->crc[0] = ch->blk->txt[ch->blk->len];
ch->blk->crc[1] = ch->blk->txt[ch->blk->len + 1];
ch->Acarsstate = CRC2;
goto putmsg_lbl;
}
if (ch->blk->len > 240) {
if (verbose)
fprintf(stderr, "#%d too long\n", ch->chn + 1);
resetAcars(ch);
return;
}
ch->nbits = 8;
return;
case CRC1:
ch->blk->crc[0] = r;
ch->Acarsstate = CRC2;
ch->nbits = 8;
return;
case CRC2:
ch->blk->crc[1] = r;
putmsg_lbl:
ch->blk->lvl = 10*log10(ch->MskLvlSum / ch->MskBitCount);
if (verbose)
fprintf(stderr, "put message #%d\n", ch->chn + 1);
pthread_mutex_lock(&blkq_mtx);
ch->blk->prev = NULL;
if (blkq_s)
blkq_s->prev = ch->blk;
blkq_s = ch->blk;
if (blkq_e == NULL)
blkq_e = blkq_s;
pthread_cond_signal(&blkq_wcd);
pthread_mutex_unlock(&blkq_mtx);
ch->blk=NULL;
ch->Acarsstate = END;
ch->nbits = 8;
return;
case END:
resetAcars(ch);
ch->nbits = 8;
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;
}