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 authored and pull[bot] committed Jan 10, 2024
1 parent 575d3c1 commit 1419608
Show file tree
Hide file tree
Showing 27 changed files with 632 additions and 68 deletions.
1 change: 1 addition & 0 deletions examples/chip-tool/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ static_library("chip-tool-utils") {
"commands/common/Commands.cpp",
"commands/common/Commands.h",
"commands/common/CredentialIssuerCommands.h",
"commands/common/DeviceScanner.cpp",
"commands/common/HexConversion.h",
"commands/common/RemoteDataModelLogger.cpp",
"commands/common/RemoteDataModelLogger.h",
Expand Down
120 changes: 120 additions & 0 deletions examples/chip-tool/commands/common/DeviceScanner.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
* 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 "DeviceScanner.h"

using namespace chip;
using namespace chip::Ble;
using namespace chip::Dnssd;

CHIP_ERROR DeviceScanner::Start()
{
mDiscoveredResults.clear();

#if CHIP_TOOL_DEVICE_SCANNER_USE_BLE
ReturnErrorOnFailure(DeviceLayer::PlatformMgrImpl().StartBleScan(this));
#endif // CHIP_TOOL_DEVICE_SCANNER_USE_BLE

ReturnErrorOnFailure(mDNSResolver.Init(DeviceLayer::UDPEndPointManager()));
mDNSResolver.SetCommissioningDelegate(this);

DiscoveryFilter filter(DiscoveryFilterType::kNone, (uint64_t) 0);
return mDNSResolver.DiscoverCommissionableNodes(filter);
}

CHIP_ERROR DeviceScanner::Stop()
{
#if CHIP_TOOL_DEVICE_SCANNER_USE_BLE
ReturnErrorOnFailure(DeviceLayer::PlatformMgrImpl().StopBleScan());
#endif // CHIP_TOOL_DEVICE_SCANNER_USE_BLE

mDNSResolver.SetCommissioningDelegate(nullptr);
ReturnErrorOnFailure(mDNSResolver.StopDiscovery());
mDNSResolver.Shutdown();
return CHIP_NO_ERROR;
}

void DeviceScanner::OnNodeDiscovered(const DiscoveredNodeData & nodeData)
{
auto & commissionData = nodeData.commissionData;

auto discriminator = commissionData.longDiscriminator;
auto vendorId = static_cast<VendorId>(commissionData.vendorId);
auto productId = commissionData.productId;

ChipLogProgress(chipTool, "OnNodeDiscovered (MDNS): discriminator: %u, vendorId: %u, productId: %u", discriminator, vendorId,
productId);

auto & resolutionData = nodeData.resolutionData;
for (size_t i = 0; i < resolutionData.numIPs; i++)
{
auto params = Controller::SetUpCodePairerParameters(resolutionData, i);
DeviceScannerResult result = { params, vendorId, productId, discriminator };
mDiscoveredResults.push_back(result);
}

nodeData.LogDetail();
}

#if CHIP_TOOL_DEVICE_SCANNER_USE_BLE
void DeviceScanner::OnBleScanResult(BLE_CONNECTION_OBJECT connObj, const ChipBLEDeviceIdentificationInfo & info)
{
auto discriminator = info.GetDeviceDiscriminator();
auto vendorId = static_cast<VendorId>(info.GetVendorId());
auto productId = info.GetProductId();

ChipLogProgress(chipTool, "OnNodeDiscovered (BLE): discriminator: %u, vendorId: %u, productId: %u", discriminator, vendorId,
productId);

auto params = Controller::SetUpCodePairerParameters(connObj, false /* connected */);
DeviceScannerResult result = { params, vendorId, productId, discriminator };
mDiscoveredResults.push_back(result);
}
#endif // CHIP_TOOL_DEVICE_SCANNER_USE_BLE

CHIP_ERROR DeviceScanner::Get(uint16_t index, RendezvousParameters & params)
{
VerifyOrReturnError(index < mDiscoveredResults.size(), CHIP_ERROR_NOT_FOUND);

auto & result = mDiscoveredResults.at(index);
params = result.mParams;
return CHIP_NO_ERROR;
}

void DeviceScanner::Log() const
{
auto resultsCount = mDiscoveredResults.size();
VerifyOrReturn(resultsCount > 0, ChipLogProgress(chipTool, "No device discovered."));

uint16_t index = 0;
for (auto & result : mDiscoveredResults)
{
char addr[Transport::PeerAddress::kMaxToStringSize];
result.mParams.GetPeerAddress().ToString(addr);

ChipLogProgress(chipTool, "\t %u - Discriminator: %u - Vendor: %u - Product: %u - %s", index, result.mDiscriminator,
result.mVendorId, result.mProductId, addr);
index++;
}
}

DeviceScanner & GetDeviceScanner()
{
static DeviceScanner scanner;
return scanner;
}
70 changes: 70 additions & 0 deletions examples/chip-tool/commands/common/DeviceScanner.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* 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 <controller/CHIPDeviceController.h>
#include <lib/dnssd/ResolverProxy.h>

#include <vector>

#if CONFIG_NETWORK_LAYER_BLE
#if CHIP_DEVICE_LAYER_TARGET_DARWIN
#include <platform/Darwin/BleScannerDelegate.h>
#define CHIP_TOOL_DEVICE_SCANNER_USE_BLE 1
#endif // CHIP_DEVICE_LAYER_TARGET_DARWIN
#endif // CONFIG_NETWORK_LAYER_BLE

#ifndef CHIP_TOOL_DEVICE_SCANNER_USE_BLE
#define CHIP_TOOL_DEVICE_SCANNER_USE_BLE 0
#endif // CHIP_TOOL_DEVICE_SCANNER_USE_BLE

struct DeviceScannerResult
{
chip::Controller::SetUpCodePairerParameters mParams;
chip::VendorId mVendorId;
uint16_t mProductId;
uint16_t mDiscriminator;
};

class DeviceScanner : public chip::Dnssd::CommissioningResolveDelegate
#if CHIP_TOOL_DEVICE_SCANNER_USE_BLE
,
public chip::DeviceLayer::BleScannerDelegate
#endif // CHIP_TOOL_DEVICE_SCANNER_USE_BLE
{
public:
CHIP_ERROR Start();
CHIP_ERROR Stop();
CHIP_ERROR Get(uint16_t index, chip::RendezvousParameters & params);
void Log() const;

/////////// CommissioningResolveDelegate Interface /////////
void OnNodeDiscovered(const chip::Dnssd::DiscoveredNodeData & nodeData) override;

#if CHIP_TOOL_DEVICE_SCANNER_USE_BLE
/////////// BleScannerDelegate Interface /////////
void OnBleScanResult(BLE_CONNECTION_OBJECT connObj, const chip::Ble::ChipBLEDeviceIdentificationInfo & info) override;
#endif // CHIP_TOOL_DEVICE_SCANNER_USE_BLE

private:
std::vector<DeviceScannerResult> mDiscoveredResults;
chip::Dnssd::ResolverProxy mDNSResolver;
};

DeviceScanner & GetDeviceScanner();
3 changes: 3 additions & 0 deletions examples/chip-tool/commands/discover/Commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ void registerCommandsDiscover(Commands & commands, CredentialIssuerCommands * cr

commands_list clusterCommands = {
make_unique<Resolve>(credsIssuerConfig),
make_unique<DiscoverCommissionablesStartCommand>(credsIssuerConfig),
make_unique<DiscoverCommissionablesStopCommand>(credsIssuerConfig),
make_unique<DiscoverCommissionablesListCommand>(credsIssuerConfig),
make_unique<DiscoverCommissionablesCommand>(credsIssuerConfig),
make_unique<DiscoverCommissionableByShortDiscriminatorCommand>(credsIssuerConfig),
make_unique<DiscoverCommissionableByLongDiscriminatorCommand>(credsIssuerConfig),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/

#include "DiscoverCommissionablesCommand.h"
#include <commands/common/DeviceScanner.h>
#include <commands/common/RemoteDataModelLogger.h>
#include <lib/support/BytesToHex.h>

Expand All @@ -35,6 +36,33 @@ void DiscoverCommissionablesCommandBase::OnDiscoveredDevice(const chip::Dnssd::D
}
}

CHIP_ERROR DiscoverCommissionablesStartCommand::RunCommand()
{
VerifyOrReturnError(IsInteractive(), CHIP_ERROR_INCORRECT_STATE);
ReturnErrorOnFailure(GetDeviceScanner().Start());

SetCommandExitStatus(CHIP_NO_ERROR);
return CHIP_NO_ERROR;
}

CHIP_ERROR DiscoverCommissionablesStopCommand::RunCommand()
{
VerifyOrReturnError(IsInteractive(), CHIP_ERROR_INCORRECT_STATE);
ReturnErrorOnFailure(GetDeviceScanner().Stop());

SetCommandExitStatus(CHIP_NO_ERROR);
return CHIP_NO_ERROR;
}

CHIP_ERROR DiscoverCommissionablesListCommand::RunCommand()
{
VerifyOrReturnError(IsInteractive(), CHIP_ERROR_INCORRECT_STATE);
GetDeviceScanner().Log();

SetCommandExitStatus(CHIP_NO_ERROR);
return CHIP_NO_ERROR;
}

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

class DiscoverCommissionablesStartCommand : public CHIPCommand
{
public:
DiscoverCommissionablesStartCommand(CredentialIssuerCommands * credIssuerCommands) : CHIPCommand("start", credIssuerCommands) {}

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

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 DiscoverCommissionablesListCommand : public CHIPCommand
{
public:
DiscoverCommissionablesListCommand(CredentialIssuerCommands * credIssuerCommands) : CHIPCommand("list", credIssuerCommands) {}

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

class DiscoverCommissionablesCommand : public DiscoverCommissionablesCommandBase
{
public:
Expand Down
20 changes: 20 additions & 0 deletions examples/chip-tool/commands/pairing/Commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,24 @@ class PairAlreadyDiscovered : public PairingCommand
{}
};

class PairAlreadyDiscoveredByIndex : public PairingCommand
{
public:
PairAlreadyDiscoveredByIndex(CredentialIssuerCommands * credsIssuerConfig) :
PairingCommand("already-discovered-by-index", PairingMode::AlreadyDiscoveredByIndex, PairingNetworkType::None,
credsIssuerConfig)
{}
};

class PairAlreadyDiscoveredByIndexWithWiFi : public PairingCommand
{
public:
PairAlreadyDiscoveredByIndexWithWiFi(CredentialIssuerCommands * credsIssuerConfig) :
PairingCommand("already-discovered-by-index-with-wifi", PairingMode::AlreadyDiscoveredByIndex, PairingNetworkType::WiFi,
credsIssuerConfig)
{}
};

class StartUdcServerCommand : public CHIPCommand
{
public:
Expand Down Expand Up @@ -208,6 +226,8 @@ void registerCommandsPairing(Commands & commands, CredentialIssuerCommands * cre
make_unique<PairBleThread>(credsIssuerConfig),
make_unique<PairSoftAP>(credsIssuerConfig),
make_unique<PairAlreadyDiscovered>(credsIssuerConfig),
make_unique<PairAlreadyDiscoveredByIndex>(credsIssuerConfig),
make_unique<PairAlreadyDiscoveredByIndexWithWiFi>(credsIssuerConfig),
make_unique<PairOnNetwork>(credsIssuerConfig),
make_unique<PairOnNetworkShort>(credsIssuerConfig),
make_unique<PairOnNetworkLong>(credsIssuerConfig),
Expand Down
25 changes: 25 additions & 0 deletions examples/chip-tool/commands/pairing/PairingCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "PairingCommand.h"
#include "platform/PlatformManager.h"
#include <commands/common/DeviceScanner.h>
#include <controller/ExampleOperationalCredentialsIssuer.h>
#include <crypto/CHIPCryptoPAL.h>
#include <lib/core/CHIPSafeCasts.h>
Expand Down Expand Up @@ -78,6 +79,9 @@ CHIP_ERROR PairingCommand::RunInternal(NodeId remoteId)
case PairingMode::AlreadyDiscovered:
err = Pair(remoteId, PeerAddress::UDP(mRemoteAddr.address, mRemotePort, mRemoteAddr.interfaceId));
break;
case PairingMode::AlreadyDiscoveredByIndex:
err = PairWithMdnsOrBleByIndex(remoteId, mIndex);
break;
}

return err;
Expand Down Expand Up @@ -167,6 +171,27 @@ CHIP_ERROR PairingCommand::Pair(NodeId remoteId, PeerAddress address)
return err;
}

CHIP_ERROR PairingCommand::PairWithMdnsOrBleByIndex(NodeId remoteId, uint16_t index)
{
VerifyOrReturnError(IsInteractive(), CHIP_ERROR_INCORRECT_STATE);

RendezvousParameters params;
ReturnErrorOnFailure(GetDeviceScanner().Get(index, params));
params.SetSetupPINCode(mSetupPINCode);

CHIP_ERROR err = CHIP_NO_ERROR;
if (mPaseOnly.ValueOr(false))
{
err = CurrentCommissioner().EstablishPASEConnection(remoteId, params);
}
else
{
auto commissioningParams = GetCommissioningParameters();
err = CurrentCommissioner().PairDevice(remoteId, params, commissioningParams);
}
return err;
}

CHIP_ERROR PairingCommand::PairWithMdns(NodeId remoteId)
{
Dnssd::DiscoveryFilter filter(mFilterType);
Expand Down
Loading

0 comments on commit 1419608

Please sign in to comment.