Skip to main content

zigbee_zcl/
types.rs

1//! ZCL data types (ZCL specification, chapter 2.6).
2
3/// A ZCL data type, by its wire id.
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
5#[allow(missing_docs)]
6#[repr(u8)]
7pub enum DataType {
8    NoData = 0x00,
9    Data8 = 0x08,
10    Data16 = 0x09,
11    Data24 = 0x0A,
12    Data32 = 0x0B,
13    Data40 = 0x0C,
14    Data48 = 0x0D,
15    Data56 = 0x0E,
16    Data64 = 0x0F,
17    Bool = 0x10,
18    Map8 = 0x18,
19    Map16 = 0x19,
20    Map24 = 0x1A,
21    Map32 = 0x1B,
22    Map40 = 0x1C,
23    Map48 = 0x1D,
24    Map56 = 0x1E,
25    Map64 = 0x1F,
26    Uint8 = 0x20,
27    Uint16 = 0x21,
28    Uint24 = 0x22,
29    Uint32 = 0x23,
30    Uint40 = 0x24,
31    Uint48 = 0x25,
32    Uint56 = 0x26,
33    Uint64 = 0x27,
34    Int8 = 0x28,
35    Int16 = 0x29,
36    Int24 = 0x2A,
37    Int32 = 0x2B,
38    Int40 = 0x2C,
39    Int48 = 0x2D,
40    Int56 = 0x2E,
41    Int64 = 0x2F,
42    Enum8 = 0x30,
43    Enum16 = 0x31,
44    Semi = 0x38,
45    Single = 0x39,
46    Double = 0x3A,
47    OctetString = 0x41,
48    CharString = 0x42,
49    LongOctetString = 0x43,
50    LongCharString = 0x44,
51    Array = 0x48,
52    Struct = 0x4C,
53    Set = 0x50,
54    Bag = 0x51,
55    TimeOfDay = 0xE0,
56    Date = 0xE1,
57    UtcTime = 0xE2,
58    ClusterId = 0xE8,
59    AttributeId = 0xE9,
60    BacnetOid = 0xEA,
61    Ieee = 0xF0,
62    SecurityKey = 0xF1,
63    Unknown = 0xFF,
64}
65
66/// What a data type holds, which decides how it is encoded.
67#[derive(Debug, Clone, Copy, PartialEq, Eq)]
68pub enum Class {
69    /// No value.
70    NoData,
71    /// Raw bits (`dataN`).
72    Data,
73    /// Boolean.
74    Bool,
75    /// Bitmap.
76    Bitmap,
77    /// Unsigned integer, including time and id types.
78    Unsigned,
79    /// Signed integer.
80    Signed,
81    /// Enumeration.
82    Enum,
83    /// Floating point.
84    Float,
85    /// Octet string (one- or two-byte length).
86    Octets,
87    /// Character string (one- or two-byte length).
88    Chars,
89    /// Array, set or bag.
90    List,
91    /// Structure.
92    Struct,
93    /// IEEE address.
94    Ieee,
95    /// 128-bit key.
96    Key,
97    /// Unknown type.
98    Unknown,
99}
100
101impl DataType {
102    /// From the wire id.
103    pub fn from_u8(id: u8) -> Option<DataType> {
104        use DataType::*;
105        Some(match id {
106            0x00 => NoData,
107            0x08 => Data8,
108            0x09 => Data16,
109            0x0A => Data24,
110            0x0B => Data32,
111            0x0C => Data40,
112            0x0D => Data48,
113            0x0E => Data56,
114            0x0F => Data64,
115            0x10 => Bool,
116            0x18 => Map8,
117            0x19 => Map16,
118            0x1A => Map24,
119            0x1B => Map32,
120            0x1C => Map40,
121            0x1D => Map48,
122            0x1E => Map56,
123            0x1F => Map64,
124            0x20 => Uint8,
125            0x21 => Uint16,
126            0x22 => Uint24,
127            0x23 => Uint32,
128            0x24 => Uint40,
129            0x25 => Uint48,
130            0x26 => Uint56,
131            0x27 => Uint64,
132            0x28 => Int8,
133            0x29 => Int16,
134            0x2A => Int24,
135            0x2B => Int32,
136            0x2C => Int40,
137            0x2D => Int48,
138            0x2E => Int56,
139            0x2F => Int64,
140            0x30 => Enum8,
141            0x31 => Enum16,
142            0x38 => Semi,
143            0x39 => Single,
144            0x3A => Double,
145            0x41 => OctetString,
146            0x42 => CharString,
147            0x43 => LongOctetString,
148            0x44 => LongCharString,
149            0x48 => Array,
150            0x4C => Struct,
151            0x50 => Set,
152            0x51 => Bag,
153            0xE0 => TimeOfDay,
154            0xE1 => Date,
155            0xE2 => UtcTime,
156            0xE8 => ClusterId,
157            0xE9 => AttributeId,
158            0xEA => BacnetOid,
159            0xF0 => Ieee,
160            0xF1 => SecurityKey,
161            0xFF => Unknown,
162            _ => return None,
163        })
164    }
165
166    /// The wire id.
167    pub fn id(self) -> u8 {
168        self as u8
169    }
170
171    /// The type's class.
172    pub fn class(self) -> Class {
173        use DataType::*;
174        match self {
175            NoData => Class::NoData,
176            Data8 | Data16 | Data24 | Data32 | Data40 | Data48 | Data56 | Data64 => Class::Data,
177            Bool => Class::Bool,
178            Map8 | Map16 | Map24 | Map32 | Map40 | Map48 | Map56 | Map64 => Class::Bitmap,
179            Uint8 | Uint16 | Uint24 | Uint32 | Uint40 | Uint48 | Uint56 | Uint64 => Class::Unsigned,
180            TimeOfDay | Date | UtcTime | ClusterId | AttributeId | BacnetOid => Class::Unsigned,
181            Int8 | Int16 | Int24 | Int32 | Int40 | Int48 | Int56 | Int64 => Class::Signed,
182            Enum8 | Enum16 => Class::Enum,
183            Semi | Single | Double => Class::Float,
184            OctetString | LongOctetString => Class::Octets,
185            CharString | LongCharString => Class::Chars,
186            Array | Set | Bag => Class::List,
187            Struct => Class::Struct,
188            Ieee => Class::Ieee,
189            SecurityKey => Class::Key,
190            Unknown => Class::Unknown,
191        }
192    }
193
194    /// Encoded size of a fixed-size type; `None` for strings, lists and
195    /// structures.
196    pub fn fixed_size(self) -> Option<usize> {
197        use DataType::*;
198        Some(match self {
199            NoData | Unknown => 0,
200            Data8 | Bool | Map8 | Uint8 | Int8 | Enum8 => 1,
201            Data16 | Map16 | Uint16 | Int16 | Enum16 | Semi | ClusterId | AttributeId => 2,
202            Data24 | Map24 | Uint24 | Int24 => 3,
203            Data32 | Map32 | Uint32 | Int32 | Single | TimeOfDay | Date | UtcTime | BacnetOid => 4,
204            Data40 | Map40 | Uint40 | Int40 => 5,
205            Data48 | Map48 | Uint48 | Int48 => 6,
206            Data56 | Map56 | Uint56 | Int56 => 7,
207            Data64 | Map64 | Uint64 | Int64 | Double | Ieee => 8,
208            SecurityKey => 16,
209            OctetString | CharString | LongOctetString | LongCharString | Array | Struct | Set
210            | Bag => return None,
211        })
212    }
213
214    /// Analog types carry a reportable change in reporting configuration;
215    /// discrete types do not.
216    pub fn is_analog(self) -> bool {
217        matches!(self.class(), Class::Unsigned | Class::Signed | Class::Float)
218            && !matches!(
219                self,
220                DataType::ClusterId | DataType::AttributeId | DataType::BacnetOid
221            )
222    }
223}
224
225#[cfg(test)]
226mod tests {
227    use super::*;
228
229    #[test]
230    fn every_id_round_trips() {
231        for id in 0..=255u8 {
232            if let Some(t) = DataType::from_u8(id) {
233                assert_eq!(t.id(), id);
234            }
235        }
236        assert!(DataType::Int16.is_analog());
237        assert!(!DataType::Enum8.is_analog());
238        assert!(!DataType::ClusterId.is_analog());
239        assert_eq!(DataType::Uint48.fixed_size(), Some(6));
240    }
241}