embassy/embassy-stm32-wpan/src/mac/runner.rs

81 lines
2.7 KiB
Rust
Raw Normal View History

2023-07-19 03:52:03 +02:00
use embassy_futures::join;
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
use embassy_sync::channel::Channel;
2023-07-16 02:15:01 +02:00
2023-07-19 03:52:03 +02:00
use crate::mac::commands::DataRequest;
use crate::mac::event::{Event, MacEvent};
2023-07-19 03:52:03 +02:00
use crate::mac::typedefs::{AddressMode, MacAddress, PanId, SecurityLevel};
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;
pub struct Runner<'a> {
mac_subsystem: Mac,
pub(crate) rx_channel: Channel<CriticalSectionRawMutex, Event, 1>,
2023-07-19 03:52:03 +02:00
pub(crate) tx_channel: Channel<CriticalSectionRawMutex, &'a mut [u8], 5>,
pub(crate) tx_buf_channel: Channel<CriticalSectionRawMutex, &'a mut [u8], 5>,
2023-07-16 02:15:01 +02:00
}
impl<'a> Runner<'a> {
2023-07-19 03:52:03 +02:00
pub fn new(mac: Mac, tx_buf_queue: [&'a mut [u8; MTU]; 5]) -> Self {
let this = Self {
mac_subsystem: mac,
rx_channel: Channel::new(),
tx_channel: Channel::new(),
2023-07-19 03:52:03 +02:00
tx_buf_channel: Channel::new(),
};
for buf in tx_buf_queue {
this.tx_buf_channel.try_send(buf).unwrap();
}
2023-07-16 02:15:01 +02:00
2023-07-19 03:52:03 +02:00
this
2023-07-16 02:15:01 +02:00
}
pub async fn run(&self) -> ! {
2023-07-19 03:52:03 +02:00
join::join(
async {
loop {
let event = self.mac_subsystem.read().await;
if let Ok(evt) = event.mac_event() {
match evt {
MacEvent::McpsDataInd(_) => {
self.rx_channel.send(event).await;
}
_ => {}
}
}
}
2023-07-19 03:52:03 +02:00
},
async {
loop {
let buf = self.tx_channel.recv().await;
2023-07-19 03:52:03 +02:00
self.mac_subsystem
.send_command(
DataRequest {
src_addr_mode: AddressMode::Short,
dst_addr_mode: AddressMode::Short,
dst_pan_id: PanId([0x1A, 0xAA]),
dst_address: MacAddress::BROADCAST,
msdu_handle: 0x02,
ack_tx: 0x00,
gts_tx: false,
security_level: SecurityLevel::Unsecure,
..Default::default()
}
.set_buffer(&buf),
)
.await
.unwrap();
// The tx channel should always be of equal capacity to the tx_buf channel
self.tx_buf_channel.try_send(buf).unwrap();
}
},
)
.await;
loop {}
2023-07-16 02:15:01 +02:00
}
}