forked from TLeconte/acarsdec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
acars.c
363 lines (319 loc) · 6.88 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
/*
* 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
#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)
{
channel_t* ch = (channel_t*)arg;
do {
msgblk_t *blk;
int i, pn;
unsigned short crc;
int pr[MAXPERR];
pthread_mutex_lock(&ch->blkmtx);
while ((ch->blkq_e == NULL)&&!ch->acars_shutdown)
pthread_cond_wait(&ch->blkwcd, &ch->blkmtx);
if (ch->acars_shutdown) {
pthread_mutex_unlock(&ch->blkmtx);
break;
}
blk = ch->blkq_e;
ch->blkq_e = blk->prev;
if (ch->blkq_e == NULL)
ch->blkq_s = NULL;
pthread_mutex_unlock(&ch->blkmtx);
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[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[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)
{
ch->outbits = 0;
ch->nbits = 8;
ch->Acarsstate = WSYN;
ch->blkq_s = NULL;
ch->blkq_e = NULL;
ch->acars_shutdown = 0;
ch->blk = malloc(sizeof(msgblk_t));
ch->blk->chn = ch->chn;
pthread_mutex_init(&ch->blkmtx, NULL);
pthread_cond_init(&ch->blkwcd, NULL);
pthread_create(&ch->th, NULL, blk_thread, ch);
return 0;
}
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;
}
ch->Acarsstate = WSYN;
ch->MskDf = 0;
ch->nbits = 1;
return;
case SOH1:
if (r == SOH) {
time(&(ch->blk->tm));
ch->Acarsstate = TXT;
ch->blk->len = 0;
ch->blk->err = 0;
ch->nbits = 8;
ch->Msklvl = 0;
return;
}
ch->Acarsstate = WSYN;
ch->MskDf = 0;
ch->nbits = 1;
return;
case TXT:
ch->blk->txt[ch->blk->len] = r;
ch->blk->len++;
if ((numbits[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);
ch->Acarsstate = WSYN;
ch->MskDf = 0;
ch->nbits = 1;
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);
ch->Acarsstate = WSYN;
ch->MskDf = 0;
ch->nbits = 1;
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->Msklvl);
pthread_mutex_lock(&ch->blkmtx);
ch->blk->prev = NULL;
if (ch->blkq_s)
ch->blkq_s->prev = ch->blk;
ch->blkq_s = ch->blk;
if (ch->blkq_e == NULL)
ch->blkq_e = ch->blkq_s;
pthread_cond_signal(&ch->blkwcd);
pthread_mutex_unlock(&ch->blkmtx);
ch->blk = malloc(sizeof(msgblk_t));
ch->blk->chn = ch->chn;
ch->Acarsstate = END;
ch->nbits = 8;
return;
case END:
ch->Acarsstate = WSYN;
ch->MskDf = 0;
ch->nbits = 8;
return;
}
}
int deinitAcars(channel_t * ch)
{
pthread_mutex_lock(&ch->blkmtx);
ch->acars_shutdown = 1;
pthread_cond_signal(&ch->blkwcd);
pthread_mutex_unlock(&ch->blkmtx);
pthread_join(ch->th, NULL);
return 0;
}