ads-b message decoding and beutification

bug_fixes_integration_tx
David Michaeli 2022-04-21 13:04:08 +03:00
rodzic 6a60ecbc31
commit ebdba93510
18 zmienionych plików z 1259 dodań i 576 usunięć

Wyświetl plik

@ -20,6 +20,7 @@ set(CMAKE_POSITION_INDEPENDENT_CODE ON)
add_executable(caribou_dump1090 add_executable(caribou_dump1090
dump1090.cpp dump1090.cpp
modes.c modes.c
cpr.c
) )
target_link_libraries(caribou_dump1090 SoapySDR iir_static) target_link_libraries(caribou_dump1090 SoapySDR iir_static)

Wyświetl plik

@ -93,12 +93,12 @@ clean: CMakeFiles/caribou_dump1090.dir/clean
CMakeFiles/caribou_dump1090.dir/all: CMakeFiles/caribou_dump1090.dir/all:
$(MAKE) $(MAKESILENT) -f CMakeFiles/caribou_dump1090.dir/build.make CMakeFiles/caribou_dump1090.dir/depend $(MAKE) $(MAKESILENT) -f CMakeFiles/caribou_dump1090.dir/build.make CMakeFiles/caribou_dump1090.dir/depend
$(MAKE) $(MAKESILENT) -f CMakeFiles/caribou_dump1090.dir/build.make CMakeFiles/caribou_dump1090.dir/build $(MAKE) $(MAKESILENT) -f CMakeFiles/caribou_dump1090.dir/build.make CMakeFiles/caribou_dump1090.dir/build
@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --progress-dir=/home/pi/projects/cariboulite/examples/cpp/build/CMakeFiles --progress-num=1,2,3 "Built target caribou_dump1090" @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --progress-dir=/home/pi/projects/cariboulite/examples/cpp/build/CMakeFiles --progress-num=1,2,3,4 "Built target caribou_dump1090"
.PHONY : CMakeFiles/caribou_dump1090.dir/all .PHONY : CMakeFiles/caribou_dump1090.dir/all
# Build rule for subdir invocation for target. # Build rule for subdir invocation for target.
CMakeFiles/caribou_dump1090.dir/rule: cmake_check_build_system CMakeFiles/caribou_dump1090.dir/rule: cmake_check_build_system
$(CMAKE_COMMAND) -E cmake_progress_start /home/pi/projects/cariboulite/examples/cpp/build/CMakeFiles 3 $(CMAKE_COMMAND) -E cmake_progress_start /home/pi/projects/cariboulite/examples/cpp/build/CMakeFiles 4
$(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 CMakeFiles/caribou_dump1090.dir/all $(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 CMakeFiles/caribou_dump1090.dir/all
$(CMAKE_COMMAND) -E cmake_progress_start /home/pi/projects/cariboulite/examples/cpp/build/CMakeFiles 0 $(CMAKE_COMMAND) -E cmake_progress_start /home/pi/projects/cariboulite/examples/cpp/build/CMakeFiles 0
.PHONY : CMakeFiles/caribou_dump1090.dir/rule .PHONY : CMakeFiles/caribou_dump1090.dir/rule

Wyświetl plik

@ -55,6 +55,6 @@ unistd.h
- -
math.h math.h
- -
sys/time.h time.h
- -

Wyświetl plik

@ -5,6 +5,7 @@ set(CMAKE_DEPENDS_LANGUAGES
) )
# The set of files for implicit dependencies of each language: # The set of files for implicit dependencies of each language:
set(CMAKE_DEPENDS_CHECK_C set(CMAKE_DEPENDS_CHECK_C
"/home/pi/projects/cariboulite/examples/cpp/cpr.c" "/home/pi/projects/cariboulite/examples/cpp/build/CMakeFiles/caribou_dump1090.dir/cpr.c.o"
"/home/pi/projects/cariboulite/examples/cpp/modes.c" "/home/pi/projects/cariboulite/examples/cpp/build/CMakeFiles/caribou_dump1090.dir/modes.c.o" "/home/pi/projects/cariboulite/examples/cpp/modes.c" "/home/pi/projects/cariboulite/examples/cpp/build/CMakeFiles/caribou_dump1090.dir/modes.c.o"
) )
set(CMAKE_C_COMPILER_ID "GNU") set(CMAKE_C_COMPILER_ID "GNU")

Wyświetl plik

@ -102,19 +102,34 @@ CMakeFiles/caribou_dump1090.dir/modes.c.s: cmake_force
@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --green "Compiling C source to assembly CMakeFiles/caribou_dump1090.dir/modes.c.s" @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --green "Compiling C source to assembly CMakeFiles/caribou_dump1090.dir/modes.c.s"
/usr/bin/cc $(C_DEFINES) $(C_INCLUDES) $(C_FLAGS) -S /home/pi/projects/cariboulite/examples/cpp/modes.c -o CMakeFiles/caribou_dump1090.dir/modes.c.s /usr/bin/cc $(C_DEFINES) $(C_INCLUDES) $(C_FLAGS) -S /home/pi/projects/cariboulite/examples/cpp/modes.c -o CMakeFiles/caribou_dump1090.dir/modes.c.s
CMakeFiles/caribou_dump1090.dir/cpr.c.o: CMakeFiles/caribou_dump1090.dir/flags.make
CMakeFiles/caribou_dump1090.dir/cpr.c.o: ../cpr.c
@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --green --progress-dir=/home/pi/projects/cariboulite/examples/cpp/build/CMakeFiles --progress-num=$(CMAKE_PROGRESS_3) "Building C object CMakeFiles/caribou_dump1090.dir/cpr.c.o"
/usr/bin/cc $(C_DEFINES) $(C_INCLUDES) $(C_FLAGS) -o CMakeFiles/caribou_dump1090.dir/cpr.c.o -c /home/pi/projects/cariboulite/examples/cpp/cpr.c
CMakeFiles/caribou_dump1090.dir/cpr.c.i: cmake_force
@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --green "Preprocessing C source to CMakeFiles/caribou_dump1090.dir/cpr.c.i"
/usr/bin/cc $(C_DEFINES) $(C_INCLUDES) $(C_FLAGS) -E /home/pi/projects/cariboulite/examples/cpp/cpr.c > CMakeFiles/caribou_dump1090.dir/cpr.c.i
CMakeFiles/caribou_dump1090.dir/cpr.c.s: cmake_force
@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --green "Compiling C source to assembly CMakeFiles/caribou_dump1090.dir/cpr.c.s"
/usr/bin/cc $(C_DEFINES) $(C_INCLUDES) $(C_FLAGS) -S /home/pi/projects/cariboulite/examples/cpp/cpr.c -o CMakeFiles/caribou_dump1090.dir/cpr.c.s
# Object files for target caribou_dump1090 # Object files for target caribou_dump1090
caribou_dump1090_OBJECTS = \ caribou_dump1090_OBJECTS = \
"CMakeFiles/caribou_dump1090.dir/dump1090.cpp.o" \ "CMakeFiles/caribou_dump1090.dir/dump1090.cpp.o" \
"CMakeFiles/caribou_dump1090.dir/modes.c.o" "CMakeFiles/caribou_dump1090.dir/modes.c.o" \
"CMakeFiles/caribou_dump1090.dir/cpr.c.o"
# External object files for target caribou_dump1090 # External object files for target caribou_dump1090
caribou_dump1090_EXTERNAL_OBJECTS = caribou_dump1090_EXTERNAL_OBJECTS =
caribou_dump1090: CMakeFiles/caribou_dump1090.dir/dump1090.cpp.o caribou_dump1090: CMakeFiles/caribou_dump1090.dir/dump1090.cpp.o
caribou_dump1090: CMakeFiles/caribou_dump1090.dir/modes.c.o caribou_dump1090: CMakeFiles/caribou_dump1090.dir/modes.c.o
caribou_dump1090: CMakeFiles/caribou_dump1090.dir/cpr.c.o
caribou_dump1090: CMakeFiles/caribou_dump1090.dir/build.make caribou_dump1090: CMakeFiles/caribou_dump1090.dir/build.make
caribou_dump1090: CMakeFiles/caribou_dump1090.dir/link.txt caribou_dump1090: CMakeFiles/caribou_dump1090.dir/link.txt
@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --green --bold --progress-dir=/home/pi/projects/cariboulite/examples/cpp/build/CMakeFiles --progress-num=$(CMAKE_PROGRESS_3) "Linking CXX executable caribou_dump1090" @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --green --bold --progress-dir=/home/pi/projects/cariboulite/examples/cpp/build/CMakeFiles --progress-num=$(CMAKE_PROGRESS_4) "Linking CXX executable caribou_dump1090"
$(CMAKE_COMMAND) -E cmake_link_script CMakeFiles/caribou_dump1090.dir/link.txt --verbose=$(VERBOSE) $(CMAKE_COMMAND) -E cmake_link_script CMakeFiles/caribou_dump1090.dir/link.txt --verbose=$(VERBOSE)
# Rule to build all files generated by this target. # Rule to build all files generated by this target.

Wyświetl plik

@ -1,4 +1,5 @@
file(REMOVE_RECURSE file(REMOVE_RECURSE
"CMakeFiles/caribou_dump1090.dir/cpr.c.o"
"CMakeFiles/caribou_dump1090.dir/dump1090.cpp.o" "CMakeFiles/caribou_dump1090.dir/dump1090.cpp.o"
"CMakeFiles/caribou_dump1090.dir/modes.c.o" "CMakeFiles/caribou_dump1090.dir/modes.c.o"
"caribou_dump1090" "caribou_dump1090"

Wyświetl plik

@ -1,6 +1,9 @@
# CMAKE generated file: DO NOT EDIT! # CMAKE generated file: DO NOT EDIT!
# Generated by "Unix Makefiles" Generator, CMake Version 3.18 # Generated by "Unix Makefiles" Generator, CMake Version 3.18
CMakeFiles/caribou_dump1090.dir/cpr.c.o
/home/pi/projects/cariboulite/examples/cpp/cpr.c
/home/pi/projects/cariboulite/examples/cpp/cpr.h
CMakeFiles/caribou_dump1090.dir/modes.c.o CMakeFiles/caribou_dump1090.dir/modes.c.o
/home/pi/projects/cariboulite/examples/cpp/modes.c /home/pi/projects/cariboulite/examples/cpp/modes.c
/home/pi/projects/cariboulite/examples/cpp/modes.h /home/pi/projects/cariboulite/examples/cpp/modes.h

Wyświetl plik

@ -1,6 +1,9 @@
# CMAKE generated file: DO NOT EDIT! # CMAKE generated file: DO NOT EDIT!
# Generated by "Unix Makefiles" Generator, CMake Version 3.18 # Generated by "Unix Makefiles" Generator, CMake Version 3.18
CMakeFiles/caribou_dump1090.dir/cpr.c.o: ../cpr.c
CMakeFiles/caribou_dump1090.dir/cpr.c.o: ../cpr.h
CMakeFiles/caribou_dump1090.dir/modes.c.o: ../modes.c CMakeFiles/caribou_dump1090.dir/modes.c.o: ../modes.c
CMakeFiles/caribou_dump1090.dir/modes.c.o: ../modes.h CMakeFiles/caribou_dump1090.dir/modes.c.o: ../modes.h

Wyświetl plik

@ -1 +1 @@
/usr/bin/c++ -O3 -DNDEBUG CMakeFiles/caribou_dump1090.dir/dump1090.cpp.o CMakeFiles/caribou_dump1090.dir/modes.c.o -o caribou_dump1090 -lSoapySDR -liir_static /usr/bin/c++ -O3 -DNDEBUG CMakeFiles/caribou_dump1090.dir/dump1090.cpp.o CMakeFiles/caribou_dump1090.dir/modes.c.o CMakeFiles/caribou_dump1090.dir/cpr.c.o -o caribou_dump1090 -lSoapySDR -liir_static

Wyświetl plik

@ -1,4 +1,5 @@
CMAKE_PROGRESS_1 = 1 CMAKE_PROGRESS_1 = 1
CMAKE_PROGRESS_2 = 2 CMAKE_PROGRESS_2 = 2
CMAKE_PROGRESS_3 = 3 CMAKE_PROGRESS_3 = 3
CMAKE_PROGRESS_4 = 4

Wyświetl plik

@ -142,6 +142,33 @@ caribou_dump1090/fast:
$(MAKE) $(MAKESILENT) -f CMakeFiles/caribou_dump1090.dir/build.make CMakeFiles/caribou_dump1090.dir/build $(MAKE) $(MAKESILENT) -f CMakeFiles/caribou_dump1090.dir/build.make CMakeFiles/caribou_dump1090.dir/build
.PHONY : caribou_dump1090/fast .PHONY : caribou_dump1090/fast
cpr.o: cpr.c.o
.PHONY : cpr.o
# target to build an object file
cpr.c.o:
$(MAKE) $(MAKESILENT) -f CMakeFiles/caribou_dump1090.dir/build.make CMakeFiles/caribou_dump1090.dir/cpr.c.o
.PHONY : cpr.c.o
cpr.i: cpr.c.i
.PHONY : cpr.i
# target to preprocess a source file
cpr.c.i:
$(MAKE) $(MAKESILENT) -f CMakeFiles/caribou_dump1090.dir/build.make CMakeFiles/caribou_dump1090.dir/cpr.c.i
.PHONY : cpr.c.i
cpr.s: cpr.c.s
.PHONY : cpr.s
# target to generate assembly for a file
cpr.c.s:
$(MAKE) $(MAKESILENT) -f CMakeFiles/caribou_dump1090.dir/build.make CMakeFiles/caribou_dump1090.dir/cpr.c.s
.PHONY : cpr.c.s
dump1090.o: dump1090.cpp.o dump1090.o: dump1090.cpp.o
.PHONY : dump1090.o .PHONY : dump1090.o
@ -205,6 +232,9 @@ help:
@echo "... edit_cache" @echo "... edit_cache"
@echo "... rebuild_cache" @echo "... rebuild_cache"
@echo "... caribou_dump1090" @echo "... caribou_dump1090"
@echo "... cpr.o"
@echo "... cpr.i"
@echo "... cpr.s"
@echo "... dump1090.o" @echo "... dump1090.o"
@echo "... dump1090.i" @echo "... dump1090.i"
@echo "... dump1090.s" @echo "... dump1090.s"

Plik binarny nie jest wyświetlany.

369
examples/cpp/cpr.c 100644
Wyświetl plik

@ -0,0 +1,369 @@
// Part of dump1090, a Mode S message decoder for RTLSDR devices.
//
// cpr.c - Compact Position Reporting decoder and tests
//
// Copyright (c) 2014,2015 Oliver Jowett <oliver@mutability.co.uk>
//
// This file is free software: you may copy, redistribute 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 file 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 <http://www.gnu.org/licenses/>.
// This file incorporates work covered by the following copyright and
// permission notice:
//
// Copyright (C) 2012 by Salvatore Sanfilippo <antirez@gmail.com>
//
// 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 "cpr.h"
#include <math.h>
#include <stdio.h>
//
//=========================================================================
//
// Always positive MOD operation, used for CPR decoding.
//
static int cprModInt(int a, int b) {
int res = a % b;
if (res < 0) res += b;
return res;
}
static double cprModDouble(double a, double b) {
double res = fmod(a, b);
if (res < 0) res += b;
return res;
}
//
//=========================================================================
//
// The NL function uses the precomputed table from 1090-WP-9-14
//
static int cprNLFunction(double lat) {
if (lat < 0) lat = -lat; // Table is simmetric about the equator
if (lat < 10.47047130) return 59;
if (lat < 14.82817437) return 58;
if (lat < 18.18626357) return 57;
if (lat < 21.02939493) return 56;
if (lat < 23.54504487) return 55;
if (lat < 25.82924707) return 54;
if (lat < 27.93898710) return 53;
if (lat < 29.91135686) return 52;
if (lat < 31.77209708) return 51;
if (lat < 33.53993436) return 50;
if (lat < 35.22899598) return 49;
if (lat < 36.85025108) return 48;
if (lat < 38.41241892) return 47;
if (lat < 39.92256684) return 46;
if (lat < 41.38651832) return 45;
if (lat < 42.80914012) return 44;
if (lat < 44.19454951) return 43;
if (lat < 45.54626723) return 42;
if (lat < 46.86733252) return 41;
if (lat < 48.16039128) return 40;
if (lat < 49.42776439) return 39;
if (lat < 50.67150166) return 38;
if (lat < 51.89342469) return 37;
if (lat < 53.09516153) return 36;
if (lat < 54.27817472) return 35;
if (lat < 55.44378444) return 34;
if (lat < 56.59318756) return 33;
if (lat < 57.72747354) return 32;
if (lat < 58.84763776) return 31;
if (lat < 59.95459277) return 30;
if (lat < 61.04917774) return 29;
if (lat < 62.13216659) return 28;
if (lat < 63.20427479) return 27;
if (lat < 64.26616523) return 26;
if (lat < 65.31845310) return 25;
if (lat < 66.36171008) return 24;
if (lat < 67.39646774) return 23;
if (lat < 68.42322022) return 22;
if (lat < 69.44242631) return 21;
if (lat < 70.45451075) return 20;
if (lat < 71.45986473) return 19;
if (lat < 72.45884545) return 18;
if (lat < 73.45177442) return 17;
if (lat < 74.43893416) return 16;
if (lat < 75.42056257) return 15;
if (lat < 76.39684391) return 14;
if (lat < 77.36789461) return 13;
if (lat < 78.33374083) return 12;
if (lat < 79.29428225) return 11;
if (lat < 80.24923213) return 10;
if (lat < 81.19801349) return 9;
if (lat < 82.13956981) return 8;
if (lat < 83.07199445) return 7;
if (lat < 83.99173563) return 6;
if (lat < 84.89166191) return 5;
if (lat < 85.75541621) return 4;
if (lat < 86.53536998) return 3;
if (lat < 87.00000000) return 2;
else return 1;
}
//
//=========================================================================
//
static int cprNFunction(double lat, int fflag) {
int nl = cprNLFunction(lat) - (fflag ? 1 : 0);
if (nl < 1) nl = 1;
return nl;
}
//
//=========================================================================
//
static double cprDlonFunction(double lat, int fflag, int surface) {
return (surface ? 90.0 : 360.0) / cprNFunction(lat, fflag);
}
//
//=========================================================================
//
// This algorithm comes from:
// http://www.lll.lu/~edward/edward/adsb/DecodingADSBposition.html.
//
// A few remarks:
// 1) 131072 is 2^17 since CPR latitude and longitude are encoded in 17 bits.
//
int decodeCPRairborne(int even_cprlat, int even_cprlon,
int odd_cprlat, int odd_cprlon,
int fflag,
double *out_lat, double *out_lon)
{
double AirDlat0 = 360.0 / 60.0;
double AirDlat1 = 360.0 / 59.0;
double lat0 = even_cprlat;
double lat1 = odd_cprlat;
double lon0 = even_cprlon;
double lon1 = odd_cprlon;
double rlat, rlon;
// Compute the Latitude Index "j"
int j = (int) floor(((59*lat0 - 60*lat1) / 131072) + 0.5);
double rlat0 = AirDlat0 * (cprModInt(j,60) + lat0 / 131072);
double rlat1 = AirDlat1 * (cprModInt(j,59) + lat1 / 131072);
if (rlat0 >= 270) rlat0 -= 360;
if (rlat1 >= 270) rlat1 -= 360;
// Check to see that the latitude is in range: -90 .. +90
if (rlat0 < -90 || rlat0 > 90 || rlat1 < -90 || rlat1 > 90)
return (-2); // bad data
// Check that both are in the same latitude zone, or abort.
if (cprNLFunction(rlat0) != cprNLFunction(rlat1))
return (-1); // positions crossed a latitude zone, try again later
// Compute ni and the Longitude Index "m"
if (fflag) { // Use odd packet.
int ni = cprNFunction(rlat1,1);
int m = (int) floor((((lon0 * (cprNLFunction(rlat1)-1)) -
(lon1 * cprNLFunction(rlat1))) / 131072.0) + 0.5);
rlon = cprDlonFunction(rlat1, 1, 0) * (cprModInt(m, ni)+lon1/131072);
rlat = rlat1;
} else { // Use even packet.
int ni = cprNFunction(rlat0,0);
int m = (int) floor((((lon0 * (cprNLFunction(rlat0)-1)) -
(lon1 * cprNLFunction(rlat0))) / 131072) + 0.5);
rlon = cprDlonFunction(rlat0, 0, 0) * (cprModInt(m, ni)+lon0/131072);
rlat = rlat0;
}
// Renormalize to -180 .. +180
rlon -= floor( (rlon + 180) / 360 ) * 360;
*out_lat = rlat;
*out_lon = rlon;
return 0;
}
int decodeCPRsurface(double reflat, double reflon,
int even_cprlat, int even_cprlon,
int odd_cprlat, int odd_cprlon,
int fflag,
double *out_lat, double *out_lon)
{
double AirDlat0 = 90.0 / 60.0;
double AirDlat1 = 90.0 / 59.0;
double lat0 = even_cprlat;
double lat1 = odd_cprlat;
double lon0 = even_cprlon;
double lon1 = odd_cprlon;
double rlon, rlat;
// Compute the Latitude Index "j"
int j = (int) floor(((59*lat0 - 60*lat1) / 131072) + 0.5);
double rlat0 = AirDlat0 * (cprModInt(j,60) + lat0 / 131072);
double rlat1 = AirDlat1 * (cprModInt(j,59) + lat1 / 131072);
// Pick the quadrant that's closest to the reference location -
// this is not necessarily the same quadrant that contains the
// reference location.
//
// There are also only two valid quadrants: -90..0 and 0..90;
// no correct message would try to encoding a latitude in the
// ranges -180..-90 and 90..180.
//
// If the computed latitude is more than 45 degrees north of
// the reference latitude (using the northern hemisphere
// solution), then the southern hemisphere solution will be
// closer to the refernce latitude.
//
// e.g. reflat=0, rlat=44, use rlat=44
// reflat=0, rlat=46, use rlat=46-90 = -44
// reflat=40, rlat=84, use rlat=84
// reflat=40, rlat=86, use rlat=86-90 = -4
// reflat=-40, rlat=4, use rlat=4
// reflat=-40, rlat=6, use rlat=6-90 = -84
// As a special case, -90, 0 and +90 all encode to zero, so
// there's a little extra work to do there.
if (rlat0 == 0) {
if (reflat < -45)
rlat0 = -90;
else if (reflat > 45)
rlat0 = 90;
} else if ((rlat0 - reflat) > 45) {
rlat0 -= 90;
}
if (rlat1 == 0) {
if (reflat < -45)
rlat1 = -90;
else if (reflat > 45)
rlat1 = 90;
} else if ((rlat1 - reflat) > 45) {
rlat1 -= 90;
}
// Check to see that the latitude is in range: -90 .. +90
if (rlat0 < -90 || rlat0 > 90 || rlat1 < -90 || rlat1 > 90)
return (-2); // bad data
// Check that both are in the same latitude zone, or abort.
if (cprNLFunction(rlat0) != cprNLFunction(rlat1))
return (-1); // positions crossed a latitude zone, try again later
// Compute ni and the Longitude Index "m"
if (fflag) { // Use odd packet.
int ni = cprNFunction(rlat1,1);
int m = (int) floor((((lon0 * (cprNLFunction(rlat1)-1)) -
(lon1 * cprNLFunction(rlat1))) / 131072.0) + 0.5);
rlon = cprDlonFunction(rlat1, 1, 1) * (cprModInt(m, ni)+lon1/131072);
rlat = rlat1;
} else { // Use even packet.
int ni = cprNFunction(rlat0,0);
int m = (int) floor((((lon0 * (cprNLFunction(rlat0)-1)) -
(lon1 * cprNLFunction(rlat0))) / 131072) + 0.5);
rlon = cprDlonFunction(rlat0, 0, 1) * (cprModInt(m, ni)+lon0/131072);
rlat = rlat0;
}
// Pick the quadrant that's closest to the reference location -
// this is not necessarily the same quadrant that contains the
// reference location. Unlike the latitude case, all four
// quadrants are valid.
// if reflon is more than 45 degrees away, move some multiple of 90 degrees towards it
rlon += floor( (reflon - rlon + 45) / 90 ) * 90; // this might move us outside (-180..+180), we fix this below
// Renormalize to -180 .. +180
rlon -= floor( (rlon + 180) / 360 ) * 360;
*out_lat = rlat;
*out_lon = rlon;
return 0;
}
//
//=========================================================================
//
// This algorithm comes from:
// 1090-WP29-07-Draft_CPR101 (which also defines decodeCPR() )
//
// Despite what the earlier comment here said, we should *not* be using trunc().
// See Figure 5-5 / 5-6 and note that floor is applied to (0.5 + fRP - fEP), not
// directly to (fRP - fEP). Eq 38 is correct.
//
int decodeCPRrelative(double reflat, double reflon,
int cprlat, int cprlon,
int fflag, int surface,
double *out_lat, double *out_lon)
{
double AirDlat;
double AirDlon;
double fractional_lat = cprlat / 131072.0;
double fractional_lon = cprlon / 131072.0;
double rlon, rlat;
int j,m;
AirDlat = (surface ? 90.0 : 360.0) / (fflag ? 59.0 : 60.0);
// Compute the Latitude Index "j"
j = (int) (floor(reflat/AirDlat) +
floor(0.5 + cprModDouble(reflat, AirDlat)/AirDlat - fractional_lat));
rlat = AirDlat * (j + fractional_lat);
if (rlat >= 270) rlat -= 360;
// Check to see that the latitude is in range: -90 .. +90
if (rlat < -90 || rlat > 90) {
return (-1); // Time to give up - Latitude error
}
// Check to see that answer is reasonable - ie no more than 1/2 cell away
if (fabs(rlat - reflat) > (AirDlat/2)) {
return (-1); // Time to give up - Latitude error
}
// Compute the Longitude Index "m"
AirDlon = cprDlonFunction(rlat, fflag, surface);
m = (int) (floor(reflon/AirDlon) +
floor(0.5 + cprModDouble(reflon, AirDlon)/AirDlon - fractional_lon));
rlon = AirDlon * (m + fractional_lon);
if (rlon > 180) rlon -= 360;
// Check to see that answer is reasonable - ie no more than 1/2 cell away
if (fabs(rlon - reflon) > (AirDlon/2))
return (-1); // Time to give up - Longitude error
*out_lat = rlat;
*out_lon = rlon;
return (0);
}

39
examples/cpp/cpr.h 100644
Wyświetl plik

@ -0,0 +1,39 @@
// Part of dump1090, a Mode S message decoder for RTLSDR devices.
//
// cpr.h - Compact Position Reporting prototypes
//
// Copyright (c) 2014,2015 Oliver Jowett <oliver@mutability.co.uk>
//
// This file is free software: you may copy, redistribute 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 file 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 <http://www.gnu.org/licenses/>.
#ifndef DUMP1090_CPR_H
#define DUMP1090_CPR_H
int decodeCPRairborne(int even_cprlat, int even_cprlon,
int odd_cprlat, int odd_cprlon,
int fflag,
double *out_lat, double *out_lon);
int decodeCPRsurface(double reflat, double reflon,
int even_cprlat, int even_cprlon,
int odd_cprlat, int odd_cprlon,
int fflag,
double *out_lat, double *out_lon);
int decodeCPRrelative(double reflat, double reflon,
int cprlat, int cprlon,
int fflag, int surface,
double *out_lat, double *out_lon);
#endif

Wyświetl plik

@ -19,9 +19,7 @@
#include "modes.h" #include "modes.h"
/*********************************************************************** //=================================================================================
* Print the banner
**********************************************************************/
static void printBanner(void) static void printBanner(void)
{ {
std::cout << "######################################################" << std::endl; std::cout << "######################################################" << std::endl;
@ -30,86 +28,21 @@ static void printBanner(void)
std::cout << std::endl; std::cout << std::endl;
} }
/*********************************************************************** //=================================================================================
* Find devices and print args
**********************************************************************/
static int findDevices(const std::string &argStr)
{
const auto results = SoapySDR::Device::enumerate(argStr);
std::cout << "Found " << results.size() << " devices" << std::endl;
for (size_t i = 0; i < results.size(); i++)
{
std::cout << "Found device " << i << std::endl;
for (const auto &it : results[i])
{
std::cout << " " << it.first << " = " << it.second << std::endl;
}
std::cout << std::endl;
}
if (results.empty()) std::cerr << "No devices found! " << argStr << std::endl;
else std::cout << std::endl;
return results.empty()?EXIT_FAILURE:EXIT_SUCCESS;
}
/***********************************************************************
* Run the stream function
**********************************************************************/
static sig_atomic_t loopDone = false; static sig_atomic_t loopDone = false;
static void sigIntHandler(const int) static void sigIntHandler(const int)
{ {
loopDone = true; loopDone = true;
} }
//=================================================================================
void onModeSMessage(mode_s_t *self, struct mode_s_msg *mm) void onModeSMessage(mode_s_t *self, struct mode_s_msg *mm)
{ {
printf("Got message from flight %s\n", mm->flight); mode_s_display_message(mm);
printf("\n");
printf(" HEADER: #Bits: %d, Type: %d, CRCOK: %d, ECC: %d, ICAO_ADDR:%02X%02X%02X, PHC: %d, CAP: %08X\n",
mm->msgbits, // Number of bits in message
mm->msgtype, // Downlink format #
mm->crcok, // True if CRC was valid
mm->errorbit, // Bit corrected. -1 if no bit corrected.
mm->aa1, mm->aa2, mm->aa3, // ICAO Address bytes 1 2 and 3
mm->phase_corrected, // True if phase correction was applied.
mm->ca); // Responder capabilities.
printf(" DATA1: MsgTp: %d, MsgSTp: %d, HdVld: %d, Hd: %d, AirCftTp:%d, FFlg: %d, UtcSync: %d, Lat: %d, Lon: %d, Dir W/S: %d/%d\n"
" DATA2: Vel (W/S): %d/%d, Vel: %d, VertRt (Src/Sgn/Rt): %d/%d/%d, Alt: %d, Stat: %d, Id: %08X, Unt: %d\n\n",
mm->metype, // Extended squitter message type.
mm->mesub, // Extended squitter message subtype.
mm->heading_is_valid, // heading_is_valid
mm->heading, // heading
mm->aircraft_type, // aircraft_type
mm->fflag, // 1 = Odd, 0 = Even CPR message.
mm->tflag, // UTC synchronized?
mm->raw_latitude, // Non decoded latitude
mm->raw_longitude, // Non decoded raw_longitude
mm->ew_dir, // 0 = East, 1 = West.
mm->ns_dir, // 0 = North, 1 = South.
mm->ew_velocity, // E/W velocity.
mm->ns_velocity, // N/S velocity.
mm->velocity,
mm->vert_rate_source, // Vertical rate source.
mm->vert_rate_sign, // Vertical rate sign.
mm->vert_rate, // // Vertical rate.
mm->altitude,
mm->fs, // Flight status for DF4,5,20,21
mm->identity, // 13 bits identity (Squawk).
mm->unit);
}
#define FILT_ORDER 6
Iir::Butterworth::LowPass<FILT_ORDER> filt;
void makeFilter(double fs, double cutoff)
{
try {filt.setup(fs, cutoff);}
catch (...) {printf("Filter Setup Exception!\n");}
} }
//=================================================================================
// Turn I/Q samples pointed by `data` into the magnitude vector pointed by `mag` // Turn I/Q samples pointed by `data` into the magnitude vector pointed by `mag`
void MagnitudeVectorDownSample(short *data, uint16_t *mag, uint32_t size) void MagnitudeVectorDownSample(short *data, uint16_t *mag, uint32_t size)
{ {
@ -126,11 +59,9 @@ void MagnitudeVectorDownSample(short *data, uint16_t *mag, uint32_t size)
} }
} }
void runSoapyProcess( //=================================================================================
SoapySDR::Device *device, void runSoapyProcess( SoapySDR::Device *device,
SoapySDR::Stream *stream, SoapySDR::Stream *stream,
const int direction,
const size_t numChans,
const size_t elemSize) const size_t elemSize)
{ {
// allocate buffers for the stream read/write // allocate buffers for the stream read/write
@ -138,38 +69,40 @@ void runSoapyProcess(
int16_t* buff = (int16_t*)malloc(2*sizeof(int16_t)*numElems); // complex 16 bit samples int16_t* buff = (int16_t*)malloc(2*sizeof(int16_t)*numElems); // complex 16 bit samples
uint16_t* mag = (uint16_t*)malloc(sizeof(uint16_t)*numElems); uint16_t* mag = (uint16_t*)malloc(sizeof(uint16_t)*numElems);
// MODE-S Stuff // MODE-S
mode_s_t state; mode_s_t state;
mode_s_init(&state); mode_s_init(&state);
makeFilter(4e6, 50e3);
std::cout << "Starting stream loop, press Ctrl+C to exit..." << std::endl; std::cout << "Starting stream loop, press Ctrl+C to exit..." << std::endl;
device->activateStream(stream); device->activateStream(stream);
signal(SIGINT, sigIntHandler); signal(SIGINT, sigIntHandler);
// Main Processing Loop
while (not loopDone) while (not loopDone)
{ {
int ret = 0;
int flags = 0;
long long timeUS = numElems; long long timeUS = numElems;
ret = device->readStream(stream, (void* const*)&buff, numElems, flags, timeUS); int flags = 0;
int ret = device->readStream(stream, (void* const*)&buff, numElems, flags, timeUS);
if (ret == SOAPY_SDR_TIMEOUT) continue;
if (ret == SOAPY_SDR_OVERFLOW)
{
//overflows++;
continue;
}
if (ret == SOAPY_SDR_UNDERFLOW)
{
//underflows++;
continue;
}
if (ret < 0) if (ret < 0)
{ {
std::cerr << "Unexpected stream error " << ret << std::endl; std::cerr << "Unexpected stream error " << ret << std::endl;
break; break;
} }
switch (ret)
{
case SOAPY_SDR_TIMEOUT: continue;
case SOAPY_SDR_OVERFLOW:
//overflows++;
continue;
case SOAPY_SDR_UNDERFLOW:
//underflows++;
continue;
default:
break;
}
// All is good - proceed to DSP
// compute the magnitude of the signal // compute the magnitude of the signal
MagnitudeVectorDownSample(buff, mag, ret); MagnitudeVectorDownSample(buff, mag, ret);
@ -178,6 +111,10 @@ void runSoapyProcess(
} }
device->deactivateStream(stream); device->deactivateStream(stream);
// free memory
free(buff);
free(mag);
} }
@ -187,38 +124,35 @@ void runSoapyProcess(
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
SoapySDR::ModuleManager mm(false); SoapySDR::ModuleManager mm(false);
SoapySDR::Device *device(nullptr); SoapySDR::Device *device(nullptr);
std::vector<size_t> channels; std::vector<size_t> channels;
std::string argStr = "driver=Cariboulite,channel=HiF"; std::string argStr = "driver=Cariboulite,channel=HiF";
double fullScale = 0.0; double fullScale = 0.0;
double freq = 1090.0e6;
//double freq = 1090e6;
printBanner(); printBanner();
try try
{ {
device = SoapySDR::Device::make(argStr); device = SoapySDR::Device::make(argStr);
channels.push_back(0);
// set the sample rate // set the sample rate, frequency, ...
device->setSampleRate(SOAPY_SDR_RX, channels[0], 4e6); device->setSampleRate(SOAPY_SDR_RX, 0, 4e6);
device->setBandwidth(SOAPY_SDR_RX, channels[0], 2500e5); device->setBandwidth(SOAPY_SDR_RX, 0, 100e5);
device->setGainMode(SOAPY_SDR_RX, channels[0], false); device->setGainMode(SOAPY_SDR_RX, 0, false);
device->setGain(SOAPY_SDR_RX, channels[0], 50); device->setGain(SOAPY_SDR_RX, 0, 50);
device->setFrequency(SOAPY_SDR_RX, channels[0], freq); device->setFrequency(SOAPY_SDR_RX, 0, 1090e6);
// create the stream, use the native format // create the stream, use the native format
const auto format = device->getNativeStreamFormat(SOAPY_SDR_RX, channels.front(), fullScale); const auto format = device->getNativeStreamFormat(SOAPY_SDR_RX, 0, fullScale);
const size_t elemSize = SoapySDR::formatToSize(format); const size_t elemSize = SoapySDR::formatToSize(format);
auto stream = device->setupStream(SOAPY_SDR_RX, format, channels); auto stream = device->setupStream(SOAPY_SDR_RX, format, channels);
// run the rate test one setup is complete // run the rate test one setup is complete
std::cout << "Running Soapy process with CaribouLite Config:" << std::endl;
std::cout << " Stream format: " << format << std::endl; std::cout << " Stream format: " << format << std::endl;
std::cout << "Num channels: " << channels.size() << std::endl; std::cout << " Channel: HiF" << std::endl;
std::cout << "Element size: " << elemSize << " bytes" << std::endl; std::cout << " Sample size: " << elemSize << " bytes" << std::endl;
runSoapyProcess(device, stream, SOAPY_SDR_RX, channels.size(), elemSize); runSoapyProcess(device, stream, elemSize);
// cleanup stream and device // cleanup stream and device
device->closeStream(stream); device->closeStream(stream);
@ -232,6 +166,5 @@ int main(int argc, char *argv[])
} }
std::cout << std::endl; std::cout << std::endl;
return 0; return 0;
} }

Wyświetl plik

@ -1,25 +1,219 @@
/* /*
* Source of file: https://github.com/watson/libmodes * Modified source file by David Michaeli @ CaribouLabs Ltd. Apr. 2022
* Based on source of file: https://github.com/watson/libmodes
* Author: Thomas Watson (@watson) * Author: Thomas Watson (@watson)
* Contact: w@tson.dk / https://twitter.com/wa7son * Contact: w@tson.dk / https://twitter.com/wa7son
* License: BSD-2-Clause * License: BSD-2-Clause
*/ */
#include "modes.h" #include "modes.h"
#include <stdio.h>
// ==========================================================================
#define MODE_S_PREAMBLE_US 8 // microseconds #define MODE_S_PREAMBLE_US 8 // microseconds
#define MODE_S_LONG_MSG_BITS 112 #define MODE_S_LONG_MSG_BITS 112
#define MODE_S_SHORT_MSG_BITS 56 #define MODE_S_SHORT_MSG_BITS 56
#define MODE_S_FULL_LEN (MODE_S_PREAMBLE_US+MODE_S_LONG_MSG_BITS) #define MODE_S_FULL_LEN (MODE_S_PREAMBLE_US+MODE_S_LONG_MSG_BITS)
#define MODE_S_UNIT_METERS 1
#define MODE_S_ICAO_CACHE_TTL 60 // Time to live of cached addresses. #define MODE_S_ICAO_CACHE_TTL 60 // Time to live of cached addresses.
static uint16_t maglut[129*129*2]; static uint16_t maglut[129*129*2];
static int maglut_initialized = 0; static int maglut_initialized = 0;
// =============================== Initialization =========================== // ==========================================================================
// Capability table
char *ca_str[8] =
{
/* 0 */ "Level 1 (Survillance Only)",
/* 1 */ "Level 2 (DF0,4,5,11)",
/* 2 */ "Level 3 (DF0,4,5,11,20,21)",
/* 3 */ "Level 4 (DF0,4,5,11,20,21,24)",
/* 4 */ "Level 2+3+4 (DF0,4,5,11,20,21,24,code7 - is on ground)",
/* 5 */ "Level 2+3+4 (DF0,4,5,11,20,21,24,code7 - is on airborne)",
/* 6 */ "Level 2+3+4 (DF0,4,5,11,20,21,24,code7)",
/* 7 */ "Level 7 ???"
};
void mode_s_init(mode_s_t *self) { // ==========================================================================
// Flight status table
char *fs_str[8] =
{
/* 0 */ "Normal, Airborne",
/* 1 */ "Normal, On the ground",
/* 2 */ "ALERT, Airborne",
/* 3 */ "ALERT, On the ground",
/* 4 */ "ALERT & Special Position Identification. Airborne or Ground",
/* 5 */ "Special Position Identification. Airborne or Ground",
/* 6 */ "Value 6 is not assigned",
/* 7 */ "Value 7 is not assigned"
};
// ==========================================================================
// ME message type to description table.
char *me_str[] =
{
};
// ==========================================================================
char *getMEDescription(int metype, int mesub)
{
char *mename = "Unknown";
if (metype >= 1 && metype <= 4)
mename = "Aircraft Identification and Category";
else if (metype >= 5 && metype <= 8)
mename = "Surface Position";
else if (metype >= 9 && metype <= 18)
mename = "Airborne Position (Baro Altitude)";
else if (metype == 19 && mesub >=1 && mesub <= 4)
mename = "Airborne Velocity";
else if (metype >= 20 && metype <= 22)
mename = "Airborne Position (GNSS Height)";
else if (metype == 23 && mesub == 0)
mename = "Test Message";
else if (metype == 24 && mesub == 1)
mename = "Surface System Status";
else if (metype == 28 && mesub == 1)
mename = "Extended Squitter Aircraft Status (Emergency)";
else if (metype == 28 && mesub == 2)
mename = "Extended Squitter Aircraft Status (1090ES TCAS RA)";
else if (metype == 29 && (mesub == 0 || mesub == 1))
mename = "Target State and Status Message";
else if (metype == 31 && (mesub == 0 || mesub == 1))
mename = "Aircraft Operational Status Message";
return mename;
}
// ==========================================================================
// This function gets a decoded Mode S Message and prints it on the screen
// in a human readable format
void mode_s_display_message(struct mode_s_msg *mm)
{
int j;
/* Show the raw message. */
printf("*");
for (j = 0; j < mm->msgbits/8; j++)
{
printf("%02x", mm->msg[j]);
}
printf(";\n");
printf("CRC: %06x (%s)\n", (int)mm->crc, mm->crcok ? "ok" : "wrong");
if (mm->errorbit != -1)
{
printf("Single bit error fixed, bit %d\n", mm->errorbit);
}
if (mm->msgtype == 0)
{
/* DF 0 */
printf("DF 0: Short Air-Air Surveillance.\n");
printf(" Altitude : %d %s\n", mm->altitude,
(mm->unit == MODE_S_UNIT_METERS) ? "meters" : "feet");
printf(" ICAO Address : %02x%02x%02x\n", mm->aa1, mm->aa2, mm->aa3);
}
else if (mm->msgtype == 4 || mm->msgtype == 20)
{
printf("DF %d: %s, Altitude Reply.\n", mm->msgtype, (mm->msgtype == 4) ? "Surveillance" : "Comm-B");
printf(" Flight Status : %s\n", fs_str[mm->fs]);
printf(" DR : %d\n", mm->dr);
printf(" UM : %d\n", mm->um);
printf(" Altitude : %d %s\n", mm->altitude, (mm->unit == MODE_S_UNIT_METERS) ? "meters" : "feet");
printf(" ICAO Address : %02x%02x%02x\n", mm->aa1, mm->aa2, mm->aa3);
if (mm->msgtype == 20)
{
/* TODO: 56 bits DF20 MB additional field. */
}
}
else if (mm->msgtype == 5 || mm->msgtype == 21)
{
printf("DF %d: %s, Identity Reply.\n", mm->msgtype, (mm->msgtype == 5) ? "Surveillance" : "Comm-B");
printf(" Flight Status : %s\n", fs_str[mm->fs]);
printf(" DR : %d\n", mm->dr);
printf(" UM : %d\n", mm->um);
printf(" Squawk : %d\n", mm->identity);
printf(" ICAO Address : %02x%02x%02x\n", mm->aa1, mm->aa2, mm->aa3);
if (mm->msgtype == 21) {
/* TODO: 56 bits DF21 MB additional field. */
}
}
else if (mm->msgtype == 11)
{
/* DF 11 */
printf("DF 11: All Call Reply.\n");
printf(" Capability : %s\n", ca_str[mm->ca]);
printf(" ICAO Address: %02x%02x%02x\n", mm->aa1, mm->aa2, mm->aa3);
}
else if (mm->msgtype == 17)
{
/* DF 17 */
printf("DF 17: ADS-B message.\n");
printf(" Capability : %d (%s)\n", mm->ca, ca_str[mm->ca]);
printf(" ICAO Address : %02x%02x%02x\n", mm->aa1, mm->aa2, mm->aa3);
printf(" Extended Squitter Type: %d\n", mm->metype);
printf(" Extended Squitter Sub : %d\n", mm->mesub);
printf(" Extended Squitter Name: %s\n", getMEDescription(mm->metype,mm->mesub));
/* Decode the extended squitter message. */
if (mm->metype >= 1 && mm->metype <= 4)
{
/* Aircraft identification. */
char *ac_type_str[4] =
{
"Aircraft Type D",
"Aircraft Type C",
"Aircraft Type B",
"Aircraft Type A"
};
printf(" Aircraft Type : %s\n", ac_type_str[mm->aircraft_type]);
printf(" Identification : %s\n", mm->flight);
}
else if (mm->metype >= 9 && mm->metype <= 18)
{
printf(" F flag : %s\n", mm->fflag ? "odd" : "even");
printf(" T flag : %s\n", mm->tflag ? "UTC" : "non-UTC");
printf(" Altitude : %d feet\n", mm->altitude);
printf(" Latitude : %d (not decoded)\n", mm->raw_latitude);
printf(" Longitude: %d (not decoded)\n", mm->raw_longitude);
}
else if (mm->metype == 19 && mm->mesub >= 1 && mm->mesub <= 4)
{
if (mm->mesub == 1 || mm->mesub == 2)
{
/* Velocity */
printf(" EW direction : %d\n", mm->ew_dir);
printf(" EW velocity : %d\n", mm->ew_velocity);
printf(" NS direction : %d\n", mm->ns_dir);
printf(" NS velocity : %d\n", mm->ns_velocity);
printf(" Vertical rate src : %d\n", mm->vert_rate_source);
printf(" Vertical rate sign: %d\n", mm->vert_rate_sign);
printf(" Vertical rate : %d\n", mm->vert_rate);
}
else if (mm->mesub == 3 || mm->mesub == 4)
{
printf(" Heading status: %d", mm->heading_is_valid);
printf(" Heading: %d", mm->heading);
}
}
else
{
printf(" Unrecognized ME type: %d subtype: %d\n", mm->metype, mm->mesub);
}
}
else
{
printf("DF %d with good CRC received (decoding still not implemented).\n",
mm->msgtype);
}
}
// =============================== Initialization ===========================
void mode_s_init(mode_s_t *self)
{
int i, q; int i, q;
self->fix_errors = 1; self->fix_errors = 1;
@ -36,9 +230,12 @@ void mode_s_init(mode_s_t *self) {
// We scale to 0-255 range multiplying by 1.4 in order to ensure that every // We scale to 0-255 range multiplying by 1.4 in order to ensure that every
// different I/Q pair will result in a different magnitude value, not losing // different I/Q pair will result in a different magnitude value, not losing
// any resolution. // any resolution.
if (!maglut_initialized) { if (!maglut_initialized)
for (i = 0; i <= 128; i++) { {
for (q = 0; q <= 128; q++) { for (i = 0; i <= 128; i++)
{
for (q = 0; q <= 128; q++)
{
maglut[i*129+q] = round(sqrt(i*i+q*q)*360); maglut[i*129+q] = round(sqrt(i*i+q*q)*360);
} }
} }
@ -47,7 +244,6 @@ void mode_s_init(mode_s_t *self) {
} }
// ===================== Mode S detection and decoding ===================== // ===================== Mode S detection and decoding =====================
// Parity table for MODE S Messages. // Parity table for MODE S Messages.
// //
// The table contains 112 elements, every element corresponds to a bit set in // The table contains 112 elements, every element corresponds to a bit set in
@ -65,7 +261,8 @@ void mode_s_init(mode_s_t *self) {
// Note: this function can be used with DF11 and DF17, other modes have the CRC // Note: this function can be used with DF11 and DF17, other modes have the CRC
// xored with the sender address as they are reply to interrogations, but a // xored with the sender address as they are reply to interrogations, but a
// casual listener can't split the address from the checksum. // casual listener can't split the address from the checksum.
uint32_t mode_s_checksum_table[] = { uint32_t mode_s_checksum_table[] =
{
0x3935ea, 0x1c9af5, 0xf1b77e, 0x78dbbf, 0xc397db, 0x9e31e9, 0xb0e2f0, 0x587178, 0x3935ea, 0x1c9af5, 0xf1b77e, 0x78dbbf, 0xc397db, 0x9e31e9, 0xb0e2f0, 0x587178,
0x2c38bc, 0x161c5e, 0x0b0e2f, 0xfa7d13, 0x82c48d, 0xbe9842, 0x5f4c21, 0xd05c14, 0x2c38bc, 0x161c5e, 0x0b0e2f, 0xfa7d13, 0x82c48d, 0xbe9842, 0x5f4c21, 0xd05c14,
0x682e0a, 0x341705, 0xe5f186, 0x72f8c3, 0xc68665, 0x9cb936, 0x4e5c9b, 0xd8d449, 0x682e0a, 0x341705, 0xe5f186, 0x72f8c3, 0xc68665, 0x9cb936, 0x4e5c9b, 0xd8d449,
@ -82,23 +279,26 @@ uint32_t mode_s_checksum_table[] = {
0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000
}; };
uint32_t mode_s_checksum(unsigned char *msg, int bits) { // ==========================================================================
uint32_t mode_s_checksum(unsigned char *msg, int bits)
{
uint32_t crc = 0; uint32_t crc = 0;
int offset = (bits == 112) ? 0 : (112-56); int offset = (bits == 112) ? 0 : (112-56);
int j; int j;
for(j = 0; j < bits; j++) { for(j = 0; j < bits; j++)
{
int byte = j/8; int byte = j/8;
int bit = j%8; int bit = j%8;
int bitmask = 1 << (7-bit); int bitmask = 1 << (7-bit);
// If bit is set, xor with corresponding table entry. // If bit is set, xor with corresponding table entry.
if (msg[byte] & bitmask) if (msg[byte] & bitmask) crc ^= mode_s_checksum_table[j+offset];
crc ^= mode_s_checksum_table[j+offset];
} }
return crc; // 24 bit checksum. return crc; // 24 bit checksum.
} }
// ==========================================================================
// Given the Downlink Format (DF) of the message, return the message length in // Given the Downlink Format (DF) of the message, return the message length in
// bits. // bits.
int mode_s_msg_len_by_type(int type) int mode_s_msg_len_by_type(int type)
@ -113,14 +313,17 @@ int mode_s_msg_len_by_type(int type)
return MODE_S_SHORT_MSG_BITS; return MODE_S_SHORT_MSG_BITS;
} }
// ==========================================================================
// Try to fix single bit errors using the checksum. On success modifies the // Try to fix single bit errors using the checksum. On success modifies the
// original buffer with the fixed version, and returns the position of the // original buffer with the fixed version, and returns the position of the
// error bit. Otherwise if fixing failed -1 is returned. // error bit. Otherwise if fixing failed -1 is returned.
int fix_single_bit_errors(unsigned char *msg, int bits) { int fix_single_bit_errors(unsigned char *msg, int bits)
{
int j; int j;
unsigned char aux[MODE_S_LONG_MSG_BITS/8]; unsigned char aux[MODE_S_LONG_MSG_BITS/8];
for (j = 0; j < bits; j++) { for (j = 0; j < bits; j++)
{
int byte = j/8; int byte = j/8;
int bitmask = 1 << (7-(j%8)); int bitmask = 1 << (7-(j%8));
uint32_t crc1, crc2; uint32_t crc1, crc2;
@ -133,7 +336,8 @@ int fix_single_bit_errors(unsigned char *msg, int bits) {
(uint32_t)aux[(bits/8)-1]; (uint32_t)aux[(bits/8)-1];
crc2 = mode_s_checksum(aux, bits); crc2 = mode_s_checksum(aux, bits);
if (crc1 == crc2) { if (crc1 == crc2)
{
// The error is fixed. Overwrite the original buffer with the // The error is fixed. Overwrite the original buffer with the
// corrected sequence, and returns the error bit position. // corrected sequence, and returns the error bit position.
memcpy(msg, aux, bits/8); memcpy(msg, aux, bits/8);
@ -143,19 +347,23 @@ int fix_single_bit_errors(unsigned char *msg, int bits) {
return -1; return -1;
} }
// ==========================================================================
// Similar to fix_single_bit_errors() but try every possible two bit // Similar to fix_single_bit_errors() but try every possible two bit
// combination. This is very slow and should be tried only against DF17 // combination. This is very slow and should be tried only against DF17
// messages that don't pass the checksum, and only in Aggressive Mode. // messages that don't pass the checksum, and only in Aggressive Mode.
int fix_two_bits_errors(unsigned char *msg, int bits) { int fix_two_bits_errors(unsigned char *msg, int bits)
{
int j, i; int j, i;
unsigned char aux[MODE_S_LONG_MSG_BITS/8]; unsigned char aux[MODE_S_LONG_MSG_BITS/8];
for (j = 0; j < bits; j++) { for (j = 0; j < bits; j++)
{
int byte1 = j/8; int byte1 = j/8;
int bitmask1 = 1 << (7-(j%8)); int bitmask1 = 1 << (7-(j%8));
// Don't check the same pairs multiple times, so i starts from j+1 // Don't check the same pairs multiple times, so i starts from j+1
for (i = j+1; i < bits; i++) { for (i = j+1; i < bits; i++)
{
int byte2 = i/8; int byte2 = i/8;
int bitmask2 = 1 << (7-(i%8)); int bitmask2 = 1 << (7-(i%8));
uint32_t crc1, crc2; uint32_t crc1, crc2;
@ -170,10 +378,12 @@ int fix_two_bits_errors(unsigned char *msg, int bits) {
(uint32_t)aux[(bits/8)-1]; (uint32_t)aux[(bits/8)-1];
crc2 = mode_s_checksum(aux, bits); crc2 = mode_s_checksum(aux, bits);
if (crc1 == crc2) { if (crc1 == crc2)
{
// The error is fixed. Overwrite the original buffer with the // The error is fixed. Overwrite the original buffer with the
// corrected sequence, and returns the error bit position. // corrected sequence, and returns the error bit position.
memcpy(msg, aux, bits/8); memcpy(msg, aux, bits/8);
// We return the two bits as a 16 bit integer by shifting 'i' // We return the two bits as a 16 bit integer by shifting 'i'
// on the left. This is possible since 'i' will always be // on the left. This is possible since 'i' will always be
// non-zero because i starts from j+1. // non-zero because i starts from j+1.
@ -184,9 +394,11 @@ int fix_two_bits_errors(unsigned char *msg, int bits) {
return -1; return -1;
} }
// ==========================================================================
// Hash the ICAO address to index our cache of MODE_S_ICAO_CACHE_LEN elements, // Hash the ICAO address to index our cache of MODE_S_ICAO_CACHE_LEN elements,
// that is assumed to be a power of two. // that is assumed to be a power of two.
uint32_t icao_cache_has_addr(uint32_t a) { uint32_t icao_cache_has_addr(uint32_t a)
{
// The following three rounds wil make sure that every bit affects every // The following three rounds wil make sure that every bit affects every
// output bit with ~ 50% of probability. // output bit with ~ 50% of probability.
a = ((a >> 16) ^ a) * 0x45d9f3b; a = ((a >> 16) ^ a) * 0x45d9f3b;
@ -195,19 +407,23 @@ uint32_t icao_cache_has_addr(uint32_t a) {
return a & (MODE_S_ICAO_CACHE_LEN-1); return a & (MODE_S_ICAO_CACHE_LEN-1);
} }
// ==========================================================================
// Add the specified entry to the cache of recently seen ICAO addresses. Note // Add the specified entry to the cache of recently seen ICAO addresses. Note
// that we also add a timestamp so that we can make sure that the entry is only // that we also add a timestamp so that we can make sure that the entry is only
// valid for MODE_S_ICAO_CACHE_TTL seconds. // valid for MODE_S_ICAO_CACHE_TTL seconds.
void add_recently_seen_icao_addr(mode_s_t *self, uint32_t addr) { void add_recently_seen_icao_addr(mode_s_t *self, uint32_t addr)
{
uint32_t h = icao_cache_has_addr(addr); uint32_t h = icao_cache_has_addr(addr);
self->icao_cache[h*2] = addr; self->icao_cache[h*2] = addr;
self->icao_cache[h*2+1] = (uint32_t) time(NULL); self->icao_cache[h*2+1] = (uint32_t) time(NULL);
} }
// ==========================================================================
// Returns 1 if the specified ICAO address was seen in a DF format with proper // Returns 1 if the specified ICAO address was seen in a DF format with proper
// checksum (not xored with address) no more than * MODE_S_ICAO_CACHE_TTL // checksum (not xored with address) no more than * MODE_S_ICAO_CACHE_TTL
// seconds ago. Otherwise returns 0. // seconds ago. Otherwise returns 0.
int icao_addr_was_recently_seen(mode_s_t *self, uint32_t addr) { int icao_addr_was_recently_seen(mode_s_t *self, uint32_t addr)
{
uint32_t h = icao_cache_has_addr(addr); uint32_t h = icao_cache_has_addr(addr);
uint32_t a = self->icao_cache[h*2]; uint32_t a = self->icao_cache[h*2];
int32_t t = self->icao_cache[h*2+1]; int32_t t = self->icao_cache[h*2+1];
@ -215,6 +431,7 @@ int icao_addr_was_recently_seen(mode_s_t *self, uint32_t addr) {
return a && a == addr && time(NULL)-t <= MODE_S_ICAO_CACHE_TTL; return a && a == addr && time(NULL)-t <= MODE_S_ICAO_CACHE_TTL;
} }
// ==========================================================================
// If the message type has the checksum xored with the ICAO address, try to // If the message type has the checksum xored with the ICAO address, try to
// brute force it using a list of recently seen ICAO addresses. // brute force it using a list of recently seen ICAO addresses.
// //
@ -230,7 +447,8 @@ int icao_addr_was_recently_seen(mode_s_t *self, uint32_t addr) {
// //
// If the function successfully recovers a message with a correct checksum it // If the function successfully recovers a message with a correct checksum it
// returns 1. Otherwise 0 is returned. // returns 1. Otherwise 0 is returned.
int brute_force_ap(mode_s_t *self, unsigned char *msg, struct mode_s_msg *mm) { int brute_force_ap(mode_s_t *self, unsigned char *msg, struct mode_s_msg *mm)
{
unsigned char aux[MODE_S_LONG_MSG_BYTES]; unsigned char aux[MODE_S_LONG_MSG_BYTES];
int msgtype = mm->msgtype; int msgtype = mm->msgtype;
int msgbits = mm->msgbits; int msgbits = mm->msgbits;
@ -252,7 +470,6 @@ int brute_force_ap(mode_s_t *self, unsigned char *msg, struct mode_s_msg *mm) {
// Compute the CRC of the message and XOR it with the AP field so that // Compute the CRC of the message and XOR it with the AP field so that
// we recover the address, because: // we recover the address, because:
//
// (ADDR xor CRC) xor CRC = ADDR. // (ADDR xor CRC) xor CRC = ADDR.
crc = mode_s_checksum(aux, msgbits); crc = mode_s_checksum(aux, msgbits);
aux[lastbyte] ^= crc & 0xff; aux[lastbyte] ^= crc & 0xff;
@ -262,7 +479,9 @@ int brute_force_ap(mode_s_t *self, unsigned char *msg, struct mode_s_msg *mm) {
// If the obtained address exists in our cache we consider the message // If the obtained address exists in our cache we consider the message
// valid. // valid.
addr = aux[lastbyte] | (aux[lastbyte-1] << 8) | (aux[lastbyte-2] << 16); addr = aux[lastbyte] | (aux[lastbyte-1] << 8) | (aux[lastbyte-2] << 16);
if (icao_addr_was_recently_seen(self, addr)) {
if (icao_addr_was_recently_seen(self, addr))
{
mm->aa1 = aux[lastbyte-2]; mm->aa1 = aux[lastbyte-2];
mm->aa2 = aux[lastbyte-1]; mm->aa2 = aux[lastbyte-1];
mm->aa3 = aux[lastbyte]; mm->aa3 = aux[lastbyte];
@ -272,55 +491,73 @@ int brute_force_ap(mode_s_t *self, unsigned char *msg, struct mode_s_msg *mm) {
return 0; return 0;
} }
// ==========================================================================
// Decode the 13 bit AC altitude field (in DF 20 and others). Returns the // Decode the 13 bit AC altitude field (in DF 20 and others). Returns the
// altitude, and set 'unit' to either MODE_S_UNIT_METERS or MDOES_UNIT_FEETS. // altitude, and set 'unit' to either MODE_S_UNIT_METERS or MDOES_UNIT_FEETS.
int decode_ac13_field(unsigned char *msg, int *unit) { int decode_ac13_field(unsigned char *msg, int *unit)
{
int m_bit = msg[3] & (1<<6); int m_bit = msg[3] & (1<<6);
int q_bit = msg[3] & (1<<4); int q_bit = msg[3] & (1<<4);
if (!m_bit) { if (!m_bit)
{
*unit = MODE_S_UNIT_FEET; *unit = MODE_S_UNIT_FEET;
if (q_bit) {
if (q_bit)
{
// N is the 11 bit integer resulting from the removal of bit Q and M // N is the 11 bit integer resulting from the removal of bit Q and M
int n = ((msg[2]&31)<<6) | int n = ((msg[2]&31)<<6) |
((msg[3]&0x80)>>2) | ((msg[3]&0x80)>>2) |
((msg[3]&0x20)>>1) | ((msg[3]&0x20)>>1) |
(msg[3]&15); (msg[3]&15);
// The final altitude is due to the resulting number multiplied by // The final altitude is due to the resulting number multiplied by
// 25, minus 1000. // 25, minus 1000.
return n*25-1000; return n*25-1000;
} else { }
else
{
// TODO: Implement altitude where Q=0 and M=0 // TODO: Implement altitude where Q=0 and M=0
} }
} else { }
else
{
*unit = MODE_S_UNIT_METERS; *unit = MODE_S_UNIT_METERS;
// TODO: Implement altitude when meter unit is selected. // TODO: Implement altitude when meter unit is selected.
} }
return 0; return 0;
} }
// ==========================================================================
// Decode the 12 bit AC altitude field (in DF 17 and others). Returns the // Decode the 12 bit AC altitude field (in DF 17 and others). Returns the
// altitude or 0 if it can't be decoded. // altitude or 0 if it can't be decoded.
int decode_ac12_field(unsigned char *msg, int *unit) { int decode_ac12_field(unsigned char *msg, int *unit)
{
int q_bit = msg[5] & 1; int q_bit = msg[5] & 1;
if (q_bit) { if (q_bit)
{
// N is the 11 bit integer resulting from the removal of bit Q // N is the 11 bit integer resulting from the removal of bit Q
*unit = MODE_S_UNIT_FEET; *unit = MODE_S_UNIT_FEET;
int n = ((msg[5]>>1)<<4) | ((msg[6]&0xF0) >> 4); int n = ((msg[5]>>1)<<4) | ((msg[6]&0xF0) >> 4);
// The final altitude is due to the resulting number multiplied by 25, // The final altitude is due to the resulting number multiplied by 25,
// minus 1000. // minus 1000.
return n*25-1000; return n*25-1000;
} else { }
else
{
return 0; return 0;
} }
} }
static const char *ais_charset = "?ABCDEFGHIJKLMNOPQRSTUVWXYZ????? ???????????????0123456789??????"; // ==========================================================================
// Decode a raw Mode S message demodulated as a stream of bytes by // Decode a raw Mode S message demodulated as a stream of bytes by
// mode_s_detect(), and split it into fields populating a mode_s_msg structure. // mode_s_detect(), and split it into fields populating a mode_s_msg structure.
void mode_s_decode(mode_s_t *self, struct mode_s_msg *mm, unsigned char *msg) { static const char *ais_charset = "?ABCDEFGHIJKLMNOPQRSTUVWXYZ????? ???????????????0123456789??????";
void mode_s_decode(mode_s_t *self, struct mode_s_msg *mm, unsigned char *msg)
{
uint32_t crc2; // Computed CRC, used to verify the message CRC. uint32_t crc2; // Computed CRC, used to verify the message CRC.
// Work on our local copy // Work on our local copy
@ -341,12 +578,15 @@ void mode_s_decode(mode_s_t *self, struct mode_s_msg *mm, unsigned char *msg) {
mm->errorbit = -1; // No error mm->errorbit = -1; // No error
mm->crcok = (mm->crc == crc2); mm->crcok = (mm->crc == crc2);
if (!mm->crcok && self->fix_errors && (mm->msgtype == 11 || mm->msgtype == 17)) { if (!mm->crcok && self->fix_errors && (mm->msgtype == 11 || mm->msgtype == 17))
if ((mm->errorbit = fix_single_bit_errors(msg, mm->msgbits)) != -1) { {
if ((mm->errorbit = fix_single_bit_errors(msg, mm->msgbits)) != -1)
{
mm->crc = mode_s_checksum(msg, mm->msgbits); mm->crc = mode_s_checksum(msg, mm->msgbits);
mm->crcok = 1; mm->crcok = 1;
} else if (self->aggressive && mm->msgtype == 17 && }
(mm->errorbit = fix_two_bits_errors(msg, mm->msgbits)) != -1) { else if (self->aggressive && mm->msgtype == 17 && (mm->errorbit = fix_two_bits_errors(msg, mm->msgbits)) != -1)
{
mm->crc = mode_s_checksum(msg, mm->msgbits); mm->crc = mode_s_checksum(msg, mm->msgbits);
mm->crcok = 1; mm->crcok = 1;
} }
@ -369,8 +609,7 @@ void mode_s_decode(mode_s_t *self, struct mode_s_msg *mm, unsigned char *msg) {
// Fields for DF4,5,20,21 // Fields for DF4,5,20,21
mm->fs = msg[0] & 7; // Flight status for DF4,5,20,21 mm->fs = msg[0] & 7; // Flight status for DF4,5,20,21
mm->dr = msg[1] >> 3 & 31; // Request extraction of downlink request. mm->dr = msg[1] >> 3 & 31; // Request extraction of downlink request.
mm->um = ((msg[1] & 7)<<3)| // Request extraction of downlink request. mm->um = ( (msg[1] & 7)<<3) | msg[2]>>5;// Request extraction of downlink request.
msg[2]>>5;
// In the squawk (identity) field bits are interleaved like that (message // In the squawk (identity) field bits are interleaved like that (message
// bit 20 to bit 32): // bit 20 to bit 32):
@ -404,20 +643,27 @@ void mode_s_decode(mode_s_t *self, struct mode_s_msg *mm, unsigned char *msg) {
// DF 11 & 17: try to populate our ICAO addresses whitelist. DFs with an AP // DF 11 & 17: try to populate our ICAO addresses whitelist. DFs with an AP
// field (xored addr and crc), try to decode it. // field (xored addr and crc), try to decode it.
if (mm->msgtype != 11 && mm->msgtype != 17) { if (mm->msgtype != 11 && mm->msgtype != 17)
{
// Check if we can check the checksum for the Downlink Formats where // Check if we can check the checksum for the Downlink Formats where
// the checksum is xored with the aircraft ICAO address. We try to // the checksum is xored with the aircraft ICAO address. We try to
// brute force it using a list of recently seen aircraft addresses. // brute force it using a list of recently seen aircraft addresses.
if (brute_force_ap(self, msg, mm)) { if (brute_force_ap(self, msg, mm))
{
// We recovered the message, mark the checksum as valid. // We recovered the message, mark the checksum as valid.
mm->crcok = 1; mm->crcok = 1;
} else { }
else
{
mm->crcok = 0; mm->crcok = 0;
} }
} else { }
else
{
// If this is DF 11 or DF 17 and the checksum was ok, we can add this // If this is DF 11 or DF 17 and the checksum was ok, we can add this
// address to the list of recently seen addresses. // address to the list of recently seen addresses.
if (mm->crcok && mm->errorbit == -1) { if (mm->crcok && mm->errorbit == -1)
{
uint32_t addr = (mm->aa1 << 16) | (mm->aa2 << 8) | mm->aa3; uint32_t addr = (mm->aa1 << 16) | (mm->aa2 << 8) | mm->aa3;
add_recently_seen_icao_addr(self, addr); add_recently_seen_icao_addr(self, addr);
} }
@ -425,15 +671,18 @@ void mode_s_decode(mode_s_t *self, struct mode_s_msg *mm, unsigned char *msg) {
// Decode 13 bit altitude for DF0, DF4, DF16, DF20 // Decode 13 bit altitude for DF0, DF4, DF16, DF20
if (mm->msgtype == 0 || mm->msgtype == 4 || if (mm->msgtype == 0 || mm->msgtype == 4 ||
mm->msgtype == 16 || mm->msgtype == 20) { mm->msgtype == 16 || mm->msgtype == 20)
{
mm->altitude = decode_ac13_field(msg, &mm->unit); mm->altitude = decode_ac13_field(msg, &mm->unit);
} }
// Decode extended squitter specific stuff. // Decode extended squitter specific stuff.
if (mm->msgtype == 17) { if (mm->msgtype == 17)
{
// Decode the extended squitter message. // Decode the extended squitter message.
if (mm->metype >= 1 && mm->metype <= 4) { if (mm->metype >= 1 && mm->metype <= 4)
{
// Aircraft Identification and Category // Aircraft Identification and Category
mm->aircraft_type = mm->metype-1; mm->aircraft_type = mm->metype-1;
mm->flight[0] = (ais_charset)[msg[5]>>2]; mm->flight[0] = (ais_charset)[msg[5]>>2];
@ -445,7 +694,9 @@ void mode_s_decode(mode_s_t *self, struct mode_s_msg *mm, unsigned char *msg) {
mm->flight[6] = ais_charset[((msg[9]&15)<<2)|(msg[10]>>6)]; mm->flight[6] = ais_charset[((msg[9]&15)<<2)|(msg[10]>>6)];
mm->flight[7] = ais_charset[msg[10]&63]; mm->flight[7] = ais_charset[msg[10]&63];
mm->flight[8] = '\0'; mm->flight[8] = '\0';
} else if (mm->metype >= 9 && mm->metype <= 18) { }
else if (mm->metype >= 9 && mm->metype <= 18)
{
// Airborne position Message // Airborne position Message
mm->fflag = msg[6] & (1<<2); mm->fflag = msg[6] & (1<<2);
mm->tflag = msg[6] & (1<<3); mm->tflag = msg[6] & (1<<3);
@ -456,9 +707,12 @@ void mode_s_decode(mode_s_t *self, struct mode_s_msg *mm, unsigned char *msg) {
mm->raw_longitude = ((msg[8]&1) << 16) | mm->raw_longitude = ((msg[8]&1) << 16) |
(msg[9] << 8) | (msg[9] << 8) |
msg[10]; msg[10];
} else if (mm->metype == 19 && mm->mesub >= 1 && mm->mesub <= 4) { }
else if (mm->metype == 19 && mm->mesub >= 1 && mm->mesub <= 4)
{
// Airborne Velocity Message // Airborne Velocity Message
if (mm->mesub == 1 || mm->mesub == 2) { if (mm->mesub == 1 || mm->mesub == 2)
{
mm->ew_dir = (msg[5]&4) >> 2; mm->ew_dir = (msg[5]&4) >> 2;
mm->ew_velocity = ((msg[5]&3) << 8) | msg[6]; mm->ew_velocity = ((msg[5]&3) << 8) | msg[6];
mm->ns_dir = (msg[7]&0x80) >> 7; mm->ns_dir = (msg[7]&0x80) >> 7;
@ -466,10 +720,13 @@ void mode_s_decode(mode_s_t *self, struct mode_s_msg *mm, unsigned char *msg) {
mm->vert_rate_source = (msg[8]&0x10) >> 4; mm->vert_rate_source = (msg[8]&0x10) >> 4;
mm->vert_rate_sign = (msg[8]&0x8) >> 3; mm->vert_rate_sign = (msg[8]&0x8) >> 3;
mm->vert_rate = ((msg[8]&7) << 6) | ((msg[9]&0xfc) >> 2); mm->vert_rate = ((msg[8]&7) << 6) | ((msg[9]&0xfc) >> 2);
// Compute velocity and angle from the two speed components // Compute velocity and angle from the two speed components
mm->velocity = sqrt(mm->ns_velocity*mm->ns_velocity+ mm->velocity = sqrt(mm->ns_velocity*mm->ns_velocity+
mm->ew_velocity*mm->ew_velocity); mm->ew_velocity*mm->ew_velocity);
if (mm->velocity) {
if (mm->velocity)
{
int ewv = mm->ew_velocity; int ewv = mm->ew_velocity;
int nsv = mm->ns_velocity; int nsv = mm->ns_velocity;
double heading; double heading;
@ -480,28 +737,34 @@ void mode_s_decode(mode_s_t *self, struct mode_s_msg *mm, unsigned char *msg) {
// Convert to degrees. // Convert to degrees.
mm->heading = heading * 360 / (M_PI*2); mm->heading = heading * 360 / (M_PI*2);
// We don't want negative values but a 0-360 scale. // We don't want negative values but a 0-360 scale.
if (mm->heading < 0) mm->heading += 360; if (mm->heading < 0) mm->heading += 360;
} else { }
else
{
mm->heading = 0; mm->heading = 0;
} }
} else if (mm->mesub == 3 || mm->mesub == 4) { }
else if (mm->mesub == 3 || mm->mesub == 4)
{
mm->heading_is_valid = msg[5] & (1<<2); mm->heading_is_valid = msg[5] & (1<<2);
mm->heading = (360.0/128) * (((msg[5] & 3) << 5) | mm->heading = (360.0/128) * (((msg[5] & 3) << 5) | (msg[6] >> 3));
(msg[6] >> 3));
} }
} }
} }
mm->phase_corrected = 0; // Set to 1 by the caller if needed. mm->phase_corrected = 0; // Set to 1 by the caller if needed.
} }
// ==========================================================================
// Return -1 if the message is out of fase left-side // Return -1 if the message is out of fase left-side
// Return 1 if the message is out of fase right-size // Return 1 if the message is out of fase right-size
// Return 0 if the message is not particularly out of phase. // Return 0 if the message is not particularly out of phase.
// //
// Note: this function will access mag[-1], so the caller should make sure to // Note: this function will access mag[-1], so the caller should make sure to
// call it only if we are not at the start of the current buffer. // call it only if we are not at the start of the current buffer.
int detect_out_of_phase(uint16_t *mag) { int detect_out_of_phase(uint16_t *mag)
{
if (mag[3] > mag[2]/3) return 1; if (mag[3] > mag[2]/3) return 1;
if (mag[10] > mag[9]/3) return 1; if (mag[10] > mag[9]/3) return 1;
if (mag[6] > mag[7]/3) return -1; if (mag[6] > mag[7]/3) return -1;
@ -509,6 +772,7 @@ int detect_out_of_phase(uint16_t *mag) {
return 0; return 0;
} }
// ==========================================================================
// This function does not really correct the phase of the message, it just // This function does not really correct the phase of the message, it just
// applies a transformation to the first sample representing a given bit: // applies a transformation to the first sample representing a given bit:
// //
@ -535,21 +799,27 @@ int detect_out_of_phase(uint16_t *mag) {
// a zero, to detect another zero. Symmetrically if it is a one it will be more // a zero, to detect another zero. Symmetrically if it is a one it will be more
// likely to detect a one because of the transformation. In this way similar // likely to detect a one because of the transformation. In this way similar
// levels will be interpreted more likely in the correct way. // levels will be interpreted more likely in the correct way.
void apply_phase_correction(uint16_t *mag) { void apply_phase_correction(uint16_t *mag)
{
int j; int j;
mag += 16; // Skip preamble. mag += 16; // Skip preamble.
for (j = 0; j < (MODE_S_LONG_MSG_BITS-1)*2; j += 2) { for (j = 0; j < (MODE_S_LONG_MSG_BITS-1)*2; j += 2)
if (mag[j] > mag[j+1]) { {
if (mag[j] > mag[j+1])
{
// One // One
mag[j+2] = (mag[j+2] * 5) / 4; mag[j+2] = (mag[j+2] * 5) / 4;
} else { }
else
{
// Zero // Zero
mag[j+2] = (mag[j+2] * 4) / 5; mag[j+2] = (mag[j+2] * 4) / 5;
} }
} }
} }
// ==========================================================================
// Detect a Mode S messages inside the magnitude buffer pointed by 'mag' and of // Detect a Mode S messages inside the magnitude buffer pointed by 'mag' and of
// size 'maglen' bytes. Every detected Mode S message is convert it into a // size 'maglen' bytes. Every detected Mode S message is convert it into a
// stream of bits and passed to the function to display it. // stream of bits and passed to the function to display it.
@ -589,8 +859,9 @@ void mode_s_detect(mode_s_t *self, uint16_t *mag, uint32_t maglen, mode_s_callba
int low, high, delta, i, errors; int low, high, delta, i, errors;
int good_message = 0; int good_message = 0;
if (use_correction) goto good_preamble; // We already checked it. // We already checked it - skip the preemble
if (!use_correction)
{
// First check of relations between the first 10 samples representing a // First check of relations between the first 10 samples representing a
// valid preamble. We don't even investigate further if this simple // valid preamble. We don't even investigate further if this simple
// test is not passed. // test is not passed.
@ -629,13 +900,15 @@ void mode_s_detect(mode_s_t *self, uint16_t *mag, uint32_t maglen, mode_s_callba
{ {
continue; continue;
} }
}
good_preamble:
// If the previous attempt with this message failed, retry using // If the previous attempt with this message failed, retry using
// magnitude correction. // magnitude correction.
if (use_correction) { if (use_correction)
{
memcpy(aux, mag+j+MODE_S_PREAMBLE_US*2, sizeof(aux)); memcpy(aux, mag+j+MODE_S_PREAMBLE_US*2, sizeof(aux));
if (j && detect_out_of_phase(mag+j)) { if (j && detect_out_of_phase(mag+j))
{
apply_phase_correction(mag+j); apply_phase_correction(mag+j);
} }
// TODO ... apply other kind of corrections. // TODO ... apply other kind of corrections.
@ -644,23 +917,35 @@ good_preamble:
// Decode all the next 112 bits, regardless of the actual message size. // Decode all the next 112 bits, regardless of the actual message size.
// We'll check the actual message type later. // We'll check the actual message type later.
errors = 0; errors = 0;
for (i = 0; i < MODE_S_LONG_MSG_BITS*2; i += 2) { for (i = 0; i < MODE_S_LONG_MSG_BITS*2; i += 2)
{
low = mag[j+i+MODE_S_PREAMBLE_US*2]; low = mag[j+i+MODE_S_PREAMBLE_US*2];
high = mag[j+i+MODE_S_PREAMBLE_US*2+1]; high = mag[j+i+MODE_S_PREAMBLE_US*2+1];
delta = low-high; delta = low-high;
if (delta < 0) delta = -delta; if (delta < 0) delta = -delta;
if (i > 0 && delta < 256) { if (i > 0 && delta < 256)
{
bits[i/2] = bits[i/2-1]; bits[i/2] = bits[i/2-1];
} else if (low == high) { }
else if (low == high)
{
// Checking if two adiacent samples have the same magnitude is // Checking if two adiacent samples have the same magnitude is
// an effective way to detect if it's just random noise that // an effective way to detect if it's just random noise that
// was detected as a valid preamble. // was detected as a valid preamble.
bits[i/2] = 2; // error bits[i/2] = 2; // error
if (i < MODE_S_SHORT_MSG_BITS*2) errors++; if (i < MODE_S_SHORT_MSG_BITS*2)
} else if (low > high) { {
errors++;
}
}
else if (low > high)
{
bits[i/2] = 1; bits[i/2] = 1;
} else { }
else
{
// (low < high) for exclusion // (low < high) for exclusion
bits[i/2] = 0; bits[i/2] = 0;
} }

Wyświetl plik

@ -1,4 +1,5 @@
/* /*
* Modified source file by David Michaeli @ CaribouLabs Ltd. Apr. 2022
* Source of file: https://github.com/watson/libmodes * Source of file: https://github.com/watson/libmodes
* Author: Thomas Watson (@watson) * Author: Thomas Watson (@watson)
* Contact: w@tson.dk / https://twitter.com/wa7son * Contact: w@tson.dk / https://twitter.com/wa7son
@ -16,7 +17,7 @@ extern "C" {
#include <stdint.h> #include <stdint.h>
#include <unistd.h> #include <unistd.h>
#include <math.h> #include <math.h>
#include <sys/time.h> #include <time.h>
#define MODE_S_ICAO_CACHE_LEN 1024 // Power of two required #define MODE_S_ICAO_CACHE_LEN 1024 // Power of two required
#define MODE_S_LONG_MSG_BYTES (112/8) #define MODE_S_LONG_MSG_BYTES (112/8)
@ -26,8 +27,8 @@ extern "C" {
// Program state // Program state
typedef struct typedef struct
{ {
// Internal state // Internal state - recently seen ICAO addresses cache
uint32_t icao_cache[sizeof(uint32_t)*MODE_S_ICAO_CACHE_LEN*2]; // Recently seen ICAO addresses cache uint32_t icao_cache[sizeof(uint32_t)*MODE_S_ICAO_CACHE_LEN*2];
// Configuration // Configuration
int fix_errors; // Single bit error correction if true int fix_errors; // Single bit error correction if true
@ -38,7 +39,7 @@ typedef struct
// The struct we use to store information about a decoded message // The struct we use to store information about a decoded message
struct mode_s_msg struct mode_s_msg
{ {
// Generic fields // ------ Generic fields ------------
unsigned char msg[MODE_S_LONG_MSG_BYTES]; // Binary message unsigned char msg[MODE_S_LONG_MSG_BYTES]; // Binary message
int msgbits; // Number of bits in message int msgbits; // Number of bits in message
int msgtype; // Downlink format # int msgtype; // Downlink format #
@ -48,10 +49,10 @@ struct mode_s_msg
int aa1, aa2, aa3; // ICAO Address bytes 1 2 and 3 int aa1, aa2, aa3; // ICAO Address bytes 1 2 and 3
int phase_corrected; // True if phase correction was applied. int phase_corrected; // True if phase correction was applied.
// DF 11 // ------ DF 11 ---------------------
int ca; // Responder capabilities. int ca; // Responder capabilities.
// DF 17 // ------ DF 17 ---------------------
int metype; // Extended squitter message type. int metype; // Extended squitter message type.
int mesub; // Extended squitter message subtype. int mesub; // Extended squitter message subtype.
int heading_is_valid; int heading_is_valid;
@ -71,7 +72,7 @@ struct mode_s_msg
int vert_rate; // Vertical rate. int vert_rate; // Vertical rate.
int velocity; // Computed from EW and NS velocity. int velocity; // Computed from EW and NS velocity.
// DF4, DF5, DF20, DF21 // ------ DF4, DF5, DF20, DF21 -------
int fs; // Flight status for DF4,5,20,21 int fs; // Flight status for DF4,5,20,21
int dr; // Request extraction of downlink request. int dr; // Request extraction of downlink request.
int um; // Request extraction of downlink request. int um; // Request extraction of downlink request.
@ -86,6 +87,7 @@ typedef void (*mode_s_callback_t)(mode_s_t *self, struct mode_s_msg *mm);
void mode_s_init(mode_s_t *self); void mode_s_init(mode_s_t *self);
void mode_s_detect(mode_s_t *self, uint16_t *mag, uint32_t maglen, mode_s_callback_t); void mode_s_detect(mode_s_t *self, uint16_t *mag, uint32_t maglen, mode_s_callback_t);
void mode_s_decode(mode_s_t *self, struct mode_s_msg *mm, unsigned char *msg); void mode_s_decode(mode_s_t *self, struct mode_s_msg *mm, unsigned char *msg);
void mode_s_display_message(struct mode_s_msg *mm);
#ifdef __cplusplus #ifdef __cplusplus
} }