forked from airnavsystems/rbfeeder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
airnav_mlat.c
executable file
·229 lines (190 loc) · 5.67 KB
/
airnav_mlat.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
/*
* Copyright (c) 2020 - AirNav Systems
*
* https://www.radarbox.com
*
* More info: https://github.com/AirNav-Systems/rbfeeder
*
*/
#include "rbfeeder.h"
#include "airnav_mlat.h"
pid_t p_mlat;
char *mlat_cmd;
char *mlat_server;
char *mlat_pidfile;
int autostart_mlat;
char *mlat_config;
/*
* Check if MLAT is running
*/
int mlat_checkMLATRunning(void) {
if (p_mlat <= 0) {
return 0;
}
if (kill(p_mlat, 0) == 0) {
return 1;
} else {
p_mlat = 0;
return 0;
}
}
/*
* Start MLAT, if not running
*/
void mlat_startMLAT(void) {
if (mlat_checkMLATRunning() != 0) {
airnav_log_level(3, "Looks like MLAT is already running.\n");
return;
}
if (mlat_cmd == NULL) {
airnav_log_level(3, "MLAT command line not defined.\n");
return;
}
if (g_lat != 0 && g_lon != 0 && g_alt != -999 && sn != NULL) {
char *tmp_cmd = malloc(300);
memset(tmp_cmd, 0, 300);
sprintf(tmp_cmd, "%s --input-type dump1090 --input-connect 127.0.0.1:%s --server %s --lat %f --lon %f --alt %d --user %s --results beast,connect,127.0.0.1:%s", mlat_cmd, beast_out_port, mlat_server, g_lat, g_lon, g_alt, sn, beast_in_port);
airnav_log_level(3, "Starting MLAT with this command: '%s'\n", tmp_cmd);
p_mlat = run_cmd3(tmp_cmd);
free(tmp_cmd);
sleep(3);
if (mlat_checkMLATRunning() != 0) {
airnav_log_level(3, "Ok, MLAT started! Pid is: %i\n", p_mlat);
net_sendStats();
} else {
airnav_log_level(3, "Error starting MLAT\n");
net_sendStats();
}
} else {
airnav_log_level(2, "Can't start MLAT. Missing parameters.\n");
}
return;
}
/*
* Stop MLAT
*/
void mlat_stopMLAT(void) {
if (mlat_checkMLATRunning() == 0) {
airnav_log_level(3, "MLAT is not running.\n");
return;
}
if (kill(p_mlat, SIGTERM) == 0) {
airnav_log_level(3, "Succesfully stopped MLAT!\n");
sleep(2);
net_sendStats();
return;
} else {
airnav_log_level(3, "Error stopping MLAT.\n");
return;
}
return;
}
/*
* Stop and start VHF, if running
*/
void mlat_restartMLAT() {
if (mlat_checkMLATRunning() == 1) {
mlat_stopMLAT();
sleep(3);
mlat_startMLAT();
} else {
mlat_startMLAT();
}
}
/*
* Send MLAT configuration to server
*/
void mlat_sendMLATConfig(void) {
}
/*
* Save MLAT configuration to file
*/
int mlat_saveMLATConfig(void) {
char *template = "\n# MLAT - RadarBoxPi\n"
"\n"
"START_CLIENT=\"yes\"\n"
"RUN_AS_USER=\"mlat\"\n"
"SERVER_USER=\"%s\"\n"
"LOGFILE=\"/var/log/mlat-client-config-rb.log\"\n"
"INPUT_TYPE=\"dump1090\"\n"
"INPUT_HOSTPORT=\"127.0.0.1:%d\"\n"
"SERVER_HOSTPORT=\"%s\"\n"
"LAT=\"%f\"\n"
"LON=\"%f\"\n"
"ALT=\"%d\"\n"
"RESULTS=\"beast,connect,127.0.0.1:%s\"\n"
"EXTRA_ARGS=\"\"\n"
"\n";
if (g_lat != 0 && g_lon != 0 && g_alt != -999 && sn != NULL && mlat_config != NULL) {
FILE *f = fopen(mlat_config, "w");
if (f == NULL) {
airnav_log("Cannot write MLAT Config file (%s): %s\n", mlat_config, strerror(errno));
return 0;
} else {
fprintf(f, template, sn, beast_out_port, mlat_server, g_lat, g_lon, g_alt, beast_in_port);
fclose(f);
return 1;
}
}
return 0;
}
// check if this aircraft is mlat or not
int mlat_check_is_mlat(struct aircraft *a) {
if (a->callsign_valid.source == SOURCE_MLAT)
return 1;
if (a->altitude_baro_valid.source == SOURCE_MLAT)
return 1;
if (a->altitude_geom_valid.source == SOURCE_MLAT)
return 1;
if (a->gs_valid.source == SOURCE_MLAT)
return 1;
if (a->ias_valid.source == SOURCE_MLAT)
return 1;
if (a->tas_valid.source == SOURCE_MLAT)
return 1;
if (a->mach_valid.source == SOURCE_MLAT)
return 1;
if (a->track_valid.source == SOURCE_MLAT)
return 1;
if (a->track_rate_valid.source == SOURCE_MLAT)
return 1;
if (a->roll_valid.source == SOURCE_MLAT)
return 1;
if (a->mag_heading_valid.source == SOURCE_MLAT)
return 1;
if (a->true_heading_valid.source == SOURCE_MLAT)
return 1;
if (a->baro_rate_valid.source == SOURCE_MLAT)
return 1;
if (a->geom_rate_valid.source == SOURCE_MLAT)
return 1;
if (a->squawk_valid.source == SOURCE_MLAT)
return 1;
if (a->emergency_valid.source == SOURCE_MLAT)
return 1;
if (a->nav_qnh_valid.source == SOURCE_MLAT)
return 1;
if (a->nav_altitude_mcp_valid.source == SOURCE_MLAT)
return 1;
if (a->nav_altitude_fms_valid.source == SOURCE_MLAT)
return 1;
if (a->nav_heading_valid.source == SOURCE_MLAT)
return 1;
if (a->nav_modes_valid.source == SOURCE_MLAT)
return 1;
if (a->position_valid.source == SOURCE_MLAT)
return 1;
if (a->nic_baro_valid.source == SOURCE_MLAT)
return 1;
if (a->nac_p_valid.source == SOURCE_MLAT)
return 1;
if (a->nac_v_valid.source == SOURCE_MLAT)
return 1;
if (a->sil_valid.source == SOURCE_MLAT)
return 1;
if (a->gva_valid.source == SOURCE_MLAT)
return 1;
if (a->sda_valid.source == SOURCE_MLAT)
return 1;
return 0;
}