zigbee_cluster_library/header/
command_identifier.rs

1//! Command Identifier.
2use byte::{TryRead, TryWrite};
3
4/// Command Identifier.
5///
6/// See Section 2.4.1.4 (Values can be found in Table 2-3)
7#[allow(missing_docs)]
8#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9#[repr(u8)]
10pub enum CommandIdentifier {
11    ReadAttributes = 0x00,
12    ReadAttributesResponse = 0x01,
13    WriteAttributes = 0x02,
14    WriteAttributesUndivided = 0x03,
15    WriteAttributesResponse = 0x04,
16    WriteAttributesNoResponse = 0x05,
17    ConfigureReporting = 0x06,
18    ConfigureReportingResponse = 0x07,
19    ReadReportingConfiguration = 0x08,
20    ReadReportingConfigurationResponse = 0x09,
21    ReportAttributes = 0x0a,
22    DefaultResponse = 0x0b,
23    DiscoverAttributes = 0x0c,
24    DiscoverAttributesResponse = 0x0d,
25    ReadAttributesStructured = 0x0e,
26    WriteAttributesStructured = 0x0f,
27    WriteAttributesStructuredResponse = 0x10,
28    DiscoverCommandsReceived = 0x11,
29    DiscoverCommandsReceivedResponse = 0x12,
30    DiscoverCommandsGenerated = 0x13,
31    DiscoverCommandsGeneratedResponse = 0x14,
32    DiscoverAttributesExtended = 0x15,
33    DiscoverAttributesExtendedResponse = 0x16,
34    Reserved = 0xff,
35}
36
37impl CommandIdentifier {
38    fn from_bits(b: u8) -> Self {
39        match b {
40            0x00 => Self::ReadAttributes,
41            0x01 => Self::ReadAttributesResponse,
42            0x02 => Self::WriteAttributes,
43            0x03 => Self::WriteAttributesUndivided,
44            0x04 => Self::WriteAttributesResponse,
45            0x05 => Self::WriteAttributesNoResponse,
46            0x06 => Self::ConfigureReporting,
47            0x07 => Self::ConfigureReportingResponse,
48            0x08 => Self::ReadReportingConfiguration,
49            0x09 => Self::ReadReportingConfigurationResponse,
50            0x0a => Self::ReportAttributes,
51            0x0b => Self::DefaultResponse,
52            0x0c => Self::DiscoverAttributes,
53            0x0d => Self::DiscoverAttributesResponse,
54            0x0e => Self::ReadAttributesStructured,
55            0x0f => Self::WriteAttributesStructured,
56            0x10 => Self::WriteAttributesStructuredResponse,
57            0x11 => Self::DiscoverCommandsReceived,
58            0x12 => Self::DiscoverCommandsReceivedResponse,
59            0x13 => Self::DiscoverCommandsGenerated,
60            0x14 => Self::DiscoverCommandsGeneratedResponse,
61            0x15 => Self::DiscoverAttributesExtended,
62            0x16 => Self::DiscoverAttributesExtendedResponse,
63            _ => Self::Reserved,
64        }
65    }
66}
67
68impl TryRead<'_, byte::ctx::Endian> for CommandIdentifier {
69    fn try_read(bytes: &[u8], ctx: byte::ctx::Endian) -> byte::Result<(Self, usize)> {
70        let (value, size) = u8::try_read(bytes, ctx)?;
71        Ok((Self::from_bits(value), size))
72    }
73}
74
75impl TryWrite<byte::ctx::Endian> for CommandIdentifier {
76    fn try_write(self, bytes: &mut [u8], ctx: byte::ctx::Endian) -> byte::Result<usize> {
77        (self as u8).try_write(bytes, ctx)
78    }
79}
80
81#[cfg(test)]
82mod tests {
83    use byte::TryRead;
84
85    use super::*;
86
87    #[test]
88    fn unpack_command_identifier() {
89        // given
90        let input = [0x0a];
91
92        // when
93        let (command_identifier, _) = CommandIdentifier::try_read(&input, byte::LE)
94            .expect("Could not read CommandIdentifier in test");
95
96        // then
97        assert_eq!(command_identifier, CommandIdentifier::ReportAttributes);
98    }
99}