From 562fd7e8e3200fa876312e58427b3c9a7575d3fa Mon Sep 17 00:00:00 2001 From: Kyle Keen Date: Sun, 9 Dec 2012 17:41:20 -0500 Subject: rtl_adsb: prototype Signed-off-by: Steve Markgraf --- src/rtl_adsb.c | 463 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 463 insertions(+) create mode 100644 src/rtl_adsb.c (limited to 'src/rtl_adsb.c') 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; +} + -- cgit v1.2.3