ACARS Rx App using libacars (#3029)

* Using same decode as libacrars
* Enable ACARS RX
* indent + fix warning
* fixed CRC calculations, length validation, message print
Co-authored-by: gullradriel <gullradriel@users.noreply.github.com>
This commit is contained in:
Sandbox
2026-02-23 04:29:20 +08:00
committed by GitHub
parent 0fe51fa7e3
commit 1ad092f341
4 changed files with 180 additions and 91 deletions
+73 -4
View File
@@ -33,6 +33,76 @@ using namespace portapack;
namespace ui::external_app::acars_rx { namespace ui::external_app::acars_rx {
// ACARS frame field layout (0-based byte offsets):
// [0] SOH framing byte (skipped)
// [1..7] Aircraft registration (7 chars)
// [8] STX framing byte (skipped)
// [9..10] Label (2 chars)
// [11] Block ID (1 char)
// [12..14] Message number (3 chars)
// [15..20] Flight ID (6 chars)
// [21..N-3] Free-text payload (variable)
// [N-2..N-1] CRC-16/CCITT (2 bytes, MSB first, NOT part of payload)
//
// Minimum valid frame: 21-byte fixed header + 2 CRC bytes = 23 bytes.
// (A zero-length payload is legal per spec; we require at least 23 bytes.)
static constexpr std::string::size_type kAcarsCrcLen = 2;
static constexpr std::string::size_type kAcarsHeaderLen = 21;
static constexpr std::string::size_type kAcarsMinLen = kAcarsHeaderLen + kAcarsCrcLen;
// CRC-16/CCITT: poly 0x1021, init 0x0000, no reflection, no final XOR.
// This is the variant used by ACARS (same as XMODEM CRC).
static uint16_t acars_crc16(const std::string& data, std::string::size_type len) {
uint16_t crc = 0x0000;
for (std::string::size_type i = 0; i < len; ++i) {
crc ^= static_cast<uint16_t>(static_cast<uint8_t>(data[i])) << 8;
for (int bit = 0; bit < 8; ++bit) {
if (crc & 0x8000)
crc = static_cast<uint16_t>((crc << 1) ^ 0x1021);
else
crc = static_cast<uint16_t>(crc << 1);
}
}
return crc;
}
AcarsDecoded acars_decode(const std::string& raw) {
AcarsDecoded result;
if (raw.size() < kAcarsMinLen) {
result.txt = "ACARS message too short (" + std::to_string(raw.size()) +
" bytes, need " + std::to_string(kAcarsMinLen) + ")";
return result;
}
// Verify CRC: computed over everything except the last 2 bytes,
// then compared against those 2 bytes (MSB first).
const std::string::size_type payload_len = raw.size() - kAcarsCrcLen;
const uint16_t computed = acars_crc16(raw, payload_len);
const uint16_t received = (static_cast<uint16_t>(static_cast<uint8_t>(raw[raw.size() - 2])) << 8) |
static_cast<uint16_t>(static_cast<uint8_t>(raw[raw.size() - 1]));
result.crc_ok = (computed == received);
result.reg = raw.substr(1, 7);
result.label = raw.substr(9, 2);
result.block_id = raw[11];
result.msg_num = raw.substr(12, 3);
result.flight_id = raw.substr(15, 6);
// Payload sits between end of fixed header and the 2 trailing CRC bytes.
if (payload_len > kAcarsHeaderLen)
result.txt = raw.substr(kAcarsHeaderLen, payload_len - kAcarsHeaderLen);
return result;
}
std::string acars_format(const AcarsDecoded& msg) {
return std::string("ACARS Decoded Result\nCRC: ") + (msg.crc_ok ? "OK" : "FAIL") +
"\nRegistration: " + msg.reg +
"\nLabel: " + msg.label +
"\nBlockID: " + msg.block_id +
"\nMsgNum: " + msg.msg_num +
"\nFlightID: " + msg.flight_id +
"\nMessage: " + msg.txt;
}
void ACARSLogger::log_str(std::string msg) { void ACARSLogger::log_str(std::string msg) {
log_file.write_entry(msg); log_file.write_entry(msg);
} }
@@ -77,17 +147,16 @@ void ACARSAppView::focus() {
void ACARSAppView::on_packet(const ACARSPacketMessage* packet) { void ACARSAppView::on_packet(const ACARSPacketMessage* packet) {
std::string console_info; std::string console_info;
if (packet->state == 255) { if (packet->state == 255) {
// got a packet, parse it, and display // got a packet, parse it, and display
rtc::RTC datetime; rtc::RTC datetime;
rtc_time::now(datetime); rtc_time::now(datetime);
// todo parity error recovery
console_info = to_string_datetime(datetime, HMS); console_info = to_string_datetime(datetime, HMS);
console_info += ": "; console_info += ": ";
console_info += packet->message; std::string message{packet->message, packet->message + packet->msg_len};
AcarsDecoded decoded = acars_decode(message);
console_info += acars_format(decoded);
console.writeln(console_info); console.writeln(console_info);
// Log raw data whatever it contains
if (logger && logging) if (logger && logging)
logger->log_str(console_info); logger->log_str(console_info);
} else { } else {
+20
View File
@@ -33,6 +33,26 @@
namespace ui::external_app::acars_rx { namespace ui::external_app::acars_rx {
// Decoded ACARS message fields extracted from a raw frame.
// CRC-16/CCITT (poly 0x1021, init 0x0000) is verified against the two
// trailing bytes of the raw frame; crc_ok reflects that result.
struct AcarsDecoded {
bool crc_ok{false};
std::string reg{};
std::string label{};
std::string flight_id{};
std::string msg_num{};
char block_id{'\0'};
std::string txt{};
};
// Decode a raw ACARS frame: verify CRC-16/CCITT and extract fixed-offset fields.
// Returns a partially-filled AcarsDecoded (txt error only) if the frame is too short.
AcarsDecoded acars_decode(const std::string& raw);
// Format a decoded ACARS message for display or logging.
std::string acars_format(const AcarsDecoded& msg);
class ACARSLogger { class ACARSLogger {
public: public:
Optional<File::Error> append(const std::filesystem::path& filename) { Optional<File::Error> append(const std::filesystem::path& filename) {
+3 -3
View File
@@ -113,8 +113,8 @@ set(EXTCPPSRC
external/random_password/sha512.cpp external/random_password/sha512.cpp
#acars #acars
#external/acars_rx/main.cpp external/acars_rx/main.cpp
#external/acars_rx/acars_app.cpp external/acars_rx/acars_app.cpp
#wefax_rx 192 bytes #wefax_rx 192 bytes
external/wefax_rx/main.cpp external/wefax_rx/main.cpp
@@ -337,7 +337,7 @@ set(EXTAPPLIST
sstvtx sstvtx
sstvrx sstvrx
random_password random_password
# acars_rx --not working acars_rx
wefax_rx wefax_rx
noaaapt_rx noaaapt_rx
shoppingcart_lock shoppingcart_lock