rs-matter 0.1.0

Native Rust implementation of the Matter (Smart-Home) ecosystem
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/*
 *
 *    Copyright (c) 2020-2022 Project CHIP Authors
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

use rs_matter::{
    data_model::{
        cluster_on_off,
        objects::{EncodeValue, GlobalElements},
    },
    interaction_model::{
        core::IMStatusCode,
        messages::ib::{AttrData, AttrPath, AttrResp, AttrStatus},
        messages::GenericPath,
    },
    tlv::{ElementType, TLVElement, TLVWriter, TagType},
};

use crate::{
    attr_data, attr_data_path, attr_status,
    common::{attributes::*, echo_cluster, im_engine::ImEngine, init_env_logger},
};

#[test]
fn test_read_success() {
    // 3 Attr Read Requests
    // - first on endpoint 0, att1
    // - second on endpoint 1, att2
    // - third on endpoint 1, attcustom a custom attribute
    init_env_logger();

    let ep0_att1 = GenericPath::new(
        Some(0),
        Some(echo_cluster::ID),
        Some(echo_cluster::AttributesDiscriminants::Att1 as u32),
    );
    let ep1_att2 = GenericPath::new(
        Some(1),
        Some(echo_cluster::ID),
        Some(echo_cluster::AttributesDiscriminants::Att2 as u32),
    );
    let ep1_attcustom = GenericPath::new(
        Some(1),
        Some(echo_cluster::ID),
        Some(echo_cluster::AttributesDiscriminants::AttCustom as u32),
    );
    let input = &[
        AttrPath::new(&ep0_att1),
        AttrPath::new(&ep1_att2),
        AttrPath::new(&ep1_attcustom),
    ];
    let expected = &[
        attr_data_path!(ep0_att1, ElementType::U16(0x1234)),
        attr_data_path!(ep1_att2, ElementType::U16(0x5678)),
        attr_data_path!(
            ep1_attcustom,
            ElementType::U32(echo_cluster::ATTR_CUSTOM_VALUE)
        ),
    ];
    ImEngine::read_reqs(input, expected);
}

#[test]
fn test_read_unsupported_fields() {
    // 6 reads
    // - endpoint doesn't exist - UnsupportedEndpoint
    // - cluster doesn't exist - UnsupportedCluster
    // - cluster doesn't exist and endpoint is wildcard - Silently ignore
    // - attribute doesn't exist - UnsupportedAttribute
    // - attribute doesn't exist and endpoint is wildcard - Silently ignore
    // - attribute doesn't exist and cluster is wildcard - Silently ignore
    init_env_logger();

    let invalid_endpoint = GenericPath::new(
        Some(2),
        Some(echo_cluster::ID),
        Some(echo_cluster::AttributesDiscriminants::Att1 as u32),
    );
    let invalid_cluster = GenericPath::new(
        Some(0),
        Some(0x1234),
        Some(echo_cluster::AttributesDiscriminants::Att1 as u32),
    );
    let invalid_cluster_wc_endpoint = GenericPath::new(
        None,
        Some(0x1234),
        Some(echo_cluster::AttributesDiscriminants::AttCustom as u32),
    );
    let invalid_attribute = GenericPath::new(Some(0), Some(echo_cluster::ID), Some(0x1234));
    let invalid_attribute_wc_endpoint =
        GenericPath::new(None, Some(echo_cluster::ID), Some(0x1234));
    let invalid_attribute_wc_cluster = GenericPath::new(Some(0), None, Some(0x1234));
    let input = &[
        AttrPath::new(&invalid_endpoint),
        AttrPath::new(&invalid_cluster),
        AttrPath::new(&invalid_cluster_wc_endpoint),
        AttrPath::new(&invalid_attribute),
        AttrPath::new(&invalid_attribute_wc_endpoint),
        AttrPath::new(&invalid_attribute_wc_cluster),
    ];

    let expected = &[
        attr_status!(&invalid_endpoint, IMStatusCode::UnsupportedEndpoint),
        attr_status!(&invalid_cluster, IMStatusCode::UnsupportedCluster),
        attr_status!(&invalid_attribute, IMStatusCode::UnsupportedAttribute),
    ];
    ImEngine::read_reqs(input, expected);
}

#[test]
fn test_read_wc_endpoint_all_have_clusters() {
    // 1 Attr Read Requests
    // - wildcard endpoint, att1
    // - 2 responses are expected
    init_env_logger();

    let wc_ep_att1 = GenericPath::new(
        None,
        Some(echo_cluster::ID),
        Some(echo_cluster::AttributesDiscriminants::Att1 as u32),
    );
    let input = &[AttrPath::new(&wc_ep_att1)];

    let expected = &[
        attr_data!(
            0,
            echo_cluster::ID,
            echo_cluster::AttributesDiscriminants::Att1,
            ElementType::U16(0x1234)
        ),
        attr_data!(
            1,
            echo_cluster::ID,
            echo_cluster::AttributesDiscriminants::Att1,
            ElementType::U16(0x1234)
        ),
    ];
    ImEngine::read_reqs(input, expected);
}

#[test]
fn test_read_wc_endpoint_only_1_has_cluster() {
    // 1 Attr Read Requests
    // - wildcard endpoint, on/off Cluster OnOff Attribute
    // - 1 response are expected
    init_env_logger();

    let wc_ep_onoff = GenericPath::new(
        None,
        Some(cluster_on_off::ID),
        Some(cluster_on_off::AttributesDiscriminants::OnOff as u32),
    );
    let input = &[AttrPath::new(&wc_ep_onoff)];

    let expected = &[attr_data_path!(
        GenericPath::new(
            Some(1),
            Some(cluster_on_off::ID),
            Some(cluster_on_off::AttributesDiscriminants::OnOff as u32)
        ),
        ElementType::False
    )];
    ImEngine::read_reqs(input, expected);
}

#[test]
fn test_read_wc_endpoint_wc_attribute() {
    // 1 Attr Read Request
    // - wildcard endpoint, wildcard attribute
    // - 8 responses are expected, 1+3 attributes on endpoint 0, 1+3 on endpoint 1
    init_env_logger();
    let wc_ep_wc_attr = GenericPath::new(None, Some(echo_cluster::ID), None);
    let input = &[AttrPath::new(&wc_ep_wc_attr)];

    let attr_list = TLVHolder::new_array(
        2,
        &[
            GlobalElements::FeatureMap as u16,
            GlobalElements::AttributeList as u16,
            echo_cluster::AttributesDiscriminants::Att1 as u16,
            echo_cluster::AttributesDiscriminants::Att2 as u16,
            echo_cluster::AttributesDiscriminants::AttWrite as u16,
            echo_cluster::AttributesDiscriminants::AttCustom as u16,
        ],
    );
    let attr_list_tlv = attr_list.to_tlv();

    let expected = &[
        attr_data_path!(
            GenericPath::new(
                Some(0),
                Some(echo_cluster::ID),
                Some(GlobalElements::FeatureMap as u32),
            ),
            ElementType::U8(0)
        ),
        attr_data_path!(
            GenericPath::new(
                Some(0),
                Some(echo_cluster::ID),
                Some(GlobalElements::AttributeList as u32),
            ),
            attr_list_tlv.get_element_type().clone()
        ),
        attr_data_path!(
            GenericPath::new(
                Some(0),
                Some(echo_cluster::ID),
                Some(echo_cluster::AttributesDiscriminants::Att1 as u32),
            ),
            ElementType::U16(0x1234)
        ),
        attr_data_path!(
            GenericPath::new(
                Some(0),
                Some(echo_cluster::ID),
                Some(echo_cluster::AttributesDiscriminants::Att2 as u32),
            ),
            ElementType::U16(0x5678)
        ),
        attr_data_path!(
            GenericPath::new(
                Some(0),
                Some(echo_cluster::ID),
                Some(echo_cluster::AttributesDiscriminants::AttCustom as u32),
            ),
            ElementType::U32(echo_cluster::ATTR_CUSTOM_VALUE)
        ),
        attr_data_path!(
            GenericPath::new(
                Some(1),
                Some(echo_cluster::ID),
                Some(GlobalElements::FeatureMap as u32),
            ),
            ElementType::U8(0)
        ),
        attr_data_path!(
            GenericPath::new(
                Some(1),
                Some(echo_cluster::ID),
                Some(GlobalElements::AttributeList as u32),
            ),
            attr_list_tlv.get_element_type().clone()
        ),
        attr_data_path!(
            GenericPath::new(
                Some(1),
                Some(echo_cluster::ID),
                Some(echo_cluster::AttributesDiscriminants::Att1 as u32),
            ),
            ElementType::U16(0x1234)
        ),
        attr_data_path!(
            GenericPath::new(
                Some(1),
                Some(echo_cluster::ID),
                Some(echo_cluster::AttributesDiscriminants::Att2 as u32),
            ),
            ElementType::U16(0x5678)
        ),
        attr_data_path!(
            GenericPath::new(
                Some(1),
                Some(echo_cluster::ID),
                Some(echo_cluster::AttributesDiscriminants::AttCustom as u32),
            ),
            ElementType::U32(echo_cluster::ATTR_CUSTOM_VALUE)
        ),
    ];
    ImEngine::read_reqs(input, expected);
}

#[test]
fn test_write_success() {
    // 2 Attr Write Request
    // - first on endpoint 0, AttWrite
    // - second on endpoint 1, AttWrite
    let val0 = 10;
    let val1 = 15;
    init_env_logger();
    let attr_data0 = |tag, t: &mut TLVWriter| {
        let _ = t.u16(tag, val0);
    };
    let attr_data1 = |tag, t: &mut TLVWriter| {
        let _ = t.u16(tag, val1);
    };

    let ep0_att = GenericPath::new(
        Some(0),
        Some(echo_cluster::ID),
        Some(echo_cluster::AttributesDiscriminants::AttWrite as u32),
    );
    let ep1_att = GenericPath::new(
        Some(1),
        Some(echo_cluster::ID),
        Some(echo_cluster::AttributesDiscriminants::AttWrite as u32),
    );

    let input = &[
        AttrData::new(
            None,
            AttrPath::new(&ep0_att),
            EncodeValue::Closure(&attr_data0),
        ),
        AttrData::new(
            None,
            AttrPath::new(&ep1_att),
            EncodeValue::Closure(&attr_data1),
        ),
    ];
    let expected = &[
        AttrStatus::new(&ep0_att, IMStatusCode::Success, 0),
        AttrStatus::new(&ep1_att, IMStatusCode::Success, 0),
    ];

    let im = ImEngine::new_default();
    let handler = im.handler();

    im.add_default_acl();
    im.handle_write_reqs(&handler, input, expected);

    assert_eq!(val0, handler.echo_cluster(0).att_write.get());
    assert_eq!(val1, handler.echo_cluster(1).att_write.get());
}

#[test]
fn test_write_wc_endpoint() {
    // 1 Attr Write Request
    // - wildcard endpoint, AttWrite
    let val0 = 10;
    init_env_logger();
    let attr_data0 = |tag, t: &mut TLVWriter| {
        let _ = t.u16(tag, val0);
    };

    let ep_att = GenericPath::new(
        None,
        Some(echo_cluster::ID),
        Some(echo_cluster::AttributesDiscriminants::AttWrite as u32),
    );
    let input = &[AttrData::new(
        None,
        AttrPath::new(&ep_att),
        EncodeValue::Closure(&attr_data0),
    )];

    let ep0_att = GenericPath::new(
        Some(0),
        Some(echo_cluster::ID),
        Some(echo_cluster::AttributesDiscriminants::AttWrite as u32),
    );

    let ep1_att = GenericPath::new(
        Some(1),
        Some(echo_cluster::ID),
        Some(echo_cluster::AttributesDiscriminants::AttWrite as u32),
    );
    let expected = &[
        AttrStatus::new(&ep0_att, IMStatusCode::Success, 0),
        AttrStatus::new(&ep1_att, IMStatusCode::Success, 0),
    ];

    let im = ImEngine::new_default();
    let handler = im.handler();

    im.add_default_acl();
    im.handle_write_reqs(&handler, input, expected);

    assert_eq!(val0, handler.echo_cluster(0).att_write.get());
}

#[test]
fn test_write_unsupported_fields() {
    // 7 writes
    // - endpoint doesn't exist - UnsupportedEndpoint
    // - cluster doesn't exist - UnsupportedCluster
    // - attribute doesn't exist - UnsupportedAttribute
    // - cluster doesn't exist and endpoint is wildcard - Silently ignore
    // - attribute doesn't exist and endpoint is wildcard - Silently ignore
    // - cluster is wildcard - Cluster cannot be wildcard - UnsupportedCluster
    // - attribute is wildcard - Attribute cannot be wildcard - UnsupportedAttribute
    init_env_logger();

    let val0 = 50;
    let attr_data0 = |tag, t: &mut TLVWriter| {
        let _ = t.u16(tag, val0);
    };

    let invalid_endpoint = GenericPath::new(
        Some(4),
        Some(echo_cluster::ID),
        Some(echo_cluster::AttributesDiscriminants::AttWrite as u32),
    );
    let invalid_cluster = GenericPath::new(
        Some(0),
        Some(0x1234),
        Some(echo_cluster::AttributesDiscriminants::AttWrite as u32),
    );
    let invalid_attribute = GenericPath::new(Some(0), Some(echo_cluster::ID), Some(0x1234));
    let wc_endpoint_invalid_cluster = GenericPath::new(
        None,
        Some(0x1234),
        Some(echo_cluster::AttributesDiscriminants::AttWrite as u32),
    );
    let wc_endpoint_invalid_attribute =
        GenericPath::new(None, Some(echo_cluster::ID), Some(0x1234));
    let wc_cluster = GenericPath::new(
        Some(0),
        None,
        Some(echo_cluster::AttributesDiscriminants::AttWrite as u32),
    );
    let wc_attribute = GenericPath::new(Some(0), Some(echo_cluster::ID), None);

    let input = &[
        AttrData::new(
            None,
            AttrPath::new(&invalid_endpoint),
            EncodeValue::Closure(&attr_data0),
        ),
        AttrData::new(
            None,
            AttrPath::new(&invalid_cluster),
            EncodeValue::Closure(&attr_data0),
        ),
        AttrData::new(
            None,
            AttrPath::new(&invalid_attribute),
            EncodeValue::Closure(&attr_data0),
        ),
        AttrData::new(
            None,
            AttrPath::new(&wc_endpoint_invalid_cluster),
            EncodeValue::Closure(&attr_data0),
        ),
        AttrData::new(
            None,
            AttrPath::new(&wc_endpoint_invalid_attribute),
            EncodeValue::Closure(&attr_data0),
        ),
        AttrData::new(
            None,
            AttrPath::new(&wc_cluster),
            EncodeValue::Closure(&attr_data0),
        ),
        AttrData::new(
            None,
            AttrPath::new(&wc_attribute),
            EncodeValue::Closure(&attr_data0),
        ),
    ];
    let expected = &[
        AttrStatus::new(&invalid_endpoint, IMStatusCode::UnsupportedEndpoint, 0),
        AttrStatus::new(&invalid_cluster, IMStatusCode::UnsupportedCluster, 0),
        AttrStatus::new(&invalid_attribute, IMStatusCode::UnsupportedAttribute, 0),
        AttrStatus::new(&wc_cluster, IMStatusCode::UnsupportedCluster, 0),
        AttrStatus::new(&wc_attribute, IMStatusCode::UnsupportedAttribute, 0),
    ];
    let im = ImEngine::new_default();
    let handler = im.handler();

    im.add_default_acl();
    im.handle_write_reqs(&handler, input, expected);

    assert_eq!(
        echo_cluster::ATTR_WRITE_DEFAULT_VALUE,
        handler.echo_cluster(0).att_write.get()
    );
}