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