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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137// SPDX-FileCopyrightText: © 2020-2021 Oliver Jowett <oliver@mutability.co.uk>// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: GPL-2.0-or-later
//! A block of magnitude samples, on its way from the radio to the//! demodulator.//!//! Samples arrive from the radio in blocks, and a transmission does not//! politely fit inside one: a message that begins near the end of a block//! runs off the end of it. So each block carries an *overlap* — a copy of the//! tail of the previous block at its front, long enough to hold the longest//! possible message. The demodulator hunts for signals only in the part//! before the trailing overlap; anything starting later is left for the next//! block, where it will appear in the leading overlap with all its samples//! present.//!//! ```text//! 0 overlap valid-overlap valid//! | | | |//! | tail of the | new samples | new samples that|//! | previous block| | become the next |//! | | | block's overlap |//! | | | |//! | | <- signals are looked for | |//! | | starting in here -> | |//! ```
const std = @import("std");const dsp = @import("dsp.zig");const modes = @import("modes.zig");
/// Samples per microsecond at the rate everything here assumes.pub const sample_rate = 2_400_000;
/// The receiver clock the timestamps are quoted in, in Hz. Mode S equipment/// has counted in twelfths of a microsecond since long before anything/// sampled at 2.4 MHz, and the Beast protocol carries the same units.pub const receive_clock_rate = 12_000_000;
/// The smallest overlap that can hold any message the demodulator might find:/// the preamble it looks past, one sample of slack, and the longest message.pub const minimum_overlap = 19 + 1 + 269;
/// The overlap actually used: the preamble, the longest message, and sixteen/// bits of margin, converted from microseconds to samples.pub const trailing_samples = (8 + 112 + 16) * sample_rate / 1_000_000;
/// Samples per block. About a twentieth of a second at 2.4 MHz, and exactly/// what one USB transfer from an RTL-SDR holds.pub const default_block_samples = 16 * 16384 / 2;
/// How many blocks to keep in flight. Fewer than the radio has transfers/// outstanding, so that running out of blocks throttles the radio rather than/// the other way about.pub const default_block_count = 12;
/// One block of magnitude samples.pub const MagBuf = struct { /// The samples. Its length is the allocated size; `valid` says how much /// of it holds data. data: []u16, /// How many samples are meaningful, counting the leading overlap. valid: usize, /// How many samples at the front are a copy of the previous block's tail, /// and equally how many at the back will be copied to the next block. overlap: usize,
/// The receiver's 12 MHz clock at the first sample. sample_timestamp: u64, /// Wall-clock milliseconds at the first sample. system_timestamp_ms: u64,
/// Whether samples were lost between the previous block and this one, so /// that the overlap does not in fact continue it. discontinuous: bool, /// Roughly how many samples were lost, when `discontinuous`. dropped: usize,
/// The mean level and power of this block, measured during conversion. level: dsp.Level,
/// The samples the demodulator should hunt through: everything except the /// trailing overlap, which belongs to the next block. pub fn searchable(self: *const MagBuf) []const u16 { return self.data[0 .. self.valid - self.overlap]; }
/// Everything valid, including the trailing overlap that a message found /// near the end will run into. pub fn samples(self: *const MagBuf) []const u16 { return self.data[0..self.valid]; }
/// Where new samples should be written: after the leading overlap. pub fn writable(self: *MagBuf) []u16 { return self.data[self.overlap..]; }
/// How many receiver-clock ticks a number of samples takes: five ticks /// per sample at 2.4 MHz. pub fn elapsedClock(sample_count: usize) u64 { return @as(u64, sample_count) * (receive_clock_rate / sample_rate); }};
/// Milliseconds between two receiver-clock timestamps.pub fn clockElapsedMs(from: u64, to: u64) u64 { return (to -% from) / (receive_clock_rate / 1000);}
const testing = std.testing;
test "the searchable region stops short of the trailing overlap" { var data: [1000]u16 = @splat(0); var buf: MagBuf = .{ .data = &data, .valid = 900, .overlap = 300, .sample_timestamp = 0, .system_timestamp_ms = 0, .discontinuous = false, .dropped = 0, .level = .{ .mean_level = 0, .mean_power = 0 }, };
try testing.expectEqual(@as(usize, 600), buf.searchable().len); try testing.expectEqual(@as(usize, 900), buf.samples().len); try testing.expectEqual(@as(usize, 700), buf.writable().len);}
test "clock arithmetic is in twelfths of a microsecond" { try testing.expectEqual(@as(u64, 5), MagBuf.elapsedClock(1)); try testing.expectEqual(@as(u64, 12_000_000), MagBuf.elapsedClock(sample_rate)); try testing.expectEqual(@as(u64, 1000), clockElapsedMs(0, 12_000_000)); try testing.expectEqual(@as(u64, 0), clockElapsedMs(1000, 1100));}