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.
//
// Alarm Base — cluster 0xFFFF0001.
//
// 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.

//! Alarm Base (cluster `0xFFFF0001`, revision 2).
//!
//! 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 = 0xFFFF0001;

/// The highest revision the specification's history defines — what `ClusterRevision`
/// must report (Core §7.13.1).
pub const REVISION: u16 = 2;

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

/// 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 {
    }
}

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))?,
        })
    }
}