Skip to content

Commit

Permalink
Fix numerous issues with iOS CHIPTool (#15115)
Browse files Browse the repository at this point in the history
* Fix numerous issues with CHIPTool

* Restyled by clang-format

* Fix zap src file

* Fix darwin test compile

* Remove unpair from chip-tool Darwin

* Restyled by whitespace

* Fix accidental merge conflict markers

* Properly remove unpair from chip-tool Darwin

Co-authored-by: Restyled.io <commits@restyled.io>
  • Loading branch information
2 people authored and pull[bot] committed Nov 3, 2023
1 parent 875465d commit 1306791
Show file tree
Hide file tree
Showing 12 changed files with 144 additions and 147 deletions.
7 changes: 0 additions & 7 deletions examples/chip-tool-darwin/commands/pairing/Commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,6 @@

#include "PairingCommandBridge.h"

class Unpair : public PairingCommandBridge
{
public:
Unpair() : PairingCommandBridge("unpair", PairingMode::None) {}
};

class PairQRCode : public PairingCommandBridge
{
public:
Expand All @@ -49,7 +43,6 @@ void registerCommandsPairing(Commands & commands)
const char * clusterName = "Pairing";

commands_list clusterCommands = {
make_unique<Unpair>(),
make_unique<PairQRCode>(),
make_unique<PairManualCode>(),
make_unique<PairWithIPAddress>(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

enum class PairingMode
{
None,
QRCode,
ManualCode,
Ethernet
Expand All @@ -38,8 +37,6 @@ class PairingCommandBridge : public CHIPCommandBridge

switch (mode)
{
case PairingMode::None:
break;
case PairingMode::QRCode:
AddArgument("payload", &mOnboardingPayload);
break;
Expand All @@ -62,7 +59,6 @@ class PairingCommandBridge : public CHIPCommandBridge
private:
void PairWithCode(NSError * __autoreleasing * error);
void PairWithIPAddress(NSError * __autoreleasing * error);
void Unpair(NSError * __autoreleasing * error);
void SetUpPairingDelegate();

const PairingMode mPairingMode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@
{
NSError * error;
switch (mPairingMode) {
case PairingMode::None:
Unpair(&error);
break;
case PairingMode::QRCode:
case PairingMode::ManualCode:
PairWithCode(&error);
Expand Down Expand Up @@ -75,5 +72,3 @@
setupPINCode:mSetupPINCode
error:error];
}

void PairingCommandBridge::Unpair(NSError * __autoreleasing * error) { [CurrentCommissioner() unpairDevice:mNodeId error:error]; }
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ id CHIPGetDomainValueForKey(NSString * domain, NSString * key);
void CHIPSetDomainValueForKey(NSString * domain, NSString * key, id value);
void CHIPRemoveDomainValueForKey(NSString * domain, NSString * key);
uint64_t CHIPGetNextAvailableDeviceID(void);
NSString * KeyForPairedDevice(uint64_t id);
uint64_t CHIPGetLastPairedDeviceId(void);
void CHIPSetNextAvailableDeviceID(uint64_t id);
void CHIPSetDevicePaired(uint64_t id, BOOL paired);
BOOL CHIPIsDevicePaired(uint64_t id);
BOOL CHIPGetConnectedDevice(CHIPDeviceConnectionCallback completionHandler);
BOOL CHIPGetConnectedDeviceWithID(uint64_t deviceId, CHIPDeviceConnectionCallback completionHandler);
Expand Down
52 changes: 40 additions & 12 deletions src/darwin/CHIPTool/CHIPTool/Framework Helpers/DefaultsUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
NSString * const kNetworkPasswordDefaultsKey = @"networkPassword";
NSString * const kCHIPNextAvailableDeviceIDKey = @"nextDeviceID";
NSString * const kFabricIdKey = @"fabricId";
NSString * const kDevicePairedKey = @"Paired";

id CHIPGetDomainValueForKey(NSString * domain, NSString * key)
{
Expand Down Expand Up @@ -115,23 +116,50 @@ BOOL CHIPGetConnectedDeviceWithID(uint64_t deviceId, CHIPDeviceConnectionCallbac

BOOL CHIPIsDevicePaired(uint64_t deviceId)
{
CHIPDeviceController * controller = InitializeCHIP();
NSString * PairedString = CHIPGetDomainValueForKey(kCHIPToolDefaultsDomain, KeyForPairedDevice(deviceId));
return [PairedString boolValue];
}

NSError * error;
bool paired = [controller isDevicePaired:deviceId error:&error];
if (error != nil) {
NSLog(@"Error retrieving device info for deviceId %llu", deviceId);
paired = NO;
}
return paired;
void CHIPSetDevicePaired(uint64_t deviceId, BOOL paired)
{
CHIPSetDomainValueForKey(kCHIPToolDefaultsDomain, KeyForPairedDevice(deviceId), paired ? @"YES" : @"NO");
}

NSString * KeyForPairedDevice(uint64_t deviceId) { return [NSString stringWithFormat:@"%@%llu", kDevicePairedKey, deviceId]; }

void CHIPUnpairDeviceWithID(uint64_t deviceId)
{
CHIPDeviceController * controller = InitializeCHIP();

NSError * error;
[controller unpairDevice:deviceId error:&error];
CHIPSetDevicePaired(deviceId, NO);
CHIPGetConnectedDeviceWithID(deviceId, ^(CHIPDevice * _Nullable device, NSError * _Nullable error) {
if (error) {
NSLog(@"Failed to unpair device %llu still removing from CHIPTool. %@", deviceId, error);
return;
}
NSLog(@"Attempting to unpair device %llu", deviceId);
CHIPOperationalCredentials * opCredsCluster = [[CHIPOperationalCredentials alloc] initWithDevice:device
endpoint:0
queue:dispatch_get_main_queue()];
[opCredsCluster
readAttributeCurrentFabricIndexWithCompletionHandler:^(NSNumber * _Nullable value, NSError * _Nullable error) {
if (error) {
NSLog(@"Failed to get current fabric index for device %llu still removing from CHIPTool. %@", deviceId, error);
return;
}
CHIPOperationalCredentialsClusterRemoveFabricParams * params =
[[CHIPOperationalCredentialsClusterRemoveFabricParams alloc] init];
params.fabricIndex = value;
[opCredsCluster removeFabricWithParams:params
completionHandler:^(CHIPOperationalCredentialsClusterNOCResponseParams * _Nullable data,
NSError * _Nullable error) {
if (error) {
NSLog(@"Failed to remove current fabric index %@ for device %llu. %@",
params.fabricIndex, deviceId, error);
return;
}
NSLog(@"Successfully unpaired deviceId %llu", deviceId);
}];
}];
});
}

@implementation CHIPToolPersistentStorageDelegate
Expand Down
Loading

0 comments on commit 1306791

Please sign in to comment.