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::blocking_mutex::raw::CriticalSectionRawMutex;
use embassy_sync::channel::Channel; use embassy_sync::channel::Channel;
use crate::mac::event::{Event, MacEvent}; use crate::mac::event::MacEvent;
use crate::mac::runner::Runner; use crate::mac::runner::Runner;
use crate::mac::MTU; use crate::mac::MTU;
@ -81,7 +81,7 @@ impl<'d> embassy_net_driver::Driver for Driver<'d> {
} }
pub struct RxToken<'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> { 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 // 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, MacEvent::McpsDataInd(data_event) => data_event,
_ => unreachable!(), _ => unreachable!(),
}; };

View file

@ -1,4 +1,4 @@
use core::{mem, ops}; use core::{mem, ptr};
use super::indications::{ use super::indications::{
AssociateIndication, BeaconNotifyIndication, CommStatusIndication, DataIndication, DisassociateIndication, AssociateIndication, BeaconNotifyIndication, CommStatusIndication, DataIndication, DisassociateIndication,
@ -8,9 +8,9 @@ use super::responses::{
AssociateConfirm, CalibrateConfirm, DataConfirm, DisassociateConfirm, DpsConfirm, GetConfirm, GtsConfirm, AssociateConfirm, CalibrateConfirm, DataConfirm, DisassociateConfirm, DpsConfirm, GetConfirm, GtsConfirm,
PollConfirm, PurgeConfirm, ResetConfirm, RxEnableConfirm, ScanConfirm, SetConfirm, SoundingConfirm, StartConfirm, PollConfirm, PurgeConfirm, ResetConfirm, RxEnableConfirm, ScanConfirm, SetConfirm, SoundingConfirm, StartConfirm,
}; };
use crate::evt::EvtBox; use crate::evt::{EvtBox, MemoryManager};
use crate::mac::opcodes::OpcodeM0ToM4; use crate::mac::opcodes::OpcodeM0ToM4;
use crate::sub::mac::Mac; use crate::sub::mac::{self, Mac};
pub(crate) trait ParseableMacEvent: Sized { pub(crate) trait ParseableMacEvent: Sized {
fn from_buffer<'a>(buf: &'a [u8]) -> Result<&'a Self, ()> { fn from_buffer<'a>(buf: &'a [u8]) -> Result<&'a Self, ()> {
@ -22,13 +22,36 @@ pub(crate) trait ParseableMacEvent: Sized {
} }
} }
pub struct Event<'a> { #[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[allow(dead_code)] pub enum MacEvent<'a> {
event_box: EvtBox<Mac>, MlmeAssociateCnf(&'a AssociateConfirm),
mac_event: MacEvent<'a>, 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, ()> { pub(crate) fn new(event_box: EvtBox<Mac>) -> Result<Self, ()> {
let payload = event_box.payload(); let payload = event_box.payload();
let opcode = u16::from_le_bytes(payload[0..2].try_into().unwrap()); 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> { impl<'a> Drop for MacEvent<'a> {
type Target = MacEvent<'a>; fn drop(&mut self) {
unsafe { mac::Mac::drop_event_packet(ptr::null_mut()) };
fn deref(&self) -> &Self::Target {
&self.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),
}

View file

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

View file

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

View file

@ -73,10 +73,7 @@ async fn main(spawner: Spawner) {
}) })
.await .await
.unwrap(); .unwrap();
{ defmt::info!("{:#x}", mbox.mac_subsystem.read().await.unwrap());
let evt = mbox.mac_subsystem.read().await.unwrap();
defmt::info!("{:#x}", *evt);
}
info!("setting extended address"); info!("setting extended address");
let extended_address: u64 = 0xACDE480000000001; let extended_address: u64 = 0xACDE480000000001;
@ -87,10 +84,7 @@ async fn main(spawner: Spawner) {
}) })
.await .await
.unwrap(); .unwrap();
{ defmt::info!("{:#x}", mbox.mac_subsystem.read().await.unwrap());
let evt = mbox.mac_subsystem.read().await.unwrap();
defmt::info!("{:#x}", *evt);
}
info!("setting short address"); info!("setting short address");
let short_address: u16 = 0x1122; let short_address: u16 = 0x1122;
@ -101,10 +95,7 @@ async fn main(spawner: Spawner) {
}) })
.await .await
.unwrap(); .unwrap();
{ defmt::info!("{:#x}", mbox.mac_subsystem.read().await.unwrap());
let evt = mbox.mac_subsystem.read().await.unwrap();
defmt::info!("{:#x}", *evt);
}
info!("setting association permit"); info!("setting association permit");
let association_permit: bool = true; let association_permit: bool = true;
@ -115,10 +106,7 @@ async fn main(spawner: Spawner) {
}) })
.await .await
.unwrap(); .unwrap();
{ defmt::info!("{:#x}", mbox.mac_subsystem.read().await.unwrap());
let evt = mbox.mac_subsystem.read().await.unwrap();
defmt::info!("{:#x}", *evt);
}
info!("setting TX power"); info!("setting TX power");
let transmit_power: i8 = 2; let transmit_power: i8 = 2;
@ -129,10 +117,7 @@ async fn main(spawner: Spawner) {
}) })
.await .await
.unwrap(); .unwrap();
{ defmt::info!("{:#x}", mbox.mac_subsystem.read().await.unwrap());
let evt = mbox.mac_subsystem.read().await.unwrap();
defmt::info!("{:#x}", *evt);
}
info!("starting FFD device"); info!("starting FFD device");
mbox.mac_subsystem mbox.mac_subsystem
@ -147,10 +132,7 @@ async fn main(spawner: Spawner) {
}) })
.await .await
.unwrap(); .unwrap();
{ defmt::info!("{:#x}", mbox.mac_subsystem.read().await.unwrap());
let evt = mbox.mac_subsystem.read().await.unwrap();
defmt::info!("{:#x}", *evt);
}
info!("setting RX on when idle"); info!("setting RX on when idle");
let rx_on_while_idle: bool = true; let rx_on_while_idle: bool = true;
@ -161,18 +143,15 @@ async fn main(spawner: Spawner) {
}) })
.await .await
.unwrap(); .unwrap();
{ defmt::info!("{:#x}", mbox.mac_subsystem.read().await.unwrap());
let evt = mbox.mac_subsystem.read().await.unwrap();
defmt::info!("{:#x}", *evt);
}
loop { loop {
let evt = mbox.mac_subsystem.read().await; let evt = mbox.mac_subsystem.read().await;
if let Ok(evt) = evt { if let Ok(evt) = evt {
defmt::info!("parsed mac event"); defmt::info!("parsed mac event");
defmt::info!("{:#x}", *evt); defmt::info!("{:#x}", evt);
match *evt { match evt {
MacEvent::MlmeAssociateInd(association) => mbox MacEvent::MlmeAssociateInd(association) => mbox
.mac_subsystem .mac_subsystem
.send_command(&AssociateResponse { .send_command(&AssociateResponse {

View file

@ -79,10 +79,7 @@ async fn main(spawner: Spawner) {
}) })
.await .await
.unwrap(); .unwrap();
{ defmt::info!("{:#x}", mbox.mac_subsystem.read().await.unwrap());
let evt = mbox.mac_subsystem.read().await.unwrap();
defmt::info!("{:#x}", *evt);
}
info!("setting extended address"); info!("setting extended address");
let extended_address: u64 = 0xACDE480000000001; let extended_address: u64 = 0xACDE480000000001;
@ -93,10 +90,7 @@ async fn main(spawner: Spawner) {
}) })
.await .await
.unwrap(); .unwrap();
{ defmt::info!("{:#x}", mbox.mac_subsystem.read().await.unwrap());
let evt = mbox.mac_subsystem.read().await.unwrap();
defmt::info!("{:#x}", *evt);
}
info!("setting short address"); info!("setting short address");
let short_address: u16 = 0x1122; let short_address: u16 = 0x1122;
@ -107,10 +101,7 @@ async fn main(spawner: Spawner) {
}) })
.await .await
.unwrap(); .unwrap();
{ defmt::info!("{:#x}", mbox.mac_subsystem.read().await.unwrap());
let evt = mbox.mac_subsystem.read().await.unwrap();
defmt::info!("{:#x}", *evt);
}
info!("setting association permit"); info!("setting association permit");
let association_permit: bool = true; let association_permit: bool = true;
@ -121,10 +112,7 @@ async fn main(spawner: Spawner) {
}) })
.await .await
.unwrap(); .unwrap();
{ defmt::info!("{:#x}", mbox.mac_subsystem.read().await.unwrap());
let evt = mbox.mac_subsystem.read().await.unwrap();
defmt::info!("{:#x}", *evt);
}
info!("setting TX power"); info!("setting TX power");
let transmit_power: i8 = 2; let transmit_power: i8 = 2;
@ -135,10 +123,7 @@ async fn main(spawner: Spawner) {
}) })
.await .await
.unwrap(); .unwrap();
{ defmt::info!("{:#x}", mbox.mac_subsystem.read().await.unwrap());
let evt = mbox.mac_subsystem.read().await.unwrap();
defmt::info!("{:#x}", *evt);
}
info!("starting FFD device"); info!("starting FFD device");
mbox.mac_subsystem mbox.mac_subsystem
@ -153,10 +138,7 @@ async fn main(spawner: Spawner) {
}) })
.await .await
.unwrap(); .unwrap();
{ defmt::info!("{:#x}", mbox.mac_subsystem.read().await.unwrap());
let evt = mbox.mac_subsystem.read().await.unwrap();
defmt::info!("{:#x}", *evt);
}
info!("setting RX on when idle"); info!("setting RX on when idle");
let rx_on_while_idle: bool = true; let rx_on_while_idle: bool = true;
@ -167,10 +149,7 @@ async fn main(spawner: Spawner) {
}) })
.await .await
.unwrap(); .unwrap();
{ defmt::info!("{:#x}", mbox.mac_subsystem.read().await.unwrap());
let evt = mbox.mac_subsystem.read().await.unwrap();
defmt::info!("{:#x}", *evt);
}
let tx_queue = [ let tx_queue = [
make_static!([0u8; 127]), make_static!([0u8; 127]),

View file

@ -75,10 +75,7 @@ async fn main(spawner: Spawner) {
}) })
.await .await
.unwrap(); .unwrap();
{ defmt::info!("{:#x}", mbox.mac_subsystem.read().await.unwrap());
let evt = mbox.mac_subsystem.read().await.unwrap();
defmt::info!("{:#x}", *evt);
}
info!("setting extended address"); info!("setting extended address");
let extended_address: u64 = 0xACDE480000000002; let extended_address: u64 = 0xACDE480000000002;
@ -89,10 +86,7 @@ async fn main(spawner: Spawner) {
}) })
.await .await
.unwrap(); .unwrap();
{ defmt::info!("{:#x}", mbox.mac_subsystem.read().await.unwrap());
let evt = mbox.mac_subsystem.read().await.unwrap();
defmt::info!("{:#x}", *evt);
}
info!("getting extended address"); info!("getting extended address");
mbox.mac_subsystem mbox.mac_subsystem
@ -105,9 +99,9 @@ async fn main(spawner: Spawner) {
{ {
let evt = mbox.mac_subsystem.read().await.unwrap(); 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 { if evt.pib_attribute_value_len == 8 {
let value = unsafe { core::ptr::read_unaligned(evt.pib_attribute_value_ptr as *const u64) }; 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(); mbox.mac_subsystem.send_command(&a).await.unwrap();
let short_addr = { let short_addr = {
let evt = mbox.mac_subsystem.read().await.unwrap(); 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 conf.assoc_short_address
} else { } else {
defmt::panic!() defmt::panic!()
@ -152,7 +146,7 @@ async fn main(spawner: Spawner) {
.unwrap(); .unwrap();
{ {
let evt = mbox.mac_subsystem.read().await.unwrap(); let evt = mbox.mac_subsystem.read().await.unwrap();
info!("{:#x}", *evt); info!("{:#x}", evt);
} }
info!("sending data"); info!("sending data");
@ -176,12 +170,12 @@ async fn main(spawner: Spawner) {
.unwrap(); .unwrap();
{ {
let evt = mbox.mac_subsystem.read().await.unwrap(); let evt = mbox.mac_subsystem.read().await.unwrap();
info!("{:#x}", *evt); info!("{:#x}", evt);
} }
loop { loop {
match mbox.mac_subsystem.read().await { match mbox.mac_subsystem.read().await {
Ok(evt) => info!("{:#x}", *evt), Ok(evt) => info!("{:#x}", evt),
_ => continue, _ => continue,
}; };
} }

View file

@ -57,7 +57,7 @@ async fn main(spawner: Spawner) {
.unwrap(); .unwrap();
{ {
let evt = mbox.mac_subsystem.read().await.unwrap(); let evt = mbox.mac_subsystem.read().await.unwrap();
info!("{:#x}", *evt); info!("{:#x}", evt);
} }
info!("setting extended address"); info!("setting extended address");
@ -71,7 +71,7 @@ async fn main(spawner: Spawner) {
.unwrap(); .unwrap();
{ {
let evt = mbox.mac_subsystem.read().await.unwrap(); let evt = mbox.mac_subsystem.read().await.unwrap();
info!("{:#x}", *evt); info!("{:#x}", evt);
} }
info!("getting extended address"); info!("getting extended address");
@ -85,9 +85,9 @@ async fn main(spawner: Spawner) {
{ {
let evt = mbox.mac_subsystem.read().await.unwrap(); 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 { if evt.pib_attribute_value_len == 8 {
let value = unsafe { core::ptr::read_unaligned(evt.pib_attribute_value_ptr as *const u64) }; 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); info!("{}", a);
mbox.mac_subsystem.send_command(&a).await.unwrap(); mbox.mac_subsystem.send_command(&a).await.unwrap();
let short_addr = { let short_addr = if let MacEvent::MlmeAssociateCnf(conf) = mbox.mac_subsystem.read().await.unwrap() {
let evt = mbox.mac_subsystem.read().await.unwrap();
info!("{:#x}", *evt);
if let MacEvent::MlmeAssociateCnf(conf) = *evt {
conf.assoc_short_address conf.assoc_short_address
} else { } else {
defmt::panic!() defmt::panic!()
}
}; };
_ = short_addr; info!("{}", short_addr);
info!("Test OK"); info!("Test OK");
cortex_m::asm::bkpt(); cortex_m::asm::bkpt();