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.
//
// Account Login — cluster 0x050E.
//
// 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.

//! Account Login (cluster `0x050E`, 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 = 0x050E;

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

/// This cluster defines no features, so its `FeatureMap` is always zero.
pub const FEATURES: &[Feature] = &[];

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

    /// `GetSetupPIN`.
    pub const GET_SETUP_PIN: CommandId = 0x00;
    /// `GetSetupPINResponse`.
    pub const GET_SETUP_PIN_RESPONSE: CommandId = 0x01;
    /// `Login`.
    pub const LOGIN: CommandId = 0x02;
    /// `Logout`.
    pub const LOGOUT: CommandId = 0x03;
}

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

    /// `LoggedOut`.
    pub const LOGGED_OUT: EventId = 0x00;
}

/// The fields of `GetSetupPIN` (command `0x00`).
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct GetSetupPINFields<'a> {
    /// `TempAccountIdentifier`, field 0.
    pub temp_account_identifier: &'a str,
}

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

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

/// The fields of `GetSetupPINResponse` (command `0x01`).
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct GetSetupPINResponseFields<'a> {
    /// `SetupPIN`, field 0.
    pub setup_pin: &'a str,
}

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

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

/// The fields of `Login` (command `0x02`).
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct LoginFields<'a> {
    /// `TempAccountIdentifier`, field 0.
    pub temp_account_identifier: &'a str,
    /// `SetupPIN`, field 1.
    pub setup_pin: &'a str,
    /// `Node`, field 2.
    pub node: Option<crate::msg::NodeId>,
}

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

impl<'a> FromTlv<'a> for LoginFields<'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 temp_account_identifier = None;
        let mut setup_pin = None;
        let mut node = 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) => temp_account_identifier = Some(<&'a str>::from_tlv(reader, &field)?),
                Some(1) => setup_pin = Some(<&'a str>::from_tlv(reader, &field)?),
                Some(2) => node = Some(<crate::msg::NodeId>::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 {
            temp_account_identifier: temp_account_identifier
                .ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
            setup_pin: setup_pin.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
            node,
        })
    }
}

/// The fields of `Logout` (command `0x03`).
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct LogoutFields {
    /// `Node`, field 0.
    pub node: Option<crate::msg::NodeId>,
}

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

impl<'a> FromTlv<'a> for LogoutFields {
    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 node = 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) => node = Some(<crate::msg::NodeId>::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 { node })
    }
}

/// Every attribute the specification defines, with its conformance.
pub const ATTRIBUTES: &[Attribute] = &[];

/// Every command, with its conformance.
pub const COMMANDS: &[Command] = &[
    Command {
        id: 0x00,
        name: "GetSetupPIN",
        to_server: true,
        response: Some(0x01),
        access: Access::invoke(Privilege::Administer)
            .with_qualities(AccessQualities::FABRIC_SCOPED.union(AccessQualities::TIMED)),
        conform: Conform::MANDATORY,
    },
    Command {
        id: 0x01,
        name: "GetSetupPINResponse",
        to_server: false,
        response: None,
        access: Access::invoke(Privilege::Operate).with_qualities(AccessQualities::FABRIC_SCOPED),
        conform: Conform::MANDATORY,
    },
    Command {
        id: 0x02,
        name: "Login",
        to_server: true,
        response: None,
        access: Access::invoke(Privilege::Administer)
            .with_qualities(AccessQualities::FABRIC_SCOPED.union(AccessQualities::TIMED)),
        conform: Conform::MANDATORY,
    },
    Command {
        id: 0x03,
        name: "Logout",
        to_server: true,
        response: None,
        access: Access::invoke(Privilege::Operate)
            .with_qualities(AccessQualities::FABRIC_SCOPED.union(AccessQualities::TIMED)),
        conform: Conform::MANDATORY,
    },
];

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

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