1use zigbee_primitives::wire::{Reader, Truncated};
10
11#[derive(Debug, Clone, Copy, PartialEq, Eq)]
13pub enum Format {
14 Legacy,
16 Extended,
18}
19
20pub const CONTROL_RESPONSE: u8 = 0x80;
22pub const CONTROL_OVERFLOW: u8 = 0x01;
24pub const CONTROL_TRUNCATED: u8 = 0x02;
26pub const CONTROL_PENDING_CALLBACKS: u8 = 0x04;
28pub const CONTROL_SYNC_CALLBACK: u8 = 0x08;
30pub const CONTROL_ASYNC_CALLBACK: u8 = 0x10;
32pub const CONTROL_HIGH_FORMAT_1: u8 = 0x01;
34
35#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
37pub enum HeaderError {
38 #[error(transparent)]
40 Truncated(#[from] Truncated),
41 #[error("unsupported EZSP frame format version {0}")]
43 FormatVersion(u8),
44 #[error("secured or padded EZSP frames are not supported")]
47 Secured,
48}
49
50#[derive(Debug, Clone, PartialEq, Eq)]
52pub struct Header<'a> {
53 pub seq: u8,
55 pub control: u8,
57 pub frame_id: u16,
59 pub params: &'a [u8],
61}
62
63impl Header<'_> {
64 pub fn is_response(&self) -> bool {
66 self.control & CONTROL_RESPONSE != 0
67 }
68
69 pub fn is_callback(&self) -> bool {
71 self.control & (CONTROL_SYNC_CALLBACK | CONTROL_ASYNC_CALLBACK) != 0
72 }
73
74 pub fn overflow(&self) -> bool {
76 self.is_response() && self.control & CONTROL_OVERFLOW != 0
77 }
78
79 pub fn truncated(&self) -> bool {
81 self.is_response() && self.control & CONTROL_TRUNCATED != 0
82 }
83}
84
85pub fn parse(frame: &[u8], format: Format) -> Result<Header<'_>, HeaderError> {
87 let mut r = Reader::new(frame);
88 let seq = r.u8()?;
89 let control = r.u8()?;
90 let frame_id = match format {
91 Format::Legacy => u16::from(r.u8()?),
92 Format::Extended => {
93 let high = r.u8()?;
94 if high & 0x03 != CONTROL_HIGH_FORMAT_1 {
95 return Err(HeaderError::FormatVersion(high & 0x03));
96 }
97 if high & 0xC0 != 0 {
98 return Err(HeaderError::Secured);
99 }
100 r.u16()?
101 }
102 };
103 Ok(Header {
104 seq,
105 control,
106 frame_id,
107 params: r.rest(),
108 })
109}
110
111pub fn detect_command_format(frame: &[u8]) -> Format {
114 match frame {
115 [_, 0x00, 0x00, _] => Format::Legacy,
116 _ => Format::Extended,
117 }
118}
119
120pub fn encode(format: Format, seq: u8, control: u8, frame_id: u16, params: &[u8]) -> Vec<u8> {
122 let mut out = Vec::with_capacity(5 + params.len());
123 out.push(seq);
124 out.push(control);
125 match format {
126 Format::Legacy => out.push(frame_id as u8),
127 Format::Extended => {
128 out.push(CONTROL_HIGH_FORMAT_1);
129 out.extend_from_slice(&frame_id.to_le_bytes());
130 }
131 }
132 out.extend_from_slice(params);
133 out
134}
135
136#[cfg(test)]
137mod tests {
138 use super::*;
139
140 #[test]
141 fn legacy_and_extended_round_trip() {
142 let legacy = encode(Format::Legacy, 0, 0, 0, &[13]);
143 assert_eq!(legacy, [0, 0, 0, 13]);
144 assert_eq!(detect_command_format(&legacy), Format::Legacy);
145 let ext = encode(Format::Extended, 5, 0, 0x0114, &[1, 2]);
146 assert_eq!(ext, [5, 0, 1, 0x14, 0x01, 1, 2]);
147 assert_eq!(detect_command_format(&ext), Format::Extended);
148 let h = parse(&ext, Format::Extended).unwrap();
149 assert_eq!((h.seq, h.frame_id, h.params), (5, 0x0114, &[1u8, 2][..]));
150 assert!(parse(&[1, 0x80, 0x81, 0, 0], Format::Extended).is_err());
151 assert!(parse(&[1], Format::Extended).is_err());
152 }
153}