net/chan: split state runner.

This commit is contained in:
Dario Nieuwenhuis 2022-12-27 01:07:58 +01:00
parent 4a4b593694
commit 771806be79
2 changed files with 20 additions and 12 deletions

View File

@ -50,9 +50,13 @@ pub struct Runner<'d, const MTU: usize> {
shared: &'d Mutex<NoopRawMutex, RefCell<Shared>>, shared: &'d Mutex<NoopRawMutex, RefCell<Shared>>,
} }
#[derive(Clone, Copy)]
pub struct StateRunner<'d> {
shared: &'d Mutex<NoopRawMutex, RefCell<Shared>>,
}
pub struct RxRunner<'d, const MTU: usize> { pub struct RxRunner<'d, const MTU: usize> {
rx_chan: zerocopy_channel::Sender<'d, NoopRawMutex, PacketBuf<MTU>>, rx_chan: zerocopy_channel::Sender<'d, NoopRawMutex, PacketBuf<MTU>>,
shared: &'d Mutex<NoopRawMutex, RefCell<Shared>>,
} }
pub struct TxRunner<'d, const MTU: usize> { 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> { 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 { StateRunner { shared: self.shared },
shared: self.shared, RxRunner { rx_chan: self.rx_chan },
rx_chan: self.rx_chan,
},
TxRunner { tx_chan: self.tx_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) { pub fn set_link_state(&mut self, state: LinkState) {
self.shared.lock(|s| { self.shared.lock(|s| {
let s = &mut *s.borrow_mut(); 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> { impl<'d> StateRunner<'d> {
pub fn set_link_state(&mut self, state: LinkState) { pub fn set_link_state(&self, state: LinkState) {
self.shared.lock(|s| { self.shared.lock(|s| {
let s = &mut *s.borrow_mut(); let s = &mut *s.borrow_mut();
s.link_state = state; 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| { self.shared.lock(|s| {
let s = &mut *s.borrow_mut(); let s = &mut *s.borrow_mut();
s.ethernet_address = address; s.ethernet_address = address;
s.waker.wake(); s.waker.wake();
}); });
} }
}
impl<'d, const MTU: usize> RxRunner<'d, MTU> {
pub async fn rx_buf(&mut self) -> &mut [u8] { pub async fn rx_buf(&mut self) -> &mut [u8] {
let p = self.rx_chan.send().await; let p = self.rx_chan.send().await;
&mut p.buf &mut p.buf

View File

@ -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> { impl<'d, D: Driver<'d>, const MTU: usize> Runner<'d, D, MTU> {
pub async fn run(mut self) -> ! { 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 { let rx_fut = async move {
loop { loop {
trace!("WAITING for connection"); 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(); self.rx_usb.wait_connection().await.unwrap();
trace!("Connected"); trace!("Connected");
rx_chan.set_link_state(LinkState::Up); state_chan.set_link_state(LinkState::Up);
loop { loop {
let p = rx_chan.rx_buf().await; let p = rx_chan.rx_buf().await;