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.
//
// Audio Output — cluster 0x050B.
//
// 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.

//! Audio Output (cluster `0x050B`, revision 1).
//!
//! 4 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 = 0x050B;

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

/// 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 updates to output names
    ///
    /// Code `NU`, bit 0.
    pub const NAME_UPDATES: u32 = 1 << 0;
}

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

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

    /// `OutputList`.
    pub const OUTPUT_LIST: AttributeId = 0x00;
    /// `CurrentOutput`.
    pub const CURRENT_OUTPUT: AttributeId = 0x01;
}

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

    /// `SelectOutput`.
    pub const SELECT_OUTPUT: CommandId = 0x00;
    /// `RenameOutput`.
    pub const RENAME_OUTPUT: CommandId = 0x01;
}

/// `OutputTypeEnum`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(u8)]
pub enum OutputTypeEnum {
    /// HDMI
    HDMI = 0,
    /// `BT`.
    BT = 1,
    /// `Optical`.
    Optical = 2,
    /// `Headphone`.
    Headphone = 3,
    /// `Internal`.
    Internal = 4,
    /// `Other`.
    Other = 5,
}

impl OutputTypeEnum {
    /// 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::HDMI),
            1 => Some(Self::BT),
            2 => Some(Self::Optical),
            3 => Some(Self::Headphone),
            4 => Some(Self::Internal),
            5 => Some(Self::Other),
            _ => None,
        }
    }

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

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

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

/// `OutputInfoStruct`.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct OutputInfoStruct<'a> {
    /// `Index`, field 0.
    pub index: u8,
    /// `OutputType`, field 1.
    pub output_type: OutputTypeEnum,
    /// `Name`, field 2.
    pub name: &'a str,
}

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

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

/// The fields of `SelectOutput` (command `0x00`).
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct SelectOutputFields {
    /// `Index`, field 0.
    pub index: u8,
}

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

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

/// The fields of `RenameOutput` (command `0x01`).
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct RenameOutputFields<'a> {
    /// `Index`, field 0.
    pub index: u8,
    /// `Name`, field 1.
    pub name: &'a str,
}

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

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

/// Every attribute the specification defines, with its conformance.
pub const ATTRIBUTES: &[Attribute] = &[
    Attribute {
        id: 0x0000,
        name: "OutputList",
        kind: "list[OutputInfoStruct]",
        access: Access::read_only(Privilege::View),
        qualities: AttributeQualities::empty(),
        reporting: Reporting::OnChange,
        conform: Conform::MANDATORY,
    },
    Attribute {
        id: 0x0001,
        name: "CurrentOutput",
        kind: "uint8",
        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: "SelectOutput",
        to_server: true,
        response: None,
        access: Access::invoke(Privilege::Operate),
        conform: Conform::MANDATORY,
    },
    Command {
        id: 0x01,
        name: "RenameOutput",
        to_server: true,
        response: None,
        access: Access::invoke(Privilege::Manage),
        conform: Conform {
            clauses: &[Clause {
                verdict: Conformance::Mandatory,
                when: Condition::Feature(0),
            }],
        },
    },
];

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