From 68792bb9188b69c1e7629425a0d39110c602b9b2 Mon Sep 17 00:00:00 2001 From: goueslati Date: Thu, 13 Jul 2023 15:48:15 +0100 Subject: [PATCH] final structs unchecked --- embassy-stm32-wpan/src/sub/mac/indications.rs | 23 ++++++++++++-- embassy-stm32-wpan/src/sub/mac/responses.rs | 31 +++++++++++++++++-- embassy-stm32-wpan/src/sub/mac/typedefs.rs | 17 +++++++++- 3 files changed, 66 insertions(+), 5 deletions(-) diff --git a/embassy-stm32-wpan/src/sub/mac/indications.rs b/embassy-stm32-wpan/src/sub/mac/indications.rs index b67f0a68..6df4aa23 100644 --- a/embassy-stm32-wpan/src/sub/mac/indications.rs +++ b/embassy-stm32-wpan/src/sub/mac/indications.rs @@ -96,12 +96,31 @@ pub struct BeaconNotifyIndication { } impl ParseableMacEvent for BeaconNotifyIndication { - const SIZE: usize = 12; + const SIZE: usize = 88; fn try_parse(buf: &[u8]) -> Result { + // TODO: this is unchecked + Self::validate(buf)?; - todo!() + let addr_list = [ + MacAddress::try_from(&buf[26..34])?, + MacAddress::try_from(&buf[34..42])?, + MacAddress::try_from(&buf[42..50])?, + MacAddress::try_from(&buf[50..58])?, + MacAddress::try_from(&buf[58..66])?, + MacAddress::try_from(&buf[66..74])?, + MacAddress::try_from(&buf[74..82])?, + ]; + + Ok(Self { + sdu_ptr: to_u32(&buf[0..4]) as *const u8, + pan_descriptor: PanDescriptor::try_from(&buf[4..26])?, + addr_list, + bsn: buf[82], + pend_addr_spec: buf[83], + sdu_length: buf[83], + }) } } diff --git a/embassy-stm32-wpan/src/sub/mac/responses.rs b/embassy-stm32-wpan/src/sub/mac/responses.rs index 2b90ccdc..0d3c0986 100644 --- a/embassy-stm32-wpan/src/sub/mac/responses.rs +++ b/embassy-stm32-wpan/src/sub/mac/responses.rs @@ -199,12 +199,39 @@ pub struct ScanConfirm { } impl ParseableMacEvent for ScanConfirm { - const SIZE: usize = 9; + const SIZE: usize = 185; fn try_parse(buf: &[u8]) -> Result { + // TODO: this is unchecked + Self::validate(buf)?; - todo!() + let mut energy_detect_list = [0; MAX_ED_SCAN_RESULTS_SUPPORTED]; + energy_detect_list.copy_from_slice(&buf[8..24]); + + let pan_descriptor_list = [ + PanDescriptor::try_from(&buf[24..46])?, + PanDescriptor::try_from(&buf[46..68])?, + PanDescriptor::try_from(&buf[68..90])?, + PanDescriptor::try_from(&buf[90..102])?, + PanDescriptor::try_from(&buf[102..124])?, + PanDescriptor::try_from(&buf[124..146])?, + ]; + + let mut uwb_energy_detect_list = [0; MAX_ED_SCAN_RESULTS_SUPPORTED]; + uwb_energy_detect_list.copy_from_slice(&buf[147..163]); + + Ok(Self { + status: MacStatus::try_from(buf[0])?, + scan_type: ScanType::try_from(buf[1])?, + channel_page: buf[2], + unscanned_channels: [buf[3], buf[4], buf[5], buf[6]], + result_list_size: buf[7], + energy_detect_list, + pan_descriptor_list, + detected_category: buf[146], + uwb_energy_detect_list, + }) } } diff --git a/embassy-stm32-wpan/src/sub/mac/typedefs.rs b/embassy-stm32-wpan/src/sub/mac/typedefs.rs index 5ff051c9..30c7731b 100644 --- a/embassy-stm32-wpan/src/sub/mac/typedefs.rs +++ b/embassy-stm32-wpan/src/sub/mac/typedefs.rs @@ -135,6 +135,21 @@ impl MacAddress { pub const BROADCAST: Self = Self { short: [0xFF, 0xFF] }; } +impl TryFrom<&[u8]> for MacAddress { + type Error = (); + + fn try_from(buf: &[u8]) -> Result { + const SIZE: usize = 8; + if buf.len() < SIZE { + return Err(()); + } + + Ok(Self { + extended: [buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7]], + }) + } +} + #[cfg_attr(feature = "defmt", derive(defmt::Format))] pub struct GtsCharacteristics { pub fields: u8, @@ -171,7 +186,7 @@ impl TryFrom<&[u8]> for PanDescriptor { type Error = (); fn try_from(buf: &[u8]) -> Result { - const SIZE: usize = 24; + const SIZE: usize = 22; if buf.len() < SIZE { return Err(()); }