rs-matter-node-core 0.5.0

Native Rust implementation of the Matter (Smart-Home) ecosystem - what the node is built on: fabrics, ACL, fail-safe
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
/*
 *
 *    Copyright (c) 2026 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.
 */

//! The networks store of the node: the credentials of the wireless networks
//! it may connect to, and their persistence.

use core::fmt;

use rs_matter_tlv::TlvError;

use crate::error::{Error, ErrorCode};
use crate::persist::{KvBlobStoreAccess, Persist};
use crate::utils::sync::MaybeSendSync;

/// Network type supported by the `NetCtl` implementations
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum NetworkType {
    Ethernet,
    Wifi,
    Thread,
}

impl NetworkType {
    /// Check if the given credentials match the network type
    pub fn check_creds(&self, creds: &WirelessCreds<'_>) -> Result<(), Error> {
        match creds {
            WirelessCreds::Wifi { .. } if matches!(self, NetworkType::Wifi) => Ok(()),
            WirelessCreds::Thread { .. } if matches!(self, NetworkType::Thread) => Ok(()),
            _ => Err(ErrorCode::InvalidAction.into()),
        }
    }
}

/// Wireless credentials used for connecting to a network
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub enum WirelessCreds<'a> {
    /// WiFi credentials
    Wifi { ssid: &'a [u8], pass: &'a [u8] },
    /// Thread credentials
    Thread { dataset_tlv: &'a [u8] },
}

impl WirelessCreds<'_> {
    /// Return the network ID of the credentials
    /// For Wifi networks, this is the SSID
    /// For Thread networks, this is the extended PAN ID
    pub fn id(&self) -> Result<&[u8], Error> {
        match self {
            WirelessCreds::Wifi { ssid, .. } => Ok(ssid),
            WirelessCreds::Thread { dataset_tlv } => ThreadTLV::new(dataset_tlv).ext_pan_id(),
        }
    }
}

impl fmt::Display for WirelessCreds<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            WirelessCreds::Wifi { ssid, .. } => write!(
                f,
                "SSID({})",
                core::str::from_utf8(ssid).ok().unwrap_or("???")
            ),
            WirelessCreds::Thread { dataset_tlv } => write!(
                f,
                "ExtEpanId({:?})",
                ThreadTLV::new(dataset_tlv).ext_pan_id().ok().unwrap_or(&[])
            ),
        }
    }
}

#[cfg(feature = "defmt")]
impl defmt::Format for WirelessCreds<'_> {
    fn format(&self, fmt: defmt::Formatter) {
        match self {
            WirelessCreds::Wifi { ssid, .. } => defmt::write!(
                fmt,
                "SSID({})",
                core::str::from_utf8(ssid).ok().unwrap_or("???")
            ),
            WirelessCreds::Thread { dataset_tlv } => defmt::write!(
                fmt,
                "ExtEpanId({:?})",
                ThreadTLV::new(dataset_tlv).ext_pan_id().ok().unwrap_or(&[])
            ),
        }
    }
}

/// Network error type for the `Networks` trait
#[derive(Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum NetworksError {
    NetworkIdNotFound,
    DuplicateNetworkId,
    OutOfRange,
    BoundsExceeded,
    Other(Error),
}

impl From<Error> for NetworksError {
    fn from(err: Error) -> Self {
        NetworksError::Other(err)
    }
}

// TODO: Remove once network errors are refactored.
impl From<TlvError> for NetworksError {
    fn from(err: TlvError) -> Self {
        NetworksError::Other(err.into())
    }
}

/// Trait for managing networks' credentials storage
pub trait Networks: MaybeSendSync {
    /// Return the maximum number of networks supported by the implementation
    ///
    /// For `NetworkType::Ethernet` this method should always return 1
    fn max_networks(&self) -> Result<u8, Error>;

    /// Iterate over the networks recorded in the implementation and call the provided function for each network
    fn networks(&self, f: &mut dyn FnMut(&[u8]) -> Result<(), Error>) -> Result<(), Error>;

    /// Get the credentials for the given network ID by calling the provided function
    ///
    /// For `NetworkType::Ethernet` this method should always fail with an error.
    ///
    /// The function will be called with the credentials for the network ID, or an error if the network ID is not found.
    ///
    /// Return the index of the network ID if found, or an error if not found.
    fn creds(
        &self,
        network_id: &[u8],
        f: &mut dyn FnMut(&WirelessCreds) -> Result<(), Error>,
    ) -> Result<u8, NetworksError>;

