diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index bfdaaf0..26bb39a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -71,7 +71,8 @@ add_executable(rtl_tcp rtl_tcp.c) add_executable(rtl_test rtl_test.c) add_executable(rtl_fm rtl_fm.c) add_executable(rtl_eeprom rtl_eeprom.c) -set(INSTALL_TARGETS rtlsdr_shared rtlsdr_static rtl_sdr rtl_tcp rtl_test rtl_fm rtl_eeprom) +add_executable(rtl_adsb rtl_adsb.c) +set(INSTALL_TARGETS rtlsdr_shared rtlsdr_static rtl_sdr rtl_tcp rtl_test rtl_fm rtl_eeprom rtl_adsb) target_link_libraries(rtl_sdr rtlsdr_shared ${LIBUSB_LIBRARIES} @@ -93,6 +94,10 @@ target_link_libraries(rtl_eeprom rtlsdr_shared ${LIBUSB_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ) +target_link_libraries(rtl_adsb rtlsdr_shared + ${LIBUSB_LIBRARIES} + ${CMAKE_THREAD_LIBS_INIT} +) if(UNIX) target_link_libraries(rtl_fm m) if(APPLE) @@ -108,11 +113,13 @@ target_link_libraries(rtl_tcp libgetopt_static) target_link_libraries(rtl_test libgetopt_static) target_link_libraries(rtl_fm libgetopt_static) target_link_libraries(rtl_eeprom libgetopt_static) +target_link_libraries(rtl_adsb libgetopt_static) set_property(TARGET rtl_sdr APPEND PROPERTY COMPILE_DEFINITIONS "rtlsdr_STATIC" ) set_property(TARGET rtl_tcp APPEND PROPERTY COMPILE_DEFINITIONS "rtlsdr_STATIC" ) set_property(TARGET rtl_test APPEND PROPERTY COMPILE_DEFINITIONS "rtlsdr_STATIC" ) set_property(TARGET rtl_fm APPEND PROPERTY COMPILE_DEFINITIONS "rtlsdr_STATIC" ) set_property(TARGET rtl_eeprom APPEND PROPERTY COMPILE_DEFINITIONS "rtlsdr_STATIC" ) +set_property(TARGET rtl_adsb APPEND PROPERTY COMPILE_DEFINITIONS "rtlsdr_STATIC" ) endif() ######################################################################## # Install built library files & utilities diff --git a/src/Makefile.am b/src/Makefile.am index f1162f5..51e1b9f 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -10,7 +10,7 @@ lib_LTLIBRARIES = librtlsdr.la librtlsdr_la_SOURCES = librtlsdr.c tuner_e4k.c tuner_fc0012.c tuner_fc0013.c tuner_fc2580.c tuner_r820t.c librtlsdr_la_LDFLAGS = -version-info $(LIBVERSION) -bin_PROGRAMS = rtl_sdr rtl_tcp rtl_test rtl_fm rtl_eeprom +bin_PROGRAMS = rtl_sdr rtl_tcp rtl_test rtl_fm rtl_eeprom rtl_adsb rtl_sdr_SOURCES = rtl_sdr.c rtl_sdr_LDADD = librtlsdr.la @@ -26,3 +26,6 @@ rtl_fm_LDADD = librtlsdr.la $(LIBM) rtl_eeprom_SOURCES = rtl_eeprom.c rtl_eeprom_LDADD = librtlsdr.la $(LIBM) + +rtl_adsb_SOURCES = rtl_adsb.c +rtl_adsb_LDADD = librtlsdr.la diff --git a/src/rtl_adsb.c b/src/rtl_adsb.c new file mode 100644 index 0000000..bdba983 --- /dev/null +++ b/src/rtl_adsb.c @@ -0,0 +1,463 @@ +/* + * rtl-sdr, turns your Realtek RTL2832 based DVB dongle into a SDR receiver + * Copyright (C) 2012 by Steve Markgraf + * Copyright (C) 2012 by Hoernchen + * Copyright (C) 2012 by Kyle Keen + * Copyright (C) 2012 by Youssef Touil + * Copyright (C) 2012 by Ian Gilmour + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +#include +#include +#include +#include +#include + +#ifndef _WIN32 +#include +#else +#include +#include +#include +#include "getopt/getopt.h" +#endif + +#include +#include +#include + +#include "rtl-sdr.h" + +#ifdef _WIN32 +#define sleep Sleep +#undef min +#undef max +#endif + +#define ADSB_RATE 2000000 +#define ADSB_FREQ 1090000000 +#define DEFAULT_ASYNC_BUF_NUMBER 32 +#define DEFAULT_BUF_LENGTH (128 * 16384) +#define AUTO_GAIN -100 + +static pthread_t demod_thread; +static sem_t data_ready; +static volatile int do_exit = 0; +static rtlsdr_dev_t *dev = NULL; + +/* todo, bundle these up in a struct */ +uint8_t *buffer; +int raw_output = 0; +int short_output = 0; +int allowed_errors = 5; +FILE *file; +int adsb_frame[14]; +#define preamble_len 16 +#define long_frame 112 +#define short_frame 56 + +void usage(void) +{ + fprintf(stderr, + "rtl_adsb, a simple ADS-B decoder\n\n" + "Use:\trtl_adsb [-R] [-g gain] [-p ppm] [output file]\n" + "\t[-d device_index (default: 0)]\n" + "\t[-R output raw bitstream (default: off)]\n" + "\t[-S show short frames (default: off)]\n" + "\t[-e allowed_errors (default: 5)]\n" + "\t[-g tuner_gain (default: automatic)]\n" + "\t[-p ppm_error (default: 0)]\n" + "\tfilename (a '-' dumps samples to stdout)\n" + "\t (omitting the filename also uses stdout)\n\n" + "Streaming with netcat:\n" + "\trtl_adsb -R | netcat -lp 8080\n" + "\twhile true; do rtl_adsb -R | nc -lp 8080; done\n" + "\n"); + exit(1); +} + +#ifdef _WIN32 +BOOL WINAPI +sighandler(int signum) +{ + if (CTRL_C_EVENT == signum) { + fprintf(stderr, "Signal caught, exiting!\n"); + do_exit = 1; + rtlsdr_cancel_async(dev); + return TRUE; + } + return FALSE; +} +#else +static void sighandler(int signum) +{ + fprintf(stderr, "Signal caught, exiting!\n"); + do_exit = 1; + rtlsdr_cancel_async(dev); +} +#endif + +void display(int *frame, int len) +{ + int i; + if (!short_output && len <= short_frame) { + return;} + if (raw_output) { + fprintf(file, "*"); + for (i=0; i<((len+7)/8); i++) { + fprintf(file, "%02x", frame[i]);} + fprintf(file, ";\r\n"); + return; + } + fprintf(file, "----------\n"); + fprintf(file, "DF=%x CA=%x\n", (frame[0] >> 3) & 0x1f, frame[0] & 0x07); + fprintf(file, "ICAO Address=%06x\n", frame[1] << 16 | frame[2] << 8 | frame[3]); + if (len <= short_frame) { + return;} + fprintf(file, "PI=0x%06x\n", frame[11] << 16 | frame[12] << 8 | frame[13]); + fprintf(file, "Type Code=%x S.Type/Ant.=%x\n", (frame[4] >> 3) & 0x1f, frame[4] & 0x07); +} + +int magnitute(unsigned char *buf, int len) +/* takes i/q, changes buf in place, returns new len */ +{ + int i, mag; + for (i=0; i 255) { // todo, compression + mag = 255;} + buf[i/2] = (unsigned char)mag; + } + return len/2; +} + +inline unsigned char single_manchester(unsigned char a, unsigned char b, unsigned char c, unsigned char d) +/* takes 4 consecutive real samples, return 0 or 1, 255 on error */ +{ + int bit, bit_p; + bit_p = a > b; + bit = c > d; + if ( bit && bit_p && c > b && d < a) { + return 1;} + if ( bit && !bit_p && c > a && d < b) { + return 1;} + if (!bit && bit_p && c < a && d > b) { + return 0;} + if (!bit && !bit_p && c < b && d > a) { + return 0;} + return 255; +} + +inline unsigned char min(unsigned char a, unsigned char b) +{ + return ab ? a : b; +} + +inline int preamble(unsigned char *buf, int len, int i) +/* returns 0/1 for preamble at index i */ +{ + int i2; + unsigned char low = 0; + unsigned char high = 255; + for (i2=0; i2 allowed_errors) { + buf[i2] = 255; + break; + } else { + bit = 0; + a = 0; + b = 255; + } + } + buf[i] = buf[i+1] = 254; /* to be overwritten */ + buf[i2] = bit; + } + // todo, nuke short segments + //printf("%i\n", i2 - start); + } +} + +void messages(unsigned char *buf, int len) +{ + int i, i2, start, preamble_found; + int data_i, index, shift, frame_len; + // todo, allow wrap across buffers + for (i=0; i 1) { + continue;} + frame_len = long_frame; + data_i = 0; + for (index=0; index<14; index++) { + adsb_frame[index] = 0;} + for(; i= 0 ? r : -r; +} +