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.
//
// Dishwasher Alarm — cluster 0x005D.
//
// 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.

//! Dishwasher Alarm (cluster `0x005D`, revision 1).
//!
//! 7 identifiers, 1 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 = 0x005D;

/// 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 = "DISHALM";

/// 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 the ability to reset alarms
    ///
    /// Code `RESET`, bit 0.
    pub const RESET: u32 = 1 << 0;
}

/// Every feature bit, with its conformance.
pub const FEATURES: &[Feature] = &[Feature {
    bit: 0,
    code: "RESET",
    name: "Reset",
    conform: Conform::OPTIONAL,
}];

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

    /// `Mask`.
    pub const MASK: AttributeId = 0x00;
    /// `Latch`.
    pub const LATCH: AttributeId = 0x01;
    /// `State`.
    pub const STATE: AttributeId = 0x02;
    /// `Supported`.
    pub const SUPPORTED: AttributeId = 0x03;
}

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

    /// `Reset`.
    pub const RESET: CommandId = 0x00;
    /// `ModifyEnabledAlarms`.
    pub const MODIFY_ENABLED_ALARMS: CommandId = 0x01;
}

/// The event ids.
pub mod event {
    use crate::im::EventId;

    /// `Notify`.
    pub const NOTIFY: EventId = 0x00;
}

bitflags::bitflags! {
    /// `AlarmBitmap`.
    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
    pub struct AlarmBitmap: u8 {
        /// Water inflow is abnormal
        const INFLOW_ERROR = 1 << 0;
        /// Water draining is abnormal
        const DRAIN_ERROR = 1 << 1;
        /// Door or door lock is abnormal
        const DOOR_ERROR = 1 << 2;
        /// Unable to reach normal temperature
        const TEMP_TOO_LOW = 1 << 3;
        /// Temperature is too high
        const TEMP_TOO_HIGH = 1 << 4;
        /// Water level is abnormal
        const WATER_LEVEL_ERROR = 1 << 5;
    }
}

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

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

/// The fields of `Reset` (command `0x00`).
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ResetFields {
    /// `Alarms`, field 0.
    pub alarms: AlarmBitmap,
}

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

impl<'a> FromTlv<'a> for ResetFields {
    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 alarms = 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) => alarms = Some(<AlarmBitmap>::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 {
            alarms: alarms.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
        })
    }
}

/// The fields of `ModifyEnabledAlarms` (command `0x01`).
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ModifyEnabledAlarmsFields {
    /// `Mask`, field 0.
    pub mask: AlarmBitmap,
}

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

impl<'a> FromTlv<'a> for ModifyEnabledAlarmsFields {
    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 mask = 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) => mask = Some(<AlarmBitmap>::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 {
            mask: mask.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
        })
    }
}

/// Every attribute the specification defines, with its conformance.
pub const ATTRIBUTES: &[Attribute] = &[
    Attribute {
        id: 0x0000,
        name: "Mask",
        kind: "AlarmBitmap",
        access: Access::read_only(Privilege::View),
        qualities: AttributeQualities::empty(),
        reporting: Reporting::OnChange,
        conform: Conform::MANDATORY,
    },
    Attribute {
        id: 0x0001,
        name: "Latch",
        kind: "AlarmBitmap",
        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: "State",
        kind: "AlarmBitmap",
        access: Access::read_only(Privilege::View),
        qualities: AttributeQualities::empty(),
        reporting: Reporting::OnChange,
        conform: Conform::MANDATORY,
    },
    Attribute {
        id: 0x0003,
        name: "Supported",
        kind: "AlarmBitmap",
        access: Access::read_only(Privilege::View),
        qualities: AttributeQualities::FIXED,
        reporting: Reporting::OnChange,
        conform: Conform::MANDATORY,
    },
];

/// Every command, with its conformance.
pub const COMMANDS: &[Command] = &[
    Command {
        id: 0x00,
        name: "Reset",
        to_server: true,
        response: None,
        access: Access::invoke(Privilege::Operate),
        conform: Conform {
            clauses: &[Clause {
                verdict: Conformance::Mandatory,
                when: Condition::Feature(0),
            }],
        },
    },
    Command {
        id: 0x01,
        name: "ModifyEnabledAlarms",
        to_server: true,
        response: None,
        access: Access::invoke(Privilege::Operate),
        conform: Conform::OPTIONAL,
    },
];

/// Every event, with its conformance.
pub const EVENTS: &[Event] = &[Event {
    id: 0x00,
    name: "Notify",
    priority: EventPriority::Info,
    access: Access::read_only(Privilege::View),
    conform: Conform::MANDATORY,
}];

/// The whole cluster, as the specification defines it.
pub const CLUSTER: Cluster = Cluster {
    id: ID,
    name: "Dishwasher Alarm",
    revision: REVISION,
    pics: "DISHALM",
    derived_from: Some("Alarm Base"),
    features: FEATURES,
    attributes: ATTRIBUTES,
    commands: COMMANDS,
    events: EVENTS,
};