    /// Return the next credentials after the ones corresponding to the given network ID by calling the provided function
    ///
    /// For `NetworkType::Ethernet` this method should always fail with an error.
    ///
    /// If the network ID is `None` or credentials with the provided network ID cannot be found,
    /// the first credentials will be returned.
    ///
    /// If the credentials corresponding to the network ID are the last ones recorded in the `Netwrks` trait implementation,
    /// the method will wrap-over and will return the first credentials or even the same credentials again if there is only one
    /// recorded network.
    ///
    /// Return `true` if the credentials were found, `false` otherwise.
    fn next_creds(
        &self,
        last_network_id: Option<&[u8]>,
        f: &mut dyn FnMut(&WirelessCreds) -> Result<(), Error>,
    ) -> Result<bool, Error>;

    /// Return whether the network interface is enabled
    fn enabled(&self) -> Result<bool, Error>;

    /// Set the network interface enabled or disabled
    fn set_enabled(&mut self, enabled: bool) -> Result<(), Error>;

    /// Add or update the credentials for the given network ID
    ///
    /// For `NetworkType::Ethernet` this method should always fail with an error.
    ///
    /// The network ID is derived from the credentials.
    ///
    /// Return the index of the network ID if it was added or updated, or an error if the operation failed.
    fn add_or_update(&mut self, creds: &WirelessCreds<'_>) -> Result<u8, NetworksError>;

    /// Reorder the network with the given index
    ///
    /// For `NetworkType::Ethernet` this method should always fail with an error.
    ///
    /// The index is the new index of the network ID.
    ///
    /// Return the index of the network ID if it was reordered, or an error if the operation failed.
    fn reorder(&mut self, index: u8, network_id: &[u8]) -> Result<u8, NetworksError>;

    /// Remove the network with the given network ID
    ///
    /// For `NetworkType::Ethernet` this method should always fail with an error.
    ///
    /// Return the index of the network ID if it was removed, or an error if the operation failed.
    fn remove(&mut self, network_id: &[u8]) -> Result<u8, NetworksError>;

    /// Return whether the networks store is in "managed" state.
    ///
    /// Managed means: the store holds *committed* operational network
    /// configuration, and the operational connectivity manager
    /// (`WirelessMgr`) may act on it. The store starts unmanaged (never
    /// commissioned, empty), and temporarily *becomes* unmanaged again while
    /// network changes are staged under an armed fail-safe (any successful
    /// `AddOrUpdate*Network` / `RemoveNetwork` / `ReorderNetwork` /
    /// `ConnectNetwork` flips it to `false`) - during such a window the
    /// commissioner drives connectivity explicitly, and the manager must
    /// stand down.
    ///
    /// The state comes back to managed on either exit from the window:
    /// - commit: `CommissioningComplete` calls `set_managed(true)` and
    ///   persists the store;
    /// - revert: the fail-safe expiry reloads the persisted store, which
    ///   always carries the committed - hence managed - state.
    ///
    /// Note that a window which stages no network changes (e.g. opened only
    /// to commission an additional fabric) leaves the store managed, so
    /// connectivity maintenance of the operational network continues
    /// throughout - matching CHIP's behavior.
    fn managed(&self) -> Result<bool, Error>;

    /// Set the managed state of the networks store (see `managed`).
    fn set_managed(&mut self, managed: bool) -> Result<(), Error>;

    /// Reset the networks to the initial state, removing all recorded network credentials
    fn reset(&mut self) -> Result<(), Error>;

    /// Load the networks' credentials from the given data
    fn load(&mut self, data: &[u8]) -> Result<(), Error>;

    /// Save the networks' credentials into the given buffer and return the number of bytes written
    /// or `None` if the networks do not need persistence.
    fn save(&self, buf: &mut [u8]) -> Result<Option<usize>, Error>;

    /// Return the type of the networks the store holds, if it knows it.
    ///
    /// The default implementation returns `None`.
    fn net_type(&self) -> Option<NetworkType> {
        None
    }
}

impl<T> Networks for &mut T
where
    T: Networks,
{
    fn max_networks(&self) -> Result<u8, Error> {
        (**self).max_networks()
    }

    fn networks(&self, f: &mut dyn FnMut(&[u8]) -> Result<(), Error>) -> Result<(), Error> {
        (**self).networks(f)
    }

    fn creds(
        &self,
        network_id: &[u8],
        f: &mut dyn FnMut(&WirelessCreds) -> Result<(), Error>,
    ) -> Result<u8, NetworksError> {
        (**self).creds(network_id, f)
    }

    fn next_creds(
        &self,
        last_network_id: Option<&[u8]>,
        f: &mut dyn FnMut(&WirelessCreds) -> Result<(), Error>,
    ) -> Result<bool, Error> {
        (**self).next_creds(last_network_id, f)
    }

    fn enabled(&self) -> Result<bool, Error> {
        (**self).enabled()
    }

    fn set_enabled(&mut self, enabled: bool) -> Result<(), Error> {
        (*self).set_enabled(enabled)
    }

    fn add_or_update(&mut self, creds: &WirelessCreds<'_>) -> Result<u8, NetworksError> {
        (*self).add_or_update(creds)
    }

    fn reorder(&mut self, index: u8, network_id: &[u8]) -> Result<u8, NetworksError> {
        (*self).reorder(index, network_id)
    }

    fn remove(&mut self, network_id: &[u8]) -> Result<u8, NetworksError> {
        (*self).remove(network_id)
    }

    fn managed(&self) -> Result<bool, Error> {
        (**self).managed()
    }

    fn set_managed(&mut self, managed: bool) -> Result<(), Error> {
        (**self).set_managed(managed)
    }

    fn reset(&mut self) -> Result<(), Error> {
        (**self).reset()
    }

    fn load(&mut self, data: &[u8]) -> Result<(), Error> {
        (**self).load(data)
    }

    fn save(&self, buf: &mut [u8]) -> Result<Option<usize>, Error> {
        (**self).save(buf)
    }

    fn net_type(&self) -> Option<NetworkType> {
        (**self).net_type()
    }
}

impl Networks for &mut dyn Networks {
    fn max_networks(&self) -> Result<u8, Error> {
        (**self).max_networks()
    }

