Skip to content

Commit 34cc8b8

Browse files
Darwin: Move preWarmCommissioningSession to MTRDeviceControllerFactory (#33184)
* Darwin: Move preWarmCommissioningSession to MTRDeviceControllerFactory This makes it possible to start pre-warming before a MTRDeviceController has been created, since doing that requires the client to have obtained various configuration details. * Apply suggestions from code review Co-authored-by: Boris Zbarsky <bzbarsky@apple.com> * Tweaks from review * restyle --------- Co-authored-by: Boris Zbarsky <bzbarsky@apple.com>
1 parent f9ac954 commit 34cc8b8

11 files changed

+148
-90
lines changed

src/darwin/Framework/CHIP/MTRDeviceController.h

+2-10
Original file line numberDiff line numberDiff line change
@@ -178,16 +178,8 @@ MTR_AVAILABLE(ios(16.1), macos(13.0), watchos(9.1), tvos(16.1))
178178
error:(NSError * __autoreleasing *)error
179179
MTR_AVAILABLE(ios(16.4), macos(13.3), watchos(9.4), tvos(16.4));
180180

181-
/**
182-
* Optionally pre-warm the controller for setting up a commissioning session.
183-
* This may be called before setupCommissioningSessionWithPayload if it's known
184-
* that a commissioning attempt will soon take place but the commissioning
185-
* payload is not known yet.
186-
*
187-
* For example this may do a BLE scan in advance so results are ready earlier
188-
* once the discriminator is known.
189-
*/
190-
- (void)preWarmCommissioningSession MTR_AVAILABLE(ios(16.4), macos(13.3), watchos(9.4), tvos(16.4));
181+
- (void)preWarmCommissioningSession MTR_AVAILABLE(ios(16.4), macos(13.3), watchos(9.4), tvos(16.4))
182+
MTR_NEWLY_DEPRECATED("-[MTRDeviceControllerFactory preWarmCommissioningSession]");
191183

192184
/**
193185
* Set the Delegate for the device controller as well as the Queue on which the Delegate callbacks will be triggered

src/darwin/Framework/CHIP/MTRDeviceController.mm

+1-10
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@
8686
static NSString * const kErrorPartialDacVerifierInit = @"Init failure while creating a partial DAC verifier";
8787
static NSString * const kErrorPairDevice = @"Failure while pairing the device";
8888
static NSString * const kErrorStopPairing = @"Failure while trying to stop the pairing process";
89-
static NSString * const kErrorPreWarmCommissioning = @"Failure while trying to pre-warm the commissioning process";
9089
static NSString * const kErrorOpenPairingWindow = @"Open Pairing Window failed";
9190
static NSString * const kErrorNotRunning = @"Controller is not running. Call startup first.";
9291
static NSString * const kErrorSetupCodeGen = @"Generating Manual Pairing Code failed";
@@ -896,15 +895,7 @@ - (BOOL)stopBrowseForCommissionables
896895

897896
- (void)preWarmCommissioningSession
898897
{
899-
auto block = ^{
900-
auto errorCode = chip::DeviceLayer::PlatformMgrImpl().PrepareCommissioning();
901-
MATTER_LOG_METRIC(kMetricPreWarmCommissioning, errorCode);
902-
903-
// The checkForError is just so it logs
904-
[MTRDeviceController checkForError:errorCode logMsg:kErrorPreWarmCommissioning error:nil];
905-
};
906-
907-
[self syncRunOnWorkQueue:block error:nil];
898+
[_factory preWarmCommissioningSession];
908899
}
909900

910901
- (MTRBaseDevice *)deviceBeingCommissionedWithNodeID:(NSNumber *)nodeID error:(NSError * __autoreleasing *)error

src/darwin/Framework/CHIP/MTRDeviceControllerFactory.h

+12
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,18 @@ MTR_AVAILABLE(ios(16.4), macos(13.3), watchos(9.4), tvos(16.4))
177177
- (MTRDeviceController * _Nullable)createControllerOnNewFabric:(MTRDeviceControllerStartupParams *)startupParams
178178
error:(NSError * __autoreleasing *)error;
179179

180+
/**
181+
* If possible, pre-warm the Matter stack for setting up a commissioning session.
182+
*
183+
* This may be called before -[MTRDeviceController setupCommissioningSessionWithPayload:]
184+
* if it is known that a commissioning attempt will soon take place, but the commissioning
185+
* payload is not known yet.
186+
*
187+
* The controller factory must be running for pre-warming to take place. Pre-warming can take place
188+
* before any controllers are started.
189+
*/
190+
- (void)preWarmCommissioningSession MTR_NEWLY_AVAILABLE;
191+
180192
@end
181193

182194
/**

src/darwin/Framework/CHIP/MTRDeviceControllerFactory.mm

+40
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#import "MTRFabricInfo_Internal.h"
3535
#import "MTRFramework.h"
3636
#import "MTRLogging_Internal.h"
37+
#import "MTRMetricKeys.h"
3738
#import "MTRMetricsCollector.h"
3839
#import "MTROTAProviderDelegateBridge.h"
3940
#import "MTROperationalBrowser.h"
@@ -66,6 +67,7 @@
6667

6768
using namespace chip;
6869
using namespace chip::Controller;
70+
using namespace chip::Tracing::DarwinFramework;
6971

7072
static bool sExitHandlerRegistered = false;
7173
static void ShutdownOnExit()
@@ -82,6 +84,11 @@ @interface MTRDeviceControllerFactoryParams ()
8284

8385
@end
8486

87+
MTR_DIRECT_MEMBERS
88+
@interface MTRDeviceControllerFactory ()
89+
- (void)preWarmCommissioningSessionDone;
90+
@end
91+
8592
MTR_DIRECT_MEMBERS
8693
@implementation MTRDeviceControllerFactory {
8794
dispatch_queue_t _chipWorkQueue;
@@ -163,6 +170,13 @@ @implementation MTRDeviceControllerFactory {
163170
// in an atomic way that endpoint IDs are unique.
164171
NSMutableArray<MTRServerEndpoint *> * _serverEndpoints;
165172
os_unfair_lock _serverEndpointsLock; // Protects access to _serverEndpoints.
173+
174+
class final : public DeviceLayer::BleScannerDelegate {
175+
void OnBleScanStopped() override
176+
{
177+
[MTRDeviceControllerFactory.sharedInstance preWarmCommissioningSessionDone];
178+
}
179+
} _preWarmingDelegate;
166180
}
167181

168182
+ (void)initialize
@@ -742,6 +756,32 @@ - (MTRDeviceController * _Nullable)createControllerOnNewFabric:(MTRDeviceControl
742756
error:error];
743757
}
744758

759+
- (void)preWarmCommissioningSession
760+
{
761+
dispatch_async(_chipWorkQueue, ^{
762+
CHIP_ERROR err = CHIP_ERROR_INCORRECT_STATE;
763+
if (!self->_running) {
764+
MTR_LOG_ERROR("Can't pre-warm, Matter controller factory is not running");
765+
} else {
766+
MTR_LOG_DEFAULT("Pre-warming commissioning session");
767+
self->_controllerFactory->EnsureAndRetainSystemState();
768+
err = DeviceLayer::PlatformMgrImpl().StartBleScan(&self->_preWarmingDelegate, DeviceLayer::BleScanMode::kPreWarm);
769+
if (err != CHIP_NO_ERROR) {
770+
MTR_LOG_ERROR("Pre-warming failed: %" CHIP_ERROR_FORMAT, err.Format());
771+
self->_controllerFactory->ReleaseSystemState();
772+
}
773+
}
774+
MATTER_LOG_METRIC(kMetricPreWarmCommissioning, err);
775+
});
776+
}
777+
778+
- (void)preWarmCommissioningSessionDone
779+
{
780+
assertChipStackLockedByCurrentThread();
781+
MTR_LOG_DEFAULT("Pre-warming done");
782+
self->_controllerFactory->ReleaseSystemState();
783+
}
784+
745785
// Finds a fabric that matches the given params, if one exists.
746786
//
747787
// Returns NO on failure, YES on success. If YES is returned, the

src/platform/Darwin/BLEManagerImpl.cpp

+7-13
Original file line numberDiff line numberDiff line change
@@ -87,24 +87,18 @@ void BLEManagerImpl::_Shutdown()
8787
}
8888
}
8989

90-
CHIP_ERROR BLEManagerImpl::StartScan(BleScannerDelegate * delegate)
90+
CHIP_ERROR BLEManagerImpl::StartScan(BleScannerDelegate * delegate, BleScanMode mode)
9191
{
92-
if (mConnectionDelegate)
93-
{
94-
static_cast<BleConnectionDelegateImpl *>(mConnectionDelegate)->StartScan(delegate);
95-
return CHIP_NO_ERROR;
96-
}
97-
return CHIP_ERROR_INCORRECT_STATE;
92+
VerifyOrReturnError(mConnectionDelegate != nullptr, CHIP_ERROR_INCORRECT_STATE);
93+
static_cast<BleConnectionDelegateImpl *>(mConnectionDelegate)->StartScan(delegate, mode);
94+
return CHIP_NO_ERROR;
9895
}
9996

10097
CHIP_ERROR BLEManagerImpl::StopScan()
10198
{
102-
if (mConnectionDelegate)
103-
{
104-
static_cast<BleConnectionDelegateImpl *>(mConnectionDelegate)->StopScan();
105-
return CHIP_NO_ERROR;
106-
}
107-
return CHIP_ERROR_INCORRECT_STATE;
99+
VerifyOrReturnError(mConnectionDelegate != nullptr, CHIP_ERROR_INCORRECT_STATE);
100+
static_cast<BleConnectionDelegateImpl *>(mConnectionDelegate)->StopScan();
101+
return CHIP_NO_ERROR;
108102
}
109103

110104
bool BLEManagerImpl::_IsAdvertisingEnabled()

src/platform/Darwin/BLEManagerImpl.h

+2-4
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,12 @@
2525

2626
#include <lib/core/Global.h>
2727
#include <lib/support/CodeUtils.h>
28+
#include <platform/Darwin/BleScannerDelegate.h>
2829

2930
#if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE
3031

3132
namespace chip {
3233
namespace DeviceLayer {
33-
34-
class BleScannerDelegate;
35-
3634
namespace Internal {
3735

3836
using namespace chip::Ble;
@@ -48,7 +46,7 @@ class BLEManagerImpl final : public BLEManager, private BleLayer
4846

4947
public:
5048
CHIP_ERROR ConfigureBle(uint32_t aNodeId, bool aIsCentral) { return CHIP_NO_ERROR; }
51-
CHIP_ERROR StartScan(BleScannerDelegate * delegate = nullptr);
49+
CHIP_ERROR StartScan(BleScannerDelegate * delegate, BleScanMode mode = BleScanMode::kDefault);
5250
CHIP_ERROR StopScan();
5351

5452
private:

src/platform/Darwin/BleConnectionDelegate.h

+6-4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#pragma once
1919

2020
#include <ble/Ble.h>
21+
#include <platform/Darwin/BleScannerDelegate.h>
2122

2223
namespace chip {
2324
namespace DeviceLayer {
@@ -26,11 +27,12 @@ namespace Internal {
2627
class BleConnectionDelegateImpl : public Ble::BleConnectionDelegate
2728
{
2829
public:
29-
void StartScan(BleScannerDelegate * delegate = nullptr);
30+
void StartScan(BleScannerDelegate * delegate, BleScanMode mode = BleScanMode::kDefault);
3031
void StopScan();
31-
virtual void NewConnection(Ble::BleLayer * bleLayer, void * appState, const SetupDiscriminator & connDiscriminator);
32-
virtual void NewConnection(Ble::BleLayer * bleLayer, void * appState, BLE_CONNECTION_OBJECT connObj);
33-
virtual CHIP_ERROR CancelConnection();
32+
33+
void NewConnection(Ble::BleLayer * bleLayer, void * appState, const SetupDiscriminator & connDiscriminator) override;
34+
void NewConnection(Ble::BleLayer * bleLayer, void * appState, BLE_CONNECTION_OBJECT connObj) override;
35+
CHIP_ERROR CancelConnection() override;
3436

3537
private:
3638
CHIP_ERROR DoCancel();

0 commit comments

Comments
 (0)