Skip to content

Commit

Permalink
Add BLE and thread pairing capabilities to chip-tool-darwin. (#16126)
Browse files Browse the repository at this point in the history
  • Loading branch information
krypton36 authored and pull[bot] committed Jan 26, 2024
1 parent 9c0b6f1 commit 3310009
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 13 deletions.
23 changes: 17 additions & 6 deletions examples/chip-tool-darwin/commands/pairing/Commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,40 @@
class PairQRCode : public PairingCommandBridge
{
public:
PairQRCode() : PairingCommandBridge("qrcode", PairingMode::QRCode) {}
PairQRCode() : PairingCommandBridge("qrcode", PairingMode::QRCode, PairingNetworkType::None) {}
};

class PairManualCode : public PairingCommandBridge
{
public:
PairManualCode() : PairingCommandBridge("manualcode", PairingMode::ManualCode) {}
PairManualCode() : PairingCommandBridge("manualcode", PairingMode::ManualCode, PairingNetworkType::None) {}
};

class PairWithIPAddress : public PairingCommandBridge
{
public:
PairWithIPAddress() : PairingCommandBridge("ethernet", PairingMode::Ethernet) {}
PairWithIPAddress() : PairingCommandBridge("ethernet", PairingMode::Ethernet, PairingNetworkType::Ethernet) {}
};

class PairBleWiFi : public PairingCommandBridge
{
public:
PairBleWiFi() : PairingCommandBridge("ble-wifi", PairingMode::Ble, PairingNetworkType::WiFi) {}
};

class PairBleThread : public PairingCommandBridge
{
public:
PairBleThread() : PairingCommandBridge("ble-thread", PairingMode::Ble, PairingNetworkType::Thread) {}
};

void registerCommandsPairing(Commands & commands)
{
const char * clusterName = "Pairing";

commands_list clusterCommands = {
make_unique<PairQRCode>(),
make_unique<PairManualCode>(),
make_unique<PairWithIPAddress>(),
make_unique<PairQRCode>(), make_unique<PairManualCode>(), make_unique<PairWithIPAddress>(),
make_unique<PairBleWiFi>(), make_unique<PairBleThread>(),
};

commands.Register(clusterName, clusterCommands);
Expand Down
36 changes: 34 additions & 2 deletions examples/chip-tool-darwin/commands/pairing/PairingCommandBridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,38 @@ enum class PairingMode
{
QRCode,
ManualCode,
Ethernet
Ethernet,
Ble,
};

enum class PairingNetworkType
{
None,
WiFi,
Thread,
Ethernet,
};

class PairingCommandBridge : public CHIPCommandBridge
{
public:
PairingCommandBridge(const char * commandName, PairingMode mode) : CHIPCommandBridge(commandName), mPairingMode(mode)
PairingCommandBridge(const char * commandName, PairingMode mode, PairingNetworkType networkType) :
CHIPCommandBridge(commandName), mPairingMode(mode), mNetworkType(networkType)
{
AddArgument("node-id", 0, UINT64_MAX, &mNodeId);
switch (networkType)
{
case PairingNetworkType::None:
case PairingNetworkType::Ethernet:
break;
case PairingNetworkType::WiFi:
AddArgument("ssid", &mSSID);
AddArgument("password", &mPassword);
break;
case PairingNetworkType::Thread:
AddArgument("operationalDataset", &mOperationalDataset);
break;
}

switch (mode)
{
Expand All @@ -49,6 +72,10 @@ class PairingCommandBridge : public CHIPCommandBridge
AddArgument("device-remote-ip", &ipAddress);
AddArgument("device-remote-port", 0, UINT16_MAX, &mRemotePort);
break;
case PairingMode::Ble:
AddArgument("setup-pin-code", 0, 134217727, &mSetupPINCode);
AddArgument("discriminator", 0, 4096, &mDiscriminator);
break;
}
}

Expand All @@ -58,10 +85,15 @@ class PairingCommandBridge : public CHIPCommandBridge

private:
void PairWithCode(NSError * __autoreleasing * error);
void PairWithPayload(NSError * __autoreleasing * error);
void PairWithIPAddress(NSError * __autoreleasing * error);
void SetUpPairingDelegate();

const PairingMode mPairingMode;
const PairingNetworkType mNetworkType;
chip::ByteSpan mOperationalDataset;
chip::ByteSpan mSSID;
chip::ByteSpan mPassword;
chip::NodeId mNodeId;
uint16_t mRemotePort;
uint16_t mDiscriminator;
Expand Down
34 changes: 31 additions & 3 deletions examples/chip-tool-darwin/commands/pairing/PairingCommandBridge.mm
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
*
*/

#import <CHIP/CHIPCommissioningParameters.h>
#import <CHIP/CHIPError_Internal.h>
#import <setup_payload/ManualSetupPayloadGenerator.h>
#import <setup_payload/SetupPayload.h>

#include "../common/CHIPCommandBridge.h"
#include "PairingCommandBridge.h"
Expand All @@ -31,9 +34,25 @@
{
dispatch_queue_t callbackQueue = dispatch_queue_create("com.chip.pairing", DISPATCH_QUEUE_SERIAL);
CHIPToolPairingDelegate * pairing = [[CHIPToolPairingDelegate alloc] init];
CHIPCommissioningParameters * params = [[CHIPCommissioningParameters alloc] init];

pairing.deviceID = mNodeId;
pairing.commandBridge = this;
[pairing setDeviceID:mNodeId];
switch (mNetworkType) {
case PairingNetworkType::None:
case PairingNetworkType::Ethernet:
break;
case PairingNetworkType::WiFi:
[params setWifiSSID:[NSData dataWithBytes:mSSID.data() length:mSSID.size()]];
[params setWifiCredentials:[NSData dataWithBytes:mPassword.data() length:mPassword.size()]];
break;
case PairingNetworkType::Thread:
[params setThreadOperationalDataset:[NSData dataWithBytes:mOperationalDataset.data() length:mOperationalDataset.size()]];
break;
}

[pairing setCommandBridge:this];
[pairing setParams:params];
[pairing setCommissioner:CurrentCommissioner()];

[CurrentCommissioner() setPairingDelegate:pairing queue:callbackQueue];
}
Expand All @@ -44,17 +63,26 @@
switch (mPairingMode) {
case PairingMode::QRCode:
case PairingMode::ManualCode:
PairWithCode(&error);
PairWithPayload(&error);
break;
case PairingMode::Ethernet:
PairWithIPAddress(&error);
break;
case PairingMode::Ble:
PairWithCode(&error);
break;
}

return [CHIPError errorToCHIPErrorCode:error];
}

void PairingCommandBridge::PairWithCode(NSError * __autoreleasing * error)
{
SetUpPairingDelegate();
[CurrentCommissioner() pairDevice:mNodeId discriminator:mDiscriminator setupPINCode:mSetupPINCode error:error];
}

void PairingCommandBridge::PairWithPayload(NSError * __autoreleasing * error)
{
NSString * payload = [NSString stringWithUTF8String:mOnboardingPayload];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
@interface CHIPToolPairingDelegate : NSObject <CHIPDevicePairingDelegate>
@property PairingCommandBridge * commandBridge;
@property chip::NodeId deviceID;
@property CHIPDeviceController * commissioner;
@property CHIPCommissioningParameters * params;

- (void)onPairingComplete:(NSError *)error;
- (void)onPairingDeleted:(NSError *)error;
- (void)onCommissioningComplete:(NSError *)error;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/

#include "PairingDelegateBridge.h"
#import <CHIP/CHIPCommissioningParameters.h>
#import <CHIP/CHIPError_Internal.h>

@interface CHIPToolPairingDelegate ()
Expand All @@ -41,10 +42,19 @@ - (void)onStatusUpdate:(CHIPPairingStatus)status

- (void)onPairingComplete:(NSError *)error
{
NSError * __block commissionError;
CHIP_ERROR err = [CHIPError errorToCHIPErrorCode:error];
if (err != CHIP_NO_ERROR) {
_commandBridge->SetCommandExitStatus(err);
return;
}
ChipLogProgress(chipTool, "Pairing Complete: %s", chip::ErrorStr(err));

_commandBridge->SetCommandExitStatus(err);
[_commissioner commissionDevice:_deviceID commissioningParams:_params error:&commissionError];
err = [CHIPError errorToCHIPErrorCode:commissionError];
if (err != CHIP_NO_ERROR) {
_commandBridge->SetCommandExitStatus(err);
return;
}
}

- (void)onPairingDeleted:(NSError *)error
Expand Down
1 change: 1 addition & 0 deletions src/darwin/Framework/CHIP/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ static_library("framework") {
sources = [
"CHIP.h",
"CHIPCluster.mm",
"CHIPCommissioningParameters.m",
"CHIPControllerAccessControl.h",
"CHIPControllerAccessControl.mm",
"CHIPDevice.h",
Expand Down

0 comments on commit 3310009

Please sign in to comment.