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.
//
// Target Navigator — cluster 0x0505.
//
// 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.

//! Target Navigator (cluster `0x0505`, revision 2).
//!
//! 5 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 = 0x0505;

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

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

    /// `TargetList`.
    pub const TARGET_LIST: AttributeId = 0x00;
    /// `CurrentTarget`.
    pub const CURRENT_TARGET: AttributeId = 0x01;
}

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

    /// `NavigateTarget`.
    pub const NAVIGATE_TARGET: CommandId = 0x00;
    /// `NavigateTargetResponse`.
    pub const NAVIGATE_TARGET_RESPONSE: CommandId = 0x01;
}

/// The event ids.
pub mod event {
    use crate::im::EventId;

    /// `TargetUpdated`.
    pub const TARGET_UPDATED: EventId = 0x00;
}

/// `StatusEnum`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(u8)]
pub enum StatusEnum {
    /// Command succeeded
    Success = 0,
    /// Requested target was not found in the TargetList
    TargetNotFound = 1,
    /// Target request is not allowed in current state.
    NotAllowed = 2,
}

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::TargetNotFound),
            2 => Some(Self::NotAllowed),
            _ => 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))
    }
}

/// `TargetInfoStruct`.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct TargetInfoStruct<'a> {
    /// `Identifier`, field 0.
    pub identifier: u8,
    /// `Name`, field 1.
    pub name: &'a str,
}

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

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

/// The fields of `NavigateTarget` (command `0x00`).
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct NavigateTargetFields<'a> {
    /// `Target`, field 0.
    pub target: u8,
    /// `Data`, field 1.
    pub data: Option<&'a str>,
}

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

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

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

impl<'a> ToTlv for NavigateTargetResponseFields<'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))?;
        }
        w.end_container()
    }
}

impl<'a> FromTlv<'a> for NavigateTargetResponseFields<'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;
        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)?),
                // 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,
        })
    }
}

/// Every attribute the specification defines, with its conformance.
pub const ATTRIBUTES: &[Attribute] = &[
    Attribute {
        id: 0x0000,
        name: "TargetList",
        kind: "list[TargetInfoStruct]",
        access: Access::read_only(Privilege::View),
        qualities: AttributeQualities::empty(),
        reporting: Reporting::OnChange,
        conform: Conform::MANDATORY,
    },
    Attribute {
        id: 0x0001,
        name: "CurrentTarget",
        kind: "uint8",
        access: Access::read_only(Privilege::View),
        qualities: AttributeQualities::empty(),
        reporting: Reporting::OnChange,
        conform: Conform::OPTIONAL,
    },
];

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

/// Every event, with its conformance.
pub const EVENTS: &[Event] = &[Event {
    id: 0x00,
    name: "TargetUpdated",
    priority: EventPriority::Info,
    access: Access::read_only(Privilege::View),
    conform: Conform::OPTIONAL,
}];

/// The whole cluster, as the specification defines it.
pub const CLUSTER: Cluster = Cluster {
    id: ID,
    name: "Target Navigator",
    revision: REVISION,
    pics: "TGTNAV",
    derived_from: None,
    features: FEATURES,
    attributes: ATTRIBUTES,
    commands: COMMANDS,
    events: EVENTS,
};