From 377448434b5074158dc0e54add94a77715654d6c Mon Sep 17 00:00:00 2001 From: Karsten Sperling <113487422+ksperling-apple@users.noreply.github.com> Date: Thu, 15 Dec 2022 11:26:53 +1300 Subject: [PATCH] Darwin: Expose MTRBaseDevice.sessionTransportType (#24065) * Expose MTRBaseDevice.sessionTransportType. - Use it in iOS CHIPTool - Remove _deviceBeingCommissionedOverBLE * Format / address comments --- .../QRCode/QRCodeViewController.m | 15 +------ src/darwin/Framework/CHIP/MTRBaseDevice.h | 13 ++++++ src/darwin/Framework/CHIP/MTRBaseDevice.mm | 5 +++ .../Framework/CHIP/MTRBaseDevice_Internal.h | 12 +++-- .../Framework/CHIP/MTRDeviceController.mm | 44 +++++++++++-------- .../CHIP/MTRDeviceController_Internal.h | 8 +++- 6 files changed, 62 insertions(+), 35 deletions(-) diff --git a/src/darwin/CHIPTool/CHIPTool/View Controllers/QRCode/QRCodeViewController.m b/src/darwin/CHIPTool/CHIPTool/View Controllers/QRCode/QRCodeViewController.m index e31e52b6bff618..2105bec3b20545 100644 --- a/src/darwin/CHIPTool/CHIPTool/View Controllers/QRCode/QRCodeViewController.m +++ b/src/darwin/CHIPTool/CHIPTool/View Controllers/QRCode/QRCodeViewController.m @@ -43,17 +43,6 @@ #define NOT_APPLICABLE_STRING @"N/A" -@interface MTRDeviceController (ToDoRemove) - -/** - * TODO: Temporary until PairingDelegate is fixed to clearly communicate this - * information to consumers. - * This should be migrated over to the proper pairing delegate path - */ -- (BOOL)_deviceBeingCommissionedOverBLE:(uint64_t)deviceId; - -@end - @interface QRCodeViewController () @property (nonatomic, strong) AVCaptureSession * captureSession; @@ -497,8 +486,8 @@ - (void)onPairingComplete:(NSError * _Nullable)error } else { MTRDeviceController * controller = InitializeMTR(); uint64_t deviceId = MTRGetLastPairedDeviceId(); - if ([controller respondsToSelector:@selector(_deviceBeingCommissionedOverBLE:)] && - [controller _deviceBeingCommissionedOverBLE:deviceId]) { + MTRBaseDevice * device = [controller deviceBeingCommissionedWithNodeID:@(deviceId) error:NULL]; + if (device.sessionTransportType == MTRTransportTypeBLE) { dispatch_async(dispatch_get_main_queue(), ^{ [self->_deviceList refreshDeviceList]; [self retrieveAndSendWiFiCredentials]; diff --git a/src/darwin/Framework/CHIP/MTRBaseDevice.h b/src/darwin/Framework/CHIP/MTRBaseDevice.h index f09b2564318041..c93a7e48b002d4 100644 --- a/src/darwin/Framework/CHIP/MTRBaseDevice.h +++ b/src/darwin/Framework/CHIP/MTRBaseDevice.h @@ -122,6 +122,13 @@ extern NSString * const MTRArrayValueType; @class MTRReadParams; @class MTRSubscribeParams; +typedef NS_ENUM(uint8_t, MTRTransportType) { + MTRTransportTypeUndefined = 0, + MTRTransportTypeUDP, + MTRTransportTypeBLE, + MTRTransportTypeTCP, +} MTR_NEWLY_AVAILABLE; + @interface MTRBaseDevice : NSObject - (instancetype)init NS_UNAVAILABLE; @@ -135,6 +142,12 @@ extern NSString * const MTRArrayValueType; */ + (instancetype)deviceWithNodeID:(NSNumber *)nodeID controller:(MTRDeviceController *)controller MTR_NEWLY_AVAILABLE; +/** + * The transport used by the current session with this device, or + * `MTRTransportTypeUndefined` if no session is currently active. + */ +@property (readonly) MTRTransportType sessionTransportType MTR_NEWLY_AVAILABLE; + /** * Subscribe to receive attribute reports for everything (all endpoints, all * clusters, all attributes, all events) on the device. diff --git a/src/darwin/Framework/CHIP/MTRBaseDevice.mm b/src/darwin/Framework/CHIP/MTRBaseDevice.mm index c21eeb498558c3..7725c9ab6e1f1b 100644 --- a/src/darwin/Framework/CHIP/MTRBaseDevice.mm +++ b/src/darwin/Framework/CHIP/MTRBaseDevice.mm @@ -268,6 +268,11 @@ + (instancetype)deviceWithNodeID:(NSNumber *)nodeID controller:(MTRDeviceControl return [controller baseDeviceForNodeID:nodeID]; } +- (MTRTransportType)sessionTransportType +{ + return [self.deviceController sessionTransportTypeForDevice:self]; +} + - (void)invalidateCASESession { if (self.isPASEDevice) { diff --git a/src/darwin/Framework/CHIP/MTRBaseDevice_Internal.h b/src/darwin/Framework/CHIP/MTRBaseDevice_Internal.h index c4527c8a3e5edc..9e943d2327b690 100644 --- a/src/darwin/Framework/CHIP/MTRBaseDevice_Internal.h +++ b/src/darwin/Framework/CHIP/MTRBaseDevice_Internal.h @@ -27,6 +27,15 @@ NS_ASSUME_NONNULL_BEGIN +static inline MTRTransportType MTRMakeTransportType(chip::Transport::Type type) +{ + static_assert(MTRTransportTypeUndefined == (uint8_t) chip::Transport::Type::kUndefined, "MTRTransportType != Transport::Type"); + static_assert(MTRTransportTypeUDP == (uint8_t) chip::Transport::Type::kUdp, "MTRTransportType != Transport::Type"); + static_assert(MTRTransportTypeBLE == (uint8_t) chip::Transport::Type::kBle, "MTRTransportType != Transport::Type"); + static_assert(MTRTransportTypeTCP == (uint8_t) chip::Transport::Type::kTcp, "MTRTransportType != Transport::Type"); + return static_cast(type); +} + @interface MTRBaseDevice () - (instancetype)initWithPASEDevice:(chip::DeviceProxy *)device controller:(MTRDeviceController *)controller; @@ -55,9 +64,6 @@ NS_ASSUME_NONNULL_BEGIN */ @property (nonatomic, assign, readonly) chip::NodeId nodeID; -- (instancetype)init NS_UNAVAILABLE; -+ (instancetype)new NS_UNAVAILABLE; - /** * Initialize the device object as a CASE device with the given node id and * controller. This will always succeed, even if there is no such node id on diff --git a/src/darwin/Framework/CHIP/MTRDeviceController.mm b/src/darwin/Framework/CHIP/MTRDeviceController.mm index 0fa42fe57eb5ef..8474bbc517fd47 100644 --- a/src/darwin/Framework/CHIP/MTRDeviceController.mm +++ b/src/darwin/Framework/CHIP/MTRDeviceController.mm @@ -14,9 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#import - -#import "MTRDeviceController.h" +#import "MTRDeviceController_Internal.h" #import "MTRBaseDevice_Internal.h" #import "MTRCommissioningParameters.h" @@ -54,6 +52,8 @@ #include #include +#import + static NSString * const kErrorCommissionerInit = @"Init failure while initializing a commissioner"; static NSString * const kErrorIPKInit = @"Init failure while initializing IPK"; static NSString * const kErrorSigningKeypairInit = @"Init failure while creating signing keypair bridge"; @@ -678,17 +678,6 @@ - (BOOL)checkIsRunning:(NSError * __autoreleasing *)error return NO; } -- (BOOL)_deviceBeingCommissionedOverBLE:(uint64_t)deviceID -{ - VerifyOrReturnValue([self checkIsRunning], NO); - - chip::CommissioneeDeviceProxy * deviceProxy; - auto errorCode = self->_cppCommissioner->GetDeviceBeingCommissioned(deviceID, &deviceProxy); - VerifyOrReturnValue(errorCode == CHIP_NO_ERROR, NO); - - return deviceProxy->GetDeviceTransportType() == chip::Transport::Type::kBle; -} - - (void)getSessionForNode:(chip::NodeId)nodeID completion:(MTRInternalDeviceConnectionCallback)completion { [self @@ -728,6 +717,28 @@ - (void)getSessionForCommissioneeDevice:(chip::NodeId)deviceID completion:(MTRIn }]; } +- (MTRTransportType)sessionTransportTypeForDevice:(MTRBaseDevice *)device +{ + VerifyOrReturnValue([self checkIsRunning], MTRTransportTypeUndefined); + + __block MTRTransportType result = MTRTransportTypeUndefined; + dispatch_sync(_chipWorkQueue, ^{ + VerifyOrReturn([self checkIsRunning]); + + if (device.isPASEDevice) { + chip::CommissioneeDeviceProxy * deviceProxy; + VerifyOrReturn(CHIP_NO_ERROR == self->_cppCommissioner->GetDeviceBeingCommissioned(device.nodeID, &deviceProxy)); + result = MTRMakeTransportType(deviceProxy->GetDeviceTransportType()); + } else { + auto scopedNodeID = self->_cppCommissioner->GetPeerScopedId(device.nodeID); + auto sessionHandle = self->_cppCommissioner->SessionMgr()->FindSecureSessionForNode(scopedNodeID); + VerifyOrReturn(sessionHandle.HasValue()); + result = MTRMakeTransportType(sessionHandle.Value()->AsSecureSession()->GetPeerAddress().GetTransportType()); + } + }); + return result; +} + - (void)asyncGetCommissionerOnMatterQueue:(void (^)(chip::Controller::DeviceCommissioner *))block errorHandler:(nullable MTRDeviceErrorHandler)errorHandler { @@ -795,10 +806,6 @@ - (BOOL)syncRunOnWorkQueueWithBoolReturnValue:(SyncWorkQueueBlockWithBoolReturnV return success; } -@end - -@implementation MTRDeviceController (InternalMethods) - - (chip::FabricIndex)fabricIndex { if (!_cppCommissioner) { @@ -848,6 +855,7 @@ - (void)invalidateCASESessionForNode:(chip::NodeId)nodeID; [self syncRunOnWorkQueue:block error:nil]; } + @end /** diff --git a/src/darwin/Framework/CHIP/MTRDeviceController_Internal.h b/src/darwin/Framework/CHIP/MTRDeviceController_Internal.h index 08a31c45c0ba7f..e3e795ac15bb46 100644 --- a/src/darwin/Framework/CHIP/MTRDeviceController_Internal.h +++ b/src/darwin/Framework/CHIP/MTRDeviceController_Internal.h @@ -44,7 +44,7 @@ namespace Controller { NS_ASSUME_NONNULL_BEGIN -@interface MTRDeviceController (InternalMethods) +@interface MTRDeviceController () #pragma mark - MTRDeviceControllerFactory methods @@ -139,6 +139,12 @@ NS_ASSUME_NONNULL_BEGIN */ - (void)getSessionForCommissioneeDevice:(chip::NodeId)deviceID completion:(MTRInternalDeviceConnectionCallback)completion; +/** + * Returns the transport used by the current session with the given device, + * or `MTRTransportTypeUndefined` if no session is currently active. + */ +- (MTRTransportType)sessionTransportTypeForDevice:(MTRBaseDevice *)device; + /** * Invalidate the CASE session for the given node ID. This is a temporary thing * just to support MTRBaseDevice's invalidateCASESession. Must not be called on