pub mod access_control;
#[cfg(feature = "rustcrypto")]
#[cfg_attr(docsrs, doc(cfg(feature = "rustcrypto")))]
pub mod administrator_commissioning;
pub mod basic_information;
pub mod binding;
pub mod bridged_device_basic_information;
pub mod commissioner_control;
pub mod descriptor;
pub mod device_energy_management;
pub mod electrical_measurement;
pub mod energy_evse;
pub mod ethernet_network_diagnostics;
pub mod general_commissioning;
pub mod general_diagnostics;
pub mod generated;
#[cfg(feature = "rustcrypto")]
#[cfg_attr(docsrs, doc(cfg(feature = "rustcrypto")))]
pub mod group_key_management;
#[cfg(all(feature = "rustcrypto", feature = "provisional"))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "rustcrypto", feature = "provisional"))))]
pub mod groupcast;
pub mod groups;
#[cfg(feature = "rustcrypto")]
#[cfg_attr(docsrs, doc(cfg(feature = "rustcrypto")))]
pub mod icd_management;
pub mod identify;
#[cfg(feature = "rustcrypto")]
#[cfg_attr(docsrs, doc(cfg(feature = "rustcrypto")))]
pub mod joint_fabric_administrator;
#[cfg(feature = "rustcrypto")]
#[cfg_attr(docsrs, doc(cfg(feature = "rustcrypto")))]
pub mod joint_fabric_datastore;
pub mod label;
pub mod level_control;
pub mod mode;
pub mod network_commissioning;
pub mod on_off;
#[cfg(feature = "rustcrypto")]
#[cfg_attr(docsrs, doc(cfg(feature = "rustcrypto")))]
pub mod operational_credentials;
pub mod ota_provider;
pub mod ota_requestor;
pub mod scenes;
pub mod software_diagnostics;
pub mod thermostat_suggestions;
pub mod thread_network_diagnostics;
#[cfg(feature = "rustcrypto")]
#[cfg_attr(docsrs, doc(cfg(feature = "rustcrypto")))]
pub mod tls;
pub mod water_heater_management;
pub mod wi_fi_network_diagnostics;
#[cfg(feature = "rustcrypto")]
pub use administrator_commissioning::AdministratorCommissioning;
pub use basic_information::BasicInformation;
pub use descriptor::Descriptor;
pub use general_commissioning::GeneralCommissioning;
pub use network_commissioning::NetworkCommissioning;
#[cfg(feature = "rustcrypto")]
pub use operational_credentials::OperationalCredentials;
use crate::dm::{Resolved, ResolvedCommand};
use crate::im::{
ClusterHandler, ClusterId, CommandId, InteractionContext, Status, StatusIb, WriteOp,
};
use crate::tlv::{ContainerKind, Element, FromTlv, Tag, TlvReader, TlvWriter};
pub(crate) fn decode_fields<'a, T: FromTlv<'a>>(fields: &'a [u8]) -> Result<T, StatusIb> {
let mut reader = TlvReader::new_in(fields, ContainerKind::Structure);
let element: Element<'a> = reader
.next_element()
.map_err(|_| StatusIb::from(Status::InvalidAction))?
.ok_or(StatusIb::from(Status::InvalidAction))?;
T::from_tlv(&mut reader, &element).map_err(|error| {
StatusIb::from(match error.code() {
crate::ErrorCode::TlvNotFound => Status::InvalidCommand,
crate::ErrorCode::TlvOutOfRange | crate::ErrorCode::TlvWrongType => {
Status::ConstraintError
}
_ => Status::InvalidAction,
})
})
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Reading<T> {
Value(T),
NotOperational,
#[default]
Unsupported,
}
impl<T> Reading<T> {
#[must_use]
pub const fn is_supported(&self) -> bool {
!matches!(self, Self::Unsupported)
}
}
pub trait Cluster: ClusterHandler {
const ID: ClusterId;
}
impl<T: Cluster + ?Sized> Cluster for &T {
const ID: ClusterId = T::ID;
}
const UNMATCHED: Status = Status::UnsupportedCluster;
macro_rules! dispatch_tuple {
($($name:ident => $slot:ident),+ $(,)?) => {
impl<$($name: Cluster),+> ClusterHandler for ($($name,)+) {
fn read(
&self,
resolved: &Resolved<'_>,
ctx: &InteractionContext<'_>,
w: &mut TlvWriter<'_>,
tag: Tag,
) -> core::result::Result<(), Status> {
let ($($slot,)+) = self;
match resolved.cluster.id {
$(id if id == $name::ID => $slot.read(resolved, ctx, w, tag),)+
_ => Err(UNMATCHED),
}
}
fn data_version(&self, resolved: &Resolved<'_>) -> Option<u32> {
let ($($slot,)+) = self;
match resolved.cluster.id {
$(id if id == $name::ID => $slot.data_version(resolved),)+
_ => None,
}
}
fn write(
&self,
resolved: &Resolved<'_>,
data: &[u8],
op: WriteOp,
ctx: &InteractionContext<'_>,
) -> core::result::Result<(), Status> {
let ($($slot,)+) = self;
match resolved.cluster.id {
$(id if id == $name::ID => $slot.write(resolved, data, op, ctx),)+
_ => Err(UNMATCHED),
}
}
fn invoke(
&self,
resolved: &ResolvedCommand<'_>,
fields: Option<&[u8]>,
ctx: &InteractionContext<'_>,
w: &mut TlvWriter<'_>,
tag: Tag,
) -> core::result::Result<Option<CommandId>, StatusIb> {
let ($($slot,)+) = self;
match resolved.cluster.id {
$(id if id == $name::ID => $slot.invoke(resolved, fields, ctx, w, tag),)+
_ => Err(UNMATCHED.into()),
}
}
fn on_lifecycle(&self, event: crate::im::Lifecycle) {
let ($($slot,)+) = self;
$($slot.on_lifecycle(event);)+
}
}
};
}
dispatch_tuple!(A => a);
dispatch_tuple!(A => a, B => b);
dispatch_tuple!(A => a, B => b, C => c);
dispatch_tuple!(A => a, B => b, C => c, D => d);
dispatch_tuple!(A => a, B => b, C => c, D => d, E => e);
dispatch_tuple!(A => a, B => b, C => c, D => d, E => e, F => f);
dispatch_tuple!(A => a, B => b, C => c, D => d, E => e, F => f, G => g);
dispatch_tuple!(A => a, B => b, C => c, D => d, E => e, F => f, G => g, H => h);
dispatch_tuple!(A => a, B => b, C => c, D => d, E => e, F => f, G => g, H => h, I => i);
dispatch_tuple!(A => a, B => b, C => c, D => d, E => e, F => f, G => g, H => h, I => i, J => j);
dispatch_tuple!(A => a, B => b, C => c, D => d, E => e, F => f, G => g, H => h, I => i, J => j, K => k);
dispatch_tuple!(A => a, B => b, C => c, D => d, E => e, F => f, G => g, H => h, I => i, J => j, K => k, L => l);
#[derive(Debug, Clone, Copy)]
pub struct At<H> {
pub endpoint: crate::im::EndpointId,
pub handler: H,
}
impl<H> At<H> {
pub const fn new(endpoint: crate::im::EndpointId, handler: H) -> Self {
Self { endpoint, handler }
}
}
#[derive(Debug, Clone, Copy)]
pub struct Endpoints<T>(pub T);
const NO_ENDPOINT: Status = Status::UnsupportedEndpoint;
macro_rules! endpoint_tuple {
($($name:ident => $slot:ident),+ $(,)?) => {
impl<$($name: ClusterHandler),+> ClusterHandler for Endpoints<($(At<$name>,)+)> {
fn read(
&self,
resolved: &Resolved<'_>,
ctx: &InteractionContext<'_>,
w: &mut TlvWriter<'_>,
tag: Tag,
) -> core::result::Result<(), Status> {
let Endpoints(($($slot,)+)) = self;
$(if $slot.endpoint == resolved.endpoint {
return $slot.handler.read(resolved, ctx, w, tag);
})+
Err(NO_ENDPOINT)
}
fn data_version(&self, resolved: &Resolved<'_>) -> Option<u32> {
let Endpoints(($($slot,)+)) = self;
$(if $slot.endpoint == resolved.endpoint {
return $slot.handler.data_version(resolved);
})+
None
}
fn write(
&self,
resolved: &Resolved<'_>,
data: &[u8],
op: WriteOp,
ctx: &InteractionContext<'_>,
) -> core::result::Result<(), Status> {
let Endpoints(($($slot,)+)) = self;
$(if $slot.endpoint == resolved.endpoint {
return $slot.handler.write(resolved, data, op, ctx);
})+
Err(NO_ENDPOINT)
}
fn invoke(
&self,
resolved: &ResolvedCommand<'_>,
fields: Option<&[u8]>,
ctx: &InteractionContext<'_>,
w: &mut TlvWriter<'_>,
tag: Tag,
) -> core::result::Result<Option<CommandId>, StatusIb> {
let Endpoints(($($slot,)+)) = self;
$(if $slot.endpoint == resolved.endpoint {
return $slot.handler.invoke(resolved, fields, ctx, w, tag);
})+
Err(NO_ENDPOINT.into())
}
fn on_lifecycle(&self, event: crate::im::Lifecycle) {
let Endpoints(($($slot,)+)) = self;
$($slot.handler.on_lifecycle(event);)+
}
}
};
}
endpoint_tuple!(A => a);
endpoint_tuple!(A => a, B => b);
endpoint_tuple!(A => a, B => b, C => c);
endpoint_tuple!(A => a, B => b, C => c, D => d);
endpoint_tuple!(A => a, B => b, C => c, D => d, E => e);
endpoint_tuple!(A => a, B => b, C => c, D => d, E => e, F => f);
endpoint_tuple!(A => a, B => b, C => c, D => d, E => e, F => f, G => g);
endpoint_tuple!(A => a, B => b, C => c, D => d, E => e, F => f, G => g, H => h);
pub fn validate_endpoint(
endpoint: &crate::dm::Endpoint<'_>,
device_type: &crate::dm::device::DeviceType,
found: impl FnMut(crate::dm::device::Defect),
) {
device_type.validate(endpoint, generated::find, found);
}
#[must_use]
pub fn endpoint_satisfies(
endpoint: &crate::dm::Endpoint<'_>,
device_type: &crate::dm::device::DeviceType,
) -> bool {
device_type.is_satisfied_by(endpoint, generated::find)
}