Skip to content

Commit

Permalink
[Darwin] Fix Matter framework delegates to pass the delegating object…
Browse files Browse the repository at this point in the history
… as the first arg to their methods (#22682)
  • Loading branch information
jtung-apple authored and pull[bot] committed Feb 13, 2024
1 parent 0453d1d commit 1173711
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@

#import <Matter/Matter.h>

@class MTRDeviceController;

@interface CHIPToolDeviceControllerDelegate : NSObject <MTRDeviceControllerDelegate>
@property PairingCommandBridge * commandBridge;
@property chip::NodeId deviceID;
@property MTRDeviceController * commissioner;
@property MTRCommissioningParameters * params;

- (void)onCommissioningSessionEstablishmentDone:(NSError *)error;
- (void)onCommissioningComplete:(NSError *)error;
- (void)controller:(MTRDeviceController *)controller commissioningSessionEstablishmentDone:(NSError *)error;
- (void)controller:(MTRDeviceController *)controller commissioningComplete:(NSError *)error;

@end
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ - (void)onStatusUpdate:(MTRCommissioningStatus)status
}
}

- (void)onCommissioningSessionEstablishmentDone:(NSError *)error
- (void)controller:(MTRDeviceController *)controller commissioningSessionEstablishmentDone:(NSError *)error
{
if (error != nil) {
ChipLogProgress(chipTool, "PASE establishment failed");
Expand All @@ -61,7 +61,7 @@ - (void)onCommissioningSessionEstablishmentDone:(NSError *)error
}
}

- (void)onCommissioningComplete:(NSError *)error
- (void)controller:(MTRDeviceController *)controller commissioningComplete:(NSError *)error
{
_commandBridge->SetCommandExitStatus(error, "Pairing Commissioning Complete");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ - (void)setVendorIDOnAccessory
}

// MARK: MTRDeviceControllerDelegate
- (void)onCommissioningSessionEstablishmentDone:(NSError * _Nullable)error
- (void)controller:(MTRDeviceController *)controller commissioningSessionEstablishmentDone:(NSError * _Nullable)error
{
if (error != nil) {
NSLog(@"Got pairing error back %@", error);
Expand Down Expand Up @@ -683,7 +683,7 @@ - (void)commissionWithSSID:(NSString *)ssid password:(NSString *)password
}
}

