// SPDX-FileCopyrightText: © 2012 Salvatore Sanfilippo // SPDX-FileCopyrightText: © 2014-2016 Oliver Jowett // SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: GPL-2.0-or-later //! Constants describing the Mode S air interface itself, as opposed to any //! particular part of the decoder. //! //! A Mode S downlink transmission is either 56 or 112 bits long, and which of //! the two it is follows from the five-bit downlink format in the first byte. //! Nothing else about the frame is variable, so these two lengths turn up in //! every layer of the program, from the demodulator's correlation windows to //! the width of a Beast output frame. const std = @import("std"); /// How long a Mode S downlink frame is. There are only two possibilities, and /// which one applies is decided by the downlink format field. pub const MessageLength = enum { /// A 56-bit (7 byte) frame: DF 0, 4, 5, 11 and the unassigned formats /// below 16. short, /// A 112-bit (14 byte) frame: DF 16, 17, 18, 20, 21, 24 and friends. long, /// The frame length in bits, which is what the demodulator counts. pub fn bits(self: MessageLength) u8 { return switch (self) { .short => 56, .long => 112, }; } /// The frame length in bytes, which is what everything above the /// demodulator counts. pub fn bytes(self: MessageLength) u8 { return switch (self) { .short => 7, .long => 14, }; } /// The length implied by a downlink format. Every DF from 16 up is long; /// everything below it is short. pub fn fromDownlinkFormat(df: u5) MessageLength { return if (df >= 16) .long else .short; } /// The length a byte count corresponds to, or null if it is neither of the /// two valid frame sizes. pub fn fromBytes(n: usize) ?MessageLength { return switch (n) { 7 => .short, 14 => .long, else => null, }; } }; /// The longest a frame can be, in bytes; the size to give a fixed buffer that /// has to hold any frame. pub const max_message_bytes = MessageLength.long.bytes(); /// The longest a frame can be, in bits. pub const max_message_bits = MessageLength.long.bits(); /// The downlink format, the first five bits of every Mode S frame. pub fn downlinkFormat(message: []const u8) u5 { return @truncate(message[0] >> 3); } test "message lengths follow the downlink format" { try std.testing.expectEqual(MessageLength.short, MessageLength.fromDownlinkFormat(11)); try std.testing.expectEqual(MessageLength.long, MessageLength.fromDownlinkFormat(17)); try std.testing.expectEqual(@as(u8, 7), MessageLength.short.bytes()); try std.testing.expectEqual(@as(u8, 14), MessageLength.long.bytes()); try std.testing.expectEqual(MessageLength.long, MessageLength.fromBytes(14).?); try std.testing.expectEqual(@as(?MessageLength, null), MessageLength.fromBytes(9)); } test "the downlink format is the top five bits" { try std.testing.expectEqual(@as(u5, 17), downlinkFormat(&[_]u8{0x8d})); }