use core::cell::Cell;
use crate::dm::Resolved;
use crate::dm::access::{Access, Privilege};
use crate::dm::meta::{
AttributeDescriptor, AttributeQualities, ClusterDescriptor, EventDescriptor, EventPriority,
};
use crate::im::{
AttributeId, ClusterHandler, ClusterId, EventId, InteractionContext, Status, WriteOp,
};
use crate::msg::VendorId;
use crate::tlv::{ContainerKind, Element, Tag, TlvReader, TlvWriter, Value};
use super::Cluster;
pub const ID: ClusterId = 0x0028;
pub const REVISION: u16 = 6;
pub const SPECIFICATION_VERSION: u32 = 0x0106_0000;
#[doc(inline)]
pub use crate::DATA_MODEL_REVISION;
pub const DATA_MODEL_REVISION_ID: AttributeId = 0x0000;
pub const VENDOR_NAME: AttributeId = 0x0001;
pub const VENDOR_ID: AttributeId = 0x0002;
pub const PRODUCT_NAME: AttributeId = 0x0003;
pub const PRODUCT_ID: AttributeId = 0x0004;
pub const NODE_LABEL: AttributeId = 0x0005;
pub const LOCATION: AttributeId = 0x0006;
pub const HARDWARE_VERSION: AttributeId = 0x0007;
pub const HARDWARE_VERSION_STRING: AttributeId = 0x0008;
pub const SOFTWARE_VERSION: AttributeId = 0x0009;
pub const SOFTWARE_VERSION_STRING: AttributeId = 0x000A;
pub const MANUFACTURING_DATE: AttributeId = 0x000B;
pub const PART_NUMBER: AttributeId = 0x000C;
pub const PRODUCT_URL: AttributeId = 0x000D;
pub const PRODUCT_LABEL: AttributeId = 0x000E;
pub const SERIAL_NUMBER: AttributeId = 0x000F;
pub const LOCAL_CONFIG_DISABLED: AttributeId = 0x0010;
pub const REACHABLE: AttributeId = 0x0011;
pub const UNIQUE_ID: AttributeId = 0x0012;
pub const CAPABILITY_MINIMA: AttributeId = 0x0013;
pub const PRODUCT_APPEARANCE: AttributeId = 0x0014;
pub const SPECIFICATION_VERSION_ID: AttributeId = 0x0015;
pub const MAX_PATHS_PER_INVOKE: AttributeId = 0x0016;
pub const CONFIGURATION_VERSION: AttributeId = 0x0018;
pub const START_UP: EventId = 0x00;
pub const SHUT_DOWN: EventId = 0x01;
pub const LEAVE: EventId = 0x02;
pub const REACHABLE_CHANGED: EventId = 0x03;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u8)]
#[non_exhaustive]
pub enum ProductFinish {
Other = 0,
Matte = 1,
Satin = 2,
Polished = 3,
Rugged = 4,
Fabric = 5,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ProductAppearance {
pub finish: ProductFinish,
pub primary_color: Option<u8>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct CapabilityMinima {
pub case_sessions_per_fabric: u16,
pub subscriptions_per_fabric: u16,
pub simultaneous_invocations: Option<u16>,
pub simultaneous_writes: Option<u16>,
pub read_paths: Option<u16>,
pub subscribe_paths: Option<u16>,
}
impl CapabilityMinima {
#[must_use]
pub const fn from_tables<C, S, B>() -> Self
where
C: crate::config::Config,
S: crate::config::Capacity,
B: crate::config::SubscriptionCapacity,
{
Self {
case_sessions_per_fabric: saturate(S::PER_FABRIC),
subscriptions_per_fabric: saturate(B::PER_FABRIC),
simultaneous_invocations: Some(1),
simultaneous_writes: Some(1),
read_paths: Some(saturate(C::READ_PATHS)),
subscribe_paths: Some(saturate(B::PATHS)),
}
}
}
#[expect(
clippy::cast_possible_truncation,
reason = "the branch above is the truncation check — §11.1.4.4 caps every field at 10000"
)]
const fn saturate(n: usize) -> u16 {
if n > 10_000 { 10_000 } else { n as u16 }
}
impl Default for CapabilityMinima {
fn default() -> Self {
Self {
case_sessions_per_fabric: 3,
subscriptions_per_fabric: 3,
simultaneous_invocations: Some(1),
simultaneous_writes: Some(1),
read_paths: Some(9),
subscribe_paths: Some(3),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Product<'a> {
pub vendor_name: &'a str,
pub vendor_id: VendorId,
pub product_name: &'a str,
pub product_id: u16,
pub hardware_version: u16,
pub hardware_version_string: &'a str,
pub software_version: u32,
pub software_version_string: &'a str,
pub unique_id: &'a str,
pub capability_minima: CapabilityMinima,
pub max_paths_per_invoke: u16,
pub manufacturing_date: Option<&'a str>,
pub part_number: Option<&'a str>,
pub product_url: Option<&'a str>,
pub product_label: Option<&'a str>,
pub serial_number: Option<&'a str>,
pub appearance: Option<ProductAppearance>,
pub configuration_version: u32,
}
impl<'a> Product<'a> {
#[must_use]
pub const fn new(
vendor_name: &'a str,
vendor_id: VendorId,
product_name: &'a str,
product_id: u16,
unique_id: &'a str,
) -> Self {
Self {
vendor_name,
vendor_id,
product_name,
product_id,
hardware_version: 0,
hardware_version_string: "0",
software_version: 0,
software_version_string: "0",
unique_id,
capability_minima: CapabilityMinima {
case_sessions_per_fabric: 3,
subscriptions_per_fabric: 3,
simultaneous_invocations: Some(1),
simultaneous_writes: Some(1),
read_paths: Some(9),
subscribe_paths: Some(3),
},
max_paths_per_invoke: 1,
manufacturing_date: None,
part_number: None,
product_url: None,
product_label: None,
serial_number: None,
appearance: None,
configuration_version: 1,
}
}
#[must_use]
pub const fn with_hardware(mut self, version: u16, string: &'a str) -> Self {
self.hardware_version = version;
self.hardware_version_string = string;
self
}
#[must_use]
pub const fn with_software(mut self, version: u32, string: &'a str) -> Self {
self.software_version = version;
self.software_version_string = string;
self
}
#[must_use]
pub const fn with_serial_number(mut self, serial_number: &'a str) -> Self {
self.serial_number = Some(serial_number);
self
}
#[must_use]
pub const fn with_capability_minima(mut self, minima: CapabilityMinima) -> Self {
self.capability_minima = minima;
self
}
#[must_use]
pub const fn with_max_paths_per_invoke(mut self, max: u16) -> Self {
self.max_paths_per_invoke = max;
self
}
}
#[derive(Debug, Default)]
pub struct Location(Cell<[u8; 2]>);
impl Location {
#[must_use]
pub const fn region_agnostic() -> Self {
Self(Cell::new(*b"XX"))
}
#[must_use]
pub fn new(code: &str) -> Option<Self> {
let bytes = code.as_bytes();
match bytes {
[a, b] if a.is_ascii() && b.is_ascii() => Some(Self(Cell::new([*a, *b]))),
_ => None,
}
}
#[must_use]
pub fn get(&self) -> [u8; 2] {
self.0.get()
}
pub fn set(&self, code: &str) -> Result<(), Status> {
match code.as_bytes() {
[a, b] if a.is_ascii() && b.is_ascii() => {
self.0.set([*a, *b]);
Ok(())
}
_ => Err(Status::ConstraintError),
}
}
fn as_str(&self) -> heapless::String<2> {
let bytes = self.0.get();
let mut out = heapless::String::new();
for byte in bytes {
let _ = out.push(byte as char);
}
out
}
}
#[derive(Debug)]
pub struct BasicInformation<'a> {
pub product: &'a Product<'a>,
pub location: &'a Location,
node_label: core::cell::RefCell<heapless::String<32>>,
local_config_disabled: Cell<Option<bool>>,
reachable: Cell<Option<bool>>,
}
impl<'a> BasicInformation<'a> {
#[must_use]
pub fn new(product: &'a Product<'a>, location: &'a Location) -> Self {
Self {
product,
location,
node_label: core::cell::RefCell::new(heapless::String::new()),
local_config_disabled: Cell::new(None),
reachable: Cell::new(None),
}
}
#[must_use]
pub fn with_local_config_disabled(self, initial: bool) -> Self {
self.local_config_disabled.set(Some(initial));
self
}
#[must_use]
pub fn with_reachable(self, initial: bool) -> Self {
self.reachable.set(Some(initial));
self
}
#[must_use]
pub fn node_label(&self) -> heapless::String<32> {
self.node_label.borrow().clone()
}
pub fn set_node_label(&self, label: &str) -> Result<(), Status> {
let mut next = heapless::String::new();
next.push_str(label).map_err(|_| Status::ConstraintError)?;
*self.node_label.borrow_mut() = next;
Ok(())
}
#[must_use]
pub fn reachable(&self) -> Option<bool> {
self.reachable.get()
}
pub fn set_reachable(&self, reachable: bool) -> bool {
match self.reachable.get() {
Some(current) if current == reachable => false,
Some(_) => {
self.reachable.set(Some(reachable));
true
}
None => false,
}
}
#[must_use]
pub fn serves_local_config_disabled(&self) -> bool {
self.local_config_disabled.get().is_some()
}
#[must_use]
pub fn serves_reachable(&self) -> bool {
self.reachable.get().is_some()
}
}
const fn fixed(id: AttributeId) -> AttributeDescriptor {
AttributeDescriptor::read_only(id).with_qualities(AttributeQualities::FIXED)
}
const NODE_LABEL_DESC: AttributeDescriptor = AttributeDescriptor::read_write(NODE_LABEL)
.with_access(Access::read_write_with(Privilege::View, Privilege::Manage))
.with_qualities(AttributeQualities::NON_VOLATILE);
const LOCATION_DESC: AttributeDescriptor = AttributeDescriptor::read_write(LOCATION)
.with_access(Access::read_write_with(
Privilege::View,
Privilege::Administer,
))
.with_qualities(AttributeQualities::NON_VOLATILE);
const LOCAL_CONFIG_DISABLED_DESC: AttributeDescriptor =
AttributeDescriptor::read_write(LOCAL_CONFIG_DISABLED)
.with_access(Access::read_write_with(Privilege::View, Privilege::Manage))
.with_qualities(AttributeQualities::NON_VOLATILE);
const EVENTS: &[EventDescriptor] = &[
EventDescriptor::new(START_UP).with_priority(EventPriority::Critical),
EventDescriptor::new(SHUT_DOWN).with_priority(EventPriority::Critical),
EventDescriptor::new(LEAVE),
];
pub const MAX_ATTRIBUTES: usize = 24;
#[derive(Debug, Clone, Copy)]
pub struct Attributes {
entries: [AttributeDescriptor; MAX_ATTRIBUTES],
len: usize,
}
impl Attributes {
#[must_use]
pub fn new(product: &Product<'_>, local_config_disabled: bool, reachable: bool) -> Self {
let mut this = Self {
entries: [fixed(DATA_MODEL_REVISION_ID); MAX_ATTRIBUTES],
len: 0,
};
this.push(fixed(DATA_MODEL_REVISION_ID));
this.push(fixed(VENDOR_NAME));
this.push(fixed(VENDOR_ID));
this.push(fixed(PRODUCT_NAME));
this.push(fixed(PRODUCT_ID));
this.push(NODE_LABEL_DESC);
this.push(LOCATION_DESC);
this.push(fixed(HARDWARE_VERSION));
this.push(fixed(HARDWARE_VERSION_STRING));
this.push(fixed(SOFTWARE_VERSION));
this.push(fixed(SOFTWARE_VERSION_STRING));
this.push_if(
product.manufacturing_date.is_some(),
fixed(MANUFACTURING_DATE),
);
this.push_if(product.part_number.is_some(), fixed(PART_NUMBER));
this.push_if(product.product_url.is_some(), fixed(PRODUCT_URL));
this.push_if(product.product_label.is_some(), fixed(PRODUCT_LABEL));
this.push_if(product.serial_number.is_some(), fixed(SERIAL_NUMBER));
this.push_if(local_config_disabled, LOCAL_CONFIG_DISABLED_DESC);
this.push_if(reachable, AttributeDescriptor::read_only(REACHABLE));
this.push(fixed(UNIQUE_ID));
this.push(fixed(CAPABILITY_MINIMA));
this.push_if(product.appearance.is_some(), fixed(PRODUCT_APPEARANCE));
this.push(fixed(SPECIFICATION_VERSION_ID));
this.push(fixed(MAX_PATHS_PER_INVOKE));
this.push(
AttributeDescriptor::read_only(CONFIGURATION_VERSION)
.with_qualities(AttributeQualities::NON_VOLATILE),
);
this
}
#[must_use]
pub fn as_slice(&self) -> &[AttributeDescriptor] {
self.entries.get(..self.len).unwrap_or(&[])
}
#[must_use]
pub fn cluster(&self) -> ClusterDescriptor<'_> {
ClusterDescriptor {
id: ID,
revision: REVISION,
feature_map: 0,
attributes: self.as_slice(),
accepted_commands: &[],
generated_commands: &[],
events: EVENTS,
}
}
fn push(&mut self, descriptor: AttributeDescriptor) {
if let Some(slot) = self.entries.get_mut(self.len) {
*slot = descriptor;
self.len = self.len.saturating_add(1);
}
}
fn push_if(&mut self, condition: bool, descriptor: AttributeDescriptor) {
if condition {
self.push(descriptor);
}
}
}
impl ClusterHandler for BasicInformation<'_> {
fn read(
&self,
resolved: &Resolved<'_>,
_ctx: &InteractionContext<'_>,
w: &mut TlvWriter<'_>,
tag: Tag,
) -> Result<(), Status> {
let product = self.product;
let full = |r: crate::error::Result<()>| r.map_err(|_| Status::ResourceExhausted);
match resolved.attribute {
DATA_MODEL_REVISION_ID => full(w.unsigned(tag, u64::from(DATA_MODEL_REVISION))),
VENDOR_NAME => full(w.utf8(tag, product.vendor_name)),
VENDOR_ID => full(w.unsigned(tag, u64::from(product.vendor_id.0))),
PRODUCT_NAME => full(w.utf8(tag, product.product_name)),
PRODUCT_ID => full(w.unsigned(tag, u64::from(product.product_id))),
NODE_LABEL => full(w.utf8(tag, self.node_label().as_str())),
LOCATION => full(w.utf8(tag, self.location.as_str().as_str())),
HARDWARE_VERSION => full(w.unsigned(tag, u64::from(product.hardware_version))),
HARDWARE_VERSION_STRING => full(w.utf8(tag, product.hardware_version_string)),
SOFTWARE_VERSION => full(w.unsigned(tag, u64::from(product.software_version))),
SOFTWARE_VERSION_STRING => full(w.utf8(tag, product.software_version_string)),
MANUFACTURING_DATE => optional_string(w, tag, product.manufacturing_date),
PART_NUMBER => optional_string(w, tag, product.part_number),
PRODUCT_URL => optional_string(w, tag, product.product_url),
PRODUCT_LABEL => optional_string(w, tag, product.product_label),
SERIAL_NUMBER => optional_string(w, tag, product.serial_number),
LOCAL_CONFIG_DISABLED => {
let value = self
.local_config_disabled
.get()
.ok_or(Status::UnsupportedAttribute)?;
full(w.bool(tag, value))
}
REACHABLE => {
let value = self.reachable.get().ok_or(Status::UnsupportedAttribute)?;
full(w.bool(tag, value))
}
UNIQUE_ID => full(w.utf8(tag, product.unique_id)),
CAPABILITY_MINIMA => write_capability_minima(w, tag, &product.capability_minima),
PRODUCT_APPEARANCE => {
let appearance = product.appearance.ok_or(Status::UnsupportedAttribute)?;
full(w.start_structure(tag))?;
full(w.unsigned(Tag::Context(0), appearance.finish as u64))?;
match appearance.primary_color {
Some(color) => full(w.unsigned(Tag::Context(1), u64::from(color)))?,
None => full(w.null(Tag::Context(1)))?,
}
full(w.end_container())
}
SPECIFICATION_VERSION_ID => full(w.unsigned(tag, u64::from(SPECIFICATION_VERSION))),
MAX_PATHS_PER_INVOKE => full(w.unsigned(tag, u64::from(product.max_paths_per_invoke))),
CONFIGURATION_VERSION => {
full(w.unsigned(tag, u64::from(product.configuration_version)))
}
_ => Err(Status::UnsupportedAttribute),
}
}
fn write(
&self,
resolved: &Resolved<'_>,
data: &[u8],
_op: WriteOp,
_ctx: &InteractionContext<'_>,
) -> Result<(), Status> {
match resolved.attribute {
NODE_LABEL => self.set_node_label(read_utf8(data)?),
LOCATION => self.location.set(read_utf8(data)?),
LOCAL_CONFIG_DISABLED => {
if self.local_config_disabled.get().is_none() {
return Err(Status::UnsupportedAttribute);
}
self.local_config_disabled.set(Some(read_bool(data)?));
Ok(())
}
_ => Err(Status::UnsupportedWrite),
}
}
}
fn optional_string(w: &mut TlvWriter<'_>, tag: Tag, value: Option<&str>) -> Result<(), Status> {
let value = value.ok_or(Status::UnsupportedAttribute)?;
w.utf8(tag, value).map_err(|_| Status::ResourceExhausted)
}
fn write_capability_minima(
w: &mut TlvWriter<'_>,
tag: Tag,
minima: &CapabilityMinima,
) -> Result<(), Status> {
let full = |r: crate::error::Result<()>| r.map_err(|_| Status::ResourceExhausted);
full(w.start_structure(tag))?;
full(w.unsigned(Tag::Context(0), u64::from(minima.case_sessions_per_fabric)))?;
full(w.unsigned(Tag::Context(1), u64::from(minima.subscriptions_per_fabric)))?;
for (field, value) in [
(2, minima.simultaneous_invocations),
(3, minima.simultaneous_writes),
(4, minima.read_paths),
(5, minima.subscribe_paths),
] {
if let Some(value) = value {
full(w.unsigned(Tag::Context(field), u64::from(value)))?;
}
}
full(w.end_container())
}
fn read_utf8(data: &[u8]) -> Result<&str, Status> {
let mut reader = TlvReader::new_in(data, ContainerKind::Structure);
match reader.next_element() {
Ok(Some(Element {
value: Value::Utf8(text),
..
})) => Ok(text),
Ok(Some(_)) => Err(Status::InvalidDataType),
_ => Err(Status::InvalidDataType),
}
}
fn read_bool(data: &[u8]) -> Result<bool, Status> {
let mut reader = TlvReader::new_in(data, ContainerKind::Structure);
match reader.next_element() {
Ok(Some(Element {
value: Value::Bool(value),
..
})) => Ok(value),
Ok(Some(_)) => Err(Status::InvalidDataType),
_ => Err(Status::InvalidDataType),
}
}
impl Cluster for BasicInformation<'_> {
const ID: ClusterId = ID;
}