Matter SDK Coverage Report
Current view: top level - controller - CHIPDeviceControllerFactory.cpp (source / functions) Coverage Total Hit
Test: SHA:209dc18e4021e7d0dff8120ccc585909391dd862 Lines: 0.0 % 282 0
Test Date: 2026-06-16 07:34:53 Functions: 0.0 % 14 0

            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              :  *      Implementation of CHIP Device Controller Factory, a utility/manager class
      22              :  *      that vends Controller objects
      23              :  */
      24              : 
      25              : #include <controller/CHIPDeviceControllerFactory.h>
      26              : 
      27              : #include <app/InteractionModelEngine.h>
      28              : #include <app/OperationalSessionSetup.h>
      29              : #include <app/reporting/ReportSchedulerImpl.h>
      30              : #include <app/util/DataModelHandler.h>
      31              : #include <lib/core/ErrorStr.h>
      32              : #include <messaging/ReliableMessageProtocolConfig.h>
      33              : 
      34              : #if CONFIG_DEVICE_LAYER
      35              : #include <platform/CHIPDeviceLayer.h>
      36              : #include <platform/ConfigurationManager.h>
      37              : #include <platform/DefaultTimerDelegate.h>
      38              : #endif
      39              : 
      40              : #include <app/server/Dnssd.h>
      41              : #include <protocols/secure_channel/CASEServer.h>
      42              : #include <protocols/secure_channel/SimpleSessionResumptionStorage.h>
      43              : 
      44              : using namespace chip::Inet;
      45              : using namespace chip::System;
      46              : using namespace chip::Credentials;
      47              : 
      48              : namespace chip {
      49              : namespace Controller {
      50              : 
      51            0 : CHIP_ERROR DeviceControllerFactory::Init(FactoryInitParams params)
      52              : {
      53              : 
      54              :     // SystemState is only set the first time init is called, after that it is managed
      55              :     // internally. If SystemState is set then init has already completed.
      56            0 :     if (mSystemState != nullptr)
      57              :     {
      58            0 :         ChipLogError(Controller, "Device Controller Factory already initialized...");
      59            0 :         return CHIP_NO_ERROR;
      60              :     }
      61              : 
      62              :     // Save our initialization state that we can't recover later from a
      63              :     // created-but-shut-down system state.
      64            0 :     mListenPort                = params.listenPort;
      65            0 :     mInterfaceId               = params.interfaceId;
      66            0 :     mFabricIndependentStorage  = params.fabricIndependentStorage;
      67            0 :     mOperationalKeystore       = params.operationalKeystore;
      68            0 :     mOpCertStore               = params.opCertStore;
      69            0 :     mCertificateValidityPolicy = params.certificateValidityPolicy;
      70            0 :     mSessionResumptionStorage  = params.sessionResumptionStorage;
      71            0 :     mEnableServerInteractions  = params.enableServerInteractions;
      72            0 :     mPreventDnssdPortOverwrite = params.preventDnssdPortOverwrite;
      73            0 :     mDataModelProvider         = params.dataModelProvider;
      74              : 
      75              :     // Initialize the system state. Note that it is left in a somewhat
      76              :     // special state where it is initialized, but has a ref count of 0.
      77            0 :     CHIP_ERROR err = InitSystemState(params);
      78              : 
      79            0 :     return err;
      80              : }
      81              : 
      82            0 : CHIP_ERROR DeviceControllerFactory::ReinitSystemStateIfNecessary()
      83              : {
      84            0 :     VerifyOrReturnError(mSystemState != nullptr, CHIP_ERROR_INCORRECT_STATE);
      85            0 :     VerifyOrReturnError(mSystemState->IsShutDown(), CHIP_NO_ERROR);
      86              : 
      87            0 :     FactoryInitParams params;
      88            0 :     params.systemLayer        = mSystemState->SystemLayer();
      89            0 :     params.udpEndPointManager = mSystemState->UDPEndPointManager();
      90              : #if INET_CONFIG_ENABLE_TCP_ENDPOINT
      91            0 :     params.tcpEndPointManager = mSystemState->TCPEndPointManager();
      92              : #endif
      93              : #if CONFIG_NETWORK_LAYER_BLE
      94            0 :     params.bleLayer = mSystemState->BleLayer();
      95              : #endif
      96            0 :     params.listenPort                = mListenPort;
      97            0 :     params.interfaceId               = mInterfaceId;
      98            0 :     params.fabricIndependentStorage  = mFabricIndependentStorage;
      99            0 :     params.enableServerInteractions  = mEnableServerInteractions;
     100            0 :     params.preventDnssdPortOverwrite = mPreventDnssdPortOverwrite;
     101            0 :     params.groupDataProvider         = mSystemState->GetGroupDataProvider();
     102            0 :     params.sessionKeystore           = mSystemState->GetSessionKeystore();
     103            0 :     params.fabricTable               = mSystemState->Fabrics();
     104            0 :     params.operationalKeystore       = mOperationalKeystore;
     105            0 :     params.opCertStore               = mOpCertStore;
     106            0 :     params.certificateValidityPolicy = mCertificateValidityPolicy;
     107            0 :     params.sessionResumptionStorage  = mSessionResumptionStorage;
     108            0 :     params.dataModelProvider         = mDataModelProvider;
     109              : 
     110            0 :     return InitSystemState(params);
     111              : }
     112              : 
     113            0 : CHIP_ERROR DeviceControllerFactory::InitSystemState(FactoryInitParams params)
     114              : {
     115            0 :     if (mSystemState != nullptr)
     116              :     {
     117            0 :         Platform::Delete(mSystemState);
     118            0 :         mSystemState = nullptr;
     119              :     }
     120              : 
     121            0 :     DeviceControllerSystemStateParams stateParams;
     122              : #if CONFIG_DEVICE_LAYER
     123            0 :     ReturnErrorOnFailure(DeviceLayer::PlatformMgr().InitChipStack());
     124              : 
     125            0 :     stateParams.systemLayer        = &DeviceLayer::SystemLayer();
     126            0 :     stateParams.udpEndPointManager = DeviceLayer::UDPEndPointManager();
     127              : #if INET_CONFIG_ENABLE_TCP_ENDPOINT
     128            0 :     stateParams.tcpEndPointManager = DeviceLayer::TCPEndPointManager();
     129              : #endif
     130              : #else
     131              :     stateParams.systemLayer        = params.systemLayer;
     132              :     stateParams.tcpEndPointManager = params.tcpEndPointManager;
     133              :     stateParams.udpEndPointManager = params.udpEndPointManager;
     134              :     ChipLogError(Controller, "Warning: Device Controller Factory should be with a CHIP Device Layer...");
     135              : #endif // CONFIG_DEVICE_LAYER
     136              : 
     137              : #if INET_CONFIG_ENABLE_TCP_ENDPOINT
     138            0 :     auto tcpListenParams = Transport::TcpListenParameters(stateParams.tcpEndPointManager)
     139            0 :                                .SetAddressType(IPAddressType::kIPv6)
     140            0 :                                .SetListenPort(params.listenPort)
     141            0 :                                .SetServerListenEnabled(false); // Initialize as a TCP Client
     142              : #endif
     143              : 
     144            0 :     if (params.dataModelProvider == nullptr)
     145              :     {
     146            0 :         ChipLogError(AppServer, "Device Controller Factory requires a `dataModelProvider` value.");
     147            0 :         ChipLogError(AppServer, "For backwards compatibility, you likely can use `CodegenDataModelProviderInstance(...)`");
     148              :     }
     149              : 
     150            0 :     VerifyOrReturnError(params.dataModelProvider != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
     151            0 :     VerifyOrReturnError(stateParams.systemLayer != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
     152            0 :     VerifyOrReturnError(stateParams.udpEndPointManager != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
     153              : 
     154              :     // OperationalCertificateStore needs to be provided to init the fabric table if fabric table is
     155              :     // not provided wholesale.
     156            0 :     VerifyOrReturnError((params.fabricTable != nullptr) || (params.opCertStore != nullptr), CHIP_ERROR_INVALID_ARGUMENT);
     157            0 :     VerifyOrReturnError(params.sessionKeystore != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
     158              : 
     159              : #if CONFIG_NETWORK_LAYER_BLE
     160              : #if CONFIG_DEVICE_LAYER
     161            0 :     stateParams.bleLayer = DeviceLayer::ConnectivityMgr().GetBleLayer();
     162              : #else
     163              :     stateParams.bleLayer = params.bleLayer;
     164              : #endif // CONFIG_DEVICE_LAYER
     165              : #if CHIP_DEVICE_CONFIG_ENABLE_WIFIPAF
     166            0 :     stateParams.wifipaf_layer = params.wifipaf_layer;
     167              : #endif
     168            0 :     VerifyOrReturnError(stateParams.bleLayer != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
     169              : #endif
     170              : 
     171            0 :     stateParams.transportMgr = chip::Platform::New<DeviceTransportMgr>();
     172              : 
     173              :     //
     174              :     // The logic below expects IPv6 to be at index 0 of this tuple. Keep that logic in sync with
     175              :     // this code.
     176              :     //
     177            0 :     ReturnErrorOnFailure(stateParams.transportMgr->Init(Transport::UdpListenParameters(stateParams.udpEndPointManager)
     178              :                                                             .SetAddressType(Inet::IPAddressType::kIPv6)
     179              :                                                             .SetListenPort(params.listenPort)
     180              : #if INET_CONFIG_ENABLE_IPV4
     181              :                                                             ,
     182              :                                                         //
     183              :                                                         // The logic below expects IPv4 to be at index 1 of this tuple,
     184              :                                                         // if it's enabled. Keep that logic in sync with this code.
     185              :                                                         //
     186              :                                                         Transport::UdpListenParameters(stateParams.udpEndPointManager)
     187              :                                                             .SetAddressType(Inet::IPAddressType::kIPv4)
     188              :                                                             .SetListenPort(params.listenPort)
     189              : #endif
     190              : #if CONFIG_NETWORK_LAYER_BLE
     191              :                                                             ,
     192              :                                                         Transport::BleListenParameters(stateParams.bleLayer)
     193              : #endif
     194              : #if INET_CONFIG_ENABLE_TCP_ENDPOINT
     195              :                                                             ,
     196              :                                                         tcpListenParams
     197              : #if INET_CONFIG_ENABLE_IPV4
     198              :                                                         ,
     199              :                                                         Transport::TcpListenParameters(stateParams.tcpEndPointManager)
     200              :                                                             .SetAddressType(Inet::IPAddressType::kIPv4)
     201              :                                                             .SetListenPort(params.listenPort)
     202              :                                                             .SetServerListenEnabled(false)
     203              : #endif
     204              : #endif
     205              : #if CHIP_DEVICE_CONFIG_ENABLE_WIFIPAF
     206              :                                                             ,
     207              :                                                         Transport::WiFiPAFListenParameters()
     208              : #endif
     209              : #if CHIP_DEVICE_CONFIG_ENABLE_NFC_BASED_COMMISSIONING
     210              :                                                             ,
     211              :                                                         Transport::NfcListenParameters(nullptr)
     212              : #endif
     213              :                                                             ));
     214              : 
     215              :     // TODO(#16231): All the new'ed state above/below in this method is never properly released or null-checked!
     216            0 :     stateParams.sessionMgr                = chip::Platform::New<SessionManager>();
     217            0 :     stateParams.certificateValidityPolicy = params.certificateValidityPolicy;
     218            0 :     stateParams.unsolicitedStatusHandler  = Platform::New<Protocols::SecureChannel::UnsolicitedStatusHandler>();
     219            0 :     stateParams.exchangeMgr               = chip::Platform::New<Messaging::ExchangeManager>();
     220            0 :     stateParams.messageCounterManager     = chip::Platform::New<secure_channel::MessageCounterManager>();
     221            0 :     stateParams.groupDataProvider         = params.groupDataProvider;
     222            0 :     stateParams.timerDelegate             = chip::Platform::New<chip::app::DefaultTimerDelegate>();
     223            0 :     stateParams.reportScheduler           = chip::Platform::New<app::reporting::ReportSchedulerImpl>(stateParams.timerDelegate);
     224            0 :     stateParams.sessionKeystore           = params.sessionKeystore;
     225            0 :     stateParams.bdxTransferServer         = chip::Platform::New<bdx::BDXTransferServer>();
     226              : 
     227              :     // if no fabricTable was provided, create one and track it in stateParams for cleanup
     228            0 :     stateParams.fabricTable = params.fabricTable;
     229              : 
     230            0 :     FabricTable * tempFabricTable = nullptr;
     231            0 :     if (stateParams.fabricTable == nullptr)
     232              :     {
     233              :         // TODO(#16231): Previously (and still) the objects new-ed in this entire method seem expected to last forever...
     234            0 :         auto newFabricTable = Platform::MakeUnique<FabricTable>();
     235            0 :         VerifyOrReturnError(newFabricTable, CHIP_ERROR_NO_MEMORY);
     236              : 
     237            0 :         FabricTable::InitParams fabricTableInitParams;
     238            0 :         fabricTableInitParams.storage             = params.fabricIndependentStorage;
     239            0 :         fabricTableInitParams.operationalKeystore = params.operationalKeystore;
     240            0 :         fabricTableInitParams.opCertStore         = params.opCertStore;
     241            0 :         ReturnErrorOnFailure(newFabricTable->Init(fabricTableInitParams));
     242            0 :         stateParams.fabricTable = newFabricTable.release();
     243            0 :         tempFabricTable         = stateParams.fabricTable;
     244            0 :     }
     245              : 
     246              :     SessionResumptionStorage * sessionResumptionStorage;
     247            0 :     if (params.sessionResumptionStorage == nullptr)
     248              :     {
     249            0 :         auto ownedSessionResumptionStorage = chip::Platform::MakeUnique<SimpleSessionResumptionStorage>();
     250            0 :         ReturnErrorOnFailure(ownedSessionResumptionStorage->Init(params.fabricIndependentStorage));
     251            0 :         stateParams.ownedSessionResumptionStorage    = std::move(ownedSessionResumptionStorage);
     252            0 :         stateParams.externalSessionResumptionStorage = nullptr;
     253            0 :         sessionResumptionStorage                     = stateParams.ownedSessionResumptionStorage.get();
     254            0 :     }
     255              :     else
     256              :     {
     257            0 :         stateParams.ownedSessionResumptionStorage    = nullptr;
     258            0 :         stateParams.externalSessionResumptionStorage = params.sessionResumptionStorage;
     259            0 :         sessionResumptionStorage                     = stateParams.externalSessionResumptionStorage;
     260              :     }
     261              : 
     262            0 :     auto delegate = chip::Platform::MakeUnique<ControllerFabricDelegate>();
     263            0 :     ReturnErrorOnFailure(delegate->Init(sessionResumptionStorage, stateParams.groupDataProvider));
     264            0 :     stateParams.fabricTableDelegate = delegate.get();
     265            0 :     ReturnErrorOnFailure(stateParams.fabricTable->AddFabricDelegate(stateParams.fabricTableDelegate));
     266            0 :     delegate.release();
     267              : 
     268            0 :     ReturnErrorOnFailure(stateParams.sessionMgr->Init(stateParams.systemLayer, stateParams.transportMgr,
     269              :                                                       stateParams.messageCounterManager, params.fabricIndependentStorage,
     270              :                                                       stateParams.fabricTable, *stateParams.sessionKeystore));
     271            0 :     ReturnErrorOnFailure(stateParams.exchangeMgr->Init(stateParams.sessionMgr));
     272            0 :     ReturnErrorOnFailure(stateParams.messageCounterManager->Init(stateParams.exchangeMgr));
     273            0 :     ReturnErrorOnFailure(stateParams.unsolicitedStatusHandler->Init(stateParams.exchangeMgr));
     274            0 :     ReturnErrorOnFailure(stateParams.bdxTransferServer->Init(stateParams.systemLayer, stateParams.exchangeMgr));
     275              : 
     276            0 :     chip::app::InteractionModelEngine * interactionModelEngine = chip::app::InteractionModelEngine::GetInstance();
     277              : 
     278              :     // Initialize the data model now that everything cluster implementations might
     279              :     // depend on is initalized.
     280            0 :     interactionModelEngine->SetDataModelProvider(params.dataModelProvider);
     281              : 
     282            0 :     ReturnErrorOnFailure(Dnssd::Resolver::Instance().Init(stateParams.udpEndPointManager));
     283              : 
     284            0 :     if (params.enableServerInteractions)
     285              :     {
     286            0 :         stateParams.caseServer = chip::Platform::New<CASEServer>();
     287              : 
     288              :         // Enable listening for session establishment messages.
     289            0 :         ReturnErrorOnFailure(stateParams.caseServer->ListenForSessionEstablishment(
     290              :             stateParams.exchangeMgr, stateParams.sessionMgr, stateParams.fabricTable, sessionResumptionStorage,
     291              :             stateParams.certificateValidityPolicy, stateParams.groupDataProvider));
     292              : 
     293            0 :         if (!params.preventDnssdPortOverwrite)
     294              :         {
     295              :             // Our IPv6 transport is at index 0.
     296            0 :             app::DnssdServer::Instance().SetSecuredIPv6Port(
     297            0 :                 stateParams.transportMgr->GetTransport().GetImplAtIndex<0>().GetBoundPort());
     298              : 
     299              : #if INET_CONFIG_ENABLE_IPV4
     300              :             // If enabled, our IPv4 transport is at index 1.
     301            0 :             app::DnssdServer::Instance().SetSecuredIPv4Port(
     302            0 :                 stateParams.transportMgr->GetTransport().GetImplAtIndex<1>().GetBoundPort());
     303              : #endif // INET_CONFIG_ENABLE_IPV4
     304              :         }
     305              : 
     306            0 :         if (params.interfaceId)
     307              :         {
     308            0 :             app::DnssdServer::Instance().SetInterfaceId(*params.interfaceId);
     309              :         }
     310              : 
     311              :         //
     312              :         // TODO: This is a hack to workaround the fact that we have a bi-polar stack that has controller and server modalities that
     313              :         // are mutually exclusive in terms of initialization of key stack singletons. Consequently, DnssdServer accesses
     314              :         // Server::GetInstance().GetFabricTable() to access the fabric table, but we don't want to do that when we're initializing
     315              :         // the controller logic since the factory here has its own fabric table.
     316              :         //
     317              :         // Consequently, reach in set the fabric table pointer to point to the right version.
     318              :         //
     319            0 :         app::DnssdServer::Instance().SetFabricTable(stateParams.fabricTable);
     320              : 
     321              : #if INET_CONFIG_ENABLE_TCP_ENDPOINT
     322              :         // Disable the TCP Server based on the TCPListenParameters setting.
     323            0 :         app::DnssdServer::Instance().SetTCPServerEnabled(tcpListenParams.IsServerListenEnabled());
     324              : #endif
     325              :     }
     326              : 
     327            0 :     stateParams.sessionSetupPool = Platform::New<DeviceControllerSystemStateParams::SessionSetupPool>();
     328            0 :     stateParams.caseClientPool   = Platform::New<DeviceControllerSystemStateParams::CASEClientPool>();
     329              : 
     330              :     CASEClientInitParams sessionInitParams = {
     331            0 :         .sessionManager            = stateParams.sessionMgr,
     332              :         .sessionResumptionStorage  = sessionResumptionStorage,
     333            0 :         .certificateValidityPolicy = stateParams.certificateValidityPolicy,
     334            0 :         .exchangeMgr               = stateParams.exchangeMgr,
     335            0 :         .fabricTable               = stateParams.fabricTable,
     336            0 :         .groupDataProvider         = stateParams.groupDataProvider,
     337              :         // Don't provide an MRP local config, so each CASE initiation will use
     338              :         // the then-current value.
     339              :         .mrpLocalConfig            = NullOptional,
     340            0 :         .minimumLITBackoffInterval = params.minimumLITBackoffInterval,
     341            0 :     };
     342              : 
     343              :     CASESessionManagerConfig sessionManagerConfig = {
     344              :         .sessionInitParams = sessionInitParams,
     345            0 :         .clientPool        = stateParams.caseClientPool,
     346            0 :         .sessionSetupPool  = stateParams.sessionSetupPool,
     347            0 :     };
     348              : 
     349              :     // TODO: Need to be able to create a CASESessionManagerConfig here!
     350            0 :     stateParams.caseSessionManager = Platform::New<CASESessionManager>();
     351            0 :     ReturnErrorOnFailure(stateParams.caseSessionManager->Init(stateParams.systemLayer, sessionManagerConfig));
     352              : 
     353            0 :     ReturnErrorOnFailure(interactionModelEngine->Init(stateParams.exchangeMgr, stateParams.fabricTable, stateParams.reportScheduler,
     354              :                                                       stateParams.caseSessionManager));
     355              : 
     356              :     // store the system state
     357            0 :     mSystemState = chip::Platform::New<DeviceControllerSystemState>(std::move(stateParams));
     358            0 :     mSystemState->SetTempFabricTable(tempFabricTable, params.enableServerInteractions);
     359            0 :     ChipLogDetail(Controller, "System State Initialized...");
     360            0 :     return CHIP_NO_ERROR;
     361            0 : }
     362              : 
     363            0 : void DeviceControllerFactory::PopulateInitParams(ControllerInitParams & controllerParams, const SetupParams & params)
     364              : {
     365            0 :     controllerParams.operationalCredentialsDelegate       = params.operationalCredentialsDelegate;
     366            0 :     controllerParams.operationalKeypair                   = params.operationalKeypair;
     367            0 :     controllerParams.hasExternallyOwnedOperationalKeypair = params.hasExternallyOwnedOperationalKeypair;
     368            0 :     controllerParams.controllerNOC                        = params.controllerNOC;
     369            0 :     controllerParams.controllerICAC                       = params.controllerICAC;
     370            0 :     controllerParams.controllerRCAC                       = params.controllerRCAC;
     371            0 :     controllerParams.permitMultiControllerFabrics         = params.permitMultiControllerFabrics;
     372            0 :     controllerParams.removeFromFabricTableOnShutdown      = params.removeFromFabricTableOnShutdown;
     373            0 :     controllerParams.deleteFromFabricTableOnShutdown      = params.deleteFromFabricTableOnShutdown;
     374              : 
     375            0 :     controllerParams.systemState        = mSystemState;
     376            0 :     controllerParams.controllerVendorId = params.controllerVendorId;
     377              : 
     378            0 :     controllerParams.enableServerInteractions = params.enableServerInteractions;
     379            0 :     if (params.fabricIndex.HasValue())
     380              :     {
     381            0 :         controllerParams.fabricIndex.SetValue(params.fabricIndex.Value());
     382              :     }
     383            0 : }
     384              : 
     385            0 : void DeviceControllerFactory::ControllerInitialized(const DeviceController & controller)
     386              : {
     387            0 :     if (mEnableServerInteractions && controller.GetFabricIndex() != kUndefinedFabricIndex)
     388              :     {
     389              :         // Restart DNS-SD advertising, because initialization of this controller could
     390              :         // have modified whether a particular fabric identity should be
     391              :         // advertised.  Just calling AdvertiseOperational() is not good enough
     392              :         // here, since we might be removing advertising.
     393            0 :         app::DnssdServer::Instance().StartServer();
     394              :     }
     395            0 : }
     396              : 
     397            0 : CHIP_ERROR DeviceControllerFactory::SetupController(SetupParams params, DeviceController & controller)
     398              : {
     399            0 :     VerifyOrReturnError(params.controllerVendorId != VendorId::Unspecified, CHIP_ERROR_INVALID_ARGUMENT);
     400            0 :     ReturnErrorOnFailure(ReinitSystemStateIfNecessary());
     401              : 
     402            0 :     ControllerInitParams controllerParams;
     403            0 :     PopulateInitParams(controllerParams, params);
     404              : 
     405            0 :     CHIP_ERROR err = controller.Init(controllerParams);
     406              : 
     407            0 :     if (err == CHIP_NO_ERROR)
     408              :     {
     409            0 :         ControllerInitialized(controller);
     410              :     }
     411              : 
     412            0 :     return err;
     413              : }
     414              : 
     415            0 : CHIP_ERROR DeviceControllerFactory::SetupCommissioner(SetupParams params, DeviceCommissioner & commissioner)
     416              : {
     417            0 :     VerifyOrReturnError(params.controllerVendorId != VendorId::Unspecified, CHIP_ERROR_INVALID_ARGUMENT);
     418            0 :     ReturnErrorOnFailure(ReinitSystemStateIfNecessary());
     419              : 
     420            0 :     CommissionerInitParams commissionerParams;
     421              : 
     422              :     // PopulateInitParams works against ControllerInitParams base class of CommissionerInitParams only
     423            0 :     PopulateInitParams(commissionerParams, params);
     424              : 
     425              :     // Set commissioner-specific fields not in ControllerInitParams
     426            0 :     commissionerParams.pairingDelegate           = params.pairingDelegate;
     427            0 :     commissionerParams.defaultCommissioner       = params.defaultCommissioner;
     428            0 :     commissionerParams.deviceAttestationVerifier = params.deviceAttestationVerifier;
     429              : 
     430            0 :     CHIP_ERROR err = commissioner.Init(commissionerParams);
     431              : 
     432            0 :     if (err == CHIP_NO_ERROR)
     433              :     {
     434            0 :         ControllerInitialized(commissioner);
     435              :     }
     436              : 
     437            0 :     return err;
     438              : }
     439              : 
     440            0 : CHIP_ERROR DeviceControllerFactory::ServiceEvents()
     441              : {
     442            0 :     VerifyOrReturnError(mSystemState != nullptr, CHIP_ERROR_INCORRECT_STATE);
     443              : 
     444              : #if CONFIG_DEVICE_LAYER
     445            0 :     ReturnErrorOnFailure(DeviceLayer::PlatformMgr().StartEventLoopTask());
     446              : #endif // CONFIG_DEVICE_LAYER
     447              : 
     448            0 :     return CHIP_NO_ERROR;
     449              : }
     450              : 
     451            0 : void DeviceControllerFactory::RetainSystemState()
     452              : {
     453            0 :     (void) mSystemState->Retain();
     454            0 : }
     455              : 
     456            0 : bool DeviceControllerFactory::ReleaseSystemState()
     457              : {
     458            0 :     return mSystemState->Release();
     459              : }
     460              : 
     461            0 : CHIP_ERROR DeviceControllerFactory::EnsureAndRetainSystemState()
     462              : {
     463            0 :     ReturnErrorOnFailure(ReinitSystemStateIfNecessary());
     464            0 :     RetainSystemState();
     465            0 :     return CHIP_NO_ERROR;
     466              : }
     467              : 
     468            0 : DeviceControllerFactory::~DeviceControllerFactory()
     469              : {
     470            0 :     Shutdown();
     471            0 : }
     472              : 
     473            0 : void DeviceControllerFactory::Shutdown()
     474              : {
     475            0 :     if (mSystemState != nullptr)
     476              :     {
     477              :         // ~DeviceControllerSystemState will call Shutdown(),
     478              :         // which in turn ensures that the reference count is 0.
     479            0 :         Platform::Delete(mSystemState);
     480            0 :         mSystemState = nullptr;
     481              :     }
     482            0 :     mFabricIndependentStorage  = nullptr;
     483            0 :     mOperationalKeystore       = nullptr;
     484            0 :     mOpCertStore               = nullptr;
     485            0 :     mCertificateValidityPolicy = nullptr;
     486            0 :     mSessionResumptionStorage  = nullptr;
     487            0 :     mDataModelProvider         = nullptr;
     488            0 : }
     489              : 
     490            0 : void DeviceControllerSystemState::Shutdown()
     491              : {
     492            0 :     VerifyOrDie(mRefCount == 0);
     493            0 :     if (mHaveShutDown)
     494              :     {
     495              :         // Nothing else to do here.
     496            0 :         return;
     497              :     }
     498            0 :     mHaveShutDown = true;
     499              : 
     500            0 :     ChipLogDetail(Controller, "Shutting down the System State, this will teardown the CHIP Stack");
     501              : 
     502            0 :     if (mTempFabricTable && mEnableServerInteractions)
     503              :     {
     504              :         // The DnssdServer is holding a reference to our temp fabric table,
     505              :         // which we are about to destroy.  Stop it, so that it will stop trying
     506              :         // to use it.
     507            0 :         app::DnssdServer::Instance().StopServer();
     508              :     }
     509              : 
     510            0 :     if (mFabricTableDelegate != nullptr)
     511              :     {
     512            0 :         if (mFabrics != nullptr)
     513              :         {
     514            0 :             mFabrics->RemoveFabricDelegate(mFabricTableDelegate);
     515              :         }
     516              : 
     517            0 :         chip::Platform::Delete(mFabricTableDelegate);
     518            0 :         mFabricTableDelegate = nullptr;
     519              :     }
     520              : 
     521            0 :     if (mBDXTransferServer != nullptr)
     522              :     {
     523            0 :         mBDXTransferServer->Shutdown();
     524            0 :         chip::Platform::Delete(mBDXTransferServer);
     525            0 :         mBDXTransferServer = nullptr;
     526              :     }
     527              : 
     528            0 :     if (mCASEServer != nullptr)
     529              :     {
     530            0 :         mCASEServer->Shutdown();
     531            0 :         chip::Platform::Delete(mCASEServer);
     532            0 :         mCASEServer = nullptr;
     533              :     }
     534              : 
     535            0 :     if (mCASESessionManager != nullptr)
     536              :     {
     537            0 :         mCASESessionManager->Shutdown();
     538            0 :         Platform::Delete(mCASESessionManager);
     539            0 :         mCASESessionManager = nullptr;
     540              :     }
     541              : 
     542              :     // The above took care of CASE handshakes, and shutting down all the
     543              :     // controllers should have taken care of the PASE handshakes.  Clean up any
     544              :     // outstanding secure sessions (shouldn't really be any, since controllers
     545              :     // should have handled that, but just in case).
     546            0 :     if (mSessionMgr != nullptr)
     547              :     {
     548            0 :         mSessionMgr->ExpireAllSecureSessions();
     549              :     }
     550              : 
     551              :     // mCASEClientPool and mSessionSetupPool must be deallocated
     552              :     // after mCASESessionManager, which uses them.
     553              : 
     554            0 :     if (mSessionSetupPool != nullptr)
     555              :     {
     556            0 :         Platform::Delete(mSessionSetupPool);
     557            0 :         mSessionSetupPool = nullptr;
     558              :     }
     559              : 
     560            0 :     if (mCASEClientPool != nullptr)
     561              :     {
     562            0 :         Platform::Delete(mCASEClientPool);
     563            0 :         mCASEClientPool = nullptr;
     564              :     }
     565              : 
     566            0 :     Dnssd::Resolver::Instance().Shutdown();
     567              : 
     568              :     // Shut down the interaction model
     569            0 :     app::InteractionModelEngine::GetInstance()->Shutdown();
     570            0 :     app::InteractionModelEngine::GetInstance()->SetDataModelProvider(nullptr);
     571              : 
     572              :     // Shut down the TransportMgr. This holds Inet::UDPEndPoints so it must be shut down
     573              :     // before PlatformMgr().Shutdown() shuts down Inet.
     574            0 :     if (mTransportMgr != nullptr)
     575              :     {
     576            0 :         mTransportMgr->Close();
     577            0 :         chip::Platform::Delete(mTransportMgr);
     578            0 :         mTransportMgr = nullptr;
     579              :     }
     580              : 
     581            0 :     if (mExchangeMgr != nullptr)
     582              :     {
     583            0 :         mExchangeMgr->Shutdown();
     584              :     }
     585            0 :     if (mSessionMgr != nullptr)
     586              :     {
     587            0 :         mSessionMgr->Shutdown();
     588              :     }
     589              : 
     590            0 :     mSystemLayer        = nullptr;
     591            0 :     mUDPEndPointManager = nullptr;
     592              : #if INET_CONFIG_ENABLE_TCP_ENDPOINT
     593            0 :     mTCPEndPointManager = nullptr;
     594              : #endif
     595              : #if CONFIG_NETWORK_LAYER_BLE
     596            0 :     mBleLayer = nullptr;
     597              : #endif // CONFIG_NETWORK_LAYER_BLE
     598              : 
     599            0 :     if (mMessageCounterManager != nullptr)
     600              :     {
     601            0 :         chip::Platform::Delete(mMessageCounterManager);
     602            0 :         mMessageCounterManager = nullptr;
     603              :     }
     604              : 
     605            0 :     if (mExchangeMgr != nullptr)
     606              :     {
     607            0 :         chip::Platform::Delete(mExchangeMgr);
     608            0 :         mExchangeMgr = nullptr;
     609              :     }
     610              : 
     611            0 :     if (mUnsolicitedStatusHandler != nullptr)
     612              :     {
     613            0 :         Platform::Delete(mUnsolicitedStatusHandler);
     614            0 :         mUnsolicitedStatusHandler = nullptr;
     615              :     }
     616              : 
     617            0 :     if (mSessionMgr != nullptr)
     618              :     {
     619            0 :         chip::Platform::Delete(mSessionMgr);
     620            0 :         mSessionMgr = nullptr;
     621              :     }
     622              : 
     623            0 :     if (mReportScheduler != nullptr)
     624              :     {
     625            0 :         chip::Platform::Delete(mReportScheduler);
     626            0 :         mReportScheduler = nullptr;
     627              :     }
     628              : 
     629            0 :     if (mTimerDelegate != nullptr)
     630              :     {
     631            0 :         chip::Platform::Delete(mTimerDelegate);
     632            0 :         mTimerDelegate = nullptr;
     633              :     }
     634              : 
     635            0 :     if (mTempFabricTable != nullptr)
     636              :     {
     637            0 :         mTempFabricTable->Shutdown();
     638            0 :         chip::Platform::Delete(mTempFabricTable);
     639            0 :         mTempFabricTable = nullptr;
     640              :         // if we created a temp fabric table, then mFabrics points to it.
     641              :         // if we did not create a temp fabric table, then keep the reference
     642              :         // so that SetupController/Commissioner can use it
     643            0 :         mFabrics = nullptr;
     644              :     }
     645              : 
     646              : #if CONFIG_DEVICE_LAYER
     647              :     //
     648              :     // We can safely call PlatformMgr().Shutdown(), which like DeviceController::Shutdown(),
     649              :     // expects to be called with external thread synchronization and will not try to acquire the
     650              :     // stack lock.
     651              :     //
     652              :     // Actually stopping the event queue is a separable call that applications will have to sequence.
     653              :     // Consumers are expected to call PlaformMgr().StopEventLoopTask() before calling
     654              :     // DeviceController::Shutdown() in the CONFIG_DEVICE_LAYER configuration
     655              :     //
     656            0 :     DeviceLayer::PlatformMgr().Shutdown();
     657              : #endif
     658              : }
     659              : 
     660              : } // namespace Controller
     661              : } // namespace chip
        

Generated by: LCOV version 2.0-1