use zigbee_primitives::wire::{Reader, Truncated};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Format {
Legacy,
Extended,
}
pub const CONTROL_RESPONSE: u8 = 0x80;
pub const CONTROL_OVERFLOW: u8 = 0x01;
pub const CONTROL_TRUNCATED: u8 = 0x02;
pub const CONTROL_PENDING_CALLBACKS: u8 = 0x04;
pub const CONTROL_SYNC_CALLBACK: u8 = 0x08;
pub const CONTROL_ASYNC_CALLBACK: u8 = 0x10;
pub const CONTROL_HIGH_FORMAT_1: u8 = 0x01;
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
pub enum HeaderError {
#[error(transparent)]
Truncated(#[from] Truncated),
#[error("unsupported EZSP frame format version {0}")]
FormatVersion(u8),
#[error("secured or padded EZSP frames are not supported")]
Secured,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Header<'a> {
pub seq: u8,
pub control: u8,
pub frame_id: u16,
pub params: &'a [u8],
}
impl Header<'_> {
pub fn is_response(&self) -> bool {
self.control & CONTROL_RESPONSE != 0
}
pub fn is_callback(&self) -> bool {
self.control & (CONTROL_SYNC_CALLBACK | CONTROL_ASYNC_CALLBACK) != 0
}
pub fn overflow(&self) -> bool {
self.is_response() && self.control & CONTROL_OVERFLOW != 0
}
pub fn truncated(&self) -> bool {
self.is_response() && self.control & CONTROL_TRUNCATED != 0
}
}
pub fn parse(frame: &[u8], format: Format) -> Result<Header<'_>, HeaderError> {
let mut r = Reader::new(frame);
let seq = r.u8()?;
let control = r.u8()?;
let frame_id = match format {
Format::Legacy => u16::from(r.u8()?),
Format::Extended => {
let high = r.u8()?;
if high & 0x03 != CONTROL_HIGH_FORMAT_1 {
return Err(HeaderError::FormatVersion(high & 0x03));
}
if high & 0xC0 != 0 {
return Err(HeaderError::Secured);
}
r.u16()?
}
};
Ok(Header {
seq,
control,
frame_id,
params: r.rest(),
})
}
pub fn detect_command_format(frame: &[u8]) -> Format {
match frame {
[_, 0x00, 0x00, _] => Format::Legacy,
_ => Format::Extended,
}
}
pub fn encode(format: Format, seq: u8, control: u8, frame_id: u16, params: &[u8]) -> Vec<u8> {
let mut out = Vec::with_capacity(5 + params.len());
out.push(seq);
out.push(control);
match format {
Format::Legacy => out.push(frame_id as u8),
Format::Extended => {
out.push(CONTROL_HIGH_FORMAT_1);
out.extend_from_slice(&frame_id.to_le_bytes());
}
}
out.extend_from_slice(params);
out
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn legacy_and_extended_round_trip() {
let legacy = encode(Format::Legacy, 0, 0, 0, &[13]);
assert_eq!(legacy, [0, 0, 0, 13]);
assert_eq!(detect_command_format(&legacy), Format::Legacy);
let ext = encode(Format::Extended, 5, 0, 0x0114, &[1, 2]);
assert_eq!(ext, [5, 0, 1, 0x14, 0x01, 1, 2]);
assert_eq!(detect_command_format(&ext), Format::Extended);
let h = parse(&ext, Format::Extended).unwrap();
assert_eq!((h.seq, h.frame_id, h.params), (5, 0x0114, &[1u8, 2][..]));
assert!(parse(&[1, 0x80, 0x81, 0, 0], Format::Extended).is_err());
assert!(parse(&[1], Format::Extended).is_err());
}
}