Skip to main content

zigbee_core/nwk/frame/
mod.rs

1//! NWK Frame Formats
2pub 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/// NWK Frame
22#[derive(Debug, Clone)]
23pub enum Frame<'a> {
24    /// Data Frame
25    Data(DataFrame<'a>),
26    /// Command Frame
27    NwkCommand(CommandFrame<'a>),
28    /// Reserved
29    Reserved(Header<'a>),
30    /// Inter-Pan
31    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        // SAFETY: the slice is not used afterwards, so casting away constness
59        // is sound
60        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        // the full frame is always consumed; the "rest" lives in frame.payload
64        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/// NWK Data Frame
76#[derive(Debug, Clone)]
77pub struct DataFrame<'a> {
78    pub header: Header<'a>,
79    pub payload: &'a [u8],
80}
81
82impl<'a> DataFrame<'a> {
83    // SAFETY: caller must ensure the buffer referenced by `self` is mutable
84    // locally
85    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/// NWK Command Frame
91#[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, // frame control
106        0xff, 0xff, // destination,
107        0x34, 0x12, // src
108        0x01, // radius
109        0xaa, // seq number
110        0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, // ext src
111        0x28, //sec header
112        0xff, 0xff, 0xff, 0xff, // frame counter
113        0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, // ext src
114        0x01, // key seq
115        0x01, // command id
116    ];
117
118    #[test]
119    fn command_with_security() {
120        // TODO: parse CMD_FRAME with a real SecurityContext and assert the
121        // decrypted command frame's security flag and aux header
122    }
123}