Line data Source code
1 : /*
2 : *
3 : * Copyright (c) 2021 Project CHIP Authors
4 : * All rights reserved.
5 : *
6 : * Licensed under the Apache License, Version 2.0 (the "License");
7 : * you may not use this file except in compliance with the License.
8 : * You may obtain a copy of the License at
9 : *
10 : * http://www.apache.org/licenses/LICENSE-2.0
11 : *
12 : * Unless required by applicable law or agreed to in writing, software
13 : * distributed under the License is distributed on an "AS IS" BASIS,
14 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 : * See the License for the specific language governing permissions and
16 : * limitations under the License.
17 : */
18 :
19 : /**
20 : * @file
21 : * DeviceControllerSystemState is a representation of all the runtime state
22 : * inside of CHIP that can be shared across Controllers and Commissioners.
23 : *
24 : * The System State assumes it's being owned by and managed by the DeviceControllerFactory.
25 : * It will automatically shutdown the underlying CHIP Stack when its reference count
26 : * decreases to "1".
27 : *
28 : */
29 :
30 : #pragma once
31 :
32 : #include <app/CASEClientPool.h>
33 : #include <app/CASESessionManager.h>
34 : #include <app/reporting/ReportScheduler.h>
35 : #include <credentials/FabricTable.h>
36 : #include <credentials/GroupDataProvider.h>
37 : #include <crypto/SessionKeystore.h>
38 : #include <lib/core/CHIPConfig.h>
39 : #include <protocols/bdx/BdxTransferServer.h>
40 : #include <protocols/secure_channel/CASEServer.h>
41 : #include <protocols/secure_channel/MessageCounterManager.h>
42 : #include <protocols/secure_channel/SimpleSessionResumptionStorage.h>
43 : #include <protocols/secure_channel/UnsolicitedStatusHandler.h>
44 :
45 : #include <transport/TransportMgr.h>
46 : #include <transport/raw/UDP.h>
47 : #if CONFIG_DEVICE_LAYER
48 : #include <platform/CHIPDeviceLayer.h>
49 : #endif
50 :
51 : #if CONFIG_NETWORK_LAYER_BLE
52 : #include <ble/Ble.h>
53 : #include <transport/raw/BLE.h>
54 : #endif
55 : #if CHIP_DEVICE_CONFIG_ENABLE_WIFIPAF
56 : #include <transport/raw/WiFiPAF.h>
57 : #endif
58 : #if CHIP_DEVICE_CONFIG_ENABLE_NFC_BASED_COMMISSIONING
59 : #include <transport/raw/NFC.h>
60 : #endif
61 :
62 : namespace chip {
63 :
64 : inline constexpr size_t kMaxDeviceTransportBlePendingPackets = 1;
65 : #if CHIP_DEVICE_CONFIG_ENABLE_WIFIPAF
66 : inline constexpr size_t kMaxDeviceTransportWiFiPAFPendingPackets = 1;
67 : #endif
68 :
69 : #if INET_CONFIG_ENABLE_TCP_ENDPOINT
70 : inline constexpr size_t kMaxDeviceTransportTcpActiveConnectionCount = CHIP_CONFIG_MAX_ACTIVE_TCP_CONNECTIONS;
71 :
72 : inline constexpr size_t kMaxDeviceTransportTcpPendingPackets = CHIP_CONFIG_MAX_TCP_PENDING_PACKETS;
73 : #endif // INET_CONFIG_ENABLE_TCP_ENDPOINT
74 :
75 : using DeviceTransportMgr = TransportMgr<
76 : Transport::UDP /* UDP over IPv6 */
77 : #if INET_CONFIG_ENABLE_IPV4
78 : ,
79 : Transport::UDP /* UDP over IPv4 */
80 : #endif
81 : #if CONFIG_NETWORK_LAYER_BLE
82 : ,
83 : Transport::BLE<kMaxDeviceTransportBlePendingPackets> /* BLE */
84 : #endif
85 : #if INET_CONFIG_ENABLE_TCP_ENDPOINT
86 : ,
87 : Transport::TCP<kMaxDeviceTransportTcpActiveConnectionCount, kMaxDeviceTransportTcpPendingPackets> /* TCP over IPv6 */
88 : #if INET_CONFIG_ENABLE_IPV4
89 : ,
90 : Transport::TCP<kMaxDeviceTransportTcpActiveConnectionCount, kMaxDeviceTransportTcpPendingPackets> /* TCP over IPv4 */
91 : #endif
92 : #endif
93 : #if CHIP_DEVICE_CONFIG_ENABLE_WIFIPAF
94 : ,
95 : Transport::WiFiPAF<kMaxDeviceTransportWiFiPAFPendingPackets> /* WiFiPAF */
96 : #endif
97 : #if CHIP_DEVICE_CONFIG_ENABLE_NFC_BASED_COMMISSIONING
98 : ,
99 : Transport::NFC /* NFC */
100 : #endif
101 : >;
102 :
103 : namespace Controller {
104 :
105 : struct DeviceControllerSystemStateParams
106 : {
107 : using SessionSetupPool = OperationalSessionSetupPool<CHIP_CONFIG_CONTROLLER_MAX_ACTIVE_DEVICES>;
108 : using CASEClientPool = chip::CASEClientPool<CHIP_CONFIG_CONTROLLER_MAX_ACTIVE_CASE_CLIENTS>;
109 :
110 : // Params that can outlive the DeviceControllerSystemState
111 : System::Layer * systemLayer = nullptr;
112 : Inet::EndPointManager<Inet::TCPEndPoint> * tcpEndPointManager = nullptr;
113 : Inet::EndPointManager<Inet::UDPEndPoint> * udpEndPointManager = nullptr;
114 : FabricTable * fabricTable = nullptr;
115 : #if CONFIG_NETWORK_LAYER_BLE
116 : Ble::BleLayer * bleLayer = nullptr;
117 : #endif
118 : #if CHIP_DEVICE_CONFIG_ENABLE_WIFIPAF
119 : WiFiPAF::WiFiPAFLayer * wifipaf_layer = nullptr;
120 : #endif
121 : Credentials::GroupDataProvider * groupDataProvider = nullptr;
122 : Crypto::SessionKeystore * sessionKeystore = nullptr;
123 :
124 : // NOTE: Exactly one of externalSessionResumptionStorage (externally provided,
125 : // externally owned) or ownedSessionResumptionStorage (managed by the system
126 : // state) must be non-null.
127 : SessionResumptionStorage * externalSessionResumptionStorage = nullptr;
128 :
129 : // Params that will be deallocated via Platform::Delete in
130 : // DeviceControllerSystemState::Shutdown.
131 : DeviceTransportMgr * transportMgr = nullptr;
132 : // NOTE: Exactly one of externalSessionResumptionStorage (externally provided,
133 : // externally owned) or ownedSessionResumptionStorage (managed by the system
134 : // state) must be non-null.
135 : Platform::UniquePtr<SimpleSessionResumptionStorage> ownedSessionResumptionStorage;
136 : Credentials::CertificateValidityPolicy * certificateValidityPolicy = nullptr;
137 : SessionManager * sessionMgr = nullptr;
138 : Protocols::SecureChannel::UnsolicitedStatusHandler * unsolicitedStatusHandler = nullptr;
139 : Messaging::ExchangeManager * exchangeMgr = nullptr;
140 : secure_channel::MessageCounterManager * messageCounterManager = nullptr;
141 : bdx::BDXTransferServer * bdxTransferServer = nullptr;
142 : CASEServer * caseServer = nullptr;
143 : CASESessionManager * caseSessionManager = nullptr;
144 : SessionSetupPool * sessionSetupPool = nullptr;
145 : CASEClientPool * caseClientPool = nullptr;
146 : FabricTable::Delegate * fabricTableDelegate = nullptr;
147 : chip::app::reporting::ReportScheduler::TimerDelegate * timerDelegate = nullptr;
148 : chip::app::reporting::ReportScheduler * reportScheduler = nullptr;
149 : };
150 :
151 : // A representation of the internal state maintained by the DeviceControllerFactory.
152 : //
153 : // This class automatically maintains a count of active device controllers and
154 : // shuts down Matter when there are none remaining.
155 : //
156 : // NB: Lifetime of the object itself is not managed by reference counting; it is
157 : // owned by DeviceControllerFactory.
158 : class DeviceControllerSystemState
159 : {
160 : using SessionSetupPool = DeviceControllerSystemStateParams::SessionSetupPool;
161 : using CASEClientPool = DeviceControllerSystemStateParams::CASEClientPool;
162 :
163 : public:
164 0 : ~DeviceControllerSystemState()
165 : {
166 : // We could get here if a DeviceControllerFactory is shut down
167 : // without ever creating any controllers, so our refcount never goes
168 : // above 1. In that case we need to make sure we call Shutdown().
169 0 : Shutdown();
170 0 : };
171 :
172 0 : DeviceControllerSystemState(DeviceControllerSystemStateParams params) :
173 0 : mSystemLayer(params.systemLayer), mTCPEndPointManager(params.tcpEndPointManager),
174 0 : mUDPEndPointManager(params.udpEndPointManager), mTransportMgr(params.transportMgr), mSessionMgr(params.sessionMgr),
175 0 : mUnsolicitedStatusHandler(params.unsolicitedStatusHandler), mExchangeMgr(params.exchangeMgr),
176 0 : mMessageCounterManager(params.messageCounterManager), mFabrics(params.fabricTable),
177 0 : mBDXTransferServer(params.bdxTransferServer), mCASEServer(params.caseServer),
178 0 : mCASESessionManager(params.caseSessionManager), mSessionSetupPool(params.sessionSetupPool),
179 0 : mCASEClientPool(params.caseClientPool), mGroupDataProvider(params.groupDataProvider), mTimerDelegate(params.timerDelegate),
180 0 : mReportScheduler(params.reportScheduler), mSessionKeystore(params.sessionKeystore),
181 0 : mFabricTableDelegate(params.fabricTableDelegate),
182 0 : mOwnedSessionResumptionStorage(std::move(params.ownedSessionResumptionStorage))
183 : {
184 0 : if (mOwnedSessionResumptionStorage)
185 : {
186 0 : mSessionResumptionStorage = mOwnedSessionResumptionStorage.get();
187 : }
188 : else
189 : {
190 0 : mSessionResumptionStorage = params.externalSessionResumptionStorage;
191 : }
192 :
193 : #if CONFIG_NETWORK_LAYER_BLE
194 0 : mBleLayer = params.bleLayer;
195 : #endif
196 0 : VerifyOrDie(IsInitialized());
197 0 : };
198 :
199 : // Acquires a reference to the system state.
200 : //
201 : // While a reference is held, the shared state is kept alive. Release()
202 : // should be called to release the reference once it is no longer needed.
203 0 : DeviceControllerSystemState * Retain()
204 : {
205 0 : auto count = mRefCount++;
206 0 : VerifyOrDie(count < std::numeric_limits<decltype(count)>::max()); // overflow
207 0 : VerifyOrDie(!IsShutDown()); // avoid zombie
208 0 : return this;
209 : };
210 :
211 : // Releases a reference to the system state.
212 : //
213 : // The stack will shut down when all references are released.
214 : //
215 : // NB: The system state is owned by the factory; Release() will not free it
216 : // but will free its members (Shutdown()).
217 : //
218 : // Returns true if the system state was shut down in response to this call.
219 0 : bool Release()
220 : {
221 0 : auto count = mRefCount--;
222 0 : VerifyOrDie(count > 0); // underflow
223 0 : VerifyOrReturnValue(count == 1, false);
224 0 : Shutdown();
225 0 : return true;
226 : };
227 0 : bool IsInitialized()
228 : {
229 0 : return mSystemLayer != nullptr && mUDPEndPointManager != nullptr && mTransportMgr != nullptr && mSessionMgr != nullptr &&
230 0 : mUnsolicitedStatusHandler != nullptr && mExchangeMgr != nullptr && mMessageCounterManager != nullptr &&
231 0 : mFabrics != nullptr && mCASESessionManager != nullptr && mSessionSetupPool != nullptr && mCASEClientPool != nullptr &&
232 0 : mGroupDataProvider != nullptr && mReportScheduler != nullptr && mTimerDelegate != nullptr &&
233 0 : mSessionKeystore != nullptr && mSessionResumptionStorage != nullptr && mBDXTransferServer != nullptr;
234 : };
235 0 : bool IsShutDown() const { return mHaveShutDown; }
236 :
237 0 : System::Layer * SystemLayer() const { return mSystemLayer; };
238 0 : Inet::EndPointManager<Inet::TCPEndPoint> * TCPEndPointManager() const { return mTCPEndPointManager; };
239 0 : Inet::EndPointManager<Inet::UDPEndPoint> * UDPEndPointManager() const { return mUDPEndPointManager; };
240 0 : DeviceTransportMgr * TransportMgr() const { return mTransportMgr; };
241 0 : SessionManager * SessionMgr() const { return mSessionMgr; };
242 0 : Messaging::ExchangeManager * ExchangeMgr() const { return mExchangeMgr; }
243 : secure_channel::MessageCounterManager * MessageCounterManager() const { return mMessageCounterManager; };
244 0 : FabricTable * Fabrics() const { return mFabrics; };
245 : #if CONFIG_NETWORK_LAYER_BLE
246 0 : Ble::BleLayer * BleLayer() const { return mBleLayer; };
247 : #endif
248 0 : CASESessionManager * CASESessionMgr() const { return mCASESessionManager; }
249 0 : Credentials::GroupDataProvider * GetGroupDataProvider() const { return mGroupDataProvider; }
250 : chip::app::reporting::ReportScheduler * GetReportScheduler() const { return mReportScheduler; }
251 : SessionResumptionStorage * GetSessionResumptionStorage() const { return mSessionResumptionStorage; }
252 :
253 0 : Crypto::SessionKeystore * GetSessionKeystore() const { return mSessionKeystore; }
254 0 : void SetTempFabricTable(FabricTable * tempFabricTable, bool enableServerInteractions)
255 : {
256 0 : mTempFabricTable = tempFabricTable;
257 0 : mEnableServerInteractions = enableServerInteractions;
258 0 : }
259 0 : bdx::BDXTransferServer * BDXTransferServer() const { return mBDXTransferServer; }
260 :
261 : private:
262 : DeviceControllerSystemState() {}
263 :
264 : System::Layer * mSystemLayer = nullptr;
265 : Inet::EndPointManager<Inet::TCPEndPoint> * mTCPEndPointManager = nullptr;
266 : Inet::EndPointManager<Inet::UDPEndPoint> * mUDPEndPointManager = nullptr;
267 : #if CONFIG_NETWORK_LAYER_BLE
268 : Ble::BleLayer * mBleLayer = nullptr;
269 : #endif
270 : DeviceTransportMgr * mTransportMgr = nullptr;
271 : SessionManager * mSessionMgr = nullptr;
272 : Protocols::SecureChannel::UnsolicitedStatusHandler * mUnsolicitedStatusHandler = nullptr;
273 : Messaging::ExchangeManager * mExchangeMgr = nullptr;
274 : secure_channel::MessageCounterManager * mMessageCounterManager = nullptr;
275 : FabricTable * mFabrics = nullptr;
276 : bdx::BDXTransferServer * mBDXTransferServer = nullptr;
277 : CASEServer * mCASEServer = nullptr;
278 : CASESessionManager * mCASESessionManager = nullptr;
279 : SessionSetupPool * mSessionSetupPool = nullptr;
280 : CASEClientPool * mCASEClientPool = nullptr;
281 : Credentials::GroupDataProvider * mGroupDataProvider = nullptr;
282 : app::reporting::ReportScheduler::TimerDelegate * mTimerDelegate = nullptr;
283 : app::reporting::ReportScheduler * mReportScheduler = nullptr;
284 : Crypto::SessionKeystore * mSessionKeystore = nullptr;
285 : FabricTable::Delegate * mFabricTableDelegate = nullptr;
286 : SessionResumptionStorage * mSessionResumptionStorage = nullptr;
287 : Platform::UniquePtr<SimpleSessionResumptionStorage> mOwnedSessionResumptionStorage;
288 :
289 : // If mTempFabricTable is not null, it was created during
290 : // DeviceControllerFactory::InitSystemState and needs to be
291 : // freed during shutdown
292 : FabricTable * mTempFabricTable = nullptr;
293 :
294 : std::atomic<uint32_t> mRefCount{ 0 };
295 :
296 : bool mHaveShutDown = false;
297 :
298 : bool mEnableServerInteractions = false;
299 :
300 : void Shutdown();
301 : };
302 :
303 : } // namespace Controller
304 : } // namespace chip
|