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