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.
//
// Mode Select โ€” cluster 0x0050.
//
// 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.

//! Mode Select (cluster `0x0050`, 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 = 0x0050;

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

/// 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 {
    /// Dependency with the OnOff cluster
    ///
    /// Code `DEPONOFF`, bit 0.
    pub const ON_OFF: u32 = 1 << 0;
}

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

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

    /// `Description`.
    pub const DESCRIPTION: AttributeId = 0x00;
    /// `StandardNamespace`.
    pub const STANDARD_NAMESPACE: AttributeId = 0x01;
    /// `SupportedModes`.
    pub const SUPPORTED_MODES: AttributeId = 0x02;
    /// `CurrentMode`.
    pub const CURRENT_MODE: AttributeId = 0x03;
    /// `StartUpMode`.
    pub const START_UP_MODE: AttributeId = 0x04;
    /// `OnMode`.
    pub const ON_MODE: AttributeId = 0x05;
}

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

    /// `ChangeToMode`.
    pub const CHANGE_TO_MODE: CommandId = 0x00;
}

/// `ModeOptionStruct`.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ModeOptionStruct<'a> {
    /// `Label`, field 0.
    pub label: &'a str,
    /// `Mode`, field 1.
    pub mode: u8,
    /// `SemanticTags`, field 2.
    pub semantic_tags: TlvList<'a, SemanticTagStruct>,
}

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

impl<'a> FromTlv<'a> for ModeOptionStruct<'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 label = None;
        let mut mode = None;
        let mut semantic_tags = 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) => label = Some(<&'a str>::from_tlv(reader, &field)?),
                Some(1) => mode = Some(<u8>::from_tlv(reader, &field)?),
                Some(2) => {
                    semantic_tags =
                        Some(<TlvList<'a, SemanticTagStruct>>::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 {
            label: label.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
            mode: mode.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
            semantic_tags: semantic_tags.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
        })
    }
}

/// `SemanticTagStruct`.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct SemanticTagStruct {
    /// `MfgCode`, field 0.
    pub mfg_code: crate::msg::VendorId,
    /// `Value`, field 1.
    pub value: u16,
}

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

impl<'a> FromTlv<'a> for SemanticTagStruct {
    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 mfg_code = None;
        let mut 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) => mfg_code = Some(<crate::msg::VendorId>::from_tlv(reader, &field)?),
                Some(1) => value = 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 {
            mfg_code: mfg_code.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
            value: value.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
        })
    }
}

/// The fields of `ChangeToMode` (command `0x00`).
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ChangeToModeFields {
    /// `NewMode`, field 0.
    pub new_mode: u8,
}

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

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

/// Every attribute the specification defines, with its conformance.
pub const ATTRIBUTES: &[Attribute] = &[
    Attribute {
        id: 0x0000,
        name: "Description",
        kind: "string",
        access: Access::read_only(Privilege::View),
        qualities: AttributeQualities::FIXED,
        reporting: Reporting::OnChange,
        conform: Conform::MANDATORY,
    },
    Attribute {
        id: 0x0001,
        name: "StandardNamespace",
        kind: "enum16",
        access: Access::read_only(Privilege::View),
        qualities: AttributeQualities::NULLABLE.union(AttributeQualities::FIXED),
        reporting: Reporting::OnChange,
        conform: Conform::MANDATORY,
    },
    Attribute {
        id: 0x0002,
        name: "SupportedModes",
        kind: "list[ModeOptionStruct]",
        access: Access::read_only(Privilege::View),
        qualities: AttributeQualities::FIXED,
        reporting: Reporting::OnChange,
        conform: Conform::MANDATORY,
    },
    Attribute {
        id: 0x0003,
        name: "CurrentMode",
        kind: "uint8",
        access: Access::read_only(Privilege::View),
        qualities: AttributeQualities::NON_VOLATILE,
        reporting: Reporting::OnChange,
        conform: Conform::MANDATORY,
    },
    Attribute {
        id: 0x0004,
        name: "StartUpMode",
        kind: "uint8",
        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: "OnMode",
        kind: "uint8",
        access: Access::read_write_with(Privilege::View, Privilege::Operate),
        qualities: AttributeQualities::NULLABLE.union(AttributeQualities::NON_VOLATILE),
        reporting: Reporting::OnChange,
        conform: Conform {
            clauses: &[Clause {
                verdict: Conformance::Mandatory,
                when: Condition::Feature(0),
            }],
        },
    },
];

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

/// 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: "Mode Select",
    revision: REVISION,
    pics: "MOD",
    derived_from: None,
    features: FEATURES,
    attributes: ATTRIBUTES,
    commands: COMMANDS,
    events: EVENTS,
};