From 46dfdedea8fedcf9b312ff3b215567528259c745 Mon Sep 17 00:00:00 2001 From: chrysn Date: Tue, 29 Oct 2024 14:18:47 +0100 Subject: [PATCH 1/3] refactor(embassy): Allow building net without a backend for docs and linting --- src/riot-rs-embassy/src/lib.rs | 23 ++++++++-- src/riot-rs-embassy/src/network.rs | 73 ++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 5 deletions(-) diff --git a/src/riot-rs-embassy/src/lib.rs b/src/riot-rs-embassy/src/lib.rs index 5b55a7eaa..77a3a9a50 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 { + use network::DummyDriver as NetworkDevice; + } +} #[cfg(feature = "net")] pub use network::NetworkStack; @@ -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 diff --git a/src/riot-rs-embassy/src/network.rs b/src/riot-rs-embassy/src/network.rs index 8eeef0b0f..1699d6554 100644 --- a/src/riot-rs-embassy/src/network.rs +++ b/src/riot-rs-embassy/src/network.rs @@ -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"))] { @@ -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> { + 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(self, _len: usize, _f: F) -> R + where + F: FnOnce(&mut [u8]) -> R, + { + match self.0 {} + } +} + +impl embassy_net::driver::RxToken for DummyDriver { + fn consume(self, _f: F) -> R + where + F: FnOnce(&mut [u8]) -> R, + { + match self.0 {} + } +} From faf2af7772e46c288cbb2dcccb8a7129046c54a2 Mon Sep 17 00:00:00 2001 From: chrysn Date: Tue, 29 Oct 2024 14:41:59 +0100 Subject: [PATCH 2/3] chore(CI): Enable docs and checks on riot-rs/net --- .github/workflows/build-deploy-docs.yml | 2 +- .github/workflows/main.yml | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/build-deploy-docs.yml b/.github/workflows/build-deploy-docs.yml index 7da684463..d977313b4 100644 --- a/.github/workflows/build-deploy-docs.yml +++ b/.github/workflows/build-deploy-docs.yml @@ -48,7 +48,7 @@ jobs: - name: Build rustdoc docs run: | - cargo doc -p riot-rs --features bench,csprng,executor-thread,external-interrupts,hwrng,i2c,no-boards,random,spi,threading,usb + cargo doc -p riot-rs --features bench,csprng,executor-thread,external-interrupts,hwrng,i2c,net,no-boards,random,spi,threading,usb echo "" > target/doc/index.html mkdir -p ./_site/dev/docs/api && mv target/doc/* ./_site/dev/docs/api diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 94e6d4418..3630bd8c0 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -216,7 +216,7 @@ jobs: - name: clippy uses: clechasseur/rs-clippy-check@v3 with: - args: --verbose --locked --features no-boards,external-interrupts,spi,storage -p riot-rs -p riot-rs-arch -p riot-rs-boards -p riot-rs-chips -p riot-rs-debug -p riot-rs-embassy -p riot-rs-embassy-common -p riot-rs-identity -p riot-rs-macros -p riot-rs-random -p riot-rs-rt -p riot-rs-storage -p riot-rs-threads -p riot-rs-utils -- --deny warnings + args: --verbose --locked --features net,no-boards,external-interrupts,spi,storage -p riot-rs -p riot-rs-arch -p riot-rs-boards -p riot-rs-chips -p riot-rs-debug -p riot-rs-embassy -p riot-rs-embassy-common -p riot-rs-identity -p riot-rs-macros -p riot-rs-random -p riot-rs-rt -p riot-rs-storage -p riot-rs-threads -p riot-rs-utils -- --deny warnings - run: echo 'RUSTFLAGS=--cfg context="esp32c6"' >> $GITHUB_ENV - name: clippy for ESP32 @@ -246,7 +246,7 @@ jobs: - run: echo 'RUSTFLAGS=' >> $GITHUB_ENV - name: rustdoc - run: RUSTDOCFLAGS='-D warnings' cargo doc -p riot-rs --features bench,csprng,executor-thread,external-interrupts,hwrng,i2c,no-boards,random,spi,storage,threading,usb + run: RUSTDOCFLAGS='-D warnings' cargo doc -p riot-rs --features bench,csprng,executor-thread,external-interrupts,hwrng,i2c,net,no-boards,random,spi,storage,threading,usb - name: rustdoc for ESP32 run: RUSTDOCFLAGS='-D warnings --cfg context="esp32c6"' cargo doc --target=riscv32imac-unknown-none-elf --features external-interrupts,i2c,spi,esp-hal/esp32c6,esp-hal-embassy/esp32c6 -p riot-rs-esp From 516a8468ceb5849826b07e37fe01ffd7841890ee Mon Sep 17 00:00:00 2001 From: chrysn Date: Tue, 29 Oct 2024 15:19:50 +0100 Subject: [PATCH 3/3] fix(embassy): Fix lints --- src/riot-rs-embassy/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/riot-rs-embassy/src/lib.rs b/src/riot-rs-embassy/src/lib.rs index 77a3a9a50..ae9a51fc0 100644 --- a/src/riot-rs-embassy/src/lib.rs +++ b/src/riot-rs-embassy/src/lib.rs @@ -303,7 +303,7 @@ async fn init_task(mut peripherals: arch::OptionalPeripherals) { let (stack, runner) = embassy_net::new( device, config, - RESOURCES.init_with(|| StackResources::new()), + RESOURCES.init_with(StackResources::new), seed, );