Something went wrong. Try again.
A port of FlightAware's dump1090, a Mode S and ADS-B decoder for software defined radios, to Zig 0.16.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111// SPDX-FileCopyrightText: © 2014-2016 Oliver Jowett <oliver@mutability.co.uk>// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: GPL-2.0-or-later
//! Pulling fields out of a Mode S frame.//!//! Every published description of a Mode S message numbers its bits from 1,//! starting at the most significant bit of the first byte, and gives each//! field as an inclusive range — "ME is bits 33-88". These functions take//! their arguments in exactly that form, so a decoder can be read against the//! document it came from without an off-by-one in between.
const std = @import("std");const assert = std.debug.assert;
/// One bit of a frame, numbered from 1.pub fn get1(message: []const u8, bit: u16) u1 { assert(bit >= 1); const index = bit - 1; return @truncate(message[index / 8] >> @intCast(7 - (index % 8)));}
/// Whether one bit of a frame, numbered from 1, is set.pub fn flag(message: []const u8, bit: u16) bool { return get1(message, bit) != 0;}
/// Bits `first` through `last` of a frame, inclusive and numbered from 1, as/// an unsigned integer with the first bit most significant.////// At most 32 bits, which is every field Mode S defines; the payload of a/// Comm-B or an extended squitter is read out a field at a time rather than/// whole.pub fn get(message: []const u8, first: u16, last: u16) u32 { assert(first >= 1); assert(first <= last); assert(last - first + 1 <= 32);
const first_index = first - 1; const last_index = last - 1;
// A field of at most 32 bits touches at most five bytes, which fits in a // u64 with room to spare. var accumulator: u64 = 0; for (message[first_index / 8 .. last_index / 8 + 1]) |byte| { accumulator = (accumulator << 8) | byte; }
// Drop whatever followed the last bit inside its byte, then everything // before the first. accumulator >>= @intCast(7 - (last_index % 8)); const width: u6 = @intCast(last - first + 1); const mask = (@as(u64, 1) << width) - 1;
return @intCast(accumulator & mask);}
const testing = std.testing;
test "single bits are numbered from one, most significant first" { const message = [_]u8{ 0b1000_0001, 0b0100_0010 }; try testing.expectEqual(@as(u1, 1), get1(&message, 1)); try testing.expectEqual(@as(u1, 0), get1(&message, 2)); try testing.expectEqual(@as(u1, 1), get1(&message, 8)); try testing.expectEqual(@as(u1, 0), get1(&message, 9)); try testing.expectEqual(@as(u1, 1), get1(&message, 10)); try testing.expectEqual(@as(u1, 1), get1(&message, 15)); try testing.expect(flag(&message, 1)); try testing.expect(!flag(&message, 2));}
test "fields are inclusive ranges" { const message = [_]u8{ 0x8d, 0x48, 0x40, 0xd6, 0x20, 0x2c, 0xc3, 0x71, 0xc3, 0x2c, 0xe0, 0x57, 0x60, 0x98 };
// DF is bits 1-5, CA is 6-8, AA is 9-32, ME is 33-88, PI is 89-112. try testing.expectEqual(@as(u32, 17), get(&message, 1, 5)); try testing.expectEqual(@as(u32, 5), get(&message, 6, 8)); try testing.expectEqual(@as(u32, 0x4840d6), get(&message, 9, 32)); try testing.expectEqual(@as(u32, 0x20), get(&message, 33, 40)); try testing.expectEqual(@as(u32, 0xc32ce0), get(&message, 65, 88)); try testing.expectEqual(@as(u32, 0x576098), get(&message, 89, 112));
// A whole 32-bit field, and one that straddles five bytes. try testing.expectEqual(@as(u32, 0x8d4840d6), get(&message, 1, 32)); try testing.expectEqual(@as(u32, 0xd4840d62), get(&message, 5, 36));}
test "a field of one bit agrees with get1" { const message = [_]u8{ 0x8d, 0x48, 0x40, 0xd6, 0x20, 0x2c, 0xc3, 0x71 }; for (1..message.len * 8 + 1) |bit| { try testing.expectEqual( @as(u32, get1(&message, @intCast(bit))), get(&message, @intCast(bit), @intCast(bit)), ); }}
test "a field is the concatenation of its bits" { const message = [_]u8{ 0x8d, 0x48, 0x40, 0xd6, 0x20, 0x2c, 0xc3, 0x71, 0xc3, 0x2c, 0xe0, 0x57, 0x60, 0x98 }; for (1..113) |first| { var last = first; while (last < @min(first + 32, 113)) : (last += 1) { var expected: u32 = 0; for (first..last + 1) |bit| { expected = (expected << 1) | get1(&message, @intCast(bit)); } try testing.expectEqual(expected, get(&message, @intCast(first), @intCast(last))); } }}