From 809d3476aac88ddba01cd16f0df3565e35beddea Mon Sep 17 00:00:00 2001 From: xoviat Date: Thu, 20 Jul 2023 16:45:04 -0500 Subject: wpan: further optimize mac event --- embassy-stm32-wpan/src/mac/driver.rs | 6 +-- embassy-stm32-wpan/src/mac/event.rs | 81 +++++++++++++++++------------------- embassy-stm32-wpan/src/mac/runner.rs | 6 +-- embassy-stm32-wpan/src/sub/mac.rs | 10 +++-- 4 files changed, 51 insertions(+), 52 deletions(-) (limited to 'embassy-stm32-wpan/src') diff --git a/embassy-stm32-wpan/src/mac/driver.rs b/embassy-stm32-wpan/src/mac/driver.rs index 3017808f0..fffbb9edc 100644 --- a/embassy-stm32-wpan/src/mac/driver.rs +++ b/embassy-stm32-wpan/src/mac/driver.rs @@ -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, 1>, + rx: &'d Channel, 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!(), }; diff --git a/embassy-stm32-wpan/src/mac/event.rs b/embassy-stm32-wpan/src/mac/event.rs index d975c5bda..b6f57fdae 100644 --- a/embassy-stm32-wpan/src/mac/event.rs +++ b/embassy-stm32-wpan/src/mac/event.rs @@ -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_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) -> Result { 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 -impl<'a> ops::Deref for Event<'a> { - type Target = MacEvent<'a>; + mem::forget(event_box); - fn deref(&self) -> &Self::Target { - &self.mac_event + Ok(mac_event) } } -#[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> Drop for MacEvent<'a> { + fn drop(&mut self) { + unsafe { mac::Mac::drop_event_packet(ptr::null_mut()) }; + } } diff --git a/embassy-stm32-wpan/src/mac/runner.rs b/embassy-stm32-wpan/src/mac/runner.rs index 779712cdc..a0090012e 100644 --- a/embassy-stm32-wpan/src/mac/runner.rs +++ b/embassy-stm32-wpan/src/mac/runner.rs @@ -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, 1>, + pub(crate) rx_channel: Channel, 1>, pub(crate) tx_channel: Channel, pub(crate) tx_buf_channel: Channel, } @@ -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; } diff --git a/embassy-stm32-wpan/src/sub/mac.rs b/embassy-stm32-wpan/src/sub/mac.rs index 5ecbfe8c5..b0cf0248a 100644 --- a/embassy-stm32-wpan/src/sub/mac.rs +++ b/embassy-stm32-wpan/src/sub/mac.rs @@ -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::new(self.tl_read().await) + pub async fn read(&self) -> Result, ()> { + 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::( + let _ = poll_once(Ipcc::receive::<()>( channels::cpu2::IPCC_MAC_802_15_4_NOTIFICATION_ACK_CHANNEL, || None, )); -- cgit