aboutsummaryrefslogtreecommitdiff
path: root/embassy-stm32-wpan
diff options
context:
space:
mode:
authorxoviat <[email protected]>2023-07-20 16:45:04 -0500
committerxoviat <[email protected]>2023-07-20 16:45:04 -0500
commit809d3476aac88ddba01cd16f0df3565e35beddea (patch)
tree1a02eb3c0a11ad9703c5c0e7de4f1f193fc64976 /embassy-stm32-wpan
parent02d57afd51451fe9e7d224a0ea665a665ba2b72f (diff)
wpan: further optimize mac event
Diffstat (limited to 'embassy-stm32-wpan')
-rw-r--r--embassy-stm32-wpan/src/mac/driver.rs6
-rw-r--r--embassy-stm32-wpan/src/mac/event.rs81
-rw-r--r--embassy-stm32-wpan/src/mac/runner.rs6
-rw-r--r--embassy-stm32-wpan/src/sub/mac.rs10
4 files changed, 51 insertions, 52 deletions
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};
7use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex; 7use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
8use embassy_sync::channel::Channel; 8use embassy_sync::channel::Channel;
9 9
10use crate::mac::event::{Event, MacEvent}; 10use crate::mac::event::MacEvent;
11use crate::mac::runner::Runner; 11use crate::mac::runner::Runner;
12use crate::mac::MTU; 12use crate::mac::MTU;
13 13
@@ -81,7 +81,7 @@ impl<'d> embassy_net_driver::Driver for Driver<'d> {
81} 81}
82 82
83pub struct RxToken<'d> { 83pub struct RxToken<'d> {
84 rx: &'d Channel<CriticalSectionRawMutex, Event<'d>, 1>, 84 rx: &'d Channel<CriticalSectionRawMutex, MacEvent<'d>, 1>,
85} 85}
86 86
87impl<'d> embassy_net_driver::RxToken for RxToken<'d> { 87impl<'d> embassy_net_driver::RxToken for RxToken<'d> {
@@ -91,7 +91,7 @@ impl<'d> embassy_net_driver::RxToken for RxToken<'d> {
91 { 91 {
92 // Only valid data events should be put into the queue 92 // Only valid data events should be put into the queue
93 93
94 let data_event = match *self.rx.try_recv().unwrap() { 94 let data_event = match self.rx.try_recv().unwrap() {
95 MacEvent::McpsDataInd(data_event) => data_event, 95 MacEvent::McpsDataInd(data_event) => data_event,
96 _ => unreachable!(), 96 _ => unreachable!(),
97 }; 97 };
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 @@
1use core::{mem, ops}; 1use core::{mem, ptr};
2 2
3use super::indications::{ 3use super::indications::{
4 AssociateIndication, BeaconNotifyIndication, CommStatusIndication, DataIndication, DisassociateIndication, 4 AssociateIndication, BeaconNotifyIndication, CommStatusIndication, DataIndication, DisassociateIndication,
@@ -8,9 +8,9 @@ use super::responses::{
8 AssociateConfirm, CalibrateConfirm, DataConfirm, DisassociateConfirm, DpsConfirm, GetConfirm, GtsConfirm, 8 AssociateConfirm, CalibrateConfirm, DataConfirm, DisassociateConfirm, DpsConfirm, GetConfirm, GtsConfirm,
9 PollConfirm, PurgeConfirm, ResetConfirm, RxEnableConfirm, ScanConfirm, SetConfirm, SoundingConfirm, StartConfirm, 9 PollConfirm, PurgeConfirm, ResetConfirm, RxEnableConfirm, ScanConfirm, SetConfirm, SoundingConfirm, StartConfirm,
10}; 10};
11use crate::evt::EvtBox; 11use crate::evt::{EvtBox, MemoryManager};
12use crate::mac::opcodes::OpcodeM0ToM4; 12use crate::mac::opcodes::OpcodeM0ToM4;
13use crate::sub::mac::Mac; 13use crate::sub::mac::{self, Mac};
14 14
15pub(crate) trait ParseableMacEvent: Sized { 15pub(crate) trait ParseableMacEvent: Sized {
16 fn from_buffer<'a>(buf: &'a [u8]) -> Result<&'a Self, ()> { 16 fn from_buffer<'a>(buf: &'a [u8]) -> Result<&'a Self, ()> {
@@ -22,13 +22,36 @@ pub(crate) trait ParseableMacEvent: Sized {
22 } 22 }
23} 23}
24 24
25pub struct Event<'a> { 25#[cfg_attr(feature = "defmt", derive(defmt::Format))]
26 #[allow(dead_code)] 26pub enum MacEvent<'a> {
27 event_box: EvtBox<Mac>, 27 MlmeAssociateCnf(&'a AssociateConfirm),
28 mac_event: MacEvent<'a>, 28 MlmeDisassociateCnf(&'a DisassociateConfirm),
29 MlmeGetCnf(&'a GetConfirm),
30 MlmeGtsCnf(&'a GtsConfirm),
31 MlmeResetCnf(&'a ResetConfirm),
32 MlmeRxEnableCnf(&'a RxEnableConfirm),
33 MlmeScanCnf(&'a ScanConfirm),
34 MlmeSetCnf(&'a SetConfirm),
35 MlmeStartCnf(&'a StartConfirm),
36 MlmePollCnf(&'a PollConfirm),
37 MlmeDpsCnf(&'a DpsConfirm),
38 MlmeSoundingCnf(&'a SoundingConfirm),
39 MlmeCalibrateCnf(&'a CalibrateConfirm),
40 McpsDataCnf(&'a DataConfirm),
41 McpsPurgeCnf(&'a PurgeConfirm),
42 MlmeAssociateInd(&'a AssociateIndication),
43 MlmeDisassociateInd(&'a DisassociateIndication),
44 MlmeBeaconNotifyInd(&'a BeaconNotifyIndication),
45 MlmeCommStatusInd(&'a CommStatusIndication),
46 MlmeGtsInd(&'a GtsIndication),
47 MlmeOrphanInd(&'a OrphanIndication),
48 MlmeSyncLossInd(&'a SyncLossIndication),
49 MlmeDpsInd(&'a DpsIndication),
50 McpsDataInd(&'a DataIndication),
51 MlmePollInd(&'a PollIndication),
29} 52}
30 53
31impl<'a> Event<'a> { 54impl<'a> MacEvent<'a> {
32 pub(crate) fn new(event_box: EvtBox<Mac>) -> Result<Self, ()> { 55 pub(crate) fn new(event_box: EvtBox<Mac>) -> Result<Self, ()> {
33 let payload = event_box.payload(); 56 let payload = event_box.payload();
34 let opcode = u16::from_le_bytes(payload[0..2].try_into().unwrap()); 57 let opcode = u16::from_le_bytes(payload[0..2].try_into().unwrap());
@@ -111,43 +134,17 @@ impl<'a> Event<'a> {
111 } 134 }
112 }; 135 };
113 136
114 Ok(Self { event_box, mac_event }) 137 // Forget the event box so that drop isn't called
115 } 138 // We want to handle the lifetime ourselves
116}
117 139
118impl<'a> ops::Deref for Event<'a> { 140 mem::forget(event_box);
119 type Target = MacEvent<'a>;
120 141
121 fn deref(&self) -> &Self::Target { 142 Ok(mac_event)
122 &self.mac_event
123 } 143 }
124} 144}
125 145
126#[cfg_attr(feature = "defmt", derive(defmt::Format))] 146impl<'a> Drop for MacEvent<'a> {
127pub enum MacEvent<'a> { 147 fn drop(&mut self) {
128 MlmeAssociateCnf(&'a AssociateConfirm), 148 unsafe { mac::Mac::drop_event_packet(ptr::null_mut()) };
129 MlmeDisassociateCnf(&'a DisassociateConfirm), 149 }
130 MlmeGetCnf(&'a GetConfirm),
131 MlmeGtsCnf(&'a GtsConfirm),
132 MlmeResetCnf(&'a ResetConfirm),
133 MlmeRxEnableCnf(&'a RxEnableConfirm),
134 MlmeScanCnf(&'a ScanConfirm),
135 MlmeSetCnf(&'a SetConfirm),
136 MlmeStartCnf(&'a StartConfirm),
137 MlmePollCnf(&'a PollConfirm),
138 MlmeDpsCnf(&'a DpsConfirm),
139 MlmeSoundingCnf(&'a SoundingConfirm),
140 MlmeCalibrateCnf(&'a CalibrateConfirm),
141 McpsDataCnf(&'a DataConfirm),
142 McpsPurgeCnf(&'a PurgeConfirm),
143 MlmeAssociateInd(&'a AssociateIndication),
144 MlmeDisassociateInd(&'a DisassociateIndication),
145 MlmeBeaconNotifyInd(&'a BeaconNotifyIndication),
146 MlmeCommStatusInd(&'a CommStatusIndication),
147 MlmeGtsInd(&'a GtsIndication),
148 MlmeOrphanInd(&'a OrphanIndication),
149 MlmeSyncLossInd(&'a SyncLossIndication),
150 MlmeDpsInd(&'a DpsIndication),
151 McpsDataInd(&'a DataIndication),
152 MlmePollInd(&'a PollIndication),
153} 150}
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;
3use embassy_sync::channel::Channel; 3use embassy_sync::channel::Channel;
4 4
5use crate::mac::commands::DataRequest; 5use crate::mac::commands::DataRequest;
6use crate::mac::event::{Event, MacEvent}; 6use crate::mac::event::MacEvent;
7use crate::mac::typedefs::{AddressMode, MacAddress, PanId, SecurityLevel}; 7use crate::mac::typedefs::{AddressMode, MacAddress, PanId, SecurityLevel};
8use crate::mac::MTU; 8use crate::mac::MTU;
9use crate::sub::mac::Mac; 9use crate::sub::mac::Mac;
10 10
11pub struct Runner<'a> { 11pub struct Runner<'a> {
12 mac_subsystem: Mac, 12 mac_subsystem: Mac,
13 pub(crate) rx_channel: Channel<CriticalSectionRawMutex, Event<'a>, 1>, 13 pub(crate) rx_channel: Channel<CriticalSectionRawMutex, MacEvent<'a>, 1>,
14 pub(crate) tx_channel: Channel<CriticalSectionRawMutex, (&'a mut [u8; MTU], usize), 5>, 14 pub(crate) tx_channel: Channel<CriticalSectionRawMutex, (&'a mut [u8; MTU], usize), 5>,
15 pub(crate) tx_buf_channel: Channel<CriticalSectionRawMutex, &'a mut [u8; MTU], 5>, 15 pub(crate) tx_buf_channel: Channel<CriticalSectionRawMutex, &'a mut [u8; MTU], 5>,
16} 16}
@@ -36,7 +36,7 @@ impl<'a> Runner<'a> {
36 async { 36 async {
37 loop { 37 loop {
38 if let Ok(mac_event) = self.mac_subsystem.read().await { 38 if let Ok(mac_event) = self.mac_subsystem.read().await {
39 match *mac_event { 39 match mac_event {
40 MacEvent::McpsDataInd(_) => { 40 MacEvent::McpsDataInd(_) => {
41 self.rx_channel.send(mac_event).await; 41 self.rx_channel.send(mac_event).await;
42 } 42 }
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;
12use crate::consts::TlPacketType; 12use crate::consts::TlPacketType;
13use crate::evt::{EvtBox, EvtPacket}; 13use crate::evt::{EvtBox, EvtPacket};
14use crate::mac::commands::MacCommand; 14use crate::mac::commands::MacCommand;
15use crate::mac::event::Event; 15use crate::mac::event::MacEvent;
16use crate::mac::typedefs::MacError; 16use crate::mac::typedefs::MacError;
17use crate::tables::{MAC_802_15_4_CMD_BUFFER, MAC_802_15_4_NOTIF_RSP_EVT_BUFFER}; 17use crate::tables::{MAC_802_15_4_CMD_BUFFER, MAC_802_15_4_NOTIF_RSP_EVT_BUFFER};
18use crate::{channels, evt}; 18use crate::{channels, evt};
@@ -94,14 +94,16 @@ impl Mac {
94 } 94 }
95 } 95 }
96 96
97 pub async fn read(&self) -> Result<Event<'_>, ()> { 97 pub async fn read(&self) -> Result<MacEvent<'_>, ()> {
98 Event::new(self.tl_read().await) 98 MacEvent::new(self.tl_read().await)
99 } 99 }
100} 100}
101 101
102impl evt::MemoryManager for Mac { 102impl evt::MemoryManager for Mac {
103 /// SAFETY: passing a pointer to something other than a managed event packet is UB 103 /// SAFETY: passing a pointer to something other than a managed event packet is UB
104 unsafe fn drop_event_packet(_: *mut EvtPacket) { 104 unsafe fn drop_event_packet(_: *mut EvtPacket) {
105 trace!("mac drop event");
106
105 // Write the ack 107 // Write the ack
106 CmdPacket::write_into( 108 CmdPacket::write_into(
107 MAC_802_15_4_NOTIF_RSP_EVT_BUFFER.as_mut_ptr() as *mut _, 109 MAC_802_15_4_NOTIF_RSP_EVT_BUFFER.as_mut_ptr() as *mut _,
@@ -111,7 +113,7 @@ impl evt::MemoryManager for Mac {
111 ); 113 );
112 114
113 // Clear the rx flag 115 // Clear the rx flag
114 let _ = poll_once(Ipcc::receive::<bool>( 116 let _ = poll_once(Ipcc::receive::<()>(
115 channels::cpu2::IPCC_MAC_802_15_4_NOTIFICATION_ACK_CHANNEL, 117 channels::cpu2::IPCC_MAC_802_15_4_NOTIFICATION_ACK_CHANNEL,
116 || None, 118 || None,
117 )); 119 ));