Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3000797

Browse files
bzbarsky-applevivien-apple
authored andcommittedSep 7, 2023
[Darwin] Rename MTRDevicePairingDelegate to MTRDeviceControllerDelegate and MTRPairingStatus to MTRCommissioningStatus (#23587)
* [Darwin] Rename MTRDevicePairingDelegate to MTRDeviceControllerDelegate and MTRPairingStatus to MTRCommissioningStatus This is a re-landing of PR #22633 but with changes made for backwards compat. * [Darwin] Rename MTRDevicePairingDelegate to MTRDeviceControllerDelegate * [Darwin] Rename MTRPairingStatus to MTRCommissioningStatus * Add backwards compat shims for the old APIs. * Address review comments. Co-authored-by: Vivien Nicolas <vnicolas@apple.com>
1 parent 3bfcb68 commit 3000797

15 files changed

+199
-106
lines changed
 

‎examples/darwin-framework-tool/BUILD.gn

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,10 @@ executable("darwin-framework-tool") {
128128
"commands/common/MTRError_Utils.h",
129129
"commands/common/MTRLogging.h",
130130
"commands/pairing/Commands.h",
131+
"commands/pairing/DeviceControllerDelegateBridge.mm",
131132
"commands/pairing/OpenCommissioningWindowCommand.h",
132133
"commands/pairing/OpenCommissioningWindowCommand.mm",
133134
"commands/pairing/PairingCommandBridge.mm",
134-
"commands/pairing/PairingDelegateBridge.mm",
135135
"commands/payload/SetupPayloadParseCommand.mm",
136136
"commands/provider/Commands.h",
137137
"commands/provider/OTAProviderDelegate.mm",

‎examples/darwin-framework-tool/commands/pairing/PairingDelegateBridge.h ‎examples/darwin-framework-tool/commands/pairing/DeviceControllerDelegateBridge.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
#import <Matter/Matter.h>
2222

23-
@interface CHIPToolPairingDelegate : NSObject <MTRDevicePairingDelegate>
23+
@interface CHIPToolDeviceControllerDelegate : NSObject <MTRDeviceControllerDelegate>
2424
@property PairingCommandBridge * commandBridge;
2525
@property chip::NodeId deviceID;
2626
@property MTRDeviceController * commissioner;

‎examples/darwin-framework-tool/commands/pairing/PairingDelegateBridge.mm ‎examples/darwin-framework-tool/commands/pairing/DeviceControllerDelegateBridge.mm

+8-8
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,29 @@
1616
*
1717
*/
1818

19-
#include "PairingDelegateBridge.h"
19+
#include "DeviceControllerDelegateBridge.h"
2020
#import <Matter/Matter.h>
2121

22-
@interface CHIPToolPairingDelegate ()
22+
@interface CHIPToolDeviceControllerDelegate ()
2323
@end
2424

25-
@implementation CHIPToolPairingDelegate
26-
- (void)onStatusUpdate:(MTRPairingStatus)status
25+
@implementation CHIPToolDeviceControllerDelegate
26+
- (void)onStatusUpdate:(MTRCommissioningStatus)status
2727
{
2828
NSLog(@"Pairing Status Update: %tu", status);
2929
switch (status) {
30-
case MTRPairingStatusSuccess:
30+
case MTRCommissioningStatusSuccess:
3131
ChipLogProgress(chipTool, "Secure Pairing Success");
3232
ChipLogProgress(chipTool, "CASE establishment successful");
3333
break;
34-
case MTRPairingStatusFailed:
34+
case MTRCommissioningStatusFailed:
3535
ChipLogError(chipTool, "Secure Pairing Failed");
3636
_commandBridge->SetCommandExitStatus(CHIP_ERROR_INCORRECT_STATE);
3737
break;
38-
case MTRPairingStatusDiscoveringMoreDevices:
38+
case MTRCommissioningStatusDiscoveringMoreDevices:
3939
ChipLogProgress(chipTool, "Secure Pairing Discovering More Devices");
4040
break;
41-
case MTRPairingStatusUnknown:
41+
case MTRCommissioningStatusUnknown:
4242
ChipLogError(chipTool, "Uknown Pairing Status");
4343
break;
4444
}

‎examples/darwin-framework-tool/commands/pairing/PairingCommandBridge.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class PairingCommandBridge : public CHIPCommandBridge
7878
void PairWithCode(NSError * __autoreleasing * error);
7979
void PairWithPayload(NSError * __autoreleasing * error);
8080
void Unpair();
81-
void SetUpPairingDelegate();
81+
void SetUpDeviceControllerDelegate();
8282

8383
const PairingMode mPairingMode;
8484
const PairingNetworkType mNetworkType;

‎examples/darwin-framework-tool/commands/pairing/PairingCommandBridge.mm

+10-10
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,23 @@
1919
#import <Matter/Matter.h>
2020

2121
#include "../common/CHIPCommandBridge.h"
22+
#include "DeviceControllerDelegateBridge.h"
2223
#include "PairingCommandBridge.h"
23-
#include "PairingDelegateBridge.h"
2424
#include <lib/support/logging/CHIPLogging.h>
2525

2626
#import "MTRError_Utils.h"
2727

2828
using namespace ::chip;
2929
using namespace ::chip::Controller;
3030

31-
void PairingCommandBridge::SetUpPairingDelegate()
31+
void PairingCommandBridge::SetUpDeviceControllerDelegate()
3232
{
3333
dispatch_queue_t callbackQueue = dispatch_queue_create("com.chip.pairing", DISPATCH_QUEUE_SERIAL);
34-
CHIPToolPairingDelegate * pairing = [[CHIPToolPairingDelegate alloc] init];
34+
CHIPToolDeviceControllerDelegate * deviceControllerDelegate = [[CHIPToolDeviceControllerDelegate alloc] init];
3535
MTRCommissioningParameters * params = [[MTRCommissioningParameters alloc] init];
3636
MTRDeviceController * commissioner = CurrentCommissioner();
3737

38-
[pairing setDeviceID:mNodeId];
38+
[deviceControllerDelegate setDeviceID:mNodeId];
3939
switch (mNetworkType) {
4040
case PairingNetworkType::None:
4141
case PairingNetworkType::Ethernet:
@@ -49,11 +49,11 @@
4949
break;
5050
}
5151

52-
[pairing setCommandBridge:this];
53-
[pairing setParams:params];
54-
[pairing setCommissioner:commissioner];
52+
[deviceControllerDelegate setCommandBridge:this];
53+
[deviceControllerDelegate setParams:params];
54+
[deviceControllerDelegate setCommissioner:commissioner];
5555

56-
[commissioner setPairingDelegate:pairing queue:callbackQueue];
56+
[commissioner setDeviceControllerDelegate:deviceControllerDelegate queue:callbackQueue];
5757
}
5858

5959
CHIP_ERROR PairingCommandBridge::RunCommand()
@@ -79,7 +79,7 @@
7979

8080
void PairingCommandBridge::PairWithCode(NSError * __autoreleasing * error)
8181
{
82-
SetUpPairingDelegate();
82+
SetUpDeviceControllerDelegate();
8383
auto * payload = [[MTRSetupPayload alloc] initWithSetupPasscode:@(mSetupPINCode) discriminator:@(mDiscriminator)];
8484
MTRDeviceController * commissioner = CurrentCommissioner();
8585
[commissioner setupCommissioningSessionWithPayload:payload newNodeID:@(mNodeId) error:error];
@@ -88,7 +88,7 @@
8888
void PairingCommandBridge::PairWithPayload(NSError * __autoreleasing * error)
8989
{
9090
NSString * onboardingPayload = [NSString stringWithUTF8String:mOnboardingPayload];
91-
SetUpPairingDelegate();
91+
SetUpDeviceControllerDelegate();
9292
auto * payload = [MTRSetupPayload setupPayloadWithOnboardingPayload:onboardingPayload error:error];
9393
if (payload == nil) {
9494
return;

‎examples/darwin-framework-tool/commands/tests/TestCommandBridge.h

+11-11
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ const char * getScriptsFolder() { return basePath; }
4343

4444
constexpr const char * kDefaultKey = "default";
4545

46-
@interface TestPairingDelegate : NSObject <MTRDevicePairingDelegate>
46+
@interface TestDeviceControllerDelegate : NSObject <MTRDeviceControllerDelegate>
4747
@property TestCommandBridge * commandBridge;
4848
@property chip::NodeId deviceId;
4949
@property BOOL active; // Whether to pass on notifications to the commandBridge
5050

51-
- (void)onStatusUpdate:(MTRPairingStatus)status;
51+
- (void)onStatusUpdate:(MTRCommissioningStatus)status;
5252
- (void)onPairingComplete:(NSError * _Nullable)error;
5353
- (void)onPairingDeleted:(NSError * _Nullable)error;
5454
- (void)onCommissioningComplete:(NSError * _Nullable)error;
@@ -70,7 +70,7 @@ class TestCommandBridge : public CHIPCommandBridge,
7070
public:
7171
TestCommandBridge(const char * _Nonnull commandName)
7272
: CHIPCommandBridge(commandName)
73-
, mPairingDelegate([[TestPairingDelegate alloc] initWithTestCommandBridge:this])
73+
, mDeviceControllerDelegate([[TestDeviceControllerDelegate alloc] initWithTestCommandBridge:this])
7474
{
7575
AddArgument("delayInMs", 0, UINT64_MAX, &mDelayInMs);
7676
AddArgument("PICS", &mPICSFilePath);
@@ -183,9 +183,9 @@ class TestCommandBridge : public CHIPCommandBridge,
183183

184184
SetIdentity(identity);
185185

186-
[controller setPairingDelegate:mPairingDelegate queue:mCallbackQueue];
187-
[mPairingDelegate setDeviceId:value.nodeId];
188-
[mPairingDelegate setActive:YES];
186+
[controller setDeviceControllerDelegate:mDeviceControllerDelegate queue:mCallbackQueue];
187+
[mDeviceControllerDelegate setDeviceId:value.nodeId];
188+
[mDeviceControllerDelegate setActive:YES];
189189

190190
NSString * payloadStr = [[NSString alloc] initWithBytes:value.payload.data()
191191
length:value.payload.size()
@@ -521,21 +521,21 @@ class TestCommandBridge : public CHIPCommandBridge,
521521
}
522522

523523
private:
524-
TestPairingDelegate * _Nonnull mPairingDelegate;
524+
TestDeviceControllerDelegate * _Nonnull mDeviceControllerDelegate;
525525

526526
// Set of our connected devices, keyed by identity.
527527
std::map<std::string, MTRBaseDevice *> mConnectedDevices;
528528
};
529529

530530
NS_ASSUME_NONNULL_BEGIN
531531

532-
@implementation TestPairingDelegate
533-
- (void)onStatusUpdate:(MTRPairingStatus)status
532+
@implementation TestDeviceControllerDelegate
533+
- (void)onStatusUpdate:(MTRCommissioningStatus)status
534534
{
535535
if (_active) {
536-
if (status == MTRPairingStatusSuccess) {
536+
if (status == MTRCommissioningStatusSuccess) {
537537
NSLog(@"Secure pairing success");
538-
} else if (status == MTRPairingStatusFailed) {
538+
} else if (status == MTRCommissioningStatusFailed) {
539539
_active = NO;
540540
NSLog(@"Secure pairing failed");
541541
_commandBridge->OnStatusUpdate(chip::app::StatusIB(chip::Protocols::InteractionModel::Status::Failure));

‎src/darwin/Framework/CHIP/MTRDeviceController.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ typedef void (^MTRDeviceConnectionCallback)(MTRBaseDevice * _Nullable device, NS
2929
@class MTRCommissioningParameters;
3030
@class MTRSetupPayload;
3131
@protocol MTRDevicePairingDelegate;
32+
@protocol MTRDeviceControllerDelegate;
3233

3334
@interface MTRDeviceController : NSObject
3435

@@ -114,7 +115,7 @@ typedef void (^MTRDeviceConnectionCallback)(MTRBaseDevice * _Nullable device, NS
114115
*
115116
* @param[in] queue The queue on which the callbacks will be delivered
116117
*/
117-
- (void)setPairingDelegate:(id<MTRDevicePairingDelegate>)delegate queue:(dispatch_queue_t)queue;
118+
- (void)setDeviceControllerDelegate:(id<MTRDeviceControllerDelegate>)delegate queue:(dispatch_queue_t)queue MTR_NEWLY_AVAILABLE;
118119

119120
/**
120121
* Sets this MTRDeviceController to use the given issuer for issuing operational certs. By default, the MTRDeviceController uses an
@@ -217,6 +218,9 @@ typedef void (^MTRDeviceConnectionCallback)(MTRBaseDevice * _Nullable device, NS
217218
salt:(NSData *)salt
218219
MTR_NEWLY_DEPRECATED("Please use computePASEVerifierForSetupPasscode:iterations:salt:error:");
219220

221+
- (void)setPairingDelegate:(id<MTRDevicePairingDelegate>)delegate
222+
queue:(dispatch_queue_t)queue MTR_NEWLY_DEPRECATED("Please use setDeviceControllerDelegate:");
223+
220224
@end
221225

222226
NS_ASSUME_NONNULL_END

‎src/darwin/Framework/CHIP/MTRDeviceController.mm

+80-10
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020

2121
#import "MTRBaseDevice_Internal.h"
2222
#import "MTRCommissioningParameters.h"
23+
#import "MTRDeviceControllerDelegateBridge.h"
2324
#import "MTRDeviceControllerFactory_Internal.h"
2425
#import "MTRDeviceControllerStartupParams.h"
2526
#import "MTRDeviceControllerStartupParams_Internal.h"
26-
#import "MTRDevicePairingDelegateBridge.h"
2727
#import "MTRDevice_Internal.h"
2828
#import "MTRError_Internal.h"
2929
#import "MTRKeypair.h"
@@ -84,7 +84,7 @@ @interface MTRDeviceController ()
8484

8585
@property (readonly) chip::Controller::DeviceCommissioner * cppCommissioner;
8686
@property (readonly) chip::Credentials::PartialDACVerifier * partialDACVerifier;
87-
@property (readonly) MTRDevicePairingDelegateBridge * pairingDelegateBridge;
87+
@property (readonly) MTRDeviceControllerDelegateBridge * deviceControllerDelegateBridge;
8888
@property (readonly) MTROperationalCredentialsDelegate * operationalCredentialsDelegate;
8989
@property (readonly) MTRP256KeypairBridge signingKeypairBridge;
9090
@property (readonly) MTRP256KeypairBridge operationalKeypairBridge;
@@ -104,8 +104,8 @@ - (instancetype)initWithFactory:(MTRDeviceControllerFactory *)factory queue:(dis
104104
_deviceMapLock = OS_UNFAIR_LOCK_INIT;
105105
_nodeIDToDeviceMap = [NSMutableDictionary dictionary];
106106

107-
_pairingDelegateBridge = new MTRDevicePairingDelegateBridge();
108-
if ([self checkForInitError:(_pairingDelegateBridge != nullptr) logMsg:kErrorPairingInit]) {
107+
_deviceControllerDelegateBridge = new MTRDeviceControllerDelegateBridge();
108+
if ([self checkForInitError:(_deviceControllerDelegateBridge != nullptr) logMsg:kErrorPairingInit]) {
109109
return nil;
110110
}
111111

@@ -184,9 +184,9 @@ - (void)cleanup
184184
_partialDACVerifier = nullptr;
185185
}
186186

187-
if (_pairingDelegateBridge) {
188-
delete _pairingDelegateBridge;
189-
_pairingDelegateBridge = nullptr;
187+
if (_deviceControllerDelegateBridge) {
188+
delete _deviceControllerDelegateBridge;
189+
_deviceControllerDelegateBridge = nullptr;
190190
}
191191
}
192192

@@ -256,7 +256,7 @@ - (BOOL)startup:(MTRDeviceControllerStartupParamsInternal *)startupParams
256256

257257
chip::Controller::SetupParams commissionerParams;
258258

259-
commissionerParams.pairingDelegate = _pairingDelegateBridge;
259+
commissionerParams.pairingDelegate = _deviceControllerDelegateBridge;
260260

261261
_operationalCredentialsDelegate->SetDeviceCommissioner(_cppCommissioner);
262262

@@ -535,14 +535,14 @@ - (void)removeDevice:(MTRDevice *)device
535535
os_unfair_lock_unlock(&_deviceMapLock);
536536
}
537537

538-
- (void)setPairingDelegate:(id<MTRDevicePairingDelegate>)delegate queue:(dispatch_queue_t)queue
538+
- (void)setDeviceControllerDelegate:(id<MTRDeviceControllerDelegate>)delegate queue:(dispatch_queue_t)queue
539539
{
540540
VerifyOrReturn([self checkIsRunning]);
541541

542542
dispatch_async(_chipWorkQueue, ^{
543543
VerifyOrReturn([self checkIsRunning]);
544544

545-
self->_pairingDelegateBridge->setDelegate(delegate, queue);
545+
self->_deviceControllerDelegateBridge->setDelegate(delegate, queue);
546546
});
547547
}
548548

@@ -823,6 +823,70 @@ - (void)invalidateCASESessionForNode:(chip::NodeId)nodeID;
823823
}
824824
@end
825825

826+
/**
827+
* Shim to allow us to treat an MTRDevicePairingDelegate as an
828+
* MTRDeviceControllerDelegate.
829+
*/
830+
@interface MTRDevicePairingDelegateShim : NSObject <MTRDeviceControllerDelegate>
831+
@property (nonatomic, readonly) id<MTRDevicePairingDelegate> delegate;
832+
- (instancetype)initWithDelegate:(id<MTRDevicePairingDelegate>)delegate;
833+
@end
834+
835+
@implementation MTRDevicePairingDelegateShim
836+
- (instancetype)initWithDelegate:(id<MTRDevicePairingDelegate>)delegate
837+
{
838+
if (self = [super init]) {
839+
_delegate = delegate;
840+
}
841+
return self;
842+
}
843+
844+
- (BOOL)respondsToSelector:(SEL)selector
845+
{
846+
// This logic will need to change a bit when the MTRDeviceControllerDelegate
847+
// signatures change. It's shaped the way it is to make those changes
848+
// easier.
849+
if (selector == @selector(onStatusUpdate:)) {
850+
return [self.delegate respondsToSelector:@selector(onStatusUpdate:)];
851+
}
852+
853+
if (selector == @selector(onPairingComplete:)) {
854+
return [self.delegate respondsToSelector:@selector(onPairingComplete:)];
855+
}
856+
857+
if (selector == @selector(onCommissioningComplete:)) {
858+
return [self.delegate respondsToSelector:@selector(onCommissioningComplete:)];
859+
}
860+
861+
if (selector == @selector(onPairingDeleted:)) {
862+
return [self.delegate respondsToSelector:@selector(onPairingDeleted:)];
863+
}
864+
865+
return [super respondsToSelector:selector];
866+
}
867+
868+
- (void)onStatusUpdate:(MTRCommissioningStatus)status
869+
{
870+
[self.delegate onStatusUpdate:static_cast<MTRPairingStatus>(status)];
871+
}
872+
873+
- (void)onPairingComplete:(NSError * _Nullable)error
874+
{
875+
[self.delegate onPairingComplete:error];
876+
}
877+
878+
- (void)onCommissioningComplete:(NSError * _Nullable)error
879+
{
880+
[self.delegate onCommissioningComplete:error];
881+
}
882+
883+
- (void)onPairingDeleted:(NSError * _Nullable)error
884+
{
885+
[self.delegate onPairingDeleted:error];
886+
}
887+
888+
@end
889+
826890
@implementation MTRDeviceController (Deprecated)
827891

828892
- (NSNumber *)controllerNodeId
@@ -1040,4 +1104,10 @@ - (nullable NSData *)computePaseVerifier:(uint32_t)setupPincode iterations:(uint
10401104
return [MTRDeviceController computePASEVerifierForSetupPasscode:@(setupPincode) iterations:@(iterations) salt:salt error:nil];
10411105
}
10421106

1107+
- (void)setPairingDelegate:(id<MTRDevicePairingDelegate>)delegate queue:(dispatch_queue_t)queue
1108+
{
1109+
auto * delegateShim = [[MTRDevicePairingDelegateShim alloc] initWithDelegate:delegate];
1110+
[self setDeviceControllerDelegate:delegateShim queue:queue];
1111+
}
1112+
10431113
@end

0 commit comments

Comments
 (0)
Please sign in to comment.