From 771806be790a2758f1314d6460defe7c2f0d3e99 Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Tue, 27 Dec 2022 01:07:58 +0100 Subject: [PATCH] net/chan: split state runner. --- embassy-net-driver-channel/src/lib.rs | 26 +++++++++++++------- embassy-usb/src/class/cdc_ncm/embassy_net.rs | 6 ++--- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/embassy-net-driver-channel/src/lib.rs b/embassy-net-driver-channel/src/lib.rs index 073edb41a..0c8dcc22b 100644 --- a/embassy-net-driver-channel/src/lib.rs +++ b/embassy-net-driver-channel/src/lib.rs @@ -50,9 +50,13 @@ pub struct Runner<'d, const MTU: usize> { shared: &'d Mutex>, } +#[derive(Clone, Copy)] +pub struct StateRunner<'d> { + shared: &'d Mutex>, +} + pub struct RxRunner<'d, const MTU: usize> { rx_chan: zerocopy_channel::Sender<'d, NoopRawMutex, PacketBuf>, - shared: &'d Mutex>, } pub struct TxRunner<'d, const MTU: usize> { @@ -60,16 +64,18 @@ pub struct TxRunner<'d, const MTU: usize> { } impl<'d, const MTU: usize> Runner<'d, MTU> { - pub fn split(self) -> (RxRunner<'d, MTU>, TxRunner<'d, MTU>) { + pub fn split(self) -> (StateRunner<'d>, RxRunner<'d, MTU>, TxRunner<'d, MTU>) { ( - RxRunner { - shared: self.shared, - rx_chan: self.rx_chan, - }, + StateRunner { shared: self.shared }, + RxRunner { rx_chan: self.rx_chan }, TxRunner { tx_chan: self.tx_chan }, ) } + pub fn state_runner(&self) -> StateRunner<'d> { + StateRunner { shared: self.shared } + } + pub fn set_link_state(&mut self, state: LinkState) { self.shared.lock(|s| { let s = &mut *s.borrow_mut(); @@ -131,8 +137,8 @@ impl<'d, const MTU: usize> Runner<'d, MTU> { } } -impl<'d, const MTU: usize> RxRunner<'d, MTU> { - pub fn set_link_state(&mut self, state: LinkState) { +impl<'d> StateRunner<'d> { + pub fn set_link_state(&self, state: LinkState) { self.shared.lock(|s| { let s = &mut *s.borrow_mut(); s.link_state = state; @@ -140,14 +146,16 @@ impl<'d, const MTU: usize> RxRunner<'d, MTU> { }); } - pub fn set_ethernet_address(&mut self, address: [u8; 6]) { + pub fn set_ethernet_address(&self, address: [u8; 6]) { self.shared.lock(|s| { let s = &mut *s.borrow_mut(); s.ethernet_address = address; s.waker.wake(); }); } +} +impl<'d, const MTU: usize> RxRunner<'d, MTU> { pub async fn rx_buf(&mut self) -> &mut [u8] { let p = self.rx_chan.send().await; &mut p.buf diff --git a/embassy-usb/src/class/cdc_ncm/embassy_net.rs b/embassy-usb/src/class/cdc_ncm/embassy_net.rs index 7ecf693d2..501df2d8c 100644 --- a/embassy-usb/src/class/cdc_ncm/embassy_net.rs +++ b/embassy-usb/src/class/cdc_ncm/embassy_net.rs @@ -25,16 +25,16 @@ pub struct Runner<'d, D: Driver<'d>, const MTU: usize> { impl<'d, D: Driver<'d>, const MTU: usize> Runner<'d, D, MTU> { pub async fn run(mut self) -> ! { - let (mut rx_chan, mut tx_chan) = self.ch.split(); + let (state_chan, mut rx_chan, mut tx_chan) = self.ch.split(); let rx_fut = async move { loop { trace!("WAITING for connection"); - rx_chan.set_link_state(LinkState::Down); + state_chan.set_link_state(LinkState::Down); self.rx_usb.wait_connection().await.unwrap(); trace!("Connected"); - rx_chan.set_link_state(LinkState::Up); + state_chan.set_link_state(LinkState::Up); loop { let p = rx_chan.rx_buf().await;