Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[chip-tool] Add missing commands to get TestDiscovery to work. #25056

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions examples/chip-tool/commands/common/RemoteDataModelLogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "RemoteDataModelLogger.h"

#include <lib/support/SafeInt.h>
#include <lib/support/jsontlv/TlvJson.h>

constexpr const char * kClusterIdKey = "clusterId";
Expand Down Expand Up @@ -164,6 +165,61 @@ CHIP_ERROR LogGetCommissionerNodeId(chip::NodeId value)
return gDelegate->LogJSON(valueStr.c_str());
}

CHIP_ERROR LogDiscoveredNodeData(const chip::Dnssd::DiscoveredNodeData & nodeData)
{
VerifyOrReturnError(gDelegate != nullptr, CHIP_NO_ERROR);

if (!chip::CanCastTo<uint8_t>(nodeData.resolutionData.numIPs))
{
ChipLogError(chipTool, "Too many ips.");
return CHIP_ERROR_INVALID_ARGUMENT;
}

if (!chip::CanCastTo<uint64_t>(nodeData.commissionData.rotatingIdLen))
{
ChipLogError(chipTool, "Can not convert rotatingId to json format.");
return CHIP_ERROR_INVALID_ARGUMENT;
}

char rotatingId[chip::Dnssd::kMaxRotatingIdLen * 2 + 1] = "";
chip::Encoding::BytesToUppercaseHexString(nodeData.commissionData.rotatingId, nodeData.commissionData.rotatingIdLen, rotatingId,
sizeof(rotatingId));

Json::Value value;
value["hostName"] = nodeData.resolutionData.hostName;
value["instanceName"] = nodeData.commissionData.instanceName;
value["longDiscriminator"] = nodeData.commissionData.longDiscriminator;
value["shortDiscriminator"] = ((nodeData.commissionData.longDiscriminator >> 8) & 0x0F);
value["vendorId"] = nodeData.commissionData.vendorId;
value["productId"] = nodeData.commissionData.productId;
value["commissioningMode"] = nodeData.commissionData.commissioningMode;
value["deviceType"] = nodeData.commissionData.deviceType;
value["deviceName"] = nodeData.commissionData.deviceName;
value["rotatingId"] = rotatingId;
value["rotatingIdLen"] = static_cast<uint64_t>(nodeData.commissionData.rotatingIdLen);
value["pairingHint"] = nodeData.commissionData.pairingHint;
value["pairingInstruction"] = nodeData.commissionData.pairingInstruction;
value["supportsTcp"] = nodeData.resolutionData.supportsTcp;
value["port"] = nodeData.resolutionData.port;
value["numIPs"] = static_cast<uint8_t>(nodeData.resolutionData.numIPs);

if (nodeData.resolutionData.mrpRetryIntervalIdle.HasValue())
{
value["mrpRetryIntervalIdle"] = nodeData.resolutionData.mrpRetryIntervalIdle.Value().count();
}

if (nodeData.resolutionData.mrpRetryIntervalActive.HasValue())
{
value["mrpRetryIntervalActive"] = nodeData.resolutionData.mrpRetryIntervalActive.Value().count();
}

Json::Value rootValue;
rootValue[kValueKey] = value;

auto valueStr = chip::JsonToString(rootValue);
return gDelegate->LogJSON(valueStr.c_str());
}

