use matter_codec::{Tag, Value};
pub(crate) const ICD_MANAGEMENT_CLUSTER: u32 = 0x0046;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum IcdClientType {
Permanent,
Ephemeral,
}
impl IcdClientType {
fn to_u8(self) -> u8 {
match self {
Self::Permanent => 0,
Self::Ephemeral => 1,
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct IcdRegistration {
pub node_id: u64,
pub check_in_node_id: u64,
pub monitored_subject: u64,
pub key: [u8; 16],
pub start_counter: u32,
}
impl IcdRegistration {
#[must_use]
pub fn new(
node_id: u64,
check_in_node_id: u64,
monitored_subject: u64,
key: [u8; 16],
start_counter: u32,
) -> Self {
Self {
node_id,
check_in_node_id,
monitored_subject,
key,
start_counter,
}
}
}
pub(crate) fn register_client_fields(
check_in_node_id: u64,
monitored_subject: u64,
key: &[u8; 16],
client_type: IcdClientType,
) -> Value {
Value::Structure(vec![
(Tag::Context(0), Value::Uint(check_in_node_id)),
(Tag::Context(1), Value::Uint(monitored_subject)),
(Tag::Context(2), Value::Bytes(key.to_vec())),
(Tag::Context(4), Value::Uint(u64::from(client_type.to_u8()))),
])
}
#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used, clippy::expect_used)] use super::*;
#[test]
fn register_client_fields_has_expected_tags() {
let key = [0xABu8; 16];
let v = register_client_fields(1, 2, &key, IcdClientType::Permanent);
let Value::Structure(m) = v else {
panic!("expected struct")
};
assert_eq!(m[0], (Tag::Context(0), Value::Uint(1)));
assert_eq!(m[1], (Tag::Context(1), Value::Uint(2)));
assert_eq!(m[2], (Tag::Context(2), Value::Bytes(key.to_vec())));
assert_eq!(m[3], (Tag::Context(4), Value::Uint(0)));
}
}