Skip to content

Commit 533ac10

Browse files
Add a "commissionee has received network credentials" notification to Matter.framework. (#38155)
1 parent 3d434c3 commit 533ac10

6 files changed

+46
-0
lines changed

examples/darwin-framework-tool/commands/pairing/DeviceControllerDelegateBridge.h

+1
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,6 @@
3232
- (void)controller:(MTRDeviceController *)controller commissioningSessionEstablishmentDone:(NSError *)error;
3333
- (void)controller:(MTRDeviceController *)controller commissioningComplete:(NSError *)error;
3434
- (void)controller:(MTRDeviceController *)controller commissioningComplete:(NSError *)error nodeID:(NSNumber *)nodeID metrics:(MTRMetrics *)metrics;
35+
- (void)controller:(MTRDeviceController *)controller commissioneeHasReceivedNetworkCredentials:(NSNumber *)nodeID;
3536

3637
@end

examples/darwin-framework-tool/commands/pairing/DeviceControllerDelegateBridge.mm

+5
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,9 @@ - (void)controller:(MTRDeviceController *)controller commissioningComplete:(NSEr
7575
_commandBridge->SetCommandExitStatus(error, [message UTF8String]);
7676
}
7777

78+
- (void)controller:(MTRDeviceController *)controller commissioneeHasReceivedNetworkCredentials:(NSNumber *)nodeID
79+
{
80+
NSLog(@"Node %@ has received network credentials", nodeID);
81+
}
82+
7883
@end

src/darwin/Framework/CHIP/MTRDeviceController.mm

+9
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,15 @@ - (void)controller:(MTRDeviceController *)controller readCommissioneeInfo:(MTRCo
715715
} logString:__PRETTY_FUNCTION__];
716716
}
717717

718+
- (void)controller:(MTRDeviceController *)controller commissioneeHasReceivedNetworkCredentials:(NSNumber *)nodeID
719+
{
720+
[self _callDelegatesWithBlock:^(id<MTRDeviceControllerDelegate> delegate) {
721+
if ([delegate respondsToSelector:@selector(controller:commissioneeHasReceivedNetworkCredentials:)]) {
722+
[delegate controller:controller commissioneeHasReceivedNetworkCredentials:nodeID];
723+
}
724+
} logString:__PRETTY_FUNCTION__];
725+
}
726+
718727
@end
719728

720729
// TODO: This should not be in the superclass: either move to

src/darwin/Framework/CHIP/MTRDeviceControllerDelegate.h

+8
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,14 @@ MTR_AVAILABLE(ios(16.4), macos(13.3), watchos(9.4), tvos(16.4))
112112
*/
113113
- (void)devicesChangedForController:(MTRDeviceController *)controller MTR_AVAILABLE(ios(18.4), macos(15.4), watchos(11.4), tvos(18.4));
114114

115+
/**
116+
* Notify the delegate that we have successfully communicated the network
117+
* credentials to the device being commissioned and are about to tell it to join
118+
* that network. Note that for devices that are already on-network this
119+
* notification will not happen.
120+
*/
121+
- (void)controller:(MTRDeviceController *)controller commissioneeHasReceivedNetworkCredentials:(NSNumber *)nodeID MTR_AVAILABLE(ios(18.5), macos(15.5), watchos(11.5), tvos(18.5));
122+
115123
@end
116124

117125
typedef NS_ENUM(NSUInteger, MTRPairingStatus) {

src/darwin/Framework/CHIP/MTRDeviceControllerDelegateBridge.h

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class MTRDeviceControllerDelegateBridge : public chip::Controller::DevicePairing
4141
void OnReadCommissioningInfo(const chip::Controller::ReadCommissioningInfo & info) override;
4242

4343
void OnCommissioningComplete(chip::NodeId deviceId, CHIP_ERROR error) override;
44+
void OnCommissioningStatusUpdate(chip::PeerId peerId, chip::Controller::CommissioningStage stageCompleted, CHIP_ERROR error) override;
4445

4546
void SetDeviceNodeID(chip::NodeId deviceNodeId);
4647

src/darwin/Framework/CHIP/MTRDeviceControllerDelegateBridge.mm

+22
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,28 @@
188188
}
189189
}
190190

191+
void MTRDeviceControllerDelegateBridge::OnCommissioningStatusUpdate(chip::PeerId peerId, chip::Controller::CommissioningStage stageCompleted, CHIP_ERROR error)
192+
{
193+
using namespace chip::Controller;
194+
195+
MTRDeviceController * strongController = mController;
196+
197+
MTR_LOG("%@ DeviceControllerDelegate stage %s completed for nodeID 0x%016llx with status %s", strongController, StageToString(stageCompleted), peerId.GetNodeId(), error.AsString());
198+
199+
id<MTRDeviceControllerDelegate> strongDelegate = mDelegate;
200+
201+
if (strongDelegate && mQueue && strongController && error == CHIP_NO_ERROR && (stageCompleted == CommissioningStage::kFailsafeBeforeWiFiEnable || stageCompleted == CommissioningStage::kFailsafeBeforeThreadEnable)) {
202+
// We're about to send the ConnectNetwork command. Let our delegate
203+
// know that the other side now has network credentials.
204+
NSNumber * nodeID = @(peerId.GetNodeId());
205+
if ([strongDelegate respondsToSelector:@selector(controller:commissioneeHasReceivedNetworkCredentials:)]) {
206+
dispatch_async(mQueue, ^{
207+
[strongDelegate controller:strongController commissioneeHasReceivedNetworkCredentials:nodeID];
208+
});
209+
}
210+
}
211+
}
212+
191213
void MTRDeviceControllerDelegateBridge::SetDeviceNodeID(chip::NodeId deviceNodeId)
192214
{
193215
mDeviceNodeId = deviceNodeId;

0 commit comments

Comments
 (0)