zigbee_cluster_library/header/
mod.rs

1//! ZCL Header
2#![allow(dead_code, unreachable_pub)]
3
4pub mod command_identifier;
5pub mod frame_control;
6pub mod manufacturer_code;
7
8use core::fmt::Debug;
9
10use command_identifier::CommandIdentifier;
11use frame_control::FrameControl;
12use manufacturer_code::ManufacturerCode;
13use zigbee::internal::macros::impl_byte;
14
15impl_byte! {
16    /// 2.4.1 ZCL Header
17    #[derive(Clone, Copy, Debug, Eq, PartialEq)]
18    pub struct ZclHeader {
19        /// See Section 2.4.1.1.
20        pub frame_control: FrameControl,
21        /// See Section 2.4.1.2.
22        #[parse_if = frame_control.is_manufacturer_specific()]
23        pub manufacturer_code: Option<ManufacturerCode>,
24        /// See Section 2.4.1.3.
25        pub sequence_number: u8,
26        /// See Section 2.4.1.4.
27        pub command_identifier: CommandIdentifier,
28    }
29}
30
31#[cfg(test)]
32mod tests {
33    use byte::TryRead;
34
35    use super::*;
36    use crate::header::frame_control::FrameType;
37
38    #[test]
39    fn unpack_header_without_manufacturer_code() {
40        // given
41        let input = [0x18, 0x01, 0x0a];
42
43        // when
44        let (header, _) =
45            ZclHeader::try_read(&input, ()).expect("Could not read ZclHeader in test");
46
47        // then
48        assert_eq!(header.frame_control.frame_type(), FrameType::GlobalCommand);
49        assert!(!header.frame_control.is_manufacturer_specific());
50        assert_eq!(header.manufacturer_code, None);
51        assert_eq!(header.sequence_number, 1);
52        assert_eq!(
53            header.command_identifier,
54            CommandIdentifier::ReportAttributes
55        );
56    }
57
58    #[test]
59    fn unpack_header_with_manufacturer_code() {
60        // given
61        let input = [0x1c, 0x11, 0x12, 0x02, 0x0a];
62
63        // when
64        let (header, _) =
65            ZclHeader::try_read(&input, ()).expect("Could not read ZclHeader in test");
66
67        // then
68        assert_eq!(header.frame_control.frame_type(), FrameType::GlobalCommand);
69        assert!(header.frame_control.is_manufacturer_specific());
70        assert_eq!(header.manufacturer_code, Some(ManufacturerCode(4625)));
71        assert_eq!(header.sequence_number, 2);
72        assert_eq!(
73            header.command_identifier,
74            CommandIdentifier::ReportAttributes
75        );
76    }
77}