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.
//
// Software Diagnostics โ€” cluster 0x0034.
//
// 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.

//! Software Diagnostics (cluster `0x0034`, revision 1).
//!
//! 6 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 = 0x0034;

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

/// 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 {
    /// Node makes available the metrics for high watermark related to memory consumption.
    ///
    /// Code `WTRMRK`, bit 0.
    pub const WATERMARKS: u32 = 1 << 0;
}

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

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

    /// `ThreadMetrics`.
    pub const THREAD_METRICS: AttributeId = 0x00;
    /// `CurrentHeapFree`.
    pub const CURRENT_HEAP_FREE: AttributeId = 0x01;
    /// `CurrentHeapUsed`.
    pub const CURRENT_HEAP_USED: AttributeId = 0x02;
    /// `CurrentHeapHighWatermark`.
    pub const CURRENT_HEAP_HIGH_WATERMARK: AttributeId = 0x03;
}

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

    /// `ResetWatermarks`.
    pub const RESET_WATERMARKS: CommandId = 0x00;
}

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

    /// `SoftwareFault`.
    pub const SOFTWARE_FAULT: EventId = 0x00;
}

/// `ThreadMetricsStruct`.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ThreadMetricsStruct<'a> {
    /// `ID`, field 0.
    pub id: u64,
    /// `Name`, field 1.
    pub name: Option<&'a str>,
    /// `StackFreeCurrent`, field 2.
    pub stack_free_current: Option<u32>,
    /// `StackFreeMinimum`, field 3.
    pub stack_free_minimum: Option<u32>,
    /// `StackSize`, field 4.
    pub stack_size: Option<u32>,
}

impl<'a> ToTlv for ThreadMetricsStruct<'a> {
    fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
        w.start_structure(tag)?;
        self.id.to_tlv(w, Tag::Context(0))?;
        if let Some(value) = &self.name {
            value.to_tlv(w, Tag::Context(1))?;
        }
        if let Some(value) = &self.stack_free_current {
            value.to_tlv(w, Tag::Context(2))?;
        }
        if let Some(value) = &self.stack_free_minimum {
            value.to_tlv(w, Tag::Context(3))?;
        }
        if let Some(value) = &self.stack_size {
            value.to_tlv(w, Tag::Context(4))?;
        }
        w.end_container()
    }
}

impl<'a> FromTlv<'a> for ThreadMetricsStruct<'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 id = None;
        let mut name = None;
        let mut stack_free_current = None;
        let mut stack_free_minimum = None;
        let mut stack_size = 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) => id = Some(<u64>::from_tlv(reader, &field)?),
                Some(1) => name = Some(<&'a str>::from_tlv(reader, &field)?),
                Some(2) => stack_free_current = Some(<u32>::from_tlv(reader, &field)?),
                Some(3) => stack_free_minimum = Some(<u32>::from_tlv(reader, &field)?),
                Some(4) => stack_size = Some(<u32>::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 {
            id: id.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
            name,
            stack_free_current,
            stack_free_minimum,
            stack_size,
        })
    }
}

/// Every attribute the specification defines, with its conformance.
pub const ATTRIBUTES: &[Attribute] = &[
    Attribute {
        id: 0x0000,
        name: "ThreadMetrics",
        kind: "list[ThreadMetricsStruct]",
        access: Access::read_only(Privilege::View),
        qualities: AttributeQualities::empty(),
        reporting: Reporting::ChangesOmitted,
        conform: Conform::OPTIONAL,
    },
    Attribute {
        id: 0x0001,
        name: "CurrentHeapFree",
        kind: "uint64",
        access: Access::read_only(Privilege::View),
        qualities: AttributeQualities::empty(),
        reporting: Reporting::ChangesOmitted,
        conform: Conform::OPTIONAL,
    },
    Attribute {
        id: 0x0002,
        name: "CurrentHeapUsed",
        kind: "uint64",
        access: Access::read_only(Privilege::View),
        qualities: AttributeQualities::empty(),
        reporting: Reporting::ChangesOmitted,
        conform: Conform::OPTIONAL,
    },
    Attribute {
        id: 0x0003,
        name: "CurrentHeapHighWatermark",
        kind: "uint64",
        access: Access::read_only(Privilege::View),
        qualities: AttributeQualities::empty(),
        reporting: Reporting::ChangesOmitted,
        conform: Conform {
            clauses: &[Clause {
                verdict: Conformance::Mandatory,
                when: Condition::Feature(0),
            }],
        },
    },
];

/// Every command, with its conformance.
pub const COMMANDS: &[Command] = &[Command {
    id: 0x00,
    name: "ResetWatermarks",
    to_server: true,
    response: None,
    access: Access::invoke(Privilege::Manage),
    conform: Conform {
        clauses: &[Clause {
            verdict: Conformance::Mandatory,
            when: Condition::Feature(0),
        }],
    },
}];

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

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