aboutsummaryrefslogtreecommitdiff
path: root/embassy-stm32-wpan/src/wb55/cmd.rs
diff options
context:
space:
mode:
Diffstat (limited to 'embassy-stm32-wpan/src/wb55/cmd.rs')
-rw-r--r--embassy-stm32-wpan/src/wb55/cmd.rs109
1 files changed, 109 insertions, 0 deletions
diff --git a/embassy-stm32-wpan/src/wb55/cmd.rs b/embassy-stm32-wpan/src/wb55/cmd.rs
new file mode 100644
index 000000000..34f02d6e7
--- /dev/null
+++ b/embassy-stm32-wpan/src/wb55/cmd.rs
@@ -0,0 +1,109 @@
1use core::ptr;
2use core::sync::atomic::{Ordering, compiler_fence};
3
4use crate::consts::TlPacketType;
5use crate::wb55::PacketHeader;
6
7#[derive(Copy, Clone)]
8#[repr(C, packed)]
9pub struct Cmd {
10 pub cmd_code: u16,
11 pub payload_len: u8,
12 pub payload: [u8; 255],
13}
14
15impl Default for Cmd {
16 fn default() -> Self {
17 Self {
18 cmd_code: 0,
19 payload_len: 0,
20 payload: [0u8; 255],
21 }
22 }
23}
24
25#[derive(Copy, Clone, Default)]
26#[repr(C, packed)]
27pub struct CmdSerial {
28 pub ty: u8,
29 pub cmd: Cmd,
30}
31
32#[derive(Copy, Clone, Default)]
33#[repr(C, packed)]
34pub struct CmdSerialStub {
35 pub ty: u8,
36 pub cmd_code: u16,
37 pub payload_len: u8,
38}
39
40#[derive(Copy, Clone, Default)]
41#[repr(C, packed)]
42pub struct CmdPacket {
43 pub header: PacketHeader,
44 pub cmdserial: CmdSerial,
45}
46
47impl CmdPacket {
48 pub unsafe fn write_into(cmd_buf: *mut CmdPacket, packet_type: TlPacketType, cmd_code: u16, payload: &[u8]) {
49 let p_cmd_serial = (cmd_buf as *mut u8).add(size_of::<PacketHeader>());
50 let p_payload = p_cmd_serial.add(size_of::<CmdSerialStub>());
51
52 ptr::write_unaligned(
53 p_cmd_serial as *mut _,
54 CmdSerialStub {
55 ty: packet_type as u8,
56 cmd_code,
57 payload_len: payload.len() as u8,
58 },
59 );
60
61 ptr::copy_nonoverlapping(payload as *const _ as *const u8, p_payload, payload.len());
62
63 compiler_fence(Ordering::Release);
64 }
65}
66
67#[derive(Copy, Clone)]
68#[repr(C, packed)]
69pub struct AclDataSerial {
70 pub ty: u8,
71 pub handle: u16,
72 pub length: u16,
73 pub acl_data: [u8; 1],
74}
75
76#[derive(Copy, Clone)]
77#[repr(C, packed)]
78pub struct AclDataSerialStub {
79 pub ty: u8,
80 pub handle: u16,
81 pub length: u16,
82}
83
84#[derive(Copy, Clone)]
85#[repr(C, packed)]
86pub struct AclDataPacket {
87 pub header: PacketHeader,
88 pub acl_data_serial: AclDataSerial,
89}
90
91impl AclDataPacket {
92 pub unsafe fn write_into(cmd_buf: *mut AclDataPacket, packet_type: TlPacketType, handle: u16, payload: &[u8]) {
93 let p_cmd_serial = (cmd_buf as *mut u8).add(size_of::<PacketHeader>());
94 let p_payload = p_cmd_serial.add(size_of::<AclDataSerialStub>());
95
96 ptr::write_unaligned(
97 p_cmd_serial as *mut _,
98 AclDataSerialStub {
99 ty: packet_type as u8,
100 handle: handle,
101 length: payload.len() as u16,
102 },
103 );
104
105 ptr::copy_nonoverlapping(payload as *const _ as *const u8, p_payload, payload.len());
106
107 compiler_fence(Ordering::Release);
108 }
109}