zigbee_cluster_library/header/
command_identifier.rs

1//! Command Identifier.
2use byte::TryRead;
3use byte::TryWrite;
4
5/// Command Identifier.
6///
7/// See Section 2.4.1.4 (Values can be found in Table 2-3)
8#[allow(missing_docs)]
9#[derive(Debug, Clone, Copy, PartialEq, Eq)]
10#[repr(u8)]
11#[non_exhaustive]
12pub enum CommandIdentifier {
13    ReadAttributes = 0x00,
14    ReadAttributesResponse = 0x01,
15    WriteAttributes = 0x02,
16    WriteAttributesUndivided = 0x03,
17    WriteAttributesResponse = 0x04,
18    WriteAttributesNoResponse = 0x05,
19    ConfigureReporting = 0x06,
20    ConfigureReportingResponse = 0x07,
21    ReadReportingConfiguration = 0x08,
22    ReadReportingConfigurationResponse = 0x09,
23    ReportAttributes = 0x0a,
24    DefaultResponse = 0x0b,
25    DiscoverAttributes = 0x0c,
26    DiscoverAttributesResponse = 0x0d,
27    ReadAttributesStructured = 0x0e,
28    WriteAttributesStructured = 0x0f,
29    WriteAttributesStructuredResponse = 0x10,
30    DiscoverCommandsReceived = 0x11,
31    DiscoverCommandsReceivedResponse = 0x12,
32    DiscoverCommandsGenerated = 0x13,
33    DiscoverCommandsGeneratedResponse = 0x14,
34    DiscoverAttributesExtended = 0x15,
35    DiscoverAttributesExtendedResponse = 0x16,
36    Reserved = 0xff,
37}
38
39impl CommandIdentifier {
40    fn from_bits(b: u8) -> Self {
41        match b {
42            0x00 => Self::ReadAttributes,
43            0x01 => Self::ReadAttributesResponse,
44            0x02 => Self::WriteAttributes,
45            0x03 => Self::WriteAttributesUndivided,
46            0x04 => Self::WriteAttributesResponse,
47            0x05 => Self::WriteAttributesNoResponse,
48            0x06 => Self::ConfigureReporting,
49            0x07 => Self::ConfigureReportingResponse,
50            0x08 => Self::ReadReportingConfiguration,
51            0x09 => Self::ReadReportingConfigurationResponse,
52            0x0a => Self::ReportAttributes,
53            0x0b => Self::DefaultResponse,
54            0x0c => Self::DiscoverAttributes,
55            0x0d => Self::DiscoverAttributesResponse,
56            0x0e => Self::ReadAttributesStructured,
57            0x0f => Self::WriteAttributesStructured,
58            0x10 => Self::WriteAttributesStructuredResponse,
59            0x11 => Self::DiscoverCommandsReceived,
60            0x12 => Self::DiscoverCommandsReceivedResponse,
61            0x13 => Self::DiscoverCommandsGenerated,
62            0x14 => Self::DiscoverCommandsGeneratedResponse,
63            0x15 => Self::DiscoverAttributesExtended,
64            0x16 => Self::DiscoverAttributesExtendedResponse,
65            _ => Self::Reserved,
66        }
67    }
68}
69
70impl TryRead<'_, byte::ctx::Endian> for CommandIdentifier {
71    fn try_read(bytes: &[u8], ctx: byte::ctx::Endian) -> byte::Result<(Self, usize)> {
72        let (value, size) = u8::try_read(bytes, ctx)?;
73        Ok((Self::from_bits(value), size))
74    }
75}
76
77impl TryWrite<byte::ctx::Endian> for CommandIdentifier {
78    fn try_write(self, bytes: &mut [u8], ctx: byte::ctx::Endian) -> byte::Result<usize> {
79        (self as u8).try_write(bytes, ctx)
80    }
81}
82
83#[cfg(test)]
84mod tests {
85    use byte::TryRead;
86
87    use super::*;
88    use crate::test_branches_command_identifier;
89
90    test_branches_command_identifier! {
91        cmd_ident_read_attributes: &[0x00] => CommandIdentifier::ReadAttributes,
92        cmd_ident_read_attributes_response: &[0x01] => CommandIdentifier::ReadAttributesResponse,
93        cmd_ident_write_attributes: &[0x02] => CommandIdentifier::WriteAttributes,
94        cmd_ident_write_attributes_undivided: &[0x03] => CommandIdentifier::WriteAttributesUndivided,
95        cmd_ident_write_attributes_response: &[0x04] => CommandIdentifier::WriteAttributesResponse,
96        cmd_ident_write_attributes_no_response: &[0x05] => CommandIdentifier::WriteAttributesNoResponse,
97        cmd_ident_configure_reporting: &[0x06] => CommandIdentifier::ConfigureReporting,
98        cmd_ident_configure_reporting_response: &[0x07] => CommandIdentifier::ConfigureReportingResponse,
99        cmd_ident_read_reporting_configuration: &[0x08] => CommandIdentifier::ReadReportingConfiguration,
100        cmd_ident_read_reporting_configuration_response: &[0x09] => CommandIdentifier::ReadReportingConfigurationResponse,
101        cmd_ident_report_attributes: &[0x0a] => CommandIdentifier::ReportAttributes,
102        cmd_ident_default_response: &[0x0b] => CommandIdentifier::DefaultResponse,
103        cmd_ident_discover_attributes: &[0x0c] => CommandIdentifier::DiscoverAttributes,
104        cmd_ident_discover_attributes_response: &[0x0d] => CommandIdentifier::DiscoverAttributesResponse,
105        cmd_ident_read_attributes_structured: &[0x0e] => CommandIdentifier::ReadAttributesStructured,
106        cmd_ident_write_attributes_structured: &[0x0f] => CommandIdentifier::WriteAttributesStructured,
107        cmd_ident_write_attributes_structured_response: &[0x10] => CommandIdentifier::WriteAttributesStructuredResponse,
108        cmd_ident_discover_commands_received: &[0x11] => CommandIdentifier::DiscoverCommandsReceived,
109        cmd_ident_discovercommandsreceivedresponse: &[0x12] => CommandIdentifier::DiscoverCommandsReceivedResponse,
110        cmd_ident_discovercommandsgenerated: &[0x13] => CommandIdentifier::DiscoverCommandsGenerated,
111        cmd_ident_discovercommandsgeneratedresponse: &[0x14] => CommandIdentifier::DiscoverCommandsGeneratedResponse,
112        cmd_ident_discoverattributesextended: &[0x15] => CommandIdentifier::DiscoverAttributesExtended,
113        cmd_ident_discoverattributesextendedresponse: &[0x16] => CommandIdentifier::DiscoverAttributesExtendedResponse,
114    }
115}
116
117#[macro_export]
118macro_rules! test_branches_command_identifier {
119    ($($name:ident: $input:expr => $want:expr,)+) => {
120        $(
121            #[test]
122            fn $name() {
123                // when
124                let (command_identifier, _) = CommandIdentifier::try_read($input, byte::LE)
125                    .expect("Could not read CommandIdentifier in test");
126
127                // then
128                assert_eq!(command_identifier, $want);
129            }
130        )+
131    };
132}