matter-kit 0.3.0

Matter (CSA smart-home standard) protocol implementation in Rust: no_std, no-alloc, executor-agnostic
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
//! The Timed transaction window — Core §8.7.4, §8.7.3.2, §8.8.2.3.
//!
//! A Timed transaction puts a deadline on its second phase:
//!
//! ```text
//! Timed Request (Timeout = 1000 ms)  ──▶
//!                                    ◀──  Status Response (SUCCESS)   ← the clock starts here
//! Write Request / Invoke Request     ──▶   … within 1000 ms, or TIMEOUT
//! ```
//!
//! Elements with the `T` quality require one — `Door Lock`'s unlock,
//! `OpenCommissioningWindow`, Access Control entries — so that a captured message stops being
//! useful shortly after it is captured.
//!
//! [`ClusterHandler`](super::server::ClusterHandler) already refuses an element that requires
//! a Timed transaction when
//! [`InteractionContext::timed`](super::server::InteractionContext::timed) is false.
//! [`TimedWindows`] is what *decides* that flag, applying §8.7.3.2's and §8.8.2.3's rules —
//! the same on both the Write and Invoke sides:
//!
//! | Window | `TimedRequest` flag | Outcome |
//! |---|---|---|
//! | open, not expired | true | proceed, timed |
//! | open, **expired** | either | `TIMEOUT` |
//! | open, not expired | false | `TIMED_REQUEST_MISMATCH` |
//! | none | true | `TIMED_REQUEST_MISMATCH` |
//! | none | false | proceed, untimed |
//!
//! The two failures stay distinct: `TIMEOUT` means retry with a longer timeout,
//! `TIMED_REQUEST_MISMATCH` means the client is wrong. Both were `UNSUPPORTED_ACCESS` before
//! Matter 1.4.

use crate::msg::{ExchangeId, SessionId};
use crate::platform::{Duration, Instant};

use super::status::Status;

/// One open Timed transaction window.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct Window {
    /// The session the Timed Request arrived on.
    session: Option<SessionId>,
    /// The exchange — §8.7.3.2's "matching the same TransactionID".
    exchange: ExchangeId,
    /// When the window closes.
    deadline: Instant,
}

/// The open Timed transaction windows of a node.
///
/// `N` is how many may be open at once. One per concurrent administrator is generous: a Timed
/// transaction's whole life is one round trip plus a timeout the *client* chose, and a client
/// with two open at once on the same session is already misbehaving.
#[derive(Debug, Clone)]
pub struct TimedWindows<const N: usize> {
    windows: [Option<Window>; N],
}

impl<const N: usize> Default for TimedWindows<N> {
    fn default() -> Self {
        Self::new()
    }
}

impl<const N: usize> TimedWindows<N> {
    /// A table with nothing open.
    #[must_use]
    pub const fn new() -> Self {
        Self { windows: [None; N] }
    }

    /// Opens a window, as §8.7.4.3 requires after answering a Timed Request.
    ///
    /// `sent_at` is when the `SUCCESS` status response went out, **not** when the Timed Request
    /// arrived: §8.7.4, "the Timeout interval SHALL start when the Status Response action
    /// acknowledging the Timed Request action with a success code is sent".
    ///
    /// A second Timed Request on the same exchange replaces the first: one transaction, one
    /// deadline.
    ///
    /// # Errors
    ///
    /// [`ErrorCode::NoSpace`](crate::ErrorCode::NoSpace) when every slot holds a live window —
    /// §8.7.3.2's `BUSY` case, so a caller answers [`Status::Busy`] rather than pretending the
    /// window opened.
    pub fn open(
        &mut self,
        session: Option<SessionId>,
        exchange: ExchangeId,
        timeout_ms: u16,
        sent_at: Instant,
    ) -> crate::Result<()> {
        let window = Window {
            session,
            exchange,
            deadline: sent_at.saturating_add(Duration::from_millis(u64::from(timeout_ms))),
        };
        if let Some(slot) = self.slot_for(session, exchange) {
            *slot = Some(window);
            return Ok(());
        }
        if let Some(slot) = self.windows.iter_mut().find(|slot| slot.is_none()) {
            *slot = Some(window);
            return Ok(());
        }
        // Every slot is taken. An expired window is a window whose client is not coming back,
        // so it is the one to give up — at the cost that its owner now gets
        // TIMED_REQUEST_MISMATCH instead of TIMEOUT if it turns up after all. Both are
        // refusals, and holding the slot would refuse a client that is on time.
        let oldest_expired = self
            .windows
            .iter_mut()
            .filter(|slot| slot.is_some_and(|w| w.deadline <= sent_at))
            .min_by_key(|slot| slot.map(|w| w.deadline));
        if let Some(slot) = oldest_expired {
            *slot = Some(window);
            return Ok(());
        }
        Err(crate::Error::new(crate::ErrorCode::NoSpace))
    }

