18
18
19
19
#include < lib/core/CHIPError.h>
20
20
#include < lib/support/CodeUtils.h>
21
+ #include < lib/support/Pool.h>
21
22
#include < system/TimeSource.h>
22
23
#include < transport/SecureSession.h>
23
24
@@ -43,152 +44,55 @@ class SecureSessionTable
43
44
/* *
44
45
* Allocates a new secure session out of the internal resource pool.
45
46
*
46
- * @param peerNode represents peer Node's ID
47
- * @param peerSessionId represents the encryption key ID assigned by peer node
48
47
* @param localSessionId represents the encryption key ID assigned by local node
49
- * @param state [out] will contain the session if one was available. May be null if no return value is desired.
48
+ * @param peerNodeId represents peer Node's ID
49
+ * @param peerSessionId represents the encryption key ID assigned by peer node
50
+ * @param fabric represents fabric ID for the session
50
51
*
51
52
* @note the newly created state will have an 'active' time set based on the current time source.
52
53
*
53
54
* @returns CHIP_NO_ERROR if state could be initialized. May fail if maximum session count
54
55
* has been reached (with CHIP_ERROR_NO_MEMORY).
55
56
*/
56
57
CHECK_RETURN_VALUE
57
- CHIP_ERROR CreateNewSecureSession (NodeId peerNode, uint16_t peerSessionId , uint16_t localSessionId, SecureSession ** state )
58
+ SecureSession * CreateNewSecureSession (uint16_t localSessionId, NodeId peerNodeId , uint16_t peerSessionId, FabricIndex fabric )
58
59
{
59
- CHIP_ERROR err = CHIP_ERROR_NO_MEMORY;
60
-
61
- if (state)
62
- {
63
- *state = nullptr ;
64
- }
65
-
66
- for (size_t i = 0 ; i < kMaxSessionCount ; i++)
67
- {
68
- if (!mStates [i].IsInitialized ())
69
- {
70
- mStates [i] = SecureSession ();
71
- mStates [i].SetPeerNodeId (peerNode);
72
- mStates [i].SetPeerSessionId (peerSessionId);
73
- mStates [i].SetLocalSessionId (localSessionId);
74
- mStates [i].SetLastActivityTimeMs (mTimeSource .GetCurrentMonotonicTimeMs ());
75
-
76
- if (state)
77
- {
78
- *state = &mStates [i];
79
- }
80
-
81
- err = CHIP_NO_ERROR;
82
- break ;
83
- }
84
- }
85
-
86
- return err;
60
+ return mEntries .CreateObject (localSessionId, peerNodeId, peerSessionId, fabric, mTimeSource .GetCurrentMonotonicTimeMs ());
87
61
}
88
62
89
- /* *
90
- * Get a secure session given a Node Id.
91
- *
92
- * @param nodeId is the session to find (based on nodeId).
93
- * @param begin If a member of the pool, will start search from the next item. Can be nullptr to search from start.
94
- *
95
- * @return the state found, nullptr if not found
96
- */
97
- CHECK_RETURN_VALUE
98
- SecureSession * FindSecureSession (NodeId nodeId, SecureSession * begin)
99
- {
100
- SecureSession * state = nullptr ;
101
- SecureSession * iter = &mStates [0 ];
102
-
103
- if (begin >= iter && begin < &mStates [kMaxSessionCount ])
104
- {
105
- iter = begin + 1 ;
106
- }
63
+ void ReleaseSession (SecureSession * session) { mEntries .ReleaseObject (session); }
107
64
108
- for (; iter < &mStates [kMaxSessionCount ]; iter++)
109
- {
110
- if (!iter->IsInitialized ())
111
- {
112
- continue ;
113
- }
114
- if (iter->GetPeerNodeId () == nodeId)
115
- {
116
- state = iter;
117
- break ;
118
- }
119
- }
120
- return state;
65
+ template <typename Function>
66
+ bool ForEachSession (Function && function)
67
+ {
68
+ return mEntries .ForEachActiveObject (std::forward<Function>(function));
121
69
}
122
70
123
71
/* *
124
72
* Get a secure session given a Node Id and Peer's Encryption Key Id.
125
73
*
126
74
* @param localSessionId Encryption key ID used by the local node.
127
- * @param begin If a member of the pool, will start search from the next item. Can be nullptr to search from start.
128
75
*
129
76
* @return the state found, nullptr if not found
130
77
*/
131
78
CHECK_RETURN_VALUE
132
- SecureSession * FindSecureSessionByLocalKey (uint16_t localSessionId, SecureSession * begin )
79
+ SecureSession * FindSecureSessionByLocalKey (uint16_t localSessionId)
133
80
{
134
- SecureSession * state = nullptr ;
135
- SecureSession * iter = &mStates [0 ];
136
-
137
- if (begin >= iter && begin < &mStates [kMaxSessionCount ])
138
- {
139
- iter = begin + 1 ;
140
- }
141
-
142
- for (; iter < &mStates [kMaxSessionCount ]; iter++)
143
- {
144
- if (!iter->IsInitialized ())
81
+ SecureSession * result = nullptr ;
82
+ mEntries .ForEachActiveObject ([&](auto session) {
83
+ if (session->GetLocalSessionId () == localSessionId)
145
84
{
146
- continue ;
85
+ result = session;
86
+ return false ;
147
87
}
148
- if (iter->GetLocalSessionId () == localSessionId)
149
- {
150
- state = iter;
151
- break ;
152
- }
153
- }
154
- return state;
155
- }
156
-
157
- /* *
158
- * Get the first session that matches the given fabric index.
159
- *
160
- * @param fabric The fabric index to match
161
- *
162
- * @return the session found, nullptr if not found
163
- */
164
- CHECK_RETURN_VALUE
165
- SecureSession * FindSecureSessionByFabric (FabricIndex fabric)
166
- {
167
- for (auto & state : mStates )
168
- {
169
- if (!state.IsInitialized ())
170
- {
171
- continue ;
172
- }
173
- if (state.GetFabricIndex () == fabric)
174
- {
175
- return &state;
176
- }
177
- }
178
- return nullptr ;
88
+ return true ;
89
+ });
90
+ return result;
179
91
}
180
92
181
93
// / Convenience method to mark a session as active
182
94
void MarkSessionActive (SecureSession * state) { state->SetLastActivityTimeMs (mTimeSource .GetCurrentMonotonicTimeMs ()); }
183
95
184
- // / Convenience method to expired a session and fired the related callback
185
- template <typename Callback>
186
- void MarkSessionExpired (SecureSession * state, Callback callback)
187
- {
188
- callback (*state);
189
- *state = SecureSession (PeerAddress::Uninitialized ());
190
- }
191
-
192
96
/* *
193
97
* Iterates through all active sessions and expires any sessions with an idle time
194
98
* larger than the given amount.
@@ -199,30 +103,22 @@ class SecureSessionTable
199
103
void ExpireInactiveSessions (uint64_t maxIdleTimeMs, Callback callback)
200
104
{
201
105
const uint64_t currentTime = mTimeSource .GetCurrentMonotonicTimeMs ();
202
-
203
- for (size_t i = 0 ; i < kMaxSessionCount ; i++)
204
- {
205
- if (!mStates [i].IsInitialized ())
106
+ mEntries .ForEachActiveObject ([&](auto session) {
107
+ if (session->GetLastActivityTimeMs () + maxIdleTimeMs < currentTime)
206
108
{
207
- continue ; // not an active session
109
+ callback (*session);
110
+ ReleaseSession (session);
208
111
}
209
-
210
- uint64_t sessionActiveTime = mStates [i].GetLastActivityTimeMs ();
211
- if (sessionActiveTime + maxIdleTimeMs >= currentTime)
212
- {
213
- continue ; // not expired
214
- }
215
-
216
- MarkSessionExpired (&mStates [i], callback);
217
- }
112
+ return true ;
113
+ });
218
114
}
219
115
220
116
// / Allows access to the underlying time source used for keeping track of session active time
221
117
Time::TimeSource<kTimeSource > & GetTimeSource () { return mTimeSource ; }
222
118
223
119
private:
224
120
Time::TimeSource<kTimeSource > mTimeSource ;
225
- SecureSession mStates [ kMaxSessionCount ] ;
121
+ BitMapObjectPool< SecureSession, kMaxSessionCount > mEntries ;
226
122
};
227
123
228
124
} // namespace Transport
0 commit comments