- (void)onCommissioningComplete:(NSError * _Nullable)error
- (void)controller:(MTRDeviceController *)controller commissioningComplete:(NSError * _Nullable)error
{
if (error != nil) {
NSLog(@"Error retrieving device informations over Mdns: %@", error);
Expand Down Expand Up @@ -1105,7 +1105,7 @@ - (instancetype)initWithViewController:(QRCodeViewController *)viewController
return self;
}

- (void)deviceAttestation:(MTRDeviceController *)controller failedForDevice:(void *)device error:(NSError * _Nonnull)error
- (void)deviceAttestationFailedForController:(MTRDeviceController *)controller device:(void *)device error:(NSError * _Nonnull)error
{
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertController * alertController = [UIAlertController
Expand Down
16 changes: 9 additions & 7 deletions src/darwin/Framework/CHIP/MTRDeviceAttestationDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ NS_ASSUME_NONNULL_BEGIN
/**
* Only one of the following delegate callbacks should be implemented.
*
* If -deviceAttestation:failedForDevice:error: is implemented, then it will be called when device
* If -deviceAttestationFailedForController:device:error: is implemented, then it will be called when device
* attestation fails, and the client can decide to continue or stop the commissioning.
*
* If -deviceAttestation:completedForDevice:attestationDeviceInfo:error: is implemented, then it
* If -deviceAttestationCompletedForController:device:attestationDeviceInfo:error: is implemented, then it
* will always be called when device attestation completes.
*/

Expand All @@ -58,10 +58,10 @@ NS_ASSUME_NONNULL_BEGIN
* @param attestationDeviceInfo Attestation information for the device
* @param error NSError representing the error code on attestation failure. Nil if success.
*/
- (void)deviceAttestation:(MTRDeviceController *)controller
completedForDevice:(void *)device
attestationDeviceInfo:(MTRDeviceAttestationDeviceInfo *)attestationDeviceInfo
error:(NSError * _Nullable)error;
- (void)deviceAttestationCompletedForController:(MTRDeviceController *)controller
device:(void *)device
attestationDeviceInfo:(MTRDeviceAttestationDeviceInfo *)attestationDeviceInfo
error:(NSError * _Nullable)error;

/**
* Notify the delegate when device attestation fails
Expand All @@ -70,7 +70,9 @@ NS_ASSUME_NONNULL_BEGIN
* @param device Handle of device being commissioned
* @param error NSError representing the error code for the failure
*/
- (void)deviceAttestation:(MTRDeviceController *)controller failedForDevice:(void *)device error:(NSError * _Nonnull)error;
- (void)deviceAttestationFailedForController:(MTRDeviceController *)controller
device:(void *)device
error:(NSError * _Nonnull)error;

@end

Expand Down
15 changes: 8 additions & 7 deletions src/darwin/Framework/CHIP/MTRDeviceAttestationDelegateBridge.mm
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
mResult = attestationResult;

id<MTRDeviceAttestationDelegate> strongDelegate = mDeviceAttestationDelegate;
if ([strongDelegate respondsToSelector:@selector(deviceAttestation:completedForDevice:attestationDeviceInfo:error:)]) {
if ([strongDelegate respondsToSelector:@selector(deviceAttestationCompletedForController:
device:attestationDeviceInfo:error:)]) {
MTRDeviceController * strongController = mDeviceController;
if (strongController) {
NSData * dacData = AsData(info.dacDerBuffer());
Expand All @@ -43,18 +44,18 @@
NSError * error = (attestationResult == chip::Credentials::AttestationVerificationResult::kSuccess)
? nil
: [MTRError errorForCHIPErrorCode:CHIP_ERROR_INTEGRITY_CHECK_FAILED];
[strongDelegate deviceAttestation:mDeviceController
completedForDevice:device
attestationDeviceInfo:deviceInfo
error:error];
[strongDelegate deviceAttestationCompletedForController:mDeviceController
device:device
attestationDeviceInfo:deviceInfo
error:error];
}
} else if ((attestationResult != chip::Credentials::AttestationVerificationResult::kSuccess) &&
[strongDelegate respondsToSelector:@selector(deviceAttestation:failedForDevice:error:)]) {
[strongDelegate respondsToSelector:@selector(deviceAttestationFailedForController:device:error:)]) {

MTRDeviceController * strongController = mDeviceController;
if (strongController) {
NSError * error = [MTRError errorForCHIPErrorCode:CHIP_ERROR_INTEGRITY_CHECK_FAILED];
[strongDelegate deviceAttestation:mDeviceController failedForDevice:device error:error];
[strongDelegate deviceAttestationFailedForController:mDeviceController device:device error:error];
}
}
});
Expand Down
4 changes: 2 additions & 2 deletions src/darwin/Framework/CHIP/MTRDeviceController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ - (BOOL)commissionNodeWithID:(NSNumber *)nodeID
}
BOOL shouldWaitAfterDeviceAttestation = NO;
if ([commissioningParams.deviceAttestationDelegate
respondsToSelector:@selector(deviceAttestation:completedForDevice:attestationDeviceInfo:error:)]) {
respondsToSelector:@selector(deviceAttestationCompletedForController:device:attestationDeviceInfo:error:)]) {
shouldWaitAfterDeviceAttestation = YES;
}
_deviceAttestationDelegateBridge = new MTRDeviceAttestationDelegateBridge(
Expand Down Expand Up @@ -536,7 +536,7 @@ - (void)removeDevice:(MTRDevice *)device
- (void)setDeviceControllerDelegate:(id<MTRDeviceControllerDelegate>)delegate queue:(dispatch_queue_t)queue
{
dispatch_async(_chipWorkQueue, ^{
self->_deviceControllerDelegateBridge->setDelegate(delegate, queue);
self->_deviceControllerDelegateBridge->setDelegate(self, delegate, queue);
});
}

Expand Down
8 changes: 5 additions & 3 deletions src/darwin/Framework/CHIP/MTRDeviceControllerDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ typedef NS_ENUM(NSUInteger, MTRCommissioningStatus) {
MTRCommissioningStatusDiscoveringMoreDevices = 3
};

@class MTRDeviceController;

/**
* The protocol definition for the MTRDeviceControllerDelegate.
*
Expand All @@ -36,18 +38,18 @@ typedef NS_ENUM(NSUInteger, MTRCommissioningStatus) {
/**
* Notify the delegate when commissioning status gets updated.
*/
- (void)onStatusUpdate:(MTRCommissioningStatus)status;
- (void)controller:(MTRDeviceController *)controller statusUpdate:(MTRCommissioningStatus)status;

/**
* Notify the delegate when a commissioning session is established or the
* establishment has errored out.
*/
- (void)onCommissioningSessionEstablishmentDone:(NSError * _Nullable)error;
- (void)controller:(MTRDeviceController *)controller commissioningSessionEstablishmentDone:(NSError * _Nullable)error;

/**
* Notify the delegate when commissioning is completed.
*/
- (void)onCommissioningComplete:(NSError * _Nullable)error;
- (void)controller:(MTRDeviceController *)controller commissioningComplete:(NSError * _Nullable)error;

@end

Expand Down
8 changes: 5 additions & 3 deletions src/darwin/Framework/CHIP/MTRDeviceControllerDelegateBridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@

NS_ASSUME_NONNULL_BEGIN

class MTRDeviceControllerDelegateBridge : public chip::Controller::DevicePairingDelegate
{
@class MTRDeviceController;

class MTRDeviceControllerDelegateBridge : public chip::Controller::DevicePairingDelegate {
public:
MTRDeviceControllerDelegateBridge();
~MTRDeviceControllerDelegateBridge();

void setDelegate(id<MTRDeviceControllerDelegate> delegate, dispatch_queue_t queue);
void setDelegate(MTRDeviceController * controller, id<MTRDeviceControllerDelegate> delegate, dispatch_queue_t queue);

void OnStatusUpdate(chip::Controller::DevicePairingDelegate::Status status) override;

Expand All @@ -39,6 +40,7 @@ class MTRDeviceControllerDelegateBridge : public chip::Controller::DevicePairing
void OnCommissioningComplete(chip::NodeId deviceId, CHIP_ERROR error) override;

private:
MTRDeviceController * _Nullable mController;
_Nullable id<MTRDeviceControllerDelegate> mDelegate;
_Nullable dispatch_queue_t mQueue;

Expand Down
18 changes: 11 additions & 7 deletions src/darwin/Framework/CHIP/MTRDeviceControllerDelegateBridge.mm
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/

#import "MTRDeviceControllerDelegateBridge.h"
#import "MTRDeviceController.h"
#import "MTRError_Internal.h"

MTRDeviceControllerDelegateBridge::MTRDeviceControllerDelegateBridge(void)
Expand All @@ -25,12 +26,15 @@

MTRDeviceControllerDelegateBridge::~MTRDeviceControllerDelegateBridge(void) {}

void MTRDeviceControllerDelegateBridge::setDelegate(id<MTRDeviceControllerDelegate> delegate, dispatch_queue_t queue)
void MTRDeviceControllerDelegateBridge::setDelegate(
MTRDeviceController * controller, id<MTRDeviceControllerDelegate> delegate, dispatch_queue_t queue)
{
if (delegate && queue) {
mController = controller;
mDelegate = delegate;
mQueue = queue;
} else {
mController = nil;
mDelegate = nil;
mQueue = nil;
}
Expand Down Expand Up @@ -58,11 +62,11 @@
NSLog(@"DeviceControllerDelegate status updated: %d", status);

id<MTRDeviceControllerDelegate> strongDelegate = mDelegate;
if ([strongDelegate respondsToSelector:@selector(onStatusUpdate:)]) {
if ([strongDelegate respondsToSelector:@selector(controller:statusUpdate:)]) {
if (strongDelegate && mQueue) {
MTRCommissioningStatus commissioningStatus = MapStatus(status);
dispatch_async(mQueue, ^{
[strongDelegate onStatusUpdate:commissioningStatus];
[strongDelegate controller:mController statusUpdate:commissioningStatus];
});
}
}
Expand All @@ -73,11 +77,11 @@
NSLog(@"DeviceControllerDelegate Pairing complete. Status %s", chip::ErrorStr(error));

id<MTRDeviceControllerDelegate> strongDelegate = mDelegate;
if ([strongDelegate respondsToSelector:@selector(onCommissioningSessionEstablishmentDone:)]) {
if ([strongDelegate respondsToSelector:@selector(controller:commissioningSessionEstablishmentDone:)]) {
if (strongDelegate && mQueue) {
dispatch_async(mQueue, ^{
NSError * nsError = [MTRError errorForCHIPErrorCode:error];
[strongDelegate onCommissioningSessionEstablishmentDone:nsError];
[strongDelegate controller:mController commissioningSessionEstablishmentDone:nsError];
});
}
}
Expand All @@ -95,11 +99,11 @@
NSLog(@"DeviceControllerDelegate Commissioning complete. NodeId %llu Status %s", nodeId, chip::ErrorStr(error));

id<MTRDeviceControllerDelegate> strongDelegate = mDelegate;
if ([strongDelegate respondsToSelector:@selector(onCommissioningComplete:)]) {
if ([strongDelegate respondsToSelector:@selector(controller:commissioningComplete:)]) {
if (strongDelegate && mQueue) {
dispatch_async(mQueue, ^{
NSError * nsError = [MTRError errorForCHIPErrorCode:error];
[strongDelegate onCommissioningComplete:nsError];
[strongDelegate controller:mController commissioningComplete:nsError];
});
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/darwin/Framework/CHIPTests/MTRDeviceTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ - (id)initWithExpectation:(XCTestExpectation *)expectation
return self;
}

- (void)onCommissioningSessionEstablishmentDone:(NSError *)error
- (void)controller:(MTRDeviceController *)controller commissioningSessionEstablishmentDone:(NSError *)error
{
XCTAssertEqual(error.code, 0);

Expand All @@ -101,10 +101,10 @@ - (void)onCommissioningSessionEstablishmentDone:(NSError *)error
error:&commissionError];
XCTAssertNil(commissionError);

// Keep waiting for onCommissioningComplete
// Keep waiting for controller:MTRXPCListenerSampleTests.mcommissioningComplete
}

- (void)onCommissioningComplete:(NSError *)error
- (void)controller:(MTRDeviceController *)controller commissioningComplete:(NSError *)error
{
XCTAssertEqual(error.code, 0);
[_expectation fulfill];
Expand Down
6 changes: 3 additions & 3 deletions src/darwin/Framework/CHIPTests/MTRXPCListenerSampleTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ - (id)initWithExpectation:(XCTestExpectation *)expectation
return self;
}

- (void)onCommissioningSessionEstablishmentDone:(NSError *)error
- (void)controller:(MTRDeviceController *)controller commissioningSessionEstablishmentDone:(NSError *)error
{
XCTAssertEqual(error.code, 0);
NSError * commissionError = nil;
Expand All @@ -410,10 +410,10 @@ - (void)onCommissioningSessionEstablishmentDone:(NSError *)error
error:&commissionError];
XCTAssertNil(commissionError);

// Keep waiting for onCommissioningComplete
// Keep waiting for controller:commissioningComplete
}

- (void)onCommissioningComplete:(NSError *)error
- (void)controller:(MTRDeviceController *)controller commissioningComplete:(NSError *)error
{
XCTAssertEqual(error.code, 0);
[_expectation fulfill];
Expand Down

0 comments on commit 1173711

Please sign in to comment.