Skip to main content

rs_matter_node_core/
session.rs

1/*
2 *
3 *    Copyright (c) 2026 Project CHIP Authors
4 *
5 *    Licensed under the Apache License, Version 2.0 (the "License");
6 *    you may not use this file except in compliance with the License.
7 *    You may obtain a copy of the License at
8 *
9 *        http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *    Unless required by applicable law or agreed to in writing, software
12 *    distributed under the License is distributed on an "AS IS" BASIS,
13 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *    See the License for the specific language governing permissions and
15 *    limitations under the License.
16 */
17
18//! What the node state needs to know about sessions: who is on the other end of
19//! a session and how many sessions there are. The sessions themselves are in
20//! `rs-matter-node`.
21
22use core::num::NonZeroU8;
23
24use cfg_if::cfg_if;
25
26use crate::fabric::MAX_FABRICS;
27
28pub const MAX_CAT_IDS_PER_NOC: usize = 3;
29pub type NocCatIds = [u32; MAX_CAT_IDS_PER_NOC];
30
31#[derive(Debug, PartialEq, Eq, Clone, Default)]
32#[cfg_attr(feature = "defmt", derive(defmt::Format))]
33pub enum SessionMode {
34    // The Case session will capture the local fabric index
35    // and the local fabric index
36    Case {
37        fab_idx: NonZeroU8,
38        cat_ids: NocCatIds,
39    },
40    // The Pase session always starts with a fabric index of 0
41    // (i.e. no fabric) but will be upgraded to the actual fabric index
42    // once AddNOC or UpdateNOC is received
43    Pase {
44        fab_idx: u8,
45    },
46    // A group session used for group (multicast) messaging.
47    Group {
48        fab_idx: NonZeroU8,
49        group_id: u16,
50    },
51    #[default]
52    PlainText,
53}
54
55impl SessionMode {
56    pub fn fab_idx(&self) -> u8 {
57        match self {
58            SessionMode::Case { fab_idx, .. } => fab_idx.get(),
59            SessionMode::Pase { fab_idx, .. } => *fab_idx,
60            SessionMode::Group { fab_idx, .. } => fab_idx.get(),
61            SessionMode::PlainText => 0,
62        }
63    }
64}
65
66cfg_if! {
67    if #[cfg(feature = "max-sessions-64")] {
68        /// Max number of supported sessions
69        pub const MAX_SESSIONS: usize = 64;
70    } else if #[cfg(feature = "max-sessions-32")] {
71        /// Max number of supported sessions
72        pub const MAX_SESSIONS: usize = 32;
73    } else if #[cfg(feature = "max-sessions-16")] {
74        /// Max number of supported sessions
75        pub const MAX_SESSIONS: usize = 16;
76    } else if #[cfg(feature = "max-sessions-8")] {
77        /// Max number of supported sessions
78        pub const MAX_SESSIONS: usize = 8;
79    } else if #[cfg(feature = "max-sessions-7")] {
80        /// Max number of supported sessions
81        pub const MAX_SESSIONS: usize = 7;
82    } else if #[cfg(feature = "max-sessions-6")] {
83        /// Max number of supported sessions
84        pub const MAX_SESSIONS: usize = 6;
85    } else if #[cfg(feature = "max-sessions-5")] {
86        /// Max number of supported sessions
87        pub const MAX_SESSIONS: usize = 5;
88    } else if #[cfg(feature = "max-sessions-4")] {
89        /// Max number of supported sessions
90        pub const MAX_SESSIONS: usize = 4;
91    } else if #[cfg(feature = "max-sessions-3")] {
92        /// Max number of supported sessions
93        pub const MAX_SESSIONS: usize = 3;
94    } else {
95        /// Max number of supported sessions
96        pub const MAX_SESSIONS: usize = 16;
97    }
98}
99
100/// The CASE sessions each fabric is guaranteed, as advertised in
101/// `CapabilityMinima.CaseSessionsPerFabric`.
102///
103/// A fabric may use more while the session table has room. Making room in a
104/// full table evicts a CASE session of a fabric within this share only when no
105/// other session can be evicted.
106pub const CASE_SESSIONS_PER_FABRIC: usize = MAX_SESSIONS / MAX_FABRICS;