diff --git a/examples/chip-tool-darwin/commands/pairing/Commands.h b/examples/chip-tool-darwin/commands/pairing/Commands.h index 4e88b1ea3a502a..23975a93f18b12 100644 --- a/examples/chip-tool-darwin/commands/pairing/Commands.h +++ b/examples/chip-tool-darwin/commands/pairing/Commands.h @@ -50,13 +50,19 @@ class PairBleThread : public PairingCommandBridge PairBleThread() : PairingCommandBridge("ble-thread", PairingMode::Ble, PairingNetworkType::Thread) {} }; +class Unpair : public PairingCommandBridge +{ +public: + Unpair() : PairingCommandBridge("unpair", PairingMode::None, PairingNetworkType::None) {} +}; + void registerCommandsPairing(Commands & commands) { const char * clusterName = "Pairing"; commands_list clusterCommands = { make_unique(), make_unique(), make_unique(), - make_unique(), make_unique(), + make_unique(), make_unique(), make_unique(), }; commands.Register(clusterName, clusterCommands); diff --git a/examples/chip-tool-darwin/commands/pairing/PairingCommandBridge.h b/examples/chip-tool-darwin/commands/pairing/PairingCommandBridge.h index 272de92dce994b..7c28fdf5338287 100644 --- a/examples/chip-tool-darwin/commands/pairing/PairingCommandBridge.h +++ b/examples/chip-tool-darwin/commands/pairing/PairingCommandBridge.h @@ -22,6 +22,7 @@ enum class PairingMode { + None, QRCode, ManualCode, Ethernet, @@ -59,6 +60,8 @@ class PairingCommandBridge : public CHIPCommandBridge switch (mode) { + case PairingMode::None: + break; case PairingMode::QRCode: AddArgument("payload", &mOnboardingPayload); break; @@ -86,6 +89,7 @@ class PairingCommandBridge : public CHIPCommandBridge void PairWithCode(NSError * __autoreleasing * error); void PairWithPayload(NSError * __autoreleasing * error); void PairWithIPAddress(NSError * __autoreleasing * error); + void Unpair(); void SetUpPairingDelegate(); const PairingMode mPairingMode; diff --git a/examples/chip-tool-darwin/commands/pairing/PairingCommandBridge.mm b/examples/chip-tool-darwin/commands/pairing/PairingCommandBridge.mm index 3fa1e3e04c70f4..4f19011eb5140e 100644 --- a/examples/chip-tool-darwin/commands/pairing/PairingCommandBridge.mm +++ b/examples/chip-tool-darwin/commands/pairing/PairingCommandBridge.mm @@ -17,6 +17,7 @@ */ #import +#import #include "../common/CHIPCommandBridge.h" #include "PairingCommandBridge.h" @@ -57,6 +58,9 @@ { NSError * error; switch (mPairingMode) { + case PairingMode::None: + Unpair(); + break; case PairingMode::QRCode: case PairingMode::ManualCode: PairWithPayload(&error); @@ -98,3 +102,51 @@ setupPINCode:mSetupPINCode error:error]; } + +void PairingCommandBridge::Unpair() +{ + dispatch_queue_t callbackQueue = dispatch_queue_create("com.chip-tool.command", DISPATCH_QUEUE_SERIAL); + [CurrentCommissioner() + getConnectedDevice:mNodeId + queue:callbackQueue + completionHandler:^(CHIPDevice * _Nullable device, NSError * _Nullable error) { + CHIP_ERROR err = CHIP_NO_ERROR; + if (error) { + err = [CHIPError errorToCHIPErrorCode:error]; + LogNSError("Error: ", error); + SetCommandExitStatus(err); + } else if (device == nil) { + ChipLogError(chipTool, "Error: %s", chip::ErrorStr(CHIP_ERROR_INTERNAL)); + SetCommandExitStatus(CHIP_ERROR_INTERNAL); + } else { + ChipLogProgress(chipTool, "Attempting to unpair device %llu", mNodeId); + CHIPOperationalCredentials * opCredsCluster = [[CHIPOperationalCredentials alloc] initWithDevice:device + endpoint:0 + queue:callbackQueue]; + [opCredsCluster readAttributeCurrentFabricIndexWithCompletionHandler:^( + NSNumber * _Nullable value, NSError * _Nullable readError) { + if (readError) { + CHIP_ERROR readErr = [CHIPError errorToCHIPErrorCode:readError]; + LogNSError("Failed to get current fabric: ", readError); + SetCommandExitStatus(readErr); + return; + } + CHIPOperationalCredentialsClusterRemoveFabricParams * params = + [[CHIPOperationalCredentialsClusterRemoveFabricParams alloc] init]; + params.fabricIndex = value; + [opCredsCluster removeFabricWithParams:params + completionHandler:^(CHIPOperationalCredentialsClusterNOCResponseParams * _Nullable data, + NSError * _Nullable removeError) { + CHIP_ERROR removeErr = CHIP_NO_ERROR; + if (removeError) { + removeErr = [CHIPError errorToCHIPErrorCode:removeError]; + LogNSError("Failed to remove current fabric: ", removeError); + } else { + ChipLogProgress(chipTool, "Successfully unpaired deviceId %llu", mNodeId); + } + SetCommandExitStatus(removeErr); + }]; + }]; + } + }]; +}