Skip to main content

rs_matter/im/encoding/
status.rs

1/*
2 *
3 *    Copyright (c) 2025-2026 Project CHIP Authors
4 *
5 *    Licensed under the Apache License, Version 2.0 (the "License");
6 *    you may not use this file except in compliance with the License.
7 *    You may obtain a copy of the License at
8 *
9 *        http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *    Unless required by applicable law or agreed to in writing, software
12 *    distributed under the License is distributed on an "AS IS" BASIS,
13 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *    See the License for the specific language governing permissions and
15 *    limitations under the License.
16 */
17
18//! This module defines the `Status` and `StatusResp` structures used in the Interaction Model.
19
20use crate::error::Error;
21use crate::tlv::{FromTLV, TagType, ToTLV};
22use crate::utils::storage::WriteBuf;
23
24use super::{IMStatusCode, IM_REVISION};
25
26/// An IM status structure that contains an `IMStatusCode` and an optional cluster status code.
27///
28/// Corresponds to the `StatusIB` block in the Matter Interaction Model.
29#[derive(Debug, Clone, PartialEq, Eq, Hash, FromTLV, ToTLV)]
30#[cfg_attr(feature = "defmt", derive(defmt::Format))]
31pub struct Status {
32    /// The status code of the IM operation.
33    pub status: IMStatusCode,
34    /// An optional cluster status code, which is used for cluster-specific status codes.
35    pub cluster_status: Option<u16>,
36}
37
38impl Status {
39    /// Create a new `Status` instance with the given `IMStatusCode` and an optional cluster status code.
40    pub const fn new(status: IMStatusCode, cluster_status: Option<u16>) -> Status {
41        Status {
42            status,
43            cluster_status,
44        }
45    }
46}
47
48/// An IM status response structure used for sending/receiving status responses in the Interaction Model.
49///
50/// Corresponds to the `StatusResponseMessage` struct in the Matter Interaction Model.
51#[derive(Debug, Clone, PartialEq, Eq, Hash, FromTLV, ToTLV)]
52#[cfg_attr(feature = "defmt", derive(defmt::Format))]
53pub struct StatusResp {
54    pub status: IMStatusCode,
55    /// `interactionModelRevision` — mandatory in every IM message we send;
56    /// modelled as `Option<u8>` so we tolerate peers that omit it (the C++
57    /// SDK is tolerant in practice).
58    #[tagval(crate::im::encoding::IM_REVISION_TAG)]
59    pub interaction_model_revision: Option<u8>,
60}
61
62// Custom `Default` (rather than derived) so the trailing
63// `interaction_model_revision` defaults to `Some(IM_REVISION)` instead of
64// `None` — that way struct-literal fallbacks like
65// `StatusResp { status, ..Default::default() }` produce a spec-compliant
66// on-the-wire response without each caller setting it.
67impl Default for StatusResp {
68    fn default() -> Self {
69        Self {
70            status: IMStatusCode::Success,
71            interaction_model_revision: Some(IM_REVISION),
72        }
73    }
74}
75
76impl StatusResp {
77    pub fn write(wb: &mut WriteBuf, status: IMStatusCode) -> Result<(), Error> {
78        Self {
79            status,
80            ..Default::default()
81        }
82        .to_tlv(&TagType::Anonymous, &mut *wb)
83    }
84}