diff options
| author | xoviat <[email protected]> | 2023-07-16 15:09:30 -0500 |
|---|---|---|
| committer | xoviat <[email protected]> | 2023-07-16 15:09:30 -0500 |
| commit | 28b419d65ede6ff29c79dbcaa27145f1c3458a57 (patch) | |
| tree | 318a614bc7ddb86f53af7029928d96201a793f5d /embassy-stm32-wpan | |
| parent | 7c465465c1a97234c3fbeb18154bfd7f79ab07f2 (diff) | |
wpan/mac: use lifetimes to control events
Diffstat (limited to 'embassy-stm32-wpan')
| -rw-r--r-- | embassy-stm32-wpan/src/mac/event.rs | 163 | ||||
| -rw-r--r-- | embassy-stm32-wpan/src/mac/indications.rs | 269 | ||||
| -rw-r--r-- | embassy-stm32-wpan/src/mac/responses.rs | 271 | ||||
| -rw-r--r-- | embassy-stm32-wpan/src/sub/mac.rs | 9 |
4 files changed, 156 insertions, 556 deletions
diff --git a/embassy-stm32-wpan/src/mac/event.rs b/embassy-stm32-wpan/src/mac/event.rs index c2bdc7e11..67f207d57 100644 --- a/embassy-stm32-wpan/src/mac/event.rs +++ b/embassy-stm32-wpan/src/mac/event.rs | |||
| @@ -1,3 +1,5 @@ | |||
| 1 | use core::mem; | ||
| 2 | |||
| 1 | use super::indications::{ | 3 | use super::indications::{ |
| 2 | AssociateIndication, BeaconNotifyIndication, CommStatusIndication, DataIndication, DisassociateIndication, | 4 | AssociateIndication, BeaconNotifyIndication, CommStatusIndication, DataIndication, DisassociateIndication, |
| 3 | DpsIndication, GtsIndication, OrphanIndication, PollIndication, SyncLossIndication, | 5 | DpsIndication, GtsIndication, OrphanIndication, PollIndication, SyncLossIndication, |
| @@ -6,89 +8,110 @@ use super::responses::{ | |||
| 6 | AssociateConfirm, CalibrateConfirm, DataConfirm, DisassociateConfirm, DpsConfirm, GetConfirm, GtsConfirm, | 8 | AssociateConfirm, CalibrateConfirm, DataConfirm, DisassociateConfirm, DpsConfirm, GetConfirm, GtsConfirm, |
| 7 | PollConfirm, PurgeConfirm, ResetConfirm, RxEnableConfirm, ScanConfirm, SetConfirm, SoundingConfirm, StartConfirm, | 9 | PollConfirm, PurgeConfirm, ResetConfirm, RxEnableConfirm, ScanConfirm, SetConfirm, SoundingConfirm, StartConfirm, |
| 8 | }; | 10 | }; |
| 11 | use crate::evt::EvtBox; | ||
| 9 | use crate::mac::opcodes::OpcodeM0ToM4; | 12 | use crate::mac::opcodes::OpcodeM0ToM4; |
| 13 | use crate::sub::mac::Mac; | ||
| 10 | 14 | ||
| 11 | pub trait ParseableMacEvent { | 15 | pub(crate) trait ParseableMacEvent: Sized { |
| 12 | const SIZE: usize; | 16 | fn from_buffer<'a>(buf: &'a [u8]) -> Result<&'a Self, ()> { |
| 13 | 17 | if buf.len() < mem::size_of::<Self>() { | |
| 14 | fn validate(buf: &[u8]) -> Result<(), ()> { | 18 | Err(()) |
| 15 | if buf.len() < Self::SIZE { | 19 | } else { |
| 16 | return Err(()); | 20 | Ok(unsafe { &*(buf as *const _ as *const Self) }) |
| 17 | } | 21 | } |
| 18 | |||
| 19 | Ok(()) | ||
| 20 | } | 22 | } |
| 21 | |||
| 22 | fn try_parse(buf: &[u8]) -> Result<Self, ()> | ||
| 23 | where | ||
| 24 | Self: Sized; | ||
| 25 | } | 23 | } |
| 26 | 24 | ||
| 27 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | 25 | pub struct Event { |
| 28 | pub enum MacEvent { | 26 | event_box: EvtBox<Mac>, |
| 29 | MlmeAssociateCnf(AssociateConfirm), | ||
| 30 | MlmeDisassociateCnf(DisassociateConfirm), | ||
| 31 | MlmeGetCnf(GetConfirm), | ||
| 32 | MlmeGtsCnf(GtsConfirm), | ||
| 33 | MlmeResetCnf(ResetConfirm), | ||
| 34 | MlmeRxEnableCnf(RxEnableConfirm), | ||
| 35 | MlmeScanCnf(ScanConfirm), | ||
| 36 | MlmeSetCnf(SetConfirm), | ||
| 37 | MlmeStartCnf(StartConfirm), | ||
| 38 | MlmePollCnf(PollConfirm), | ||
| 39 | MlmeDpsCnf(DpsConfirm), | ||
| 40 | MlmeSoundingCnf(SoundingConfirm), | ||
| 41 | MlmeCalibrateCnf(CalibrateConfirm), | ||
| 42 | McpsDataCnf(DataConfirm), | ||
| 43 | McpsPurgeCnf(PurgeConfirm), | ||
| 44 | MlmeAssociateInd(AssociateIndication), | ||
| 45 | MlmeDisassociateInd(DisassociateIndication), | ||
| 46 | MlmeBeaconNotifyInd(BeaconNotifyIndication), | ||
| 47 | MlmeCommStatusInd(CommStatusIndication), | ||
| 48 | MlmeGtsInd(GtsIndication), | ||
| 49 | MlmeOrphanInd(OrphanIndication), | ||
| 50 | MlmeSyncLossInd(SyncLossIndication), | ||
| 51 | MlmeDpsInd(DpsIndication), | ||
| 52 | McpsDataInd(DataIndication), | ||
| 53 | MlmePollInd(PollIndication), | ||
| 54 | } | 27 | } |
| 55 | 28 | ||
| 56 | impl TryFrom<&[u8]> for MacEvent { | 29 | impl Event { |
| 57 | type Error = (); | 30 | pub(crate) fn new(event_box: EvtBox<Mac>) -> Self { |
| 31 | Self { event_box } | ||
| 32 | } | ||
| 58 | 33 | ||
| 59 | fn try_from(value: &[u8]) -> Result<Self, Self::Error> { | 34 | pub fn mac_event<'a>(&'a self) -> Result<MacEvent<'a>, ()> { |
| 60 | let opcode = u16::from_le_bytes(value[0..2].try_into().unwrap()); | 35 | let payload = self.event_box.payload(); |
| 36 | let opcode = u16::from_le_bytes(payload[0..2].try_into().unwrap()); | ||
| 61 | 37 | ||
| 62 | let opcode = OpcodeM0ToM4::try_from(opcode)?; | 38 | let opcode = OpcodeM0ToM4::try_from(opcode)?; |
| 63 | 39 | ||
| 64 | let buf = &value[2..]; | ||
| 65 | |||
| 66 | match opcode { | 40 | match opcode { |
| 67 | OpcodeM0ToM4::MlmeAssociateCnf => Ok(Self::MlmeAssociateCnf(AssociateConfirm::try_parse(buf)?)), | 41 | OpcodeM0ToM4::MlmeAssociateCnf => Ok(MacEvent::MlmeAssociateCnf(AssociateConfirm::from_buffer( |
| 68 | OpcodeM0ToM4::MlmeDisassociateCnf => Ok(Self::MlmeDisassociateCnf(DisassociateConfirm::try_parse(buf)?)), | 42 | &payload[2..], |
| 69 | OpcodeM0ToM4::MlmeGetCnf => Ok(Self::MlmeGetCnf(GetConfirm::try_parse(buf)?)), | 43 | )?)), |
| 70 | OpcodeM0ToM4::MlmeGtsCnf => Ok(Self::MlmeGtsCnf(GtsConfirm::try_parse(buf)?)), | 44 | OpcodeM0ToM4::MlmeDisassociateCnf => Ok(MacEvent::MlmeDisassociateCnf(DisassociateConfirm::from_buffer( |
| 71 | OpcodeM0ToM4::MlmeResetCnf => Ok(Self::MlmeResetCnf(ResetConfirm::try_parse(buf)?)), | 45 | &payload[2..], |
| 72 | OpcodeM0ToM4::MlmeRxEnableCnf => Ok(Self::MlmeRxEnableCnf(RxEnableConfirm::try_parse(buf)?)), | 46 | )?)), |
| 73 | OpcodeM0ToM4::MlmeScanCnf => Ok(Self::MlmeScanCnf(ScanConfirm::try_parse(buf)?)), | 47 | OpcodeM0ToM4::MlmeGetCnf => Ok(MacEvent::MlmeGetCnf(GetConfirm::from_buffer(&payload[2..])?)), |
| 74 | OpcodeM0ToM4::MlmeSetCnf => Ok(Self::MlmeSetCnf(SetConfirm::try_parse(buf)?)), | 48 | OpcodeM0ToM4::MlmeGtsCnf => Ok(MacEvent::MlmeGtsCnf(GtsConfirm::from_buffer(&payload[2..])?)), |
| 75 | OpcodeM0ToM4::MlmeStartCnf => Ok(Self::MlmeStartCnf(StartConfirm::try_parse(buf)?)), | 49 | OpcodeM0ToM4::MlmeResetCnf => Ok(MacEvent::MlmeResetCnf(ResetConfirm::from_buffer(&payload[2..])?)), |
| 76 | OpcodeM0ToM4::MlmePollCnf => Ok(Self::MlmePollCnf(PollConfirm::try_parse(buf)?)), | 50 | OpcodeM0ToM4::MlmeRxEnableCnf => { |
| 77 | OpcodeM0ToM4::MlmeDpsCnf => Ok(Self::MlmeDpsCnf(DpsConfirm::try_parse(buf)?)), | 51 | Ok(MacEvent::MlmeRxEnableCnf(RxEnableConfirm::from_buffer(&payload[2..])?)) |
| 78 | OpcodeM0ToM4::MlmeSoundingCnf => Ok(Self::MlmeSoundingCnf(SoundingConfirm::try_parse(buf)?)), | 52 | } |
| 79 | OpcodeM0ToM4::MlmeCalibrateCnf => Ok(Self::MlmeCalibrateCnf(CalibrateConfirm::try_parse(buf)?)), | 53 | OpcodeM0ToM4::MlmeScanCnf => Ok(MacEvent::MlmeScanCnf(ScanConfirm::from_buffer(&payload[2..])?)), |
| 80 | OpcodeM0ToM4::McpsDataCnf => Ok(Self::McpsDataCnf(DataConfirm::try_parse(buf)?)), | 54 | OpcodeM0ToM4::MlmeSetCnf => Ok(MacEvent::MlmeSetCnf(SetConfirm::from_buffer(&payload[2..])?)), |
| 81 | OpcodeM0ToM4::McpsPurgeCnf => Ok(Self::McpsPurgeCnf(PurgeConfirm::try_parse(buf)?)), | 55 | OpcodeM0ToM4::MlmeStartCnf => Ok(MacEvent::MlmeStartCnf(StartConfirm::from_buffer(&payload[2..])?)), |
| 82 | OpcodeM0ToM4::MlmeAssociateInd => Ok(Self::MlmeAssociateInd(AssociateIndication::try_parse(buf)?)), | 56 | OpcodeM0ToM4::MlmePollCnf => Ok(MacEvent::MlmePollCnf(PollConfirm::from_buffer(&payload[2..])?)), |
| 83 | OpcodeM0ToM4::MlmeDisassociateInd => Ok(Self::MlmeDisassociateInd(DisassociateIndication::try_parse(buf)?)), | 57 | OpcodeM0ToM4::MlmeDpsCnf => Ok(MacEvent::MlmeDpsCnf(DpsConfirm::from_buffer(&payload[2..])?)), |
| 84 | OpcodeM0ToM4::MlmeBeaconNotifyInd => Ok(Self::MlmeBeaconNotifyInd(BeaconNotifyIndication::try_parse(buf)?)), | 58 | OpcodeM0ToM4::MlmeSoundingCnf => { |
| 85 | OpcodeM0ToM4::MlmeCommStatusInd => Ok(Self::MlmeCommStatusInd(CommStatusIndication::try_parse(buf)?)), | 59 | Ok(MacEvent::MlmeSoundingCnf(SoundingConfirm::from_buffer(&payload[2..])?)) |
| 86 | OpcodeM0ToM4::MlmeGtsInd => Ok(Self::MlmeGtsInd(GtsIndication::try_parse(buf)?)), | 60 | } |
| 87 | OpcodeM0ToM4::MlmeOrphanInd => Ok(Self::MlmeOrphanInd(OrphanIndication::try_parse(buf)?)), | 61 | OpcodeM0ToM4::MlmeCalibrateCnf => Ok(MacEvent::MlmeCalibrateCnf(CalibrateConfirm::from_buffer( |
| 88 | OpcodeM0ToM4::MlmeSyncLossInd => Ok(Self::MlmeSyncLossInd(SyncLossIndication::try_parse(buf)?)), | 62 | &payload[2..], |
| 89 | OpcodeM0ToM4::MlmeDpsInd => Ok(Self::MlmeDpsInd(DpsIndication::try_parse(buf)?)), | 63 | )?)), |
| 90 | OpcodeM0ToM4::McpsDataInd => Ok(Self::McpsDataInd(DataIndication::try_parse(buf)?)), | 64 | OpcodeM0ToM4::McpsDataCnf => Ok(MacEvent::McpsDataCnf(DataConfirm::from_buffer(&payload[2..])?)), |
| 91 | OpcodeM0ToM4::MlmePollInd => Ok(Self::MlmePollInd(PollIndication::try_parse(buf)?)), | 65 | OpcodeM0ToM4::McpsPurgeCnf => Ok(MacEvent::McpsPurgeCnf(PurgeConfirm::from_buffer(&payload[2..])?)), |
| 66 | OpcodeM0ToM4::MlmeAssociateInd => Ok(MacEvent::MlmeAssociateInd(AssociateIndication::from_buffer( | ||
| 67 | &payload[2..], | ||
| 68 | )?)), | ||
| 69 | OpcodeM0ToM4::MlmeDisassociateInd => Ok(MacEvent::MlmeDisassociateInd( | ||
| 70 | DisassociateIndication::from_buffer(&payload[2..])?, | ||
| 71 | )), | ||
| 72 | OpcodeM0ToM4::MlmeBeaconNotifyInd => Ok(MacEvent::MlmeBeaconNotifyInd( | ||
| 73 | BeaconNotifyIndication::from_buffer(&payload[2..])?, | ||
| 74 | )), | ||
| 75 | OpcodeM0ToM4::MlmeCommStatusInd => Ok(MacEvent::MlmeCommStatusInd(CommStatusIndication::from_buffer( | ||
| 76 | &payload[2..], | ||
| 77 | )?)), | ||
| 78 | OpcodeM0ToM4::MlmeGtsInd => Ok(MacEvent::MlmeGtsInd(GtsIndication::from_buffer(&payload[2..])?)), | ||
| 79 | OpcodeM0ToM4::MlmeOrphanInd => Ok(MacEvent::MlmeOrphanInd(OrphanIndication::from_buffer(&payload[2..])?)), | ||
| 80 | OpcodeM0ToM4::MlmeSyncLossInd => Ok(MacEvent::MlmeSyncLossInd(SyncLossIndication::from_buffer( | ||
| 81 | &payload[2..], | ||
| 82 | )?)), | ||
| 83 | OpcodeM0ToM4::MlmeDpsInd => Ok(MacEvent::MlmeDpsInd(DpsIndication::from_buffer(&payload[2..])?)), | ||
| 84 | OpcodeM0ToM4::McpsDataInd => Ok(MacEvent::McpsDataInd(DataIndication::from_buffer(&payload[2..])?)), | ||
| 85 | OpcodeM0ToM4::MlmePollInd => Ok(MacEvent::MlmePollInd(PollIndication::from_buffer(&payload[2..])?)), | ||
| 92 | } | 86 | } |
| 93 | } | 87 | } |
| 94 | } | 88 | } |
| 89 | |||
| 90 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | ||
| 91 | pub enum MacEvent<'a> { | ||
| 92 | MlmeAssociateCnf(&'a AssociateConfirm), | ||
| 93 | MlmeDisassociateCnf(&'a DisassociateConfirm), | ||
| 94 | MlmeGetCnf(&'a GetConfirm), | ||
| 95 | MlmeGtsCnf(&'a GtsConfirm), | ||
| 96 | MlmeResetCnf(&'a ResetConfirm), | ||
| 97 | MlmeRxEnableCnf(&'a RxEnableConfirm), | ||
| 98 | MlmeScanCnf(&'a ScanConfirm), | ||
| 99 | MlmeSetCnf(&'a SetConfirm), | ||
| 100 | MlmeStartCnf(&'a StartConfirm), | ||
| 101 | MlmePollCnf(&'a PollConfirm), | ||
| 102 | MlmeDpsCnf(&'a DpsConfirm), | ||
| 103 | MlmeSoundingCnf(&'a SoundingConfirm), | ||
| 104 | MlmeCalibrateCnf(&'a CalibrateConfirm), | ||
| 105 | McpsDataCnf(&'a DataConfirm), | ||
| 106 | McpsPurgeCnf(&'a PurgeConfirm), | ||
| 107 | MlmeAssociateInd(&'a AssociateIndication), | ||
| 108 | MlmeDisassociateInd(&'a DisassociateIndication), | ||
| 109 | MlmeBeaconNotifyInd(&'a BeaconNotifyIndication), | ||
| 110 | MlmeCommStatusInd(&'a CommStatusIndication), | ||
| 111 | MlmeGtsInd(&'a GtsIndication), | ||
| 112 | MlmeOrphanInd(&'a OrphanIndication), | ||
| 113 | MlmeSyncLossInd(&'a SyncLossIndication), | ||
| 114 | MlmeDpsInd(&'a DpsIndication), | ||
| 115 | McpsDataInd(&'a DataIndication), | ||
| 116 | MlmePollInd(&'a PollIndication), | ||
| 117 | } | ||
diff --git a/embassy-stm32-wpan/src/mac/indications.rs b/embassy-stm32-wpan/src/mac/indications.rs index 436b9ac9c..c7e9be84a 100644 --- a/embassy-stm32-wpan/src/mac/indications.rs +++ b/embassy-stm32-wpan/src/mac/indications.rs | |||
| @@ -23,22 +23,7 @@ pub struct AssociateIndication { | |||
| 23 | pub key_source: [u8; 8], | 23 | pub key_source: [u8; 8], |
| 24 | } | 24 | } |
| 25 | 25 | ||
| 26 | impl ParseableMacEvent for AssociateIndication { | 26 | impl ParseableMacEvent for AssociateIndication {} |
| 27 | const SIZE: usize = 20; | ||
| 28 | |||
| 29 | fn try_parse(buf: &[u8]) -> Result<Self, ()> { | ||
| 30 | Self::validate(buf)?; | ||
| 31 | |||
| 32 | Ok(Self { | ||
| 33 | device_address: [buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7]], | ||
| 34 | capability_information: Capabilities::from_bits(buf[8]).ok_or(())?, | ||
| 35 | security_level: SecurityLevel::try_from(buf[9])?, | ||
| 36 | key_id_mode: KeyIdMode::try_from(buf[10])?, | ||
| 37 | key_index: buf[11], | ||
| 38 | key_source: [buf[12], buf[13], buf[14], buf[15], buf[16], buf[17], buf[18], buf[19]], | ||
| 39 | }) | ||
| 40 | } | ||
| 41 | } | ||
| 42 | 27 | ||
| 43 | /// MLME DISASSOCIATE indication which will be used to send | 28 | /// MLME DISASSOCIATE indication which will be used to send |
| 44 | /// disassociation indication to the application. | 29 | /// disassociation indication to the application. |
| @@ -58,22 +43,7 @@ pub struct DisassociateIndication { | |||
| 58 | pub key_source: [u8; 8], | 43 | pub key_source: [u8; 8], |
| 59 | } | 44 | } |
| 60 | 45 | ||
| 61 | impl ParseableMacEvent for DisassociateIndication { | 46 | impl ParseableMacEvent for DisassociateIndication {} |
| 62 | const SIZE: usize = 20; | ||
| 63 | |||
| 64 | fn try_parse(buf: &[u8]) -> Result<Self, ()> { | ||
| 65 | Self::validate(buf)?; | ||
| 66 | |||
| 67 | Ok(Self { | ||
| 68 | device_address: [buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7]], | ||
| 69 | disassociation_reason: DisassociationReason::try_from(buf[8])?, | ||
| 70 | security_level: SecurityLevel::try_from(buf[9])?, | ||
| 71 | key_id_mode: KeyIdMode::try_from(buf[10])?, | ||
| 72 | key_index: buf[11], | ||
| 73 | key_source: [buf[12], buf[13], buf[14], buf[15], buf[16], buf[17], buf[18], buf[19]], | ||
| 74 | }) | ||
| 75 | } | ||
| 76 | } | ||
| 77 | 47 | ||
| 78 | /// MLME BEACON NOTIIFY Indication which is used to send parameters contained | 48 | /// MLME BEACON NOTIIFY Indication which is used to send parameters contained |
| 79 | /// within a beacon frame received by the MAC to the application | 49 | /// within a beacon frame received by the MAC to the application |
| @@ -94,34 +64,7 @@ pub struct BeaconNotifyIndication { | |||
| 94 | pub sdu_length: u8, | 64 | pub sdu_length: u8, |
| 95 | } | 65 | } |
| 96 | 66 | ||
| 97 | impl ParseableMacEvent for BeaconNotifyIndication { | 67 | impl ParseableMacEvent for BeaconNotifyIndication {} |
| 98 | const SIZE: usize = 88; | ||
| 99 | |||
| 100 | fn try_parse(buf: &[u8]) -> Result<Self, ()> { | ||
| 101 | // TODO: this is unchecked | ||
| 102 | |||
| 103 | Self::validate(buf)?; | ||
| 104 | |||
| 105 | let addr_list = [ | ||
| 106 | MacAddress::try_from(&buf[26..34])?, | ||
| 107 | MacAddress::try_from(&buf[34..42])?, | ||
| 108 | MacAddress::try_from(&buf[42..50])?, | ||
| 109 | MacAddress::try_from(&buf[50..58])?, | ||
| 110 | MacAddress::try_from(&buf[58..66])?, | ||
| 111 | MacAddress::try_from(&buf[66..74])?, | ||
| 112 | MacAddress::try_from(&buf[74..82])?, | ||
| 113 | ]; | ||
| 114 | |||
| 115 | Ok(Self { | ||
| 116 | sdu_ptr: u32::from_le_bytes(buf[0..4].try_into().unwrap()) as *const u8, | ||
| 117 | pan_descriptor: PanDescriptor::try_from(&buf[4..26])?, | ||
| 118 | addr_list, | ||
| 119 | bsn: buf[82], | ||
| 120 | pend_addr_spec: buf[83], | ||
| 121 | sdu_length: buf[83], | ||
| 122 | }) | ||
| 123 | } | ||
| 124 | } | ||
| 125 | 68 | ||
| 126 | /// MLME COMM STATUS Indication which is used by the MAC to indicate a communications status | 69 | /// MLME COMM STATUS Indication which is used by the MAC to indicate a communications status |
| 127 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | 70 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] |
| @@ -149,51 +92,7 @@ pub struct CommStatusIndication { | |||
| 149 | pub key_source: [u8; 8], | 92 | pub key_source: [u8; 8], |
| 150 | } | 93 | } |
| 151 | 94 | ||
| 152 | impl ParseableMacEvent for CommStatusIndication { | 95 | impl ParseableMacEvent for CommStatusIndication {} |
| 153 | const SIZE: usize = 32; | ||
| 154 | |||
| 155 | fn try_parse(buf: &[u8]) -> Result<Self, ()> { | ||
| 156 | Self::validate(buf)?; | ||
| 157 | |||
| 158 | let src_addr_mode = AddressMode::try_from(buf[2])?; | ||
| 159 | let dst_addr_mode = AddressMode::try_from(buf[3])?; | ||
| 160 | |||
| 161 | let src_address = match src_addr_mode { | ||
| 162 | AddressMode::NoAddress => MacAddress { short: [0, 0] }, | ||
| 163 | AddressMode::Reserved => MacAddress { short: [0, 0] }, | ||
| 164 | AddressMode::Short => MacAddress { | ||
| 165 | short: [buf[4], buf[5]], | ||
| 166 | }, | ||
| 167 | AddressMode::Extended => MacAddress { | ||
| 168 | extended: [buf[4], buf[5], buf[6], buf[7], buf[8], buf[9], buf[10], buf[11]], | ||
| 169 | }, | ||
| 170 | }; | ||
| 171 | |||
| 172 | let dst_address = match dst_addr_mode { | ||
| 173 | AddressMode::NoAddress => MacAddress { short: [0, 0] }, | ||
| 174 | AddressMode::Reserved => MacAddress { short: [0, 0] }, | ||
| 175 | AddressMode::Short => MacAddress { | ||
| 176 | short: [buf[12], buf[13]], | ||
| 177 | }, | ||
| 178 | AddressMode::Extended => MacAddress { | ||
| 179 | extended: [buf[12], buf[13], buf[14], buf[15], buf[16], buf[17], buf[18], buf[19]], | ||
| 180 | }, | ||
| 181 | }; | ||
| 182 | |||
| 183 | Ok(Self { | ||
| 184 | pan_id: PanId([buf[0], buf[1]]), | ||
| 185 | src_addr_mode, | ||
| 186 | dst_addr_mode, | ||
| 187 | src_address, | ||
| 188 | dst_address, | ||
| 189 | status: MacStatus::try_from(buf[20])?, | ||
| 190 | security_level: SecurityLevel::try_from(buf[21])?, | ||
| 191 | key_id_mode: KeyIdMode::try_from(buf[22])?, | ||
| 192 | key_index: buf[23], | ||
| 193 | key_source: [buf[24], buf[25], buf[26], buf[27], buf[28], buf[29], buf[30], buf[31]], | ||
| 194 | }) | ||
| 195 | } | ||
| 196 | } | ||
| 197 | 96 | ||
| 198 | /// MLME GTS Indication indicates that a GTS has been allocated or that a | 97 | /// MLME GTS Indication indicates that a GTS has been allocated or that a |
| 199 | /// previously allocated GTS has been deallocated | 98 | /// previously allocated GTS has been deallocated |
| @@ -209,27 +108,13 @@ pub struct GtsIndication { | |||
| 209 | pub key_id_mode: KeyIdMode, | 108 | pub key_id_mode: KeyIdMode, |
| 210 | /// Index of the key to be used | 109 | /// Index of the key to be used |
| 211 | pub key_index: u8, | 110 | pub key_index: u8, |
| 111 | /// byte stuffing to keep 32 bit alignment | ||
| 112 | a_stuffing: [u8; 2], | ||
| 212 | /// Originator of the key to be used | 113 | /// Originator of the key to be used |
| 213 | pub key_source: [u8; 8], | 114 | pub key_source: [u8; 8], |
| 214 | } | 115 | } |
| 215 | 116 | ||
| 216 | impl ParseableMacEvent for GtsIndication { | 117 | impl ParseableMacEvent for GtsIndication {} |
| 217 | const SIZE: usize = 16; | ||
| 218 | |||
| 219 | fn try_parse(buf: &[u8]) -> Result<Self, ()> { | ||
| 220 | Self::validate(buf)?; | ||
| 221 | |||
| 222 | Ok(Self { | ||
| 223 | device_address: [buf[0], buf[1]], | ||
| 224 | gts_characteristics: buf[2], | ||
| 225 | security_level: SecurityLevel::try_from(buf[3])?, | ||
| 226 | key_id_mode: KeyIdMode::try_from(buf[4])?, | ||
| 227 | key_index: buf[5], | ||
| 228 | // 2 byte stuffing | ||
| 229 | key_source: [buf[8], buf[9], buf[10], buf[11], buf[12], buf[13], buf[14], buf[15]], | ||
| 230 | }) | ||
| 231 | } | ||
| 232 | } | ||
| 233 | 118 | ||
| 234 | /// MLME ORPHAN Indication which is used by the coordinator to notify the | 119 | /// MLME ORPHAN Indication which is used by the coordinator to notify the |
| 235 | /// application of the presence of an orphaned device | 120 | /// application of the presence of an orphaned device |
| @@ -245,24 +130,11 @@ pub struct OrphanIndication { | |||
| 245 | pub key_id_mode: KeyIdMode, | 130 | pub key_id_mode: KeyIdMode, |
| 246 | /// Index of the key used by the originator of the received frame | 131 | /// Index of the key used by the originator of the received frame |
| 247 | pub key_index: u8, | 132 | pub key_index: u8, |
| 133 | /// byte stuffing to keep 32 bit alignment | ||
| 134 | a_stuffing: [u8; 1], | ||
| 248 | } | 135 | } |
| 249 | 136 | ||
| 250 | impl ParseableMacEvent for OrphanIndication { | 137 | impl ParseableMacEvent for OrphanIndication {} |
| 251 | const SIZE: usize = 20; | ||
| 252 | |||
| 253 | fn try_parse(buf: &[u8]) -> Result<Self, ()> { | ||
| 254 | Self::validate(buf)?; | ||
| 255 | |||
| 256 | Ok(Self { | ||
| 257 | orphan_address: [buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7]], | ||
| 258 | key_source: [buf[8], buf[9], buf[10], buf[11], buf[12], buf[13], buf[14], buf[15]], | ||
| 259 | security_level: SecurityLevel::try_from(buf[16])?, | ||
| 260 | key_id_mode: KeyIdMode::try_from(buf[17])?, | ||
| 261 | key_index: buf[18], | ||
| 262 | // 1 byte stuffing | ||
| 263 | }) | ||
| 264 | } | ||
| 265 | } | ||
| 266 | 138 | ||
| 267 | /// MLME SYNC LOSS Indication which is used by the MAC to indicate the loss | 139 | /// MLME SYNC LOSS Indication which is used by the MAC to indicate the loss |
| 268 | /// of synchronization with the coordinator | 140 | /// of synchronization with the coordinator |
| @@ -286,42 +158,20 @@ pub struct SyncLossIndication { | |||
| 286 | pub key_source: [u8; 8], | 158 | pub key_source: [u8; 8], |
| 287 | } | 159 | } |
| 288 | 160 | ||
| 289 | impl ParseableMacEvent for SyncLossIndication { | 161 | impl ParseableMacEvent for SyncLossIndication {} |
| 290 | const SIZE: usize = 16; | ||
| 291 | |||
| 292 | fn try_parse(buf: &[u8]) -> Result<Self, ()> { | ||
| 293 | Self::validate(buf)?; | ||
| 294 | |||
| 295 | Ok(Self { | ||
| 296 | pan_id: PanId([buf[0], buf[1]]), | ||
| 297 | loss_reason: buf[2], | ||
| 298 | channel_number: MacChannel::try_from(buf[3])?, | ||
| 299 | channel_page: buf[4], | ||
| 300 | security_level: SecurityLevel::try_from(buf[5])?, | ||
| 301 | key_id_mode: KeyIdMode::try_from(buf[6])?, | ||
| 302 | key_index: buf[7], | ||
| 303 | key_source: [buf[8], buf[9], buf[10], buf[11], buf[12], buf[13], buf[14], buf[15]], | ||
| 304 | }) | ||
| 305 | } | ||
| 306 | } | ||
| 307 | 162 | ||
| 308 | /// MLME DPS Indication which indicates the expiration of the DPSIndexDuration | 163 | /// MLME DPS Indication which indicates the expiration of the DPSIndexDuration |
| 309 | /// and the resetting of the DPS values in the PHY | 164 | /// and the resetting of the DPS values in the PHY |
| 310 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | 165 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] |
| 311 | pub struct DpsIndication; | 166 | pub struct DpsIndication { |
| 312 | 167 | /// byte stuffing to keep 32 bit alignment | |
| 313 | impl ParseableMacEvent for DpsIndication { | 168 | a_stuffing: [u8; 4], |
| 314 | const SIZE: usize = 4; | ||
| 315 | |||
| 316 | fn try_parse(buf: &[u8]) -> Result<Self, ()> { | ||
| 317 | Self::validate(buf)?; | ||
| 318 | |||
| 319 | Ok(Self) | ||
| 320 | } | ||
| 321 | } | 169 | } |
| 322 | 170 | ||
| 171 | impl ParseableMacEvent for DpsIndication {} | ||
| 172 | |||
| 323 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | 173 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] |
| 324 | #[repr(C, align(8))] | 174 | #[repr(C)] |
| 325 | pub struct DataIndication { | 175 | pub struct DataIndication { |
| 326 | /// Pointer to the set of octets forming the MSDU being indicated | 176 | /// Pointer to the set of octets forming the MSDU being indicated |
| 327 | pub msdu_ptr: *const u8, | 177 | pub msdu_ptr: *const u8, |
| @@ -373,65 +223,7 @@ pub struct DataIndication { | |||
| 373 | pub rssi: u8, | 223 | pub rssi: u8, |
| 374 | } | 224 | } |
| 375 | 225 | ||
| 376 | impl ParseableMacEvent for DataIndication { | 226 | impl ParseableMacEvent for DataIndication {} |
| 377 | const SIZE: usize = 68; | ||
| 378 | |||
| 379 | fn try_parse(buf: &[u8]) -> Result<Self, ()> { | ||
| 380 | Self::validate(buf)?; | ||
| 381 | |||
| 382 | let src_addr_mode = AddressMode::try_from(buf[4])?; | ||
| 383 | let src_address = match src_addr_mode { | ||
| 384 | AddressMode::NoAddress => MacAddress { short: [0, 0] }, | ||
| 385 | AddressMode::Reserved => MacAddress { short: [0, 0] }, | ||
| 386 | AddressMode::Short => MacAddress { | ||
| 387 | short: [buf[7], buf[8]], | ||
| 388 | }, | ||
| 389 | AddressMode::Extended => MacAddress { | ||
| 390 | extended: [buf[7], buf[8], buf[9], buf[10], buf[11], buf[12], buf[13], buf[14]], | ||
| 391 | }, | ||
| 392 | }; | ||
| 393 | |||
| 394 | let dst_addr_mode = AddressMode::try_from(buf[15])?; | ||
| 395 | let dst_address = match dst_addr_mode { | ||
| 396 | AddressMode::NoAddress => MacAddress { short: [0, 0] }, | ||
| 397 | AddressMode::Reserved => MacAddress { short: [0, 0] }, | ||
| 398 | AddressMode::Short => MacAddress { | ||
| 399 | short: [buf[18], buf[19]], | ||
| 400 | }, | ||
| 401 | AddressMode::Extended => MacAddress { | ||
| 402 | extended: [buf[18], buf[19], buf[20], buf[21], buf[22], buf[23], buf[24], buf[25]], | ||
| 403 | }, | ||
| 404 | }; | ||
| 405 | |||
| 406 | Ok(Self { | ||
| 407 | msdu_ptr: u32::from_le_bytes(buf[0..4].try_into().unwrap()) as *const u8, | ||
| 408 | src_addr_mode, | ||
| 409 | src_pan_id: PanId([buf[5], buf[6]]), | ||
| 410 | src_address, | ||
| 411 | dst_addr_mode, | ||
| 412 | dst_pan_id: PanId([buf[16], buf[17]]), | ||
| 413 | dst_address, | ||
| 414 | msdu_length: buf[26], | ||
| 415 | mpdu_link_quality: buf[27], | ||
| 416 | dsn: buf[28], | ||
| 417 | time_stamp: [buf[29], buf[30], buf[31], buf[32]], | ||
| 418 | security_level: SecurityLevel::try_from(buf[33]).unwrap_or(SecurityLevel::Unsecure), // TODO: this is totaly wrong, but I'm too smol brain to fix it | ||
| 419 | key_id_mode: KeyIdMode::try_from(buf[34]).unwrap_or(KeyIdMode::Implicite), // TODO: this is totaly wrong, but I'm too smol brain to fix it | ||
| 420 | key_source: [buf[35], buf[36], buf[37], buf[38], buf[39], buf[40], buf[41], buf[42]], | ||
| 421 | key_index: buf[43], | ||
| 422 | uwbprf: buf[44], | ||
| 423 | uwn_preamble_symbol_repetitions: buf[45], | ||
| 424 | datrate: buf[46], | ||
| 425 | ranging_received: buf[47], | ||
| 426 | ranging_counter_start: u32::from_le_bytes(buf[48..52].try_into().unwrap()), | ||
| 427 | ranging_counter_stop: u32::from_le_bytes(buf[52..56].try_into().unwrap()), | ||
| 428 | ranging_tracking_interval: u32::from_le_bytes(buf[56..60].try_into().unwrap()), | ||
| 429 | ranging_offset: u32::from_le_bytes(buf[60..64].try_into().unwrap()), | ||
| 430 | ranging_fom: buf[65], | ||
| 431 | rssi: buf[66], | ||
| 432 | }) | ||
| 433 | } | ||
| 434 | } | ||
| 435 | 227 | ||
| 436 | /// MLME POLL Indication which will be used for indicating the Data Request | 228 | /// MLME POLL Indication which will be used for indicating the Data Request |
| 437 | /// reception to upper layer as defined in Zigbee r22 - D.8.2 | 229 | /// reception to upper layer as defined in Zigbee r22 - D.8.2 |
| @@ -443,27 +235,4 @@ pub struct PollIndication { | |||
| 443 | pub request_address: MacAddress, | 235 | pub request_address: MacAddress, |
| 444 | } | 236 | } |
| 445 | 237 | ||
| 446 | impl ParseableMacEvent for PollIndication { | 238 | impl ParseableMacEvent for PollIndication {} |
| 447 | const SIZE: usize = 9; | ||
| 448 | |||
| 449 | fn try_parse(buf: &[u8]) -> Result<Self, ()> { | ||
| 450 | Self::validate(buf)?; | ||
| 451 | |||
| 452 | let addr_mode = AddressMode::try_from(buf[0])?; | ||
| 453 | let request_address = match addr_mode { | ||
| 454 | AddressMode::NoAddress => MacAddress { short: [0, 0] }, | ||
| 455 | AddressMode::Reserved => MacAddress { short: [0, 0] }, | ||
| 456 | AddressMode::Short => MacAddress { | ||
| 457 | short: [buf[1], buf[2]], | ||
| 458 | }, | ||
| 459 | AddressMode::Extended => MacAddress { | ||
| 460 | extended: [buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7], buf[8]], | ||
| 461 | }, | ||
| 462 | }; | ||
| 463 | |||
| 464 | Ok(Self { | ||
| 465 | addr_mode, | ||
| 466 | request_address, | ||
| 467 | }) | ||
| 468 | } | ||
| 469 | } | ||
diff --git a/embassy-stm32-wpan/src/mac/responses.rs b/embassy-stm32-wpan/src/mac/responses.rs index d29257f84..e0376a7f5 100644 --- a/embassy-stm32-wpan/src/mac/responses.rs +++ b/embassy-stm32-wpan/src/mac/responses.rs | |||
| @@ -21,24 +21,11 @@ pub struct AssociateConfirm { | |||
| 21 | pub key_id_mode: KeyIdMode, | 21 | pub key_id_mode: KeyIdMode, |
| 22 | /// the index of the key to be used | 22 | /// the index of the key to be used |
| 23 | pub key_index: u8, | 23 | pub key_index: u8, |
| 24 | /// byte stuffing to keep 32 bit alignment | ||
| 25 | a_stuffing: [u8; 2], | ||
| 24 | } | 26 | } |
| 25 | 27 | ||
| 26 | impl ParseableMacEvent for AssociateConfirm { | 28 | impl ParseableMacEvent for AssociateConfirm {} |
| 27 | const SIZE: usize = 16; | ||
| 28 | |||
| 29 | fn try_parse(buf: &[u8]) -> Result<Self, ()> { | ||
| 30 | Self::validate(buf)?; | ||
| 31 | |||
| 32 | Ok(Self { | ||
| 33 | assoc_short_address: [buf[0], buf[1]], | ||
| 34 | status: AssociationStatus::try_from(buf[2])?, | ||
| 35 | security_level: SecurityLevel::try_from(buf[3])?, | ||
| 36 | key_source: [buf[4], buf[5], buf[6], buf[7], buf[8], buf[9], buf[10], buf[11]], | ||
| 37 | key_id_mode: KeyIdMode::try_from(buf[12])?, | ||
| 38 | key_index: buf[13], | ||
| 39 | }) | ||
| 40 | } | ||
| 41 | } | ||
| 42 | 29 | ||
| 43 | /// MLME DISASSOCIATE Confirm used to send disassociation Confirmation to the application. | 30 | /// MLME DISASSOCIATE Confirm used to send disassociation Confirmation to the application. |
| 44 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | 31 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] |
| @@ -53,32 +40,7 @@ pub struct DisassociateConfirm { | |||
| 53 | pub device_address: MacAddress, | 40 | pub device_address: MacAddress, |
| 54 | } | 41 | } |
| 55 | 42 | ||
| 56 | impl ParseableMacEvent for DisassociateConfirm { | 43 | impl ParseableMacEvent for DisassociateConfirm {} |
| 57 | const SIZE: usize = 12; | ||
| 58 | |||
| 59 | fn try_parse(buf: &[u8]) -> Result<Self, ()> { | ||
| 60 | Self::validate(buf)?; | ||
| 61 | |||
| 62 | let device_addr_mode = AddressMode::try_from(buf[1])?; | ||
| 63 | let device_address = match device_addr_mode { | ||
| 64 | AddressMode::NoAddress => MacAddress { short: [0, 0] }, | ||
| 65 | AddressMode::Reserved => MacAddress { short: [0, 0] }, | ||
| 66 | AddressMode::Short => MacAddress { | ||
| 67 | short: [buf[4], buf[5]], | ||
| 68 | }, | ||
| 69 | AddressMode::Extended => MacAddress { | ||
| 70 | extended: [buf[4], buf[5], buf[6], buf[7], buf[8], buf[9], buf[10], buf[11]], | ||
| 71 | }, | ||
| 72 | }; | ||
| 73 | |||
| 74 | Ok(Self { | ||
| 75 | status: MacStatus::try_from(buf[0])?, | ||
| 76 | device_addr_mode, | ||
| 77 | device_pan_id: PanId([buf[2], buf[3]]), | ||
| 78 | device_address, | ||
| 79 | }) | ||
| 80 | } | ||
| 81 | } | ||
| 82 | 44 | ||
| 83 | /// MLME GET Confirm which requests information about a given PIB attribute | 45 | /// MLME GET Confirm which requests information about a given PIB attribute |
| 84 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | 46 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] |
| @@ -91,24 +53,11 @@ pub struct GetConfirm { | |||
| 91 | pub pib_attribute: PibId, | 53 | pub pib_attribute: PibId, |
| 92 | /// The lenght of the PIB attribute Value return | 54 | /// The lenght of the PIB attribute Value return |
| 93 | pub pib_attribute_value_len: u8, | 55 | pub pib_attribute_value_len: u8, |
| 56 | /// byte stuffing to keep 32 bit alignment | ||
| 57 | a_stuffing: [u8; 1], | ||
| 94 | } | 58 | } |
| 95 | 59 | ||
| 96 | impl ParseableMacEvent for GetConfirm { | 60 | impl ParseableMacEvent for GetConfirm {} |
| 97 | const SIZE: usize = 8; | ||
| 98 | |||
| 99 | fn try_parse(buf: &[u8]) -> Result<Self, ()> { | ||
| 100 | Self::validate(buf)?; | ||
| 101 | |||
| 102 | let address = u32::from_le_bytes(buf[0..4].try_into().unwrap()); | ||
| 103 | |||
| 104 | Ok(Self { | ||
| 105 | pib_attribute_value_ptr: address as *const u8, | ||
| 106 | status: MacStatus::try_from(buf[4])?, | ||
| 107 | pib_attribute: PibId::try_from(buf[5])?, | ||
| 108 | pib_attribute_value_len: buf[6], | ||
| 109 | }) | ||
| 110 | } | ||
| 111 | } | ||
| 112 | 61 | ||
| 113 | /// MLME GTS Confirm which eports the results of a request to allocate a new GTS | 62 | /// MLME GTS Confirm which eports the results of a request to allocate a new GTS |
| 114 | /// or to deallocate an existing GTS | 63 | /// or to deallocate an existing GTS |
| @@ -118,39 +67,22 @@ pub struct GtsConfirm { | |||
| 118 | pub gts_characteristics: u8, | 67 | pub gts_characteristics: u8, |
| 119 | /// The status of the GTS reques | 68 | /// The status of the GTS reques |
| 120 | pub status: MacStatus, | 69 | pub status: MacStatus, |
| 70 | /// byte stuffing to keep 32 bit alignment | ||
| 71 | a_stuffing: [u8; 2], | ||
| 121 | } | 72 | } |
| 122 | 73 | ||
| 123 | impl ParseableMacEvent for GtsConfirm { | 74 | impl ParseableMacEvent for GtsConfirm {} |
| 124 | const SIZE: usize = 4; | ||
| 125 | |||
| 126 | fn try_parse(buf: &[u8]) -> Result<Self, ()> { | ||
| 127 | Self::validate(buf)?; | ||
| 128 | |||
| 129 | Ok(Self { | ||
| 130 | gts_characteristics: buf[0], | ||
| 131 | status: MacStatus::try_from(buf[1])?, | ||
| 132 | }) | ||
| 133 | } | ||
| 134 | } | ||
| 135 | 75 | ||
| 136 | /// MLME RESET Confirm which is used to report the results of the reset operation | 76 | /// MLME RESET Confirm which is used to report the results of the reset operation |
| 137 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | 77 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] |
| 138 | pub struct ResetConfirm { | 78 | pub struct ResetConfirm { |
| 139 | /// The result of the reset operation | 79 | /// The result of the reset operation |
| 140 | status: MacStatus, | 80 | status: MacStatus, |
| 81 | /// byte stuffing to keep 32 bit alignment | ||
| 82 | a_stuffing: [u8; 3], | ||
| 141 | } | 83 | } |
| 142 | 84 | ||
| 143 | impl ParseableMacEvent for ResetConfirm { | 85 | impl ParseableMacEvent for ResetConfirm {} |
| 144 | const SIZE: usize = 4; | ||
| 145 | |||
| 146 | fn try_parse(buf: &[u8]) -> Result<Self, ()> { | ||
| 147 | Self::validate(buf)?; | ||
| 148 | |||
| 149 | Ok(Self { | ||
| 150 | status: MacStatus::try_from(buf[0])?, | ||
| 151 | }) | ||
| 152 | } | ||
| 153 | } | ||
| 154 | 86 | ||
| 155 | /// MLME RX ENABLE Confirm which is used to report the results of the attempt | 87 | /// MLME RX ENABLE Confirm which is used to report the results of the attempt |
| 156 | /// to enable or disable the receiver | 88 | /// to enable or disable the receiver |
| @@ -158,19 +90,11 @@ impl ParseableMacEvent for ResetConfirm { | |||
| 158 | pub struct RxEnableConfirm { | 90 | pub struct RxEnableConfirm { |
| 159 | /// Result of the request to enable or disable the receiver | 91 | /// Result of the request to enable or disable the receiver |
| 160 | status: MacStatus, | 92 | status: MacStatus, |
| 93 | /// byte stuffing to keep 32 bit alignment | ||
| 94 | a_stuffing: [u8; 3], | ||
| 161 | } | 95 | } |
| 162 | 96 | ||
| 163 | impl ParseableMacEvent for RxEnableConfirm { | 97 | impl ParseableMacEvent for RxEnableConfirm {} |
| 164 | const SIZE: usize = 4; | ||
| 165 | |||
| 166 | fn try_parse(buf: &[u8]) -> Result<Self, ()> { | ||
| 167 | Self::validate(buf)?; | ||
| 168 | |||
| 169 | Ok(Self { | ||
| 170 | status: MacStatus::try_from(buf[0])?, | ||
| 171 | }) | ||
| 172 | } | ||
| 173 | } | ||
| 174 | 98 | ||
| 175 | /// MLME SCAN Confirm which is used to report the result of the channel scan request | 99 | /// MLME SCAN Confirm which is used to report the result of the channel scan request |
| 176 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | 100 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] |
| @@ -195,42 +119,7 @@ pub struct ScanConfirm { | |||
| 195 | pub uwb_energy_detect_list: [u8; MAX_ED_SCAN_RESULTS_SUPPORTED], | 119 | pub uwb_energy_detect_list: [u8; MAX_ED_SCAN_RESULTS_SUPPORTED], |
| 196 | } | 120 | } |
| 197 | 121 | ||
| 198 | impl ParseableMacEvent for ScanConfirm { | 122 | impl ParseableMacEvent for ScanConfirm {} |
| 199 | const SIZE: usize = 185; | ||
| 200 | |||
| 201 | fn try_parse(buf: &[u8]) -> Result<Self, ()> { | ||
| 202 | // TODO: this is unchecked | ||
| 203 | |||
| 204 | Self::validate(buf)?; | ||
| 205 | |||
| 206 | let mut energy_detect_list = [0; MAX_ED_SCAN_RESULTS_SUPPORTED]; | ||
| 207 | energy_detect_list.copy_from_slice(&buf[8..24]); | ||
| 208 | |||
| 209 | let pan_descriptor_list = [ | ||
| 210 | PanDescriptor::try_from(&buf[24..46])?, | ||
| 211 | PanDescriptor::try_from(&buf[46..68])?, | ||
| 212 | PanDescriptor::try_from(&buf[68..90])?, | ||
| 213 | PanDescriptor::try_from(&buf[90..102])?, | ||
| 214 | PanDescriptor::try_from(&buf[102..124])?, | ||
| 215 | PanDescriptor::try_from(&buf[124..146])?, | ||
| 216 | ]; | ||
| 217 | |||
| 218 | let mut uwb_energy_detect_list = [0; MAX_ED_SCAN_RESULTS_SUPPORTED]; | ||
| 219 | uwb_energy_detect_list.copy_from_slice(&buf[147..163]); | ||
| 220 | |||
| 221 | Ok(Self { | ||
| 222 | status: MacStatus::try_from(buf[0])?, | ||
| 223 | scan_type: ScanType::try_from(buf[1])?, | ||
| 224 | channel_page: buf[2], | ||
| 225 | unscanned_channels: [buf[3], buf[4], buf[5], buf[6]], | ||
| 226 | result_list_size: buf[7], | ||
| 227 | energy_detect_list, | ||
| 228 | pan_descriptor_list, | ||
| 229 | detected_category: buf[146], | ||
| 230 | uwb_energy_detect_list, | ||
| 231 | }) | ||
| 232 | } | ||
| 233 | } | ||
| 234 | 123 | ||
| 235 | /// MLME SET Confirm which reports the result of an attempt to write a value to a PIB attribute | 124 | /// MLME SET Confirm which reports the result of an attempt to write a value to a PIB attribute |
| 236 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | 125 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] |
| @@ -239,20 +128,11 @@ pub struct SetConfirm { | |||
| 239 | pub status: MacStatus, | 128 | pub status: MacStatus, |
| 240 | /// The name of the PIB attribute that was written | 129 | /// The name of the PIB attribute that was written |
| 241 | pub pin_attribute: PibId, | 130 | pub pin_attribute: PibId, |
| 131 | /// byte stuffing to keep 32 bit alignment | ||
| 132 | a_stuffing: [u8; 2], | ||
| 242 | } | 133 | } |
| 243 | 134 | ||
| 244 | impl ParseableMacEvent for SetConfirm { | 135 | impl ParseableMacEvent for SetConfirm {} |
| 245 | const SIZE: usize = 4; | ||
| 246 | |||
| 247 | fn try_parse(buf: &[u8]) -> Result<Self, ()> { | ||
| 248 | Self::validate(buf)?; | ||
| 249 | |||
| 250 | Ok(Self { | ||
| 251 | status: MacStatus::try_from(buf[0])?, | ||
| 252 | pin_attribute: PibId::try_from(buf[1])?, | ||
| 253 | }) | ||
| 254 | } | ||
| 255 | } | ||
| 256 | 136 | ||
| 257 | /// MLME START Confirm which is used to report the results of the attempt to | 137 | /// MLME START Confirm which is used to report the results of the attempt to |
| 258 | /// start using a new superframe configuration | 138 | /// start using a new superframe configuration |
| @@ -260,57 +140,33 @@ impl ParseableMacEvent for SetConfirm { | |||
| 260 | pub struct StartConfirm { | 140 | pub struct StartConfirm { |
| 261 | /// Result of the attempt to start using an updated superframe configuration | 141 | /// Result of the attempt to start using an updated superframe configuration |
| 262 | pub status: MacStatus, | 142 | pub status: MacStatus, |
| 143 | /// byte stuffing to keep 32 bit alignment | ||
| 144 | a_stuffing: [u8; 3], | ||
| 263 | } | 145 | } |
| 264 | 146 | ||
| 265 | impl ParseableMacEvent for StartConfirm { | 147 | impl ParseableMacEvent for StartConfirm {} |
| 266 | const SIZE: usize = 4; | ||
| 267 | |||
| 268 | fn try_parse(buf: &[u8]) -> Result<Self, ()> { | ||
| 269 | Self::validate(buf)?; | ||
| 270 | |||
| 271 | Ok(Self { | ||
| 272 | status: MacStatus::try_from(buf[0])?, | ||
| 273 | }) | ||
| 274 | } | ||
| 275 | } | ||
| 276 | 148 | ||
| 277 | /// MLME POLL Confirm which is used to report the result of a request to poll the coordinator for data | 149 | /// MLME POLL Confirm which is used to report the result of a request to poll the coordinator for data |
| 278 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | 150 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] |
| 279 | pub struct PollConfirm { | 151 | pub struct PollConfirm { |
| 280 | /// The status of the data request | 152 | /// The status of the data request |
| 281 | pub status: MacStatus, | 153 | pub status: MacStatus, |
| 154 | /// byte stuffing to keep 32 bit alignment | ||
| 155 | a_stuffing: [u8; 3], | ||
| 282 | } | 156 | } |
| 283 | 157 | ||
| 284 | impl ParseableMacEvent for PollConfirm { | 158 | impl ParseableMacEvent for PollConfirm {} |
| 285 | const SIZE: usize = 4; | ||
| 286 | |||
| 287 | fn try_parse(buf: &[u8]) -> Result<Self, ()> { | ||
| 288 | Self::validate(buf)?; | ||
| 289 | |||
| 290 | Ok(Self { | ||
| 291 | status: MacStatus::try_from(buf[0])?, | ||
| 292 | }) | ||
| 293 | } | ||
| 294 | } | ||
| 295 | 159 | ||
| 296 | /// MLME DPS Confirm which reports the results of the attempt to enable or disable the DPS | 160 | /// MLME DPS Confirm which reports the results of the attempt to enable or disable the DPS |
| 297 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | 161 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] |
| 298 | pub struct DpsConfirm { | 162 | pub struct DpsConfirm { |
| 299 | /// The status of the DPS request | 163 | /// The status of the DPS request |
| 300 | pub status: MacStatus, | 164 | pub status: MacStatus, |
| 165 | /// byte stuffing to keep 32 bit alignment | ||
| 166 | a_stuffing: [u8; 3], | ||
| 301 | } | 167 | } |
| 302 | 168 | ||
| 303 | impl ParseableMacEvent for DpsConfirm { | 169 | impl ParseableMacEvent for DpsConfirm {} |
| 304 | const SIZE: usize = 4; | ||
| 305 | |||
| 306 | fn try_parse(buf: &[u8]) -> Result<Self, ()> { | ||
| 307 | Self::validate(buf)?; | ||
| 308 | |||
| 309 | Ok(Self { | ||
| 310 | status: MacStatus::try_from(buf[0])?, | ||
| 311 | }) | ||
| 312 | } | ||
| 313 | } | ||
| 314 | 170 | ||
| 315 | /// MLME SOUNDING Confirm which reports the result of a request to the PHY to provide | 171 | /// MLME SOUNDING Confirm which reports the result of a request to the PHY to provide |
| 316 | /// channel sounding information | 172 | /// channel sounding information |
| @@ -318,27 +174,20 @@ impl ParseableMacEvent for DpsConfirm { | |||
| 318 | pub struct SoundingConfirm { | 174 | pub struct SoundingConfirm { |
| 319 | /// Results of the sounding measurement | 175 | /// Results of the sounding measurement |
| 320 | sounding_list: [u8; MAX_SOUNDING_LIST_SUPPORTED], | 176 | sounding_list: [u8; MAX_SOUNDING_LIST_SUPPORTED], |
| 321 | } | ||
| 322 | 177 | ||
| 323 | impl ParseableMacEvent for SoundingConfirm { | 178 | status: u8, |
| 324 | const SIZE: usize = 1; | ||
| 325 | |||
| 326 | fn try_parse(buf: &[u8]) -> Result<Self, ()> { | ||
| 327 | Self::validate(buf)?; | ||
| 328 | |||
| 329 | let mut sounding_list = [0u8; MAX_SOUNDING_LIST_SUPPORTED]; | ||
| 330 | sounding_list[..buf.len()].copy_from_slice(buf); | ||
| 331 | |||
| 332 | Ok(Self { sounding_list }) | ||
| 333 | } | ||
| 334 | } | 179 | } |
| 335 | 180 | ||
| 181 | impl ParseableMacEvent for SoundingConfirm {} | ||
| 182 | |||
| 336 | /// MLME CALIBRATE Confirm which reports the result of a request to the PHY | 183 | /// MLME CALIBRATE Confirm which reports the result of a request to the PHY |
| 337 | /// to provide internal propagation path information | 184 | /// to provide internal propagation path information |
| 338 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | 185 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] |
| 339 | pub struct CalibrateConfirm { | 186 | pub struct CalibrateConfirm { |
| 340 | /// The status of the attempt to return sounding data | 187 | /// The status of the attempt to return sounding data |
| 341 | pub status: MacStatus, | 188 | pub status: MacStatus, |
| 189 | /// byte stuffing to keep 32 bit alignment | ||
| 190 | a_stuffing: [u8; 3], | ||
| 342 | /// A count of the propagation time from the ranging counter | 191 | /// A count of the propagation time from the ranging counter |
| 343 | /// to the transmit antenna | 192 | /// to the transmit antenna |
| 344 | pub cal_tx_rmaker_offset: u32, | 193 | pub cal_tx_rmaker_offset: u32, |
| @@ -347,20 +196,7 @@ pub struct CalibrateConfirm { | |||
| 347 | pub cal_rx_rmaker_offset: u32, | 196 | pub cal_rx_rmaker_offset: u32, |
| 348 | } | 197 | } |
| 349 | 198 | ||
| 350 | impl ParseableMacEvent for CalibrateConfirm { | 199 | impl ParseableMacEvent for CalibrateConfirm {} |
| 351 | const SIZE: usize = 12; | ||
| 352 | |||
| 353 | fn try_parse(buf: &[u8]) -> Result<Self, ()> { | ||
| 354 | Self::validate(buf)?; | ||
| 355 | |||
| 356 | Ok(Self { | ||
| 357 | status: MacStatus::try_from(buf[0])?, | ||
| 358 | // 3 byte stuffing | ||
| 359 | cal_tx_rmaker_offset: u32::from_le_bytes(buf[4..8].try_into().unwrap()), | ||
| 360 | cal_rx_rmaker_offset: u32::from_le_bytes(buf[8..12].try_into().unwrap()), | ||
| 361 | }) | ||
| 362 | } | ||
| 363 | } | ||
| 364 | 200 | ||
| 365 | /// MCPS DATA Confirm which will be used for reporting the results of | 201 | /// MCPS DATA Confirm which will be used for reporting the results of |
| 366 | /// MAC data related requests from the application | 202 | /// MAC data related requests from the application |
| @@ -386,27 +222,11 @@ pub struct DataConfirm { | |||
| 386 | pub ranging_offset: u32, | 222 | pub ranging_offset: u32, |
| 387 | /// The FoM characterizing the ranging measurement | 223 | /// The FoM characterizing the ranging measurement |
| 388 | pub ranging_fom: u8, | 224 | pub ranging_fom: u8, |
| 225 | /// byte stuffing to keep 32 bit alignment | ||
| 226 | a_stuffing: [u8; 3], | ||
| 389 | } | 227 | } |
| 390 | 228 | ||
| 391 | impl ParseableMacEvent for DataConfirm { | 229 | impl ParseableMacEvent for DataConfirm {} |
| 392 | const SIZE: usize = 28; | ||
| 393 | |||
| 394 | fn try_parse(buf: &[u8]) -> Result<Self, ()> { | ||
| 395 | Self::validate(buf)?; | ||
| 396 | |||
| 397 | Ok(Self { | ||
| 398 | msdu_handle: buf[0], | ||
| 399 | time_stamp: [buf[1], buf[2], buf[3], buf[4]], | ||
| 400 | ranging_received: buf[5], | ||
| 401 | status: MacStatus::try_from(buf[6])?, | ||
| 402 | ranging_counter_start: u32::from_le_bytes(buf[7..11].try_into().unwrap()), | ||
| 403 | ranging_counter_stop: u32::from_le_bytes(buf[11..15].try_into().unwrap()), | ||
| 404 | ranging_tracking_interval: u32::from_le_bytes(buf[15..19].try_into().unwrap()), | ||
| 405 | ranging_offset: u32::from_le_bytes(buf[19..23].try_into().unwrap()), | ||
| 406 | ranging_fom: buf[24], | ||
| 407 | }) | ||
| 408 | } | ||
| 409 | } | ||
| 410 | 230 | ||
| 411 | /// MCPS PURGE Confirm which will be used by the MAC to notify the application of | 231 | /// MCPS PURGE Confirm which will be used by the MAC to notify the application of |
| 412 | /// the status of its request to purge an MSDU from the transaction queue | 232 | /// the status of its request to purge an MSDU from the transaction queue |
| @@ -416,17 +236,8 @@ pub struct PurgeConfirm { | |||
| 416 | pub msdu_handle: u8, | 236 | pub msdu_handle: u8, |
| 417 | /// The status of the request | 237 | /// The status of the request |
| 418 | pub status: MacStatus, | 238 | pub status: MacStatus, |
| 239 | /// byte stuffing to keep 32 bit alignment | ||
| 240 | a_stuffing: [u8; 2], | ||
| 419 | } | 241 | } |
| 420 | 242 | ||
| 421 | impl ParseableMacEvent for PurgeConfirm { | 243 | impl ParseableMacEvent for PurgeConfirm {} |
| 422 | const SIZE: usize = 4; | ||
| 423 | |||
| 424 | fn try_parse(buf: &[u8]) -> Result<Self, ()> { | ||
| 425 | Self::validate(buf)?; | ||
| 426 | |||
| 427 | Ok(Self { | ||
| 428 | msdu_handle: buf[0], | ||
| 429 | status: MacStatus::try_from(buf[1])?, | ||
| 430 | }) | ||
| 431 | } | ||
| 432 | } | ||
diff --git a/embassy-stm32-wpan/src/sub/mac.rs b/embassy-stm32-wpan/src/sub/mac.rs index d30ed2f11..d9bf4c909 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; | |||
| 12 | use crate::consts::TlPacketType; | 12 | use crate::consts::TlPacketType; |
| 13 | use crate::evt::{EvtBox, EvtPacket}; | 13 | use crate::evt::{EvtBox, EvtPacket}; |
| 14 | use crate::mac::commands::MacCommand; | 14 | use crate::mac::commands::MacCommand; |
| 15 | use crate::mac::event::MacEvent; | 15 | use crate::mac::event::Event; |
| 16 | use crate::mac::typedefs::MacError; | 16 | use crate::mac::typedefs::MacError; |
| 17 | use crate::tables::{MAC_802_15_4_CMD_BUFFER, MAC_802_15_4_NOTIF_RSP_EVT_BUFFER}; | 17 | use crate::tables::{MAC_802_15_4_CMD_BUFFER, MAC_802_15_4_NOTIF_RSP_EVT_BUFFER}; |
| 18 | use crate::{channels, evt}; | 18 | use crate::{channels, evt}; |
| @@ -94,11 +94,8 @@ impl Mac { | |||
| 94 | } | 94 | } |
| 95 | } | 95 | } |
| 96 | 96 | ||
| 97 | pub async fn read(&self) -> Result<MacEvent, ()> { | 97 | pub async fn read(&self) -> Event { |
| 98 | let evt_box = self.tl_read().await; | 98 | Event::new(self.tl_read().await) |
| 99 | let payload = evt_box.payload(); | ||
| 100 | |||
| 101 | MacEvent::try_from(payload) | ||
| 102 | } | 99 | } |
| 103 | } | 100 | } |
| 104 | 101 | ||
