aboutsummaryrefslogtreecommitdiff
path: root/embassy-stm32-wpan/src/cmd.rs
diff options
context:
space:
mode:
authorgoueslati <[email protected]>2023-06-12 12:27:51 +0100
committergoueslati <[email protected]>2023-06-12 12:27:51 +0100
commitca8957da435eb91242fa33eb986e80a33bbc4da0 (patch)
tree89b66bdff52630cd4ef1d609122c2b04417ccd73 /embassy-stm32-wpan/src/cmd.rs
parentce1d72c609ae1e04410e68458ec3d6c36c7dae27 (diff)
stm32/ipcc: move tl_mbox into `embassy-stm32-wpan`
Diffstat (limited to 'embassy-stm32-wpan/src/cmd.rs')
-rw-r--r--embassy-stm32-wpan/src/cmd.rs77
1 files changed, 77 insertions, 0 deletions
diff --git a/embassy-stm32-wpan/src/cmd.rs b/embassy-stm32-wpan/src/cmd.rs
new file mode 100644
index 000000000..1f7dae7f7
--- /dev/null
+++ b/embassy-stm32-wpan/src/cmd.rs
@@ -0,0 +1,77 @@
1use crate::evt::{EvtPacket, EvtSerial};
2use crate::{PacketHeader, TL_EVT_HEADER_SIZE};
3
4#[derive(Copy, Clone)]
5#[repr(C, packed)]
6pub struct Cmd {
7 pub cmd_code: u16,
8 pub payload_len: u8,
9 pub payload: [u8; 255],
10}
11
12impl Default for Cmd {
13 fn default() -> Self {
14 Self {
15 cmd_code: 0,
16 payload_len: 0,
17 payload: [0u8; 255],
18 }
19 }
20}
21
22#[derive(Copy, Clone, Default)]
23#[repr(C, packed)]
24pub struct CmdSerial {
25 pub ty: u8,
26 pub cmd: Cmd,
27}
28
29#[derive(Copy, Clone, Default)]
30#[repr(C, packed)]
31pub struct CmdPacket {
32 pub header: PacketHeader,
33 pub cmdserial: CmdSerial,
34}
35
36impl CmdPacket {
37 /// Writes an underlying CmdPacket into the provided buffer.
38 /// Returns a number of bytes that were written.
39 /// Returns an error if event kind is unknown or if provided buffer size is not enough.
40 #[allow(clippy::result_unit_err)]
41 pub fn write(&self, buf: &mut [u8]) -> Result<usize, ()> {
42 unsafe {
43 let cmd_ptr: *const CmdPacket = self;
44 let self_as_evt_ptr: *const EvtPacket = cmd_ptr.cast();
45 let evt_serial: *const EvtSerial = &(*self_as_evt_ptr).evt_serial;
46
47 let acl_data: *const AclDataPacket = cmd_ptr.cast();
48 let acl_serial: *const AclDataSerial = &(*acl_data).acl_data_serial;
49 let acl_serial_buf: *const u8 = acl_serial.cast();
50
51 let len = (*evt_serial).evt.payload_len as usize + TL_EVT_HEADER_SIZE;
52 if len > buf.len() {
53 return Err(());
54 }
55
56 core::ptr::copy(acl_serial_buf, buf.as_mut_ptr(), len);
57
58 Ok(len)
59 }
60 }
61}
62
63#[derive(Copy, Clone)]
64#[repr(C, packed)]
65pub struct AclDataSerial {
66 pub ty: u8,
67 pub handle: u16,
68 pub length: u16,
69 pub acl_data: [u8; 1],
70}
71
72#[derive(Copy, Clone)]
73#[repr(C, packed)]
74pub struct AclDataPacket {
75 pub header: PacketHeader,
76 pub acl_data_serial: AclDataSerial,
77}