Skip to content

Commit aad02db

Browse files
authored
Merge pull request #4906 from xoviat/wpan
wpan: fix src/dst address
2 parents 45797ec + 50501e0 commit aad02db

File tree

3 files changed

+36
-8
lines changed

3 files changed

+36
-8
lines changed

embassy-stm32-wpan/src/mac/driver.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use embassy_sync::mutex::Mutex;
1111
use embassy_sync::waitqueue::AtomicWaker;
1212

1313
use crate::mac::event::MacEvent;
14-
use crate::mac::indications::write_frame_from_data_indication;
14+
use crate::mac::indications::{write_frame_from_beacon_indication, write_frame_from_data_indication};
1515
use crate::mac::runner::{BUF_SIZE, ZeroCopyPubSub};
1616
use crate::mac::{Control, MTU, Runner};
1717
use crate::sub::mac::{Mac, MacRx, MacTx};
@@ -183,6 +183,7 @@ impl<'d> embassy_net_driver::RxToken for RxToken<'d> {
183183
let mut buffer = [0u8; MTU];
184184
match self.rx.try_receive().unwrap() {
185185
MacEvent::McpsDataInd(data_event) => write_frame_from_data_indication(data_event, &mut buffer),
186+
MacEvent::MlmeBeaconNotifyInd(data_event) => write_frame_from_beacon_indication(data_event, &mut buffer),
186187
_ => {}
187188
};
188189

embassy-stm32-wpan/src/mac/indications.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use super::typedefs::{
99
AddressMode, Capabilities, DisassociationReason, KeyIdMode, MacAddress, MacChannel, MacStatus, PanDescriptor,
1010
PanId, SecurityLevel,
1111
};
12+
use crate::mac::typedefs::MacAddressAndMode;
1213

1314
/// MLME ASSOCIATE Indication which will be used by the MAC
1415
/// to indicate the reception of an association request command
@@ -77,6 +78,22 @@ pub struct BeaconNotifyIndication {
7778

7879
impl ParseableMacEvent for BeaconNotifyIndication {}
7980

81+
impl BeaconNotifyIndication {
82+
pub fn payload<'a>(&'a self) -> &'a mut [u8] {
83+
unsafe { slice::from_raw_parts_mut(self.sdu_ptr as *mut _, self.sdu_length as usize) }
84+
}
85+
}
86+
87+
pub fn write_frame_from_beacon_indication<'a, T: AsRef<[u8]> + AsMut<[u8]>>(
88+
data: &'a BeaconNotifyIndication,
89+
buffer: &'a mut T,
90+
) {
91+
let mut frame = Frame::new_unchecked(buffer);
92+
93+
frame.set_frame_type(Ieee802154FrameType::Beacon);
94+
frame.set_sequence_number(data.bsn);
95+
}
96+
8097
/// MLME COMM STATUS Indication which is used by the MAC to indicate a communications status
8198
#[repr(C)]
8299
#[derive(Debug)]
@@ -256,13 +273,13 @@ impl DataIndication {
256273
pub fn write_frame_from_data_indication<'a, T: AsRef<[u8]> + AsMut<[u8]>>(data: &'a DataIndication, buffer: &'a mut T) {
257274
let mut frame = Frame::new_unchecked(buffer);
258275

259-
// TODO: complete frame creation
260276
frame.set_frame_type(Ieee802154FrameType::Data);
261-
frame.set_dst_addr(data.dst_address.into());
262-
frame.set_src_addr(data.src_address.into());
277+
frame.set_src_addr(MacAddressAndMode(data.src_address, data.src_addr_mode).into());
278+
frame.set_dst_addr(MacAddressAndMode(data.dst_address, data.dst_addr_mode).into());
263279
frame.set_dst_pan_id(data.dst_pan_id.into());
264280
frame.set_src_pan_id(data.src_pan_id.into());
265281
frame.set_sequence_number(data.dsn);
282+
frame.set_security_enabled(data.security_level == SecurityLevel::Secured);
266283

267284
// No way around the copy with the current API
268285
frame.payload_mut().unwrap().copy_from_slice(data.payload());

embassy-stm32-wpan/src/mac/typedefs.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,19 @@ impl From<Address> for MacAddress {
140140
}
141141
}
142142

143-
impl From<MacAddress> for Address {
144-
fn from(_value: MacAddress) -> Self {
145-
todo!()
143+
pub struct MacAddressAndMode(pub MacAddress, pub AddressMode);
144+
145+
impl From<MacAddressAndMode> for Address {
146+
fn from(mac_address_and_mode: MacAddressAndMode) -> Self {
147+
let address = mac_address_and_mode.0;
148+
let mode = mac_address_and_mode.1;
149+
150+
match mode {
151+
AddressMode::Short => Address::Short(unsafe { address.short }),
152+
AddressMode::Extended => Address::Extended(unsafe { address.extended }),
153+
AddressMode::NoAddress => Address::Absent,
154+
AddressMode::Reserved => Address::Absent,
155+
}
146156
}
147157
}
148158

@@ -377,7 +387,7 @@ numeric_enum! {
377387

378388
numeric_enum! {
379389
#[repr(u8)]
380-
#[derive(Default, Clone, Copy, Debug)]
390+
#[derive(Default, Clone, Copy, Debug, PartialEq)]
381391
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
382392
pub enum SecurityLevel {
383393
/// MAC Unsecured Mode Security

0 commit comments

Comments
 (0)