2023-07-16 02:15:01 +02:00
|
|
|
use embassy_futures::select::{select3, Either3};
|
2023-07-18 03:14:06 +02:00
|
|
|
use embassy_sync::waitqueue::AtomicWaker;
|
2023-07-16 02:15:01 +02:00
|
|
|
|
2023-07-18 03:14:06 +02:00
|
|
|
use crate::mac::event::{Event, MacEvent};
|
2023-07-16 16:32:54 +02:00
|
|
|
use crate::mac::MTU;
|
2023-07-16 02:15:01 +02:00
|
|
|
use crate::sub::mac::Mac;
|
|
|
|
|
2023-07-18 03:14:06 +02:00
|
|
|
pub(crate) struct TxRing {
|
|
|
|
// stores n packets of up to mtu size
|
|
|
|
ring: [[u8; MTU]; 5],
|
|
|
|
pending: bool,
|
|
|
|
// start: u8,
|
|
|
|
// end: u8,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TxRing {
|
|
|
|
pub(crate) fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
ring: [[0; MTU]; 5],
|
|
|
|
pending: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait for a free packet to become available
|
|
|
|
pub fn is_packet_free(&self) -> bool {
|
|
|
|
!self.pending
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the next available free packet
|
|
|
|
pub fn get_free_packet<'a>(&'a mut self) -> &'a mut [u8] {
|
|
|
|
self.pending = true;
|
|
|
|
|
|
|
|
&mut self.ring[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_packet_to_transmit<'a>(&'a mut self) -> Option<&'a [u8]> {
|
|
|
|
if self.pending {
|
|
|
|
self.pending = false;
|
|
|
|
|
|
|
|
Some(&self.ring[0])
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-18 02:26:58 +02:00
|
|
|
pub struct Runner {
|
2023-07-18 03:14:06 +02:00
|
|
|
mac_subsystem: Mac,
|
|
|
|
pub(crate) rx_ring: Option<Event>,
|
|
|
|
pub(crate) tx_ring: TxRing,
|
|
|
|
pub(crate) rx_waker: AtomicWaker,
|
|
|
|
pub(crate) tx_waker: AtomicWaker,
|
2023-07-16 02:15:01 +02:00
|
|
|
}
|
|
|
|
|
2023-07-18 02:26:58 +02:00
|
|
|
impl Runner {
|
2023-07-18 03:14:06 +02:00
|
|
|
pub fn new(mac: Mac) -> Self {
|
|
|
|
Self {
|
|
|
|
mac_subsystem: mac,
|
|
|
|
rx_ring: None,
|
|
|
|
tx_ring: TxRing::new(),
|
|
|
|
rx_waker: AtomicWaker::new(),
|
|
|
|
tx_waker: AtomicWaker::new(),
|
|
|
|
}
|
2023-07-16 02:15:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) async fn init(&mut self, firmware: &[u8]) {
|
|
|
|
debug!("wifi init done");
|
|
|
|
}
|
|
|
|
|
2023-07-18 03:14:06 +02:00
|
|
|
pub async fn run(&self) -> ! {
|
2023-07-16 02:15:01 +02:00
|
|
|
loop {
|
2023-07-18 03:14:06 +02:00
|
|
|
let event = self.mac_subsystem.read().await;
|
|
|
|
if let Ok(evt) = event.mac_event() {
|
|
|
|
match evt {
|
|
|
|
MacEvent::McpsDataInd(data_ind) => {
|
|
|
|
// TODO: store mac_event in rx_ring
|
|
|
|
self.rx_waker.wake();
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: select tx event
|
2023-07-16 02:15:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|