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