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.
//
// Thread Network Directory — cluster 0x0453.
//
// 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.

//! Thread Network Directory (cluster `0x0453`, revision 1).
//!
//! 7 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 = 0x0453;

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

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

    /// `PreferredExtendedPanID`.
    pub const PREFERRED_EXTENDED_PAN_ID: AttributeId = 0x00;
    /// `ThreadNetworks`.
    pub const THREAD_NETWORKS: AttributeId = 0x01;
    /// `ThreadNetworkTableSize`.
    pub const THREAD_NETWORK_TABLE_SIZE: AttributeId = 0x02;
}

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

    /// `AddNetwork`.
    pub const ADD_NETWORK: CommandId = 0x00;
    /// `RemoveNetwork`.
    pub const REMOVE_NETWORK: CommandId = 0x01;
    /// `GetOperationalDataset`.
    pub const GET_OPERATIONAL_DATASET: CommandId = 0x02;
    /// `OperationalDatasetResponse`.
    pub const OPERATIONAL_DATASET_RESPONSE: CommandId = 0x03;
}

/// `ThreadNetworkStruct`.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ThreadNetworkStruct<'a> {
    /// `ExtendedPanID`, field 0.
    pub extended_pan_id: &'a [u8],
    /// `NetworkName`, field 1.
    pub network_name: &'a str,
    /// `Channel`, field 2.
    pub channel: u16,
    /// `ActiveTimestamp`, field 3.
    pub active_timestamp: u64,
}

impl<'a> ToTlv for ThreadNetworkStruct<'a> {
    fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
        w.start_structure(tag)?;
        self.extended_pan_id.to_tlv(w, Tag::Context(0))?;
        self.network_name.to_tlv(w, Tag::Context(1))?;
        self.channel.to_tlv(w, Tag::Context(2))?;
        self.active_timestamp.to_tlv(w, Tag::Context(3))?;
        w.end_container()
    }
}

impl<'a> FromTlv<'a> for ThreadNetworkStruct<'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 extended_pan_id = None;
        let mut network_name = None;
        let mut channel = None;
        let mut active_timestamp = 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) => extended_pan_id = Some(<&'a [u8]>::from_tlv(reader, &field)?),
                Some(1) => network_name = Some(<&'a str>::from_tlv(reader, &field)?),
                Some(2) => channel = Some(<u16>::from_tlv(reader, &field)?),
                Some(3) => active_timestamp = Some(<u64>::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 {
            extended_pan_id: extended_pan_id
                .ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
            network_name: network_name.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
            channel: channel.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
            active_timestamp: active_timestamp
                .ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
        })
    }
}

/// The fields of `AddNetwork` (command `0x00`).
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct AddNetworkFields<'a> {
    /// `OperationalDataset`, field 0.
    pub operational_dataset: &'a [u8],
}

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

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

/// The fields of `RemoveNetwork` (command `0x01`).
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct RemoveNetworkFields<'a> {
    /// `ExtendedPanID`, field 0.
    pub extended_pan_id: &'a [u8],
}

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

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

/// The fields of `GetOperationalDataset` (command `0x02`).
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct GetOperationalDatasetFields<'a> {
    /// `ExtendedPanID`, field 0.
    pub extended_pan_id: &'a [u8],
}

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

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

/// The fields of `OperationalDatasetResponse` (command `0x03`).
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct OperationalDatasetResponseFields<'a> {
    /// `OperationalDataset`, field 0.
    pub operational_dataset: &'a [u8],
}

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

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

/// Every attribute the specification defines, with its conformance.
pub const ATTRIBUTES: &[Attribute] = &[
    Attribute {
        id: 0x0000,
        name: "PreferredExtendedPanID",
        kind: "octstr",
        access: Access::read_write_with(Privilege::View, Privilege::Manage),
        qualities: AttributeQualities::NULLABLE.union(AttributeQualities::NON_VOLATILE),
        reporting: Reporting::OnChange,
        conform: Conform::MANDATORY,
    },
    Attribute {
        id: 0x0001,
        name: "ThreadNetworks",
        kind: "list[ThreadNetworkStruct]",
        access: Access::read_only(Privilege::View),
        qualities: AttributeQualities::NON_VOLATILE,
        reporting: Reporting::OnChange,
        conform: Conform::MANDATORY,
    },
    Attribute {
        id: 0x0002,
        name: "ThreadNetworkTableSize",
        kind: "uint8",
        access: Access::read_only(Privilege::View),
        qualities: AttributeQualities::FIXED,
        reporting: Reporting::OnChange,
        conform: Conform::MANDATORY,
    },
];

/// Every command, with its conformance.
pub const COMMANDS: &[Command] = &[
    Command {
        id: 0x00,
        name: "AddNetwork",
        to_server: true,
        response: None,
        access: Access::invoke(Privilege::Manage).with_qualities(AccessQualities::TIMED),
        conform: Conform::MANDATORY,
    },
    Command {
        id: 0x01,
        name: "RemoveNetwork",
        to_server: true,
        response: None,
        access: Access::invoke(Privilege::Manage).with_qualities(AccessQualities::TIMED),
        conform: Conform::MANDATORY,
    },
    Command {
        id: 0x02,
        name: "GetOperationalDataset",
        to_server: true,
        response: Some(0x03),
        access: Access::invoke(Privilege::Manage),
        conform: Conform::MANDATORY,
    },
    Command {
        id: 0x03,
        name: "OperationalDatasetResponse",
        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: "Thread Network Directory",
    revision: REVISION,
    pics: "THNETDIR",
    derived_from: None,
    features: FEATURES,
    attributes: ATTRIBUTES,
    commands: COMMANDS,
    events: EVENTS,
};