Skip to content

Commit

Permalink
Add Unpair to chip-tool-darwin (#18450)
Browse files Browse the repository at this point in the history
  • Loading branch information
krypton36 authored and pull[bot] committed Jul 18, 2023
1 parent 2ddc87b commit 1319088
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
8 changes: 7 additions & 1 deletion examples/chip-tool-darwin/commands/pairing/Commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<PairQRCode>(), make_unique<PairManualCode>(), make_unique<PairWithIPAddress>(),
make_unique<PairBleWiFi>(), make_unique<PairBleThread>(),
make_unique<PairBleWiFi>(), make_unique<PairBleThread>(), make_unique<Unpair>(),
};

commands.Register(clusterName, clusterCommands);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

enum class PairingMode
{
None,
QRCode,
ManualCode,
Ethernet,
Expand Down Expand Up @@ -59,6 +60,8 @@ class PairingCommandBridge : public CHIPCommandBridge

switch (mode)
{
case PairingMode::None:
break;
case PairingMode::QRCode:
AddArgument("payload", &mOnboardingPayload);
break;
Expand Down Expand Up @@ -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;
Expand Down
52 changes: 52 additions & 0 deletions examples/chip-tool-darwin/commands/pairing/PairingCommandBridge.mm
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/

#import <CHIP/CHIP.h>
#import <CHIP/CHIPError_Internal.h>

#include "../common/CHIPCommandBridge.h"
#include "PairingCommandBridge.h"
Expand Down Expand Up @@ -57,6 +58,9 @@
{
NSError * error;
switch (mPairingMode) {
case PairingMode::None:
Unpair();
break;
case PairingMode::QRCode:
case PairingMode::ManualCode:
PairWithPayload(&error);
Expand Down Expand Up @@ -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);
}];
}];
}
}];
}

0 comments on commit 1319088

Please sign in to comment.