matter-kit 0.3.0

Matter (CSA smart-home standard) protocol implementation in Rust: no_std, no-alloc, executor-agnostic
Documentation
// Generated by `cargo xtask` from the CSA data model 1.6. Do not edit.
//
// Water Tank Level Monitoring โ€” cluster 0x0079.
//
// Regenerate with `cargo xtask clusters --dm <checkout>/data_model/1.6`; `cargo xtask check`
// is the CI gate that keeps this file honest. The XML itself is not vendored here โ€” it
// carries the CSA's own copyright notice and is not redistributable.

//! Water Tank Level Monitoring (cluster `0x0079`, revision 1).
//!
//! 7 identifiers, 3 feature bits, and the specification's own conformance for
//! every element. `CLUSTER` is the whole table.

#![allow(unused_imports)]

use crate::dm::conformance::{Clause, Condition, Conform, Conformance};
use crate::dm::meta::{EventPriority, Reporting};
use crate::dm::spec::{Attribute, Cluster, Command, Event, Feature};
use crate::dm::{Access, AccessQualities, AttributeQualities, Privilege};
use crate::error::Result;
use crate::im::{AttributeId, ClusterId, CommandId, EventId};
use crate::tlv::{
    ContainerKind, Element, FromTlv, Nullable, Tag, TlvList, TlvReader, TlvWriter, ToTlv, Value,
};

/// The cluster id.
pub const ID: ClusterId = 0x0079;

/// The highest revision the specification's history defines โ€” what `ClusterRevision`
/// must report (Core ยง7.13.1).
pub const REVISION: u16 = 1;

/// The PICS code, which a certification declaration names the cluster by.
pub const PICS: &str = "REPM";

/// The `FeatureMap` bits (Core ยง7.13.2).
///
/// A feature has conformance of its own: a device may not claim one the
/// specification forbids it alongside another.
pub mod feature {
    /// Supports monitoring the condition of the resource in percentage
    ///
    /// Code `CON`, bit 0.
    pub const CONDITION: u32 = 1 << 0;
    /// Supports warning indication
    ///
    /// Code `WRN`, bit 1.
    pub const WARNING: u32 = 1 << 1;
    /// Supports specifying the list of replacement products
    ///
    /// Code `REP`, bit 2.
    pub const REPLACEMENT_PRODUCT_LIST: u32 = 1 << 2;
}

/// Every feature bit, with its conformance.
pub const FEATURES: &[Feature] = &[
    Feature {
        bit: 0,
        code: "CON",
        name: "Condition",
        conform: Conform::OPTIONAL,
    },
    Feature {
        bit: 1,
        code: "WRN",
        name: "Warning",
        conform: Conform::OPTIONAL,
    },
    Feature {
        bit: 2,
        code: "REP",
        name: "ReplacementProductList",
        conform: Conform::OPTIONAL,
    },
];

/// The attribute ids.
pub mod attribute {
    use crate::im::AttributeId;

    /// `Condition`.
    pub const CONDITION: AttributeId = 0x00;
    /// `DegradationDirection`.
    pub const DEGRADATION_DIRECTION: AttributeId = 0x01;
    /// `ChangeIndication`.
    pub const CHANGE_INDICATION: AttributeId = 0x02;
    /// `InPlaceIndicator`.
    pub const IN_PLACE_INDICATOR: AttributeId = 0x03;
    /// `LastChangedTime`.
    pub const LAST_CHANGED_TIME: AttributeId = 0x04;
    /// `ReplacementProductList`.
    pub const REPLACEMENT_PRODUCT_LIST: AttributeId = 0x05;
}

/// The command ids.
pub mod command {
    use crate::im::CommandId;

    /// `ResetCondition`.
    pub const RESET_CONDITION: CommandId = 0x00;
}

/// `ChangeIndicationEnum`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(u8)]
pub enum ChangeIndicationEnum {
    /// Resource is in good condition, no intervention required
    OK = 0,
    /// Resource will be exhausted soon, intervention will shortly be required
    Warning = 1,
    /// Resource is exhausted, immediate intervention is required
    Critical = 2,
}