    /// Applies §8.7.3.2 and §8.8.2.3's timed rules to an arriving Write or Invoke Request.
    ///
    /// `timed_request` is the request's own `TimedRequest` flag. Returns what
    /// [`InteractionContext::timed`](super::server::InteractionContext::timed) must be set to,
    /// or the status the interaction terminates with.
    ///
    /// The window is **consumed** either way — every outcome here terminates the transaction,
    /// and a window that survived would let one Timed Request pay for a second request later.
    ///
    /// # Errors
    ///
    /// [`Status::Timeout`] when the window expired, [`Status::TimedRequestMismatch`] when the
    /// flag and the window disagree.
    pub fn check(
        &mut self,
        session: Option<SessionId>,
        exchange: ExchangeId,
        timed_request: bool,
        now: Instant,
    ) -> core::result::Result<bool, Status> {
        let Some(slot) = self.slot_for(session, exchange) else {
            // §8.8.2.3 rule 3: "If this action is marked with TimedRequest as TRUE, but this
            // action is not part of a Timed Invoke transaction … TIMED_REQUEST_MISMATCH".
            return if timed_request {
                Err(Status::TimedRequestMismatch)
            } else {
                Ok(false)
            };
        };
        let expired = slot.is_some_and(|window| now > window.deadline);
        *slot = None;
        if expired {
            // §8.8.2.3 rule 1 is checked before rule 2: an expired window is TIMEOUT whatever
            // the flag says. A client that is both late and wrong is told it was late, which is
            // the fault it can do something about.
            return Err(Status::Timeout);
        }
        if !timed_request {
            // §8.8.2.3 rule 2.
            return Err(Status::TimedRequestMismatch);
        }
        Ok(true)
    }

    /// Closes the window on an exchange without applying any rule.
    ///
    /// For the case where the exchange itself ends — the session closed, the client vanished —
    /// rather than a request arriving on it.
    pub fn close(&mut self, session: Option<SessionId>, exchange: ExchangeId) {
        if let Some(slot) = self.slot_for(session, exchange) {
            *slot = None;
        }
    }

    /// Drops every window of a session, for when the session closes.
    pub fn close_session(&mut self, session: Option<SessionId>) {
        for slot in &mut self.windows {
            if slot.is_some_and(|window| window.session == session) {
                *slot = None;
            }
        }
    }

    /// Drops every window whose deadline has passed, returning how many went.
    ///
    /// Optional: [`TimedWindows::open`] already reclaims expired slots when it needs one. A
    /// window dropped here can no longer produce [`Status::Timeout`], only
    /// [`Status::TimedRequestMismatch`], since the evidence that there was a window is gone.
    pub fn reap(&mut self, now: Instant) -> usize {
        let mut reaped = 0usize;
        for slot in &mut self.windows {
            if slot.is_some_and(|window| now > window.deadline) {
                *slot = None;
                reaped = reaped.saturating_add(1);
            }
        }
        reaped
    }

    /// How many windows are held, expired or not.
    #[must_use]
    pub fn len(&self) -> usize {
        self.windows.iter().filter(|slot| slot.is_some()).count()
    }

    /// Whether no window is held.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// The earliest deadline, for a caller that wants to schedule a reap.
    #[must_use]
    pub fn next_deadline(&self) -> Option<Instant> {
        self.windows
            .iter()
            .filter_map(|slot| slot.map(|window| window.deadline))
            .min()
    }

