stm32/wpan: modify evtbox to use slice view

This commit is contained in:
xoviat
2023-06-17 14:38:36 -05:00
parent 041a4a4208
commit c7b0df569b
4 changed files with 112 additions and 95 deletions

View File

@@ -1,8 +1,7 @@
use core::ptr;
use crate::consts::TlPacketType;
use crate::evt::{EvtPacket, EvtSerial};
use crate::{PacketHeader, TL_EVT_HEADER_SIZE};
use crate::PacketHeader;
#[derive(Copy, Clone)]
#[repr(C, packed)]
@@ -60,31 +59,6 @@ impl CmdPacket {
ptr::copy_nonoverlapping(payload as *const _ as *const u8, p_payload, payload.len());
}
/// Writes an underlying CmdPacket into the provided buffer.
/// Returns a number of bytes that were written.
/// Returns an error if event kind is unknown or if provided buffer size is not enough.
#[allow(clippy::result_unit_err)]
pub fn write(&self, buf: &mut [u8]) -> Result<usize, ()> {
unsafe {
let cmd_ptr: *const CmdPacket = self;
let self_as_evt_ptr: *const EvtPacket = cmd_ptr.cast();
let evt_serial: *const EvtSerial = &(*self_as_evt_ptr).evt_serial;
let acl_data: *const AclDataPacket = cmd_ptr.cast();
let acl_serial: *const AclDataSerial = &(*acl_data).acl_data_serial;
let acl_serial_buf: *const u8 = acl_serial.cast();
let len = (*evt_serial).evt.payload_len as usize + TL_EVT_HEADER_SIZE;
if len > buf.len() {
return Err(());
}
core::ptr::copy(acl_serial_buf, buf.as_mut_ptr(), len);
Ok(len)
}
}
}
#[derive(Copy, Clone)]
@@ -96,9 +70,35 @@ pub struct AclDataSerial {
pub acl_data: [u8; 1],
}
#[derive(Copy, Clone)]
#[repr(C, packed)]
pub struct AclDataSerialStub {
pub ty: u8,
pub handle: u16,
pub length: u16,
}
#[derive(Copy, Clone)]
#[repr(C, packed)]
pub struct AclDataPacket {
pub header: PacketHeader,
pub acl_data_serial: AclDataSerial,
}
impl AclDataPacket {
pub unsafe fn write_into(cmd_buf: *mut AclDataPacket, packet_type: TlPacketType, handle: u16, payload: &[u8]) {
let p_cmd_serial = &mut (*cmd_buf).acl_data_serial as *mut _ as *mut AclDataSerialStub;
let p_payload = &mut (*cmd_buf).acl_data_serial.acl_data as *mut _;
ptr::write_volatile(
p_cmd_serial,
AclDataSerialStub {
ty: packet_type as u8,
handle: handle,
length: payload.len() as u16,
},
);
ptr::copy_nonoverlapping(payload as *const _ as *const u8, p_payload, payload.len());
}
}