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

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

/// 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;

    /// `SupportedModes`.
    pub const SUPPORTED_MODES: AttributeId = 0x00;
    /// `CurrentMode`.
    pub const CURRENT_MODE: AttributeId = 0x01;
    /// `StartUpMode`.
    pub const START_UP_MODE: AttributeId = 0x02;
    /// `OnMode`.
    pub const ON_MODE: AttributeId = 0x03;
}

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

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

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

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.mode_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 mode_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) => {
                    mode_tags = Some(<TlvList<'a, ModeTagStruct>>::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))?,
            mode_tags: mode_tags.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
        })
    }
}

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

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

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

/// The fields of `ChangeToModeResponse` (command `0x01`).
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ChangeToModeResponseFields<'a> {
    /// `Status`, field 0.
    pub status: u8,
    /// `StatusText`, field 1.
    pub status_text: &'a str,
}

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

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