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
/*
 *
 *    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 core::cell::RefCell;
use core::convert::TryInto;

use strum::{EnumDiscriminants, FromRepr};

use crate::acl::{self, AclEntry, AclMgr};
use crate::data_model::objects::*;
use crate::interaction_model::messages::ib::{attr_list_write, ListOperation};
use crate::tlv::{FromTLV, TLVElement, TagType, ToTLV};
use crate::utils::rand::Rand;
use crate::{attribute_enum, error::*};
use log::{error, info};

pub const ID: u32 = 0x001F;

#[derive(FromRepr, EnumDiscriminants)]
#[repr(u16)]
pub enum Attributes {
    Acl(()) = 0,
    Extension(()) = 1,
    SubjectsPerEntry(AttrType<u16>) = 2,
    TargetsPerEntry(AttrType<u16>) = 3,
    EntriesPerFabric(AttrType<u16>) = 4,
}

attribute_enum!(Attributes);

pub const CLUSTER: Cluster<'static> = Cluster {
    id: ID,
    feature_map: 0,
    attributes: &[
        FEATURE_MAP,
        ATTRIBUTE_LIST,
        Attribute::new(
            AttributesDiscriminants::Acl as u16,
            Access::RWFA,
            Quality::NONE,
        ),
        Attribute::new(
            AttributesDiscriminants::Extension as u16,
            Access::RWFA,
            Quality::NONE,
        ),
        Attribute::new(
            AttributesDiscriminants::SubjectsPerEntry as u16,
            Access::RV,
            Quality::FIXED,
        ),
        Attribute::new(
            AttributesDiscriminants::TargetsPerEntry as u16,
            Access::RV,
            Quality::FIXED,
        ),
        Attribute::new(
            AttributesDiscriminants::EntriesPerFabric as u16,
            Access::RV,
            Quality::FIXED,
        ),
    ],
    commands: &[],
};

pub struct AccessControlCluster<'a> {
    data_ver: Dataver,
    acl_mgr: &'a RefCell<AclMgr>,
}

impl<'a> AccessControlCluster<'a> {
    pub fn new(acl_mgr: &'a RefCell<AclMgr>, rand: Rand) -> Self {
        Self {
            data_ver: Dataver::new(rand),
            acl_mgr,
        }
    }

    pub fn read(&self, attr: &AttrDetails, encoder: AttrDataEncoder) -> Result<(), Error> {
        if let Some(mut writer) = encoder.with_dataver(self.data_ver.get())? {
            if attr.is_system() {
                CLUSTER.read(attr.attr_id, writer)
            } else {
                match attr.attr_id.try_into()? {
                    Attributes::Acl(_) => {
                        writer.start_array(AttrDataWriter::TAG)?;
                        self.acl_mgr.borrow().for_each_acl(|entry| {
                            if !attr.fab_filter || Some(attr.fab_idx) == entry.fab_idx {
                                entry.to_tlv(&mut writer, TagType::Anonymous)?;
                            }

                            Ok(())
                        })?;
                        writer.end_container()?;

                        writer.complete()
                    }
                    Attributes::Extension(_) => {
                        // Empty for now
                        writer.start_array(AttrDataWriter::TAG)?;
                        writer.end_container()?;

                        writer.complete()
                    }
                    Attributes::SubjectsPerEntry(codec) => {
                        codec.encode(writer, acl::SUBJECTS_PER_ENTRY as u16)
                    }
                    Attributes::TargetsPerEntry(codec) => {
                        codec.encode(writer, acl::TARGETS_PER_ENTRY as u16)
                    }
                    Attributes::EntriesPerFabric(codec) => {
                        codec.encode(writer, acl::ENTRIES_PER_FABRIC as u16)
                    }
                }
            }
        } else {
            Ok(())
        }
    }

    pub fn write(&self, attr: &AttrDetails, data: AttrData) -> Result<(), Error> {
        match attr.attr_id.try_into()? {
            Attributes::Acl(_) => {
                attr_list_write(attr, data.with_dataver(self.data_ver.get())?, |op, data| {
                    self.write_acl_attr(&op, data, attr.fab_idx)
                })
            }
            _ => {
                error!("Attribute not yet supported: this shouldn't happen");
                Err(ErrorCode::AttributeNotFound.into())
            }
        }
    }

    /// Write the ACL Attribute
    ///
    /// This takes care of 4 things, add item, edit item, delete item, delete list.
    /// Care about fabric-scoped behaviour is taken
    fn write_acl_attr(
        &self,
        op: &ListOperation,
        data: &TLVElement,
        fab_idx: u8,
    ) -> Result<(), Error> {
        info!("Performing ACL operation {:?}", op);
        match op {
            ListOperation::AddItem | ListOperation::EditItem(_) => {
                let mut acl_entry = AclEntry::from_tlv(data)?;
                info!("ACL  {:?}", acl_entry);
                // Overwrite the fabric index with our accessing fabric index
                acl_entry.fab_idx = Some(fab_idx);

                if let ListOperation::EditItem(index) = op {
                    self.acl_mgr
                        .borrow_mut()
                        .edit(*index as u8, fab_idx, acl_entry)
                } else {
                    self.acl_mgr.borrow_mut().add(acl_entry)
                }
            }
            ListOperation::DeleteItem(index) => {
                self.acl_mgr.borrow_mut().delete(*index as u8, fab_idx)
            }
            ListOperation::DeleteList => self.acl_mgr.borrow_mut().delete_for_fabric(fab_idx),
        }
    }
}

impl<'a> Handler for AccessControlCluster<'a> {
    fn read(&self, attr: &AttrDetails, encoder: AttrDataEncoder) -> Result<(), Error> {
        AccessControlCluster::read(self, attr, encoder)
    }

    fn write(&self, attr: &AttrDetails, data: AttrData) -> Result<(), Error> {
        AccessControlCluster::write(self, attr, data)
    }
}

impl<'a> NonBlockingHandler for AccessControlCluster<'a> {}

impl<'a> ChangeNotifier<()> for AccessControlCluster<'a> {
    fn consume_change(&mut self) -> Option<()> {
        self.data_ver.consume_change(())
    }
}

#[cfg(test)]
mod tests {
    use core::cell::RefCell;

    use crate::{
        acl::{AclEntry, AclMgr, AuthMode},
        data_model::objects::{AttrDataEncoder, AttrDetails, Node, Privilege},
        interaction_model::messages::ib::ListOperation,
        tlv::{get_root_node_struct, ElementType, TLVElement, TLVWriter, TagType, ToTLV},
        utils::{rand::dummy_rand, writebuf::WriteBuf},
    };

    use super::AccessControlCluster;

    #[test]
    /// Add an ACL entry
    fn acl_cluster_add() {
        let mut buf: [u8; 100] = [0; 100];
        let mut writebuf = WriteBuf::new(&mut buf);
        let mut tw = TLVWriter::new(&mut writebuf);

        let acl_mgr = RefCell::new(AclMgr::new());
        let acl = AccessControlCluster::new(&acl_mgr, dummy_rand);

        let new = AclEntry::new(2, Privilege::VIEW, AuthMode::Case);
        new.to_tlv(&mut tw, TagType::Anonymous).unwrap();
        let data = get_root_node_struct(writebuf.as_slice()).unwrap();

        // Test, ACL has fabric index 2, but the accessing fabric is 1
        //    the fabric index in the TLV should be ignored and the ACL should be created with entry 1
        let result = acl.write_acl_attr(&ListOperation::AddItem, &data, 1);
        assert!(result.is_ok());

        let verifier = AclEntry::new(1, Privilege::VIEW, AuthMode::Case);
        acl_mgr
            .borrow()
            .for_each_acl(|a| {
                assert_eq!(*a, verifier);
                Ok(())
            })
            .unwrap();
    }

    #[test]
    /// - The listindex used for edit should be relative to the current fabric
    fn acl_cluster_edit() {
        let mut buf: [u8; 100] = [0; 100];
        let mut writebuf = WriteBuf::new(&mut buf);
        let mut tw = TLVWriter::new(&mut writebuf);

        // Add 3 ACLs, belonging to fabric index 2, 1 and 2, in that order
        let acl_mgr = RefCell::new(AclMgr::new());
        let mut verifier = [
            AclEntry::new(2, Privilege::VIEW, AuthMode::Case),
            AclEntry::new(1, Privilege::VIEW, AuthMode::Case),
            AclEntry::new(2, Privilege::ADMIN, AuthMode::Case),
        ];
        for i in &verifier {
            acl_mgr.borrow_mut().add(i.clone()).unwrap();
        }
        let acl = AccessControlCluster::new(&acl_mgr, dummy_rand);

        let new = AclEntry::new(2, Privilege::VIEW, AuthMode::Case);
        new.to_tlv(&mut tw, TagType::Anonymous).unwrap();
        let data = get_root_node_struct(writebuf.as_slice()).unwrap();

        // Test, Edit Fabric 2's index 1 - with accessing fabring as 2 - allow
        let result = acl.write_acl_attr(&ListOperation::EditItem(1), &data, 2);
        // Fabric 2's index 1, is actually our index 2, update the verifier
        verifier[2] = new;
        assert!(result.is_ok());

        // Also validate in the acl_mgr that the entries are in the right order
        let mut index = 0;
        acl_mgr
            .borrow()
            .for_each_acl(|a| {
                assert_eq!(*a, verifier[index]);
                index += 1;
                Ok(())
            })
            .unwrap();
    }

    #[test]
    /// - The listindex used for delete should be relative to the current fabric
    fn acl_cluster_delete() {
        // Add 3 ACLs, belonging to fabric index 2, 1 and 2, in that order
        let acl_mgr = RefCell::new(AclMgr::new());
        let input = [
            AclEntry::new(2, Privilege::VIEW, AuthMode::Case),
            AclEntry::new(1, Privilege::VIEW, AuthMode::Case),
            AclEntry::new(2, Privilege::ADMIN, AuthMode::Case),
        ];
        for i in &input {
            acl_mgr.borrow_mut().add(i.clone()).unwrap();
        }
        let acl = AccessControlCluster::new(&acl_mgr, dummy_rand);
        // data is don't-care actually
        let data = TLVElement::new(TagType::Anonymous, ElementType::True);

        // Test , Delete Fabric 1's index 0
        let result = acl.write_acl_attr(&ListOperation::DeleteItem(0), &data, 1);
        assert!(result.is_ok());

        let verifier = [input[0].clone(), input[2].clone()];
        // Also validate in the acl_mgr that the entries are in the right order
        let mut index = 0;
        acl_mgr
            .borrow()
            .for_each_acl(|a| {
                assert_eq!(*a, verifier[index]);
                index += 1;
                Ok(())
            })
            .unwrap();
    }

    #[test]
    /// - acl read with and without fabric filtering
    fn acl_cluster_read() {
        let mut buf: [u8; 100] = [0; 100];
        let mut writebuf = WriteBuf::new(&mut buf);

        // Add 3 ACLs, belonging to fabric index 2, 1 and 2, in that order
        let acl_mgr = RefCell::new(AclMgr::new());
        let input = [
            AclEntry::new(2, Privilege::VIEW, AuthMode::Case),
            AclEntry::new(1, Privilege::VIEW, AuthMode::Case),
            AclEntry::new(2, Privilege::ADMIN, AuthMode::Case),
        ];
        for i in input {
            acl_mgr.borrow_mut().add(i).unwrap();
        }
        let acl = AccessControlCluster::new(&acl_mgr, dummy_rand);
        // Test 1, all 3 entries are read in the response without fabric filtering
        {
            let attr = AttrDetails {
                node: &Node {
                    id: 0,
                    endpoints: &[],
                },
                endpoint_id: 0,
                cluster_id: 0,
                attr_id: 0,
                list_index: None,
                fab_idx: 1,
                fab_filter: false,
                dataver: None,
                wildcard: false,
            };

            let mut tw = TLVWriter::new(&mut writebuf);
            let encoder = AttrDataEncoder::new(&attr, &mut tw);

            acl.read(&attr, encoder).unwrap();
            assert_eq!(
                // &[
                //     21, 53, 1, 36, 0, 0, 55, 1, 24, 54, 2, 21, 36, 1, 1, 36, 2, 2, 54, 3, 24, 54,
                //     4, 24, 36, 254, 2, 24, 21, 36, 1, 1, 36, 2, 2, 54, 3, 24, 54, 4, 24, 36, 254,
                //     1, 24, 21, 36, 1, 5, 36, 2, 2, 54, 3, 24, 54, 4, 24, 36, 254, 2, 24, 24, 24,
                //     24
                // ],
                &[
                    21, 53, 1, 36, 0, 0, 55, 1, 36, 2, 0, 36, 3, 0, 36, 4, 0, 24, 54, 2, 21, 36, 1,
                    1, 36, 2, 2, 54, 3, 24, 54, 4, 24, 36, 254, 2, 24, 21, 36, 1, 1, 36, 2, 2, 54,
                    3, 24, 54, 4, 24, 36, 254, 1, 24, 21, 36, 1, 5, 36, 2, 2, 54, 3, 24, 54, 4, 24,
                    36, 254, 2, 24, 24, 24, 24
                ],
                writebuf.as_slice()
            );
        }
        writebuf.reset();

        // Test 2, only single entry is read in the response with fabric filtering and fabric idx 1
        {
            let attr = AttrDetails {
                node: &Node {
                    id: 0,
                    endpoints: &[],
                },
                endpoint_id: 0,
                cluster_id: 0,
                attr_id: 0,
                list_index: None,
                fab_idx: 1,
                fab_filter: true,
                dataver: None,
                wildcard: false,
            };

            let mut tw = TLVWriter::new(&mut writebuf);
            let encoder = AttrDataEncoder::new(&attr, &mut tw);

            acl.read(&attr, encoder).unwrap();
            assert_eq!(
                // &[
                //     21, 53, 1, 36, 0, 0, 55, 1, 24, 54, 2, 21, 36, 1, 1, 36, 2, 2, 54, 3, 24, 54,
                //     4, 24, 36, 254, 1, 24, 24, 24, 24
                // ],
                &[
                    21, 53, 1, 36, 0, 0, 55, 1, 36, 2, 0, 36, 3, 0, 36, 4, 0, 24, 54, 2, 21, 36, 1,
                    1, 36, 2, 2, 54, 3, 24, 54, 4, 24, 36, 254, 1, 24, 24, 24, 24
                ],
                writebuf.as_slice()
            );
        }
        writebuf.reset();

        // Test 3, only single entry is read in the response with fabric filtering and fabric idx 2
        {
            let attr = AttrDetails {
                node: &Node {
                    id: 0,
                    endpoints: &[],
                },
                endpoint_id: 0,
                cluster_id: 0,
                attr_id: 0,
                list_index: None,
                fab_idx: 2,
                fab_filter: true,
                dataver: None,
                wildcard: false,
            };

            let mut tw = TLVWriter::new(&mut writebuf);
            let encoder = AttrDataEncoder::new(&attr, &mut tw);

            acl.read(&attr, encoder).unwrap();
            assert_eq!(
                // &[
                //     21, 53, 1, 36, 0, 0, 55, 1, 24, 54, 2, 21, 36, 1, 1, 36, 2, 2, 54, 3, 24, 54,
                //     4, 24, 36, 254, 2, 24, 21, 36, 1, 5, 36, 2, 2, 54, 3, 24, 54, 4, 24, 36, 254,
                //     2, 24, 24, 24, 24
                // ],
                &[
                    21, 53, 1, 36, 0, 0, 55, 1, 36, 2, 0, 36, 3, 0, 36, 4, 0, 24, 54, 2, 21, 36, 1,
                    1, 36, 2, 2, 54, 3, 24, 54, 4, 24, 36, 254, 2, 24, 21, 36, 1, 5, 36, 2, 2, 54,
                    3, 24, 54, 4, 24, 36, 254, 2, 24, 24, 24, 24
                ],
                writebuf.as_slice()
            );
        }
    }
}