impl ChangeIndicationEnum {
    /// Reads a wire value, or `None` for one this revision does not define.
    #[must_use]
    pub const fn from_value(value: u8) -> Option<Self> {
        match value {
            0 => Some(Self::OK),
            1 => Some(Self::Warning),
            2 => Some(Self::Critical),
            _ => None,
        }
    }

    /// Its wire value.
    #[must_use]
    pub const fn value(self) -> u8 {
        self as u8
    }
}

impl ToTlv for ChangeIndicationEnum {
    fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
        self.value().to_tlv(w, tag)
    }
}

impl<'a> FromTlv<'a> for ChangeIndicationEnum {
    fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
        let value = <u8 as FromTlv<'a>>::from_tlv(reader, element)?;
        Self::from_value(value).ok_or(crate::Error::new(crate::ErrorCode::TlvOutOfRange))
    }
}

/// `DegradationDirectionEnum`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(u8)]
pub enum DegradationDirectionEnum {
    /// The degradation of the resource is indicated by an upwards moving/increasing value
    Up = 0,
    /// The degradation of the resource is indicated by a downwards moving/decreasing value
    Down = 1,
}

impl DegradationDirectionEnum {
    /// Reads a wire value, or `None` for one this revision does not define.
    #[must_use]
    pub const fn from_value(value: u8) -> Option<Self> {
        match value {
            0 => Some(Self::Up),
            1 => Some(Self::Down),
            _ => None,
        }
    }

    /// Its wire value.
    #[must_use]
    pub const fn value(self) -> u8 {
        self as u8
    }
}

impl ToTlv for DegradationDirectionEnum {
    fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
        self.value().to_tlv(w, tag)
    }
}

impl<'a> FromTlv<'a> for DegradationDirectionEnum {
    fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
        let value = <u8 as FromTlv<'a>>::from_tlv(reader, element)?;
        Self::from_value(value).ok_or(crate::Error::new(crate::ErrorCode::TlvOutOfRange))
    }
}

/// `ProductIdentifierTypeEnum`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(u8)]
pub enum ProductIdentifierTypeEnum {
    /// 12-digit Universal Product Code
    UPC = 0,
    /// 8-digit Global Trade Item Number
    GTIN8 = 1,
    /// 13-digit European Article Number
    EAN = 2,
    /// 14-digit Global Trade Item Number
    GTIN14 = 3,
    /// Original Equipment Manufacturer part number
    OEM = 4,
}

impl ProductIdentifierTypeEnum {
    /// Reads a wire value, or `None` for one this revision does not define.
    #[must_use]
    pub const fn from_value(value: u8) -> Option<Self> {
        match value {
            0 => Some(Self::UPC),
            1 => Some(Self::GTIN8),
            2 => Some(Self::EAN),
            3 => Some(Self::GTIN14),
            4 => Some(Self::OEM),
            _ => None,
        }
    }

    /// Its wire value.
    #[must_use]
    pub const fn value(self) -> u8 {
        self as u8
    }
}

impl ToTlv for ProductIdentifierTypeEnum {
    fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
        self.value().to_tlv(w, tag)
    }
}

impl<'a> FromTlv<'a> for ProductIdentifierTypeEnum {
    fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
        let value = <u8 as FromTlv<'a>>::from_tlv(reader, element)?;
        Self::from_value(value).ok_or(crate::Error::new(crate::ErrorCode::TlvOutOfRange))
    }
}

/// `ReplacementProductStruct`.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ReplacementProductStruct<'a> {
    /// `ProductIdentifierType`, field 0.
    pub product_identifier_type: ProductIdentifierTypeEnum,
    /// `ProductIdentifierValue`, field 1.
    pub product_identifier_value: &'a str,
}

impl<'a> ToTlv for ReplacementProductStruct<'a> {
    fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
        w.start_structure(tag)?;
        self.product_identifier_type.to_tlv(w, Tag::Context(0))?;
        self.product_identifier_value.to_tlv(w, Tag::Context(1))?;
        w.end_container()
    }
}

