forked from szpajder/dumpvdl2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
acars.c
156 lines (135 loc) · 3.76 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
/*
* dumpvdl2 - a VDL Mode 2 message decoder and protocol analyzer
*
* Copyright (c) 2017 Tomasz Lemiech <[email protected]>
*
* 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.
*
* 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <unistd.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "dumpvdl2.h"
#include "acars.h"
#define ETX 0x83
#define ETB 0x97
#define DEL 0x7f
/*
* ACARS message decoder
* Based on acarsdec by Thierry Leconte
*/
acars_msg_t *parse_acars(uint8_t *buf, uint32_t len, uint32_t *msg_type) {
static acars_msg_t *msg = NULL;
int i;
if(len < MIN_ACARS_LEN) {
debug_print("too short: %u < %u\n", len, MIN_ACARS_LEN);
return NULL;
}
if(buf[len-1] != DEL) {
debug_print("%02x: no DEL byte at end\n", buf[len-1]);
return NULL;
}
if(buf[len-4] != ETX && buf[len-4] != ETB) {
debug_print("%02x: no ETX/ETB byte at end\n", buf[len-4]);
return NULL;
}
len -= 4;
if(msg == NULL)
msg = XCALLOC(1, sizeof(acars_msg_t));
else
memset(msg, 0, sizeof(acars_msg_t));
// safe default
*msg_type |= MSGFLT_ACARS_NODATA;
for(i = 0; i < len; i++)
buf[i] &= 0x7f;
uint32_t k = 0;
msg->mode = buf[k++];
for (i = 0; i < 7; i++, k++) {
msg->reg[i] = buf[k];
}
msg->reg[7] = '\0';
/* ACK/NAK */
msg->ack = buf[k++];
if (msg->ack == 0x15)
msg->ack = '!';
msg->label[0] = buf[k++];
msg->label[1] = buf[k++];
if (msg->label[1] == 0x7f)
msg->label[1] = 'd';
msg->label[2] = '\0';
msg->bid = buf[k++];
if (msg->bid == 0)
msg->bid = ' ';
/* txt start */
msg->bs = buf[k++];
msg->no[0] = '\0';
msg->fid[0] = '\0';
msg->txt[0] = '\0';
if(k >= len) { // empty txt
msg->txt[0] = '\0';
return msg;
}
if (msg->bs != 0x03) {
if (msg->mode <= 'Z' && msg->bid <= '9') {
/* message no */
for (i = 0; i < 4 && k < len; i++, k++) {
msg->no[i] = buf[k];
}
msg->no[i] = '\0';
/* Flight id */
for (i = 0; i < 6 && k < len; i++, k++) {
msg->fid[i] = buf[k];
}
msg->fid[i] = '\0';
}
/* Message txt */
len -= k;
if(len > ACARSMSG_BUFSIZE) {
debug_print("message truncated to buffer size (%u > %u)", len, ACARSMSG_BUFSIZE);
len = ACARSMSG_BUFSIZE - 1; // leave space for terminating '\0'
}
if(len > 0) {
memcpy(msg->txt, buf + k, len);
*msg_type |= MSGFLT_ACARS_DATA;
*msg_type &= ~MSGFLT_ACARS_NODATA;
}
msg->txt[len] = '\0';
}
/* txt end */
return msg;
}
void output_acars_pp(const acars_msg_t *msg) {
char pkt[ACARSMSG_BUFSIZE+32];
char txt[ACARSMSG_BUFSIZE];
strcpy(txt, msg->txt);
for(char *ptr = txt; *ptr != 0; ptr++)
if (*ptr == '\n' || *ptr == '\r')
*ptr = ' ';
sprintf(pkt, "AC%1c %7s %1c %2s %1c %4s %6s %s",
msg->mode, msg->reg, msg->ack, msg->label, msg->bid, msg->no, msg->fid, txt);
if(write(pp_sockfd, pkt, strlen(pkt)) < 0)
debug_print("write(pp_sockfd) error: %s", strerror(errno));
}
void output_acars(const acars_msg_t *msg) {
fprintf(outf, "ACARS:\n");
if(msg->mode < 0x5d)
fprintf(outf, "Reg: %s Flight: %s\n", msg->reg, msg->fid);
fprintf(outf, "Mode: %1c Label: %s Blk id: %c Ack: %c Msg no.: %s\n",
msg->mode, msg->label, msg->bid, msg->ack, msg->no);
fprintf(outf, "Message:\n%s\n", msg->txt);
if(pp_sockfd > 0)
output_acars_pp(msg);
}