Skip to content

Commit dd3f85d

Browse files
authored
Merge pull request project-chip#31 from kedars/feature/large_read_tests
Tests for multi-leg read/subscribe
2 parents 566db62 + f05a20b commit dd3f85d

File tree

13 files changed

+355
-71
lines changed

13 files changed

+355
-71
lines changed

matter/src/core.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,14 @@ impl Matter {
6161
let mdns = Mdns::get()?;
6262
mdns.set_values(dev_det.vid, dev_det.pid, &dev_det.device_name);
6363

64-
print_pairing_code_and_qr(&dev_det, &dev_comm, DiscoveryCapabilities::default());
65-
6664
let fabric_mgr = Arc::new(FabricMgr::new()?);
65+
let open_comm_window = fabric_mgr.is_empty();
66+
if open_comm_window {
67+
print_pairing_code_and_qr(&dev_det, &dev_comm, DiscoveryCapabilities::default());
68+
}
69+
6770
let acl_mgr = Arc::new(AclMgr::new()?);
6871
let mut pase = PaseMgr::new();
69-
let open_comm_window = fabric_mgr.is_empty();
7072
let data_model =
7173
DataModel::new(dev_det, dev_att, fabric_mgr.clone(), acl_mgr, pase.clone())?;
7274
let mut matter = Box::new(Matter {

matter/src/data_model/cluster_basic_information.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use num_derive::FromPrimitive;
2222
pub const ID: u32 = 0x0028;
2323

2424
#[derive(FromPrimitive)]
25-
enum Attributes {
25+
pub enum Attributes {
2626
DMRevision = 0,
2727
VendorId = 2,
2828
ProductId = 4,

matter/src/data_model/cluster_media_playback.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ enum PlaybackState {
5555
Playing = 0,
5656
Paused = 1,
5757
NotPlaying = 2,
58-
BUFFERING = 3,
58+
Buffering = 3,
5959
}
6060
#[derive(FromPrimitive)]
6161
enum CommandStatus {
@@ -186,10 +186,7 @@ impl MediaPlaybackCluster {
186186
}
187187

188188
pub fn add_callback(&mut self, name: Commands, callback: Box<dyn FnMut()>) {
189-
self.callbacks.push(ClusterCallback {
190-
name,
191-
callback: callback,
192-
});
189+
self.callbacks.push(ClusterCallback { name, callback });
193190
}
194191

195192
fn run_callback(&mut self, name: Commands) {

matter/src/data_model/core/read.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ impl DataModel {
221221
//
222222
// This is the amount of space we reserve for other things to be attached towards
223223
// the end
224-
const RESERVE_SIZE: usize = 18;
224+
const RESERVE_SIZE: usize = 24;
225225
let mut new_wb = wb_shrink!(old_wb, RESERVE_SIZE);
226226
let mut tw = TLVWriter::new(&mut new_wb);
227227

matter/src/data_model/objects/encoder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub enum EncodeValue<'a> {
4242
Tlv(TLVElement<'a>),
4343
/// This indicates a static value. This variant is typically used in the transmit/
4444
/// to-tlv path
45-
Value(&'a (dyn ToTLV)),
45+
Value(&'a dyn ToTLV),
4646
}
4747

4848
impl<'a> EncodeValue<'a> {

matter/src/data_model/sdm/noc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ pub enum Commands {
8181
}
8282

8383
#[derive(FromPrimitive)]
84-
enum Attributes {
84+
pub enum Attributes {
8585
NOCs = 0,
8686
Fabrics = 1,
8787
SupportedFabrics = 2,

matter/src/data_model/system_model/descriptor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub const ID: u32 = 0x001D;
2828

2929
#[derive(FromPrimitive)]
3030
#[allow(clippy::enum_variant_names)]
31-
enum Attributes {
31+
pub enum Attributes {
3232
DeviceTypeList = 0,
3333
ServerList = 1,
3434
ClientList = 2,

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/common/attributes.rs

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,14 @@
1515
* limitations under the License.
1616
*/
1717

18-
use matter::interaction_model::{messages::ib::AttrResp, messages::msg::ReportDataMsg};
18+
use matter::{
19+
interaction_model::{messages::ib::AttrResp, messages::msg::ReportDataMsg},
20+
tlv::{TLVElement, TLVList, TLVWriter, TagType, ToTLV},
21+
utils::writebuf::WriteBuf,
22+
};
1923

2024
/// Assert that the data received in the outbuf matches our expectations
21-
pub fn assert_attr_report(received: &ReportDataMsg, expected: &[AttrResp]) {
25+
pub fn __assert_attr_report(received: &ReportDataMsg, expected: &[AttrResp], skip_data: bool) {
2226
let mut index = 0;
2327

2428
// We can't use assert_eq because it will also try to match data-version
@@ -29,7 +33,9 @@ pub fn assert_attr_report(received: &ReportDataMsg, expected: &[AttrResp]) {
2933
AttrResp::Data(d) => {
3034
// We don't match the data-version
3135
assert_eq!(e_d.path, d.path);
32-
assert_eq!(e_d.data, d.data);
36+
if !skip_data {
37+
assert_eq!(e_d.data, d.data);
38+
}
3339
}
3440
_ => {
3541
panic!("Invalid response, expected AttrRespIn::Data");
@@ -43,12 +49,36 @@ pub fn assert_attr_report(received: &ReportDataMsg, expected: &[AttrResp]) {
4349
assert_eq!(index, expected.len());
4450
}
4551

52+
pub fn assert_attr_report(received: &ReportDataMsg, expected: &[AttrResp]) {
53+
__assert_attr_report(received, expected, false)
54+
}
55+
56+
pub fn assert_attr_report_skip_data(received: &ReportDataMsg, expected: &[AttrResp]) {
57+
__assert_attr_report(received, expected, true)
58+
}
59+
4660
// We have to hard-code this here, and it should match the tag
4761
// of the 'data' part in AttrData
4862
pub const ATTR_DATA_TAG_DATA: u8 = 2;
4963

5064
#[macro_export]
5165
macro_rules! attr_data {
66+
($endpoint:expr, $cluster:expr, $attr: expr, $data:expr) => {
67+
AttrResp::Data(AttrData {
68+
data_ver: None,
69+
path: AttrPath {
70+
endpoint: Some($endpoint),
71+
cluster: Some($cluster),
72+
attr: Some($attr as u16),
73+
..Default::default()
74+
},
75+
data: EncodeValue::Tlv(TLVElement::new(TagType::Context(ATTR_DATA_TAG_DATA), $data)),
76+
})
77+
};
78+
}
79+
80+
#[macro_export]
81+
macro_rules! attr_data_path {
5282
($path:expr, $data:expr) => {
5383
AttrResp::Data(AttrData {
5484
data_ver: None,
@@ -69,3 +99,37 @@ macro_rules! attr_status {
6999
AttrResp::Status(AttrStatus::new($path, $status, 0))
70100
};
71101
}
102+
103+
pub struct TLVHolder {
104+
buf: [u8; 100],
105+
used_len: usize,
106+
}
107+
108+
impl TLVHolder {
109+
pub fn new_array<'a, T, I>(ctx_tag: u8, data: I) -> Self
110+
where
111+
T: ToTLV + 'a,
112+
I: IntoIterator<Item = &'a T>,
113+
{
114+
let mut s = Self {
115+
buf: [0; 100],
116+
used_len: 0,
117+
};
118+
let buf_len = s.buf.len();
119+
let mut wb = WriteBuf::new(&mut s.buf, buf_len);
120+
let mut tw = TLVWriter::new(&mut wb);
121+
let _ = tw.start_array(TagType::Context(ctx_tag));
122+
for e in data {
123+
let _ = e.to_tlv(&mut tw, TagType::Anonymous);
124+
}
125+
let _ = tw.end_container();
126+
127+
s.used_len = wb.as_slice().len();
128+
s
129+
}
130+
131+
pub fn to_tlv(&self) -> TLVElement {
132+
let s = &self.buf[..self.used_len];
133+
TLVList::new(s).iter().next().unwrap()
134+
}
135+
}

matter/tests/data_model/acl_and_dataver.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use matter::{
3434
};
3535

3636
use crate::{
37-
attr_data, attr_status,
37+
attr_data_path, attr_status,
3838
common::{
3939
attributes::*,
4040
echo_cluster::{self, ATTR_WRITE_DEFAULT_VALUE},
@@ -151,7 +151,7 @@ fn wc_read_attribute() {
151151

152152
// Test2: Only Single response as only single endpoint is allowed
153153
let input = &[AttrPath::new(&wc_att1)];
154-
let expected = &[attr_data!(ep0_att1, ElementType::U16(0x1234))];
154+
let expected = &[attr_data_path!(ep0_att1, ElementType::U16(0x1234))];
155155
handle_read_reqs(&mut im, peer, input, expected);
156156

157157
// Add ACL to allow our peer to only access endpoint 1
@@ -163,8 +163,8 @@ fn wc_read_attribute() {
163163
// Test3: Both responses are valid
164164
let input = &[AttrPath::new(&wc_att1)];
165165
let expected = &[
166-
attr_data!(ep0_att1, ElementType::U16(0x1234)),
167-
attr_data!(ep1_att1, ElementType::U16(0x1234)),
166+
attr_data_path!(ep0_att1, ElementType::U16(0x1234)),
167+
attr_data_path!(ep1_att1, ElementType::U16(0x1234)),
168168
];
169169
handle_read_reqs(&mut im, peer, input, expected);
170170
}
@@ -201,7 +201,7 @@ fn exact_read_attribute() {
201201

202202
// Test2: Only Single response as only single endpoint is allowed
203203
let input = &[AttrPath::new(&wc_att1)];
204-
let expected = &[attr_data!(ep0_att1, ElementType::U16(0x1234))];
204+
let expected = &[attr_data_path!(ep0_att1, ElementType::U16(0x1234))];
205205
handle_read_reqs(&mut im, peer, input, expected);
206206
}
207207

@@ -565,15 +565,15 @@ fn test_read_data_ver() {
565565
let input = &[AttrPath::new(&wc_ep_att1)];
566566

567567
let expected = &[
568-
attr_data!(
568+
attr_data_path!(
569569
GenericPath::new(
570570
Some(0),
571571
Some(echo_cluster::ID),
572572
Some(echo_cluster::Attributes::Att1 as u32)
573573
),
574574
ElementType::U16(0x1234)
575575
),
576-
attr_data!(
576+
attr_data_path!(
577577
GenericPath::new(
578578
Some(1),
579579
Some(echo_cluster::ID),
@@ -614,7 +614,7 @@ fn test_read_data_ver() {
614614
Some(TLVArray::Slice(&dataver_filter)),
615615
&mut out_buf,
616616
);
617-
let expected_only_one = &[attr_data!(
617+
let expected_only_one = &[attr_data_path!(
618618
GenericPath::new(
619619
Some(1),
620620
Some(echo_cluster::ID),

0 commit comments

Comments
 (0)