From a8488ce1c8c271fe22ac74ff3c7a10fa774cdb4a Mon Sep 17 00:00:00 2001 From: chrysn Date: Tue, 29 Oct 2024 14:18:47 +0100 Subject: [PATCH] refactor(embassy): Allow building net without a backend for docs and linting --- src/riot-rs-embassy/src/lib.rs | 20 ++++++++--- src/riot-rs-embassy/src/network.rs | 58 ++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 5 deletions(-) diff --git a/src/riot-rs-embassy/src/lib.rs b/src/riot-rs-embassy/src/lib.rs index 5b55a7eaa..73566a6f4 100644 --- a/src/riot-rs-embassy/src/lib.rs +++ b/src/riot-rs-embassy/src/lib.rs @@ -68,11 +68,18 @@ pub mod reexports { pub use embassy_executor::Spawner; -#[cfg(feature = "usb-ethernet")] -use usb::ethernet::NetworkDevice; - -#[cfg(feature = "wifi")] -use wifi::NetworkDevice; +#[cfg(feature = "net")] +cfg_if::cfg_if! { + if #[cfg(feature = "usb-ethernet")] { + use usb::ethernet::NetworkDevice; + } else if #[cfg(feature = "wifi")] { + use wifi::NetworkDevice; + } else if #[cfg(context = "riot-rs")] { + compile_error!("no backend for net is active"); + } else { + type NetworkDevice = network::DummyDriver; + } +} #[cfg(feature = "net")] pub use network::NetworkStack; @@ -264,6 +271,9 @@ async fn init_task(mut peripherals: arch::OptionalPeripherals) { #[cfg(feature = "wifi-esp")] let device = arch::wifi::esp_wifi::init(&mut peripherals, spawner); + #[cfg(not(any(feature = "wifi-esp", feature = "wifi-cyw43", feature = "usb-ethernet")))] + let device = panic!(); + #[cfg(feature = "net")] { use embassy_net::StackResources; diff --git a/src/riot-rs-embassy/src/network.rs b/src/riot-rs-embassy/src/network.rs index 8eeef0b0f..932a6e125 100644 --- a/src/riot-rs-embassy/src/network.rs +++ b/src/riot-rs-embassy/src/network.rs @@ -34,3 +34,61 @@ pub(crate) fn config() -> embassy_net::Config { unsafe { riot_rs_network_config() } } } + +/// An uninhabited type that stands in for a network driver in documentation and linting. +/// +/// It also doubles as the infallible type for its own associated types. +// FIXME: This should be core::convert::Infallible as soon as embassy-net implements the traits on +// that. +pub(crate) enum DummyDriver {} + +impl embassy_net::driver::Driver for DummyDriver { + type RxToken<'a> = Self + where + Self: 'a; + + type TxToken<'a> = Self + where + Self: 'a; + + fn receive( + &mut self, + _cx: &mut core::task::Context, + ) -> Option<(Self::RxToken<'_>, Self::TxToken<'_>)> { + match *self {} + } + + fn transmit(&mut self, _cx: &mut core::task::Context) -> Option> { + match *self {} + } + + fn link_state(&mut self, _cx: &mut core::task::Context) -> embassy_net::driver::LinkState { + match *self {} + } + + fn capabilities(&self) -> embassy_net::driver::Capabilities { + match *self {} + } + + fn hardware_address(&self) -> embassy_net::driver::HardwareAddress { + match *self {} + } +} + +impl embassy_net::driver::TxToken for DummyDriver { + fn consume(self, _len: usize, _f: F) -> R + where + F: FnOnce(&mut [u8]) -> R, + { + match self {} + } +} + +impl embassy_net::driver::RxToken for DummyDriver { + fn consume(self, _f: F) -> R + where + F: FnOnce(&mut [u8]) -> R, + { + match self {} + } +}