2023-06-17 21:38:36 +02:00
|
|
|
use core::{ptr, slice};
|
2023-05-15 11:25:02 +02:00
|
|
|
|
2023-06-17 21:38:36 +02:00
|
|
|
use super::PacketHeader;
|
2023-06-22 16:21:14 +02:00
|
|
|
use crate::consts::TL_EVT_HEADER_SIZE;
|
2023-05-15 11:25:02 +02:00
|
|
|
|
2023-06-08 17:26:47 +02:00
|
|
|
/**
|
|
|
|
* The payload of `Evt` for a command status event
|
|
|
|
*/
|
2023-06-12 13:27:51 +02:00
|
|
|
#[derive(Copy, Clone)]
|
2023-05-02 13:16:48 +02:00
|
|
|
#[repr(C, packed)]
|
|
|
|
pub struct CsEvt {
|
|
|
|
pub status: u8,
|
|
|
|
pub num_cmd: u8,
|
|
|
|
pub cmd_code: u16,
|
|
|
|
}
|
2023-05-15 11:25:02 +02:00
|
|
|
|
2023-06-08 17:26:47 +02:00
|
|
|
/**
|
|
|
|
* The payload of `Evt` for a command complete event
|
|
|
|
*/
|
2023-06-12 13:27:51 +02:00
|
|
|
#[derive(Copy, Clone, Default)]
|
2023-05-15 11:25:02 +02:00
|
|
|
#[repr(C, packed)]
|
|
|
|
pub struct CcEvt {
|
|
|
|
pub num_cmd: u8,
|
2023-06-08 17:26:47 +02:00
|
|
|
pub cmd_code: u16,
|
2023-05-15 11:25:02 +02:00
|
|
|
pub payload: [u8; 1],
|
|
|
|
}
|
|
|
|
|
2023-06-08 17:26:47 +02:00
|
|
|
impl CcEvt {
|
|
|
|
pub fn write(&self, buf: &mut [u8]) {
|
|
|
|
unsafe {
|
|
|
|
let len = core::mem::size_of::<CcEvt>();
|
|
|
|
assert!(buf.len() >= len);
|
|
|
|
|
|
|
|
let self_ptr: *const CcEvt = self;
|
|
|
|
let self_buf_ptr: *const u8 = self_ptr.cast();
|
|
|
|
|
|
|
|
core::ptr::copy(self_buf_ptr, buf.as_mut_ptr(), len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-12 13:27:51 +02:00
|
|
|
#[derive(Copy, Clone, Default)]
|
2023-06-08 17:26:47 +02:00
|
|
|
#[repr(C, packed)]
|
|
|
|
pub struct AsynchEvt {
|
|
|
|
sub_evt_code: u16,
|
|
|
|
payload: [u8; 1],
|
|
|
|
}
|
|
|
|
|
2023-06-20 04:17:31 +02:00
|
|
|
#[derive(Copy, Clone)]
|
2023-05-15 11:25:02 +02:00
|
|
|
#[repr(C, packed)]
|
|
|
|
pub struct Evt {
|
|
|
|
pub evt_code: u8,
|
|
|
|
pub payload_len: u8,
|
2023-06-20 04:17:31 +02:00
|
|
|
pub payload: [u8; 255],
|
2023-05-15 11:25:02 +02:00
|
|
|
}
|
|
|
|
|
2023-06-20 04:17:31 +02:00
|
|
|
#[derive(Copy, Clone)]
|
2023-05-15 11:25:02 +02:00
|
|
|
#[repr(C, packed)]
|
|
|
|
pub struct EvtSerial {
|
|
|
|
pub kind: u8,
|
|
|
|
pub evt: Evt,
|
|
|
|
}
|
|
|
|
|
2023-06-17 21:38:36 +02:00
|
|
|
#[derive(Copy, Clone, Default)]
|
2023-06-18 15:37:26 +02:00
|
|
|
#[repr(C, packed)]
|
2023-06-17 21:38:36 +02:00
|
|
|
pub struct EvtStub {
|
|
|
|
pub kind: u8,
|
|
|
|
pub evt_code: u8,
|
|
|
|
}
|
|
|
|
|
2023-05-15 11:25:02 +02:00
|
|
|
/// This format shall be used for all events (asynchronous and command response) reported
|
|
|
|
/// by the CPU2 except for the command response of a system command where the header is not there
|
|
|
|
/// and the format to be used shall be `EvtSerial`.
|
|
|
|
///
|
|
|
|
/// ### Note:
|
|
|
|
/// Be careful that the asynchronous events reported by the CPU2 on the system channel do
|
|
|
|
/// include the header and shall use `EvtPacket` format. Only the command response format on the
|
|
|
|
/// system channel is different.
|
2023-06-20 04:17:31 +02:00
|
|
|
#[derive(Copy, Clone)]
|
2023-05-15 11:25:02 +02:00
|
|
|
#[repr(C, packed)]
|
|
|
|
pub struct EvtPacket {
|
|
|
|
pub header: PacketHeader,
|
|
|
|
pub evt_serial: EvtSerial,
|
|
|
|
}
|
|
|
|
|
2023-06-08 17:26:47 +02:00
|
|
|
impl EvtPacket {
|
|
|
|
pub fn kind(&self) -> u8 {
|
|
|
|
self.evt_serial.kind
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn evt(&self) -> &Evt {
|
|
|
|
&self.evt_serial.evt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// smart pointer to the [`EvtPacket`] that will dispose of [`EvtPacket`] buffer automatically
|
|
|
|
/// on [`Drop`]
|
|
|
|
#[derive(Debug)]
|
2023-05-15 11:25:02 +02:00
|
|
|
pub struct EvtBox {
|
|
|
|
ptr: *mut EvtPacket,
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe impl Send for EvtBox {}
|
|
|
|
impl EvtBox {
|
|
|
|
pub(super) fn new(ptr: *mut EvtPacket) -> Self {
|
|
|
|
Self { ptr }
|
|
|
|
}
|
|
|
|
|
2023-06-17 21:38:36 +02:00
|
|
|
/// Returns information about the event
|
|
|
|
pub fn stub(&self) -> EvtStub {
|
2023-05-15 11:25:02 +02:00
|
|
|
unsafe {
|
2023-06-17 21:38:36 +02:00
|
|
|
let p_evt_stub = &(*self.ptr).evt_serial as *const _ as *const EvtStub;
|
2023-05-15 11:25:02 +02:00
|
|
|
|
2023-06-17 21:38:36 +02:00
|
|
|
ptr::read_volatile(p_evt_stub)
|
2023-05-15 11:25:02 +02:00
|
|
|
}
|
|
|
|
}
|
2023-06-08 17:26:47 +02:00
|
|
|
|
2023-06-18 15:37:26 +02:00
|
|
|
pub fn payload<'a>(&'a self) -> &'a [u8] {
|
2023-06-08 17:26:47 +02:00
|
|
|
unsafe {
|
2023-06-17 21:38:36 +02:00
|
|
|
let p_payload_len = &(*self.ptr).evt_serial.evt.payload_len as *const u8;
|
|
|
|
let p_payload = &(*self.ptr).evt_serial.evt.payload as *const u8;
|
2023-06-08 17:26:47 +02:00
|
|
|
|
2023-06-17 21:38:36 +02:00
|
|
|
let payload_len = ptr::read_volatile(p_payload_len);
|
2023-06-08 17:26:47 +02:00
|
|
|
|
2023-06-17 21:38:36 +02:00
|
|
|
slice::from_raw_parts(p_payload, payload_len as usize)
|
2023-06-08 17:26:47 +02:00
|
|
|
}
|
|
|
|
}
|
2023-06-22 16:21:14 +02:00
|
|
|
|
|
|
|
/// writes an underlying [`EvtPacket`] into the provided buffer.
|
|
|
|
/// Returns the number of bytes that were written.
|
|
|
|
/// Returns an error if event kind is unknown or if provided buffer size is not enough.
|
2023-06-22 16:59:03 +02:00
|
|
|
pub fn serial<'a>(&'a self) -> &'a [u8] {
|
2023-06-22 16:21:14 +02:00
|
|
|
unsafe {
|
|
|
|
let evt_serial: *const EvtSerial = &(*self.ptr).evt_serial;
|
|
|
|
let evt_serial_buf: *const u8 = evt_serial.cast();
|
|
|
|
|
|
|
|
let len = (*evt_serial).evt.payload_len as usize + TL_EVT_HEADER_SIZE;
|
|
|
|
|
|
|
|
slice::from_raw_parts(evt_serial_buf, len)
|
|
|
|
}
|
|
|
|
}
|
2023-05-15 11:25:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for EvtBox {
|
|
|
|
fn drop(&mut self) {
|
2023-06-19 01:51:14 +02:00
|
|
|
#[cfg(feature = "ble")]
|
|
|
|
unsafe {
|
2023-06-24 00:54:06 +02:00
|
|
|
use crate::sub::mm;
|
2023-06-19 01:51:14 +02:00
|
|
|
|
|
|
|
mm::MemoryManager::drop_event_packet(self.ptr)
|
|
|
|
};
|
|
|
|
|
|
|
|
#[cfg(feature = "mac")]
|
|
|
|
unsafe {
|
2023-06-24 00:54:06 +02:00
|
|
|
use crate::sub::mac;
|
2023-06-19 01:51:14 +02:00
|
|
|
|
|
|
|
mac::Mac::drop_event_packet(self.ptr)
|
|
|
|
}
|
2023-05-15 11:25:02 +02:00
|
|
|
}
|
|
|
|
}
|