    fn slot_for(
        &mut self,
        session: Option<SessionId>,
        exchange: ExchangeId,
    ) -> Option<&mut Option<Window>> {
        self.windows.iter_mut().find(|slot| {
            slot.is_some_and(|window| window.session == session && window.exchange == exchange)
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    const SESSION: Option<SessionId> = Some(SessionId(7));
    const EXCHANGE: ExchangeId = ExchangeId(42);

    fn at(ms: u64) -> Instant {
        Instant::ZERO.saturating_add(Duration::from_millis(ms))
    }

    #[test]
    fn an_untimed_request_with_no_window_proceeds() {
        let mut windows = TimedWindows::<2>::new();
        assert_eq!(windows.check(SESSION, EXCHANGE, false, at(0)), Ok(false));
    }

    #[test]
    fn a_timed_request_inside_the_window_proceeds() {
        let mut windows = TimedWindows::<2>::new();
        windows.open(SESSION, EXCHANGE, 1000, at(0)).expect("open");
        assert_eq!(windows.check(SESSION, EXCHANGE, true, at(999)), Ok(true));
    }

    #[test]
    fn the_deadline_is_inclusive_of_its_last_instant() {
        // §8.7.4: "within Timeout milliseconds of sending the Status Response". A request that
        // arrives exactly on the boundary is within it.
        let mut windows = TimedWindows::<2>::new();
        windows.open(SESSION, EXCHANGE, 100, at(0)).expect("open");
        assert_eq!(windows.check(SESSION, EXCHANGE, true, at(100)), Ok(true));

        let mut windows = TimedWindows::<2>::new();
        windows.open(SESSION, EXCHANGE, 100, at(0)).expect("open");
        assert_eq!(
            windows.check(SESSION, EXCHANGE, true, at(101)),
            Err(Status::Timeout)
        );
    }

    #[test]
    fn the_clock_starts_when_the_status_response_is_sent() {
        // §8.7.4: "The Timeout interval SHALL start when the Status Response action
        // acknowledging the Timed Request action with a success code is sent." A server that
        // started it on *receipt* would charge the client for its own processing.
        let mut windows = TimedWindows::<2>::new();
        // The request arrived at 0; the status response went out at 50.
        windows.open(SESSION, EXCHANGE, 100, at(50)).expect("open");
        assert_eq!(windows.check(SESSION, EXCHANGE, true, at(150)), Ok(true));
    }

    #[test]
    fn an_expired_window_is_timeout_not_mismatch() {
        let mut windows = TimedWindows::<2>::new();
        windows.open(SESSION, EXCHANGE, 10, at(0)).expect("open");
        assert_eq!(
            windows.check(SESSION, EXCHANGE, true, at(11)),
            Err(Status::Timeout)
        );
    }

    #[test]
    fn expiry_is_checked_before_the_flag() {
        // §8.8.2.3 lists them in that order. A client that is both late and has its flag clear
        // is told it was late — the fault it can act on.
        let mut windows = TimedWindows::<2>::new();
        windows.open(SESSION, EXCHANGE, 10, at(0)).expect("open");
        assert_eq!(
            windows.check(SESSION, EXCHANGE, false, at(11)),
            Err(Status::Timeout)
        );
    }

    #[test]
    fn a_timed_window_with_an_untimed_request_is_a_mismatch() {
        // §8.8.2.3 rule 2.
        let mut windows = TimedWindows::<2>::new();
        windows.open(SESSION, EXCHANGE, 1000, at(0)).expect("open");
        assert_eq!(
            windows.check(SESSION, EXCHANGE, false, at(1)),
            Err(Status::TimedRequestMismatch)
        );
    }

    #[test]
    fn an_untimed_window_with_a_timed_request_is_a_mismatch() {
        // §8.8.2.3 rule 3. This is the one a replay would hit: a captured Invoke Request with
        // `TimedRequest` set, sent again later with no Timed Request in front of it.
        let mut windows = TimedWindows::<2>::new();
        assert_eq!(
            windows.check(SESSION, EXCHANGE, true, at(0)),
            Err(Status::TimedRequestMismatch)
        );
    }

    #[test]
    fn a_window_is_consumed_by_the_request_it_admits() {
        // Every outcome terminates the transaction, so a second request must not find the
        // window still open — that would let one Timed Request pay for two writes.
        let mut windows = TimedWindows::<2>::new();
        windows.open(SESSION, EXCHANGE, 1000, at(0)).expect("open");
        assert_eq!(windows.check(SESSION, EXCHANGE, true, at(1)), Ok(true));
        assert_eq!(
            windows.check(SESSION, EXCHANGE, true, at(2)),
            Err(Status::TimedRequestMismatch)
        );
        assert!(windows.is_empty());
    }

    #[test]
    fn a_window_is_consumed_even_by_a_request_it_refuses() {
        let mut windows = TimedWindows::<2>::new();
        windows.open(SESSION, EXCHANGE, 1000, at(0)).expect("open");
        assert_eq!(
            windows.check(SESSION, EXCHANGE, false, at(1)),
            Err(Status::TimedRequestMismatch)
        );
        assert!(windows.is_empty());
    }

    #[test]
    fn windows_are_scoped_to_their_exchange_and_session() {
        // §8.7.3.2: "matching the same TransactionID". Another exchange's window must not
        // admit this one's request, or one Timed Request would cover a whole session.
        let mut windows = TimedWindows::<4>::new();
        windows.open(SESSION, EXCHANGE, 1000, at(0)).expect("open");
        assert_eq!(
            windows.check(SESSION, ExchangeId(43), true, at(1)),
            Err(Status::TimedRequestMismatch)
        );
        assert_eq!(
            windows.check(Some(SessionId(8)), EXCHANGE, true, at(1)),
            Err(Status::TimedRequestMismatch)
        );
        // And the original is untouched.
        assert_eq!(windows.check(SESSION, EXCHANGE, true, at(1)), Ok(true));
    }

    #[test]
    fn a_second_timed_request_on_one_exchange_replaces_the_first() {
        let mut windows = TimedWindows::<2>::new();
        windows.open(SESSION, EXCHANGE, 10, at(0)).expect("open");
        windows.open(SESSION, EXCHANGE, 1000, at(0)).expect("open");
        assert_eq!(windows.len(), 1, "one transaction, one deadline");
        assert_eq!(windows.check(SESSION, EXCHANGE, true, at(500)), Ok(true));
    }

    #[test]
    fn a_full_table_of_live_windows_refuses() {
        let mut windows = TimedWindows::<2>::new();
        windows
            .open(SESSION, ExchangeId(1), 1000, at(0))
            .expect("open");
        windows
            .open(SESSION, ExchangeId(2), 1000, at(0))
            .expect("open");
        assert!(
            windows.open(SESSION, ExchangeId(3), 1000, at(0)).is_err(),
            "§8.7.3.2's BUSY case"
        );
        // Both live windows survive the refusal.
        assert_eq!(windows.check(SESSION, ExchangeId(1), true, at(1)), Ok(true));
    }

    #[test]
    fn a_full_table_gives_up_its_oldest_expired_window_first() {
        let mut windows = TimedWindows::<2>::new();
        windows
            .open(SESSION, ExchangeId(1), 10, at(0))
            .expect("open");
        windows
            .open(SESSION, ExchangeId(2), 20, at(0))
            .expect("open");
        windows
            .open(SESSION, ExchangeId(3), 1000, at(100))
            .expect("open");
        // Exchange 1 expired first, so it is the one that went.
        assert_eq!(
            windows.check(SESSION, ExchangeId(1), true, at(101)),
            Err(Status::TimedRequestMismatch)
        );
        assert_eq!(
            windows.check(SESSION, ExchangeId(2), true, at(101)),
            Err(Status::Timeout),
            "the later-expiring window is still there to report TIMEOUT"
        );
    }

    #[test]
    fn closing_a_session_drops_its_windows_and_leaves_others() {
        let mut windows = TimedWindows::<4>::new();
        windows
            .open(SESSION, ExchangeId(1), 1000, at(0))
            .expect("open");
        windows
            .open(Some(SessionId(8)), ExchangeId(1), 1000, at(0))
            .expect("open");
        windows.close_session(SESSION);
        assert_eq!(windows.len(), 1);
        assert_eq!(
            windows.check(Some(SessionId(8)), ExchangeId(1), true, at(1)),
            Ok(true)
        );
    }

    #[test]
    fn reaping_trades_timeout_for_mismatch() {
        let mut windows = TimedWindows::<2>::new();
        windows.open(SESSION, EXCHANGE, 10, at(0)).expect("open");
        assert_eq!(windows.reap(at(11)), 1);
        // The evidence that there was ever a window is gone, so the late client is told its
        // flag was wrong rather than that it was slow. Both refuse; only one is informative.
        assert_eq!(
            windows.check(SESSION, EXCHANGE, true, at(12)),
            Err(Status::TimedRequestMismatch)
        );
    }

    #[test]
    fn reaping_leaves_live_windows_alone() {
        let mut windows = TimedWindows::<2>::new();
        windows.open(SESSION, EXCHANGE, 1000, at(0)).expect("open");
        assert_eq!(windows.reap(at(11)), 0);
        assert_eq!(windows.check(SESSION, EXCHANGE, true, at(12)), Ok(true));
    }

    #[test]
    fn the_next_deadline_is_the_earliest() {
        let mut windows = TimedWindows::<4>::new();
        assert_eq!(windows.next_deadline(), None);
        windows
            .open(SESSION, ExchangeId(1), 500, at(0))
            .expect("open");
        windows
            .open(SESSION, ExchangeId(2), 100, at(0))
            .expect("open");
        assert_eq!(windows.next_deadline(), Some(at(100)));
    }

    #[test]
    fn a_zero_timeout_expires_immediately_but_still_reports_timeout() {
        // A client may legally ask for zero. It gets one instant, and after that TIMEOUT —
        // not a mismatch, because the window really did exist.
        let mut windows = TimedWindows::<2>::new();
        windows.open(SESSION, EXCHANGE, 0, at(0)).expect("open");
        assert_eq!(
            windows.check(SESSION, EXCHANGE, true, at(1)),
            Err(Status::Timeout)
        );
    }
}