    fn networks(&self, f: &mut dyn FnMut(&[u8]) -> Result<(), Error>) -> Result<(), Error> {
        (**self).networks(f)
    }

    fn creds(
        &self,
        network_id: &[u8],
        f: &mut dyn FnMut(&WirelessCreds) -> Result<(), Error>,
    ) -> Result<u8, NetworksError> {
        (**self).creds(network_id, f)
    }

    fn next_creds(
        &self,
        last_network_id: Option<&[u8]>,
        f: &mut dyn FnMut(&WirelessCreds) -> Result<(), Error>,
    ) -> Result<bool, Error> {
        (**self).next_creds(last_network_id, f)
    }

    fn enabled(&self) -> Result<bool, Error> {
        (**self).enabled()
    }

    fn set_enabled(&mut self, enabled: bool) -> Result<(), Error> {
        (**self).set_enabled(enabled)
    }

    fn add_or_update(&mut self, creds: &WirelessCreds<'_>) -> Result<u8, NetworksError> {
        (**self).add_or_update(creds)
    }

    fn reorder(&mut self, index: u8, network_id: &[u8]) -> Result<u8, NetworksError> {
        (**self).reorder(index, network_id)
    }

    fn remove(&mut self, network_id: &[u8]) -> Result<u8, NetworksError> {
        (**self).remove(network_id)
    }

    fn managed(&self) -> Result<bool, Error> {
        (**self).managed()
    }

    fn set_managed(&mut self, managed: bool) -> Result<(), Error> {
        (**self).set_managed(managed)
    }

    fn reset(&mut self) -> Result<(), Error> {
        (**self).reset()
    }

    fn load(&mut self, data: &[u8]) -> Result<(), Error> {
        (**self).load(data)
    }

    fn save(&self, buf: &mut [u8]) -> Result<Option<usize>, Error> {
        (**self).save(buf)
    }

    fn net_type(&self) -> Option<NetworkType> {
        (**self).net_type()
    }
}

/// A `Networks` implementation with no networks, which refuses to record any.
pub struct DummyNetworks;

impl Networks for DummyNetworks {
    fn max_networks(&self) -> Result<u8, Error> {
        Ok(0)
    }

    fn networks(&self, _f: &mut dyn FnMut(&[u8]) -> Result<(), Error>) -> Result<(), Error> {
        Ok(())
    }

    fn creds(
        &self,
        _network_id: &[u8],
        _f: &mut dyn FnMut(&WirelessCreds) -> Result<(), Error>,
    ) -> Result<u8, NetworksError> {
        Err(NetworksError::NetworkIdNotFound)
    }

    fn next_creds(
        &self,
        _last_network_id: Option<&[u8]>,
        _f: &mut dyn FnMut(&WirelessCreds) -> Result<(), Error>,
    ) -> Result<bool, Error> {
        Ok(false)
    }

    fn enabled(&self) -> Result<bool, Error> {
        Ok(false)
    }

    fn set_enabled(&mut self, _enabled: bool) -> Result<(), Error> {
        Ok(())
    }

