forked from nrfconnect/sdk-connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCHIPDeviceControllerSystemState.h
265 lines (235 loc) · 12.4 KB
/
CHIPDeviceControllerSystemState.h
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
/*
*
* Copyright (c) 2021 Project CHIP Authors
* All rights reserved.
*
* 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.
*/
/**
* @file
* DeviceControllerSystemState is a representation of all the runtime state
* inside of CHIP that can be shared across Controllers and Commissioners.
*
* The System State assumes it's being owned by and managed by the DeviceControllerFactory.
* It will automatically shutdown the underlying CHIP Stack when its reference count
* decreases to "1".
*
*/
#pragma once
#include <app/CASEClientPool.h>
#include <app/CASESessionManager.h>
#include <app/reporting/ReportScheduler.h>
#include <credentials/FabricTable.h>
#include <credentials/GroupDataProvider.h>
#include <crypto/SessionKeystore.h>
#include <lib/core/CHIPConfig.h>
#include <protocols/bdx/BdxTransferServer.h>
#include <protocols/secure_channel/CASEServer.h>
#include <protocols/secure_channel/MessageCounterManager.h>
#include <protocols/secure_channel/SimpleSessionResumptionStorage.h>
#include <protocols/secure_channel/UnsolicitedStatusHandler.h>
#include <transport/TransportMgr.h>
#include <transport/raw/UDP.h>
#if CONFIG_DEVICE_LAYER
#include <platform/CHIPDeviceLayer.h>
#endif
#if CONFIG_NETWORK_LAYER_BLE
#include <ble/BleLayer.h>
#include <transport/raw/BLE.h>
#endif
namespace chip {
inline constexpr size_t kMaxDeviceTransportBlePendingPackets = 1;
using DeviceTransportMgr = TransportMgr<Transport::UDP /* IPv6 */
#if INET_CONFIG_ENABLE_IPV4
,
Transport::UDP /* IPv4 */
#endif
#if CONFIG_NETWORK_LAYER_BLE
,
Transport::BLE<kMaxDeviceTransportBlePendingPackets> /* BLE */
#endif
>;
namespace Controller {
struct DeviceControllerSystemStateParams
{
using SessionSetupPool = OperationalSessionSetupPool<CHIP_CONFIG_CONTROLLER_MAX_ACTIVE_DEVICES>;
using CASEClientPool = chip::CASEClientPool<CHIP_CONFIG_CONTROLLER_MAX_ACTIVE_CASE_CLIENTS>;
// Params that can outlive the DeviceControllerSystemState
System::Layer * systemLayer = nullptr;
Inet::EndPointManager<Inet::TCPEndPoint> * tcpEndPointManager = nullptr;
Inet::EndPointManager<Inet::UDPEndPoint> * udpEndPointManager = nullptr;
FabricTable * fabricTable = nullptr;
#if CONFIG_NETWORK_LAYER_BLE
Ble::BleLayer * bleLayer = nullptr;
#endif
Credentials::GroupDataProvider * groupDataProvider = nullptr;
Crypto::SessionKeystore * sessionKeystore = nullptr;
// NOTE: Exactly one of externalSessionResumptionStorage (externally provided,
// externally owned) or ownedSessionResumptionStorage (managed by the system
// state) must be non-null.
SessionResumptionStorage * externalSessionResumptionStorage = nullptr;
// Params that will be deallocated via Platform::Delete in
// DeviceControllerSystemState::Shutdown.
DeviceTransportMgr * transportMgr = nullptr;
// NOTE: Exactly one of externalSessionResumptionStorage (externally provided,
// externally owned) or ownedSessionResumptionStorage (managed by the system
// state) must be non-null.
Platform::UniquePtr<SimpleSessionResumptionStorage> ownedSessionResumptionStorage;
Credentials::CertificateValidityPolicy * certificateValidityPolicy = nullptr;
SessionManager * sessionMgr = nullptr;
Protocols::SecureChannel::UnsolicitedStatusHandler * unsolicitedStatusHandler = nullptr;
Messaging::ExchangeManager * exchangeMgr = nullptr;
secure_channel::MessageCounterManager * messageCounterManager = nullptr;
bdx::BDXTransferServer * bdxTransferServer = nullptr;
CASEServer * caseServer = nullptr;
CASESessionManager * caseSessionManager = nullptr;
SessionSetupPool * sessionSetupPool = nullptr;
CASEClientPool * caseClientPool = nullptr;
FabricTable::Delegate * fabricTableDelegate = nullptr;
chip::app::reporting::ReportScheduler::TimerDelegate * timerDelegate = nullptr;
chip::app::reporting::ReportScheduler * reportScheduler = nullptr;
};
// A representation of the internal state maintained by the DeviceControllerFactory.
//
// This class automatically maintains a count of active device controllers and
// shuts down Matter when there are none remaining.
//
// NB: Lifetime of the object itself is not managed by reference counting; it is
// owned by DeviceControllerFactory.
class DeviceControllerSystemState
{
using SessionSetupPool = DeviceControllerSystemStateParams::SessionSetupPool;
using CASEClientPool = DeviceControllerSystemStateParams::CASEClientPool;
public:
~DeviceControllerSystemState()
{
// We could get here if a DeviceControllerFactory is shut down
// without ever creating any controllers, so our refcount never goes
// above 1. In that case we need to make sure we call Shutdown().
Shutdown();
};
DeviceControllerSystemState(DeviceControllerSystemStateParams params) :
mSystemLayer(params.systemLayer), mTCPEndPointManager(params.tcpEndPointManager),
mUDPEndPointManager(params.udpEndPointManager), mTransportMgr(params.transportMgr), mSessionMgr(params.sessionMgr),
mUnsolicitedStatusHandler(params.unsolicitedStatusHandler), mExchangeMgr(params.exchangeMgr),
mMessageCounterManager(params.messageCounterManager), mFabrics(params.fabricTable),
mBDXTransferServer(params.bdxTransferServer), mCASEServer(params.caseServer),
mCASESessionManager(params.caseSessionManager), mSessionSetupPool(params.sessionSetupPool),
mCASEClientPool(params.caseClientPool), mGroupDataProvider(params.groupDataProvider), mTimerDelegate(params.timerDelegate),
mReportScheduler(params.reportScheduler), mSessionKeystore(params.sessionKeystore),
mFabricTableDelegate(params.fabricTableDelegate),
mOwnedSessionResumptionStorage(std::move(params.ownedSessionResumptionStorage))
{
if (mOwnedSessionResumptionStorage)
{
mSessionResumptionStorage = mOwnedSessionResumptionStorage.get();
}
else
{
mSessionResumptionStorage = params.externalSessionResumptionStorage;
}
#if CONFIG_NETWORK_LAYER_BLE
mBleLayer = params.bleLayer;
#endif
VerifyOrDie(IsInitialized());
};
// Acquires a reference to the system state.
//
// While a reference is held, the shared state is kept alive. Release()
// should be called to release the reference once it is no longer needed.
DeviceControllerSystemState * Retain()
{
VerifyOrDie(mRefCount < std::numeric_limits<uint32_t>::max());
++mRefCount;
return this;
};
// Releases a reference to the system state.
//
// The stack will shut down when all references are released.
//
// NB: The system state is owned by the factory; Relase() will not free it
// but will free its members (Shutdown()).
void Release()
{
VerifyOrDie(mRefCount > 0);
if (--mRefCount == 0)
{
Shutdown();
}
};
bool IsInitialized()
{
return mSystemLayer != nullptr && mUDPEndPointManager != nullptr && mTransportMgr != nullptr && mSessionMgr != nullptr &&
mUnsolicitedStatusHandler != nullptr && mExchangeMgr != nullptr && mMessageCounterManager != nullptr &&
mFabrics != nullptr && mCASESessionManager != nullptr && mSessionSetupPool != nullptr && mCASEClientPool != nullptr &&
mGroupDataProvider != nullptr && mReportScheduler != nullptr && mTimerDelegate != nullptr &&
mSessionKeystore != nullptr && mSessionResumptionStorage != nullptr && mBDXTransferServer != nullptr;
};
System::Layer * SystemLayer() const { return mSystemLayer; };
Inet::EndPointManager<Inet::TCPEndPoint> * TCPEndPointManager() const { return mTCPEndPointManager; };
Inet::EndPointManager<Inet::UDPEndPoint> * UDPEndPointManager() const { return mUDPEndPointManager; };
DeviceTransportMgr * TransportMgr() const { return mTransportMgr; };
SessionManager * SessionMgr() const { return mSessionMgr; };
Messaging::ExchangeManager * ExchangeMgr() const { return mExchangeMgr; }
secure_channel::MessageCounterManager * MessageCounterManager() const { return mMessageCounterManager; };
FabricTable * Fabrics() const { return mFabrics; };
#if CONFIG_NETWORK_LAYER_BLE
Ble::BleLayer * BleLayer() const { return mBleLayer; };
#endif
CASESessionManager * CASESessionMgr() const { return mCASESessionManager; }
Credentials::GroupDataProvider * GetGroupDataProvider() const { return mGroupDataProvider; }
chip::app::reporting::ReportScheduler * GetReportScheduler() const { return mReportScheduler; }
Crypto::SessionKeystore * GetSessionKeystore() const { return mSessionKeystore; }
void SetTempFabricTable(FabricTable * tempFabricTable, bool enableServerInteractions)
{
mTempFabricTable = tempFabricTable;
mEnableServerInteractions = enableServerInteractions;
}
bdx::BDXTransferServer * BDXTransferServer() const { return mBDXTransferServer; }
private:
DeviceControllerSystemState() {}
System::Layer * mSystemLayer = nullptr;
Inet::EndPointManager<Inet::TCPEndPoint> * mTCPEndPointManager = nullptr;
Inet::EndPointManager<Inet::UDPEndPoint> * mUDPEndPointManager = nullptr;
#if CONFIG_NETWORK_LAYER_BLE
Ble::BleLayer * mBleLayer = nullptr;
#endif
DeviceTransportMgr * mTransportMgr = nullptr;
SessionManager * mSessionMgr = nullptr;
Protocols::SecureChannel::UnsolicitedStatusHandler * mUnsolicitedStatusHandler = nullptr;
Messaging::ExchangeManager * mExchangeMgr = nullptr;
secure_channel::MessageCounterManager * mMessageCounterManager = nullptr;
FabricTable * mFabrics = nullptr;
bdx::BDXTransferServer * mBDXTransferServer = nullptr;
CASEServer * mCASEServer = nullptr;
CASESessionManager * mCASESessionManager = nullptr;
SessionSetupPool * mSessionSetupPool = nullptr;
CASEClientPool * mCASEClientPool = nullptr;
Credentials::GroupDataProvider * mGroupDataProvider = nullptr;
app::reporting::ReportScheduler::TimerDelegate * mTimerDelegate = nullptr;
app::reporting::ReportScheduler * mReportScheduler = nullptr;
Crypto::SessionKeystore * mSessionKeystore = nullptr;
FabricTable::Delegate * mFabricTableDelegate = nullptr;
SessionResumptionStorage * mSessionResumptionStorage = nullptr;
Platform::UniquePtr<SimpleSessionResumptionStorage> mOwnedSessionResumptionStorage;
// If mTempFabricTable is not null, it was created during
// DeviceControllerFactory::InitSystemState and needs to be
// freed during shutdown
FabricTable * mTempFabricTable = nullptr;
std::atomic<uint32_t> mRefCount{ 0 };
bool mHaveShutDown = false;
bool mEnableServerInteractions = false;
void Shutdown();
};
} // namespace Controller
} // namespace chip