Skip to main content

nros_platform/board/
config.rs

1//! Cross-board configuration trait.
2//!
3//! Every board crate (`nros-board-mps2-an385`, `nros-board-stm32f4`,
4//! `nros-board-esp32-qemu`, …) declares its own `Config` / `NodeConfig`
5//! struct with board-specific fields (MAC, IP, gateway, UART base,
6//! WiFi SSID, etc.). The structs share a few universal fields —
7//! Zenoh locator, ROS 2 domain ID — but cross-board generic code
8//! (a benchmark harness, a multi-target test driver) had no way to
9//! reach those without `cfg`-gating the type name.
10//!
11//! [`BoardConfig`] is the trait every board's config implements so
12//! generic code can read the universal fields uniformly:
13//!
14//! ```ignore
15//! fn print_config<C: nros_platform::BoardConfig>(c: &C) {
16//!     println!("locator: {}", c.zenoh_locator());
17//!     println!("domain:  {}", c.domain_id());
18//! }
19//! ```
20//!
21//! The trait stays minimal on purpose: each board's transport-specific
22//! knobs (MAC address, WiFi credentials, UART base) remain on the
23//! concrete `Config` struct as ordinary fields. Adding
24//! transport-specific extension traits (e.g. `EthernetConfig`,
25//! `WifiConfig`, `SerialConfig`) is a follow-up that can land
26//! without changing this trait.
27
28/// Universal board configuration accessors.
29///
30/// Implemented by every board crate's top-level config struct
31/// (`Config`, `NodeConfig`, etc.). Generic code that needs to read the
32/// Zenoh locator or the ROS 2 domain ID can take `&impl BoardConfig`
33/// instead of `cfg`-gating on each board type.
34pub trait BoardConfig {
35    /// Zenoh router/peer locator string (e.g. `"tcp/192.168.1.50:7447"`).
36    fn zenoh_locator(&self) -> &str;
37
38    /// ROS 2 domain ID (default `0`).
39    fn domain_id(&self) -> u32;
40}
41
42/// Phase 173.5 — mutable transport knobs the orchestration generator
43/// writes into a board `Config` from `nros.toml` `[[transport]]` (the
44/// `NanoRosOwned` net-stack path: the board owns smoltcp/lwIP/NetX, so
45/// the IP / baud value lands in the board `Config` rather than an RTOS
46/// config fragment).
47///
48/// Every method has a no-op default so a board only overrides the knobs
49/// it actually has (a serial-only board ignores `set_ipv4`; an
50/// ethernet-only board ignores `set_baudrate`). Boards whose net stack
51/// is owned by the RTOS (`RtosOwned`: Zephyr / NuttX) do **not** impl
52/// this — their IP lands in the emitted config fragment instead.
53pub trait BoardTransportConfig {
54    /// Static IPv4 address + prefix length for the board's ethernet
55    /// stack. Boards without a `prefix` field ignore that argument.
56    fn set_ipv4(&mut self, _addr: [u8; 4], _prefix: u8) {}
57
58    /// Ethernet MAC address. Boards with a fixed/fused MAC ignore this.
59    /// (Phase 172.J — the orchestration generator writes it from
60    /// `nros.toml` `[[transport]]` `mac`, replacing `config.toml`'s
61    /// `[network].mac`.)
62    fn set_mac(&mut self, _mac: [u8; 6]) {}
63
64    /// Default IPv4 gateway for the board's ethernet stack. Boards on a
65    /// flat link (no gateway) ignore this. (Phase 172.J — from
66    /// `nros.toml` `[[transport]]` `gateway`, replacing `config.toml`'s
67    /// `[network].gateway`.)
68    fn set_gateway(&mut self, _addr: [u8; 4]) {}
69
70    /// Serial line rate for the board's UART transport.
71    fn set_baudrate(&mut self, _baud: u32) {}
72
73    /// WiFi SSID for boards with a WiFi transport (ESP32). Wired boards
74    /// ignore it. (Phase 172.K.4 — from `nros.toml` `[[transport]]` `ssid`,
75    /// replacing `config.toml`'s `[wifi].ssid`.)
76    fn set_ssid(&mut self, _ssid: &str) {}
77
78    /// WiFi password (paired with [`set_ssid`]). (Phase 172.K.4 —
79    /// `[[transport]]` `password`, replacing `config.toml`'s `[wifi].password`.)
80    fn set_password(&mut self, _password: &str) {}
81
82    /// NIC name(s) this transport multi-homes over (`["eth0", "eth1"]`).
83    /// Boards with a single fixed NIC (every embedded target today) ignore it;
84    /// the seam exists for a multi-homed hosted board to fold several
85    /// interfaces into one session. (Phase 172.K.7 — from `nros.toml`
86    /// `[[transport]]` `interfaces`.)
87    fn set_interfaces(&mut self, _interfaces: &[&str]) {}
88}