Skip to main content

zigbee_core/nwk/frame/
header.rs

1//! NWK Frame Header
2use zigbee_macros::impl_byte;
3use zigbee_types::IeeeAddress;
4use zigbee_types::ShortAddress;
5
6use super::frame_control::FrameControl;
7
8impl_byte! {
9    /// 3.3.1 General NPDU Frame Format
10    #[derive(Clone, Debug, Eq, PartialEq)]
11    pub struct Header<'a> {
12        /// See Section 3.3.1.1.
13        pub frame_control: FrameControl,
14        /// See Section 3.3.1.2.
15        pub destination: ShortAddress,
16        /// See Section 3.3.1.3.
17        pub source: ShortAddress,
18        /// See Section 3.3.1.4.
19        pub radius: u8,
20        /// See Section 3.3.1.5.
21        pub sequence_number: u8,
22        /// Set only if [`FrameControl::destination_ieee_flag`] is `true`.
23        /// See Section 3.3.1.6.
24        #[parse_if = frame_control.destination_ieee_flag()]
25        pub destination_ieee: Option<IeeeAddress>,
26        /// Set only if [`FrameControl::source_ieee_flag`] is `true`.
27        /// See Section 3.3.1.7.
28        #[parse_if = frame_control.source_ieee_flag()]
29        pub source_ieee: Option<IeeeAddress>,
30        /// Set only if [`FrameControl::multicast_flag`] is `true`.
31        /// See Section 3.3.1.8.
32        #[parse_if = frame_control.multicast_flag()]
33        pub multicast_control: Option<MulticastControl>,
34        /// Set only if [`FrameControl::source_flag`] is `true`.
35        /// See Section 3.3.1.9.
36        #[parse_if = frame_control.source_flag()]
37        pub source_route_subframe: Option<SourceRouteSubframe<'a>>,
38    }
39}
40
41impl_byte! {
42    /// 3.3.1.8 Multicast Control Field
43    #[derive(Clone, Copy, Debug, Eq, PartialEq)]
44    pub struct MulticastControl(u8);
45}
46
47impl_byte! {
48    /// Source Route Subframe
49    #[derive(Clone, Copy, Debug, Eq, PartialEq)]
50    pub struct SourceRouteSubframe<'a> {
51        /// Indicates the number of relays contained in
52        /// [`SourceRouteSubframe::relay_list`].
53        ///
54        /// See Section 3.3.1.9.1.
55        pub relay_count: u8,
56        /// Indicates the index of the next relay in
57        /// [`SourceRouteSubframe::relay_list`] to which the packet will be
58        /// transmitted.
59        ///
60        /// See Section 3.3.1.9.2.
61        pub relay_index: u8,
62        /// List of relay addresses from closest to the destination to closest to
63        /// the originator.
64        ///
65        /// See Section 3.3.1.9.2.
66        #[ctx = byte::ctx::Bytes::Len(relay_count as usize)]
67        #[ctx_write = ()]
68        pub relay_list: &'a [u8],
69    }
70}
71
72#[cfg(test)]
73mod tests {
74    use byte::TryRead;
75
76    use super::*;
77
78    #[test]
79    fn parse_nwk_header() {
80        let raw = [
81            0x09, 0x12, 0xfc, 0xff, 0x00, 0x00, 0x08, 0xbf, 0x66, 0x71, 0x9a, 0x2a, 0x00, 0x4b,
82            0x12, 0x00,
83        ];
84
85        let (header, _) = Header::try_read(&raw, ()).unwrap();
86
87        assert!(header.frame_control.security_flag());
88        assert!(header.frame_control.source_ieee_flag());
89        assert_eq!(header.destination, ShortAddress(0xfffc));
90        assert_eq!(header.source_ieee, Some(IeeeAddress(0x0012_4b00_2a9a_7166)));
91        assert_eq!(header.radius, 8);
92        assert_eq!(header.sequence_number, 191);
93    }
94}