nros_platform/resolve.rs
1//! Concrete platform type alias resolved at compile time.
2//!
3//! When a platform feature is enabled, [`ConcretePlatform`] resolves to
4//! the active backend. When no platform is selected (e.g., default workspace
5//! build), the type is not defined — downstream crates that need it must
6//! enable a platform feature.
7
8// Phase 121.7.b / 121.8.e — uniform CffiPlatform routing for every
9// platform that impls the full canonical surface (clock + alloc +
10// sleep + yield + random + time + threading + net). POSIX, the four
11// RTOS kernels, the four bare-metal embedded crates, and platform-cffi
12// itself all route through CffiPlatform. Bare-metal net surface is
13// backed by `nros_smoltcp::define_smoltcp_platform!` (PlatformTcp /
14// Udp / SocketHelpers / UdpMulticast) emitted by each platform crate.
15// issue 1315 / phase-451 W4 — ONE arm, because all nine define the SAME type.
16//
17// These were nine separate `#[cfg(feature = "platform-<x>")]` blocks, each
18// `pub type ConcretePlatform = nros_platform_cffi::CffiPlatform;` — identical
19// right-hand sides, mutually exclusive only by CONVENTION. Turning on two
20// platform features at once is then E0428, "defined multiple times", and
21// nothing in the tree did that until three crates became workspace members:
22// `cargo check --workspace` UNIFIES features across every member, so one
23// member wanting `platform-freertos` beside another wanting `platform-posix`
24// broke a crate neither of them names.
25//
26// `any(...)` is not a widening: nine arms that produce one type ARE one arm,
27// and writing them separately only bought a collision. A future platform whose
28// ConcretePlatform is NOT `CffiPlatform` gets its own `cfg` with a
29// `not(...)` on this one — which is the point at which the exclusivity becomes
30// real and should be stated, rather than assumed nine times.
31#[cfg(any(
32 feature = "platform-posix",
33 feature = "platform-cffi",
34 feature = "platform-mps2-an385",
35 feature = "platform-stm32f4",
36 feature = "platform-nuttx",
37 feature = "platform-freertos",
38 feature = "platform-threadx",
39 feature = "platform-zephyr",
40))]
41pub type ConcretePlatform = nros_platform_cffi::CffiPlatform;
42
43// ============================================================================
44// Phase 71.22 — opaque-buffer sizes for `_z_sys_net_socket_t` /
45// `_z_sys_net_endpoint_t`, resolved per platform.
46//
47// Each `nros-platform-*` crate computes these from `core::mem::size_of`
48// over its private `Socket` / `Endpoint` struct (which mirrors zenoh-pico's
49// platform header). Re-exporting them here lets callers like
50// RMW transport adapters size their opaque buffers exactly via
51// `nros_platform::NET_SOCKET_SIZE`, instead of paying for a `[u8; 64]`
52// worst-case.
53//
54// Bare-metal platforms (`platform-mps2-an385`, `platform-stm32f4`,
55// `platform-esp32`, `platform-esp32-qemu`, `platform-cffi`) don't yet
56// have a typed socket struct exposed; callers there get a 64-byte
57// fallback. Once the smoltcp platform crates publish their own
58// `Socket` / `Endpoint` (Phase 71.26), they can plug in alongside the
59// RTOS variants below.
60
61// POSIX still publishes typed socket sizes (the only host-runnable
62// platform crate left + the only one whose Socket / Endpoint layout
63// varies meaningfully). Every other platform uses the 64-byte fallback
64// — bare-metal smoltcp is 2 / 6 bytes; the RTOS C ports own the layout
65// behind their `_z_sys_net_*` typedefs and can publish a tighter size
66// later if the headroom matters.
67// Phase 104.A.3 — POSIX net-size constants formerly re-exported from
68// `nros_platform_posix::net::*`. Inlined here so `nros-platform`
69// stops Rust-importing the concrete POSIX platform crate. The values
70// mirror `nros-platform-posix/src/net.rs`:
71// * `Socket` = `{ int fd }` → `size_of::<c_int>() == 4` on every
72// POSIX ABI we target.
73// * `Endpoint` = `{ struct addrinfo* iptcp }` → native pointer
74// size (8 on 64-bit, 4 on 32-bit).
75//
76// Both expressible via `core::ffi` without pulling libc / the
77// concrete platform crate. Phase 123's `nros-platform-posix`
78// Rust-crate deletion adopts the same inline shape on the
79// release-prep branch.
80// Phase 129.C.3.b — exported unconditionally. Previously gated
81// on a specific `platform-<rtos>` feature, which forced every
82// RMW crate that imported them to
83// forward a `nros-platform/platform-*` feature so the constants
84// would resolve. Worst-case 64-byte / 8-aligned storage covers
85// every supported platform — POSIX's `{ int fd }` socket and
86// pointer endpoint, bare-metal smoltcp / lwIP / NetX handles
87// alike. Consumers that want a tighter packing can opt into a
88// per-platform `nros_platform_*` storage type at the link
89// layer once that ABI lands.
90pub const NET_SOCKET_SIZE: usize = 64;
91pub const NET_SOCKET_ALIGN: usize = 8;
92pub const NET_ENDPOINT_SIZE: usize = 64;
93pub const NET_ENDPOINT_ALIGN: usize = 8;