Skip to content

Commit 50b5dfb

Browse files
Make MTRBaseDevice be explicit about when it's assuming a concrete controller. (project-chip#35957)
* Make MTRBaseDevice be explicit about when it's assuming a concrete controller. This allows us to move several selectors from MTRDeviceController_Internal to MTRDeviceController_Concrete, and get rid of the never-actually-set _cppCommissioner ivar on MTRDeviceController. * Address review comment: add logs.
1 parent 59f7615 commit 50b5dfb

6 files changed

+235
-216
lines changed

src/darwin/Framework/CHIP/MTRBaseDevice.mm

+187-121
Large diffs are not rendered by default.

src/darwin/Framework/CHIP/MTRCallbackBridgeBase.h

+5
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ class MTRCallbackBridgeBase {
8181
{
8282
LogRequestStart();
8383

84+
// TODO: Figure out whether we can usefully get an MTRDeviceController_Concrete in here, so
85+
// we can move getSessionForCommissioneeDevice off of MTRDeviceController_Internal. Ideally
86+
// without bloating this inline method too much.
8487
[device.deviceController getSessionForCommissioneeDevice:device.nodeID
8588
completion:^(chip::Messaging::ExchangeManager * exchangeManager,
8689
const chip::Optional<chip::SessionHandle> & session, NSError * _Nullable error, NSNumber * _Nullable retryDelay) {
@@ -92,6 +95,8 @@ class MTRCallbackBridgeBase {
9295
{
9396
LogRequestStart();
9497

98+
// TODO: Figure out whether we can usefully get an MTRDeviceController_Concrete in here, so
99+
// we can move getSessionForNode off of MTRDeviceController_Internal.
95100
[controller getSessionForNode:nodeID
96101
completion:^(chip::Messaging::ExchangeManager * _Nullable exchangeManager,
97102
const chip::Optional<chip::SessionHandle> & session, NSError * _Nullable error, NSNumber * _Nullable retryDelay) {

src/darwin/Framework/CHIP/MTRDeviceController.mm

+3-56
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ - (instancetype)initWithDelegate:(id<MTRDeviceControllerDelegate>)delegate queue
119119
@end
120120

121121
@implementation MTRDeviceController {
122-
chip::Controller::DeviceCommissioner * _cppCommissioner;
123122
chip::Credentials::PartialDACVerifier * _partialDACVerifier;
124123
chip::Credentials::DefaultDACVerifier * _defaultDACVerifier;
125124
MTRDeviceControllerDelegateBridge * _deviceControllerDelegateBridge;
@@ -206,7 +205,8 @@ - (NSString *)description
206205

207206
- (BOOL)isRunning
208207
{
209-
return _cppCommissioner != nullptr;
208+
MTR_ABSTRACT_METHOD();
209+
return NO;
210210
}
211211

212212
#pragma mark - Suspend/resume support
@@ -547,37 +547,9 @@ - (void)getSessionForCommissioneeDevice:(chip::NodeId)deviceID completion:(MTRIn
547547
completion(nullptr, chip::NullOptional, [MTRError errorForCHIPErrorCode:CHIP_ERROR_INCORRECT_STATE], nil);
548548
}
549549

550-
- (MTRTransportType)sessionTransportTypeForDevice:(MTRBaseDevice *)device
551-
{
552-
VerifyOrReturnValue([self checkIsRunning], MTRTransportTypeUndefined);
553-
554-
__block MTRTransportType result = MTRTransportTypeUndefined;
555-
dispatch_sync(_chipWorkQueue, ^{
556-
VerifyOrReturn([self checkIsRunning]);
557-
558-
if (device.isPASEDevice) {
559-
chip::CommissioneeDeviceProxy * deviceProxy;
560-
VerifyOrReturn(CHIP_NO_ERROR == self->_cppCommissioner->GetDeviceBeingCommissioned(device.nodeID, &deviceProxy));
561-
result = MTRMakeTransportType(deviceProxy->GetDeviceTransportType());
562-
} else {
563-
auto scopedNodeID = self->_cppCommissioner->GetPeerScopedId(device.nodeID);
564-
auto sessionHandle = self->_cppCommissioner->SessionMgr()->FindSecureSessionForNode(scopedNodeID);
565-
VerifyOrReturn(sessionHandle.HasValue());
566-
result = MTRMakeTransportType(sessionHandle.Value()->AsSecureSession()->GetPeerAddress().GetTransportType());
567-
}
568-
});
569-
return result;
570-
}
571-
572-
- (void)asyncGetCommissionerOnMatterQueue:(void (^)(chip::Controller::DeviceCommissioner *))block
573-
errorHandler:(nullable MTRDeviceErrorHandler)errorHandler
574-
{
575-
MTR_ABSTRACT_METHOD();
576-
errorHandler([MTRError errorForCHIPErrorCode:CHIP_ERROR_INCORRECT_STATE]);
577-
}
578-
579550
- (void)asyncDispatchToMatterQueue:(dispatch_block_t)block errorHandler:(nullable MTRDeviceErrorHandler)errorHandler
580551
{
552+
// TODO: Figure out how to get callsites to have an MTRDeviceController_Concrete.
581553
MTR_ABSTRACT_METHOD();
582554
errorHandler([MTRError errorForCHIPErrorCode:CHIP_ERROR_INCORRECT_STATE]);
583555
}
@@ -627,31 +599,6 @@ - (nullable NSNumber *)compressedFabricID
627599
return storedValue.has_value() ? @(storedValue.value()) : nil;
628600
}
629601

630-
- (void)invalidateCASESessionForNode:(chip::NodeId)nodeID;
631-
{
632-
auto block = ^{
633-
auto sessionMgr = self->_cppCommissioner->SessionMgr();
634-
VerifyOrDie(sessionMgr != nullptr);
635-
636-
sessionMgr->MarkSessionsAsDefunct(
637-
self->_cppCommissioner->GetPeerScopedId(nodeID), chip::MakeOptional(chip::Transport::SecureSession::Type::kCASE));
638-
};
639-
640-
[self syncRunOnWorkQueue:block error:nil];
641-
}
642-
643-
- (void)downloadLogFromNodeWithID:(NSNumber *)nodeID
644-
type:(MTRDiagnosticLogType)type
645-
timeout:(NSTimeInterval)timeout
646-
queue:(dispatch_queue_t)queue
647-
completion:(void (^)(NSURL * _Nullable url, NSError * _Nullable error))completion
648-
{
649-
MTR_ABSTRACT_METHOD();
650-
dispatch_async(queue, ^{
651-
completion(nil, [MTRError errorForCHIPErrorCode:CHIP_ERROR_INCORRECT_STATE]);
652-
});
653-
}
654-
655602
#ifdef DEBUG
656603
+ (void)forceLocalhostAdvertisingOnly
657604
{

src/darwin/Framework/CHIP/MTRDeviceController_Concrete.h

+38
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#import <Foundation/Foundation.h>
1919

2020
#import <Matter/MTRAccessGrant.h>
21+
#import <Matter/MTRBaseDevice.h>
2122
#import <Matter/MTRDefines.h>
2223
#import <Matter/MTRDeviceController.h>
2324
#import <Matter/MTRDeviceControllerFactory.h>
@@ -149,6 +150,43 @@ NS_ASSUME_NONNULL_BEGIN
149150
*/
150151
- (nullable NSNumber *)neededReadPrivilegeForClusterID:(NSNumber *)clusterID attributeID:(NSNumber *)attributeID;
151152

153+
/**
154+
* Try to asynchronously dispatch the given block on the Matter queue. If the
155+
* controller is not running either at call time or when the block would be
156+
* about to run, the provided error handler will be called with an error. Note
157+
* that this means the error handler might be called on an arbitrary queue, and
158+
* might be called before this function returns or after it returns.
159+
*
160+
* The DeviceCommissioner pointer passed to the callback should only be used
161+
* synchronously during the callback invocation.
162+
*
163+
* If the error handler is nil, failure to run the block will be silent.
164+
*/
165+
- (void)asyncGetCommissionerOnMatterQueue:(void (^)(chip::Controller::DeviceCommissioner *))block
166+
errorHandler:(nullable MTRDeviceErrorHandler)errorHandler;
167+
168+
/**
169+
* Returns the transport used by the current session with the given device,
170+
* or `MTRTransportTypeUndefined` if no session is currently active.
171+
*/
172+
- (MTRTransportType)sessionTransportTypeForDevice:(MTRBaseDevice *)device;
173+
174+
/**
175+
* Invalidate the CASE session for the given node ID. This is a temporary thing
176+
* just to support MTRBaseDevice's invalidateCASESession. Must not be called on
177+
* the Matter event queue.
178+
*/
179+
- (void)invalidateCASESessionForNode:(NSNumber *)nodeID;
180+
181+
/**
182+
* Download log of the desired type from the device.
183+
*/
184+
- (void)downloadLogFromNodeWithID:(NSNumber *)nodeID
185+
type:(MTRDiagnosticLogType)type
186+
timeout:(NSTimeInterval)timeout
187+
queue:(dispatch_queue_t)queue
188+
completion:(void (^)(NSURL * _Nullable url, NSError * _Nullable error))completion;
189+
152190
@end
153191

154192
NS_ASSUME_NONNULL_END

src/darwin/Framework/CHIP/MTRDeviceController_Concrete.mm

+2-2
Original file line numberDiff line numberDiff line change
@@ -1586,14 +1586,14 @@ - (CHIP_ERROR)isRunningOnFabric:(chip::FabricTable *)fabricTable
15861586
return CHIP_NO_ERROR;
15871587
}
15881588

1589-
- (void)invalidateCASESessionForNode:(chip::NodeId)nodeID;
1589+
- (void)invalidateCASESessionForNode:(NSNumber *)nodeID;
15901590
{
15911591
auto block = ^{
15921592
auto sessionMgr = self->_cppCommissioner->SessionMgr();
15931593
VerifyOrDie(sessionMgr != nullptr);
15941594

15951595
sessionMgr->MarkSessionsAsDefunct(
1596-
self->_cppCommissioner->GetPeerScopedId(nodeID), chip::MakeOptional(chip::Transport::SecureSession::Type::kCASE));
1596+
self->_cppCommissioner->GetPeerScopedId(nodeID.unsignedLongLongValue), chip::MakeOptional(chip::Transport::SecureSession::Type::kCASE));
15971597
};
15981598

15991599
[self syncRunOnWorkQueue:block error:nil];

src/darwin/Framework/CHIP/MTRDeviceController_Internal.h

-37
Original file line numberDiff line numberDiff line change
@@ -154,34 +154,6 @@ NS_ASSUME_NONNULL_BEGIN
154154
*/
155155
- (void)getSessionForCommissioneeDevice:(chip::NodeId)deviceID completion:(MTRInternalDeviceConnectionCallback)completion;
156156

157-
/**
158-
* Returns the transport used by the current session with the given device,
159-
* or `MTRTransportTypeUndefined` if no session is currently active.
160-
*/
161-
- (MTRTransportType)sessionTransportTypeForDevice:(MTRBaseDevice *)device;
162-
163-
/**
164-
* Invalidate the CASE session for the given node ID. This is a temporary thing
165-
* just to support MTRBaseDevice's invalidateCASESession. Must not be called on
166-
* the Matter event queue.
167-
*/
168-
- (void)invalidateCASESessionForNode:(chip::NodeId)nodeID;
169-
170-
/**
171-
* Try to asynchronously dispatch the given block on the Matter queue. If the
172-
* controller is not running either at call time or when the block would be
173-
* about to run, the provided error handler will be called with an error. Note
174-
* that this means the error handler might be called on an arbitrary queue, and
175-
* might be called before this function returns or after it returns.
176-
*
177-
* The DeviceCommissioner pointer passed to the callback should only be used
178-
* synchronously during the callback invocation.
179-
*
180-
* If the error handler is nil, failure to run the block will be silent.
181-
*/
182-
- (void)asyncGetCommissionerOnMatterQueue:(void (^)(chip::Controller::DeviceCommissioner *))block
183-
errorHandler:(nullable MTRDeviceErrorHandler)errorHandler;
184-
185157
/**
186158
* Try to asynchronously dispatch the given block on the Matter queue. If the
187159
* controller is not running either at call time or when the block would be
@@ -200,15 +172,6 @@ NS_ASSUME_NONNULL_BEGIN
200172
*/
201173
- (MTRBaseDevice *)baseDeviceForNodeID:(NSNumber *)nodeID;
202174

203-
/**
204-
* Download log of the desired type from the device.
205-
*/
206-
- (void)downloadLogFromNodeWithID:(NSNumber *)nodeID
207-
type:(MTRDiagnosticLogType)type
208-
timeout:(NSTimeInterval)timeout
209-
queue:(dispatch_queue_t)queue
210-
completion:(void (^)(NSURL * _Nullable url, NSError * _Nullable error))completion;
211-
212175
#pragma mark - Device-specific data and SDK access
213176
// DeviceController will act as a central repository for this opaque dictionary that MTRDevice manages
214177
- (MTRDevice *)deviceForNodeID:(NSNumber *)nodeID;

0 commit comments

Comments
 (0)