impl<'a> FromTlv<'a> for ReplacementProductStruct<'a> {
    fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
        if element.value.container() != Some(ContainerKind::Structure) {
            return Err(crate::Error::new(crate::ErrorCode::TlvWrongType));
        }
        let mut product_identifier_type = None;
        let mut product_identifier_value = None;
        loop {
            let Some(field) = reader.next_element()? else {
                return Err(crate::Error::new(crate::ErrorCode::TlvTruncated));
            };
            if field.value == Value::EndOfContainer {
                break;
            }
            match field.tag.context() {
                Some(0) => {
                    product_identifier_type =
                        Some(<ProductIdentifierTypeEnum>::from_tlv(reader, &field)?)
                }
                Some(1) => product_identifier_value = Some(<&'a str>::from_tlv(reader, &field)?),
                // A field this revision does not define. ยง7.19.2: a receiver
                // ignores what it does not know rather than refusing the whole
                // structure, which is what lets a newer peer talk to an older one.
                _ => reader.skip_value(&field)?,
            }
        }
        Ok(Self {
            product_identifier_type: product_identifier_type
                .ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
            product_identifier_value: product_identifier_value
                .ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
        })
    }
}

/// Every attribute the specification defines, with its conformance.
pub const ATTRIBUTES: &[Attribute] = &[
    Attribute {
        id: 0x0000,
        name: "Condition",
        kind: "percent",
        access: Access::read_only(Privilege::View),
        qualities: AttributeQualities::empty(),
        reporting: Reporting::OnChange,
        conform: Conform {
            clauses: &[Clause {
                verdict: Conformance::Mandatory,
                when: Condition::Feature(0),
            }],
        },
    },
    Attribute {
        id: 0x0001,
        name: "DegradationDirection",
        kind: "DegradationDirectionEnum",
        access: Access::read_only(Privilege::View),
        qualities: AttributeQualities::FIXED,
        reporting: Reporting::OnChange,
        conform: Conform {
            clauses: &[Clause {
                verdict: Conformance::Mandatory,
                when: Condition::Feature(0),
            }],
        },
    },
    Attribute {
        id: 0x0002,
        name: "ChangeIndication",
        kind: "ChangeIndicationEnum",
        access: Access::read_only(Privilege::View),
        qualities: AttributeQualities::empty(),
        reporting: Reporting::OnChange,
        conform: Conform::MANDATORY,
    },
    Attribute {
        id: 0x0003,
        name: "InPlaceIndicator",
        kind: "bool",
        access: Access::read_only(Privilege::View),
        qualities: AttributeQualities::empty(),
        reporting: Reporting::OnChange,
        conform: Conform::OPTIONAL,
    },
    Attribute {
        id: 0x0004,
        name: "LastChangedTime",
        kind: "epoch-s",
        access: Access::read_write_with(Privilege::View, Privilege::Operate),
        qualities: AttributeQualities::NULLABLE.union(AttributeQualities::NON_VOLATILE),
        reporting: Reporting::OnChange,
        conform: Conform::OPTIONAL,
    },
    Attribute {
        id: 0x0005,
        name: "ReplacementProductList",
        kind: "list[ReplacementProductStruct]",
        access: Access::read_only(Privilege::View),
        qualities: AttributeQualities::FIXED,
        reporting: Reporting::OnChange,
        conform: Conform {
            clauses: &[Clause {
                verdict: Conformance::Mandatory,
                when: Condition::Feature(2),
            }],
        },
    },
];

/// Every command, with its conformance.
pub const COMMANDS: &[Command] = &[Command {
    id: 0x00,
    name: "ResetCondition",
    to_server: true,
    response: None,
    access: Access::invoke(Privilege::Operate),
    conform: Conform::OPTIONAL,
}];

/// Every event, with its conformance.
pub const EVENTS: &[Event] = &[];

/// The whole cluster, as the specification defines it.
pub const CLUSTER: Cluster = Cluster {
    id: ID,
    name: "Water Tank Level Monitoring",
    revision: REVISION,
    pics: "REPM",
    derived_from: None,
    features: FEATURES,
    attributes: ATTRIBUTES,
    commands: COMMANDS,
    events: EVENTS,
};