Skip to content

Commit

Permalink
refactor(embassy): Allow building net without a backend for docs and …
Browse files Browse the repository at this point in the history
…linting
  • Loading branch information
chrysn committed Oct 29, 2024
1 parent 4d487d2 commit 46dfded
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 5 deletions.
23 changes: 18 additions & 5 deletions src/riot-rs-embassy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
use network::DummyDriver as NetworkDevice;
}
}

#[cfg(feature = "net")]
pub use network::NetworkStack;
Expand Down Expand Up @@ -276,6 +283,12 @@ async fn init_task(mut peripherals: arch::OptionalPeripherals) {
"maximum number of concurrent sockets allowed by the network stack"
);

#[cfg(not(any(feature = "usb-ethernet", feature = "wifi-cyw43", feature = "wifi-esp")))]
// The creation of `device` is not organized in such a way that they could be put in a
// cfg-if without larger refactoring; relying on unused variable lints to keep the
// condition list up to date.
let device: NetworkDevice = network::new_dummy();

let config = network::config();

// Generate random seed
Expand Down
73 changes: 73 additions & 0 deletions src/riot-rs-embassy/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub(crate) async fn net_task(mut runner: Runner<'static, NetworkDevice>) -> ! {
runner.run().await
}

#[allow(dead_code, reason = "false positive during builds outside of laze")]
pub(crate) fn config() -> embassy_net::Config {
#[cfg(not(feature = "override-network-config"))]
{
Expand All @@ -34,3 +35,75 @@ pub(crate) fn config() -> embassy_net::Config {
unsafe { riot_rs_network_config() }
}
}

/// Constructor for [`DummyDriver`]
///
/// This is a standalone function instead of an associated method to ease moving [`DummyDriver`]
/// into [`embassy_net`].
#[allow(
dead_code,
reason = "constructor is only used in linter / documentation situations"
)]
pub(crate) fn new_dummy() -> DummyDriver {
panic!(
"DummyDriver must only ever be constructed for documentation and linting, not for running"
)
}

/// Stand-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) struct DummyDriver(core::convert::Infallible);

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.0 {}
}

fn transmit(&mut self, _cx: &mut core::task::Context) -> Option<Self::TxToken<'_>> {
match self.0 {}
}

fn link_state(&mut self, _cx: &mut core::task::Context) -> embassy_net::driver::LinkState {
match self.0 {}
}

fn capabilities(&self) -> embassy_net::driver::Capabilities {
match self.0 {}
}

fn hardware_address(&self) -> embassy_net::driver::HardwareAddress {
match self.0 {}
}
}

impl embassy_net::driver::TxToken for DummyDriver {
fn consume<R, F>(self, _len: usize, _f: F) -> R
where
F: FnOnce(&mut [u8]) -> R,
{
match self.0 {}
}
}

impl embassy_net::driver::RxToken for DummyDriver {
fn consume<R, F>(self, _f: F) -> R
where
F: FnOnce(&mut [u8]) -> R,
{
match self.0 {}
}
}

0 comments on commit 46dfded

Please sign in to comment.