void SetDelegate(RemoteDataModelLoggerDelegate * delegate)
{
gDelegate = delegate;
Expand Down
2 changes: 2 additions & 0 deletions examples/chip-tool/commands/common/RemoteDataModelLogger.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <app/ConcreteCommandPath.h>
#include <app/EventHeader.h>
#include <app/MessageDef/StatusIB.h>
#include <lib/dnssd/Resolver.h>

class RemoteDataModelLoggerDelegate
{
Expand All @@ -39,5 +40,6 @@ CHIP_ERROR LogEventAsJSON(const chip::app::EventHeader & header, chip::TLV::TLVR
CHIP_ERROR LogErrorAsJSON(const chip::app::EventHeader & header, const chip::app::StatusIB & status);
CHIP_ERROR LogErrorAsJSON(const CHIP_ERROR & error);
CHIP_ERROR LogGetCommissionerNodeId(chip::NodeId value);
CHIP_ERROR LogDiscoveredNodeData(const chip::Dnssd::DiscoveredNodeData & nodeData);
void SetDelegate(RemoteDataModelLoggerDelegate * delegate);
}; // namespace RemoteDataModelLogger
5 changes: 5 additions & 0 deletions examples/chip-tool/commands/discover/Commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ void registerCommandsDiscover(Commands & commands, CredentialIssuerCommands * cr
commands_list clusterCommands = {
make_unique<Resolve>(credsIssuerConfig),
make_unique<DiscoverCommissionablesCommand>(credsIssuerConfig),
make_unique<DiscoverCommissionableByShortDiscriminatorCommand>(credsIssuerConfig),
make_unique<DiscoverCommissionableByLongDiscriminatorCommand>(credsIssuerConfig),
make_unique<DiscoverCommissionableByCommissioningModeCommand>(credsIssuerConfig),
make_unique<DiscoverCommissionableByVendorIdCommand>(credsIssuerConfig),
make_unique<DiscoverCommissionableByDeviceTypeCommand>(credsIssuerConfig),
make_unique<DiscoverCommissionersCommand>(credsIssuerConfig),
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,56 @@
*/

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

using namespace ::chip;

void DiscoverCommissionablesCommandBase::OnDiscoveredDevice(const chip::Dnssd::DiscoveredNodeData & nodeData)
{
nodeData.LogDetail();
LogErrorOnFailure(RemoteDataModelLogger::LogDiscoveredNodeData(nodeData));
SetCommandExitStatus(CHIP_NO_ERROR);
}

CHIP_ERROR DiscoverCommissionablesCommand::RunCommand()
{
CurrentCommissioner().RegisterDeviceDiscoveryDelegate(this);
Dnssd::DiscoveryFilter filter(Dnssd::DiscoveryFilterType::kNone, (uint64_t) 0);
return CurrentCommissioner().DiscoverCommissionableNodes(filter);
}

void DiscoverCommissionablesCommand::OnDiscoveredDevice(const chip::Dnssd::DiscoveredNodeData & nodeData)
CHIP_ERROR DiscoverCommissionableByShortDiscriminatorCommand::RunCommand()
{
nodeData.LogDetail();
CurrentCommissioner().RegisterDeviceDiscoveryDelegate(this);
chip::Dnssd::DiscoveryFilter filter(chip::Dnssd::DiscoveryFilterType::kShortDiscriminator, mDiscriminator);
return CurrentCommissioner().DiscoverCommissionableNodes(filter);
}

CHIP_ERROR DiscoverCommissionableByLongDiscriminatorCommand::RunCommand()
{
CurrentCommissioner().RegisterDeviceDiscoveryDelegate(this);
chip::Dnssd::DiscoveryFilter filter(chip::Dnssd::DiscoveryFilterType::kLongDiscriminator, mDiscriminator);
return CurrentCommissioner().DiscoverCommissionableNodes(filter);
}

CHIP_ERROR DiscoverCommissionableByCommissioningModeCommand::RunCommand()
{
CurrentCommissioner().RegisterDeviceDiscoveryDelegate(this);
chip::Dnssd::DiscoveryFilter filter(chip::Dnssd::DiscoveryFilterType::kCommissioningMode);
return CurrentCommissioner().DiscoverCommissionableNodes(filter);
}

CHIP_ERROR DiscoverCommissionableByVendorIdCommand::RunCommand()
{
CurrentCommissioner().RegisterDeviceDiscoveryDelegate(this);
chip::Dnssd::DiscoveryFilter filter(chip::Dnssd::DiscoveryFilterType::kVendorId, mVendorId);
return CurrentCommissioner().DiscoverCommissionableNodes(filter);
}

CHIP_ERROR DiscoverCommissionableByDeviceTypeCommand::RunCommand()
{
CurrentCommissioner().RegisterDeviceDiscoveryDelegate(this);
chip::Dnssd::DiscoveryFilter filter(chip::Dnssd::DiscoveryFilterType::kDeviceType, mDeviceType);
return CurrentCommissioner().DiscoverCommissionableNodes(filter);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,103 @@

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

class DiscoverCommissionablesCommand : public CHIPCommand, public chip::Controller::DeviceDiscoveryDelegate
class DiscoverCommissionablesCommandBase : public CHIPCommand, public chip::Controller::DeviceDiscoveryDelegate
{
public:
DiscoverCommissionablesCommand(CredentialIssuerCommands * credsIssuerConfig) : CHIPCommand("commissionables", credsIssuerConfig)
DiscoverCommissionablesCommandBase(const char * name, CredentialIssuerCommands * credsIssuerConfig) :
CHIPCommand(name, credsIssuerConfig)
{}

/////////// DeviceDiscoveryDelegate Interface /////////
void OnDiscoveredDevice(const chip::Dnssd::DiscoveredNodeData & nodeData) override;

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

class DiscoverCommissionablesCommand : public DiscoverCommissionablesCommandBase
{
public:
DiscoverCommissionablesCommand(CredentialIssuerCommands * credsIssuerConfig) :
DiscoverCommissionablesCommandBase("commissionables", credsIssuerConfig)
{}

/////////// CHIPCommand Interface /////////
CHIP_ERROR RunCommand() override;
};

class DiscoverCommissionableByShortDiscriminatorCommand : public DiscoverCommissionablesCommandBase
{
public:
DiscoverCommissionableByShortDiscriminatorCommand(CredentialIssuerCommands * credsIssuerConfig) :
DiscoverCommissionablesCommandBase("find-commissionable-by-short-discriminator", credsIssuerConfig)
{
AddArgument("value", 0, UINT16_MAX, &mDiscriminator);
}

/////////// CHIPCommand Interface /////////
CHIP_ERROR RunCommand() override;

private:
uint16_t mDiscriminator;
};

class DiscoverCommissionableByLongDiscriminatorCommand : public DiscoverCommissionablesCommandBase
{
public:
DiscoverCommissionableByLongDiscriminatorCommand(CredentialIssuerCommands * credsIssuerConfig) :
DiscoverCommissionablesCommandBase("find-commissionable-by-long-discriminator", credsIssuerConfig)
{
AddArgument("value", 0, UINT16_MAX, &mDiscriminator);
}

/////////// CHIPCommand Interface /////////
CHIP_ERROR RunCommand() override;

private:
uint16_t mDiscriminator;
};

class DiscoverCommissionableByCommissioningModeCommand : public DiscoverCommissionablesCommandBase
{
public:
DiscoverCommissionableByCommissioningModeCommand(CredentialIssuerCommands * credsIssuerConfig) :
DiscoverCommissionablesCommandBase("find-commissionable-by-commissioning-mode", credsIssuerConfig)
{}

/////////// CHIPCommand Interface /////////
CHIP_ERROR RunCommand() override;
};

class DiscoverCommissionableByVendorIdCommand : public DiscoverCommissionablesCommandBase
{
public:
DiscoverCommissionableByVendorIdCommand(CredentialIssuerCommands * credsIssuerConfig) :
DiscoverCommissionablesCommandBase("find-commissionable-by-vendor-id", credsIssuerConfig)
{
AddArgument("value", 0, UINT16_MAX, &mVendorId);
}

/////////// CHIPCommand Interface /////////
CHIP_ERROR RunCommand() override;

private:
uint16_t mVendorId;
};

class DiscoverCommissionableByDeviceTypeCommand : public DiscoverCommissionablesCommandBase
{
public:
DiscoverCommissionableByDeviceTypeCommand(CredentialIssuerCommands * credsIssuerConfig) :
DiscoverCommissionablesCommandBase("find-commissionable-by-device-type", credsIssuerConfig)
{
AddArgument("value", 0, UINT16_MAX, &mDeviceType);
}

/////////// CHIPCommand Interface /////////
CHIP_ERROR RunCommand() override;

private:
// TODO: possibly 32-bit - see spec issue #3226
uint16_t mDeviceType;
};