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.
//
// Identify — cluster 0x0003.
//
// 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.

//! Identify (cluster `0x0003`, revision 6).
//!
//! 4 identifiers, 0 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 = 0x0003;

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

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

/// This cluster defines no features, so its `FeatureMap` is always zero.
pub const FEATURES: &[Feature] = &[];

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

    /// `IdentifyTime`.
    pub const IDENTIFY_TIME: AttributeId = 0x00;
    /// `IdentifyType`.
    pub const IDENTIFY_TYPE: AttributeId = 0x01;
}

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

    /// `Identify`.
    pub const IDENTIFY: CommandId = 0x00;
    /// `TriggerEffect`.
    pub const TRIGGER_EFFECT: CommandId = 0x40;
}

/// `EffectIdentifierEnum`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(u8)]
pub enum EffectIdentifierEnum {
    /// e.g., Light is turned on/off once.
    Blink = 0,
    /// e.g., Light is turned on/off over 1 second and repeated 15 times.
    Breathe = 1,
    /// e.g., Colored light turns green for 1 second; non-colored light flashes twice.
    Okay = 2,
    /// e.g., Colored light turns orange for 8 seconds; non-colored light switches to the maximum brightness for 0.5s and then minimum brightness for 7.5s.
    ChannelChange = 11,
    /// Complete the current effect sequence before terminating. e.g., if in the middle of a breathe effect (as above), first complete the current 1s breathe effect and then terminate the effect.
    FinishEffect = 254,
    /// Terminate the effect as soon as possible.
    StopEffect = 255,
}

impl EffectIdentifierEnum {
    /// 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::Blink),
            1 => Some(Self::Breathe),
            2 => Some(Self::Okay),
            11 => Some(Self::ChannelChange),
            254 => Some(Self::FinishEffect),
            255 => Some(Self::StopEffect),
            _ => None,
        }
    }

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

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

impl<'a> FromTlv<'a> for EffectIdentifierEnum {
    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))
    }
}

/// `EffectVariantEnum`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(u8)]
pub enum EffectVariantEnum {
    /// Indicates the default effect is used
    Default = 0,
}

impl EffectVariantEnum {
    /// 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::Default),
            _ => None,
        }
    }

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

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

impl<'a> FromTlv<'a> for EffectVariantEnum {
    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))
    }
}

/// `IdentifyTypeEnum`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(u8)]
pub enum IdentifyTypeEnum {
    /// No presentation.
    None = 0,
    /// Light output of a lighting product.
    LightOutput = 1,
    /// Typically a small LED.
    VisibleIndicator = 2,
    /// `AudibleBeep`.
    AudibleBeep = 3,
    /// Presentation will be visible on display screen.
    Display = 4,
    /// Presentation will be conveyed by actuator functionality such as through a window blind operation or in-wall relay.
    Actuator = 5,
}

impl IdentifyTypeEnum {
    /// 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::None),
            1 => Some(Self::LightOutput),
            2 => Some(Self::VisibleIndicator),
            3 => Some(Self::AudibleBeep),
            4 => Some(Self::Display),
            5 => Some(Self::Actuator),
            _ => None,
        }
    }

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

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

impl<'a> FromTlv<'a> for IdentifyTypeEnum {
    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))
    }
}

/// The fields of `Identify` (command `0x00`).
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct IdentifyFields {
    /// `IdentifyTime`, field 0.
    pub identify_time: u16,
}

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

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

/// The fields of `TriggerEffect` (command `0x40`).
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct TriggerEffectFields {
    /// `EffectIdentifier`, field 0.
    pub effect_identifier: EffectIdentifierEnum,
    /// `EffectVariant`, field 1.
    pub effect_variant: EffectVariantEnum,
}

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

impl<'a> FromTlv<'a> for TriggerEffectFields {
    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 effect_identifier = None;
        let mut effect_variant = 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) => {
                    effect_identifier = Some(<EffectIdentifierEnum>::from_tlv(reader, &field)?)
                }
                Some(1) => effect_variant = Some(<EffectVariantEnum>::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 {
            effect_identifier: effect_identifier
                .ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
            effect_variant: effect_variant
                .ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
        })
    }
}

/// Every attribute the specification defines, with its conformance.
pub const ATTRIBUTES: &[Attribute] = &[
    Attribute {
        id: 0x0000,
        name: "IdentifyTime",
        kind: "uint16",
        access: Access::read_write_with(Privilege::View, Privilege::Operate),
        qualities: AttributeQualities::empty(),
        reporting: Reporting::Quieter,
        conform: Conform::MANDATORY,
    },
    Attribute {
        id: 0x0001,
        name: "IdentifyType",
        kind: "IdentifyTypeEnum",
        access: Access::read_only(Privilege::View),
        qualities: AttributeQualities::empty(),
        reporting: Reporting::OnChange,
        conform: Conform::MANDATORY,
    },
];

/// Every command, with its conformance.
pub const COMMANDS: &[Command] = &[
    Command {
        id: 0x00,
        name: "Identify",
        to_server: true,
        response: None,
        access: Access::invoke(Privilege::Manage),
        conform: Conform::MANDATORY,
    },
    Command {
        id: 0x40,
        name: "TriggerEffect",
        to_server: true,
        response: None,
        access: Access::invoke(Privilege::Manage),
        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: "Identify",
    revision: REVISION,
    pics: "I",
    derived_from: None,
    features: FEATURES,
    attributes: ATTRIBUTES,
    commands: COMMANDS,
    events: EVENTS,
};