diff --git a/src/darwin/Framework/CHIP/MTRControllerFactory.mm b/src/darwin/Framework/CHIP/MTRControllerFactory.mm index 38d0cfe927388c..03d4fc8496d1c0 100644 --- a/src/darwin/Framework/CHIP/MTRControllerFactory.mm +++ b/src/darwin/Framework/CHIP/MTRControllerFactory.mm @@ -76,6 +76,8 @@ @interface MTRControllerFactory () - (BOOL)findMatchingFabric:(FabricTable &)fabricTable params:(MTRDeviceControllerStartupParams *)params fabric:(const FabricInfo * _Nullable * _Nonnull)fabric; + +- (MTRDeviceController * _Nullable)maybeInitializeOTAProvider:(MTRDeviceController * _Nonnull)controller; @end @implementation MTRControllerFactory @@ -387,7 +389,7 @@ - (MTRDeviceController * _Nullable)startControllerOnExistingFabric:(MTRDeviceCon return nil; } - return controller; + return [self maybeInitializeOTAProvider:controller]; } - (MTRDeviceController * _Nullable)startControllerOnNewFabric:(MTRDeviceControllerStartupParams *)startupParams @@ -447,7 +449,7 @@ - (MTRDeviceController * _Nullable)startControllerOnNewFabric:(MTRDeviceControll return nil; } - return controller; + return [self maybeInitializeOTAProvider:controller]; } - (MTRDeviceController * _Nullable)createController @@ -462,11 +464,6 @@ - (MTRDeviceController * _Nullable)createController // Bringing up the first controller. Start the event loop now. If we // fail to bring it up, its cleanup will stop the event loop again. chip::DeviceLayer::PlatformMgrImpl().StartEventLoopTask(); - - if (_otaProviderDelegateBridge) { - auto systemState = _controllerFactory->GetSystemState(); - _otaProviderDelegateBridge->Init(systemState->SystemLayer(), systemState->ExchangeMgr()); - } } // Add the controller to _controllers now, so if we fail partway through its @@ -516,6 +513,27 @@ - (BOOL)findMatchingFabric:(FabricTable &)fabricTable return YES; } +// Initialize the MTROTAProviderDelegateBridge if it has not been initialized already +// +// Returns NO on failure, YES on success. +// If the provider has been initialized already, it is not considered as a failure. +// +- (MTRDeviceController * _Nullable)maybeInitializeOTAProvider:(MTRDeviceController * _Nonnull)controller +{ + VerifyOrReturnValue(_otaProviderDelegateBridge != nil, controller); + VerifyOrReturnValue([_controllers count] == 1, controller); + + auto systemState = _controllerFactory->GetSystemState(); + CHIP_ERROR err = _otaProviderDelegateBridge->Init(systemState->SystemLayer(), systemState->ExchangeMgr()); + if (CHIP_NO_ERROR != err) { + MTR_LOG_ERROR("Failed to init provider delegate bridge"); + [controller shutdown]; + return nil; + } + + return controller; +} + @end @implementation MTRControllerFactory (InternalMethods) diff --git a/src/darwin/Framework/CHIP/MTROTAProviderDelegateBridge.h b/src/darwin/Framework/CHIP/MTROTAProviderDelegateBridge.h index 8240ae9e56cb63..97aa258dad2617 100644 --- a/src/darwin/Framework/CHIP/MTROTAProviderDelegateBridge.h +++ b/src/darwin/Framework/CHIP/MTROTAProviderDelegateBridge.h @@ -27,7 +27,7 @@ class MTROTAProviderDelegateBridge : public chip::app::Clusters::OTAProviderDele MTROTAProviderDelegateBridge(id delegate); ~MTROTAProviderDelegateBridge(); - void Init(chip::System::Layer * systemLayer, chip::Messaging::ExchangeManager * exchangeManager); + CHIP_ERROR Init(chip::System::Layer * systemLayer, chip::Messaging::ExchangeManager * exchangeManager); void Shutdown(); void HandleQueryImage( diff --git a/src/darwin/Framework/CHIP/MTROTAProviderDelegateBridge.mm b/src/darwin/Framework/CHIP/MTROTAProviderDelegateBridge.mm index 5ef05cb0adaff0..2045ce1f2627ce 100644 --- a/src/darwin/Framework/CHIP/MTROTAProviderDelegateBridge.mm +++ b/src/darwin/Framework/CHIP/MTROTAProviderDelegateBridge.mm @@ -314,9 +314,9 @@ void ResetState() Clusters::OTAProvider::SetDelegate(kOtaProviderEndpoint, nullptr); } -void MTROTAProviderDelegateBridge::Init(System::Layer * systemLayer, Messaging::ExchangeManager * exchangeManager) +CHIP_ERROR MTROTAProviderDelegateBridge::Init(System::Layer * systemLayer, Messaging::ExchangeManager * exchangeManager) { - gOtaSender.Init(systemLayer, exchangeManager); + return gOtaSender.Init(systemLayer, exchangeManager); } void MTROTAProviderDelegateBridge::Shutdown() { gOtaSender.Shutdown(); }