mirror of
https://github.com/eried/portapack-mayhem.git
synced 2025-08-07 06:02:20 -04:00
ADS-B frame struct, callsign decode
This commit is contained in:
parent
32e8bc4c65
commit
93c5959df6
8 changed files with 193 additions and 111 deletions
|
@ -26,25 +26,102 @@
|
|||
|
||||
namespace adsb {
|
||||
|
||||
void make_frame_mode_s(uint8_t * const adsb_frame, const uint32_t ICAO_address) {
|
||||
adsb_frame[0] = (17 << 3) | 5; // DF and CA
|
||||
adsb_frame[1] = ICAO_address >> 16;
|
||||
adsb_frame[2] = (ICAO_address >> 8) & 0xFF;
|
||||
adsb_frame[3] = ICAO_address & 0xFF;
|
||||
|
||||
memset(&adsb_frame[4], 0, 10);
|
||||
uint8_t adsb_frame::get_DF() {
|
||||
return (raw_data[0] >> 3);
|
||||
}
|
||||
|
||||
void generate_frame_id(uint8_t * const adsb_frame, const uint32_t ICAO_address, std::string & callsign) {
|
||||
uint8_t adsb_frame::get_msg_type() {
|
||||
return (raw_data[4] >> 3);
|
||||
}
|
||||
|
||||
uint32_t adsb_frame::get_ICAO_address() {
|
||||
return (raw_data[1] << 16) + (raw_data[2] << 8) + raw_data[3];
|
||||
}
|
||||
|
||||
void adsb_frame::clear() {
|
||||
index = 0;
|
||||
memset(raw_data, 0, 14);
|
||||
}
|
||||
|
||||
void adsb_frame::push_byte(uint8_t byte) {
|
||||
if (index >= 14)
|
||||
return;
|
||||
|
||||
raw_data[index++] = byte;
|
||||
}
|
||||
|
||||
uint8_t adsb_frame::get_byte(uint8_t index) {
|
||||
if (index >= 14)
|
||||
return 0;
|
||||
|
||||
return raw_data[index];
|
||||
}
|
||||
|
||||
std::string adsb_frame::get_callsign() {
|
||||
uint64_t callsign_coded = 0;
|
||||
uint32_t c;
|
||||
std::string callsign = "";
|
||||
|
||||
// Frame bytes to long
|
||||
for (c = 5; c < 11; c++) {
|
||||
callsign_coded <<= 8;
|
||||
callsign_coded |= raw_data[c];
|
||||
}
|
||||
|
||||
// Long to 6-bit characters
|
||||
for (c = 0; c < 8; c++) {
|
||||
callsign.append(1, icao_id_lut[(callsign_coded >> 42) & 0x3F]);
|
||||
callsign_coded <<= 6;
|
||||
}
|
||||
|
||||
return callsign;
|
||||
}
|
||||
|
||||
void adsb_frame::make_CRC() {
|
||||
uint8_t adsb_crc[14]; // Temp buffer
|
||||
uint8_t b, c, s, bitn;
|
||||
const uint32_t crc_poly = 0x1205FFF;
|
||||
|
||||
// Clear CRC
|
||||
raw_data[11] = 0x00;
|
||||
raw_data[12] = 0x00;
|
||||
raw_data[13] = 0x00;
|
||||
|
||||
// Compute CRC
|
||||
memcpy(adsb_crc, raw_data, 14);
|
||||
for (c = 0; c < 11; c++) {
|
||||
for (b = 0; b < 8; b++) {
|
||||
if ((adsb_crc[c] << b) & 0x80) {
|
||||
for (s = 0; s < 25; s++) {
|
||||
bitn = (c * 8) + b + s;
|
||||
if ((crc_poly >> s) & 1) adsb_crc[bitn >> 3] ^= (0x80 >> (bitn & 7));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Insert CRC in frame
|
||||
memcpy(&raw_data[11], &adsb_crc[11], 3);
|
||||
}
|
||||
|
||||
void make_frame_mode_s(adsb_frame& frame, const uint32_t ICAO_address) {
|
||||
frame.clear();
|
||||
frame.push_byte((17 << 3) | 5); // DF17 and CA
|
||||
frame.push_byte(ICAO_address >> 16);
|
||||
frame.push_byte(ICAO_address >> 8);
|
||||
frame.push_byte(ICAO_address & 0xFF);
|
||||
}
|
||||
|
||||
void generate_frame_id(adsb_frame& frame, const uint32_t ICAO_address, std::string & callsign) {
|
||||
std::string callsign_formatted(8, '_');
|
||||
uint64_t callsign_coded = 0;
|
||||
uint32_t c, s;
|
||||
char ch;
|
||||
|
||||
make_frame_mode_s(adsb_frame, ICAO_address);
|
||||
make_frame_mode_s(frame, ICAO_address);
|
||||
|
||||
frame.push_byte(4 << 3); // TC = 4: Aircraft ID
|
||||
|
||||
adsb_frame[4] = 4 << 3; // TC = 4: Aircraft ID
|
||||
|
||||
// Translate and encode callsign
|
||||
for (c = 0; c < 8; c++) {
|
||||
ch = callsign[c];
|
||||
|
@ -62,18 +139,18 @@ void generate_frame_id(uint8_t * const adsb_frame, const uint32_t ICAO_address,
|
|||
|
||||
// Insert callsign in frame
|
||||
for (c = 0; c < 6; c++)
|
||||
adsb_frame[c + 5] = (callsign_coded >> ((5 - c) * 8)) & 0xFF;
|
||||
frame.push_byte((callsign_coded >> ((5 - c) * 8)) & 0xFF);
|
||||
|
||||
ADSB_generate_CRC(adsb_frame);
|
||||
frame.make_CRC();
|
||||
}
|
||||
|
||||
void generate_frame_emergency(uint8_t * const adsb_frame, const uint32_t ICAO_address, const uint8_t code) {
|
||||
make_frame_mode_s(adsb_frame, ICAO_address);
|
||||
void generate_frame_emergency(adsb_frame& frame, const uint32_t ICAO_address, const uint8_t code) {
|
||||
make_frame_mode_s(frame, ICAO_address);
|
||||
|
||||
adsb_frame[4] = (28 << 3) + 1; // TC = 28 (Emergency), subtype = 1 (Emergency)
|
||||
adsb_frame[5] = code << 5;
|
||||
frame.push_byte((28 << 3) + 1); // TC = 28 (Emergency), subtype = 1 (Emergency)
|
||||
frame.push_byte(code << 5);
|
||||
|
||||
ADSB_generate_CRC(adsb_frame);
|
||||
frame.make_CRC();
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -118,31 +195,4 @@ void generate_frame_pos(uint8_t * const adsb_frame, const uint32_t ICAO_address,
|
|||
}
|
||||
*/
|
||||
|
||||
void ADSB_generate_CRC(uint8_t * const in_frame) {
|
||||
uint8_t adsb_crc[14]; // Temp buffer
|
||||
uint8_t b, c, s, bitn;
|
||||
const uint32_t crc_poly = 0x1205FFF;
|
||||
|
||||
// Clear CRC
|
||||
in_frame[11] = 0x00;
|
||||
in_frame[12] = 0x00;
|
||||
in_frame[13] = 0x00;
|
||||
|
||||
// Compute CRC
|
||||
memcpy(adsb_crc, in_frame, 14);
|
||||
for (c = 0; c < 11; c++) {
|
||||
for (b = 0; b < 8; b++) {
|
||||
if ((adsb_crc[c] << b) & 0x80) {
|
||||
for (s = 0; s < 25; s++) {
|
||||
bitn = (c * 8) + b + s;
|
||||
if ((crc_poly >> s) & 1) adsb_crc[bitn >> 3] ^= (0x80 >> (bitn & 7));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Insert CRC in frame
|
||||
memcpy(&in_frame[11], &adsb_crc[11], 3);
|
||||
}
|
||||
|
||||
} /* namespace adsb */
|
||||
|
|
|
@ -26,19 +26,35 @@
|
|||
#ifndef __ADSB_H__
|
||||
#define __ADSB_H__
|
||||
|
||||
#define ADSB_PREAMBLE_LENGTH 16
|
||||
|
||||
namespace adsb {
|
||||
|
||||
const char icao_id_lut[65] = "#ABCDEFGHIJKLMNOPQRSTUVWXYZ##### ###############0123456789######";
|
||||
const uint8_t adsb_preamble[16] = { 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0 };
|
||||
struct adsb_frame {
|
||||
public:
|
||||
uint8_t get_DF();
|
||||
uint8_t get_msg_type();
|
||||
uint32_t get_ICAO_address();
|
||||
std::string get_callsign();
|
||||
|
||||
void make_frame_mode_s(uint8_t * const adsb_frame, const uint32_t ICAO_address);
|
||||
|
||||
void generate_frame_id(uint8_t * const adsb_frame, const uint32_t ICAO_address, std::string & callsign);
|
||||
//void generate_frame_pos(uint8_t * const adsb_frame, const uint32_t ICAO_address, const uint32_t altitude,
|
||||
// const float latitude, const float longitude);
|
||||
void generate_frame_emergency(uint8_t * const adsb_frame, const uint32_t ICAO_address, const uint8_t code);
|
||||
|
||||
void ADSB_generate_CRC(uint8_t * const in_message);
|
||||
void push_byte(uint8_t byte);
|
||||
uint8_t get_byte(uint8_t index); // DEBUG
|
||||
void make_CRC();
|
||||
void clear();
|
||||
|
||||
private:
|
||||
uint8_t index { 0 };
|
||||
uint8_t raw_data[14] { }; // 112 bits at most
|
||||
};
|
||||
|
||||
const char icao_id_lut[65] = "#ABCDEFGHIJKLMNOPQRSTUVWXYZ##### ###############0123456789######";
|
||||
const uint8_t adsb_preamble[16] = { 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0 };
|
||||
|
||||
void make_frame_mode_s(adsb_frame& frame, const uint32_t ICAO_address);
|
||||
void generate_frame_id(adsb_frame&, const uint32_t ICAO_address, std::string& callsign);
|
||||
//void generate_frame_pos(uint8_t * const adsb_frame, const uint32_t ICAO_address, const uint32_t altitude,
|
||||
// const float latitude, const float longitude);
|
||||
void generate_frame_emergency(adsb_frame&, const uint32_t ICAO_address, const uint8_t code);
|
||||
|
||||
} /* namespace adsb */
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue