view1090.c
Go to the documentation of this file.
1 // view1090, a Mode S messages viewer for dump1090 devices.
2 //
3 // Copyright (C) 2013 by Malcolm Robb <Support@ATTAvionics.com>
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 "coaa.h"
31 #include "view1090.h"
32 //
33 // ============================= Utility functions ==========================
34 //
35 void sigintHandler(int dummy) {
36  NOTUSED(dummy);
37  signal(SIGINT, SIG_DFL); // reset signal handler - bit extra safety
38  Modes.exit = 1; // Signal to threads that we are done
39 }
40 //
41 // =============================== Initialization ===========================
42 //
43 void view1090InitConfig(void) {
44  // Default everything to zero/NULL
45  memset(&Modes, 0, sizeof(Modes));
46  memset(&View1090, 0, sizeof(View1090));
47 
48  // Now initialise things that should not be 0/NULL to their defaults
49  Modes.check_crc = 1;
50  strcpy(View1090.net_input_beast_ipaddr,VIEW1090_NET_OUTPUT_IP_ADDRESS);
51  Modes.net_input_beast_port = MODES_NET_OUTPUT_BEAST_PORT;
52  Modes.interactive_rows = MODES_INTERACTIVE_ROWS;
53  Modes.interactive_delete_ttl = MODES_INTERACTIVE_DELETE_TTL;
54  Modes.interactive_display_ttl = MODES_INTERACTIVE_DISPLAY_TTL;
57 
58  Modes.interactive = 1;
59 }
60 //
61 //=========================================================================
62 //
63 void view1090Init(void) {
64 
65  // Allocate the various buffers used by Modes
66  if ( NULL == (Modes.icao_cache = (uint32_t *) malloc(sizeof(uint32_t) * MODES_ICAO_CACHE_LEN * 2)))
67  {
68  fprintf(stderr, "Out of memory allocating data buffer.\n");
69  exit(1);
70  }
71 
72  // Clear the buffers that have just been allocated, just in-case
73  memset(Modes.icao_cache, 0, sizeof(uint32_t) * MODES_ICAO_CACHE_LEN * 2);
74 
75  // Validate the users Lat/Lon home location inputs
76  if ( (Modes.fUserLat > 90.0) // Latitude must be -90 to +90
77  || (Modes.fUserLat < -90.0) // and
78  || (Modes.fUserLon > 360.0) // Longitude must be -180 to +360
79  || (Modes.fUserLon < -180.0) ) {
80  Modes.fUserLat = Modes.fUserLon = 0.0;
81  } else if (Modes.fUserLon > 180.0) { // If Longitude is +180 to +360, make it -180 to 0
82  Modes.fUserLon -= 360.0;
83  }
84  // If both Lat and Lon are 0.0 then the users location is either invalid/not-set, or (s)he's in the
85  // Atlantic ocean off the west coast of Africa. This is unlikely to be correct.
86  // Set the user LatLon valid flag only if either Lat or Lon are non zero. Note the Greenwich meridian
87  // is at 0.0 Lon,so we must check for either fLat or fLon being non zero not both.
88  // Testing the flag at runtime will be much quicker than ((fLon != 0.0) || (fLat != 0.0))
89  Modes.bUserFlags &= ~MODES_USER_LATLON_VALID;
90  if ((Modes.fUserLat != 0.0) || (Modes.fUserLon != 0.0)) {
91  Modes.bUserFlags |= MODES_USER_LATLON_VALID;
92  }
93 
94  // Prepare error correction tables
96 }
97 //
98 // ================================ Main ====================================
99 //
100 void showHelp(void) {
101  printf(
102 "-----------------------------------------------------------------------------\n"
103 "| view1090 dump1090 Viewer Ver : "MODES_DUMP1090_VERSION " |\n"
104 "-----------------------------------------------------------------------------\n"
105  "--interactive Interactive mode refreshing data on screen\n"
106  "--interactive-rows <num> Max number of rows in interactive mode (default: 15)\n"
107  "--interactive-ttl <sec> Remove from list if idle for <sec> (default: 60)\n"
108  "--interactive-rtl1090 Display flight table in RTL1090 format\n"
109  "--modeac Enable decoding of SSR modes 3/A & 3/C\n"
110  "--net-bo-ipaddr <IPv4> TCP Beast output listen IPv4 (default: 127.0.0.1)\n"
111  "--net-bo-port <port> TCP Beast output listen port (default: 30005)\n"
112  "--lat <latitude> Reference/receiver latitide for surface posn (opt)\n"
113  "--lon <longitude> Reference/receiver longitude for surface posn (opt)\n"
114  "--no-crc-check Disable messages with broken CRC (discouraged)\n"
115  "--no-fix Disable single-bits error correction using CRC\n"
116  "--fix Enable single-bits error correction using CRC\n"
117  "--aggressive More CPU for more messages (two bits fixes, ...)\n"
118  "--metric Use metric units (meters, km/h, ...)\n"
119  "--help Show this help\n"
120  );
121 }
122 //
123 //=========================================================================
124 //
125 int main(int argc, char **argv) {
126  int j, fd;
127  struct client *c;
128 
129  // Set sane defaults
130 
132  signal(SIGINT, sigintHandler); // Define Ctrl/C handler (exit program)
133 
134  // Parse the command line options
135  for (j = 1; j < argc; j++) {
136  int more = ((j + 1) < argc); // There are more arguments
137 
138  if (!strcmp(argv[j],"--net-bo-port") && more) {
139  Modes.net_input_beast_port = atoi(argv[++j]);
140  } else if (!strcmp(argv[j],"--net-bo-ipaddr") && more) {
141  strcpy(View1090.net_input_beast_ipaddr, argv[++j]);
142  } else if (!strcmp(argv[j],"--modeac")) {
143  Modes.mode_ac = 1;
144  } else if (!strcmp(argv[j],"--interactive-rows") && more) {
145  Modes.interactive_rows = atoi(argv[++j]);
146  } else if (!strcmp(argv[j],"--interactive")) {
147  Modes.interactive = 1;
148  } else if (!strcmp(argv[j],"--interactive-ttl") && more) {
149  Modes.interactive_display_ttl = atoi(argv[++j]);
150  } else if (!strcmp(argv[j],"--interactive-rtl1090")) {
151  Modes.interactive = 1;
152  Modes.interactive_rtl1090 = 1;
153  } else if (!strcmp(argv[j],"--lat") && more) {
154  Modes.fUserLat = atof(argv[++j]);
155  } else if (!strcmp(argv[j],"--lon") && more) {
156  Modes.fUserLon = atof(argv[++j]);
157  } else if (!strcmp(argv[j],"--metric")) {
158  Modes.metric = 1;
159  } else if (!strcmp(argv[j],"--no-crc-check")) {
160  Modes.check_crc = 0;
161  } else if (!strcmp(argv[j],"--fix")) {
162  Modes.nfix_crc = 1;
163  } else if (!strcmp(argv[j],"--no-fix")) {
164  Modes.nfix_crc = 0;
165  } else if (!strcmp(argv[j],"--aggressive")) {
166  Modes.nfix_crc = MODES_MAX_BITERRORS;
167  } else if (!strcmp(argv[j],"--help")) {
168  showHelp();
169  exit(0);
170  } else {
171  fprintf(stderr, "Unknown or not enough arguments for option '%s'.\n\n", argv[j]);
172  showHelp();
173  exit(1);
174  }
175  }
176 
177  // Initialization
178  view1090Init();
179 
180  // Try to connect to the selected ip address and port. We only support *ONE* input connection which we initiate.here.
181  if ((fd = anetTcpConnect(Modes.aneterr, View1090.net_input_beast_ipaddr, Modes.net_input_beast_port)) == ANET_ERR) {
182  fprintf(stderr, "Failed to connect to %s:%d\n", View1090.net_input_beast_ipaddr, Modes.net_input_beast_port);
183  exit(1);
184  }
185  //
186  // Setup a service callback client structure for a beast binary input (from dump1090)
187  // This is a bit dodgy under Windows. The fd parameter is a handle to the internet
188  // socket on which we are receiving data. Under Linux, these seem to start at 0 and
189  // count upwards. However, Windows uses "HANDLES" and these don't nececeriy start at 0.
190  // dump1090 limits fd to values less than 1024, and then uses the fd parameter to
191  // index into an array of clients. This is ok-ish if handles are allocated up from 0.
192  // However, there is no gaurantee that Windows will behave like this, and if Windows
193  // allocates a handle greater than 1024, then dump1090 won't like it. On my test machine,
194  // the first Windows handle is usually in the 0x54 (84 decimal) region.
195 
196  if (fd >= MODES_NET_MAX_FD) { // Max number of clients reached
197  fprintf(stderr, "Max number of clients exceeded : fd = 0x%X\n", fd);
198  close(fd);
199  exit(1);
200  }
201 
202  c = (struct client *) malloc(sizeof(*c));
203  c->buflen = 0;
204  c->fd =
205  c->service =
206  Modes.bis = fd;
207  Modes.clients[fd] = c;
208  if (Modes.maxfd < fd) {
209  Modes.maxfd = fd;
210  }
211 
212  // Keep going till the user does something that stops us
213  while (!Modes.exit) {
217  }
218 
219  // The user has stopped us, so close any socket we opened
220  if (fd != ANET_ERR)
221  {close(fd);}
222 
223  pthread_exit(0);
224 
225  return (0);
226 }
227 //
228 //=========================================================================
229 //
void interactiveRemoveStaleAircrafts(void)
Definition: interactive.c:446
void showHelp(void)
Definition: dump1090.c:339
int anetTcpConnect(char *err, char *addr, int port)
Definition: anet.c:181
void sigintHandler(int dummy)
Definition: dump1090.c:36
int fd
Definition: dump1090.h:185
void interactiveShowData(void)
Definition: interactive.c:324
#define MODES_ICAO_CACHE_LEN
Definition: dump1090.h:120
void view1090InitConfig(void)
Definition: view1090.c:43
int decodeBinMessage(struct client *c, char *p)
Definition: net_io.c:425
int exit
Definition: dump1090.h:237
#define MODES_USER_LATITUDE_DFLT
Definition: dump1090.h:75
void modesInitErrorInfo()
Definition: mode_s.c:234
void modesReadFromClient(struct client *c, char *sep, int(*handler)(struct client *, char *))
Definition: net_io.c:759
int service
Definition: dump1090.h:186
#define MODES_USER_LONGITUDE_DFLT
Definition: dump1090.h:76
int fd
Definition: dump1090.h:233
#define MODES_INTERACTIVE_DISPLAY_TTL
Definition: dump1090.h:163
#define NOTUSED(V)
Definition: ppup1090.h:63
int main(int argc, char *argv[])
Definition: DBSync.cc:58
struct @0 Modes
#define MODES_MAX_BITERRORS
Definition: dump1090.h:92
#define MODES_INTERACTIVE_DELETE_TTL
Definition: dump1090.h:162
#define VIEW1090_NET_OUTPUT_IP_ADDRESS
Definition: view1090.h:60
#define MODES_NET_MAX_FD
Definition: dump1090.h:165
#define MODES_DUMP1090_VERSION
Definition: dump1090.h:40
struct @2 View1090
void view1090Init(void)
Definition: view1090.c:63
#define MODES_USER_LATLON_VALID
Definition: dump1090.h:125
#define MODES_NET_OUTPUT_BEAST_PORT
Definition: dump1090.h:170
int buflen
Definition: dump1090.h:187
#define ANET_ERR
Definition: anet.h:35
#define MODES_INTERACTIVE_ROWS
Definition: dump1090.h:161

, generated on Tue Sep 26 2023.