Skip to content

Commit

Permalink
Fix OTA delegate bridge init on Darwin
Browse files Browse the repository at this point in the history
  • Loading branch information
vivien-apple committed Jul 29, 2022
1 parent f6b7f35 commit 9dca574
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
32 changes: 25 additions & 7 deletions src/darwin/Framework/CHIP/MTRControllerFactory.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -387,7 +389,7 @@ - (MTRDeviceController * _Nullable)startControllerOnExistingFabric:(MTRDeviceCon
return nil;
}

return controller;
return [self maybeInitializeOTAProvider:controller];
}

- (MTRDeviceController * _Nullable)startControllerOnNewFabric:(MTRDeviceControllerStartupParams *)startupParams
Expand Down Expand Up @@ -447,7 +449,7 @@ - (MTRDeviceController * _Nullable)startControllerOnNewFabric:(MTRDeviceControll
return nil;
}

return controller;
return [self maybeInitializeOTAProvider:controller];
}

- (MTRDeviceController * _Nullable)createController
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/darwin/Framework/CHIP/MTROTAProviderDelegateBridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class MTROTAProviderDelegateBridge : public chip::app::Clusters::OTAProviderDele
MTROTAProviderDelegateBridge(id<MTROTAProviderDelegate> 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(
Expand Down
4 changes: 2 additions & 2 deletions src/darwin/Framework/CHIP/MTROTAProviderDelegateBridge.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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(); }
Expand Down

0 comments on commit 9dca574

Please sign in to comment.