use std::collections::{BTreeMap, HashMap};
use zigbee_primitives::{Ieee, Key, NodeId};
use zigbee_zcl::foundation::{self, Global, ReadRecord, ReportingConfig, ReportingStatus};
use zigbee_zcl::{attr, cluster_id, status, DataType, Direction, Frame, FrameType, Value};
use zigbee_zdo::{Binding, Body, Message, NodeDescriptor, SimpleDescriptor};
#[derive(Debug, Clone)]
pub struct SimEndpoint {
pub endpoint: u8,
pub profile_id: u16,
pub device_id: u16,
pub servers: BTreeMap<u16, BTreeMap<u16, Value>>,
pub clients: Vec<u16>,
}
#[derive(Debug, Clone)]
pub struct SimDevice {
pub ieee: Ieee,
pub router: bool,
pub sleepy: bool,
pub legacy: bool,
pub install_code_key: Option<Key>,
pub manufacturer_code: u16,
pub endpoints: Vec<SimEndpoint>,
pub lqi: u8,
pub rssi: i8,
pub pairing: bool,
pub firmware: Option<SimFirmware>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SimFirmware {
pub image_type: u16,
pub file_version: u32,
pub image: Vec<u8>,
}
#[derive(Debug, Clone, Default)]
pub struct OtaDownload {
manufacturer: u16,
image_type: u16,
file_version: u32,
size: u32,
data: Vec<u8>,
}
const OTA_BLOCK: u8 = 48;
fn basic(manufacturer: &str, model: &str, power_source: u8) -> BTreeMap<u16, Value> {
BTreeMap::from([
(attr::basic::ZCL_VERSION, Value::u8(8)),
(attr::basic::APPLICATION_VERSION, Value::u8(1)),
(attr::basic::STACK_VERSION, Value::u8(2)),
(attr::basic::HW_VERSION, Value::u8(1)),
(attr::basic::MANUFACTURER_NAME, Value::string(manufacturer)),
(attr::basic::MODEL_IDENTIFIER, Value::string(model)),
(attr::basic::DATE_CODE, Value::string("20261119")),
(attr::basic::POWER_SOURCE, Value::enum8(power_source)),
(attr::basic::SW_BUILD_ID, Value::string("1.0.0")),
])
}
pub const SIM_MANUFACTURER: &str = "zigbee-bridge-sim";
impl SimDevice {
pub fn temperature_humidity_sensor(ieee: Ieee) -> SimDevice {
let servers = BTreeMap::from([
(cluster_id::BASIC, basic(SIM_MANUFACTURER, "SIM-TH1", 0x03)),
(
cluster_id::POWER_CONFIGURATION,
BTreeMap::from([
(attr::power_configuration::BATTERY_VOLTAGE, Value::u8(30)),
(
attr::power_configuration::BATTERY_PERCENTAGE_REMAINING,
Value::u8(200),
),
]),
),
(
cluster_id::IDENTIFY,
BTreeMap::from([(attr::identify::IDENTIFY_TIME, Value::u16(0))]),
),
(
cluster_id::TEMPERATURE_MEASUREMENT,
BTreeMap::from([
(
attr::temperature_measurement::MEASURED_VALUE,
Value::i16(2000),
),
(
attr::temperature_measurement::MIN_MEASURED_VALUE,
Value::i16(-4000),
),
(
attr::temperature_measurement::MAX_MEASURED_VALUE,
Value::i16(12500),
),
]),
),
(
cluster_id::RELATIVE_HUMIDITY_MEASUREMENT,
BTreeMap::from([(
attr::relative_humidity_measurement::MEASURED_VALUE,
Value::u16(4500),
)]),
),
]);
SimDevice {
ieee,
router: false,
sleepy: true,
legacy: false,
install_code_key: None,
manufacturer_code: 0x1234,
endpoints: vec![SimEndpoint {
endpoint: 1,
profile_id: 0x0104,
device_id: 0x0302,
servers,
clients: vec![cluster_id::OTA_UPGRADE],
}],
lqi: 180,
rssi: -58,
pairing: true,
firmware: None,
}
}
pub fn plug(ieee: Ieee) -> SimDevice {
let servers = BTreeMap::from([
(
cluster_id::BASIC,
basic(SIM_MANUFACTURER, "SIM-PLUG1", 0x01),
),
(
cluster_id::IDENTIFY,
BTreeMap::from([(attr::identify::IDENTIFY_TIME, Value::u16(0))]),
),
(
cluster_id::GROUPS,
BTreeMap::from([(attr::groups::NAME_SUPPORT, Value::map8(0))]),
),
(
cluster_id::ON_OFF,
BTreeMap::from([(attr::on_off::ON_OFF, Value::bool(false))]),
),
(
cluster_id::ELECTRICAL_MEASUREMENT,
BTreeMap::from([
(
attr::electrical_measurement::ACTIVE_POWER,
Value::Int(DataType::Int16, 0),
),
(attr::electrical_measurement::RMS_VOLTAGE, Value::u16(230)),
(
attr::electrical_measurement::AC_POWER_MULTIPLIER,
Value::u16(1),
),
(
attr::electrical_measurement::AC_POWER_DIVISOR,
Value::u16(1),
),
]),
),
]);
SimDevice {
ieee,
router: true,
sleepy: false,
legacy: false,
install_code_key: None,
manufacturer_code: 0x1234,
endpoints: vec![SimEndpoint {
endpoint: 1,
profile_id: 0x0104,
device_id: 0x0051,
servers,
clients: vec![cluster_id::OTA_UPGRADE],
}],
lqi: 230,
rssi: -40,
pairing: true,
firmware: Some(SimFirmware {
image_type: 0x0001,
file_version: 0x0100_0000,
image: Vec::new(),
}),
}
}
pub fn contact_sensor(ieee: Ieee) -> SimDevice {
let servers = BTreeMap::from([
(
cluster_id::BASIC,
basic(SIM_MANUFACTURER, "SIM-DOOR1", 0x03),
),
(
cluster_id::POWER_CONFIGURATION,
BTreeMap::from([(
attr::power_configuration::BATTERY_PERCENTAGE_REMAINING,
Value::u8(180),
)]),
),
(
cluster_id::IAS_ZONE,
BTreeMap::from([
(attr::ias_zone::ZONE_STATE, Value::enum8(0)),
(attr::ias_zone::ZONE_TYPE, Value::enum16(0x0015)),
(attr::ias_zone::ZONE_STATUS, Value::map16(0)),
(attr::ias_zone::IAS_CIE_ADDRESS, Value::Ieee(Ieee::ZERO)),
(attr::ias_zone::ZONE_ID, Value::u8(0xFF)),
]),
),
]);
SimDevice {
ieee,
router: false,
sleepy: true,
legacy: false,
install_code_key: None,
manufacturer_code: 0x1234,
endpoints: vec![SimEndpoint {
endpoint: 1,
profile_id: 0x0104,
device_id: 0x0402,
servers,
clients: vec![],
}],
lqi: 150,
rssi: -70,
pairing: true,
firmware: None,
}
}
fn node_descriptor(&self) -> NodeDescriptor {
let (logical, mac) = if self.router {
(1, 0x8E)
} else if self.sleepy {
(2, 0x80)
} else {
(2, 0x88)
};
NodeDescriptor {
byte0: logical,
byte1: 0x40,
mac_capabilities: mac,
manufacturer_code: self.manufacturer_code,
max_buffer_size: 82,
max_incoming_transfer: 82,
server_mask: 22 << 9,
max_outgoing_transfer: 82,
descriptor_capability: 0,
}
}
fn simple_descriptor(&self, endpoint: u8) -> Option<SimpleDescriptor> {
self.endpoints
.iter()
.find(|e| e.endpoint == endpoint)
.map(|e| SimpleDescriptor {
endpoint: e.endpoint,
profile_id: e.profile_id,
device_id: e.device_id,
device_version: 1,
input_clusters: e.servers.keys().copied().collect(),
output_clusters: e.clients.clone(),
})
}
}
#[derive(Debug, Clone, Default)]
pub struct DeviceState {
pub bindings: Vec<Binding>,
pub reporting: HashMap<(u8, u16, u16), ReportingConfig>,
pub seq: u8,
pub ias_enrolled: bool,
pub ota: Option<OtaDownload>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Outgoing {
pub profile: u16,
pub cluster: u16,
pub source_endpoint: u8,
pub destination_endpoint: u8,
pub payload: Vec<u8>,
}
#[derive(Debug, Default)]
pub struct Reaction {
pub replies: Vec<Outgoing>,
pub leave: bool,
}
impl SimDevice {
pub fn handle_zdo(
&self,
nwk: NodeId,
state: &mut DeviceState,
cluster: u16,
payload: &[u8],
coordinator: Ieee,
) -> Reaction {
let mut reaction = Reaction::default();
let Ok(request) = Message::parse(cluster, payload) else {
return reaction;
};
let body = match request.body {
Body::NodeDescReq { .. } => Body::NodeDescRsp {
status: 0,
nwk,
descriptor: Some(self.node_descriptor()),
},
Body::ActiveEpReq { .. } => Body::ActiveEpRsp {
status: 0,
nwk,
endpoints: self.endpoints.iter().map(|e| e.endpoint).collect(),
},
Body::SimpleDescReq { endpoint, .. } => match self.simple_descriptor(endpoint) {
Some(d) => Body::SimpleDescRsp {
status: 0,
nwk,
descriptor: Some(d),
},
None => Body::SimpleDescRsp {
status: zigbee_zdo::status::INVALID_EP,
nwk,
descriptor: None,
},
},
Body::BindReq(binding) => {
let ok = binding.source == self.ieee
&& self
.endpoints
.iter()
.any(|e| e.endpoint == binding.source_endpoint);
if ok && !state.bindings.contains(&binding) {
state.bindings.push(binding);
}
Body::BindRsp {
status: if ok {
0
} else {
zigbee_zdo::status::NOT_SUPPORTED
},
}
}
Body::UnbindReq(binding) => {
let before = state.bindings.len();
state.bindings.retain(|b| *b != binding);
Body::UnbindRsp {
status: if state.bindings.len() < before {
0
} else {
zigbee_zdo::status::NO_ENTRY
},
}
}
Body::MgmtLeaveReq { ieee, .. } if ieee == self.ieee || ieee == Ieee::ZERO => {
reaction.leave = true;
Body::MgmtLeaveRsp { status: 0 }
}
Body::MgmtLqiReq { .. } => Body::MgmtLqiRsp {
status: 0,
total: 1,
start_index: 0,
neighbors: vec![zigbee_zdo::Neighbor {
extended_pan_id: zigbee_primitives::ExtPanId(0),
ieee: coordinator,
nwk: 0,
flags: 0x04,
permit_joining: 2,
depth: 0,
lqi: self.lqi,
}],
},
Body::MgmtRtgReq { start_index } if self.router => Body::MgmtRtgRsp {
status: 0,
total: 1,
start_index,
routes: if start_index == 0 {
vec![zigbee_zdo::Route {
destination: 0x0000,
flags: 0,
next_hop: 0x0000,
}]
} else {
Vec::new()
},
},
Body::MgmtRtgReq { start_index } => Body::MgmtRtgRsp {
status: zigbee_zdo::status::NOT_SUPPORTED,
total: 0,
start_index,
routes: Vec::new(),
},
Body::MgmtPermitJoiningReq { .. } => Body::MgmtPermitJoiningRsp { status: 0 },
Body::IeeeAddrReq { .. } => Body::IeeeAddrRsp {
status: 0,
ieee: self.ieee,
nwk,
associated: None,
},
Body::NwkAddrReq { .. } => Body::NwkAddrRsp {
status: 0,
ieee: self.ieee,
nwk,
associated: None,
},
_ => return reaction,
};
let (cluster, payload) = Message {
tsn: request.tsn,
body,
}
.encode();
reaction.replies.push(Outgoing {
profile: 0,
cluster,
source_endpoint: 0,
destination_endpoint: 0,
payload,
});
reaction
}
fn handle_ota(
&mut self,
state: &mut DeviceState,
command: u8,
payload: &[u8],
) -> Option<(u8, Vec<u8>)> {
let manufacturer = self.manufacturer_code;
let firmware = self.firmware.as_mut()?;
let mut r = zigbee_primitives::wire::Reader::new(payload);
let block_request = |d: &OtaDownload| {
let mut p = vec![0u8];
p.extend_from_slice(&d.manufacturer.to_le_bytes());
p.extend_from_slice(&d.image_type.to_le_bytes());
p.extend_from_slice(&d.file_version.to_le_bytes());
p.extend_from_slice(&(d.data.len() as u32).to_le_bytes());
p.push(OTA_BLOCK);
(0x03, p)
};
match command {
0x00 => {
let mut p = vec![0u8];
p.extend_from_slice(&manufacturer.to_le_bytes());
p.extend_from_slice(&firmware.image_type.to_le_bytes());
p.extend_from_slice(&firmware.file_version.to_le_bytes());
Some((0x01, p))
}
0x02 => {
if r.u8().ok()? != 0 {
return None;
}
let d = OtaDownload {
manufacturer: r.u16().ok()?,
image_type: r.u16().ok()?,
file_version: r.u32().ok()?,
size: r.u32().ok()?,
data: Vec::new(),
};
let next = block_request(&d);
state.ota = Some(d);
Some(next)
}
0x05 => {
if r.u8().ok()? != 0 {
state.ota = None;
return None;
}
let d = state.ota.as_mut()?;
let _ = (r.u16().ok()?, r.u16().ok()?, r.u32().ok()?);
let offset = r.u32().ok()?;
let n = usize::from(r.u8().ok()?);
let data = r.bytes(n).ok()?;
if offset as usize != d.data.len() || n == 0 {
return Some(block_request(d));
}
d.data.extend_from_slice(data);
if d.data.len() as u32 >= d.size {
let mut p = vec![0u8];
p.extend_from_slice(&d.manufacturer.to_le_bytes());
p.extend_from_slice(&d.image_type.to_le_bytes());
p.extend_from_slice(&d.file_version.to_le_bytes());
Some((0x06, p))
} else {
Some(block_request(d))
}
}
0x07 => {
let d = state.ota.take()?;
firmware.file_version = d.file_version;
firmware.image = d.data;
None
}
_ => None,
}
}
fn endpoint_mut(&mut self, endpoint: u8) -> Option<&mut SimEndpoint> {
self.endpoints.iter_mut().find(|e| e.endpoint == endpoint)
}
pub fn handle_zcl(
&mut self,
state: &mut DeviceState,
endpoint: u8,
cluster: u16,
source_endpoint: u8,
payload: &[u8],
) -> Reaction {
let mut reaction = Reaction::default();
let Ok(frame) = Frame::parse(payload) else {
return reaction;
};
let h = frame.header;
let reply_to = |frame: Frame| Outgoing {
profile: 0x0104,
cluster,
source_endpoint: endpoint,
destination_endpoint: source_endpoint,
payload: frame.encode(),
};
let default_response = |status: u8| {
let mut f = Frame::global(
Direction::ServerToClient,
h.seq,
foundation::id::DEFAULT_RESPONSE,
Global::DefaultResponse {
command: h.command,
status,
}
.encode(),
);
f.header.disable_default_response = true;
f
};
if cluster == cluster_id::OTA_UPGRADE
&& h.frame_type == FrameType::Cluster
&& h.direction == Direction::ServerToClient
{
if let Some(payload) = self.handle_ota(state, h.command, &frame.payload) {
state.seq = state.seq.wrapping_add(1);
let request =
Frame::cluster(Direction::ClientToServer, state.seq, payload.0, payload.1);
reaction.replies.push(reply_to(request));
}
return reaction;
}
let Some(ep) = self.endpoint_mut(endpoint) else {
return reaction;
};
let Some(attributes) = ep.servers.get_mut(&cluster) else {
if !h.disable_default_response {
reaction
.replies
.push(reply_to(default_response(status::UNSUPPORTED_CLUSTER)));
}
return reaction;
};
let mut changed = Vec::new();
let response = match h.frame_type {
FrameType::Global => match Global::decode(h.command, &frame.payload) {
Ok(Global::ReadAttributes(ids)) => Some(Global::ReadAttributesResponse(
ids.into_iter()
.map(|id| match attributes.get(&id) {
Some(v) => ReadRecord {
attribute: id,
status: status::SUCCESS,
value: Some(v.clone()),
},
None => ReadRecord {
attribute: id,
status: status::UNSUPPORTED_ATTRIBUTE,
value: None,
},
})
.collect(),
)),
Ok(Global::WriteAttributes(values)) => {
let mut failures = Vec::new();
for (id, value) in values {
match attributes.get_mut(&id) {
Some(slot) => *slot = value,
None => failures.push((status::UNSUPPORTED_ATTRIBUTE, Some(id))),
}
}
Some(Global::WriteAttributesResponse(if failures.is_empty() {
vec![(status::SUCCESS, None)]
} else {
failures
}))
}
Ok(Global::ConfigureReporting(configs)) => {
let mut failures = Vec::new();
for c in configs {
if attributes.contains_key(&c.attribute) {
state.reporting.insert((endpoint, cluster, c.attribute), c);
} else {
failures.push(ReportingStatus {
status: status::UNSUPPORTED_ATTRIBUTE,
attribute: Some(c.attribute),
});
}
}
Some(Global::ConfigureReportingResponse(if failures.is_empty() {
vec![ReportingStatus {
status: status::SUCCESS,
attribute: None,
}]
} else {
failures
}))
}
Ok(_) => None,
Err(_) => {
reaction
.replies
.push(reply_to(default_response(status::MALFORMED_COMMAND)));
return reaction;
}
},
FrameType::Cluster => {
let handled = match (cluster, h.command) {
(cluster_id::ON_OFF, 0x00..=0x02) => {
let current = attributes
.get(&attr::on_off::ON_OFF)
.and_then(Value::as_bool)
.unwrap_or(false);
let next = match h.command {
0x00 => false,
0x01 => true,
_ => !current,
};
attributes.insert(attr::on_off::ON_OFF, Value::bool(next));
changed.push(attr::on_off::ON_OFF);
true
}
(cluster_id::IDENTIFY, 0x00) => true,
(cluster_id::IAS_ZONE, 0x00) => {
state.ias_enrolled = true;
if let [_, zone_id, ..] = frame.payload.as_slice() {
attributes.insert(attr::ias_zone::ZONE_ID, Value::u8(*zone_id));
attributes.insert(attr::ias_zone::ZONE_STATE, Value::enum8(1));
}
true
}
_ => false,
};
if !h.disable_default_response {
reaction.replies.push(reply_to(default_response(if handled {
status::SUCCESS
} else {
status::UNSUP_COMMAND
})));
}
None
}
};
if let Some(g) = response {
let mut f = Frame::global(Direction::ServerToClient, h.seq, g.command_id(), g.encode());
f.header.disable_default_response = true;
reaction.replies.push(reply_to(f));
}
for attribute in changed {
reaction
.replies
.extend(self.report(state, endpoint, cluster, attribute));
}
reaction
}
pub fn set_attribute(
&mut self,
state: &mut DeviceState,
endpoint: u8,
cluster: u16,
attribute: u16,
value: Value,
) -> Vec<Outgoing> {
if let Some(attributes) = self
.endpoint_mut(endpoint)
.and_then(|e| e.servers.get_mut(&cluster))
{
attributes.insert(attribute, value);
}
self.report(state, endpoint, cluster, attribute)
}
fn report(
&self,
state: &mut DeviceState,
endpoint: u8,
cluster: u16,
attribute: u16,
) -> Vec<Outgoing> {
if !state
.reporting
.contains_key(&(endpoint, cluster, attribute))
{
return Vec::new();
}
let Some(value) = self
.endpoints
.iter()
.find(|e| e.endpoint == endpoint)
.and_then(|e| e.servers.get(&cluster))
.and_then(|a| a.get(&attribute))
.cloned()
else {
return Vec::new();
};
state
.bindings
.iter()
.filter(|b| b.source_endpoint == endpoint && b.cluster == cluster)
.filter_map(|b| match b.target {
zigbee_zdo::BindTarget::Endpoint { endpoint: dst, .. } => Some(dst),
zigbee_zdo::BindTarget::Group(_) => None,
})
.map(|dst| {
state.seq = state.seq.wrapping_add(1);
let mut f = Frame::global(
Direction::ServerToClient,
state.seq,
foundation::id::REPORT_ATTRIBUTES,
Global::ReportAttributes(vec![(attribute, value.clone())]).encode(),
);
f.header.disable_default_response = true;
Outgoing {
profile: 0x0104,
cluster,
source_endpoint: endpoint,
destination_endpoint: dst,
payload: f.encode(),
}
})
.collect()
}
pub fn announce(&self, nwk: NodeId) -> Outgoing {
let capability = self.node_descriptor().mac_capabilities;
let (cluster, payload) = Message {
tsn: 0x80,
body: Body::DeviceAnnce {
nwk,
ieee: self.ieee,
capability,
},
}
.encode();
Outgoing {
profile: 0,
cluster,
source_endpoint: 0,
destination_endpoint: 0,
payload,
}
}
}