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.
//
// Content App Observer — cluster 0x0510.
//
// 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.

//! Content App Observer (cluster `0x0510`, revision 1).
//!
//! 2 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 = 0x0510;

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

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

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

    /// `ContentAppMessage`.
    pub const CONTENT_APP_MESSAGE: CommandId = 0x00;
    /// `ContentAppMessageResponse`.
    pub const CONTENT_APP_MESSAGE_RESPONSE: CommandId = 0x01;
}

/// `StatusEnum`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(u8)]
pub enum StatusEnum {
    /// Command succeeded
    Success = 0,
    /// Data field in command was not understood by the Observer
    UnexpectedData = 1,
}

impl StatusEnum {
    /// 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::Success),
            1 => Some(Self::UnexpectedData),
            _ => None,
        }
    }

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

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

impl<'a> FromTlv<'a> for StatusEnum {
    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 `ContentAppMessage` (command `0x00`).
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ContentAppMessageFields<'a> {
    /// `Data`, field 0.
    pub data: &'a str,
    /// `EncodingHint`, field 1.
    pub encoding_hint: Option<&'a str>,
}

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

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

/// The fields of `ContentAppMessageResponse` (command `0x01`).
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ContentAppMessageResponseFields<'a> {
    /// `Status`, field 0.
    pub status: StatusEnum,
    /// `Data`, field 1.
    pub data: Option<&'a str>,
    /// `EncodingHint`, field 2.
    pub encoding_hint: Option<&'a str>,
}

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

impl<'a> FromTlv<'a> for ContentAppMessageResponseFields<'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 data = None;
        let mut encoding_hint = 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(<StatusEnum>::from_tlv(reader, &field)?),
                Some(1) => data = Some(<&'a str>::from_tlv(reader, &field)?),
                Some(2) => encoding_hint = 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))?,
            data,
            encoding_hint,
        })
    }
}

/// Every attribute the specification defines, with its conformance.
pub const ATTRIBUTES: &[Attribute] = &[];

/// Every command, with its conformance.
pub const COMMANDS: &[Command] = &[
    Command {
        id: 0x00,
        name: "ContentAppMessage",
        to_server: true,
        response: Some(0x01),
        access: Access::invoke(Privilege::Operate),
        conform: Conform::MANDATORY,
    },
    Command {
        id: 0x01,
        name: "ContentAppMessageResponse",
        to_server: false,
        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: "Content App Observer",
    revision: REVISION,
    pics: "APPOBSERVER",
    derived_from: None,
    features: FEATURES,
    attributes: ATTRIBUTES,
    commands: COMMANDS,
    events: EVENTS,
};