    fn add_or_update(&mut self, _creds: &WirelessCreds<'_>) -> Result<u8, NetworksError> {
        Err(NetworksError::Other(ErrorCode::InvalidAction.into()))
    }

    fn reorder(&mut self, _index: u8, _network_id: &[u8]) -> Result<u8, NetworksError> {
        Err(NetworksError::Other(ErrorCode::InvalidAction.into()))
    }

    fn remove(&mut self, _network_id: &[u8]) -> Result<u8, NetworksError> {
        Err(NetworksError::Other(ErrorCode::InvalidAction.into()))
    }

    fn managed(&self) -> Result<bool, Error> {
        Ok(false)
    }

    fn set_managed(&mut self, _managed: bool) -> Result<(), Error> {
        Ok(())
    }

    fn reset(&mut self) -> Result<(), Error> {
        Ok(())
    }

    fn load(&mut self, _data: &[u8]) -> Result<(), Error> {
        Ok(())
    }

    fn save(&self, _buf: &mut [u8]) -> Result<Option<usize>, Error> {
        Ok(None)
    }
}

/// Reload the committed networks of `networks` from `kv`, where they are
/// persisted under `key`; reset the store if nothing is persisted, or if what
/// is cannot be loaded.
pub fn load_networks<N, K>(mut networks: N, key: u16, kv: K) -> Result<(), Error>
where
    N: Networks,
    K: KvBlobStoreAccess,
{
    let loaded = Persist::new(kv).load(key, |data| networks.load(data))?;

    // A load that failed may have stopped half-way
    match loaded {
        Some(()) => Ok(()),
        None => networks.reset(),
    }
}

/// Persist `networks` into `kv`, under `key`.
pub fn store_networks<N, K>(networks: N, key: u16, kv: K) -> Result<(), Error>
where
    N: Networks,
    K: KvBlobStoreAccess,
{
    let mut persist = Persist::new(kv);

    persist.store(key, |buf| networks.save(buf))?;

    persist.run()
}

/// Reset `networks` and remove what is persisted for it in `kv`, under `key`.
pub fn remove_networks<N, K>(mut networks: N, key: u16, kv: K) -> Result<(), Error>
where
    N: Networks,
    K: KvBlobStoreAccess,
{
    networks.reset()?;

    // Strict, as credentials surviving a factory reset must not go unreported
    let mut persist = Persist::strict(kv);

    persist.remove(key)?;

    persist.run()
}

/// A simple Thread TLV reader
#[derive(Debug, Copy, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct ThreadTLV<'a>(&'a [u8]);

impl<'a> ThreadTLV<'a> {
    /// Create a new `ThreadTLV` instance with the given TLV data
    pub const fn new(tlv: &'a [u8]) -> Self {
        Self(tlv)
    }

    /// Get the Extended PAN ID from the operational dataset
    pub fn ext_pan_id(&mut self) -> Result<&'a [u8], Error> {
        const EXT_PAN_ID: u8 = 2;

        let ext_pan_id =
            self.find_map(|(tlv_type, tlv_value)| (tlv_type == EXT_PAN_ID).then_some(tlv_value));

        let Some(ext_pan_id) = ext_pan_id else {
            return Err(ErrorCode::InvalidData.into());
        };

        Ok(ext_pan_id)
    }

    /// Get the next TLV from the data
    ///
    /// Returns `Some` with the TLV type and value if there is a TLV available,
    /// otherwise returns `None`.
    pub fn next_tlv(&mut self) -> Option<(u8, &'a [u8])> {
        const LONG_VALUE_ID: u8 = 255;

        // Adopted from here:
        // https://github.com/openthread/openthread/blob/main/tools/tcat_ble_client/tlv/tlv.py

        let mut slice = self.0;

        (slice.len() >= 2).then_some(())?;

        let tlv_type = slice[0];
        slice = &slice[1..];

        let tlv_len_size = if slice[0] == LONG_VALUE_ID {
            slice = &slice[1..];
            3
        } else {
            1
        };

        (slice.len() >= tlv_len_size).then_some(())?;

        let tlv_len = if tlv_len_size == 1 {
            slice[0] as usize
        } else {
            u32::from_be_bytes([0, slice[0], slice[1], slice[2]]) as usize
        };

        slice = &slice[tlv_len_size..];
        (slice.len() >= tlv_len).then_some(())?;

        let tlv_value = &slice[..tlv_len];

        slice = &slice[tlv_len..];

        self.0 = slice;

        Some((tlv_type, tlv_value))
    }
}

impl<'a> Iterator for ThreadTLV<'a> {
    type Item = (u8, &'a [u8]);

    fn next(&mut self) -> Option<Self::Item> {
        self.next_tlv()
    }
}