use zigbee_primitives::wire::{Reader, Truncated, Writer};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FrameType {
Global,
Cluster,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Direction {
ClientToServer,
ServerToClient,
}
impl Direction {
pub fn reverse(self) -> Direction {
match self {
Direction::ClientToServer => Direction::ServerToClient,
Direction::ServerToClient => Direction::ClientToServer,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Header {
pub frame_type: FrameType,
pub manufacturer: Option<u16>,
pub direction: Direction,
pub disable_default_response: bool,
pub seq: u8,
pub command: u8,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Frame {
pub header: Header,
pub payload: Vec<u8>,
}
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum FrameError {
#[error(transparent)]
Truncated(#[from] Truncated),
#[error("reserved ZCL frame type {0}")]
ReservedType(u8),
}
impl Frame {
pub fn parse(bytes: &[u8]) -> Result<Frame, FrameError> {
let mut r = Reader::new(bytes);
let control = r.u8()?;
let frame_type = match control & 0x03 {
0 => FrameType::Global,
1 => FrameType::Cluster,
other => return Err(FrameError::ReservedType(other)),
};
let manufacturer = if control & 0x04 != 0 {
Some(r.u16()?)
} else {
None
};
let direction = if control & 0x08 != 0 {
Direction::ServerToClient
} else {
Direction::ClientToServer
};
let seq = r.u8()?;
let command = r.u8()?;
Ok(Frame {
header: Header {
frame_type,
manufacturer,
direction,
disable_default_response: control & 0x10 != 0,
seq,
command,
},
payload: r.rest().to_vec(),
})
}
pub fn encode(&self) -> Vec<u8> {
let h = &self.header;
let mut control = match h.frame_type {
FrameType::Global => 0,
FrameType::Cluster => 1,
};
if h.manufacturer.is_some() {
control |= 0x04;
}
if h.direction == Direction::ServerToClient {
control |= 0x08;
}
if h.disable_default_response {
control |= 0x10;
}
let mut w = Vec::with_capacity(5 + self.payload.len());
w.put_u8(control);
if let Some(m) = h.manufacturer {
w.put_u16(m);
}
w.put_u8(h.seq);
w.put_u8(h.command);
w.put_bytes(&self.payload);
w
}
pub fn global(direction: Direction, seq: u8, command: u8, payload: Vec<u8>) -> Frame {
Frame {
header: Header {
frame_type: FrameType::Global,
manufacturer: None,
direction,
disable_default_response: false,
seq,
command,
},
payload,
}
}
pub fn cluster(direction: Direction, seq: u8, command: u8, payload: Vec<u8>) -> Frame {
Frame {
header: Header {
frame_type: FrameType::Cluster,
manufacturer: None,
direction,
disable_default_response: false,
seq,
command,
},
payload,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn round_trips_with_manufacturer_code() {
let mut f = Frame::cluster(Direction::ServerToClient, 7, 0x0A, vec![1, 2]);
f.header.manufacturer = Some(0x115F);
f.header.disable_default_response = true;
let bytes = f.encode();
assert_eq!(bytes, [0x1D, 0x5F, 0x11, 7, 0x0A, 1, 2]);
assert_eq!(Frame::parse(&bytes).unwrap(), f);
assert!(Frame::parse(&[0x02, 0, 0]).is_err());
assert!(Frame::parse(&[0x04, 0]).is_err());
}
}