// SPDX-FileCopyrightText: © 2014-2016 Oliver Jowett // SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: GPL-2.0-or-later //! The six-bit character set aircraft identification is written in. //! //! A callsign is eight characters packed six bits each, in the subset of //! ASCII that ICAO Annex 10 borrows from the AIS standard: the alphabet, the //! digits, and a space. The other codes are not supposed to appear at all, //! and a transponder that sends one is decoded rather than rejected — what //! comes out is the punctuation this table maps them to, which is how a //! misbehaving aircraft shows up as `@#$%` in a listing instead of vanishing. const std = @import("std"); const bits = @import("bits.zig"); /// The 64 characters a six-bit code can name. pub const charset: [64]u8 = "@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_ !\"#$%&'()*+,-./0123456789:;<=>?".*; /// How many characters a callsign holds. pub const callsign_len = 8; /// Read the eight-character callsign that begins at `first_bit` of `message`. /// /// Trailing spaces are kept, because they are part of what was transmitted and /// the caller — which knows whether it is filling a fixed-width display or a /// JSON string — is the one that should decide whether to trim them. pub fn decodeCallsign(message: []const u8, first_bit: u16) [callsign_len]u8 { var callsign: [callsign_len]u8 = undefined; for (&callsign, 0..) |*character, i| { const bit = first_bit + @as(u16, @intCast(i)) * 6; character.* = charset[bits.get(message, bit, bit + 5)]; } return callsign; } const testing = std.testing; test "the charset covers every six-bit code" { try testing.expectEqual(@as(usize, 64), charset.len); try testing.expectEqual(@as(u8, '@'), charset[0]); try testing.expectEqual(@as(u8, 'A'), charset[1]); try testing.expectEqual(@as(u8, 'Z'), charset[26]); try testing.expectEqual(@as(u8, ' '), charset[32]); try testing.expectEqual(@as(u8, '0'), charset[48]); try testing.expectEqual(@as(u8, '9'), charset[57]); try testing.expectEqual(@as(u8, '?'), charset[63]); } test "a callsign is eight six-bit characters" { // A real DF17 identification message, from aircraft 48:40:D6. const message = [_]u8{ 0x8d, 0x48, 0x40, 0xd6, 0x20, 0x2c, 0xc3, 0x71, 0xc3, 0x2c, 0xe0, 0x57, 0x60, 0x98 }; // ME starts at bit 33, and the callsign at its bit 9. try testing.expectEqualStrings("KLM1023 ", &decodeCallsign(message[4..], 9)); }