Skip to content

Commit

Permalink
[SetUpCodePairer] Add an API to list the discovered devices if any
Browse files Browse the repository at this point in the history
  • Loading branch information
vivien-apple committed Feb 28, 2023
1 parent d6a783e commit 8fa70f1
Show file tree
Hide file tree
Showing 15 changed files with 218 additions and 12 deletions.
1 change: 1 addition & 0 deletions examples/chip-tool/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ static_library("chip-tool-utils") {
"${chip_root}/src/controller/ExamplePersistentStorage.h",
"commands/pairing/CloseSessionCommand.cpp",
"commands/pairing/CloseSessionCommand.h",
"commands/pairing/DiscoverDevicesCommand.cpp",
"commands/pairing/OpenCommissioningWindowCommand.cpp",
"commands/pairing/OpenCommissioningWindowCommand.h",
"commands/payload/AdditionalDataParseCommand.cpp",
Expand Down
2 changes: 2 additions & 0 deletions examples/chip-tool/commands/pairing/Commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "commands/common/Commands.h"
#include "commands/pairing/CloseSessionCommand.h"
#include "commands/pairing/CommissionedListCommand.h"
#include "commands/pairing/DiscoverDevicesCommand.h"
#include "commands/pairing/GetCommissionerNodeIdCommand.h"
#include "commands/pairing/OpenCommissioningWindowCommand.h"
#include "commands/pairing/PairingCommand.h"
Expand Down Expand Up @@ -222,6 +223,7 @@ void registerCommandsPairing(Commands & commands, CredentialIssuerCommands * cre
make_unique<OpenCommissioningWindowCommand>(credsIssuerConfig),
make_unique<CloseSessionCommand>(credsIssuerConfig),
make_unique<GetCommissionerNodeIdCommand>(credsIssuerConfig),
make_unique<DiscoverDevicesCommand>(credsIssuerConfig),
};

commands.Register(clusterName, clusterCommands);
Expand Down
46 changes: 46 additions & 0 deletions examples/chip-tool/commands/pairing/DiscoverDevicesCommand.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (c) 2023 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

#include "DiscoverDevicesCommand.h"

using namespace chip;

CHIP_ERROR DiscoverDevicesCommand::RunCommand()
{
return CurrentCommissioner().DiscoverDevices(this);
}

void DiscoverDevicesCommand::OnNodeDiscovered(const chip::Dnssd::DiscoveredNodeData & nodeData)
{
auto discriminator = nodeData.commissionData.longDiscriminator;
auto vendorId = static_cast<chip::VendorId>(nodeData.commissionData.vendorId);
auto productId = nodeData.commissionData.productId;

ChipLogDetail(chipTool, "IP:: OnDeviceDiscovered: discriminator: %u, vendorId: %u, productId: %u", discriminator, vendorId,
productId);

nodeData.LogDetail();
}

#if CHIP_DEVICE_LAYER_TARGET_DARWIN
void DiscoverDevicesCommand::OnNodeDiscovered(uint16_t discriminator, chip::VendorId vendorId, uint16_t productId)
{
ChipLogDetail(chipTool, "BLE:: OnDeviceDiscovered: discriminator: %u, vendorId: %u, productId: %u", discriminator, vendorId,
productId);
}
#endif // CHIP_DEVICE_LAYER_TARGET_DARWIN
37 changes: 37 additions & 0 deletions examples/chip-tool/commands/pairing/DiscoverDevicesCommand.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2023 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

#pragma once

#include "../common/CHIPCommand.h"

class DiscoverDevicesCommand : public CHIPCommand, public chip::Controller::SetUpCodePairerDiscoveryDelegate
{
public:
DiscoverDevicesCommand(CredentialIssuerCommands * credIssuerCommands) : CHIPCommand("discover", credIssuerCommands) {}

/////////// CHIPCommand Interface /////////
CHIP_ERROR RunCommand() override;
chip::System::Clock::Timeout GetWaitDuration() const override { return chip::System::Clock::Seconds16(120); }

/////////// DiscoverCommissionablesDelegate Interface /////////
void OnNodeDiscovered(const chip::Dnssd::DiscoveredNodeData & nodeData) override;
#if CHIP_DEVICE_LAYER_TARGET_DARWIN
void OnNodeDiscovered(uint16_t discriminator, chip::VendorId vendorId, uint16_t productId) override;
#endif // CHIP_DEVICE_LAYER_TARGET_DARWIN
};
6 changes: 6 additions & 0 deletions src/controller/CHIPDeviceController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,12 @@ CHIP_ERROR DeviceCommissioner::GetDeviceBeingCommissioned(NodeId deviceId, Commi
return CHIP_NO_ERROR;
}

CHIP_ERROR DeviceCommissioner::DiscoverDevices(SetUpCodePairerDiscoveryDelegate * delegate, DiscoveryType discoveryType)
{
MATTER_TRACE_EVENT_SCOPE("DiscoverDevices", "DeviceCommissioner");
return mSetUpCodePairer.DiscoverDevices(delegate, discoveryType);
}

CHIP_ERROR DeviceCommissioner::PairDevice(NodeId remoteDeviceId, const char * setUpCode, const CommissioningParameters & params,
DiscoveryType discoveryType)
{
Expand Down
9 changes: 9 additions & 0 deletions src/controller/CHIPDeviceController.h
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,15 @@ class DLL_EXPORT DeviceCommissioner : public DeviceController,
void Shutdown() override;

// ----- Connection Management -----
/**
* @brief
* Discover commissionable CHIP devices.
*
* @param[in] delegate An object to receive notifications for discovered nodes.
* @param[in] discoveryType The network discovery type, defaults to DiscoveryType::kAll.
*/
CHIP_ERROR DiscoverDevices(SetUpCodePairerDiscoveryDelegate * delegate, DiscoveryType discoveryType = DiscoveryType::kAll);

/**
* @brief
* Pair a CHIP device with the provided code. The code can be either a QRCode
Expand Down
33 changes: 33 additions & 0 deletions src/controller/SetUpCodePairer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,33 @@ constexpr uint32_t kDeviceDiscoveredTimeout = CHIP_CONFIG_SETUP_CODE_PAIRER_DISC
namespace chip {
namespace Controller {

CHIP_ERROR SetUpCodePairer::DiscoverDevices(SetUpCodePairerDiscoveryDelegate * delegate, DiscoveryType discoveryType)
{
VerifyOrReturnError(mSystemLayer != nullptr, CHIP_ERROR_INCORRECT_STATE);

ResetDiscoveryState();

mConnectionType = SetupCodePairerBehaviour::kDiscover;
mDiscoveryType = discoveryType;
mDelegate = delegate;

if (mDiscoveryType == DiscoveryType::kAll)
{
#if CHIP_DEVICE_LAYER_TARGET_DARWIN
ReturnErrorOnFailure(chip::DeviceLayer::PlatformMgrImpl().PrepareCommissioning(mDelegate));
#endif // CHIP_DEVICE_LAYER_TARGET_DARWIN
}

// We always want to search on network because any node that has already been commissioned will use on-network regardless of the
// QR code flag.
Dnssd::DiscoveryFilter filter(Dnssd::DiscoveryFilterType::kNone, (uint64_t) 0);
return mCommissioner->DiscoverCommissionableNodes(filter);
}

CHIP_ERROR SetUpCodePairer::PairDevice(NodeId remoteId, const char * setUpCode, SetupCodePairerBehaviour commission,
DiscoveryType discoveryType)
{
VerifyOrReturnError(commission != SetupCodePairerBehaviour::kDiscover, CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrReturnError(mSystemLayer != nullptr, CHIP_ERROR_INCORRECT_STATE);

SetupPayload payload;
Expand Down Expand Up @@ -340,6 +364,15 @@ bool SetUpCodePairer::NodeMatchesCurrentFilter(const Dnssd::DiscoveredNodeData &

void SetUpCodePairer::NotifyCommissionableDeviceDiscovered(const Dnssd::DiscoveredNodeData & nodeData)
{
if (mConnectionType == SetupCodePairerBehaviour::kDiscover)
{
if (nullptr != mDelegate)
{
mDelegate->OnNodeDiscovered(nodeData);
}
return;
}

if (!NodeMatchesCurrentFilter(nodeData))
{
return;
Expand Down
28 changes: 25 additions & 3 deletions src/controller/SetUpCodePairer.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,31 @@

#include <controller/DeviceDiscoveryDelegate.h>

#if CHIP_DEVICE_LAYER_TARGET_DARWIN
#include <platform/Darwin/PrepareCommissioningDelegate.h>
#endif

#include <deque>

namespace chip {
namespace Controller {

class DeviceCommissioner;

class SetUpCodePairerDiscoveryDelegate : public Dnssd::CommissioningResolveDelegate
#if CHIP_DEVICE_LAYER_TARGET_DARWIN
,
public DeviceLayer::PrepareCommissioningDelegate
#endif // CHIP_DEVICE_LAYER_TARGET_DARWIN
{
public:
using Dnssd::CommissioningResolveDelegate::OnNodeDiscovered;
#if CHIP_DEVICE_LAYER_TARGET_DARWIN
using DeviceLayer::PrepareCommissioningDelegate::OnNodeDiscovered;
#endif // CHIP_DEVICE_LAYER_TARGET_DARWIN
virtual ~SetUpCodePairerDiscoveryDelegate() {}
};

class SetUpCodePairerParameters : public RendezvousParameters
{
public:
Expand All @@ -61,6 +79,7 @@ class SetUpCodePairerParameters : public RendezvousParameters

enum class SetupCodePairerBehaviour : uint8_t
{
kDiscover,
kCommission,
kPaseOnly,
};
Expand All @@ -78,6 +97,8 @@ class DLL_EXPORT SetUpCodePairer : public DevicePairingDelegate
SetUpCodePairer(DeviceCommissioner * commissioner) : mCommissioner(commissioner) { ResetDiscoveryState(); }
virtual ~SetUpCodePairer() {}

CHIP_ERROR DiscoverDevices(SetUpCodePairerDiscoveryDelegate * delegate, DiscoveryType discoveryType = DiscoveryType::kAll);

CHIP_ERROR PairDevice(chip::NodeId remoteId, const char * setUpCode,
SetupCodePairerBehaviour connectionType = SetupCodePairerBehaviour::kCommission,
DiscoveryType discoveryType = DiscoveryType::kAll);
Expand Down Expand Up @@ -176,9 +197,10 @@ class DLL_EXPORT SetUpCodePairer : public DevicePairingDelegate
DeviceCommissioner * mCommissioner = nullptr;
System::Layer * mSystemLayer = nullptr;
chip::NodeId mRemoteId;
uint32_t mSetUpPINCode = 0;
SetupCodePairerBehaviour mConnectionType = SetupCodePairerBehaviour::kCommission;
DiscoveryType mDiscoveryType = DiscoveryType::kAll;
uint32_t mSetUpPINCode = 0;
SetupCodePairerBehaviour mConnectionType = SetupCodePairerBehaviour::kCommission;
DiscoveryType mDiscoveryType = DiscoveryType::kAll;
SetUpCodePairerDiscoveryDelegate * mDelegate = nullptr;

// While we are trying to pair, we intercept the DevicePairingDelegate
// notifications from mCommissioner. We want to make sure we send them on
Expand Down
4 changes: 2 additions & 2 deletions src/platform/Darwin/BLEManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ void BLEManagerImpl::_Shutdown()
}
}

CHIP_ERROR BLEManagerImpl::PrepareConnection()
CHIP_ERROR BLEManagerImpl::PrepareConnection(PrepareCommissioningDelegate * delegate)
{
if (mConnectionDelegate)
{
static_cast<BleConnectionDelegateImpl *>(mConnectionDelegate)->PrepareConnection();
static_cast<BleConnectionDelegateImpl *>(mConnectionDelegate)->PrepareConnection(delegate);
return CHIP_NO_ERROR;
}
return CHIP_ERROR_INCORRECT_STATE;
Expand Down
4 changes: 3 additions & 1 deletion src/platform/Darwin/BLEManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

#if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE

#include "PrepareCommissioningDelegate.h"

namespace chip {
namespace DeviceLayer {
namespace Internal {
Expand All @@ -42,7 +44,7 @@ class BLEManagerImpl final : public BLEManager, private BleLayer

public:
CHIP_ERROR ConfigureBle(uint32_t aNodeId, bool aIsCentral) { return CHIP_NO_ERROR; }
CHIP_ERROR PrepareConnection();
CHIP_ERROR PrepareConnection(PrepareCommissioningDelegate * delegate = nullptr);

private:
// ===== Members that implement the BLEManager internal interface.
Expand Down
2 changes: 1 addition & 1 deletion src/platform/Darwin/BleConnectionDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace Internal {
class BleConnectionDelegateImpl : public Ble::BleConnectionDelegate
{
public:
void PrepareConnection();
void PrepareConnection(PrepareCommissioningDelegate * delegate = nullptr);
virtual void NewConnection(Ble::BleLayer * bleLayer, void * appState, const SetupDiscriminator & connDiscriminator);
virtual CHIP_ERROR CancelConnection();
};
Expand Down
17 changes: 15 additions & 2 deletions src/platform/Darwin/BleConnectionDelegateImpl.mm
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ @interface BleConnection : NSObject <CBCentralManagerDelegate, CBPeripheralDeleg
@property (unsafe_unretained, nonatomic) void * appState;
@property (unsafe_unretained, nonatomic) BleConnectionDelegate::OnConnectionCompleteFunct onConnectionComplete;
@property (unsafe_unretained, nonatomic) BleConnectionDelegate::OnConnectionErrorFunct onConnectionError;
@property (unsafe_unretained, nonatomic) chip::DeviceLayer::PrepareCommissioningDelegate * prepareCommissioningDelegate;
@property (unsafe_unretained, nonatomic) chip::Ble::BleLayer * mBleLayer;

- (id)initWithDiscriminator:(const chip::SetupDiscriminator &)deviceDiscriminator;
Expand Down Expand Up @@ -85,6 +86,7 @@ - (void)update;
ble.appState = appState;
ble.onConnectionComplete = OnConnectionComplete;
ble.onConnectionError = OnConnectionError;
ble.prepareCommissioningDelegate = nil;
[ble updateWithDiscriminator:deviceDiscriminator];
return;
}
Expand All @@ -95,10 +97,11 @@ - (void)update;
ble.appState = appState;
ble.onConnectionComplete = OnConnectionComplete;
ble.onConnectionError = OnConnectionError;
ble.prepareCommissioningDelegate = nil;
ble.centralManager = [ble.centralManager initWithDelegate:ble queue:ble.workQueue];
}

void BleConnectionDelegateImpl::PrepareConnection()
void BleConnectionDelegateImpl::PrepareConnection(PrepareCommissioningDelegate * delegate)
{
ChipLogProgress(Ble, "%s", __FUNCTION__);

Expand All @@ -113,6 +116,7 @@ - (void)update;
ble = [[BleConnection alloc] init];
ble.onConnectionComplete = OnConnectionComplete;
ble.onConnectionError = OnConnectionError;
ble.prepareCommissioningDelegate = delegate;
ble.centralManager = [ble.centralManager initWithDelegate:ble queue:ble.workQueue];
}

Expand Down Expand Up @@ -251,7 +255,16 @@ - (void)centralManager:(CBCentralManager *)central

if (opCode == 0 || opCode == 1) {
if (![self hasDiscriminator]) {
ChipLogProgress(Ble, "Storing device %p with discriminator: %d", peripheral, discriminator);
if ([_cachedPeripherals objectForKey:@(discriminator)] == nil) {
ChipLogProgress(Ble, "Storing device %p with discriminator: %d", peripheral, discriminator);
if (self.prepareCommissioningDelegate != nil) {
dispatch_async(_chipWorkQueue, ^{
auto vendorId = static_cast<chip::VendorId>((bytes[3] | (bytes[4] << 8)));
auto productId = static_cast<uint16_t>((bytes[5] | (bytes[6] << 8)));
self.prepareCommissioningDelegate->OnNodeDiscovered(discriminator, vendorId, productId);
});
}
}
_cachedPeripherals[@(discriminator)] = peripheral;
} else if ([self checkDiscriminator:discriminator]) {
ChipLogProgress(Ble, "Connecting to device %p with discriminator: %d", peripheral, discriminator);
Expand Down
4 changes: 2 additions & 2 deletions src/platform/Darwin/PlatformManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,11 @@ bool PlatformManagerImpl::_IsChipStackLockedByCurrentThread() const
};
#endif

CHIP_ERROR PlatformManagerImpl::PrepareCommissioning()
CHIP_ERROR PlatformManagerImpl::PrepareCommissioning(PrepareCommissioningDelegate * delegate)
{
auto error = CHIP_NO_ERROR;
#if CONFIG_NETWORK_LAYER_BLE
error = Internal::BLEMgrImpl().PrepareConnection();
ReturnErrorOnFailure(Internal::BLEMgrImpl().PrepareConnection(delegate));
#endif // CONFIG_NETWORK_LAYER_BLE
return error;
}
Expand Down
3 changes: 2 additions & 1 deletion src/platform/Darwin/PlatformManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#pragma once

#include "PrepareCommissioningDelegate.h"
#include <dispatch/dispatch.h>
#include <platform/internal/GenericPlatformManagerImpl.h>

Expand Down Expand Up @@ -54,7 +55,7 @@ class PlatformManagerImpl final : public PlatformManager, public Internal::Gener
return mWorkQueue;
}

CHIP_ERROR PrepareCommissioning();
CHIP_ERROR PrepareCommissioning(PrepareCommissioningDelegate * delegate = nullptr);

System::Clock::Timestamp GetStartTime() { return mStartTime; }

Expand Down
Loading

0 comments on commit 8fa70f1

Please sign in to comment.