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.
//
// Ecosystem Information โ€” cluster 0x0750.
//
// 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.

//! Ecosystem Information (cluster `0x0750`, 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 = 0x0750;

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

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

    /// `DeviceDirectory`.
    pub const DEVICE_DIRECTORY: AttributeId = 0x00;
    /// `LocationDirectory`.
    pub const LOCATION_DIRECTORY: AttributeId = 0x01;
}

/// `DeviceTypeStruct`.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct DeviceTypeStruct {
    /// `DeviceType`, field 0.
    pub device_type: u32,
    /// `Revision`, field 1.
    pub revision: u16,
}

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

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

/// `EcosystemDeviceStruct`, fabric-scoped โ€” ยง7.19.1.9's `FabricIndex` is field 254.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct EcosystemDeviceStruct<'a> {
    /// `DeviceName`, field 0.
    pub device_name: Option<&'a str>,
    /// `DeviceNameLastEdit`, field 1.
    pub device_name_last_edit: Option<u64>,
    /// `BridgedEndpoint`, field 2.
    pub bridged_endpoint: Option<u16>,
    /// `OriginalEndpoint`, field 3.
    pub original_endpoint: Option<u16>,
    /// `DeviceTypes`, field 4.
    pub device_types: TlvList<'a, DeviceTypeStruct>,
    /// `UniqueLocationIDs`, field 5.
    pub unique_location_i_ds: TlvList<'a, &'a str>,
    /// `UniqueLocationIDsLastEdit`, field 6.
    pub unique_location_i_ds_last_edit: u64,
    /// `FabricIndex`, field 254.
    pub fabric_index: crate::msg::FabricIndex,
}

impl<'a> ToTlv for EcosystemDeviceStruct<'a> {
    fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
        w.start_structure(tag)?;
        if let Some(value) = &self.device_name {
            value.to_tlv(w, Tag::Context(0))?;
        }
        if let Some(value) = &self.device_name_last_edit {
            value.to_tlv(w, Tag::Context(1))?;
        }
        if let Some(value) = &self.bridged_endpoint {
            value.to_tlv(w, Tag::Context(2))?;
        }
        if let Some(value) = &self.original_endpoint {
            value.to_tlv(w, Tag::Context(3))?;
        }
        self.device_types.to_tlv(w, Tag::Context(4))?;
        self.unique_location_i_ds.to_tlv(w, Tag::Context(5))?;
        self.unique_location_i_ds_last_edit
            .to_tlv(w, Tag::Context(6))?;
        self.fabric_index.to_tlv(w, Tag::Context(254))?;
        w.end_container()
    }
}

impl<'a> FromTlv<'a> for EcosystemDeviceStruct<'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 device_name = None;
        let mut device_name_last_edit = None;
        let mut bridged_endpoint = None;
        let mut original_endpoint = None;
        let mut device_types = None;
        let mut unique_location_i_ds = None;
        let mut unique_location_i_ds_last_edit = None;
        let mut fabric_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) => device_name = Some(<&'a str>::from_tlv(reader, &field)?),
                Some(1) => device_name_last_edit = Some(<u64>::from_tlv(reader, &field)?),
                Some(2) => bridged_endpoint = Some(<u16>::from_tlv(reader, &field)?),
                Some(3) => original_endpoint = Some(<u16>::from_tlv(reader, &field)?),
                Some(4) => {
                    device_types = Some(<TlvList<'a, DeviceTypeStruct>>::from_tlv(reader, &field)?)
                }
                Some(5) => {
                    unique_location_i_ds = Some(<TlvList<'a, &'a str>>::from_tlv(reader, &field)?)
                }
                Some(6) => unique_location_i_ds_last_edit = Some(<u64>::from_tlv(reader, &field)?),
                Some(254) => {
                    fabric_index = Some(<crate::msg::FabricIndex>::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 {
            device_name,
            device_name_last_edit,
            bridged_endpoint,
            original_endpoint,
            device_types: device_types.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
            unique_location_i_ds: unique_location_i_ds
                .ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
            unique_location_i_ds_last_edit: unique_location_i_ds_last_edit
                .ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
            fabric_index: fabric_index.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
        })
    }
}

/// `EcosystemLocationStruct`, fabric-scoped โ€” ยง7.19.1.9's `FabricIndex` is field 254.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct EcosystemLocationStruct<'a> {
    /// `UniqueLocationID`, field 0.
    pub unique_location_id: &'a str,
    /// `LocationDescriptor`, field 1.
    pub location_descriptor: crate::clusters::generated::globals::LocationDescriptorStruct<'a>,
    /// `LocationDescriptorLastEdit`, field 2.
    pub location_descriptor_last_edit: u64,
    /// `FabricIndex`, field 254.
    pub fabric_index: crate::msg::FabricIndex,
}

impl<'a> ToTlv for EcosystemLocationStruct<'a> {
    fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
        w.start_structure(tag)?;
        self.unique_location_id.to_tlv(w, Tag::Context(0))?;
        self.location_descriptor.to_tlv(w, Tag::Context(1))?;
        self.location_descriptor_last_edit
            .to_tlv(w, Tag::Context(2))?;
        self.fabric_index.to_tlv(w, Tag::Context(254))?;
        w.end_container()
    }
}

impl<'a> FromTlv<'a> for EcosystemLocationStruct<'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 unique_location_id = None;
        let mut location_descriptor = None;
        let mut location_descriptor_last_edit = None;
        let mut fabric_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) => unique_location_id = Some(<&'a str>::from_tlv(reader, &field)?),
                Some(1) => location_descriptor = Some(
                    <crate::clusters::generated::globals::LocationDescriptorStruct<'a>>::from_tlv(
                        reader, &field,
                    )?,
                ),
                Some(2) => location_descriptor_last_edit = Some(<u64>::from_tlv(reader, &field)?),
                Some(254) => {
                    fabric_index = Some(<crate::msg::FabricIndex>::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 {
            unique_location_id: unique_location_id
                .ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
            location_descriptor: location_descriptor
                .ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
            location_descriptor_last_edit: location_descriptor_last_edit
                .ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
            fabric_index: fabric_index.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
        })
    }
}

/// Every attribute the specification defines, with its conformance.
pub const ATTRIBUTES: &[Attribute] = &[
    Attribute {
        id: 0x0000,
        name: "DeviceDirectory",
        kind: "list[EcosystemDeviceStruct]",
        access: Access::read_only(Privilege::Manage).with_qualities(AccessQualities::FABRIC_SCOPED),
        qualities: AttributeQualities::NON_VOLATILE,
        reporting: Reporting::OnChange,
        conform: Conform::MANDATORY,
    },
    Attribute {
        id: 0x0001,
        name: "LocationDirectory",
        kind: "list[EcosystemLocationStruct]",
        access: Access::read_only(Privilege::Manage).with_qualities(AccessQualities::FABRIC_SCOPED),
        qualities: AttributeQualities::NON_VOLATILE,
        reporting: Reporting::OnChange,
        conform: Conform::MANDATORY,
    },
];

/// Every command, with its conformance.
pub const COMMANDS: &[Command] = &[];

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