From 809d3476aac88ddba01cd16f0df3565e35beddea Mon Sep 17 00:00:00 2001 From: xoviat Date: Thu, 20 Jul 2023 16:45:04 -0500 Subject: [PATCH] wpan: further optimize mac event --- embassy-stm32-wpan/src/mac/driver.rs | 6 +- embassy-stm32-wpan/src/mac/event.rs | 83 ++++++++++++------------- embassy-stm32-wpan/src/mac/runner.rs | 6 +- embassy-stm32-wpan/src/sub/mac.rs | 10 +-- examples/stm32wb/src/bin/mac_ffd.rs | 39 +++--------- examples/stm32wb/src/bin/mac_ffd_net.rs | 35 +++-------- examples/stm32wb/src/bin/mac_rfd.rs | 24 +++---- tests/stm32/src/bin/wpan_mac.rs | 23 +++---- 8 files changed, 86 insertions(+), 140 deletions(-) diff --git a/embassy-stm32-wpan/src/mac/driver.rs b/embassy-stm32-wpan/src/mac/driver.rs index 3017808f..fffbb9ed 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 d975c5bd..b6f57fda 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 + + 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), -} diff --git a/embassy-stm32-wpan/src/mac/runner.rs b/embassy-stm32-wpan/src/mac/runner.rs index 779712cd..a0090012 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 5ecbfe8c..b0cf0248 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, )); diff --git a/examples/stm32wb/src/bin/mac_ffd.rs b/examples/stm32wb/src/bin/mac_ffd.rs index 7de30778..1379ac6b 100644 --- a/examples/stm32wb/src/bin/mac_ffd.rs +++ b/examples/stm32wb/src/bin/mac_ffd.rs @@ -73,10 +73,7 @@ async fn main(spawner: Spawner) { }) .await .unwrap(); - { - let evt = mbox.mac_subsystem.read().await.unwrap(); - defmt::info!("{:#x}", *evt); - } + defmt::info!("{:#x}", mbox.mac_subsystem.read().await.unwrap()); info!("setting extended address"); let extended_address: u64 = 0xACDE480000000001; @@ -87,10 +84,7 @@ async fn main(spawner: Spawner) { }) .await .unwrap(); - { - let evt = mbox.mac_subsystem.read().await.unwrap(); - defmt::info!("{:#x}", *evt); - } + defmt::info!("{:#x}", mbox.mac_subsystem.read().await.unwrap()); info!("setting short address"); let short_address: u16 = 0x1122; @@ -101,10 +95,7 @@ async fn main(spawner: Spawner) { }) .await .unwrap(); - { - let evt = mbox.mac_subsystem.read().await.unwrap(); - defmt::info!("{:#x}", *evt); - } + defmt::info!("{:#x}", mbox.mac_subsystem.read().await.unwrap()); info!("setting association permit"); let association_permit: bool = true; @@ -115,10 +106,7 @@ async fn main(spawner: Spawner) { }) .await .unwrap(); - { - let evt = mbox.mac_subsystem.read().await.unwrap(); - defmt::info!("{:#x}", *evt); - } + defmt::info!("{:#x}", mbox.mac_subsystem.read().await.unwrap()); info!("setting TX power"); let transmit_power: i8 = 2; @@ -129,10 +117,7 @@ async fn main(spawner: Spawner) { }) .await .unwrap(); - { - let evt = mbox.mac_subsystem.read().await.unwrap(); - defmt::info!("{:#x}", *evt); - } + defmt::info!("{:#x}", mbox.mac_subsystem.read().await.unwrap()); info!("starting FFD device"); mbox.mac_subsystem @@ -147,10 +132,7 @@ async fn main(spawner: Spawner) { }) .await .unwrap(); - { - let evt = mbox.mac_subsystem.read().await.unwrap(); - defmt::info!("{:#x}", *evt); - } + defmt::info!("{:#x}", mbox.mac_subsystem.read().await.unwrap()); info!("setting RX on when idle"); let rx_on_while_idle: bool = true; @@ -161,18 +143,15 @@ async fn main(spawner: Spawner) { }) .await .unwrap(); - { - let evt = mbox.mac_subsystem.read().await.unwrap(); - defmt::info!("{:#x}", *evt); - } + defmt::info!("{:#x}", mbox.mac_subsystem.read().await.unwrap()); loop { let evt = mbox.mac_subsystem.read().await; if let Ok(evt) = evt { defmt::info!("parsed mac event"); - defmt::info!("{:#x}", *evt); + defmt::info!("{:#x}", evt); - match *evt { + match evt { MacEvent::MlmeAssociateInd(association) => mbox .mac_subsystem .send_command(&AssociateResponse { diff --git a/examples/stm32wb/src/bin/mac_ffd_net.rs b/examples/stm32wb/src/bin/mac_ffd_net.rs index a55b1fc7..bbcd0a70 100644 --- a/examples/stm32wb/src/bin/mac_ffd_net.rs +++ b/examples/stm32wb/src/bin/mac_ffd_net.rs @@ -79,10 +79,7 @@ async fn main(spawner: Spawner) { }) .await .unwrap(); - { - let evt = mbox.mac_subsystem.read().await.unwrap(); - defmt::info!("{:#x}", *evt); - } + defmt::info!("{:#x}", mbox.mac_subsystem.read().await.unwrap()); info!("setting extended address"); let extended_address: u64 = 0xACDE480000000001; @@ -93,10 +90,7 @@ async fn main(spawner: Spawner) { }) .await .unwrap(); - { - let evt = mbox.mac_subsystem.read().await.unwrap(); - defmt::info!("{:#x}", *evt); - } + defmt::info!("{:#x}", mbox.mac_subsystem.read().await.unwrap()); info!("setting short address"); let short_address: u16 = 0x1122; @@ -107,10 +101,7 @@ async fn main(spawner: Spawner) { }) .await .unwrap(); - { - let evt = mbox.mac_subsystem.read().await.unwrap(); - defmt::info!("{:#x}", *evt); - } + defmt::info!("{:#x}", mbox.mac_subsystem.read().await.unwrap()); info!("setting association permit"); let association_permit: bool = true; @@ -121,10 +112,7 @@ async fn main(spawner: Spawner) { }) .await .unwrap(); - { - let evt = mbox.mac_subsystem.read().await.unwrap(); - defmt::info!("{:#x}", *evt); - } + defmt::info!("{:#x}", mbox.mac_subsystem.read().await.unwrap()); info!("setting TX power"); let transmit_power: i8 = 2; @@ -135,10 +123,7 @@ async fn main(spawner: Spawner) { }) .await .unwrap(); - { - let evt = mbox.mac_subsystem.read().await.unwrap(); - defmt::info!("{:#x}", *evt); - } + defmt::info!("{:#x}", mbox.mac_subsystem.read().await.unwrap()); info!("starting FFD device"); mbox.mac_subsystem @@ -153,10 +138,7 @@ async fn main(spawner: Spawner) { }) .await .unwrap(); - { - let evt = mbox.mac_subsystem.read().await.unwrap(); - defmt::info!("{:#x}", *evt); - } + defmt::info!("{:#x}", mbox.mac_subsystem.read().await.unwrap()); info!("setting RX on when idle"); let rx_on_while_idle: bool = true; @@ -167,10 +149,7 @@ async fn main(spawner: Spawner) { }) .await .unwrap(); - { - let evt = mbox.mac_subsystem.read().await.unwrap(); - defmt::info!("{:#x}", *evt); - } + defmt::info!("{:#x}", mbox.mac_subsystem.read().await.unwrap()); let tx_queue = [ make_static!([0u8; 127]), diff --git a/examples/stm32wb/src/bin/mac_rfd.rs b/examples/stm32wb/src/bin/mac_rfd.rs index d1307a84..4d8b6601 100644 --- a/examples/stm32wb/src/bin/mac_rfd.rs +++ b/examples/stm32wb/src/bin/mac_rfd.rs @@ -75,10 +75,7 @@ async fn main(spawner: Spawner) { }) .await .unwrap(); - { - let evt = mbox.mac_subsystem.read().await.unwrap(); - defmt::info!("{:#x}", *evt); - } + defmt::info!("{:#x}", mbox.mac_subsystem.read().await.unwrap()); info!("setting extended address"); let extended_address: u64 = 0xACDE480000000002; @@ -89,10 +86,7 @@ async fn main(spawner: Spawner) { }) .await .unwrap(); - { - let evt = mbox.mac_subsystem.read().await.unwrap(); - defmt::info!("{:#x}", *evt); - } + defmt::info!("{:#x}", mbox.mac_subsystem.read().await.unwrap()); info!("getting extended address"); mbox.mac_subsystem @@ -105,9 +99,9 @@ async fn main(spawner: Spawner) { { let evt = mbox.mac_subsystem.read().await.unwrap(); - info!("{:#x}", *evt); + info!("{:#x}", evt); - if let MacEvent::MlmeGetCnf(evt) = *evt { + if let MacEvent::MlmeGetCnf(evt) = evt { if evt.pib_attribute_value_len == 8 { let value = unsafe { core::ptr::read_unaligned(evt.pib_attribute_value_ptr as *const u64) }; @@ -133,9 +127,9 @@ async fn main(spawner: Spawner) { mbox.mac_subsystem.send_command(&a).await.unwrap(); let short_addr = { let evt = mbox.mac_subsystem.read().await.unwrap(); - info!("{:#x}", *evt); + info!("{:#x}", evt); - if let MacEvent::MlmeAssociateCnf(conf) = *evt { + if let MacEvent::MlmeAssociateCnf(conf) = evt { conf.assoc_short_address } else { defmt::panic!() @@ -152,7 +146,7 @@ async fn main(spawner: Spawner) { .unwrap(); { let evt = mbox.mac_subsystem.read().await.unwrap(); - info!("{:#x}", *evt); + info!("{:#x}", evt); } info!("sending data"); @@ -176,12 +170,12 @@ async fn main(spawner: Spawner) { .unwrap(); { let evt = mbox.mac_subsystem.read().await.unwrap(); - info!("{:#x}", *evt); + info!("{:#x}", evt); } loop { match mbox.mac_subsystem.read().await { - Ok(evt) => info!("{:#x}", *evt), + Ok(evt) => info!("{:#x}", evt), _ => continue, }; } diff --git a/tests/stm32/src/bin/wpan_mac.rs b/tests/stm32/src/bin/wpan_mac.rs index 2b0d67bb..b04a19ee 100644 --- a/tests/stm32/src/bin/wpan_mac.rs +++ b/tests/stm32/src/bin/wpan_mac.rs @@ -57,7 +57,7 @@ async fn main(spawner: Spawner) { .unwrap(); { let evt = mbox.mac_subsystem.read().await.unwrap(); - info!("{:#x}", *evt); + info!("{:#x}", evt); } info!("setting extended address"); @@ -71,7 +71,7 @@ async fn main(spawner: Spawner) { .unwrap(); { let evt = mbox.mac_subsystem.read().await.unwrap(); - info!("{:#x}", *evt); + info!("{:#x}", evt); } info!("getting extended address"); @@ -85,9 +85,9 @@ async fn main(spawner: Spawner) { { let evt = mbox.mac_subsystem.read().await.unwrap(); - info!("{:#x}", *evt); + info!("{:#x}", evt); - if let MacEvent::MlmeGetCnf(evt) = *evt { + if let MacEvent::MlmeGetCnf(evt) = evt { if evt.pib_attribute_value_len == 8 { let value = unsafe { core::ptr::read_unaligned(evt.pib_attribute_value_ptr as *const u64) }; @@ -111,18 +111,13 @@ async fn main(spawner: Spawner) { }; info!("{}", a); mbox.mac_subsystem.send_command(&a).await.unwrap(); - let short_addr = { - let evt = mbox.mac_subsystem.read().await.unwrap(); - info!("{:#x}", *evt); - - if let MacEvent::MlmeAssociateCnf(conf) = *evt { - conf.assoc_short_address - } else { - defmt::panic!() - } + let short_addr = if let MacEvent::MlmeAssociateCnf(conf) = mbox.mac_subsystem.read().await.unwrap() { + conf.assoc_short_address + } else { + defmt::panic!() }; - _ = short_addr; + info!("{}", short_addr); info!("Test OK"); cortex_m::asm::bkpt();