zigbee_core/nwk/frame/
mod.rs1pub mod command;
3pub mod frame_control;
4pub mod header;
5
6use core::mem;
7use core::slice;
8
9use byte::BytesExt;
10use byte::TryRead;
11use byte::TryWrite;
12use byte::ctx;
13use frame_control::FrameType;
14use header::Header;
15use zigbee_macros::impl_byte;
16
17use crate::nwk::frame::command::Command;
18use crate::security::SecurityContext;
19use crate::security::frame::AuxFrameHeader;
20
21#[derive(Debug, Clone)]
23pub enum Frame<'a> {
24 Data(DataFrame<'a>),
26 NwkCommand(CommandFrame<'a>),
28 Reserved(Header<'a>),
30 InterPan(Header<'a>),
32}
33
34impl<'a> Frame<'a> {
35 pub fn from_payload(header: Header<'a>, payload: &'a [u8]) -> byte::Result<Self> {
36 match header.frame_control.frame_type() {
37 FrameType::Data => {
38 let data_frame = DataFrame { header, payload };
39 Ok(Frame::Data(data_frame))
40 }
41 FrameType::NwkCommand => {
42 let command_frame = CommandFrame {
43 header,
44 command: payload.read_with(&mut 0, ())?,
45 };
46 Ok(Frame::NwkCommand(command_frame))
47 }
48 FrameType::Reserved => Ok(Frame::Reserved(header)),
49 FrameType::InterPan => Ok(Frame::InterPan(header)),
50 }
51 }
52}
53
54impl<'a> TryRead<'a, SecurityContext<'a>> for Frame<'a> {
55 #[allow(clippy::ptr_cast_constness, clippy::as_ptr_cast_mut)]
56 fn try_read(bytes: &'a [u8], cx: SecurityContext) -> byte::Result<(Self, usize)> {
57 let len = bytes.len();
58 let bytes: &'a mut [u8] =
61 unsafe { slice::from_raw_parts_mut(bytes.as_ptr() as *mut u8, len) };
62 let frame = cx.decrypt_nwk_frame_in_place(bytes)?;
63 Ok((frame, len))
65 }
66}
67
68impl<'a> TryWrite<SecurityContext<'a>> for Frame<'a> {
69 fn try_write(self, bytes: &mut [u8], cx: SecurityContext) -> byte::Result<usize> {
70 let len = cx.encrypt_nwk_frame_in_place(self, bytes)?;
71 Ok(len)
72 }
73}
74
75#[derive(Debug, Clone)]
77pub struct DataFrame<'a> {
78 pub header: Header<'a>,
79 pub payload: &'a [u8],
80}
81
82impl<'a> DataFrame<'a> {
83 pub(crate) unsafe fn payload_as_mut(&mut self) -> &'a mut [u8] {
86 unsafe { slice::from_raw_parts_mut(self.payload.as_ptr().cast_mut(), self.payload.len()) }
87 }
88}
89
90#[derive(Debug, Clone)]
92pub struct CommandFrame<'a> {
93 pub header: Header<'a>,
94 pub command: Command<'a>,
95}
96
97#[cfg(test)]
98mod tests {
99 use byte::TryRead;
100
101 use super::*;
102 use crate::security::SecurityContext;
103
104 const CMD_FRAME: &[u8] = &[
105 0x09, 0x12, 0xff, 0xff, 0x34, 0x12, 0x01, 0xaa, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0x28, 0xff, 0xff, 0xff, 0xff, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0x01, 0x01, ];
117
118 #[test]
119 fn command_with_security() {
120 }
123}