Matter SDK Coverage Report
Current view: top level - include/platform/internal - GenericPlatformManagerImpl.ipp (source / functions) Coverage Total Hit
Test: SHA:2a48c1efeab1c0f76f3adb3a0940b0f7de706453 Lines: 81.0 % 137 111
Test Date: 2026-01-31 08:14:20 Functions: 70.6 % 17 12

            Line data    Source code
       1              : /*
       2              :  *
       3              :  *    Copyright (c) 2020 Project CHIP Authors
       4              :  *    Copyright (c) 2018 Nest Labs, Inc.
       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              :  *          Contains non-inline method definitions for the
      22              :  *          GenericPlatformManagerImpl<> template.
      23              :  */
      24              : 
      25              : #ifndef GENERIC_PLATFORM_MANAGER_IMPL_CPP
      26              : #define GENERIC_PLATFORM_MANAGER_IMPL_CPP
      27              : 
      28              : #include <inttypes.h>
      29              : #include <new>
      30              : #include <platform/DiagnosticDataProvider.h>
      31              : #include <platform/PlatformManager.h>
      32              : #include <platform/internal/BLEManager.h>
      33              : #include <platform/internal/CHIPDeviceLayerInternal.h>
      34              : #include <platform/internal/EventLogging.h>
      35              : #include <platform/internal/GenericPlatformManagerImpl.h>
      36              : #include <platform/internal/NFCCommissioningManager.h>
      37              : 
      38              : #include <lib/support/CHIPMem.h>
      39              : #include <lib/support/CodeUtils.h>
      40              : #include <lib/support/logging/CHIPLogging.h>
      41              : 
      42              : namespace chip {
      43              : namespace DeviceLayer {
      44              : 
      45              : namespace Internal {
      46              : 
      47              : extern CHIP_ERROR InitEntropy();
      48              : 
      49              : template <class ImplClass>
      50           55 : CHIP_ERROR GenericPlatformManagerImpl<ImplClass>::_InitChipStack()
      51              : {
      52              :     CHIP_ERROR err;
      53              : 
      54           55 :     mMsgLayerWasActive = false;
      55              : 
      56              :     // Arrange for CHIP core errors to be translated to text
      57           55 :     RegisterCHIPLayerErrorFormatter();
      58              : 
      59              :     // Arrange for Device Layer errors to be translated to text.
      60           55 :     RegisterDeviceLayerErrorFormatter();
      61              : 
      62           55 :     err = InitEntropy();
      63          110 :     if (err != CHIP_NO_ERROR)
      64              :     {
      65            0 :         ChipLogError(DeviceLayer, "Entropy initialization failed: %" CHIP_ERROR_FORMAT, err.Format());
      66              :     }
      67           55 :     SuccessOrExit(err);
      68              : 
      69              :     // Initialize the CHIP system layer.
      70           55 :     err = SystemLayer().Init();
      71          110 :     if (err != CHIP_NO_ERROR)
      72              :     {
      73            0 :         ChipLogError(DeviceLayer, "SystemLayer initialization failed: %" CHIP_ERROR_FORMAT, err.Format());
      74              :     }
      75           55 :     SuccessOrExit(err);
      76              : 
      77              :     // Initialize the Configuration Manager.
      78           55 :     err = ConfigurationMgr().Init();
      79          110 :     if (err != CHIP_NO_ERROR)
      80              :     {
      81            0 :         ChipLogError(DeviceLayer, "Configuration Manager initialization failed: %" CHIP_ERROR_FORMAT, err.Format());
      82              :     }
      83           55 :     SuccessOrExit(err);
      84              : 
      85              :     // Initialize the CHIP UDP layer.
      86           55 :     err = UDPEndPointManager()->Init(SystemLayer());
      87          110 :     if (err != CHIP_NO_ERROR)
      88              :     {
      89            0 :         ChipLogError(DeviceLayer, "UDP initialization failed: %" CHIP_ERROR_FORMAT, err.Format());
      90              :     }
      91           55 :     SuccessOrExit(err);
      92              : 
      93              : #if INET_CONFIG_ENABLE_TCP_ENDPOINT
      94              :     // Initialize the CHIP TCP layer.
      95           55 :     err = TCPEndPointManager()->Init(SystemLayer());
      96          110 :     if (err != CHIP_NO_ERROR)
      97              :     {
      98            0 :         ChipLogError(DeviceLayer, "TCP initialization failed: %" CHIP_ERROR_FORMAT, err.Format());
      99              :     }
     100           55 :     SuccessOrExit(err);
     101              : #endif
     102              : 
     103              :     // TODO Perform dynamic configuration of the core CHIP objects based on stored settings.
     104              : 
     105              :     // Initialize the CHIP BLE manager.
     106              : #if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE
     107           55 :     err = BLEMgr().Init();
     108          110 :     if (err != CHIP_NO_ERROR)
     109              :     {
     110            0 :         ChipLogError(DeviceLayer, "BLEManager initialization failed: %" CHIP_ERROR_FORMAT, err.Format());
     111              :     }
     112           55 :     SuccessOrExit(err);
     113              : #endif
     114              : 
     115              :     // Initialize the Connectivity Manager object.
     116           55 :     err = ConnectivityMgr().Init();
     117          110 :     if (err != CHIP_NO_ERROR)
     118              :     {
     119            0 :         ChipLogError(DeviceLayer, "Connectivity Manager initialization failed: %" CHIP_ERROR_FORMAT, err.Format());
     120              :     }
     121           55 :     SuccessOrExit(err);
     122              : 
     123              :     // Initialize the NFC onboarding payload manager
     124              : #if CHIP_DEVICE_CONFIG_ENABLE_NFC_ONBOARDING_PAYLOAD
     125              :     err = NFCOnboardingPayloadMgr().Init();
     126              :     VerifyOrExit(
     127              :         err == CHIP_NO_ERROR,
     128              :         ChipLogError(DeviceLayer, "NFC onboarding payload manager initialization failed: %" CHIP_ERROR_FORMAT, err.Format()));
     129              : #endif
     130              : 
     131              :     // Initialize the CHIP NFC manager for NFC-based Commissioning
     132              : #if CHIP_DEVICE_CONFIG_ENABLE_NFC_BASED_COMMISSIONING
     133              :     err = NFCCommissioningMgr().Init();
     134              :     if (err != CHIP_NO_ERROR)
     135              :     {
     136              :         ChipLogError(DeviceLayer, "NFC-based Commissioning Manager initialization failed: %" CHIP_ERROR_FORMAT, err.Format());
     137              :     }
     138              :     SuccessOrExit(err);
     139              : #endif
     140              : 
     141              :     // TODO Initialize CHIP Event Logging.
     142              : 
     143              :     // TODO Initialize the Time Sync Manager object.
     144              : 
     145           55 :     SuccessOrExit(err);
     146              : 
     147           55 : exit:
     148           55 :     return err;
     149              : }
     150              : 
     151              : template <class ImplClass>
     152           55 : void GenericPlatformManagerImpl<ImplClass>::_Shutdown()
     153              : {
     154           55 :     ChipLogProgress(DeviceLayer, "Inet Layer shutdown");
     155           55 :     UDPEndPointManager()->Shutdown();
     156              : 
     157              : #if INET_CONFIG_ENABLE_TCP_ENDPOINT
     158           55 :     TCPEndPointManager()->Shutdown();
     159              : #endif
     160              : 
     161              : #if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE
     162           55 :     ChipLogProgress(DeviceLayer, "BLE Layer shutdown");
     163           55 :     BLEMgr().Shutdown();
     164              : #endif
     165              : 
     166              : #if CHIP_DEVICE_CONFIG_ENABLE_WIFIPAF
     167           55 :     ChipLogProgress(DeviceLayer, "WiFi-PAF Layer shutdown");
     168           55 :     WiFiPAF::WiFiPAFLayer::GetWiFiPAFLayer().Shutdown([](uint32_t id, WiFiPAF::WiFiPafRole role) {
     169            0 :         TEMPORARY_RETURN_IGNORED DeviceLayer::ConnectivityMgr().WiFiPAFShutdown(id, role);
     170              :     });
     171              : #endif
     172              : 
     173              : #if CHIP_DEVICE_CONFIG_ENABLE_NFC_BASED_COMMISSIONING
     174              :     ChipLogProgress(DeviceLayer, "NFCCommissioningMgr shutdown");
     175              :     NFCCommissioningMgr().Shutdown();
     176              : #endif
     177              : 
     178           55 :     ChipLogProgress(DeviceLayer, "System Layer shutdown");
     179           55 :     SystemLayer().Shutdown();
     180           55 : }
     181              : 
     182              : template <class ImplClass>
     183           39 : CHIP_ERROR GenericPlatformManagerImpl<ImplClass>::_AddEventHandler(PlatformManager::EventHandlerFunct handler, intptr_t arg)
     184              : {
     185           39 :     CHIP_ERROR err = CHIP_NO_ERROR;
     186              :     AppEventHandler * eventHandler;
     187              : 
     188              :     // Do nothing if the event handler is already registered.
     189           65 :     for (eventHandler = mAppEventHandlerList; eventHandler != nullptr; eventHandler = eventHandler->Next)
     190              :     {
     191           56 :         if (eventHandler->Handler == handler && eventHandler->Arg == arg)
     192              :         {
     193           30 :             ExitNow();
     194              :         }
     195              :     }
     196              : 
     197            9 :     eventHandler = (AppEventHandler *) chip::Platform::MemoryAlloc(sizeof(AppEventHandler));
     198            9 :     VerifyOrExit(eventHandler != nullptr, err = CHIP_ERROR_NO_MEMORY);
     199              : 
     200            9 :     eventHandler->Next    = mAppEventHandlerList;
     201            9 :     eventHandler->Handler = handler;
     202            9 :     eventHandler->Arg     = arg;
     203              : 
     204            9 :     mAppEventHandlerList = eventHandler;
     205              : 
     206           39 : exit:
     207           39 :     return err;
     208              : }
     209              : 
     210              : template <class ImplClass>
     211           16 : void GenericPlatformManagerImpl<ImplClass>::_RemoveEventHandler(PlatformManager::EventHandlerFunct handler, intptr_t arg)
     212              : {
     213              :     AppEventHandler ** eventHandlerIndirectPtr;
     214              : 
     215           43 :     for (eventHandlerIndirectPtr = &mAppEventHandlerList; *eventHandlerIndirectPtr != nullptr;)
     216              :     {
     217           27 :         AppEventHandler * eventHandler = (*eventHandlerIndirectPtr);
     218              : 
     219           27 :         if (eventHandler->Handler == handler && eventHandler->Arg == arg)
     220              :         {
     221            3 :             *eventHandlerIndirectPtr = eventHandler->Next;
     222            3 :             chip::Platform::MemoryFree(eventHandler);
     223              :         }
     224              :         else
     225              :         {
     226           24 :             eventHandlerIndirectPtr = &eventHandler->Next;
     227              :         }
     228              :     }
     229           16 : }
     230              : 
     231              : template <class ImplClass>
     232            1 : void GenericPlatformManagerImpl<ImplClass>::_HandleServerStarted()
     233              : {
     234            1 :     PlatformManagerDelegate * platformManagerDelegate = PlatformMgr().GetDelegate();
     235              : 
     236            1 :     if (platformManagerDelegate != nullptr)
     237              :     {
     238              :         uint32_t softwareVersion;
     239              : 
     240            0 :         if (ConfigurationMgr().GetSoftwareVersion(softwareVersion) == CHIP_NO_ERROR)
     241            0 :             platformManagerDelegate->OnStartUp(softwareVersion);
     242              :     }
     243            1 : }
     244              : 
     245              : template <class ImplClass>
     246            1 : void GenericPlatformManagerImpl<ImplClass>::_HandleServerShuttingDown()
     247              : {
     248            1 :     PlatformManagerDelegate * platformManagerDelegate = PlatformMgr().GetDelegate();
     249              : 
     250            1 :     if (platformManagerDelegate != nullptr)
     251              :     {
     252            0 :         platformManagerDelegate->OnShutDown();
     253              :     }
     254            1 : }
     255              : 
     256              : template <class ImplClass>
     257           90 : CHIP_ERROR GenericPlatformManagerImpl<ImplClass>::_ScheduleWork(AsyncWorkFunct workFunct, intptr_t arg)
     258              : {
     259           90 :     ChipDeviceEvent event{ .Type = DeviceEventType::kCallWorkFunct };
     260           90 :     event.CallWorkFunct = { .WorkFunct = workFunct, .Arg = arg };
     261           90 :     CHIP_ERROR err      = Impl()->PostEvent(&event);
     262          180 :     if (err != CHIP_NO_ERROR)
     263              :     {
     264            0 :         ChipLogError(DeviceLayer, "Failed to schedule work: %" CHIP_ERROR_FORMAT, err.Format());
     265              :     }
     266           90 :     return err;
     267              : }
     268              : 
     269              : template <class ImplClass>
     270            8 : CHIP_ERROR GenericPlatformManagerImpl<ImplClass>::_ScheduleBackgroundWork(AsyncWorkFunct workFunct, intptr_t arg)
     271              : {
     272            8 :     ChipDeviceEvent event{ .Type = DeviceEventType::kCallWorkFunct };
     273            8 :     event.CallWorkFunct = { .WorkFunct = workFunct, .Arg = arg };
     274            8 :     CHIP_ERROR err      = Impl()->PostBackgroundEvent(&event);
     275           16 :     if (err != CHIP_NO_ERROR)
     276              :     {
     277            0 :         ChipLogError(DeviceLayer, "Failed to schedule background work: %" CHIP_ERROR_FORMAT, err.Format());
     278              :     }
     279            8 :     return err;
     280              : }
     281              : 
     282              : template <class ImplClass>
     283            8 : CHIP_ERROR GenericPlatformManagerImpl<ImplClass>::_PostBackgroundEvent(const ChipDeviceEvent * event)
     284              : {
     285              :     // Impl class must override to implement background event processing
     286            8 :     return Impl()->PostEvent(event);
     287              : }
     288              : 
     289              : template <class ImplClass>
     290            0 : void GenericPlatformManagerImpl<ImplClass>::_RunBackgroundEventLoop(void)
     291              : {
     292              :     // Impl class must override to implement background event processing
     293            0 : }
     294              : 
     295              : template <class ImplClass>
     296            0 : CHIP_ERROR GenericPlatformManagerImpl<ImplClass>::_StartBackgroundEventLoopTask(void)
     297              : {
     298              :     // Impl class must override to implement background event processing
     299            0 :     return CHIP_NO_ERROR;
     300              : }
     301              : 
     302              : template <class ImplClass>
     303            0 : CHIP_ERROR GenericPlatformManagerImpl<ImplClass>::_StopBackgroundEventLoopTask(void)
     304              : {
     305              :     // Impl class must override to implement background event processing
     306            0 :     return CHIP_NO_ERROR;
     307              : }
     308              : 
     309              : template <class ImplClass>
     310          143 : void GenericPlatformManagerImpl<ImplClass>::_DispatchEvent(const ChipDeviceEvent * event)
     311              : {
     312              : #if (CHIP_DISPATCH_EVENT_LONG_DISPATCH_TIME_WARNING_THRESHOLD_MS != 0)
     313          143 :     System::Clock::Timestamp start = System::SystemClock().GetMonotonicTimestamp();
     314              : #endif // CHIP_DISPATCH_EVENT_LONG_DISPATCH_TIME_WARNING_THRESHOLD_MS != 0
     315              : 
     316          143 :     switch (event->Type)
     317              :     {
     318            0 :     case DeviceEventType::kNoOp:
     319              :         // Do nothing for no-op events.
     320            0 :         break;
     321              : 
     322           20 :     case DeviceEventType::kChipLambdaEvent:
     323           20 :         event->LambdaEvent();
     324           20 :         break;
     325              : 
     326           98 :     case DeviceEventType::kCallWorkFunct:
     327              :         // If the event is a "call work function" event, call the specified function.
     328           98 :         event->CallWorkFunct.WorkFunct(event->CallWorkFunct.Arg);
     329           98 :         break;
     330              : 
     331           25 :     default:
     332              :         // For all other events, deliver the event to each of the components in the Device Layer.
     333           25 :         Impl()->DispatchEventToDeviceLayer(event);
     334              : 
     335              :         // If the event is not an internal event, also deliver it to the application's registered
     336              :         // event handlers.
     337           25 :         if (!event->IsInternal())
     338              :         {
     339           25 :             Impl()->DispatchEventToApplication(event);
     340              :         }
     341              : 
     342           25 :         break;
     343              :     }
     344              : 
     345              : #if (CHIP_DISPATCH_EVENT_LONG_DISPATCH_TIME_WARNING_THRESHOLD_MS != 0)
     346          143 :     uint32_t deltaMs = System::Clock::Milliseconds32(System::SystemClock().GetMonotonicTimestamp() - start).count();
     347          143 :     if (deltaMs > CHIP_DISPATCH_EVENT_LONG_DISPATCH_TIME_WARNING_THRESHOLD_MS)
     348              :     {
     349            2 :         ChipLogError(DeviceLayer, "Long dispatch time: %" PRIu32 " ms, for event type %d", deltaMs, event->Type);
     350              :     }
     351              : #endif // CHIP_DISPATCH_EVENT_LONG_DISPATCH_TIME_WARNING_THRESHOLD_MS != 0
     352          143 : }
     353              : 
     354              : template <class ImplClass>
     355           25 : void GenericPlatformManagerImpl<ImplClass>::DispatchEventToDeviceLayer(const ChipDeviceEvent * event)
     356              : {
     357              :     // Dispatch the event to all the components in the Device Layer.
     358              : #if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE
     359           25 :     BLEMgr().OnPlatformEvent(event);
     360              : #endif
     361              : #if CHIP_DEVICE_CONFIG_ENABLE_THREAD
     362           25 :     ThreadStackMgr().OnPlatformEvent(event);
     363              : #endif
     364           25 :     ConnectivityMgr().OnPlatformEvent(event);
     365           25 : }
     366              : 
     367              : template <class ImplClass>
     368           25 : void GenericPlatformManagerImpl<ImplClass>::DispatchEventToApplication(const ChipDeviceEvent * event)
     369              : {
     370              :     // Dispatch the event to each of the registered application event handlers.
     371           48 :     for (AppEventHandler * eventHandler = mAppEventHandlerList; eventHandler != nullptr;)
     372              :     {
     373           23 :         AppEventHandler * nextEventHandler = eventHandler->Next;
     374           23 :         eventHandler->Handler(event, eventHandler->Arg);
     375           23 :         eventHandler = nextEventHandler;
     376              :     }
     377           25 : }
     378              : 
     379              : template <class ImplClass>
     380            0 : void GenericPlatformManagerImpl<ImplClass>::HandleMessageLayerActivityChanged(bool messageLayerIsActive)
     381              : {
     382            0 :     GenericPlatformManagerImpl<ImplClass> & self = PlatformMgrImpl();
     383              : 
     384            0 :     if (messageLayerIsActive != self.mMsgLayerWasActive)
     385              :     {
     386            0 :         self.mMsgLayerWasActive = messageLayerIsActive;
     387              :     }
     388            0 : }
     389              : 
     390              : // Fully instantiate the generic implementation class in whatever compilation unit includes this file.
     391              : // NB: This must come after all templated class members are defined.
     392              : template class GenericPlatformManagerImpl<PlatformManagerImpl>;
     393              : 
     394              : } // namespace Internal
     395              : } // namespace DeviceLayer
     396              : } // namespace chip
     397              : 
     398              : #endif // GENERIC_PLATFORM_MANAGER_IMPL_CPP
        

Generated by: LCOV version 2.0-1