From acd88aa571e619ff265633a368b7319dd40a6e31 Mon Sep 17 00:00:00 2001 From: Karsten Sperling Date: Tue, 13 Dec 2022 10:38:47 +1300 Subject: [PATCH] Expose MTRBaseDevice.sessionTransportType. - Use it in iOS CHIPTool - Remove _deviceBeingCommissionedOverBLE --- .../QRCode/QRCodeViewController.m | 15 +------ src/darwin/Framework/CHIP/MTRBaseDevice.h | 13 ++++++ src/darwin/Framework/CHIP/MTRBaseDevice.mm | 5 +++ .../Framework/CHIP/MTRBaseDevice_Internal.h | 8 ++++ .../Framework/CHIP/MTRDeviceController.mm | 43 +++++++++++-------- .../CHIP/MTRDeviceController_Internal.h | 8 +++- 6 files changed, 60 insertions(+), 32 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..75732f941892a1 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..a0abbf53e6df82 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, +}; + @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; + /** * 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 eb39a8609f7350..deec6fb446f305 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..5bf953a9eab58c 100644 --- a/src/darwin/Framework/CHIP/MTRBaseDevice_Internal.h +++ b/src/darwin/Framework/CHIP/MTRBaseDevice_Internal.h @@ -27,6 +27,14 @@ 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; diff --git a/src/darwin/Framework/CHIP/MTRDeviceController.mm b/src/darwin/Framework/CHIP/MTRDeviceController.mm index 066d32774f0aed..8fafc0fb90074b 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"; @@ -719,17 +719,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; -} - - (BOOL)getSessionForNode:(chip::NodeId)nodeID completion:(MTRInternalDeviceConnectionCallback)completion { if (![self checkIsRunning]) { @@ -785,6 +774,28 @@ - (BOOL)getSessionForCommissioneeDevice:(chip::NodeId)deviceID completion:(MTRIn return YES; } +- (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)asyncDispatchToMatterQueue:(void (^)(chip::Controller::DeviceCommissioner *))block errorHandler:(void (^)(NSError *))errorHandler { @@ -807,10 +818,6 @@ - (void)asyncDispatchToMatterQueue:(void (^)(chip::Controller::DeviceCommissione }); } -@end - -@implementation MTRDeviceController (InternalMethods) - - (chip::FabricIndex)fabricIndex { if (!_cppCommissioner) { diff --git a/src/darwin/Framework/CHIP/MTRDeviceController_Internal.h b/src/darwin/Framework/CHIP/MTRDeviceController_Internal.h index bed9b70162fec8..14295d87c930e2 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 @@ -134,6 +134,12 @@ NS_ASSUME_NONNULL_BEGIN */ - (BOOL)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