diff options
| author | xoviat <[email protected]> | 2023-06-17 14:38:36 -0500 |
|---|---|---|
| committer | xoviat <[email protected]> | 2023-06-17 14:38:36 -0500 |
| commit | c7b0df569b02bee80de808a5b0ad69df3d32d84c (patch) | |
| tree | 2599747a66f1e50c594c2ec22a425fefb84f115b /embassy-stm32-wpan/src/cmd.rs | |
| parent | 041a4a4208dae563ab22f9986391509f4b90f740 (diff) | |
stm32/wpan: modify evtbox to use slice view
Diffstat (limited to 'embassy-stm32-wpan/src/cmd.rs')
| -rw-r--r-- | embassy-stm32-wpan/src/cmd.rs | 54 |
1 files changed, 27 insertions, 27 deletions
diff --git a/embassy-stm32-wpan/src/cmd.rs b/embassy-stm32-wpan/src/cmd.rs index 581e5019b..edca82390 100644 --- a/embassy-stm32-wpan/src/cmd.rs +++ b/embassy-stm32-wpan/src/cmd.rs | |||
| @@ -1,8 +1,7 @@ | |||
| 1 | use core::ptr; | 1 | use core::ptr; |
| 2 | 2 | ||
| 3 | use crate::consts::TlPacketType; | 3 | use crate::consts::TlPacketType; |
| 4 | use crate::evt::{EvtPacket, EvtSerial}; | 4 | use crate::PacketHeader; |
| 5 | use crate::{PacketHeader, TL_EVT_HEADER_SIZE}; | ||
| 6 | 5 | ||
| 7 | #[derive(Copy, Clone)] | 6 | #[derive(Copy, Clone)] |
| 8 | #[repr(C, packed)] | 7 | #[repr(C, packed)] |
| @@ -60,31 +59,6 @@ impl CmdPacket { | |||
| 60 | 59 | ||
| 61 | ptr::copy_nonoverlapping(payload as *const _ as *const u8, p_payload, payload.len()); | 60 | ptr::copy_nonoverlapping(payload as *const _ as *const u8, p_payload, payload.len()); |
| 62 | } | 61 | } |
| 63 | |||
| 64 | /// Writes an underlying CmdPacket into the provided buffer. | ||
| 65 | /// Returns a number of bytes that were written. | ||
| 66 | /// Returns an error if event kind is unknown or if provided buffer size is not enough. | ||
| 67 | #[allow(clippy::result_unit_err)] | ||
| 68 | pub fn write(&self, buf: &mut [u8]) -> Result<usize, ()> { | ||
| 69 | unsafe { | ||
| 70 | let cmd_ptr: *const CmdPacket = self; | ||
| 71 | let self_as_evt_ptr: *const EvtPacket = cmd_ptr.cast(); | ||
| 72 | let evt_serial: *const EvtSerial = &(*self_as_evt_ptr).evt_serial; | ||
| 73 | |||
| 74 | let acl_data: *const AclDataPacket = cmd_ptr.cast(); | ||
| 75 | let acl_serial: *const AclDataSerial = &(*acl_data).acl_data_serial; | ||
| 76 | let acl_serial_buf: *const u8 = acl_serial.cast(); | ||
| 77 | |||
| 78 | let len = (*evt_serial).evt.payload_len as usize + TL_EVT_HEADER_SIZE; | ||
| 79 | if len > buf.len() { | ||
| 80 | return Err(()); | ||
| 81 | } | ||
| 82 | |||
| 83 | core::ptr::copy(acl_serial_buf, buf.as_mut_ptr(), len); | ||
| 84 | |||
| 85 | Ok(len) | ||
| 86 | } | ||
| 87 | } | ||
| 88 | } | 62 | } |
| 89 | 63 | ||
| 90 | #[derive(Copy, Clone)] | 64 | #[derive(Copy, Clone)] |
| @@ -98,7 +72,33 @@ pub struct AclDataSerial { | |||
| 98 | 72 | ||
| 99 | #[derive(Copy, Clone)] | 73 | #[derive(Copy, Clone)] |
| 100 | #[repr(C, packed)] | 74 | #[repr(C, packed)] |
| 75 | pub struct AclDataSerialStub { | ||
| 76 | pub ty: u8, | ||
| 77 | pub handle: u16, | ||
| 78 | pub length: u16, | ||
| 79 | } | ||
| 80 | |||
| 81 | #[derive(Copy, Clone)] | ||
| 82 | #[repr(C, packed)] | ||
| 101 | pub struct AclDataPacket { | 83 | pub struct AclDataPacket { |
| 102 | pub header: PacketHeader, | 84 | pub header: PacketHeader, |
| 103 | pub acl_data_serial: AclDataSerial, | 85 | pub acl_data_serial: AclDataSerial, |
| 104 | } | 86 | } |
| 87 | |||
| 88 | impl AclDataPacket { | ||
| 89 | pub unsafe fn write_into(cmd_buf: *mut AclDataPacket, packet_type: TlPacketType, handle: u16, payload: &[u8]) { | ||
| 90 | let p_cmd_serial = &mut (*cmd_buf).acl_data_serial as *mut _ as *mut AclDataSerialStub; | ||
| 91 | let p_payload = &mut (*cmd_buf).acl_data_serial.acl_data as *mut _; | ||
| 92 | |||
| 93 | ptr::write_volatile( | ||
| 94 | p_cmd_serial, | ||
| 95 | AclDataSerialStub { | ||
| 96 | ty: packet_type as u8, | ||
| 97 | handle: handle, | ||
| 98 | length: payload.len() as u16, | ||
| 99 | }, | ||
| 100 | ); | ||
| 101 | |||
| 102 | ptr::copy_nonoverlapping(payload as *const _ as *const u8, p_payload, payload.len()); | ||
| 103 | } | ||
| 104 | } | ||
