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 Mar 1, 2023
1 parent eff5773 commit 2f42132
Show file tree
Hide file tree
Showing 15 changed files with 293 additions and 29 deletions.
4 changes: 3 additions & 1 deletion examples/chip-tool/commands/discover/Commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ void registerCommandsDiscover(Commands & commands, CredentialIssuerCommands * cr

commands_list clusterCommands = {
make_unique<Resolve>(credsIssuerConfig),
make_unique<DiscoverCommissionablesCommand>(credsIssuerConfig),
make_unique<DiscoverCommissionablesStartCommand>(credsIssuerConfig),
make_unique<DiscoverCommissionablesStopCommand>(credsIssuerConfig),
make_unique<DiscoverCommissionableCommand>(credsIssuerConfig),
make_unique<DiscoverCommissionableByShortDiscriminatorCommand>(credsIssuerConfig),
make_unique<DiscoverCommissionableByLongDiscriminatorCommand>(credsIssuerConfig),
make_unique<DiscoverCommissionableByCommissioningModeCommand>(credsIssuerConfig),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,39 @@ void DiscoverCommissionablesCommandBase::OnDiscoveredDevice(const chip::Dnssd::D
}
}

CHIP_ERROR DiscoverCommissionablesCommand::RunCommand()
CHIP_ERROR DiscoverCommissionablesStartCommand::RunCommand()
{
ReturnErrorOnFailure(CurrentCommissioner().StartDiscoverDevices(this));

// In interactive mode, return early to allow for other commands.
if (IsInteractive())
{
SetCommandExitStatus(CHIP_NO_ERROR);
}

return CHIP_NO_ERROR;
}

void DiscoverCommissionablesStartCommand::OnNodeDiscovered(uint16_t discriminator, VendorId vendorId, uint16_t productId,
Optional<Dnssd::DiscoveredNodeData> nodeData)
{
ChipLogProgress(chipTool, "OnNodeDiscovered (%s): discriminator: %u, vendorId: %u, productId: %u",
nodeData.HasValue() ? "IP" : "BLE", discriminator, vendorId, productId);

if (nodeData.HasValue())
{
nodeData.Value().LogDetail();
}
}

CHIP_ERROR DiscoverCommissionablesStopCommand::RunCommand()
{
ReturnErrorOnFailure(CurrentCommissioner().StopDiscoverDevices());
SetCommandExitStatus(CHIP_NO_ERROR);
return CHIP_NO_ERROR;
}

CHIP_ERROR DiscoverCommissionableCommand::RunCommand()
{
mCommissioner = &CurrentCommissioner();
mCommissioner->RegisterDeviceDiscoveryDelegate(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,36 @@ class DiscoverCommissionablesCommandBase : public CHIPCommand, public chip::Cont
chip::Optional<bool> mDiscoverOnce;
};

class DiscoverCommissionablesCommand : public DiscoverCommissionablesCommandBase
class DiscoverCommissionablesStartCommand : public CHIPCommand, public chip::Controller::SetUpCodePairerDiscoveryDelegate
{
public:
DiscoverCommissionablesCommand(CredentialIssuerCommands * credsIssuerConfig) :
DiscoverCommissionablesCommandBase("commissionables", credsIssuerConfig)
DiscoverCommissionablesStartCommand(CredentialIssuerCommands * credIssuerCommands) : CHIPCommand("start", credIssuerCommands) {}

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

/////////// SetUpCodePairerDiscoveryDelegate Interface /////////
void OnNodeDiscovered(uint16_t discriminator, chip::VendorId vendorId, uint16_t productId,
chip::Optional<chip::Dnssd::DiscoveredNodeData> nodeData =
chip::Optional<chip::Dnssd::DiscoveredNodeData>::Missing()) override;
};

class DiscoverCommissionablesStopCommand : public CHIPCommand
{
public:
DiscoverCommissionablesStopCommand(CredentialIssuerCommands * credIssuerCommands) : CHIPCommand("stop", credIssuerCommands) {}

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

class DiscoverCommissionableCommand : public DiscoverCommissionablesCommandBase
{
public:
DiscoverCommissionableCommand(CredentialIssuerCommands * credsIssuerConfig) :
DiscoverCommissionablesCommandBase("find-commissionable", credsIssuerConfig)
{}

/////////// CHIPCommand Interface /////////
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
'alias': 'discover',
'commands': {
'FindCommissionable': {
'alias': 'commissionables',
'has_destination': False,
'has_endpoint': False,
},
Expand Down
12 changes: 12 additions & 0 deletions src/controller/CHIPDeviceController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,18 @@ CHIP_ERROR DeviceCommissioner::GetDeviceBeingCommissioned(NodeId deviceId, Commi
return CHIP_NO_ERROR;
}

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

CHIP_ERROR DeviceCommissioner::StopDiscoverDevices()
{
MATTER_TRACE_EVENT_SCOPE("StopDiscoverDevices", "DeviceCommissioner");
return mSetUpCodePairer.StopDiscoverDevices();
}

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

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

/**
* @brief
* Stop commissionable CHIP devices discovery.
*
*/
CHIP_ERROR StopDiscoverDevices();

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

CHIP_ERROR SetUpCodePairer::StartDiscoverDevices(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
#if CONFIG_NETWORK_LAYER_BLE
VerifyOrReturnError(mBleLayer != nullptr, CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE);
ReturnErrorOnFailure(chip::DeviceLayer::PlatformMgrImpl().PrepareCommissioning(this));
#endif // CONFIG_NETWORK_LAYER_BLE
#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::StopDiscoverDevices()
{
VerifyOrReturnError(mSystemLayer != nullptr, CHIP_ERROR_INCORRECT_STATE);
VerifyOrReturnError(mDelegate != nullptr, CHIP_ERROR_INCORRECT_STATE);

mDelegate = nullptr;

if (mDiscoveryType == DiscoveryType::kAll)
{
#if CHIP_DEVICE_LAYER_TARGET_DARWIN
#if CONFIG_NETWORK_LAYER_BLE
VerifyOrReturnError(mBleLayer != nullptr, CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE);
ReturnErrorOnFailure(mBleLayer->CancelBleIncompleteConnection());
#endif // CONFIG_NETWORK_LAYER_BLE
#endif // CHIP_DEVICE_LAYER_TARGET_DARWIN
}

return mCommissioner->StopCommissionableDiscovery();
}

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 +386,18 @@ bool SetUpCodePairer::NodeMatchesCurrentFilter(const Dnssd::DiscoveredNodeData &

void SetUpCodePairer::NotifyCommissionableDeviceDiscovered(const Dnssd::DiscoveredNodeData & nodeData)
{
if (mConnectionType == SetupCodePairerBehaviour::kDiscover)
{
if (nullptr != mDelegate)
{
auto discriminator = nodeData.commissionData.longDiscriminator;
auto vendorId = static_cast<chip::VendorId>(nodeData.commissionData.vendorId);
auto productId = nodeData.commissionData.productId;
mDelegate->OnNodeDiscovered(discriminator, vendorId, productId, MakeOptional<Dnssd::DiscoveredNodeData>(nodeData));
}
return;
}

if (!NodeMatchesCurrentFilter(nodeData))
{
return;
Expand Down
36 changes: 33 additions & 3 deletions src/controller/SetUpCodePairer.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@
#include <ble/BleLayer.h>
#endif // CONFIG_NETWORK_BLE

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

#include <controller/DeviceDiscoveryDelegate.h>

#include <deque>
Expand All @@ -48,6 +52,15 @@ namespace Controller {

class DeviceCommissioner;

class SetUpCodePairerDiscoveryDelegate
{
public:
virtual ~SetUpCodePairerDiscoveryDelegate() {}
virtual void
OnNodeDiscovered(uint16_t discriminator, VendorId vendorId, uint16_t productId,
Optional<Dnssd::DiscoveredNodeData> nodeData = Optional<Dnssd::DiscoveredNodeData>::Missing()) = 0;
};

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

enum class SetupCodePairerBehaviour : uint8_t
{
kDiscover,
kCommission,
kPaseOnly,
};
Expand All @@ -73,11 +87,18 @@ enum class DiscoveryType : uint8_t
};

class DLL_EXPORT SetUpCodePairer : public DevicePairingDelegate
#if CHIP_DEVICE_LAYER_TARGET_DARWIN
,
public DeviceLayer::PrepareCommissioningDelegate
#endif // CHIP_DEVICE_LAYER_TARGET_DARWIN
{
public:
SetUpCodePairer(DeviceCommissioner * commissioner) : mCommissioner(commissioner) { ResetDiscoveryState(); }
virtual ~SetUpCodePairer() {}

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

CHIP_ERROR PairDevice(chip::NodeId remoteId, const char * setUpCode,
SetupCodePairerBehaviour connectionType = SetupCodePairerBehaviour::kCommission,
DiscoveryType discoveryType = DiscoveryType::kAll);
Expand All @@ -102,6 +123,14 @@ class DLL_EXPORT SetUpCodePairer : public DevicePairingDelegate
void OnPairingDeleted(CHIP_ERROR error) override;
void OnCommissioningComplete(NodeId deviceId, CHIP_ERROR error) override;

#if CHIP_DEVICE_LAYER_TARGET_DARWIN
void OnNodeDiscovered(uint16_t discriminator, chip::VendorId vendorId, uint16_t productId) override
{
VerifyOrReturn(nullptr != mDelegate);
mDelegate->OnNodeDiscovered(discriminator, vendorId, productId);
}
#endif // CHIP_DEVICE_LAYER_TARGET_DARWIN

CHIP_ERROR Connect(SetupPayload & paload);
CHIP_ERROR StartDiscoverOverBle(SetupPayload & payload);
CHIP_ERROR StopConnectOverBle();
Expand Down Expand Up @@ -176,9 +205,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
Loading

0 comments on commit 2f42132

Please sign in to comment.