// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: GPL-2.0-or-later //! What the program was asked to do, and how it is told. //! //! There are two shapes here and they are deliberately different. `Config` is //! what the program runs on: flat, in the units the code works in, and //! assembled from whatever sources. `File` is what a person writes: grouped //! by the thing being configured, in the units a person thinks in — degrees, //! seconds, nautical miles — and with every field optional so that a config //! says only what it changes. //! //! The file is ZON, which is Zig's own object notation. That means no parser //! to write and no dependency to carry: `std.zon` reads it straight into the //! struct below, and a field that is misspelled or of the wrong type is a //! parse error naming the line rather than a value silently ignored. const std = @import("std"); const Allocator = std.mem.Allocator; const Io = std.Io; const adaptive = @import("adaptive.zig"); const adept = @import("adept.zig"); const convert = @import("convert.zig"); const crc = @import("crc.zig"); const json = @import("json.zig"); const mode_s = @import("mode_s.zig"); const net = @import("net.zig"); const output = @import("output.zig"); const sdr = @import("sdr.zig"); const sdr_rtlsdr = @import("sdr_rtlsdr.zig"); const track = @import("track.zig"); /// Everything the program was asked to do. pub const Config = struct { source: sdr.Options = .{}, decode: mode_s.Options = .{}, receiver: track.Receiver = .{}, adaptive: adaptive.Options = .{}, /// Write every frame to standard output in the AVR text format. raw: bool = false, /// Prefix raw output with the receiver's clock. mlat_timestamps: bool = false, /// Also demodulate Mode A/C replies. mode_ac: bool = false, /// The network outputs. net: net.Options = .{}, /// Whether to listen on anything at all. net_enabled: bool = false, /// The FlightAware uplink, if one is configured. Absent means the /// program never talks to FlightAware at all. flightaware: ?adept.Options = null, /// Where to keep the identifier FlightAware issues on first login, so /// that the next login can offer it and be recognised as the same feed. feeder_id_file: ?[]const u8 = null, /// Print a statistics report when the program stops. stats_at_exit: bool = false, /// Print one every this many milliseconds, or zero for never. stats_every_ms: u64 = 0, /// Draw the range histogram in the report. stats_range_histogram: bool = false, /// Where to write the JSON files the map polls, if anywhere. json_dir: ?[]const u8 = null, /// How often to rewrite them. json_interval_ms: u64 = 1000, /// How precisely to publish the receiver's own position. Approximate by /// default, as upstream has it: enough for a map to centre itself, /// not enough to be an address. json_location_accuracy: json.LocationAccuracy = .approximate, /// How often to rewrite the statistics into the JSON directory. json_stats_interval_ms: u64 = 60_000, /// Show only this ICAO address on standard output, if set. The address /// is also seeded into the decoder's filter, so that a receiver watching /// for one aircraft accepts its DF11 replies from the first message /// rather than having to see it announce itself first. show_only: ?u32 = null, /// Print nothing but the ICAO addresses. only_address: bool = false, /// Say nothing on standard output at all. quiet: bool = false, }; /// The shape of a configuration file. /// /// Every field is optional, and one left out keeps whatever the program /// already had — so a file says what it changes and nothing else, and a /// default that moves in a later version moves for everyone who did not /// override it. pub const File = struct { receiver: ?Receiver = null, sdr: ?Sdr = null, decode: ?Decode = null, outputs: ?Outputs = null, inputs: ?Inputs = null, connectors: []const Connector = &.{}, json: ?Json = null, stats: ?Stats = null, fatsv: ?Fatsv = null, flightaware: ?FlightAware = null, /// The FlightAware uplink. Present means feed FlightAware; absent means /// never talk to them at all. /// /// This is what PiAware is for, without PiAware: no child processes, no /// services restarted, no network reconfigured, and nothing accepted /// from their server that would run software on this machine. pub const FlightAware = struct { /// The identity FlightAware keys a site on. It has to be stable /// across restarts, and there is no sensible default to invent, so /// it is required — write the one an existing PiAware install used /// and the site carries over rather than a second being created. mac: []const u8, /// The identifier the server issued at a previous login. Usually /// left out in favour of `feeder_id_file`, which keeps it up to /// date without the file being edited by hand. feeder_id: ?[]const u8 = null, /// Where to read and write that identifier. The file is created on /// the first successful login. feeder_id_file: ?[]const u8 = null, /// Somewhere else to connect, for testing against something that is /// not FlightAware. host: ?[]const u8 = null, port: ?u16 = null, /// The codec version to declare at login. Leave this out unless /// FlightAware has said otherwise: nothing here implements their /// compact encoding, so declaring a version would promise something /// this program cannot deliver. compression_version: ?[]const u8 = null, }; /// Where the antenna is, and how far it can plausibly hear. pub const Receiver = struct { /// Degrees north, needed before any position can be resolved from a /// single message. lat: ?f64 = null, /// Degrees east. lon: ?f64 = null, /// The furthest an aircraft can plausibly be, in nautical miles. /// Zero disables the range checks, and with them the ability to /// resolve a position from one message. max_range_nm: ?f64 = null, }; /// Where the samples come from. pub const Sdr = struct { /// `none`, `ifile` or `rtlsdr`. device: ?sdr.Kind = null, /// Which radio: an index, or a serial number, or part of one. serial: ?[]const u8 = null, /// The tuner gain: `.max`, `.tuner_agc`, or `.{ .db = 25.4 }`. gain: ?sdr_rtlsdr.Gain = null, /// Hertz. frequency: ?u32 = null, /// Oscillator correction, parts per million. ppm: ?i32 = null, /// The radio's own digital gain control, which is not the tuner's. digital_agc: ?bool = null, /// Remove the DC component. filter_dc: ?bool = null, /// Read samples from a file, or `-` for standard input. file: ?[]const u8 = null, /// The sample format of that file: `uc8`, `sc16` or `sc16q11`. format: ?convert.InputFormat = null, /// Read a recording at the speed it was captured. throttle: ?bool = null, /// Direct sampling, for receiving below the tuner's range: `off`, /// `i_branch` or `q_branch`. Not useful at 1090 MHz, but the /// hardware can do it. direct_sampling: ?DirectSampling = null, adaptive: ?Adaptive = null, pub const DirectSampling = enum(u2) { off = 0, i_branch = 1, q_branch = 2 }; }; /// The gain control that turns the knob itself. pub const Adaptive = struct { /// Lower the gain when too many loud messages fail to decode. burst: ?bool = null, /// Move the gain to hold a target amount of headroom. range: ?bool = null, /// The headroom to aim for, in decibels. range_target_db: ?f32 = null, /// The quietest and loudest steps the controller may choose. min_gain_db: ?f32 = null, max_gain_db: ?f32 = null, /// The percentage of samples to measure. Measuring costs processor /// time and half of each second is plenty of evidence. duty_cycle_percent: ?f32 = null, // The rest are the controllers' tuning. The defaults are upstream's // and are what almost every receiver should use; they are here so // that a difficult site can be tuned without a rebuild. Intervals // are counted in blocks, and a block is about a second. burst_alpha: ?f32 = null, burst_change_delay_s: ?u32 = null, burst_loud_rate: ?f32 = null, burst_loud_runlength: ?u32 = null, burst_quiet_rate: ?f32 = null, burst_quiet_runlength: ?u32 = null, range_alpha: ?f32 = null, range_percentile: ?u32 = null, range_change_delay_s: ?u32 = null, range_scan_delay_s: ?u32 = null, range_rescan_delay_s: ?u32 = null, }; /// How hard to try, and what to decode. pub const Decode = struct { /// How many bit errors to correct: `none`, `one` or `two`. fix: ?crc.FixBits = null, /// Try to correct the downlink format field as well. fix_df: ?bool = null, /// Also decode Mode A/C replies. modeac: ?bool = null, /// Let Beast readers turn Mode A/C on and off. modeac_auto: ?bool = null, /// Decode Comm-D extended length messages. df24: ?bool = null, }; /// Ports to listen on, and how output is paced. A port left out is not /// listened on at all. pub const Outputs = struct { beast: ?u16 = null, beast_verbatim: ?u16 = null, raw: ?u16 = null, sbs: ?u16 = null, stratux: ?u16 = null, fatsv: ?u16 = null, /// The address to listen on. Left out, everything. bind: ?[]const u8 = null, /// Beast output passes frames on uncorrected. verbatim: ?bool = null, /// Pass multilateration results on to Beast readers. forward_mlat: ?bool = null, /// Prefix raw output with the receiver's clock. mlat_timestamps: ?bool = null, /// Report geometric altitudes in BaseStation output, suffixed H. use_gnss: ?bool = null, /// Seconds of silence before a heartbeat, or zero for none. heartbeat_s: ?f64 = null, /// Write once this much output has piled up... flush_bytes: ?usize = null, /// ...or once this long has passed. flush_interval_s: ?f64 = null, /// How often a reduced feed may repeat an aircraft. reduce_interval_s: ?f64 = null, }; /// Ports to accept frames from other receivers on. pub const Inputs = struct { beast: ?u16 = null, raw: ?u16 = null, }; /// A feed to push output to, rather than wait to be asked for it. pub const Connector = struct { host: []const u8, port: u16, /// `beast_out`, `beast_reduce_out`, `beast_reduce_plus_out`, /// `beast_verbatim_out`, `raw_out`, `sbs_out`, `stratux_out` or /// `fatsv_out`. protocol: Protocol, /// The identifier some aggregators recognise a feed by. Only /// `beast_reduce_plus_out` sends one. uuid: ?[]const u8 = null, pub const Protocol = enum { beast_out, beast_reduce_out, beast_reduce_plus_out, beast_verbatim_out, raw_out, sbs_out, stratux_out, fatsv_out, }; }; /// The files a map polls. pub const Json = struct { /// Where to write them. Left out, they are not written. dir: ?[]const u8 = null, /// How often to rewrite `aircraft.json`. interval_s: ?f64 = null, /// And `stats.json`. stats_interval_s: ?f64 = null, /// How precisely to publish the receiver's own position: /// `none`, `approximate` or `exact`. location_accuracy: ?json.LocationAccuracy = null, }; /// The tab-separated change feed. pub const Fatsv = struct { /// Scales how often each aircraft is reported: larger, more often. rate_multiplier: ?f64 = null, /// Forward Comm-B replies that could not be identified, for /// analysis at the other end. upload_unknown_commb: ?bool = null, }; /// The statistics report. pub const Stats = struct { /// Print one when the program stops. at_exit: ?bool = null, /// And every this many seconds. every_s: ?f64 = null, /// Include the range histogram. range_histogram: ?bool = null, }; }; /// Why a configuration file could not be used. pub const LoadError = error{ /// The file is not there, or could not be read. Unreadable, /// It is there and is not valid ZON, or names something unknown. Invalid, } || Allocator.Error; /// Read a configuration file and fold it onto `config`. /// /// Parsed into `arena`, and the paths, hosts and identifiers in the result /// point into it — so the arena has to outlive the config rather than being /// released when this returns. An arena rather than individual frees because /// a configuration is read once and lives as long as the program. /// /// `gpa` is only for `connectors`, which the caller owns. pub fn apply( arena: Allocator, gpa: Allocator, config: *Config, source: [:0]const u8, connectors: *std.ArrayList(net.Connector), diagnostics: ?*std.zon.parse.Diagnostics, ) LoadError!void { const file = std.zon.parse.fromSliceAlloc(File, arena, source, diagnostics, .{}) catch |err| switch (err) { error.OutOfMemory => return error.OutOfMemory, error.ParseZon => return error.Invalid, }; if (file.receiver) |r| { if (r.lat != null or r.lon != null) { config.receiver.position = .{ .latitude = r.lat orelse 0, .longitude = r.lon orelse 0, }; } if (r.max_range_nm) |nm| config.receiver.max_range = nm * 1852.0; } if (file.flightaware) |fa| { config.feeder_id_file = fa.feeder_id_file; config.flightaware = .{ .mac = fa.mac, .feeder_id = fa.feeder_id, // An identifier written into the file is `config`; one kept in a // file this program maintains is `cache`. Saying `cache` with no // identifier yet is what asks FlightAware to create a site, so a // configuration with neither cannot have one made for it. .feeder_id_source = if (fa.feeder_id != null) .config else if (fa.feeder_id_file != null) .cache else .none, .host = fa.host orelse adept.default_host, .port = fa.port orelse adept.default_port, .compression_version = fa.compression_version, // Filled in by the caller, which is the only thing that knows // what this build calls itself. .version = "", // Taken from where the receiver already is, rather than asked // for twice and given the chance to disagree. .position = if (config.receiver.position) |p| .{ .latitude = p.latitude, .longitude = p.longitude, } else null, }; } if (file.sdr) |s| { if (s.device) |kind| config.source.kind = kind; if (s.serial) |serial| config.source.device = serial; if (s.gain) |gain| config.source.gain = gain; if (s.frequency) |hz| config.source.frequency = hz; if (s.ppm) |ppm| config.source.ppm = ppm; if (s.digital_agc) |on| config.source.digital_agc = on; if (s.filter_dc) |on| config.source.filter_dc = on; if (s.throttle) |on| config.source.throttle = on; if (s.format) |format| config.source.format = format; if (s.file) |path| { config.source.path = path; // Naming a file is asking to read it, so the kind follows unless // it was said outright. if (s.device == null) config.source.kind = .ifile; } if (s.direct_sampling) |mode| config.source.direct_sampling = @intFromEnum(mode); if (s.adaptive) |a| { if (a.burst) |on| config.adaptive.burst_control = on; if (a.range) |on| config.adaptive.range_control = on; if (a.range_target_db) |db| config.adaptive.range_target = db; if (a.min_gain_db) |db| config.adaptive.min_gain_db = db; if (a.max_gain_db) |db| config.adaptive.max_gain_db = db; if (a.duty_cycle_percent) |p| config.adaptive.duty_cycle = p / 100.0; if (a.burst_alpha) |v| config.adaptive.burst_alpha = v; if (a.burst_change_delay_s) |v| config.adaptive.burst_change_delay = v; if (a.burst_loud_rate) |v| config.adaptive.burst_loud_rate = v; if (a.burst_loud_runlength) |v| config.adaptive.burst_loud_runlength = v; if (a.burst_quiet_rate) |v| config.adaptive.burst_quiet_rate = v; if (a.burst_quiet_runlength) |v| config.adaptive.burst_quiet_runlength = v; if (a.range_alpha) |v| config.adaptive.range_alpha = v; if (a.range_percentile) |v| config.adaptive.range_percentile = v; if (a.range_change_delay_s) |v| config.adaptive.range_change_delay = v; if (a.range_scan_delay_s) |v| config.adaptive.range_scan_delay = v; if (a.range_rescan_delay_s) |v| config.adaptive.range_rescan_delay = v; } } if (file.decode) |d| { if (d.fix) |fix| config.decode.fix_bits = fix; if (d.fix_df) |on| config.decode.fix_df = on; if (d.df24) |on| config.decode.enable_df24 = on; if (d.modeac) |on| config.mode_ac = on; if (d.modeac_auto) |on| config.net.mode_ac_auto = on; } if (file.outputs) |o| { if (o.beast) |port| { config.net.beast_port = port; config.net_enabled = true; } if (o.beast_verbatim) |port| { config.net.beast_verbatim_port = port; config.net_enabled = true; } if (o.raw) |port| { config.net.raw_port = port; config.net_enabled = true; } if (o.sbs) |port| { config.net.sbs_port = port; config.net_enabled = true; } if (o.stratux) |port| { config.net.stratux_port = port; config.net_enabled = true; } if (o.fatsv) |port| { config.net.fatsv_port = port; config.net_enabled = true; } if (o.bind) |address| config.net.bind_address = address; if (o.verbatim) |on| config.net.verbatim = on; if (o.forward_mlat) |on| config.net.forward_mlat = on; if (o.mlat_timestamps) |on| config.mlat_timestamps = on; if (o.use_gnss) |on| config.net.sbs.use_gnss = on; if (o.heartbeat_s) |s| config.net.heartbeat_interval_ms = millis(s); if (o.flush_bytes) |n| config.net.flush_size = @min(n, net.max_flush_size); if (o.flush_interval_s) |s| config.net.flush_interval_ms = @min(millis(s), net.max_flush_interval_ms); if (o.reduce_interval_s) |s| config.net.reduce_interval_ms = millis(s); } if (file.inputs) |i| { if (i.beast) |port| { config.net.beast_input_port = port; config.net_enabled = true; } if (i.raw) |port| { config.net.raw_input_port = port; config.net_enabled = true; } } for (file.connectors) |c| { const protocol: net.Protocol = switch (c.protocol) { .beast_out => .beast_cooked, .beast_reduce_out, .beast_reduce_plus_out => .beast_reduce, .beast_verbatim_out => .beast_verbatim, .raw_out => .raw, .sbs_out => .sbs, .stratux_out => .stratux, .fatsv_out => .fatsv, }; // Only the "plus" spelling carries an identifier, which is the whole // difference between the two. const uuid = if (c.protocol == .beast_reduce_plus_out) c.uuid else null; try connectors.append(gpa, .{ .host = c.host, .port = c.port, .protocol = protocol, .uuid = uuid, }); config.net_enabled = true; } if (file.json) |j| { if (j.dir) |dir| config.json_dir = dir; if (j.interval_s) |s| config.json_interval_ms = millis(s); if (j.stats_interval_s) |s| config.json_stats_interval_ms = millis(s); if (j.location_accuracy) |a| config.json_location_accuracy = a; } if (file.fatsv) |f| { if (f.rate_multiplier) |m| config.net.fatsv.rate_multiplier = m; if (f.upload_unknown_commb) |on| config.net.fatsv.upload_unknown_commb = on; } if (file.stats) |st| { if (st.at_exit) |on| config.stats_at_exit = on; if (st.every_s) |s| { config.stats_every_ms = millis(s); config.stats_at_exit = true; } if (st.range_histogram) |on| { config.stats_range_histogram = on; config.receiver.range_histogram = on; } } } fn millis(seconds: f64) u64 { return @intFromFloat(@max(seconds, 0) * 1000); } const testing = std.testing; fn applyTo( arena: Allocator, config: *Config, source: [:0]const u8, connectors: *std.ArrayList(net.Connector), ) !void { // Deinited with the same allocator the parse used, which is the arena. var diagnostics: std.zon.parse.Diagnostics = .{}; defer diagnostics.deinit(arena); try apply(arena, testing.allocator, config, source, connectors, &diagnostics); } test "an empty file changes nothing" { var config: Config = .{}; var connectors: std.ArrayList(net.Connector) = .empty; defer connectors.deinit(testing.allocator); var arena: std.heap.ArenaAllocator = .init(testing.allocator); defer arena.deinit(); try applyTo(arena.allocator(), &config, ".{}", &connectors); try testing.expectEqual(Config{}, config); try testing.expectEqual(@as(usize, 0), connectors.items.len); } test "a file says only what it changes" { var config: Config = .{}; var connectors: std.ArrayList(net.Connector) = .empty; defer connectors.deinit(testing.allocator); var arena: std.heap.ArenaAllocator = .init(testing.allocator); defer arena.deinit(); try applyTo(arena.allocator(), &config, \\.{ \\ .receiver = .{ .lat = 41.5868, .lon = -93.6250, .max_range_nm = 250 }, \\ .decode = .{ .fix = .two }, \\} , &connectors); try testing.expectApproxEqAbs(@as(f64, 41.5868), config.receiver.position.?.latitude, 1e-9); try testing.expectApproxEqAbs(@as(f64, -93.625), config.receiver.position.?.longitude, 1e-9); // Nautical miles in the file, metres in the program. try testing.expectApproxEqAbs(@as(f64, 250 * 1852.0), config.receiver.max_range, 1e-6); try testing.expectEqual(crc.FixBits.two, config.decode.fix_bits); // Everything it did not mention is still the default. try testing.expectEqual(false, config.mode_ac); try testing.expectEqual(@as(?u16, null), config.net.beast_port); try testing.expectEqual(false, config.net_enabled); } test "naming ports turns networking on, and only those ports" { var config: Config = .{}; var connectors: std.ArrayList(net.Connector) = .empty; defer connectors.deinit(testing.allocator); var arena: std.heap.ArenaAllocator = .init(testing.allocator); defer arena.deinit(); try applyTo(arena.allocator(), &config, \\.{ .outputs = .{ .beast = 31005, .sbs = 31003 } } , &connectors); try testing.expect(config.net_enabled); try testing.expectEqual(@as(?u16, 31005), config.net.beast_port); try testing.expectEqual(@as(?u16, 31003), config.net.sbs_port); // Not asked for, not opened. try testing.expectEqual(@as(?u16, null), config.net.raw_port); try testing.expectEqual(@as(?u16, null), config.net.beast_input_port); } test "seconds in the file are milliseconds in the program" { var config: Config = .{}; var connectors: std.ArrayList(net.Connector) = .empty; defer connectors.deinit(testing.allocator); var arena: std.heap.ArenaAllocator = .init(testing.allocator); defer arena.deinit(); try applyTo(arena.allocator(), &config, \\.{ \\ .outputs = .{ .heartbeat_s = 30, .flush_interval_s = 0.25, .reduce_interval_s = 0.5 }, \\ .json = .{ .dir = "/run/dump1090", .interval_s = 2, .stats_interval_s = 30 }, \\ .stats = .{ .every_s = 60 }, \\} , &connectors); try testing.expectEqual(@as(u64, 30_000), config.net.heartbeat_interval_ms); try testing.expectEqual(@as(u64, 250), config.net.flush_interval_ms); try testing.expectEqual(@as(u64, 500), config.net.reduce_interval_ms); try testing.expectEqualStrings("/run/dump1090", config.json_dir.?); try testing.expectEqual(@as(u64, 2_000), config.json_interval_ms); try testing.expectEqual(@as(u64, 30_000), config.json_stats_interval_ms); // Asking for a report every minute implies wanting one at the end. try testing.expectEqual(@as(u64, 60_000), config.stats_every_ms); try testing.expect(config.stats_at_exit); } test "connectors carry an identifier only where one is sent" { var config: Config = .{}; var connectors: std.ArrayList(net.Connector) = .empty; defer connectors.deinit(testing.allocator); var arena: std.heap.ArenaAllocator = .init(testing.allocator); defer arena.deinit(); try applyTo(arena.allocator(), &config, \\.{ \\ .connectors = .{ \\ .{ .host = "a.example", .port = 30004, .protocol = .beast_reduce_plus_out, \\ .uuid = "0033062d-e17e-4389-91a9-79ebb967fb4c" }, \\ .{ .host = "b.example", .port = 30005, .protocol = .beast_out, \\ .uuid = "0033062d-e17e-4389-91a9-79ebb967fb4c" }, \\ }, \\} , &connectors); try testing.expectEqual(@as(usize, 2), connectors.items.len); try testing.expect(config.net_enabled); try testing.expectEqualStrings("a.example", connectors.items[0].host); try testing.expectEqual(net.Protocol.beast_reduce, connectors.items[0].protocol); try testing.expectEqualStrings("0033062d-e17e-4389-91a9-79ebb967fb4c", connectors.items[0].uuid.?); // beast_out never sends one, so it is dropped rather than pretended. try testing.expectEqual(net.Protocol.beast_cooked, connectors.items[1].protocol); try testing.expectEqual(@as(?[]const u8, null), connectors.items[1].uuid); } test "naming a file to read implies reading from a file" { var config: Config = .{}; var connectors: std.ArrayList(net.Connector) = .empty; defer connectors.deinit(testing.allocator); var arena: std.heap.ArenaAllocator = .init(testing.allocator); defer arena.deinit(); try applyTo(arena.allocator(), &config, ".{ .sdr = .{ .file = \"x.uc8\", .format = .sc16 } }", &connectors); try testing.expectEqual(sdr.Kind.ifile, config.source.kind); try testing.expectEqualStrings("x.uc8", config.source.path.?); try testing.expectEqual(convert.InputFormat.sc16, config.source.format); } test "every setting the program has can be set from a file" { // Three options structs walked field by field at compile time is more // comparisons than the default budget allows. @setEvalBranchQuota(10_000); // A regression guard rather than a behaviour test. Trimming the command // line moved the settings here, and it is easy to add one to a module // and never expose it -- which does not fail to build, it just quietly // cannot be configured. Each entry below names a field of a runtime // options struct and the file field that reaches it; adding one to a // module without adding it here is a failing test. const covered = .{ .adaptive = .{ "burst_control", "range_control", "min_gain_db", "max_gain_db", "duty_cycle", "burst_alpha", "burst_change_delay", "burst_loud_rate", "burst_loud_runlength", "burst_quiet_rate", "burst_quiet_runlength", "range_alpha", "range_percentile", "range_target", "range_change_delay", "range_scan_delay", "range_rescan_delay", "sample_rate", }, .fatsv = .{ "rate_multiplier", "upload_unknown_commb" }, .adept = .{ "host", "port", "mac", "feeder_id", "feeder_id_source", "position", "program", "version", "allow_auto_update", "allow_manual_update", "compression_version", }, }; inline for (@typeInfo(adaptive.Options).@"struct".fields) |field| { comptime var found = false; inline for (covered.adaptive) |name| { if (comptime std.mem.eql(u8, name, field.name)) found = true; } if (!found) { std.debug.print("adaptive.Options.{s} cannot be set from a file\n", .{field.name}); return error.SettingNotConfigurable; } } inline for (@typeInfo(adept.Options).@"struct".fields) |field| { comptime var found = false; inline for (covered.adept) |name| { if (comptime std.mem.eql(u8, name, field.name)) found = true; } if (!found) { std.debug.print("adept.Options.{s} cannot be set from a file\n", .{field.name}); return error.SettingNotConfigurable; } } inline for (@typeInfo(@import("fatsv.zig").Options).@"struct".fields) |field| { comptime var found = false; inline for (covered.fatsv) |name| { if (comptime std.mem.eql(u8, name, field.name)) found = true; } if (!found) { std.debug.print("fatsv.Options.{s} cannot be set from a file\n", .{field.name}); return error.SettingNotConfigurable; } } } test "the adaptive controller's tuning is reachable" { var config: Config = .{}; var connectors: std.ArrayList(net.Connector) = .empty; defer connectors.deinit(testing.allocator); var arena: std.heap.ArenaAllocator = .init(testing.allocator); defer arena.deinit(); try applyTo(arena.allocator(), &config, \\.{ .sdr = .{ .direct_sampling = .q_branch, .adaptive = .{ \\ .burst_alpha = 0.5, .burst_change_delay_s = 9, .range_percentile = 60, \\ .range_rescan_delay_s = 1800, \\} } } , &connectors); try testing.expectEqual(@as(u2, 2), config.source.direct_sampling); try testing.expectApproxEqAbs(@as(f32, 0.5), config.adaptive.burst_alpha, 1e-9); try testing.expectEqual(@as(u32, 9), config.adaptive.burst_change_delay); try testing.expectEqual(@as(u32, 60), config.adaptive.range_percentile); try testing.expectEqual(@as(u32, 1800), config.adaptive.range_rescan_delay); } test "a misspelled field is refused rather than ignored" { var config: Config = .{}; var connectors: std.ArrayList(net.Connector) = .empty; defer connectors.deinit(testing.allocator); var arena: std.heap.ArenaAllocator = .init(testing.allocator); defer arena.deinit(); var diagnostics: std.zon.parse.Diagnostics = .{}; defer diagnostics.deinit(arena.allocator()); try testing.expectError(error.Invalid, apply( arena.allocator(), testing.allocator, &config, ".{ .receiver = .{ .latitude = 41.5 } }", &connectors, &diagnostics, )); }