Skip to content

Commit 9089ab3

Browse files
committed
Tests: Test subscribe with multi-leg reads
1 parent da8d7a9 commit 9089ab3

File tree

2 files changed

+58
-4
lines changed

2 files changed

+58
-4
lines changed

matter/src/interaction_model/messages.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ pub mod msg {
7676
EventPath,
7777
};
7878

79-
#[derive(FromTLV)]
79+
#[derive(Default, FromTLV, ToTLV)]
8080
#[tlvargs(lifetime = "'a")]
8181
pub struct SubscribeReq<'a> {
8282
pub keep_subs: bool,
@@ -92,6 +92,20 @@ pub mod msg {
9292
}
9393

9494
impl<'a> SubscribeReq<'a> {
95+
pub fn new(fabric_filtered: bool, min_int_floor: u16, max_int_ceil: u16) -> Self {
96+
Self {
97+
fabric_filtered,
98+
min_int_floor,
99+
max_int_ceil,
100+
..Default::default()
101+
}
102+
}
103+
104+
pub fn set_attr_requests(mut self, requests: &'a [AttrPath]) -> Self {
105+
self.attr_requests = Some(TLVArray::new(requests));
106+
self
107+
}
108+
95109
pub fn to_read_req(&self) -> ReadReq<'a> {
96110
ReadReq {
97111
attr_requests: self.attr_requests,
@@ -103,7 +117,7 @@ pub mod msg {
103117
}
104118
}
105119

106-
#[derive(ToTLV)]
120+
#[derive(Debug, FromTLV, ToTLV)]
107121
pub struct SubscribeResp {
108122
pub subs_id: u32,
109123
// The Context Tags are discontiguous for some reason

matter/tests/data_model/long_reads.rs

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ use matter::{
2424
},
2525
interaction_model::{
2626
core::{IMStatusCode, OpCode},
27-
messages::GenericPath,
2827
messages::{
2928
ib::{AttrData, AttrPath, AttrResp},
30-
msg::{ReadReq, ReportDataMsg, StatusResp},
29+
msg::{ReadReq, ReportDataMsg, StatusResp, SubscribeResp},
3130
},
31+
messages::{msg::SubscribeReq, GenericPath},
3232
},
3333
tlv::{self, ElementType, FromTLV, TLVElement, TagType, ToTLV},
3434
transport::{
@@ -182,3 +182,43 @@ fn test_long_read_success() {
182182
assert_eq!(report_data.more_chunks, None);
183183
assert_eq!(out_code, OpCode::ReportData as u8);
184184
}
185+
186+
#[test]
187+
fn test_long_read_subscription_success() {
188+
// Subscribe to the entire attribute database, which requires 2 reads to complete
189+
let _ = env_logger::try_init();
190+
let mut lr = LongRead::new();
191+
let mut output = [0_u8; MAX_RX_BUF_SIZE + 100];
192+
193+
let wc_path = GenericPath::new(None, None, None);
194+
195+
let read_all = [AttrPath::new(&wc_path)];
196+
let subs_req = SubscribeReq::new(true, 1, 20).set_attr_requests(&read_all);
197+
let expected_part1 = wildcard_read_resp(1);
198+
let (out_code, out_data) = lr.process(OpCode::SubscribeRequest, &subs_req, &mut output);
199+
let root = tlv::get_root_node_struct(out_data).unwrap();
200+
let report_data = ReportDataMsg::from_tlv(&root).unwrap();
201+
assert_attr_report_skip_data(&report_data, &expected_part1);
202+
assert_eq!(report_data.more_chunks, Some(true));
203+
assert_eq!(out_code, OpCode::ReportData as u8);
204+
205+
// Ask for the next read by sending a status report
206+
let status_report = StatusResp {
207+
status: IMStatusCode::Success,
208+
};
209+
let expected_part2 = wildcard_read_resp(2);
210+
let (out_code, out_data) = lr.process(OpCode::StatusResponse, &status_report, &mut output);
211+
let root = tlv::get_root_node_struct(out_data).unwrap();
212+
let report_data = ReportDataMsg::from_tlv(&root).unwrap();
213+
assert_attr_report_skip_data(&report_data, &expected_part2);
214+
assert_eq!(report_data.more_chunks, None);
215+
assert_eq!(out_code, OpCode::ReportData as u8);
216+
217+
// Finally confirm subscription
218+
let (out_code, out_data) = lr.process(OpCode::StatusResponse, &status_report, &mut output);
219+
tlv::print_tlv_list(out_data);
220+
let root = tlv::get_root_node_struct(out_data).unwrap();
221+
let subs_resp = SubscribeResp::from_tlv(&root).unwrap();
222+
assert_eq!(out_code, OpCode::SubscriptResponse as u8);
223+
assert_eq!(subs_resp.subs_id, 1);
224+
}

0 commit comments

Comments
 (0)