Skip to content

Commit

Permalink
[Darwin] add commissioning callback (read Commissioning Info, commiss…
Browse files Browse the repository at this point in the history
…ioningComplete add deviceId) (#25832)

* Implement commissioning event callback api

* restyle

* Add comment, fix typo

* Modify from comment

* Update after review

* restyle

* Update src/darwin/Framework/CHIP/MTRDeviceControllerDelegate.h

Co-authored-by: Boris Zbarsky <[email protected]>

* Update src/darwin/Framework/CHIP/MTRDeviceControllerDelegate.h

Co-authored-by: Boris Zbarsky <[email protected]>

* Update src/darwin/Framework/CHIP/MTRDeviceControllerDelegate.h

Co-authored-by: Boris Zbarsky <[email protected]>

* Change Variable name(nodeID) remove ununsed code

* Modify readCommissioningInfo to interface

* restyle

* restyle-2

* Modify nodeID error check

* restyle

* Update src/darwin/Framework/CHIP/MTRDeviceControllerDelegate.h

Co-authored-by: Boris Zbarsky <[email protected]>

* Update src/darwin/Framework/CHIP/MTRDeviceControllerDelegate.h

Co-authored-by: Boris Zbarsky <[email protected]>

* Update src/darwin/Framework/CHIP/MTRDeviceControllerDelegateBridge.mm

Co-authored-by: Boris Zbarsky <[email protected]>

* Update src/darwin/Framework/CHIP/MTRDeviceControllerDelegate.h

Co-authored-by: Boris Zbarsky <[email protected]>

* Update src/darwin/Framework/CHIP/MTRDeviceControllerDelegateBridge.mm

Co-authored-by: Boris Zbarsky <[email protected]>

* Update src/darwin/Framework/CHIP/MTRDeviceControllerDelegateBridge.mm

Co-authored-by: Boris Zbarsky <[email protected]>

* Update src/darwin/Framework/CHIP/MTRDeviceControllerDelegate.h

Co-authored-by: Boris Zbarsky <[email protected]>

* Update src/darwin/Framework/CHIP/MTRDeviceControllerDelegate.h

Co-authored-by: Boris Zbarsky <[email protected]>

* Update src/darwin/Framework/CHIP/MTRDeviceControllerDelegate.h

Co-authored-by: Boris Zbarsky <[email protected]>

* modify for changing variable name

* restyle

* restyle-2

* Apply suggestions from code review

* Fix deprecation message.

* Remove redundant availability annotations.

---------

Co-authored-by: Boris Zbarsky <[email protected]>
  • Loading branch information
2 people authored and pull[bot] committed Feb 16, 2024
1 parent 7356388 commit aacd0c8
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
#import <UIKit/UIKit.h>

@interface QRCodeViewController
: UIViewController <AVCaptureMetadataOutputObjectsDelegate, MTRDevicePairingDelegate, NFCNDEFReaderSessionDelegate>
: UIViewController <AVCaptureMetadataOutputObjectsDelegate, MTRDeviceControllerDelegate, NFCNDEFReaderSessionDelegate>

@end
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ - (void)viewDidLoad

dispatch_queue_t callbackQueue = dispatch_queue_create("com.csa.matter.qrcodevc.callback", DISPATCH_QUEUE_SERIAL);
self.chipController = InitializeMTR();
[self.chipController setPairingDelegate:self queue:callbackQueue];
[self.chipController setDeviceControllerDelegate:self queue:callbackQueue];

UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
Expand Down Expand Up @@ -478,8 +478,8 @@ - (void)setVendorIDOnAccessory
}
}

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

- (void)onCommissioningComplete:(NSError * _Nullable)error
// MARK: MTRDeviceControllerDelegate
- (void)controller:(MTRDeviceController *)controller
commissioningComplete:(NSError * _Nullable)error
nodeID:(NSNumber * _Nullable)nodeID
{
if (error != nil) {
NSLog(@"Error retrieving device informations over Mdns: %@", error);
return;
}
// track this device
uint64_t deviceId = MTRGetNextAvailableDeviceID() - 1;
MTRSetDevicePaired(deviceId, YES);
MTRSetDevicePaired([nodeID unsignedLongLongValue], YES);
[self setVendorIDOnAccessory];
}

// MARK: MTRDeviceControllerDelegate
- (void)controller:(MTRDeviceController *)controller readCommissioningInfo:(MTRProductIdentity *)info
{
NSLog(@"readCommissioningInfo, vendorID:%@, productID:%@", info.vendorID, info.productID);
}

