Debian Codegraph @

view1090.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 // view1090, a Mode S messages viewer for dump1090 devices.
2 //
3 // Copyright (C) 2014 by Malcolm Robb <[email protected]>
4 //
5 // All rights reserved.
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions are
9 // met:
10 //
11 // * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 //
14 // * Redistributions in binary form must reproduce the above copyright
15 // notice, this list of conditions and the following disclaimer in the
16 // documentation and/or other materials provided with the distribution.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 #include "dump1090.h"
•31
32 struct _Modes Modes;
33
34 //
35 // ============================= Utility functions ==========================
36 //
37 void sigintHandler(int dummy) {
38 MODES_NOTUSED(dummy);
39 signal(SIGINT, SIG_DFL); // reset signal handler - bit extra safety
40 Modes.exit = 1; // Signal to threads that we are done
41 }
42 //
43 // =============================== Terminal handling ========================
44 //
45 #ifndef _WIN32
46 // Get the number of rows after the terminal changes size.
47 int getTermRows() {
48 struct winsize w;
49 ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
50 return (w.ws_row);
51 }
52
53 // Handle resizing terminal
54 void sigWinchCallback() {
55 signal(SIGWINCH, SIG_IGN);
56 Modes.interactive_rows = getTermRows();
57 interactiveShowData();
58 signal(SIGWINCH, sigWinchCallback);
59 }
60 #else
61 int getTermRows() { return MODES_INTERACTIVE_ROWS;}
62 #endif
63 //
64 // =============================== Initialization ===========================
65 //
66 void view1090InitConfig(void) {
67 // Default everything to zero/NULL
68 memset(&Modes, 0, sizeof(Modes));
69
70 // Now initialise things that should not be 0/NULL to their defaults
71 Modes.check_crc = 1;
72 Modes.interactive_rows = getTermRows();
73 Modes.interactive_display_ttl = MODES_INTERACTIVE_DISPLAY_TTL;
74 Modes.interactive = 1;
75 Modes.maxRange = 1852 * 300; // 300NM default max range
76 }
77 //
78 //=========================================================================
79 //
80 void view1090Init(void) {
81
82 pthread_mutex_init(&Modes.data_mutex,NULL);
83 pthread_cond_init(&Modes.data_cond,NULL);
84
85 #ifdef _WIN32
86 if ( (!Modes.wsaData.wVersion)
87 && (!Modes.wsaData.wHighVersion) ) {
88 // Try to start the windows socket support
89 if (WSAStartup(MAKEWORD(2,1),&Modes.wsaData) != 0)
90 {
91 fprintf(stderr, "WSAStartup returned Error\n");
92 }
93 }
94 #endif
95
96 // Validate the users Lat/Lon home location inputs
97 if ( (Modes.fUserLat > 90.0) // Latitude must be -90 to +90
98 || (Modes.fUserLat < -90.0) // and
99 || (Modes.fUserLon > 360.0) // Longitude must be -180 to +360
100 || (Modes.fUserLon < -180.0) ) {
101 Modes.fUserLat = Modes.fUserLon = 0.0;
102 } else if (Modes.fUserLon > 180.0) { // If Longitude is +180 to +360, make it -180 to 0
103 Modes.fUserLon -= 360.0;
104 }
105 // If both Lat and Lon are 0.0 then the users location is either invalid/not-set, or (s)he's in the
106 // Atlantic ocean off the west coast of Africa. This is unlikely to be correct.
107 // Set the user LatLon valid flag only if either Lat or Lon are non zero. Note the Greenwich meridian
108 // is at 0.0 Lon,so we must check for either fLat or fLon being non zero not both.
109 // Testing the flag at runtime will be much quicker than ((fLon != 0.0) || (fLat != 0.0))
110 Modes.bUserFlags &= ~MODES_USER_LATLON_VALID;
111 if ((Modes.fUserLat != 0.0) || (Modes.fUserLon != 0.0)) {
112 Modes.bUserFlags |= MODES_USER_LATLON_VALID;
113 }
114
115 // Prepare error correction tables
116 modesChecksumInit(Modes.nfix_crc);
117 icaoFilterInit();
118 }
119
120 //
121 // ================================ Main ====================================
122 //
123 void showHelp(void) {
124 printf(
125 "-----------------------------------------------------------------------------\n"
126 "| view1090 ModeS Viewer %45s |\n"
127 "-----------------------------------------------------------------------------\n"
128 "--no-interactive Disable interactive mode, print messages to stdout\n"
129 "--interactive-rows <num> Max number of rows in interactive mode (default: 15)\n"
130 "--interactive-ttl <sec> Remove from list if idle for <sec> (default: 60)\n"
131 "--interactive-rtl1090 Display flight table in RTL1090 format\n"
132 "--modeac Enable decoding of SSR modes 3/A & 3/C\n"
133 "--net-bo-ipaddr <IPv4> TCP Beast output listen IPv4 (default: 127.0.0.1)\n"
134 "--net-bo-port <port> TCP Beast output listen port (default: 30005)\n"
135 "--lat <latitude> Reference/receiver latitide for surface posn (opt)\n"
136 "--lon <longitude> Reference/receiver longitude for surface posn (opt)\n"
137 "--max-range <distance> Absolute maximum range for position decoding (in nm, default: 300)\n"
138 "--no-crc-check Disable messages with broken CRC (discouraged)\n"
139 "--no-fix Disable single-bits error correction using CRC\n"
140 "--fix Enable single-bits error correction using CRC\n"
141 "--aggressive More CPU for more messages (two bits fixes, ...)\n"
142 "--metric Use metric units (meters, km/h, ...)\n"
143 "--show-only <addr> Show only messages from the given ICAO on stdout\n"
144 "--help Show this help\n",
145 MODES_DUMP1090_VARIANT " " MODES_DUMP1090_VERSION
146 );
147 }
148
149 //
150 //=========================================================================
151 //
152 int main(int argc, char **argv) {
153 int j;
154 struct client *c;
155 struct net_service *s;
156 char *bo_connect_ipaddr = "127.0.0.1";
157 int bo_connect_port = 30005;
158
159 // Set sane defaults
160
161 view1090InitConfig();
162 signal(SIGINT, sigintHandler); // Define Ctrl/C handler (exit program)
163
164 // Parse the command line options
165 for (j = 1; j < argc; j++) {
166 int more = ((j + 1) < argc); // There are more arguments
167
168 if (!strcmp(argv[j],"--net-bo-port") && more) {
169 bo_connect_port = atoi(argv[++j]);
170 } else if (!strcmp(argv[j],"--net-bo-ipaddr") && more) {
171 bo_connect_ipaddr = argv[++j];
172 } else if (!strcmp(argv[j],"--modeac")) {
173 Modes.mode_ac = 1;
174 } else if (!strcmp(argv[j],"--interactive-rows") && more) {
175 Modes.interactive_rows = atoi(argv[++j]);
176 } else if (!strcmp(argv[j],"--no-interactive")) {
177 Modes.interactive = 0;
178 } else if (!strcmp(argv[j],"--show-only") && more) {
179 Modes.show_only = (uint32_t) strtoul(argv[++j], NULL, 16);
180 Modes.interactive = 0;
181 } else if (!strcmp(argv[j],"--interactive-ttl") && more) {
182 Modes.interactive_display_ttl = (uint64_t)(1000 * atof(argv[++j]));
183 } else if (!strcmp(argv[j],"--interactive-rtl1090")) {
184 Modes.interactive = 1;
185 Modes.interactive_rtl1090 = 1;
186 } else if (!strcmp(argv[j],"--lat") && more) {
187 Modes.fUserLat = atof(argv[++j]);
188 } else if (!strcmp(argv[j],"--lon") && more) {
189 Modes.fUserLon = atof(argv[++j]);
190 } else if (!strcmp(argv[j],"--metric")) {
191 Modes.metric = 1;
192 } else if (!strcmp(argv[j],"--no-crc-check")) {
193 Modes.check_crc = 0;
194 } else if (!strcmp(argv[j],"--fix")) {
195 Modes.nfix_crc = 1;
196 } else if (!strcmp(argv[j],"--no-fix")) {
197 Modes.nfix_crc = 0;
198 } else if (!strcmp(argv[j],"--aggressive")) {
199 Modes.nfix_crc = MODES_MAX_BITERRORS;
200 } else if (!strcmp(argv[j],"--max-range") && more) {
201 Modes.maxRange = atof(argv[++j]) * 1852.0; // convert to metres
202 } else if (!strcmp(argv[j],"--help")) {
203 showHelp();
204 exit(0);
205 } else {
206 fprintf(stderr, "Unknown or not enough arguments for option '%s'.\n\n", argv[j]);
207 showHelp();
208 exit(1);
209 }
210 }
211
212 #ifdef _WIN32
213 // Try to comply with the Copyright license conditions for binary distribution
214 if (!Modes.quiet) {showCopyright();}
215 #define MSG_DONTWAIT 0
216 #endif
217
218 #ifndef _WIN32
219 // Setup for SIGWINCH for handling lines
220 if (Modes.interactive) {signal(SIGWINCH, sigWinchCallback);}
221 #endif
222
223 // Initialization
224 view1090Init();
225 modesInitNet();
226
227 // Try to connect to the selected ip address and port. We only support *ONE* input connection which we initiate.here.
228 s = makeBeastInputService();
229 c = serviceConnect(s, bo_connect_ipaddr, bo_connect_port);
230 if (!c) {
231 fprintf(stderr, "Failed to connect to %s:%d: %s\n", bo_connect_ipaddr, bo_connect_port, Modes.aneterr);
232 exit(1);
233 }
234
235 // Keep going till the user does something that stops us
236 while (!Modes.exit) {
237 icaoFilterExpire();
238 trackPeriodicUpdate();
239 modesNetPeriodicWork();
240
241 if (Modes.interactive)
242 interactiveShowData();
243
244 if (s->connections == 0) {
245 // lost input connection, try to reconnect
246 usleep(1000000);
247 c = serviceConnect(s, bo_connect_ipaddr, bo_connect_port);
248 continue;
249 }
250
251 usleep(100000);
252 }
253
254 return (0);
255 }
256 //
257 //=========================================================================
258 //