15
15
* limitations under the License.
16
16
*/
17
17
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
+ } ;
19
23
20
24
/// 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 ) {
22
26
let mut index = 0 ;
23
27
24
28
// 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]) {
29
33
AttrResp :: Data ( d) => {
30
34
// We don't match the data-version
31
35
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
+ }
33
39
}
34
40
_ => {
35
41
panic ! ( "Invalid response, expected AttrRespIn::Data" ) ;
@@ -43,12 +49,36 @@ pub fn assert_attr_report(received: &ReportDataMsg, expected: &[AttrResp]) {
43
49
assert_eq ! ( index, expected. len( ) ) ;
44
50
}
45
51
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
+
46
60
// We have to hard-code this here, and it should match the tag
47
61
// of the 'data' part in AttrData
48
62
pub const ATTR_DATA_TAG_DATA : u8 = 2 ;
49
63
50
64
#[ macro_export]
51
65
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 {
52
82
( $path: expr, $data: expr) => {
53
83
AttrResp :: Data ( AttrData {
54
84
data_ver: None ,
@@ -69,3 +99,37 @@ macro_rules! attr_status {
69
99
AttrResp :: Status ( AttrStatus :: new( $path, $status, 0 ) )
70
100
} ;
71
101
}
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
+ }
0 commit comments