Skip to content

Commit 1a0f9b4

Browse files
Darwin: Expose MTRBaseDevice.sessionTransportType (#24065)
* Expose MTRBaseDevice.sessionTransportType. - Use it in iOS CHIPTool - Remove _deviceBeingCommissionedOverBLE * Format / address comments
1 parent 7eba3bc commit 1a0f9b4

File tree

6 files changed

+62
-35
lines changed

6 files changed

+62
-35
lines changed

src/darwin/CHIPTool/CHIPTool/View Controllers/QRCode/QRCodeViewController.m

+2-13
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,6 @@
4343

4444
#define NOT_APPLICABLE_STRING @"N/A"
4545

46-
@interface MTRDeviceController (ToDoRemove)
47-
48-
/**
49-
* TODO: Temporary until PairingDelegate is fixed to clearly communicate this
50-
* information to consumers.
51-
* This should be migrated over to the proper pairing delegate path
52-
*/
53-
- (BOOL)_deviceBeingCommissionedOverBLE:(uint64_t)deviceId;
54-
55-
@end
56-
5746
@interface QRCodeViewController ()
5847

5948
@property (nonatomic, strong) AVCaptureSession * captureSession;
@@ -497,8 +486,8 @@ - (void)onPairingComplete:(NSError * _Nullable)error
497486
} else {
498487
MTRDeviceController * controller = InitializeMTR();
499488
uint64_t deviceId = MTRGetLastPairedDeviceId();
500-
if ([controller respondsToSelector:@selector(_deviceBeingCommissionedOverBLE:)] &&
501-
[controller _deviceBeingCommissionedOverBLE:deviceId]) {
489+
MTRBaseDevice * device = [controller deviceBeingCommissionedWithNodeID:@(deviceId) error:NULL];
490+
if (device.sessionTransportType == MTRTransportTypeBLE) {
502491
dispatch_async(dispatch_get_main_queue(), ^{
503492
[self->_deviceList refreshDeviceList];
504493
[self retrieveAndSendWiFiCredentials];

src/darwin/Framework/CHIP/MTRBaseDevice.h

+13
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,13 @@ extern NSString * const MTRArrayValueType;
122122
@class MTRReadParams;
123123
@class MTRSubscribeParams;
124124

125+
typedef NS_ENUM(uint8_t, MTRTransportType) {
126+
MTRTransportTypeUndefined = 0,
127+
MTRTransportTypeUDP,
128+
MTRTransportTypeBLE,
129+
MTRTransportTypeTCP,
130+
} MTR_NEWLY_AVAILABLE;
131+
125132
@interface MTRBaseDevice : NSObject
126133

127134
- (instancetype)init NS_UNAVAILABLE;
@@ -135,6 +142,12 @@ extern NSString * const MTRArrayValueType;
135142
*/
136143
+ (instancetype)deviceWithNodeID:(NSNumber *)nodeID controller:(MTRDeviceController *)controller MTR_NEWLY_AVAILABLE;
137144

145+
/**
146+
* The transport used by the current session with this device, or
147+
* `MTRTransportTypeUndefined` if no session is currently active.
148+
*/
149+
@property (readonly) MTRTransportType sessionTransportType MTR_NEWLY_AVAILABLE;
150+
138151
/**
139152
* Subscribe to receive attribute reports for everything (all endpoints, all
140153
* clusters, all attributes, all events) on the device.

src/darwin/Framework/CHIP/MTRBaseDevice.mm

+5
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,11 @@ + (instancetype)deviceWithNodeID:(NSNumber *)nodeID controller:(MTRDeviceControl
268268
return [controller baseDeviceForNodeID:nodeID];
269269
}
270270

271+
- (MTRTransportType)sessionTransportType
272+
{
273+
return [self.deviceController sessionTransportTypeForDevice:self];
274+
}
275+
271276
- (void)invalidateCASESession
272277
{
273278
if (self.isPASEDevice) {

src/darwin/Framework/CHIP/MTRBaseDevice_Internal.h

+9-3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@
2727

2828
NS_ASSUME_NONNULL_BEGIN
2929

30+
static inline MTRTransportType MTRMakeTransportType(chip::Transport::Type type)
31+
{
32+
static_assert(MTRTransportTypeUndefined == (uint8_t) chip::Transport::Type::kUndefined, "MTRTransportType != Transport::Type");
33+
static_assert(MTRTransportTypeUDP == (uint8_t) chip::Transport::Type::kUdp, "MTRTransportType != Transport::Type");
34+
static_assert(MTRTransportTypeBLE == (uint8_t) chip::Transport::Type::kBle, "MTRTransportType != Transport::Type");
35+
static_assert(MTRTransportTypeTCP == (uint8_t) chip::Transport::Type::kTcp, "MTRTransportType != Transport::Type");
36+
return static_cast<MTRTransportType>(type);
37+
}
38+
3039
@interface MTRBaseDevice ()
3140

3241
- (instancetype)initWithPASEDevice:(chip::DeviceProxy *)device controller:(MTRDeviceController *)controller;
@@ -55,9 +64,6 @@ NS_ASSUME_NONNULL_BEGIN
5564
*/
5665
@property (nonatomic, assign, readonly) chip::NodeId nodeID;
5766

58-
- (instancetype)init NS_UNAVAILABLE;
59-
+ (instancetype)new NS_UNAVAILABLE;
60-
6167
/**
6268
* Initialize the device object as a CASE device with the given node id and
6369
* controller. This will always succeed, even if there is no such node id on

src/darwin/Framework/CHIP/MTRDeviceController.mm

+26-18
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
#import <os/lock.h>
18-
19-
#import "MTRDeviceController.h"
17+
#import "MTRDeviceController_Internal.h"
2018

2119
#import "MTRBaseDevice_Internal.h"
2220
#import "MTRCommissioningParameters.h"
@@ -54,6 +52,8 @@
5452
#include <setup_payload/ManualSetupPayloadGenerator.h>
5553
#include <system/SystemClock.h>
5654

55+
#import <os/lock.h>
56+
5757
static NSString * const kErrorCommissionerInit = @"Init failure while initializing a commissioner";
5858
static NSString * const kErrorIPKInit = @"Init failure while initializing IPK";
5959
static NSString * const kErrorSigningKeypairInit = @"Init failure while creating signing keypair bridge";
@@ -678,17 +678,6 @@ - (BOOL)checkIsRunning:(NSError * __autoreleasing *)error
678678
return NO;
679679
}
680680

681-
- (BOOL)_deviceBeingCommissionedOverBLE:(uint64_t)deviceID
682-
{
683-
VerifyOrReturnValue([self checkIsRunning], NO);
684-
685-
chip::CommissioneeDeviceProxy * deviceProxy;
686-
auto errorCode = self->_cppCommissioner->GetDeviceBeingCommissioned(deviceID, &deviceProxy);
687-
VerifyOrReturnValue(errorCode == CHIP_NO_ERROR, NO);
688-
689-
return deviceProxy->GetDeviceTransportType() == chip::Transport::Type::kBle;
690-
}
691-
692681
- (void)getSessionForNode:(chip::NodeId)nodeID completion:(MTRInternalDeviceConnectionCallback)completion
693682
{
694683
[self
@@ -728,6 +717,28 @@ - (void)getSessionForCommissioneeDevice:(chip::NodeId)deviceID completion:(MTRIn
728717
}];
729718
}
730719

720+
- (MTRTransportType)sessionTransportTypeForDevice:(MTRBaseDevice *)device
721+
{
722+
VerifyOrReturnValue([self checkIsRunning], MTRTransportTypeUndefined);
723+
724+
__block MTRTransportType result = MTRTransportTypeUndefined;
725+
dispatch_sync(_chipWorkQueue, ^{
726+
VerifyOrReturn([self checkIsRunning]);
727+
728+
if (device.isPASEDevice) {
729+
chip::CommissioneeDeviceProxy * deviceProxy;
730+
VerifyOrReturn(CHIP_NO_ERROR == self->_cppCommissioner->GetDeviceBeingCommissioned(device.nodeID, &deviceProxy));
731+
result = MTRMakeTransportType(deviceProxy->GetDeviceTransportType());
732+
} else {
733+
auto scopedNodeID = self->_cppCommissioner->GetPeerScopedId(device.nodeID);
734+
auto sessionHandle = self->_cppCommissioner->SessionMgr()->FindSecureSessionForNode(scopedNodeID);
735+
VerifyOrReturn(sessionHandle.HasValue());
736+
result = MTRMakeTransportType(sessionHandle.Value()->AsSecureSession()->GetPeerAddress().GetTransportType());
737+
}
738+
});
739+
return result;
740+
}
741+
731742
- (void)asyncGetCommissionerOnMatterQueue:(void (^)(chip::Controller::DeviceCommissioner *))block
732743
errorHandler:(nullable MTRDeviceErrorHandler)errorHandler
733744
{
@@ -795,10 +806,6 @@ - (BOOL)syncRunOnWorkQueueWithBoolReturnValue:(SyncWorkQueueBlockWithBoolReturnV
795806
return success;
796807
}
797808

798-
@end
799-
800-
@implementation MTRDeviceController (InternalMethods)
801-
802809
- (chip::FabricIndex)fabricIndex
803810
{
804811
if (!_cppCommissioner) {
@@ -848,6 +855,7 @@ - (void)invalidateCASESessionForNode:(chip::NodeId)nodeID;
848855

849856
[self syncRunOnWorkQueue:block error:nil];
850857
}
858+
851859
@end
852860

853861
/**

src/darwin/Framework/CHIP/MTRDeviceController_Internal.h

+7-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ namespace Controller {
4444

4545
NS_ASSUME_NONNULL_BEGIN
4646

47-
@interface MTRDeviceController (InternalMethods)
47+
@interface MTRDeviceController ()
4848

4949
#pragma mark - MTRDeviceControllerFactory methods
5050

@@ -139,6 +139,12 @@ NS_ASSUME_NONNULL_BEGIN
139139
*/
140140
- (void)getSessionForCommissioneeDevice:(chip::NodeId)deviceID completion:(MTRInternalDeviceConnectionCallback)completion;
141141

142+
/**
143+
* Returns the transport used by the current session with the given device,
144+
* or `MTRTransportTypeUndefined` if no session is currently active.
145+
*/
146+
- (MTRTransportType)sessionTransportTypeForDevice:(MTRBaseDevice *)device;
147+
142148
/**
143149
* Invalidate the CASE session for the given node ID. This is a temporary thing
144150
* just to support MTRBaseDevice's invalidateCASESession. Must not be called on

0 commit comments

Comments
 (0)