2017-07-23 07:20:32 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 Jared Boone, ShareBrained Technology, Inc.
|
|
|
|
* Copyright (C) 2017 Furrtek
|
|
|
|
*
|
|
|
|
* This file is part of PortaPack.
|
|
|
|
*
|
|
|
|
* 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, 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; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 51 Franklin Street,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
2023-11-10 14:38:06 -05:00
|
|
|
// https://www.icao.int/SAM/Documents/2015-SEMAUTOM/Ses4%20Presentation%20CUBA_ADSB.pdf
|
|
|
|
|
2017-07-23 07:20:32 -04:00
|
|
|
#include "proc_adsbrx.hpp"
|
|
|
|
#include "portapack_shared_memory.hpp"
|
|
|
|
#include "sine_table_int8.hpp"
|
|
|
|
#include "event_m4.hpp"
|
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
#include <cstddef>
|
|
|
|
|
|
|
|
using namespace adsb;
|
2023-05-18 16:16:05 -04:00
|
|
|
|
2017-07-23 07:20:32 -04:00
|
|
|
void ADSBRXProcessor::execute(const buffer_c8_t& buffer) {
|
2023-05-18 16:16:05 -04:00
|
|
|
// This is called at 2M/2048 = 977Hz
|
2023-11-10 14:38:06 -05:00
|
|
|
// Each sample is 500ns.
|
|
|
|
// One bit is 2 samples == 1us.
|
|
|
|
// Bit value is the transition between samples.
|
|
|
|
// i.e. lo->hi == 0, hi->lo == 1
|
2023-05-18 16:16:05 -04:00
|
|
|
|
|
|
|
if (!configured) return;
|
|
|
|
|
2023-11-10 14:38:06 -05:00
|
|
|
uint8_t bit = 0;
|
|
|
|
uint8_t byte = 0;
|
|
|
|
|
2023-05-18 16:16:05 -04:00
|
|
|
for (size_t i = 0; i < buffer.count; i++) {
|
2023-11-10 14:38:06 -05:00
|
|
|
// Compute sample's magnitude.
|
|
|
|
int8_t re = buffer.p[i].real();
|
|
|
|
int8_t im = buffer.p[i].imag();
|
|
|
|
uint16_t mag = (re * re) + (im * im);
|
2023-05-18 16:16:05 -04:00
|
|
|
|
|
|
|
if (decoding) {
|
2023-11-10 14:38:06 -05:00
|
|
|
// 1 bit == 2 samples, transition defines bit value.
|
|
|
|
if ((sample_count & 1) == 1) {
|
|
|
|
if (bit_count >= msg_len) {
|
2023-05-18 16:16:05 -04:00
|
|
|
const ADSBFrameMessage message(frame, amp);
|
|
|
|
shared_memory.application_queue.push(message);
|
|
|
|
decoding = false;
|
|
|
|
bit = (prev_mag > mag) ? 1 : 0;
|
|
|
|
} else {
|
|
|
|
bit = (prev_mag > mag) ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
byte = bit | (byte << 1);
|
|
|
|
bit_count++;
|
|
|
|
|
2023-11-10 14:38:06 -05:00
|
|
|
// Every 8th bit...
|
|
|
|
if ((bit_count & 0x7) == 0) {
|
|
|
|
// Store the byte.
|
2023-05-18 16:16:05 -04:00
|
|
|
frame.push_byte(byte);
|
|
|
|
|
2023-11-10 14:38:06 -05:00
|
|
|
// Perform additional check on the first byte.
|
|
|
|
if (bit_count == 8) {
|
|
|
|
// Abandon all frames that aren't DF17 or DF18 extended squitters.
|
|
|
|
uint8_t df = (byte >> 3);
|
|
|
|
if (df != 17 && df != 18) {
|
|
|
|
decoding = false;
|
|
|
|
bit = (prev_mag > mag) ? 1 : 0;
|
|
|
|
frame.clear();
|
|
|
|
}
|
2023-05-18 16:16:05 -04:00
|
|
|
}
|
2023-11-10 14:38:06 -05:00
|
|
|
}
|
|
|
|
}
|
2023-05-18 16:16:05 -04:00
|
|
|
|
|
|
|
sample_count++;
|
|
|
|
}
|
|
|
|
|
2023-11-10 14:38:06 -05:00
|
|
|
// Continue looking for preamble, even if in a packet.
|
|
|
|
// Switch if new preamble is higher magnitude.
|
2023-05-18 16:16:05 -04:00
|
|
|
|
2023-11-10 14:38:06 -05:00
|
|
|
// Shift the preamble.
|
|
|
|
for (uint8_t c = 0; c < ADSB_PREAMBLE_LENGTH; c++) {
|
2023-05-18 16:16:05 -04:00
|
|
|
shifter[c] = shifter[c + 1];
|
|
|
|
}
|
|
|
|
shifter[ADSB_PREAMBLE_LENGTH] = mag;
|
|
|
|
|
2023-11-10 14:38:06 -05:00
|
|
|
// First check of relations between the first 12 samples
|
|
|
|
// representing a valid preamble. We don't even investigate
|
|
|
|
// further if this simple test is not passed.
|
|
|
|
// Preamble is 8us - or 16 samples.
|
|
|
|
// 0123456789ABCDEF
|
|
|
|
// _-_-____-_-_____
|
2023-05-18 16:16:05 -04:00
|
|
|
if (shifter[0] < shifter[1] &&
|
|
|
|
shifter[1] > shifter[2] &&
|
|
|
|
shifter[2] < shifter[3] &&
|
|
|
|
shifter[3] > shifter[4] &&
|
|
|
|
shifter[4] < shifter[1] &&
|
|
|
|
shifter[5] < shifter[1] &&
|
|
|
|
shifter[6] < shifter[1] &&
|
|
|
|
shifter[7] < shifter[1] &&
|
|
|
|
shifter[8] > shifter[9] &&
|
|
|
|
shifter[9] < shifter[10] &&
|
|
|
|
shifter[10] > shifter[11]) {
|
|
|
|
// The samples between the two spikes must be < than the average
|
|
|
|
// of the high spikes level. We don't test bits too near to
|
|
|
|
// the high levels as signals can be out of phase so part of the
|
2023-11-10 14:38:06 -05:00
|
|
|
// energy can be in the near samples.
|
|
|
|
int32_t this_amp = (shifter[1] + shifter[3] + shifter[8] + shifter[10]);
|
|
|
|
uint32_t high = this_amp / 9; // TBD: Why 9?
|
|
|
|
if (shifter[5] < high &&
|
2023-05-18 16:16:05 -04:00
|
|
|
shifter[6] < high &&
|
|
|
|
// Similarly samples in the range 11-13 must be low, as it is the
|
|
|
|
// space between the preamble and real data. Again we don't test
|
2023-11-10 14:38:06 -05:00
|
|
|
// bits too near to high levels, see above.
|
2023-05-18 16:16:05 -04:00
|
|
|
shifter[12] < high &&
|
|
|
|
shifter[13] < high &&
|
|
|
|
shifter[14] < high) {
|
2023-11-10 14:38:06 -05:00
|
|
|
if ((decoding == false) || // New preamble
|
|
|
|
((decoding == true) && (this_amp > amp))) // Higher power than existing packet
|
2023-05-18 16:16:05 -04:00
|
|
|
{
|
|
|
|
decoding = true;
|
2023-11-10 14:38:06 -05:00
|
|
|
amp = this_amp;
|
2023-05-18 16:16:05 -04:00
|
|
|
sample_count = 0;
|
|
|
|
bit_count = 0;
|
|
|
|
frame.clear();
|
|
|
|
}
|
2023-11-10 14:38:06 -05:00
|
|
|
}
|
|
|
|
}
|
2023-05-18 16:16:05 -04:00
|
|
|
|
2023-11-10 14:38:06 -05:00
|
|
|
// Store mag for next time.
|
2023-05-18 16:16:05 -04:00
|
|
|
prev_mag = mag;
|
|
|
|
}
|
2017-07-23 07:20:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void ADSBRXProcessor::on_message(const Message* const message) {
|
2023-05-18 16:16:05 -04:00
|
|
|
if (message->id == Message::ID::ADSBConfigure) {
|
|
|
|
bit_count = 0;
|
|
|
|
sample_count = 0;
|
|
|
|
decoding = false;
|
|
|
|
configured = true;
|
|
|
|
}
|
2017-07-23 07:20:32 -04:00
|
|
|
}
|
|
|
|
|
2021-10-15 09:26:33 -04:00
|
|
|
#ifndef _WIN32
|
2017-07-23 07:20:32 -04:00
|
|
|
int main() {
|
2023-05-18 16:16:05 -04:00
|
|
|
EventDispatcher event_dispatcher{std::make_unique<ADSBRXProcessor>()};
|
|
|
|
event_dispatcher.run();
|
|
|
|
return 0;
|
2017-07-23 07:20:32 -04:00
|
|
|
}
|
2021-10-15 09:26:33 -04:00
|
|
|
#endif
|