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.
//
// WebRTC Transport Requestor โ€” cluster 0x0554.
//
// 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.

//! WebRTC Transport Requestor (cluster `0x0554`, 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 = 0x0554;

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

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

    /// `CurrentSessions`.
    pub const CURRENT_SESSIONS: AttributeId = 0x00;
}

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

    /// `Offer`.
    pub const OFFER: CommandId = 0x00;
    /// `Answer`.
    pub const ANSWER: CommandId = 0x01;
    /// `ICECandidates`.
    pub const ICE_CANDIDATES: CommandId = 0x02;
    /// `End`.
    pub const END: CommandId = 0x03;
}

/// The fields of `Offer` (command `0x00`).
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct OfferFields<'a> {
    /// `WebRTCSessionID`, field 0.
    pub web_rtc_session_id: u16,
    /// `SDP`, field 1.
    pub sdp: &'a str,
    /// `ICEServers`, field 2.
    pub ice_servers: Option<TlvList<'a, crate::clusters::generated::globals::ICEServerStruct<'a>>>,
    /// `ICETransportPolicy`, field 3.
    pub ice_transport_policy: Option<&'a str>,
}

impl<'a> ToTlv for OfferFields<'a> {
    fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
        w.start_structure(tag)?;
        self.web_rtc_session_id.to_tlv(w, Tag::Context(0))?;
        self.sdp.to_tlv(w, Tag::Context(1))?;
        if let Some(value) = &self.ice_servers {
            value.to_tlv(w, Tag::Context(2))?;
        }
        if let Some(value) = &self.ice_transport_policy {
            value.to_tlv(w, Tag::Context(3))?;
        }
        w.end_container()
    }
}

impl<'a> FromTlv<'a> for OfferFields<'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 web_rtc_session_id = None;
        let mut sdp = None;
        let mut ice_servers = None;
        let mut ice_transport_policy = 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) => web_rtc_session_id = Some(<u16>::from_tlv(reader, &field)?),
                Some(1) => sdp = Some(<&'a str>::from_tlv(reader, &field)?),
                Some(2) => {
                    ice_servers = Some(<TlvList<
                        'a,
                        crate::clusters::generated::globals::ICEServerStruct<'a>,
                    >>::from_tlv(reader, &field)?)
                }
                Some(3) => ice_transport_policy = 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 {
            web_rtc_session_id: web_rtc_session_id
                .ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
            sdp: sdp.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
            ice_servers,
            ice_transport_policy,
        })
    }
}

/// The fields of `Answer` (command `0x01`).
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct AnswerFields<'a> {
    /// `WebRTCSessionID`, field 0.
    pub web_rtc_session_id: u16,
    /// `SDP`, field 1.
    pub sdp: &'a str,
}

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

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

/// The fields of `ICECandidates` (command `0x02`).
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ICECandidatesFields<'a> {
    /// `WebRTCSessionID`, field 0.
    pub web_rtc_session_id: u16,
    /// `ICECandidates`, field 1.
    pub ice_candidates: TlvList<'a, crate::clusters::generated::globals::ICECandidateStruct<'a>>,
}

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

impl<'a> FromTlv<'a> for ICECandidatesFields<'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 web_rtc_session_id = None;
        let mut ice_candidates = 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) => web_rtc_session_id = Some(<u16>::from_tlv(reader, &field)?),
                Some(1) => {
                    ice_candidates = Some(<TlvList<
                        'a,
                        crate::clusters::generated::globals::ICECandidateStruct<'a>,
                    >>::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 {
            web_rtc_session_id: web_rtc_session_id
                .ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
            ice_candidates: ice_candidates
                .ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
        })
    }
}

/// The fields of `End` (command `0x03`).
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct EndFields {
    /// `WebRTCSessionID`, field 0.
    pub web_rtc_session_id: u16,
    /// `Reason`, field 1.
    pub reason: crate::clusters::generated::globals::WebRTCEndReasonEnum,
}

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

impl<'a> FromTlv<'a> for EndFields {
    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 web_rtc_session_id = None;
        let mut reason = 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) => web_rtc_session_id = Some(<u16>::from_tlv(reader, &field)?),
                Some(1) => {
                    reason = Some(
                        <crate::clusters::generated::globals::WebRTCEndReasonEnum>::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 {
            web_rtc_session_id: web_rtc_session_id
                .ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
            reason: reason.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
        })
    }
}

/// Every attribute the specification defines, with its conformance.
pub const ATTRIBUTES: &[Attribute] = &[Attribute {
    id: 0x0000,
    name: "CurrentSessions",
    kind: "list[WebRTCSessionStruct]",
    access: Access::read_only(Privilege::Administer)
        .with_qualities(AccessQualities::FABRIC_SENSITIVE),
    qualities: AttributeQualities::empty(),
    reporting: Reporting::OnChange,
    conform: Conform::MANDATORY,
}];

/// Every command, with its conformance.
pub const COMMANDS: &[Command] = &[
    Command {
        id: 0x00,
        name: "Offer",
        to_server: true,
        response: None,
        access: Access::invoke(Privilege::Operate).with_qualities(AccessQualities::LARGE),
        conform: Conform::MANDATORY,
    },
    Command {
        id: 0x01,
        name: "Answer",
        to_server: true,
        response: None,
        access: Access::invoke(Privilege::Operate).with_qualities(AccessQualities::LARGE),
        conform: Conform::MANDATORY,
    },
    Command {
        id: 0x02,
        name: "ICECandidates",
        to_server: true,
        response: None,
        access: Access::invoke(Privilege::Operate).with_qualities(AccessQualities::LARGE),
        conform: Conform::MANDATORY,
    },
    Command {
        id: 0x03,
        name: "End",
        to_server: true,
        response: None,
        access: Access::invoke(Privilege::Operate).with_qualities(AccessQualities::LARGE),
        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: "WebRTC Transport Requestor",
    revision: REVISION,
    pics: "WEBRTCR",
    derived_from: None,
    features: FEATURES,
    attributes: ATTRIBUTES,
    commands: COMMANDS,
    events: EVENTS,
};