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 Border Router Management โ€” cluster 0x0452.
//
// 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 Border Router Management (cluster `0x0452`, revision 1).
//!
//! 11 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 = 0x0452;

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

/// 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 {
    /// The ability to change PAN configuration with pending dataset setting request.
    ///
    /// Code `PC`, bit 0.
    pub const PAN_CHANGE: u32 = 1 << 0;
}

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

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

    /// `BorderRouterName`.
    pub const BORDER_ROUTER_NAME: AttributeId = 0x00;
    /// `BorderAgentID`.
    pub const BORDER_AGENT_ID: AttributeId = 0x01;
    /// `ThreadVersion`.
    pub const THREAD_VERSION: AttributeId = 0x02;
    /// `InterfaceEnabled`.
    pub const INTERFACE_ENABLED: AttributeId = 0x03;
    /// `ActiveDatasetTimestamp`.
    pub const ACTIVE_DATASET_TIMESTAMP: AttributeId = 0x04;
    /// `PendingDatasetTimestamp`.
    pub const PENDING_DATASET_TIMESTAMP: AttributeId = 0x05;
}

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

    /// `GetActiveDatasetRequest`.
    pub const GET_ACTIVE_DATASET_REQUEST: CommandId = 0x00;
    /// `GetPendingDatasetRequest`.
    pub const GET_PENDING_DATASET_REQUEST: CommandId = 0x01;
    /// `DatasetResponse`.
    pub const DATASET_RESPONSE: CommandId = 0x02;
    /// `SetActiveDatasetRequest`.
    pub const SET_ACTIVE_DATASET_REQUEST: CommandId = 0x03;
    /// `SetPendingDatasetRequest`.
    pub const SET_PENDING_DATASET_REQUEST: CommandId = 0x04;
}

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

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

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

/// The fields of `SetActiveDatasetRequest` (command `0x03`).
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct SetActiveDatasetRequestFields<'a> {
    /// `ActiveDataset`, field 0.
    pub active_dataset: &'a [u8],
    /// `Breadcrumb`, field 1.
    pub breadcrumb: Option<u64>,
}

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

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

/// The fields of `SetPendingDatasetRequest` (command `0x04`).
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct SetPendingDatasetRequestFields<'a> {
    /// `PendingDataset`, field 0.
    pub pending_dataset: &'a [u8],
}

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

impl<'a> FromTlv<'a> for SetPendingDatasetRequestFields<'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 pending_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) => pending_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 {
            pending_dataset: pending_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: "BorderRouterName",
        kind: "string",
        access: Access::read_only(Privilege::View),
        qualities: AttributeQualities::empty(),
        reporting: Reporting::OnChange,
        conform: Conform::MANDATORY,
    },
    Attribute {
        id: 0x0001,
        name: "BorderAgentID",
        kind: "octstr",
        access: Access::read_only(Privilege::View),
        qualities: AttributeQualities::empty(),
        reporting: Reporting::OnChange,
        conform: Conform::MANDATORY,
    },
    Attribute {
        id: 0x0002,
        name: "ThreadVersion",
        kind: "uint16",
        access: Access::read_only(Privilege::View),
        qualities: AttributeQualities::FIXED,
        reporting: Reporting::OnChange,
        conform: Conform::MANDATORY,
    },
    Attribute {
        id: 0x0003,
        name: "InterfaceEnabled",
        kind: "bool",
        access: Access::read_only(Privilege::View),
        qualities: AttributeQualities::NON_VOLATILE,
        reporting: Reporting::OnChange,
        conform: Conform::MANDATORY,
    },
    Attribute {
        id: 0x0004,
        name: "ActiveDatasetTimestamp",
        kind: "uint64",
        access: Access::read_only(Privilege::View),
        qualities: AttributeQualities::NULLABLE.union(AttributeQualities::NON_VOLATILE),
        reporting: Reporting::OnChange,
        conform: Conform::MANDATORY,
    },
    Attribute {
        id: 0x0005,
        name: "PendingDatasetTimestamp",
        kind: "uint64",
        access: Access::read_only(Privilege::View),
        qualities: AttributeQualities::NULLABLE.union(AttributeQualities::NON_VOLATILE),
        reporting: Reporting::OnChange,
        conform: Conform::MANDATORY,
    },
];

/// Every command, with its conformance.
pub const COMMANDS: &[Command] = &[
    Command {
        id: 0x00,
        name: "GetActiveDatasetRequest",
        to_server: true,
        response: Some(0x02),
        access: Access::invoke(Privilege::Manage),
        conform: Conform::MANDATORY,
    },
    Command {
        id: 0x01,
        name: "GetPendingDatasetRequest",
        to_server: true,
        response: Some(0x02),
        access: Access::invoke(Privilege::Manage),
        conform: Conform::MANDATORY,
    },
    Command {
        id: 0x02,
        name: "DatasetResponse",
        to_server: false,
        response: None,
        access: Access::invoke(Privilege::Operate),
        conform: Conform::MANDATORY,
    },
    Command {
        id: 0x03,
        name: "SetActiveDatasetRequest",
        to_server: true,
        response: None,
        access: Access::invoke(Privilege::Manage).with_qualities(AccessQualities::TIMED),
        conform: Conform::MANDATORY,
    },
    Command {
        id: 0x04,
        name: "SetPendingDatasetRequest",
        to_server: true,
        response: None,
        access: Access::invoke(Privilege::Manage).with_qualities(AccessQualities::TIMED),
        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: "Thread Border Router Management",
    revision: REVISION,
    pics: "TBRM",
    derived_from: None,
    features: FEATURES,
    attributes: ATTRIBUTES,
    commands: COMMANDS,
    events: EVENTS,
};