2023-07-16 02:15:01 +02:00
|
|
|
use embassy_futures::select::{select3, Either3};
|
2023-07-19 01:28:12 +02:00
|
|
|
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
|
|
|
|
use embassy_sync::channel::Channel;
|
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-19 01:28:12 +02:00
|
|
|
pub struct Runner<'a> {
|
2023-07-18 03:14:06 +02:00
|
|
|
mac_subsystem: Mac,
|
2023-07-19 01:28:12 +02:00
|
|
|
pub(crate) rx_channel: Channel<CriticalSectionRawMutex, Event, 1>,
|
|
|
|
pub(crate) tx_channel: Channel<CriticalSectionRawMutex, &'a [u8], 1>,
|
2023-07-16 02:15:01 +02:00
|
|
|
}
|
|
|
|
|
2023-07-19 01:28:12 +02:00
|
|
|
impl<'a> Runner<'a> {
|
2023-07-18 03:14:06 +02:00
|
|
|
pub fn new(mac: Mac) -> Self {
|
|
|
|
Self {
|
|
|
|
mac_subsystem: mac,
|
2023-07-19 01:28:12 +02:00
|
|
|
rx_channel: Channel::new(),
|
|
|
|
tx_channel: Channel::new(),
|
2023-07-18 03:14:06 +02:00
|
|
|
}
|
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) => {
|
2023-07-19 01:28:12 +02:00
|
|
|
self.rx_channel.try_send(event);
|
2023-07-18 03:14:06 +02:00
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: select tx event
|
2023-07-16 02:15:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|