- (void)updateUIFields:(MTRSetupPayload *)payload rawPayload:(nullable NSString *)rawPayload isManualCode:(BOOL)isManualCode
{
if (isManualCode) {
Expand Down Expand Up @@ -789,7 +797,7 @@ - (void)_restartMatterStack
{
self.chipController = MTRRestartController(self.chipController);
dispatch_queue_t callbackQueue = dispatch_queue_create("com.csa.matter.qrcodevc.callback", DISPATCH_QUEUE_SERIAL);
[self.chipController setPairingDelegate:self queue:callbackQueue];
[self.chipController setDeviceControllerDelegate:self queue:callbackQueue];
}

- (void)handleRendezVousDefault:(NSString *)payload
Expand Down
35 changes: 34 additions & 1 deletion src/darwin/Framework/CHIP/MTRDeviceControllerDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ typedef NS_ENUM(NSInteger, MTRCommissioningStatus) {
= 3,
} API_AVAILABLE(ios(16.4), macos(13.3), watchos(9.4), tvos(16.4));

/**
* A representation of a (vendor, product) pair that identifies a specific product.
*/
MTR_NEWLY_AVAILABLE
@interface MTRProductIdentity : NSObject

@property (nonatomic, copy, readonly) NSNumber * vendorID;

@property (nonatomic, copy, readonly) NSNumber * productID;

- (instancetype)initWithVendorID:(NSNumber *)vendorID productID:(NSNumber *)productID;
@end

@class MTRDeviceController;

/**
Expand All @@ -52,8 +65,28 @@ API_AVAILABLE(ios(16.4), macos(13.3), watchos(9.4), tvos(16.4))
/**
* Notify the delegate when commissioning is completed.
*/
- (void)controller:(MTRDeviceController *)controller commissioningComplete:(NSError * _Nullable)error;
- (void)controller:(MTRDeviceController *)controller
commissioningComplete:(NSError * _Nullable)error MTR_NEWLY_DEPRECATED("Please use controller:commissioningComplete:nodeID:");

/**
* Notify the delegate when commissioning is completed.
*
* Exactly one of error and nodeID will be nil.
*
* If nodeID is not nil, then it represents the node id the node was assigned, as encoded in its operational certificate.
*/
- (void)controller:(MTRDeviceController *)controller
commissioningComplete:(NSError * _Nullable)error
nodeID:(NSNumber * _Nullable)nodeID MTR_NEWLY_AVAILABLE;

/**
* Notify the delegate when commissioning infomation has been read from the Basic
* Information cluster of the commissionee.
*
* At the point when this notification happens, device attestation has not been performed yet,
* so the information delivered by this notification should not be trusted.
*/
- (void)controller:(MTRDeviceController *)controller readCommissioningInfo:(MTRProductIdentity *)info MTR_NEWLY_AVAILABLE;
@end

typedef NS_ENUM(NSUInteger, MTRPairingStatus) {
Expand Down
3 changes: 3 additions & 0 deletions src/darwin/Framework/CHIP/MTRDeviceControllerDelegateBridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#import "MTRDeviceControllerDelegate.h"

#include <controller/CHIPDeviceController.h>
#include <controller/CommissioningDelegate.h>
#include <platform/CHIPDeviceBuildConfig.h>

NS_ASSUME_NONNULL_BEGIN
Expand All @@ -37,6 +38,8 @@ class MTRDeviceControllerDelegateBridge : public chip::Controller::DevicePairing

void OnPairingDeleted(CHIP_ERROR error) override;

void OnReadCommissioningInfo(const chip::Controller::ReadCommissioningInfo & info) override;

void OnCommissioningComplete(chip::NodeId deviceId, CHIP_ERROR error) override;

private:
Expand Down
44 changes: 44 additions & 0 deletions src/darwin/Framework/CHIP/MTRDeviceControllerDelegateBridge.mm
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,44 @@
// This is never actually called; just do nothing.
}

void MTRDeviceControllerDelegateBridge::OnReadCommissioningInfo(const chip::Controller::ReadCommissioningInfo & info)
{
chip::VendorId vendorId = info.basic.vendorId;
uint16_t productId = info.basic.productId;

MTR_LOG_DEFAULT("DeviceControllerDelegate Read Commissioning Info. VendorId %u ProductId %u", vendorId, productId);

id<MTRDeviceControllerDelegate> strongDelegate = mDelegate;
MTRDeviceController * strongController = mController;
if (strongDelegate && mQueue && strongController) {
if ([strongDelegate respondsToSelector:@selector(controller:readCommissioningInfo:)]) {
dispatch_async(mQueue, ^{
auto * info = [[MTRProductIdentity alloc] initWithVendorID:@(vendorId) productID:@(productId)];
[strongDelegate controller:strongController readCommissioningInfo:info];
});
}
}
}

void MTRDeviceControllerDelegateBridge::OnCommissioningComplete(chip::NodeId nodeId, CHIP_ERROR error)
{
MTR_LOG_DEFAULT("DeviceControllerDelegate Commissioning complete. NodeId %llu Status %s", nodeId, chip::ErrorStr(error));

id<MTRDeviceControllerDelegate> strongDelegate = mDelegate;
MTRDeviceController * strongController = mController;
if (strongDelegate && mQueue && strongController) {
if ([strongDelegate respondsToSelector:@selector(controller:commissioningComplete:nodeID:)]) {
dispatch_async(mQueue, ^{
NSError * nsError = [MTRError errorForCHIPErrorCode:error];
NSNumber * nodeID = nil;
if (error == CHIP_NO_ERROR) {
nodeID = @(nodeId);
}
[strongDelegate controller:strongController commissioningComplete:nsError nodeID:nodeID];
});
return;
}
// If only the DEPRECATED function is defined
if ([strongDelegate respondsToSelector:@selector(controller:commissioningComplete:)]) {
dispatch_async(mQueue, ^{
NSError * nsError = [MTRError errorForCHIPErrorCode:error];
Expand All @@ -109,3 +140,16 @@
}
}
}

@implementation MTRProductIdentity

- (instancetype)initWithVendorID:(NSNumber *)vendorID productID:(NSNumber *)productID
{
if (self = [super init]) {
_vendorID = vendorID;
_productID = productID;
}
return self;
}

@end

0 comments on commit aacd0c8

Please sign in to comment.