Skip to main content

rs_matter/im/encoding/attr/
read.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
18use core::fmt;
19
20use crate::error::Error;
21use crate::im::{AttrPath, DataVersionFilter, EventFilter, EventPath};
22use crate::tlv::{FromTLV, TLVArray, TLVElement, ToTLV};
23
24/// A request to read attributes and events from a Matter device.
25///
26/// Corresponds to the `ReadRequestMessage` TLV structure in the Interaction Model.
27#[derive(Clone, PartialEq, Eq, Hash, FromTLV, ToTLV)]
28#[tlvargs(lifetime = "'a")]
29pub struct ReadReq<'a>(TLVElement<'a>);
30
31impl<'a> ReadReq<'a> {
32    /// Create a new `ReadReq` from a `TLVElement`.
33    pub const fn new(element: TLVElement<'a>) -> Self {
34        Self(element)
35    }
36
37    /// Return the attribute requests in this read request, if any.
38    pub fn attr_requests(&self) -> Result<Option<TLVArray<'a, AttrPath>>, Error> {
39        Option::from_tlv(&self.0.r#struct()?.find_ctx(0)?)
40    }
41
42    /// Return the event requests in this read request, if any.
43    pub fn event_requests(&self) -> Result<Option<TLVArray<'a, EventPath>>, Error> {
44        Option::from_tlv(&self.0.r#struct()?.find_ctx(1)?)
45    }
46
47    /// Return the event filters in this read request, if any.
48    pub fn event_filters(&self) -> Result<Option<TLVArray<'a, EventFilter>>, Error> {
49        Option::from_tlv(&self.0.r#struct()?.find_ctx(2)?)
50    }
51
52    /// Return Ok(`true`) if this read request is fabric-filtered.
53    pub fn fabric_filtered(&self) -> Result<bool, Error> {
54        self.0.r#struct()?.find_ctx(3)?.bool()
55    }
56
57    /// Return the data version filters in this read request, if any.
58    pub fn dataver_filters(&self) -> Result<Option<TLVArray<'a, DataVersionFilter>>, Error> {
59        Option::from_tlv(&self.0.r#struct()?.find_ctx(4)?)
60    }
61}
62
63impl fmt::Debug for ReadReq<'_> {
64    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
65        f.debug_struct("ReadReqRef")
66            .field("attr_requests", &self.attr_requests())
67            .field("event_requests", &self.event_requests())
68            .field("event_filters", &self.event_filters())
69            .field("fabric_filtered", &self.fabric_filtered())
70            .field("dataver_filters", &self.dataver_filters())
71            .finish()
72    }
73}
74
75#[cfg(feature = "defmt")]
76impl defmt::Format for ReadReq<'_> {
77    fn format(&self, f: defmt::Formatter<'_>) {
78        defmt::write!(f,
79            "ReadReqRef {{\n  attr_requests: {:?},\n  event_requests: {:?},\n  event_filters: {:?},\n  fabric_filtered: {:?},\n  dataver_filters: {:?},\n}}",
80            self.attr_requests(),
81            self.event_requests(),
82            self.event_filters(),
83            self.fabric_filtered(),
84            self.dataver_filters(),
85        )
86    }
87}
88
89/// Tags corresponding to the fields in the `ReadReq` TLV structure.
90///
91/// Used when there is a need to perform low-level TLV serde on
92/// `ReadReq` structures.
93#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
94#[cfg_attr(feature = "defmt", derive(defmt::Format))]
95#[repr(u8)]
96pub enum ReadReqTag {
97    AttrRequests = 0,
98    EventRequests = 1,
99    EventFilters = 2,
100    FabricFiltered = 3,
101    DataVersionFilters = 4,
102}