forked from TLeconte/vdlm2dec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
outacars.c
235 lines (198 loc) · 5.47 KB
/
outacars.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
/*
* Copyright (c) 2016 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.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <time.h>
#include <netdb.h>
#include "vdlm2.h"
#include "crc.h"
#include "acars.h"
#include "cJSON.h"
extern int verbose;
extern char *idstation;
extern int jsonout;
extern char* jsonbuf;
extern int sockfd;
extern void outjson(void);
extern int DecodeLabel(acarsmsg_t *msg,oooi_t *oooi);
static void printmsg(const acarsmsg_t * msg)
{
fprintf(logfd, "ACARS\n");
if (msg->mode < 0x5d) {
fprintf(logfd, "Aircraft reg: %s ", msg->addr);
fprintf(logfd, "Flight id: %s", msg->fid);
fprintf(logfd, "\n");
}
fprintf(logfd, "Mode: %1c ", msg->mode);
fprintf(logfd, "Msg. label: %s\n", msg->label);
fprintf(logfd, "Block id: %c ", msg->bid);
fprintf(logfd, "Ack: %c\n", msg->ack);
fprintf(logfd, "Msg. no: %s\n", msg->no);
if(msg->txt[0]) fprintf(logfd, "Message :\n%s\n", msg->txt);
if (msg->be == 0x17)
fprintf(logfd, "Block End\n");
fflush(logfd);
}
static int buildjson(unsigned int vaddr,acarsmsg_t * msg, int chn, int freqb, struct timeval tv)
{
oooi_t oooi;
cJSON *json_obj;
int ok = 0;
char convert_tmp[8];
#if defined (WITH_RTL) || defined (WITH_AIR)
float freq = freqb / 1000000.0;
#else
float freq = 0;
#endif
json_obj = cJSON_CreateObject();
if (json_obj == NULL)
return ok;
double t = (double)tv.tv_sec + ((double)tv.tv_usec)/1e6;
cJSON_AddNumberToObject(json_obj, "timestamp", t);
cJSON_AddStringToObject(json_obj, "station_id", idstation);
cJSON_AddNumberToObject(json_obj, "channel", chn);
snprintf(convert_tmp, sizeof(convert_tmp), "%3.3f", freq);
cJSON_AddRawToObject(json_obj, "freq", convert_tmp);
cJSON_AddNumberToObject(json_obj, "icao", vaddr & 0xffffff);
snprintf(convert_tmp, sizeof(convert_tmp), "%c", msg->mode);
cJSON_AddStringToObject(json_obj, "mode", convert_tmp);
cJSON_AddStringToObject(json_obj, "label", msg->label);
if(msg->bid) {
snprintf(convert_tmp, sizeof(convert_tmp), "%c", msg->bid);
cJSON_AddStringToObject(json_obj, "block_id", convert_tmp);
if(msg->ack == 0x15) {
cJSON_AddFalseToObject(json_obj, "ack");
} else {
snprintf(convert_tmp, sizeof(convert_tmp), "%c", msg->ack);
cJSON_AddStringToObject(json_obj, "ack", convert_tmp);
}
cJSON_AddStringToObject(json_obj, "tail", msg->addr);
if(msg->mode <= 'Z') {
cJSON_AddStringToObject(json_obj, "flight", msg->fid);
cJSON_AddStringToObject(json_obj, "msgno", msg->no);
}
}
if(msg->txt[0])
cJSON_AddStringToObject(json_obj, "text", msg->txt);
if (msg->be == 0x17)
cJSON_AddTrueToObject(json_obj, "end");
if(DecodeLabel(msg, &oooi)) {
if(oooi.sa[0])
cJSON_AddStringToObject(json_obj, "depa", oooi.sa);
if(oooi.da[0])
cJSON_AddStringToObject(json_obj, "dsta", oooi.da);
if(oooi.eta[0])
cJSON_AddStringToObject(json_obj, "eta", oooi.eta);
if(oooi.gout[0])
cJSON_AddStringToObject(json_obj, "gtout", oooi.gout);
if(oooi.gin[0])
cJSON_AddStringToObject(json_obj, "gtin", oooi.gin);
if(oooi.woff[0])
cJSON_AddStringToObject(json_obj, "wloff", oooi.woff);
if(oooi.won[0])
cJSON_AddStringToObject(json_obj, "wlin", oooi.won);
}
ok = cJSON_PrintPreallocated(json_obj, jsonbuf, JSONBUFLEN, 0);
cJSON_Delete(json_obj);
return ok;
}
void outacars(unsigned int vaddr,msgblk_t * blk,unsigned char *txt, int len)
{
acarsmsg_t msg;
int i, k, j;
unsigned int crc;
int jok=0;
crc = 0;
/* test crc, set le and remove parity */
for (i = 0; i < len - 1; i++) {
update_crc(crc, txt[i]);
txt[i] &= 0x7f;
}
if (crc) {
fprintf(logfd, "crc error\n");
}
/* fill msg struct */
k = 0;
msg.mode = txt[k];
k++;
for (i = 0, j = 0; i < 7; i++, k++) {
if (txt[k] != '.') {
msg.addr[j] = txt[k];
j++;
}
}
msg.addr[j] = '\0';
/* ACK/NAK */
msg.ack = txt[k];
if (msg.ack == 0x15)
msg.ack = '!';
k++;
msg.label[0] = txt[k];
k++;
msg.label[1] = txt[k];
if (msg.label[1] == 0x7f)
msg.label[1] = 'd';
k++;
msg.label[2] = '\0';
msg.bid = txt[k];
if (msg.bid == 0)
msg.bid = ' ';
k++;
/* txt start */
msg.bs = txt[k];
k++;
msg.no[0] = '\0';
msg.fid[0] = '\0';
msg.txt[0] = '\0';
if (msg.bs != 0x03) {
if (msg.mode <= 'Z' && msg.bid <= '9') {
/* message no */
for (i = 0; i < 4 && k < len - 4; i++, k++) {
msg.no[i] = txt[k];
}
msg.no[i] = '\0';
/* Flight id */
for (i = 0; i < 6 && k < len - 4; i++, k++) {
msg.fid[i] = txt[k];
}
msg.fid[i] = '\0';
}
/* Message txt */
for (i = 0; (k < len - 4); i++, k++)
msg.txt[i] = txt[k];
msg.txt[i] = 0;
}
msg.be = txt[k];
if (verbose) {
printmsg(&msg);
}
// build the JSON buffer if needed
if(jsonbuf)
jok=buildjson(vaddr, &msg, blk->chn, blk->Fr, blk->tv);
if(jsonout && jok) {
fprintf(logfd, "%s\n", jsonbuf);
fflush(logfd);
}
if ((sockfd > 0) && jok) {
outjson();
}
}