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

Generated by: LCOV version 2.0-1