From 5a2bf79cfe339f19f1a97b345c122198bf958c98 Mon Sep 17 00:00:00 2001 From: Karl Lehenbauer Date: Thu, 31 Jul 2014 14:45:26 +0000 Subject: [PATCH] faup1090 forwards dump1090 ADS-B to FlightAware * connects to dump1090 a la ppup1090 * extracts the data, filters, batches packets and compresses to use very littl bandwidth * requires FA "ADS-B adept" software to actually move the data. --- faup1090.c | 413 +++++++++++++++++++++++++++++++++++++++++++++++++++ faup1090.h | 20 +++ makefaup1090 | 27 ++++ 3 files changed, 460 insertions(+) create mode 100644 faup1090.c create mode 100644 faup1090.h create mode 100644 makefaup1090 diff --git a/faup1090.c b/faup1090.c new file mode 100644 index 0000000..890c33b --- /dev/null +++ b/faup1090.c @@ -0,0 +1,413 @@ +// dump1090, a Mode S messages decoder for RTLSDR devices. +// +// Copyright (C) 2012 by Salvatore Sanfilippo +// Copyright (C) 2014 by FlightAware LLC +// +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// +// * Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +#include "faup1090.h" +// +// ============================= Utility functions ========================== +// +void sigintHandler(int dummy) { + MODES_NOTUSED(dummy); + signal(SIGINT, SIG_DFL); // reset signal handler - bit extra safety + Modes.exit = 1; // Signal to threads that we are done +} +// +// =============================== Terminal handling ======================== +// +#ifndef _WIN32 +// Get the number of rows after the terminal changes size. +int getTermRows() { + struct winsize w; + ioctl(STDOUT_FILENO, TIOCGWINSZ, &w); + return (w.ws_row); +} + +// Handle resizing terminal +void sigWinchCallback() { + signal(SIGWINCH, SIG_IGN); + Modes.interactive_rows = getTermRows(); + interactiveShowData(); + signal(SIGWINCH, sigWinchCallback); +} +#else +int getTermRows() { return MODES_INTERACTIVE_ROWS;} +#endif +// +// =============================== Initialization =========================== +// +void modesInitConfig(void) { + // Default everything to zero/NULL + memset(&Modes, 0, sizeof(Modes)); + + // Now initialise things that should not be 0/NULL to their defaults + Modes.check_crc = 1; + Modes.net = 1; + //Modes.quiet = 1; + Modes.net_heartbeat_rate = MODES_NET_HEARTBEAT_RATE; + Modes.net_input_beast_port = MODES_NET_OUTPUT_BEAST_PORT; + Modes.net_fatsv_port = MODES_NET_OUTPUT_FA_TSV_PORT; + Modes.interactive_rows = getTermRows(); + Modes.interactive_delete_ttl = MODES_INTERACTIVE_DELETE_TTL; + Modes.interactive_display_ttl = MODES_INTERACTIVE_DISPLAY_TTL; + Modes.fUserLat = MODES_USER_LATITUDE_DFLT; + Modes.fUserLon = MODES_USER_LONGITUDE_DFLT; +} + +void faupInitConfig(void) { + memset(&faup1090, 0, sizeof(faup1090)); + + strcpy (faup1090.net_input_beast_ipaddr, FAUP1090_NET_OUTPUT_IP_ADDRESS); +} + +// +//========================================================================= +// +void modesInit(void) { + int i; + + // Allocate the various buffers used by Modes + if ( ((Modes.icao_cache = (uint32_t *) malloc(sizeof(uint32_t) * MODES_ICAO_CACHE_LEN * 2) ) == NULL) || + ((Modes.beastOut = (char *) malloc(MODES_RAWOUT_BUF_SIZE) ) == NULL) || + ((Modes.rawOut = (char *) malloc(MODES_RAWOUT_BUF_SIZE) ) == NULL) ) + { + fprintf(stderr, "Out of memory allocating data buffer.\n"); + exit(1); + } + + // Clear the buffers that have just been allocated, just in-case + memset(Modes.icao_cache, 0, sizeof(uint32_t) * MODES_ICAO_CACHE_LEN * 2); + + // Validate the users Lat/Lon home location inputs + if ( (Modes.fUserLat > 90.0) // Latitude must be -90 to +90 + || (Modes.fUserLat < -90.0) // and + || (Modes.fUserLon > 360.0) // Longitude must be -180 to +360 + || (Modes.fUserLon < -180.0) ) { + Modes.fUserLat = Modes.fUserLon = 0.0; + } else if (Modes.fUserLon > 180.0) { // If Longitude is +180 to +360, make it -180 to 0 + Modes.fUserLon -= 360.0; + } + // If both Lat and Lon are 0.0 then the users location is either invalid/not-set, or (s)he's in the + // Atlantic ocean off the west coast of Africa. This is unlikely to be correct. + // Set the user LatLon valid flag only if either Lat or Lon are non zero. Note the Greenwich meridian + // is at 0.0 Lon,so we must check for either fLat or fLon being non zero not both. + // Testing the flag at runtime will be much quicker than ((fLon != 0.0) || (fLat != 0.0)) + Modes.bUserFlags &= ~MODES_USER_LATLON_VALID; + if ((Modes.fUserLat != 0.0) || (Modes.fUserLon != 0.0)) { + Modes.bUserFlags |= MODES_USER_LATLON_VALID; + } + + // Limit the maximum requested raw output size to less than one Ethernet Block + if (Modes.net_output_raw_size > (MODES_RAWOUT_BUF_FLUSH)) + {Modes.net_output_raw_size = MODES_RAWOUT_BUF_FLUSH;} + if (Modes.net_output_raw_rate > (MODES_RAWOUT_BUF_RATE)) + {Modes.net_output_raw_rate = MODES_RAWOUT_BUF_RATE;} + if (Modes.net_sndbuf_size > (MODES_NET_SNDBUF_MAX)) + {Modes.net_sndbuf_size = MODES_NET_SNDBUF_MAX;} + + // Initialise the Block Timers to something half sensible + ftime(&Modes.stSystemTimeBlk); + for (i = 0; i < MODES_ASYNC_BUF_NUMBER; i++) + {Modes.stSystemTimeRTL[i] = Modes.stSystemTimeBlk;} + + // Prepare error correction tables + modesInitErrorInfo(); +} + +struct service { + char *descr; + int *socket; + int port; + int enabled; +}; + +#define FAUP_NET_SERVICES_NUM 1 + +extern struct service services[FAUP_NET_SERVICES_NUM]; + +void faupInitNet(void) { + int j; + + struct service svc[FAUP_NET_SERVICES_NUM] = { + // {"Raw TCP output", &Modes.ros, Modes.net_output_raw_port, 0}, + // {"Raw TCP input", &Modes.ris, Modes.net_input_raw_port, 0}, + // {"Beast TCP output", &Modes.bos, Modes.net_output_beast_port, 0}, + // {"Beast TCP input", &Modes.bis, Modes.net_input_beast_port, 0}, + // {"HTTP server", &Modes.https, Modes.net_http_port, 0}, + // {"Basestation TCP output", &Modes.sbsos, Modes.net_output_sbs_port, 0}, + {"FlightAware TSV output", &Modes.fatsvos, MODES_NET_OUTPUT_FA_TSV_PORT, 1} + }; + + memcpy(&services, &svc, sizeof(svc));//services = svc; + + Modes.clients = NULL; + +#ifdef _WIN32 + if ( (!Modes.wsaData.wVersion) + && (!Modes.wsaData.wHighVersion) ) { + // Try to start the windows socket support + if (WSAStartup(MAKEWORD(2,1),&Modes.wsaData) != 0) + { + fprintf(stderr, "WSAStartup returned Error\n"); + } + } +#endif + + for (j = 0; j < MODES_NET_SERVICES_NUM; j++) { + services[j].enabled = (services[j].port != 0); + if (services[j].enabled) { + int s = anetTcpServer(Modes.aneterr, services[j].port, NULL); + if (s == -1) { + fprintf(stderr, "Error opening the listening port %d (%s): %s\n", + services[j].port, services[j].descr, strerror(errno)); + exit(1); + } + anetNonBlock(Modes.aneterr, s); + *services[j].socket = s; + } else { + if (Modes.debug & MODES_DEBUG_NET) printf("%s port is disabled\n", services[j].descr); + } + } + +#ifndef _WIN32 + signal(SIGPIPE, SIG_IGN); +#endif +} + +// +// ================================ Main ==================================== +// +void showHelp(void) { + printf( +"-----------------------------------------------------------------------------\n" +"| dump1090 ModeS Receiver Ver : " MODES_DUMP1090_VERSION " |\n" +"-----------------------------------------------------------------------------\n" +"--interactive Interactive mode refreshing data on screen\n" +"--interactive-rows Max number of rows in interactive mode (default: 15)\n" +"--interactive-ttl Remove from list if idle for (default: 60)\n" +"--interactive-rtl1090 Display flight table in RTL1090 format\n" +"--net-fatsv-port FlightAware TSV output port (default: 10001)\n" +"--net-bo-ipaddr TCP Beast output listen IPV4 address (default: 127.0.0.1)\n" +"--net-bo-port TCP Beast output listen port (default: 30005)\n" +"--net-heartbeat TCP heartbeat rate in seconds (default: 60 sec; 0 to disable)\n" +"--no-crc-check Enable messages with broken CRC (discouraged)\n" +"--net-buffer TCP buffer size 64Kb * (2^n) (default: n=0, 64Kb)\n" +"--lat Reference/receiver latitude for surface posn (opt)\n" +"--lon Reference/receiver longitude for surface posn (opt)\n" +"--debug Debug mode (verbose), see README for details\n" +"--quiet Disable output to stdout. Use for daemon applications\n" +"--help Show this help\n" +"\n" +"Debug mode flags: n = Log network debugging info\n" + ); +} + +#ifdef _WIN32 +void showCopyright(void) { + uint64_t llTime = time(NULL) + 1; + + printf( +"-----------------------------------------------------------------------------\n" +"| dump1090 ModeS Receiver Ver : " MODES_DUMP1090_VERSION " |\n" +"-----------------------------------------------------------------------------\n" +"\n" +" Copyright (C) 2012 by Salvatore Sanfilippo \n" +" Copyright (C) 2014 by Malcolm Robb \n" +" Copyright (C) 2014 by FlightAware LLC\n" +"\n" +" All rights reserved.\n" +"\n" +" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n" +" ""AS IS"" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n" +" LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n" +" A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n" +" HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n" +" SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n" +" LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n" +" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n" +" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n" +" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n" +" OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n" +"\n" +" For further details refer to \n" +"\n" + ); + + // delay for 1 second to give the user a chance to read the copyright + while (llTime >= time(NULL)) {} +} +#endif +// +//========================================================================= +// +// This function is called a few times every second by main in order to +// perform tasks we need to do continuously, like accepting new clients +// from the net, refreshing the screen in interactive mode, and so forth +// +void backgroundTasks(void) { + if (Modes.net) { + modesReadFromClients(); + showFlightsFATSV(); + } + + // If Modes.aircrafts is not NULL, remove any stale aircraft + if (Modes.aircrafts) { + interactiveRemoveStaleAircrafts(); + } + + // Refresh screen when in interactive mode + if (Modes.interactive) { + interactiveShowData(); + } +} + +// +//========================================================================= +// +int main(int argc, char **argv) { + int j, fd; + struct client *c; + + // Set sane defaults + modesInitConfig(); + faupInitConfig(); + signal(SIGINT, sigintHandler); // Define Ctrl/C handler (exit program) + + // Parse the command line options + for (j = 1; j < argc; j++) { + int more = j+1 < argc; // There are more arguments + + if (!strcmp(argv[j],"--no-crc-check")) { + Modes.check_crc = 0; + } else if (!strcmp(argv[j],"--net-heartbeat") && more) { + Modes.net_heartbeat_rate = atoi(argv[++j]) * 15; + } else if (!strcmp(argv[j],"--net-bo-ipaddr") && more) { + strcpy (faup1090.net_input_beast_ipaddr, argv[++j]); + } else if (!strcmp(argv[j],"--net-bo-port") && more) { + Modes.net_input_beast_port = atoi(argv[++j]); + } else if (!strcmp(argv[j],"--net-fatsv-port") && more) { + Modes.net_fatsv_port = atoi(argv[++j]); + } else if (!strcmp(argv[j],"--net-buffer") && more) { + Modes.net_sndbuf_size = atoi(argv[++j]); + } else if (!strcmp(argv[j],"--onlyaddr")) { + Modes.onlyaddr = 1; + } else if (!strcmp(argv[j],"--interactive")) { + Modes.interactive = 1; + } else if (!strcmp(argv[j],"--interactive-rows") && more) { + Modes.interactive_rows = atoi(argv[++j]); + } else if (!strcmp(argv[j],"--interactive-ttl") && more) { + Modes.interactive_display_ttl = atoi(argv[++j]); + } else if (!strcmp(argv[j],"--lat") && more) { + Modes.fUserLat = atof(argv[++j]); + } else if (!strcmp(argv[j],"--lon") && more) { + Modes.fUserLon = atof(argv[++j]); + } else if (!strcmp(argv[j],"--debug") && more) { + char *f = argv[++j]; + while(*f) { + switch(*f) { + case 'D': Modes.debug |= MODES_DEBUG_DEMOD; break; + case 'd': Modes.debug |= MODES_DEBUG_DEMODERR; break; + case 'C': Modes.debug |= MODES_DEBUG_GOODCRC; break; + case 'c': Modes.debug |= MODES_DEBUG_BADCRC; break; + case 'p': Modes.debug |= MODES_DEBUG_NOPREAMBLE; break; + case 'n': Modes.debug |= MODES_DEBUG_NET; break; + case 'j': Modes.debug |= MODES_DEBUG_JS; break; + default: + fprintf(stderr, "Unknown debugging flag: %c\n", *f); + exit(1); + break; + } + f++; + } + } else if (!strcmp(argv[j],"--stats")) { + Modes.stats = 1; + } else if (!strcmp(argv[j],"--help")) { + showHelp(); + exit(0); + } else if (!strcmp(argv[j],"--quiet")) { + Modes.quiet = 1; + } else { + fprintf(stderr, + "Unknown or not enough arguments for option '%s'.\n\n", + argv[j]); + showHelp(); + exit(1); + } + } + +#ifdef _WIN32 + // Try to comply with the Copyright license conditions for binary distribution + if (!Modes.quiet) {showCopyright();} +#endif + +#ifndef _WIN32 + // Setup for SIGWINCH for handling lines + if (Modes.interactive) {signal(SIGWINCH, sigWinchCallback);} +#endif + + // Initialization + modesInit(); + + Modes.net_only = 1; + + faupInitNet(); + + if ((fd = anetTcpConnect (Modes.aneterr, faup1090.net_input_beast_ipaddr, Modes.net_input_beast_port)) == ANET_ERR) { + fprintf (stderr, "failed to connect to %s:%d\n", faup1090.net_input_beast_ipaddr, Modes.net_input_beast_port); + exit (1); + } + + c = (struct client *) malloc(sizeof(*c)); + c->next = NULL; + c->buflen = 0; + c->fd = + c->service = + Modes.bis = fd; + Modes.clients = c; + + while (Modes.net_only) { + if (Modes.exit) exit(0); // If we exit net_only nothing further in main() + backgroundTasks(); + usleep(100000); + } + + while (Modes.exit == 0) { + backgroundTasks(); + } + +#ifndef _WIN32 + pthread_exit(0); +#else + return (0); +#endif +} +// +//========================================================================= +// + +// vim: set ts=4 sw=4 sts=4 expandtab : diff --git a/faup1090.h b/faup1090.h new file mode 100644 index 0000000..2c16af6 --- /dev/null +++ b/faup1090.h @@ -0,0 +1,20 @@ + + +#include "dump1090.h" + +#undef MODES_NET_INPUT_RAW_PORT +#define MODES_NET_INPUT_RAW_PORT 20001 + +#undef MODES_NET_INPUT_BEAST_PORT +#define MODES_NET_INPUT_BEAST_PORT 20004 + +#undef MODES_NET_OUTPUT_FA_TSV_PORT +#define MODES_NET_OUTPUT_FA_TSV_PORT 10002 + +struct { + char net_input_beast_ipaddr[32]; +} faup1090; + +#define FAUP1090_NET_OUTPUT_IP_ADDRESS "127.0.0.1" + + diff --git a/makefaup1090 b/makefaup1090 new file mode 100644 index 0000000..1a3c868 --- /dev/null +++ b/makefaup1090 @@ -0,0 +1,27 @@ +# +# When building a package or installing otherwise in the system, make +# sure that the variable PREFIX is defined, e.g. make PREFIX=/usr/local +# +PROGNAME=faup1090 + +ifdef PREFIX +BINDIR=$(PREFIX)/bin +SHAREDIR=$(PREFIX)/share/$(PROGNAME) +EXTRACFLAGS=-DHTMLPATH=\"$(SHAREDIR)\" +endif + +CFLAGS=-O2 -g -Wall -W `pkg-config --cflags librtlsdr` +LIBS=`pkg-config --libs librtlsdr` -lpthread -lm +CC=gcc + + +all: faup1090 + +%.o: %.c + $(CC) $(CFLAGS) $(EXTRACFLAGS) -c $< + +faup1090: faup1090.o anet.o interactive.o mode_ac.o mode_s.o net_io.o + $(CC) -g -o faup1090 faup1090.o anet.o interactive.o mode_ac.o mode_s.o net_io.o coaa1090.obj $(LIBS) $(LDFLAGS) + +clean: + rm -f *.o faup1090