wpan: further optimize mac event

This commit is contained in:
xoviat
2023-07-20 16:45:04 -05:00
parent 02d57afd51
commit 809d3476aa
8 changed files with 86 additions and 140 deletions

View File

@ -7,7 +7,7 @@ use embassy_net_driver::{Capabilities, LinkState, Medium};
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
use embassy_sync::channel::Channel;
use crate::mac::event::{Event, MacEvent};
use crate::mac::event::MacEvent;
use crate::mac::runner::Runner;
use crate::mac::MTU;
@ -81,7 +81,7 @@ impl<'d> embassy_net_driver::Driver for Driver<'d> {
}
pub struct RxToken<'d> {
rx: &'d Channel<CriticalSectionRawMutex, Event<'d>, 1>,
rx: &'d Channel<CriticalSectionRawMutex, MacEvent<'d>, 1>,
}
impl<'d> embassy_net_driver::RxToken for RxToken<'d> {
@ -91,7 +91,7 @@ impl<'d> embassy_net_driver::RxToken for RxToken<'d> {
{
// Only valid data events should be put into the queue
let data_event = match *self.rx.try_recv().unwrap() {
let data_event = match self.rx.try_recv().unwrap() {
MacEvent::McpsDataInd(data_event) => data_event,
_ => unreachable!(),
};

View File

@ -1,4 +1,4 @@
use core::{mem, ops};
use core::{mem, ptr};
use super::indications::{
AssociateIndication, BeaconNotifyIndication, CommStatusIndication, DataIndication, DisassociateIndication,
@ -8,9 +8,9 @@ use super::responses::{
AssociateConfirm, CalibrateConfirm, DataConfirm, DisassociateConfirm, DpsConfirm, GetConfirm, GtsConfirm,
PollConfirm, PurgeConfirm, ResetConfirm, RxEnableConfirm, ScanConfirm, SetConfirm, SoundingConfirm, StartConfirm,
};
use crate::evt::EvtBox;
use crate::evt::{EvtBox, MemoryManager};
use crate::mac::opcodes::OpcodeM0ToM4;
use crate::sub::mac::Mac;
use crate::sub::mac::{self, Mac};
pub(crate) trait ParseableMacEvent: Sized {
fn from_buffer<'a>(buf: &'a [u8]) -> Result<&'a Self, ()> {
@ -22,13 +22,36 @@ pub(crate) trait ParseableMacEvent: Sized {
}
}
pub struct Event<'a> {
#[allow(dead_code)]
event_box: EvtBox<Mac>,
mac_event: MacEvent<'a>,
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum MacEvent<'a> {
MlmeAssociateCnf(&'a AssociateConfirm),
MlmeDisassociateCnf(&'a DisassociateConfirm),
MlmeGetCnf(&'a GetConfirm),
MlmeGtsCnf(&'a GtsConfirm),
MlmeResetCnf(&'a ResetConfirm),
MlmeRxEnableCnf(&'a RxEnableConfirm),
MlmeScanCnf(&'a ScanConfirm),
MlmeSetCnf(&'a SetConfirm),
MlmeStartCnf(&'a StartConfirm),
MlmePollCnf(&'a PollConfirm),
MlmeDpsCnf(&'a DpsConfirm),
MlmeSoundingCnf(&'a SoundingConfirm),
MlmeCalibrateCnf(&'a CalibrateConfirm),
McpsDataCnf(&'a DataConfirm),
McpsPurgeCnf(&'a PurgeConfirm),
MlmeAssociateInd(&'a AssociateIndication),
MlmeDisassociateInd(&'a DisassociateIndication),
MlmeBeaconNotifyInd(&'a BeaconNotifyIndication),
MlmeCommStatusInd(&'a CommStatusIndication),
MlmeGtsInd(&'a GtsIndication),
MlmeOrphanInd(&'a OrphanIndication),
MlmeSyncLossInd(&'a SyncLossIndication),
MlmeDpsInd(&'a DpsIndication),
McpsDataInd(&'a DataIndication),
MlmePollInd(&'a PollIndication),
}
impl<'a> Event<'a> {
impl<'a> MacEvent<'a> {
pub(crate) fn new(event_box: EvtBox<Mac>) -> Result<Self, ()> {
let payload = event_box.payload();
let opcode = u16::from_le_bytes(payload[0..2].try_into().unwrap());
@ -111,43 +134,17 @@ impl<'a> Event<'a> {
}
};
Ok(Self { event_box, mac_event })
// Forget the event box so that drop isn't called
// We want to handle the lifetime ourselves
mem::forget(event_box);
Ok(mac_event)
}
}
impl<'a> ops::Deref for Event<'a> {
type Target = MacEvent<'a>;
fn deref(&self) -> &Self::Target {
&self.mac_event
impl<'a> Drop for MacEvent<'a> {
fn drop(&mut self) {
unsafe { mac::Mac::drop_event_packet(ptr::null_mut()) };
}
}
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum MacEvent<'a> {
MlmeAssociateCnf(&'a AssociateConfirm),
MlmeDisassociateCnf(&'a DisassociateConfirm),
MlmeGetCnf(&'a GetConfirm),
MlmeGtsCnf(&'a GtsConfirm),
MlmeResetCnf(&'a ResetConfirm),
MlmeRxEnableCnf(&'a RxEnableConfirm),
MlmeScanCnf(&'a ScanConfirm),
MlmeSetCnf(&'a SetConfirm),
MlmeStartCnf(&'a StartConfirm),
MlmePollCnf(&'a PollConfirm),
MlmeDpsCnf(&'a DpsConfirm),
MlmeSoundingCnf(&'a SoundingConfirm),
MlmeCalibrateCnf(&'a CalibrateConfirm),
McpsDataCnf(&'a DataConfirm),
McpsPurgeCnf(&'a PurgeConfirm),
MlmeAssociateInd(&'a AssociateIndication),
MlmeDisassociateInd(&'a DisassociateIndication),
MlmeBeaconNotifyInd(&'a BeaconNotifyIndication),
MlmeCommStatusInd(&'a CommStatusIndication),
MlmeGtsInd(&'a GtsIndication),
MlmeOrphanInd(&'a OrphanIndication),
MlmeSyncLossInd(&'a SyncLossIndication),
MlmeDpsInd(&'a DpsIndication),
McpsDataInd(&'a DataIndication),
MlmePollInd(&'a PollIndication),
}

View File

@ -3,14 +3,14 @@ use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
use embassy_sync::channel::Channel;
use crate::mac::commands::DataRequest;
use crate::mac::event::{Event, MacEvent};
use crate::mac::event::MacEvent;
use crate::mac::typedefs::{AddressMode, MacAddress, PanId, SecurityLevel};
use crate::mac::MTU;
use crate::sub::mac::Mac;
pub struct Runner<'a> {
mac_subsystem: Mac,
pub(crate) rx_channel: Channel<CriticalSectionRawMutex, Event<'a>, 1>,
pub(crate) rx_channel: Channel<CriticalSectionRawMutex, MacEvent<'a>, 1>,
pub(crate) tx_channel: Channel<CriticalSectionRawMutex, (&'a mut [u8; MTU], usize), 5>,
pub(crate) tx_buf_channel: Channel<CriticalSectionRawMutex, &'a mut [u8; MTU], 5>,
}
@ -36,7 +36,7 @@ impl<'a> Runner<'a> {
async {
loop {
if let Ok(mac_event) = self.mac_subsystem.read().await {
match *mac_event {
match mac_event {
MacEvent::McpsDataInd(_) => {
self.rx_channel.send(mac_event).await;
}

View File

@ -12,7 +12,7 @@ use crate::cmd::CmdPacket;
use crate::consts::TlPacketType;
use crate::evt::{EvtBox, EvtPacket};
use crate::mac::commands::MacCommand;
use crate::mac::event::Event;
use crate::mac::event::MacEvent;
use crate::mac::typedefs::MacError;
use crate::tables::{MAC_802_15_4_CMD_BUFFER, MAC_802_15_4_NOTIF_RSP_EVT_BUFFER};
use crate::{channels, evt};
@ -94,14 +94,16 @@ impl Mac {
}
}
pub async fn read(&self) -> Result<Event<'_>, ()> {
Event::new(self.tl_read().await)
pub async fn read(&self) -> Result<MacEvent<'_>, ()> {
MacEvent::new(self.tl_read().await)
}
}
impl evt::MemoryManager for Mac {
/// SAFETY: passing a pointer to something other than a managed event packet is UB
unsafe fn drop_event_packet(_: *mut EvtPacket) {
trace!("mac drop event");
// Write the ack
CmdPacket::write_into(
MAC_802_15_4_NOTIF_RSP_EVT_BUFFER.as_mut_ptr() as *mut _,
@ -111,7 +113,7 @@ impl evt::MemoryManager for Mac {
);
// Clear the rx flag
let _ = poll_once(Ipcc::receive::<bool>(
let _ = poll_once(Ipcc::receive::<()>(
channels::cpu2::IPCC_MAC_802_15_4_NOTIFICATION_ACK_CHANNEL,
|| None,
));