| 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 |
// ============================= Utility functions ========================== |
| 33 |
// |
| 34 |
void sigintHandler(int dummy) { |
| 35 |
MODES_NOTUSED(dummy); |
| 36 |
signal(SIGINT, SIG_DFL); // reset signal handler - bit extra safety |
| 37 |
Modes.exit = 1; // Signal to threads that we are done |
| 38 |
} |
| 39 |
// |
| 40 |
// =============================== Terminal handling ======================== |
| 41 |
// |
| 42 |
#ifndef _WIN32 |
| 43 |
// Get the number of rows after the terminal changes size. |
| 44 |
int getTermRows() { |
| 45 |
struct winsize w; |
| 46 |
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w); |
| 47 |
return (w.ws_row); |
| 48 |
} |
| 49 |
|
| 50 |
// Handle resizing terminal |
| 51 |
void sigWinchCallback() { |
| 52 |
signal(SIGWINCH, SIG_IGN); |
| 53 |
Modes.interactive_rows = getTermRows(); |
| 54 |
interactiveShowData(); |
| 55 |
signal(SIGWINCH, sigWinchCallback); |
| 56 |
} |
| 57 |
#else |
| 58 |
int getTermRows() { return MODES_INTERACTIVE_ROWS;} |
| 59 |
#endif |
| 60 |
// |
| 61 |
// =============================== Initialization =========================== |
| 62 |
// |
| 63 |
void view1090InitConfig(void) { |
| 64 |
// Default everything to zero/NULL |
| 65 |
memset(&Modes, 0, sizeof(Modes)); |
| 66 |
|
| 67 |
// Now initialise things that should not be 0/NULL to their defaults |
| 68 |
Modes.check_crc = 1; |
| 69 |
Modes.interactive_rows = getTermRows(); |
| 70 |
Modes.interactive_display_ttl = MODES_INTERACTIVE_DISPLAY_TTL; |
| 71 |
Modes.interactive = 1; |
| 72 |
Modes.maxRange = 1852 * 300; // 300NM default max range |
| 73 |
} |
| 74 |
// |
| 75 |
//========================================================================= |
| 76 |
// |
| 77 |
void view1090Init(void) { |
| 78 |
|
| 79 |
pthread_mutex_init(&Modes.data_mutex,NULL); |
| 80 |
pthread_cond_init(&Modes.data_cond,NULL); |
| 81 |
|
| 82 |
#ifdef _WIN32 |
| 83 |
if ( (!Modes.wsaData.wVersion) |
| 84 |
&& (!Modes.wsaData.wHighVersion) ) { |
| 85 |
// Try to start the windows socket support |
| 86 |
if (WSAStartup(MAKEWORD(2,1),&Modes.wsaData) != 0) |
| 87 |
{ |
| 88 |
fprintf(stderr, "WSAStartup returned Error\n"); |
| 89 |
} |
| 90 |
} |
| 91 |
#endif |
| 92 |
|
| 93 |
// Validate the users Lat/Lon home location inputs |
| 94 |
if ( (Modes.fUserLat > 90.0) // Latitude must be -90 to +90 |
| 95 |
|| (Modes.fUserLat < -90.0) // and |
| 96 |
|| (Modes.fUserLon > 360.0) // Longitude must be -180 to +360 |
| 97 |
|| (Modes.fUserLon < -180.0) ) { |
| 98 |
Modes.fUserLat = Modes.fUserLon = 0.0; |
| 99 |
} else if (Modes.fUserLon > 180.0) { // If Longitude is +180 to +360, make it -180 to 0 |
| 100 |
Modes.fUserLon -= 360.0; |
| 101 |
} |
| 102 |
// If both Lat and Lon are 0.0 then the users location is either invalid/not-set, or (s)he's in the |
| 103 |
// Atlantic ocean off the west coast of Africa. This is unlikely to be correct. |
| 104 |
// Set the user LatLon valid flag only if either Lat or Lon are non zero. Note the Greenwich meridian |
| 105 |
// is at 0.0 Lon,so we must check for either fLat or fLon being non zero not both. |
| 106 |
// Testing the flag at runtime will be much quicker than ((fLon != 0.0) || (fLat != 0.0)) |
| 107 |
Modes.bUserFlags &= ~MODES_USER_LATLON_VALID; |
| 108 |
if ((Modes.fUserLat != 0.0) || (Modes.fUserLon != 0.0)) { |
| 109 |
Modes.bUserFlags |= MODES_USER_LATLON_VALID; |
| 110 |
} |
| 111 |
|
| 112 |
// Prepare error correction tables |
| 113 |
modesChecksumInit(Modes.nfix_crc); |
| 114 |
icaoFilterInit(); |
| 115 |
} |
| 116 |
|
| 117 |
// |
| 118 |
// ================================ Main ==================================== |
| 119 |
// |
| 120 |
void showHelp(void) { |
| 121 |
printf( |
| 122 |
"-----------------------------------------------------------------------------\n" |
| 123 |
"| view1090 ModeS Viewer %45s |\n" |
| 124 |
"-----------------------------------------------------------------------------\n" |
| 125 |
"--no-interactive Disable interactive mode, print messages to stdout\n" |
| 126 |
"--interactive-rows <num> Max number of rows in interactive mode (default: 15)\n" |
| 127 |
"--interactive-ttl <sec> Remove from list if idle for <sec> (default: 60)\n" |
| 128 |
"--interactive-rtl1090 Display flight table in RTL1090 format\n" |
| 129 |
"--modeac Enable decoding of SSR modes 3/A & 3/C\n" |
| 130 |
"--net-bo-ipaddr <IPv4> TCP Beast output listen IPv4 (default: 127.0.0.1)\n" |
| 131 |
"--net-bo-port <port> TCP Beast output listen port (default: 30005)\n" |
| 132 |
"--lat <latitude> Reference/receiver latitide for surface posn (opt)\n" |
| 133 |
"--lon <longitude> Reference/receiver longitude for surface posn (opt)\n" |
| 134 |
"--max-range <distance> Absolute maximum range for position decoding (in nm, default: 300)\n" |
| 135 |
"--no-crc-check Disable messages with broken CRC (discouraged)\n" |
| 136 |
"--no-fix Disable single-bits error correction using CRC\n" |
| 137 |
"--fix Enable single-bits error correction using CRC\n" |
| 138 |
"--aggressive More CPU for more messages (two bits fixes, ...)\n" |
| 139 |
"--metric Use metric units (meters, km/h, ...)\n" |
| 140 |
"--show-only <addr> Show only messages from the given ICAO on stdout\n" |
| 141 |
"--help Show this help\n", |
| 142 |
MODES_DUMP1090_VARIANT " " MODES_DUMP1090_VERSION |
| 143 |
); |
| 144 |
} |
| 145 |
|
| 146 |
// |
| 147 |
//========================================================================= |
| 148 |
// |
| 149 |
int main(int argc, char **argv) { |
| 150 |
int j; |
| 151 |
struct client *c; |
| 152 |
struct net_service *s; |
| 153 |
char *bo_connect_ipaddr = "127.0.0.1"; |
| 154 |
int bo_connect_port = 30005; |
| 155 |
|
| 156 |
// Set sane defaults |
| 157 |
|
| 158 |
view1090InitConfig(); |
| 159 |
signal(SIGINT, sigintHandler); // Define Ctrl/C handler (exit program) |
| 160 |
|
| 161 |
// Parse the command line options |
| 162 |
for (j = 1; j < argc; j++) { |
| 163 |
int more = ((j + 1) < argc); // There are more arguments |
| 164 |
|
| 165 |
if (!strcmp(argv[j],"--net-bo-port") && more) { |
| 166 |
bo_connect_port = atoi(argv[++j]); |
| 167 |
} else if (!strcmp(argv[j],"--net-bo-ipaddr") && more) { |
| 168 |
bo_connect_ipaddr = argv[++j]; |
| 169 |
} else if (!strcmp(argv[j],"--modeac")) { |
| 170 |
Modes.mode_ac = 1; |
| 171 |
} else if (!strcmp(argv[j],"--interactive-rows") && more) { |
| 172 |
Modes.interactive_rows = atoi(argv[++j]); |
| 173 |
} else if (!strcmp(argv[j],"--no-interactive")) { |
| 174 |
Modes.interactive = 0; |
| 175 |
} else if (!strcmp(argv[j],"--show-only") && more) { |
| 176 |
Modes.show_only = (uint32_t) strtoul(argv[++j], NULL, 16); |
| 177 |
Modes.interactive = 0; |
| 178 |
} else if (!strcmp(argv[j],"--interactive-ttl") && more) { |
| 179 |
Modes.interactive_display_ttl = (uint64_t)(1000 * atof(argv[++j])); |
| 180 |
} else if (!strcmp(argv[j],"--interactive-rtl1090")) { |
| 181 |
Modes.interactive = 1; |
| 182 |
Modes.interactive_rtl1090 = 1; |
| 183 |
} else if (!strcmp(argv[j],"--lat") && more) { |
| 184 |
Modes.fUserLat = atof(argv[++j]); |
| 185 |
} else if (!strcmp(argv[j],"--lon") && more) { |
| 186 |
Modes.fUserLon = atof(argv[++j]); |
| 187 |
} else if (!strcmp(argv[j],"--metric")) { |
| 188 |
Modes.metric = 1; |
| 189 |
} else if (!strcmp(argv[j],"--no-crc-check")) { |
| 190 |
Modes.check_crc = 0; |
| 191 |
} else if (!strcmp(argv[j],"--fix")) { |
| 192 |
Modes.nfix_crc = 1; |
| 193 |
} else if (!strcmp(argv[j],"--no-fix")) { |
| 194 |
Modes.nfix_crc = 0; |
| 195 |
} else if (!strcmp(argv[j],"--aggressive")) { |
| 196 |
Modes.nfix_crc = MODES_MAX_BITERRORS; |
| 197 |
} else if (!strcmp(argv[j],"--max-range") && more) { |
| 198 |
Modes.maxRange = atof(argv[++j]) * 1852.0; // convert to metres |
| 199 |
} else if (!strcmp(argv[j],"--help")) { |
| 200 |
showHelp(); |
| 201 |
exit(0); |
| 202 |
} else { |
| 203 |
fprintf(stderr, "Unknown or not enough arguments for option '%s'.\n\n", argv[j]); |
| 204 |
showHelp(); |
| 205 |
exit(1); |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
#ifdef _WIN32 |
| 210 |
// Try to comply with the Copyright license conditions for binary distribution |
| 211 |
if (!Modes.quiet) {showCopyright();} |
| 212 |
#define MSG_DONTWAIT 0 |
| 213 |
#endif |
| 214 |
|
| 215 |
#ifndef _WIN32 |
| 216 |
// Setup for SIGWINCH for handling lines |
| 217 |
if (Modes.interactive) {signal(SIGWINCH, sigWinchCallback);} |
| 218 |
#endif |
| 219 |
|
| 220 |
// Initialization |
| 221 |
view1090Init(); |
| 222 |
modesInitNet(); |
| 223 |
|
| 224 |
// Try to connect to the selected ip address and port. We only support *ONE* input connection which we initiate.here. |
| 225 |
s = makeBeastInputService(); |
| 226 |
c = serviceConnect(s, bo_connect_ipaddr, bo_connect_port); |
| 227 |
if (!c) { |
| 228 |
fprintf(stderr, "Failed to connect to %s:%d: %s\n", bo_connect_ipaddr, bo_connect_port, Modes.aneterr); |
| 229 |
exit(1); |
| 230 |
} |
| 231 |
|
| 232 |
// Keep going till the user does something that stops us |
| 233 |
while (!Modes.exit) { |
| 234 |
icaoFilterExpire(); |
| 235 |
trackPeriodicUpdate(); |
| 236 |
modesNetPeriodicWork(); |
| 237 |
|
| 238 |
if (Modes.interactive) |
| 239 |
interactiveShowData(); |
| 240 |
|
| 241 |
if (s->connections == 0) { |
| 242 |
// lost input connection, try to reconnect |
| 243 |
usleep(1000000); |
| 244 |
c = serviceConnect(s, bo_connect_ipaddr, bo_connect_port); |
| 245 |
continue; |
| 246 |
} |
| 247 |
|
| 248 |
usleep(100000); |
| 249 |
} |
| 250 |
|
| 251 |
return (0); |
| 252 |
} |
| 253 |
// |
| 254 |
//========================================================================= |
| 255 |
// |