use crate::dm::meta::{AttributeDescriptor, AttributeQualities, ClusterDescriptor};
use crate::dm::{Endpoint, Node, Resolved};
use crate::im::{AttributeId, ClusterHandler, ClusterId, EndpointId, InteractionContext, Status};
use crate::tlv::{Tag, TlvWriter};
use super::Cluster;
pub const ID: ClusterId = 0x001D;
pub const REVISION: u16 = 3;
pub const DEVICE_TYPE_LIST: AttributeId = 0x0000;
pub const SERVER_LIST: AttributeId = 0x0001;
pub const CLIENT_LIST: AttributeId = 0x0002;
pub const PARTS_LIST: AttributeId = 0x0003;
pub const TAG_LIST: AttributeId = 0x0004;
pub const ENDPOINT_UNIQUE_ID: AttributeId = 0x0005;
pub const FEATURE_TAG_LIST: u32 = 1 << 0;
pub use crate::dm::DeviceType;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SemanticTag<'a> {
pub mfg_code: Option<crate::msg::VendorId>,
pub namespace_id: u8,
pub tag: u8,
pub label: Option<&'a str>,
}
const BASE_ATTRIBUTES: [AttributeDescriptor; 4] = [
AttributeDescriptor::read_only(DEVICE_TYPE_LIST).with_qualities(AttributeQualities::FIXED),
AttributeDescriptor::read_only(SERVER_LIST).with_qualities(AttributeQualities::FIXED),
AttributeDescriptor::read_only(CLIENT_LIST).with_qualities(AttributeQualities::FIXED),
AttributeDescriptor::read_only(PARTS_LIST),
];
const TAGGED_ATTRIBUTES: [AttributeDescriptor; 5] = [
BASE_ATTRIBUTES[0],
BASE_ATTRIBUTES[1],
BASE_ATTRIBUTES[2],
BASE_ATTRIBUTES[3],
AttributeDescriptor::read_only(TAG_LIST).with_qualities(AttributeQualities::FIXED),
];
const IDENTIFIED_ATTRIBUTES: [AttributeDescriptor; 5] = [
BASE_ATTRIBUTES[0],
BASE_ATTRIBUTES[1],
BASE_ATTRIBUTES[2],
BASE_ATTRIBUTES[3],
AttributeDescriptor::read_only(ENDPOINT_UNIQUE_ID).with_qualities(AttributeQualities::FIXED),
];
const FULL_ATTRIBUTES: [AttributeDescriptor; 6] = [
TAGGED_ATTRIBUTES[0],
TAGGED_ATTRIBUTES[1],
TAGGED_ATTRIBUTES[2],
TAGGED_ATTRIBUTES[3],
TAGGED_ATTRIBUTES[4],
AttributeDescriptor::read_only(ENDPOINT_UNIQUE_ID).with_qualities(AttributeQualities::FIXED),
];
#[must_use]
pub const fn cluster() -> ClusterDescriptor<'static> {
descriptor_with(&BASE_ATTRIBUTES, 0)
}
#[must_use]
pub const fn cluster_with_tags() -> ClusterDescriptor<'static> {
descriptor_with(&TAGGED_ATTRIBUTES, FEATURE_TAG_LIST)
}
#[must_use]
pub const fn cluster_with_unique_id() -> ClusterDescriptor<'static> {
descriptor_with(&IDENTIFIED_ATTRIBUTES, 0)
}
#[must_use]
pub const fn cluster_full() -> ClusterDescriptor<'static> {
descriptor_with(&FULL_ATTRIBUTES, FEATURE_TAG_LIST)
}
const fn descriptor_with(
attributes: &'static [AttributeDescriptor],
feature_map: u32,
) -> ClusterDescriptor<'static> {
ClusterDescriptor {
id: ID,
revision: REVISION,
feature_map,
attributes,
accepted_commands: &[],
generated_commands: &[],
events: &[],
}
}
#[derive(Debug, Clone, Copy)]
pub struct Descriptor<'a> {
pub node: Node<'a>,
pub endpoint: EndpointId,
pub parts: &'a [EndpointId],
pub tags: &'a [SemanticTag<'a>],
pub unique_id: Option<&'a str>,
}
impl<'a> Descriptor<'a> {
#[must_use]
pub const fn new(node: Node<'a>, endpoint: EndpointId) -> Self {
Self {
node,
endpoint,
parts: &[],
tags: &[],
unique_id: None,
}
}
#[must_use]
pub fn device_types(&self) -> &'a [DeviceType] {
match self.node.endpoint(self.endpoint) {
Some(endpoint) => endpoint.device_types,
None => &[],
}
}
#[must_use]
pub fn clients(&self) -> &'a [ClusterId] {
match self.node.endpoint(self.endpoint) {
Some(endpoint) => endpoint.clients,
None => &[],
}
}
#[must_use]
pub const fn with_parts(mut self, parts: &'a [EndpointId]) -> Self {
self.parts = parts;
self
}
#[must_use]
pub const fn with_tags(mut self, tags: &'a [SemanticTag<'a>]) -> Self {
self.tags = tags;
self
}
#[must_use]
pub const fn with_unique_id(mut self, unique_id: &'a str) -> Self {
self.unique_id = Some(unique_id);
self
}
#[must_use]
pub fn descriptor(&self) -> ClusterDescriptor<'static> {
match (!self.tags.is_empty(), self.unique_id.is_some()) {
(false, false) => cluster(),
(true, false) => cluster_with_tags(),
(false, true) => cluster_with_unique_id(),
(true, true) => cluster_full(),
}
}
fn this_endpoint(&self) -> Option<&'a Endpoint<'a>> {
self.node.endpoint(self.endpoint)
}
}
impl ClusterHandler for Descriptor<'_> {
fn read(
&self,
resolved: &Resolved<'_>,
_ctx: &InteractionContext<'_>,
w: &mut TlvWriter<'_>,
tag: Tag,
) -> core::result::Result<(), Status> {
if resolved.endpoint != self.endpoint {
return Err(Status::UnsupportedEndpoint);
}
match resolved.attribute {
DEVICE_TYPE_LIST => {
w.start_array(tag).map_err(|_| Status::ResourceExhausted)?;
for device_type in self.device_types() {
w.start_structure(Tag::Anonymous)
.map_err(|_| Status::ResourceExhausted)?;
w.unsigned(Tag::Context(0), u64::from(device_type.device_type))
.map_err(|_| Status::ResourceExhausted)?;
w.unsigned(Tag::Context(1), u64::from(device_type.revision))
.map_err(|_| Status::ResourceExhausted)?;
w.end_container().map_err(|_| Status::ResourceExhausted)?;
}
w.end_container().map_err(|_| Status::ResourceExhausted)?;
Ok(())
}
SERVER_LIST => {
let endpoint = self.this_endpoint().ok_or(Status::UnsupportedEndpoint)?;
write_cluster_ids(w, tag, endpoint.clusters.iter().map(|c| c.id))
}
CLIENT_LIST => write_cluster_ids(w, tag, self.clients().iter().copied()),
PARTS_LIST => {
w.start_array(tag).map_err(|_| Status::ResourceExhausted)?;
for part in self.parts {
w.unsigned(Tag::Anonymous, u64::from(*part))
.map_err(|_| Status::ResourceExhausted)?;
}
w.end_container().map_err(|_| Status::ResourceExhausted)?;
Ok(())
}
TAG_LIST => {
w.start_array(tag).map_err(|_| Status::ResourceExhausted)?;
for semantic in self.tags {
w.start_structure(Tag::Anonymous)
.map_err(|_| Status::ResourceExhausted)?;
match semantic.mfg_code {
Some(vendor) => w.unsigned(Tag::Context(0), u64::from(vendor.0)),
None => w.null(Tag::Context(0)),
}
.map_err(|_| Status::ResourceExhausted)?;
w.unsigned(Tag::Context(1), u64::from(semantic.namespace_id))
.map_err(|_| Status::ResourceExhausted)?;
w.unsigned(Tag::Context(2), u64::from(semantic.tag))
.map_err(|_| Status::ResourceExhausted)?;
if let Some(label) = semantic.label {
w.utf8(Tag::Context(3), label)
.map_err(|_| Status::ResourceExhausted)?;
}
w.end_container().map_err(|_| Status::ResourceExhausted)?;
}
w.end_container().map_err(|_| Status::ResourceExhausted)?;
Ok(())
}
ENDPOINT_UNIQUE_ID => {
let unique_id = self.unique_id.ok_or(Status::UnsupportedAttribute)?;
w.utf8(tag, unique_id)
.map_err(|_| Status::ResourceExhausted)
}
_ => Err(Status::UnsupportedAttribute),
}
}
}
fn write_cluster_ids(
w: &mut TlvWriter<'_>,
tag: Tag,
ids: impl Iterator<Item = ClusterId>,
) -> core::result::Result<(), Status> {
w.start_array(tag).map_err(|_| Status::ResourceExhausted)?;
for id in ids {
w.unsigned(Tag::Anonymous, u64::from(id))
.map_err(|_| Status::ResourceExhausted)?;
}
w.end_container().map_err(|_| Status::ResourceExhausted)
}
impl Cluster for Descriptor<'_> {
const ID: ClusterId = ID;
}