// SPDX-FileCopyrightText: © 2014-2016 Oliver Jowett // SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: GPL-2.0-or-later //! A record of which aircraft have been heard from recently. //! //! Several Mode S downlink formats carry no address of their own: the //! transponder overlays the address onto the parity field, so a receiver //! recovers it by assuming the frame is intact and taking whatever the CRC //! comes out as. That "address" is real if the frame was intact and complete //! noise if it was not, and nothing in the frame itself distinguishes the two. //! //! What distinguishes them is history. An address that has been seen in a //! frame that *did* carry a checkable address is almost certainly a real //! aircraft; one that has never been seen is almost certainly a bad decode. So //! every verified address goes in here, and the address-overlaid formats are //! accepted only when they name something already known. //! //! Entries have to age out, or an hour of noise eventually vouches for //! everything. Rather than time-stamping each entry, two tables are kept and //! written to alternately: a lookup consults both, an insert goes only to the //! active one, and a flip every minute empties the older. An address therefore //! stays known for between one and two minutes after it was last heard. const std = @import("std"); /// A record of recently-seen ICAO addresses. /// /// This is an open-addressed hash table with linear probing, sized so that it /// is nowhere near full at realistic traffic levels — a busy receiver hears a /// few hundred aircraft, not a few thousand. pub const Filter = struct { /// Slots per table. Must be a power of two. pub const size = 4096; /// How long between flips. An address survives at least this long after /// it was last added, and at most twice this long. pub const ttl_ms = 60_000; /// The value stored in a slot nothing has been put in. Not a valid entry: /// a real address is 24 bits, plus the `adsb_nt` marker at bit 25. const vacant: u32 = 0xffff_ffff; /// Marker bit combined with an address to record that it was heard from a /// non-transponder ADS-B emitter — a DF18 sender, which is ground /// equipment or a vehicle rather than an aircraft. /// /// Storing those separately keeps a DF18 broadcast from vouching for a /// DF11 that claims to be the same address, since the two are different /// kinds of thing that happen to share a number. pub const adsb_nt: u32 = 1 << 25; tables: [2][size]u32, /// Which table `add` writes to. Both are consulted by `contains`. active: u1, /// When the next flip is due, on the same clock `expire` is passed. next_flip_ms: u64, /// A filter that has heard nothing. The first call to `expire` will flip, /// whatever time it is given, which costs nothing on empty tables. pub const init: Filter = .{ .tables = @splat(@splat(vacant)), .active = 0, .next_flip_ms = 0, }; /// Record that `address` has been heard from. /// /// Returns false if the active table was full and the address was /// dropped, which cannot happen at any plausible traffic level. The cost /// of it happening is lost messages rather than wrong ones: an address /// that is not in the filter simply fails to vouch for the /// address-overlaid frames that name it. pub fn add(self: *Filter, address: u32) bool { const table = &self.tables[self.active]; const start = hash(address); var slot = start; while (table[slot] != vacant and table[slot] != address) { slot = (slot + 1) % size; if (slot == start) return false; } table[slot] = address; return true; } /// Whether `address` has been heard from recently enough to still be in /// either table. pub fn contains(self: *const Filter, address: u32) bool { for (&self.tables) |*table| { const start = hash(address); var slot = start; while (table[slot] != vacant and table[slot] != address) { slot = (slot + 1) % size; if (slot == start) break; } if (table[slot] == address) return true; } return false; } /// Age out old entries, if it is time to. Call this periodically; it /// decides for itself whether anything is due. /// /// `now_ms` is a monotonic millisecond count. Passing a clock in rather /// than reading one is what makes the ageing testable. pub fn expire(self: *Filter, now_ms: u64) void { if (now_ms < self.next_flip_ms) return; // Empty the table that is about to become active, so what it collects // from here on is only what is heard from here on. self.active = ~self.active; self.tables[self.active] = @splat(vacant); self.next_flip_ms = now_ms + ttl_ms; } /// Jenkins' one-at-a-time hash over the three address bytes. /// /// Only the low 24 bits are hashed, so an address and its `adsb_nt` /// counterpart land in the same bucket and are told apart by the stored /// value. They are different entries, not the same one. fn hash(address: u32) u32 { var h: u32 = 0; for (0..3) |byte| { h +%= (address >> @intCast(byte * 8)) & 0xff; h +%= h << 10; h ^= h >> 6; } h +%= h << 3; h ^= h >> 11; h +%= h << 15; return h % size; } }; const testing = std.testing; test "an address is remembered once added" { var filter: Filter = .init; try testing.expect(!filter.contains(0x4840d6)); try testing.expect(filter.add(0x4840d6)); try testing.expect(filter.contains(0x4840d6)); try testing.expect(!filter.contains(0x4840d7)); } test "an ADS-B NT emitter is a separate entry from an aircraft" { var filter: Filter = .init; try testing.expect(filter.add(0x4840d6)); try testing.expect(!filter.contains(0x4840d6 | Filter.adsb_nt)); try testing.expect(filter.add(0x4840d6 | Filter.adsb_nt)); try testing.expect(filter.contains(0x4840d6)); try testing.expect(filter.contains(0x4840d6 | Filter.adsb_nt)); } test "adding the same address twice uses one slot" { var filter: Filter = .init; for (0..1000) |_| _ = filter.add(0x4840d6); var occupied: usize = 0; for (filter.tables[filter.active]) |slot| { if (slot != 0xffff_ffff) occupied += 1; } try testing.expectEqual(@as(usize, 1), occupied); } test "an address survives one flip and not two" { var filter: Filter = .init; filter.expire(0); try testing.expect(filter.add(0x4840d6)); // Still known just before the flip, and still known just after it: the // table it went into is now the stale one, but stale entries are still // consulted. filter.expire(Filter.ttl_ms - 1); try testing.expect(filter.contains(0x4840d6)); filter.expire(Filter.ttl_ms); try testing.expect(filter.contains(0x4840d6)); // The second flip empties the table it was in. filter.expire(Filter.ttl_ms * 2); try testing.expect(!filter.contains(0x4840d6)); } test "the filter holds a realistic number of aircraft without probing far" { var filter: Filter = .init; // Two thousand addresses is far more than any receiver hears at once, and // is half the table. for (0..2000) |i| { try testing.expect(filter.add(@intCast(0x400000 + i * 7))); } for (0..2000) |i| { try testing.expect(filter.contains(@intCast(0x400000 + i * 7))); } try testing.expect(!filter.contains(0x400001)); } test "a full table refuses further addresses rather than looping" { var filter: Filter = .init; var added: usize = 0; for (0..Filter.size * 2) |i| { if (filter.add(@intCast(i))) added += 1; } try testing.expectEqual(@as(usize, Filter.size), added); try testing.expect(!filter.add(0xabcdef)); }