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