/* * 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 #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 #endif #define ADSB_RATE 2000000 #define ADSB_FREQ 1090000000 #define DEFAULT_ASYNC_BUF_NUMBER 12 #define DEFAULT_BUF_LENGTH (16 * 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; /* look up table, could be made smaller */ uint8_t pyth[129][129]; /* todo, bundle these up in a struct */ uint8_t *buffer; int verbose_output = 0; int short_output = 0; double quality = 1.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[-V verbove output (default: off)]\n" "\t[-S show short frames (default: off)]\n" "\t[-Q quality (0: no sanity checks, 0.5: half bit, 1: one bit (default), 2: two bits)]\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 | netcat -lp 8080\n" "\twhile true; do rtl_adsb | nc -lp 8080; done\n" "Streaming with socat:\n" "\trtl_adsb | socat -u - TCP4:sdrsharp.com:47806\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, df; if (!short_output && len <= short_frame) { return;} df = (frame[0] >> 3) & 0x1f; if (quality == 0.0 && !(df==11 || df==17 || df==18 || df==19)) { return;} fprintf(file, "*"); for (i=0; i<((len+7)/8); i++) { fprintf(file, "%02x", frame[i]);} fprintf(file, ";\r\n"); if (!verbose_output) { return;} fprintf(file, "DF=%i CA=%i\n", df, 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=%i S.Type/Ant.=%x\n", (frame[4] >> 3) & 0x1f, frame[4] & 0x07); fprintf(file, "--------------\n"); } void pyth_precompute(void) { int x, y; for (x=0; x<129; x++) { for (y=0; y<129; y++) { pyth[x][y] = (uint8_t)round(sqrt(x*x + y*y)); }} } inline uint8_t abs8(uint8_t x) /* do not subtract 128 from the raw iq, this handles it */ { if (x >= 128) { return x - 128;} return 128 - x; } int magnitute(uint8_t *buf, int len) /* takes i/q, changes buf in place, returns new len */ { int i; for (i=0; i b; bit = c > d; if (quality == 0.0) { return bit;} if (quality == 0.5) { if ( bit && bit_p && b > c) { return 255;} if (!bit && !bit_p && b < c) { return 255;} return bit; } if (quality == 1.0) { if ( bit && bit_p && c > b) { return 1;} if ( bit && !bit_p && d < b) { return 1;} if (!bit && bit_p && d > b) { return 0;} if (!bit && !bit_p && c < b) { return 0;} return 255; } 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 uint8_t min8(uint8_t a, uint8_t b) { return ab ? a : b; } inline int preamble(uint8_t *buf, int len, int i) /* returns 0/1 for preamble at index i */ { int i2; uint8_t low = 0; uint8_t high = 255; for (i2=0; i2 allowed_errors) { buf[i2] = 255; break; } else { bit = a > b; /* these don't have to match the bit */ a = 0; b = 255; } } buf[i] = buf[i+1] = 254; /* to be overwritten */ buf[i2] = bit; } } } void messages(uint8_t *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; }