rs_matter/im/encoding/attr/
subscribe.rs1use core::fmt;
19
20use crate::error::Error;
21use crate::im::{AttrPath, DataVersionFilter, EventFilter, EventPath, IM_REVISION};
22use crate::tlv::{FromTLV, TLVArray, TLVElement, TagType, ToTLV};
23use crate::utils::storage::WriteBuf;
24
25#[derive(Clone, PartialEq, Eq, Hash, FromTLV, ToTLV)]
29#[tlvargs(lifetime = "'a")]
30pub struct SubscribeReq<'a>(TLVElement<'a>);
31
32impl<'a> SubscribeReq<'a> {
33 pub const fn new(element: TLVElement<'a>) -> Self {
35 Self(element)
36 }
37
38 pub fn keep_subs(&self) -> Result<bool, Error> {
40 self.0.r#struct()?.find_ctx(0)?.bool()
41 }
42
43 pub fn min_int_floor(&self) -> Result<u16, Error> {
45 self.0.r#struct()?.find_ctx(1)?.u16()
46 }
47
48 pub fn max_int_ceil(&self) -> Result<u16, Error> {
50 self.0.r#struct()?.find_ctx(2)?.u16()
51 }
52
53 pub fn attr_requests(&self) -> Result<Option<TLVArray<'a, AttrPath>>, Error> {
55 Option::from_tlv(&self.0.r#struct()?.find_ctx(3)?)
56 }
57
58 pub fn event_requests(&self) -> Result<Option<TLVArray<'a, EventPath>>, Error> {
60 Option::from_tlv(&self.0.r#struct()?.find_ctx(4)?)
61 }
62
63 pub fn event_filters(&self) -> Result<Option<TLVArray<'a, EventFilter>>, Error> {
65 Option::from_tlv(&self.0.r#struct()?.find_ctx(5)?)
66 }
67
68 pub fn fabric_filtered(&self) -> Result<bool, Error> {
70 self.0.r#struct()?.find_ctx(7)?.bool()
71 }
72
73 pub fn dataver_filters(&self) -> Result<Option<TLVArray<'a, DataVersionFilter>>, Error> {
75 Option::from_tlv(&self.0.r#struct()?.find_ctx(8)?)
76 }
77}
78
79impl fmt::Debug for SubscribeReq<'_> {
80 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
81 f.debug_struct("SubscribeReqRef")
82 .field("keep_subs", &self.keep_subs())
83 .field("min_int_floor", &self.min_int_floor())
84 .field("max_int_ceil", &self.max_int_ceil())
85 .field("attr_requests", &self.attr_requests())
86 .field("event_requests", &self.event_requests())
87 .field("event_filters", &self.event_filters())
88 .field("fabric_filtered", &self.fabric_filtered())
89 .field("dataver_filters", &self.dataver_filters())
90 .finish()
91 }
92}
93
94#[cfg(feature = "defmt")]
95impl defmt::Format for SubscribeReq<'_> {
96 fn format(&self, f: defmt::Formatter<'_>) {
97 defmt::write!(f,
98 "SubscribeReqRef {{\n keep_subs: {:?},\n min_int_floor: {:?},\n max_int_ceil: {:?},\n attr_requests: {:?},\n event_requests: {:?},\n event_filters: {:?},\n fabric_filtered: {:?},\n dataver_filters: {:?},\n}}",
99 self.keep_subs(),
100 self.min_int_floor(),
101 self.max_int_ceil(),
102 self.attr_requests(),
103 self.event_requests(),
104 self.event_filters(),
105 self.fabric_filtered(),
106 self.dataver_filters(),
107 )
108 }
109}
110
111#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
118#[cfg_attr(feature = "defmt", derive(defmt::Format))]
119#[repr(u8)]
120pub enum SubscribeReqTag {
121 KeepSubs = 0,
122 MinIntFloor = 1,
123 MaxIntCeil = 2,
124 AttrRequests = 3,
125 EventRequests = 4,
126 EventFilters = 5,
127 FabricFiltered = 7,
128 DataVersionFilters = 8,
129}
130
131#[derive(Debug, Clone, FromTLV, ToTLV)]
135#[cfg_attr(feature = "defmt", derive(defmt::Format))]
136pub struct SubscribeResp {
137 pub subs_id: u32,
138 pub _dummy: Option<u32>,
140 pub max_int: u16,
141 #[tagval(crate::im::encoding::IM_REVISION_TAG)]
145 pub interaction_model_revision: Option<u8>,
146}
147
148impl SubscribeResp {
149 pub fn new(subs_id: u32, max_int: u16) -> Self {
151 Self {
152 subs_id,
153 _dummy: None,
154 max_int,
155 interaction_model_revision: Some(IM_REVISION),
156 }
157 }
158
159 pub fn write<'a>(
168 wb: &'a mut WriteBuf,
169 subscription_id: u32,
170 max_int: u16,
171 ) -> Result<&'a [u8], Error> {
172 Self::new(subscription_id, max_int).to_tlv(&TagType::Anonymous, &mut *wb)?;
173 Ok(wb.as_slice())
174 }
175}