Skip to content

Commit

Permalink
Darwin: Expose MTRBaseDevice.sessionTransportType (#24065)
Browse files Browse the repository at this point in the history
* Expose MTRBaseDevice.sessionTransportType.

- Use it in iOS CHIPTool
- Remove _deviceBeingCommissionedOverBLE

* Format / address comments
  • Loading branch information
ksperling-apple authored and pull[bot] committed Feb 1, 2023
1 parent 770589e commit 3774484
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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];
Expand Down
13 changes: 13 additions & 0 deletions src/darwin/Framework/CHIP/MTRBaseDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions src/darwin/Framework/CHIP/MTRBaseDevice.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
12 changes: 9 additions & 3 deletions src/darwin/Framework/CHIP/MTRBaseDevice_Internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<MTRTransportType>(type);
}

@interface MTRBaseDevice ()

- (instancetype)initWithPASEDevice:(chip::DeviceProxy *)device controller:(MTRDeviceController *)controller;
Expand Down Expand Up @@ -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
Expand Down
44 changes: 26 additions & 18 deletions src/darwin/Framework/CHIP/MTRDeviceController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#import <os/lock.h>

#import "MTRDeviceController.h"
#import "MTRDeviceController_Internal.h"

#import "MTRBaseDevice_Internal.h"
#import "MTRCommissioningParameters.h"
Expand Down Expand Up @@ -54,6 +52,8 @@
#include <setup_payload/ManualSetupPayloadGenerator.h>
#include <system/SystemClock.h>

#import <os/lock.h>

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";
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -795,10 +806,6 @@ - (BOOL)syncRunOnWorkQueueWithBoolReturnValue:(SyncWorkQueueBlockWithBoolReturnV
return success;
}

@end

@implementation MTRDeviceController (InternalMethods)

- (chip::FabricIndex)fabricIndex
{
if (!_cppCommissioner) {
Expand Down Expand Up @@ -848,6 +855,7 @@ - (void)invalidateCASESessionForNode:(chip::NodeId)nodeID;

[self syncRunOnWorkQueue:block error:nil];
}

@end

/**
Expand Down
8 changes: 7 additions & 1 deletion src/darwin/Framework/CHIP/MTRDeviceController_Internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ namespace Controller {

NS_ASSUME_NONNULL_BEGIN

@interface MTRDeviceController (InternalMethods)
@interface MTRDeviceController ()

#pragma mark - MTRDeviceControllerFactory methods

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3774484

Please sign in to comment.