Skip to content

Commit

Permalink
[chip-tool] Implement icd wait-for-device command (#32886)
Browse files Browse the repository at this point in the history
* [chip-tool] Implement wait-for-device command

* update

* update

* fix

* Restyled by clang-format

* Update examples/chip-tool/commands/icd/ICDCommand.cpp

Co-authored-by: Boris Zbarsky <[email protected]>

* Update ICDCommand.cpp

* Update examples/chip-tool/commands/icd/ICDCommand.cpp

Co-authored-by: Boris Zbarsky <[email protected]>

* Update examples/chip-tool/commands/icd/ICDCommand.cpp

Co-authored-by: Boris Zbarsky <[email protected]>

* Update examples/chip-tool/commands/icd/ICDCommand.h

Co-authored-by: Boris Zbarsky <[email protected]>

* Update examples/chip-tool/commands/icd/ICDCommand.h

Co-authored-by: mkardous-silabs <[email protected]>

* Update examples/chip-tool/commands/icd/ICDCommand.h

Co-authored-by: Boris Zbarsky <[email protected]>

* Update ICDCommand.cpp

* Update CHIPCommand.cpp

* Update ICDCommand.cpp

* Update ICDCommand.cpp

* Update ICDCommand.cpp

---------

Co-authored-by: Restyled.io <[email protected]>
Co-authored-by: yunhanw-google <[email protected]>
Co-authored-by: Boris Zbarsky <[email protected]>
Co-authored-by: mkardous-silabs <[email protected]>
  • Loading branch information
5 people authored May 17, 2024
1 parent 7b2f729 commit 8978118
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 4 deletions.
6 changes: 6 additions & 0 deletions examples/chip-tool/commands/clusters/ModelCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,9 @@ void ModelCommand::CheckPeerICDType()
}
}
}

bool ModelCommand::IsPeerLIT()
{
CheckPeerICDType();
return mIsPeerLIT.ValueOr(false);
}
4 changes: 3 additions & 1 deletion examples/chip-tool/commands/clusters/ModelCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ class ModelCommand : public CHIPCommand
void Shutdown() override;

protected:
bool IsPeerLIT() { return mIsPeerLIT.ValueOr(false); }
bool IsPeerLIT();

chip::NodeId GetDestinationId() const { return mDestinationId; }

chip::Optional<uint16_t> mTimeout;

Expand Down
6 changes: 3 additions & 3 deletions examples/chip-tool/commands/common/CHIPCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "CHIPCommand.h"

#include <commands/icd/ICDCommand.h>
#include <controller/CHIPDeviceControllerFactory.h>
#include <credentials/attestation_verifier/FileAttestationTrustStore.h>
#include <lib/core/CHIPConfig.h>
Expand Down Expand Up @@ -52,7 +53,6 @@ chip::Credentials::GroupDataProviderImpl CHIPCommand::sGroupDataProvider{ kMaxGr
// All fabrics share the same ICD client storage.
chip::app::DefaultICDClientStorage CHIPCommand::sICDClientStorage;
chip::Crypto::RawKeySessionKeystore CHIPCommand::sSessionKeystore;
chip::app::DefaultCheckInDelegate CHIPCommand::sCheckInDelegate;
chip::app::CheckInHandler CHIPCommand::sCheckInHandler;

namespace {
Expand Down Expand Up @@ -153,9 +153,9 @@ CHIP_ERROR CHIPCommand::MaybeSetUpStack()

auto engine = chip::app::InteractionModelEngine::GetInstance();
VerifyOrReturnError(engine != nullptr, CHIP_ERROR_INCORRECT_STATE);
ReturnLogErrorOnFailure(sCheckInDelegate.Init(&sICDClientStorage, engine));
ReturnLogErrorOnFailure(ChipToolCheckInDelegate()->Init(&sICDClientStorage, engine));
ReturnLogErrorOnFailure(sCheckInHandler.Init(DeviceControllerFactory::GetInstance().GetSystemState()->ExchangeMgr(),
&sICDClientStorage, &sCheckInDelegate, engine));
&sICDClientStorage, ChipToolCheckInDelegate(), engine));

CommissionerIdentity nullIdentity{ kIdentityNull, chip::kUndefinedNodeId };
ReturnLogErrorOnFailure(InitializeCommissioner(nullIdentity, kIdentityNullFabricId));
Expand Down
61 changes: 61 additions & 0 deletions examples/chip-tool/commands/icd/ICDCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <crypto/RawKeySessionKeystore.h>

using namespace ::chip;
using namespace ::chip::app;

CHIP_ERROR ICDListCommand::RunCommand()
{
Expand Down Expand Up @@ -64,13 +65,73 @@ CHIP_ERROR ICDListCommand::RunCommand()
return CHIP_NO_ERROR;
}

CHIP_ERROR ICDWaitForDeviceCommand::RunCommand()
{
if (!IsPeerLIT())
{
ChipLogError(chipTool, "The device is not a registered LIT-ICD device.");
return CHIP_ERROR_NOT_FOUND;
}
mInterestedNode = ScopedNodeId(GetDestinationId(), CurrentCommissioner().GetFabricIndex());
ChipLogProgress(chipTool, "Please trigger the device active mode.");
return CHIP_NO_ERROR;
}

void ICDWaitForDeviceCommand::OnCheckInComplete(const chip::app::ICDClientInfo & clientInfo)
{
DefaultCheckInDelegate::OnCheckInComplete(clientInfo);

if (clientInfo.peer_node != mInterestedNode)
{
ChipLogDetail(chipTool, "The node " ChipLogFormatScopedNodeId " is not the one we are interested in.",
ChipLogValueScopedNodeId(clientInfo.peer_node));
return;
}

ChipLogDetail(chipTool, "Received check-in message from the node, send stay active request to the device.");
mInterestedNode = ScopedNodeId();

// Intentionally call RunCommand, since it includes all necessary steps for SendCommand.
CHIP_ERROR err = ClusterCommand::RunCommand();
if (err != CHIP_NO_ERROR)
{
SetCommandExitStatus(err);
return;
}
}

CHIP_ERROR ICDWaitForDeviceCommand::SendCommand(DeviceProxy * device,
std::vector<chip::EndpointId> /* not used, always send to endpoint 0 */)
{
Clusters::IcdManagement::Commands::StayActiveRequest::Type request;
request.stayActiveDuration = mStayActiveDurationSeconds;
return ClusterCommand::SendCommand(device, kRootEndpointId, Clusters::IcdManagement::Id,
Clusters::IcdManagement::Commands::StayActiveRequest::Id, request);
}

namespace {
DefaultCheckInDelegate * sCheckInDelegate;
}

void registerCommandsICD(Commands & commands, CredentialIssuerCommands * credsIssuerConfig)
{
const char * name = "ICD";

auto icdWaitForDeviceCommand = make_unique<ICDWaitForDeviceCommand>(credsIssuerConfig);

// This should be safe within chip-tool, since the lifespan of Commands is longer than any commands and the CHIPStack.
// So this object will not be used after free within chip-tool.
sCheckInDelegate = static_cast<ICDWaitForDeviceCommand *>(icdWaitForDeviceCommand.get());

commands_list list = {
make_unique<ICDListCommand>(credsIssuerConfig),
std::move(icdWaitForDeviceCommand),
};

commands.RegisterCommandSet(name, list, "Commands for client-side ICD management.");
}

DefaultCheckInDelegate * ChipToolCheckInDelegate()
{
return sCheckInDelegate;
}
29 changes: 29 additions & 0 deletions examples/chip-tool/commands/icd/ICDCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@
#pragma once

#include "../common/CHIPCommand.h"
#include "commands/clusters/ClusterCommand.h"
#include "commands/common/Commands.h"

#include <app/CommandSender.h>
#include <app/icd/client/DefaultCheckInDelegate.h>
#include <app/tests/suites/commands/interaction_model/InteractionModel.h>
#include <lib/support/Span.h>

class ICDCommand : public CHIPCommand
Expand All @@ -42,4 +46,29 @@ class ICDListCommand : public ICDCommand
CHIP_ERROR RunCommand() override;
};

class ICDWaitForDeviceCommand : public ClusterCommand, public chip::app::DefaultCheckInDelegate
{
public:
ICDWaitForDeviceCommand(CredentialIssuerCommands * credIssuerCmds) : ClusterCommand("wait-for-device", credIssuerCmds)
{
ModelCommand::AddArguments(/* skipEndpoints= */ true);
AddArgument("stay-active-duration-seconds", 30, UINT32_MAX, &mStayActiveDurationSeconds,
"The requested duration in seconds for the device to stay active after check-in completes.");
}

virtual ~ICDWaitForDeviceCommand() = default;

CHIP_ERROR RunCommand() override;

void OnCheckInComplete(const chip::app::ICDClientInfo & clientInfo) override;

CHIP_ERROR SendCommand(chip::DeviceProxy * device, std::vector<chip::EndpointId> endPointIds) override;

private:
chip::ScopedNodeId mInterestedNode;
uint32_t mStayActiveDurationSeconds = 30;
};

void registerCommandsICD(Commands & commands, CredentialIssuerCommands * credsIssuerConfig);

chip::app::DefaultCheckInDelegate * ChipToolCheckInDelegate();
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,9 @@ void ModelCommand::Shutdown()
mOnDeviceConnectedCallback.Cancel();
mOnDeviceConnectionFailureCallback.Cancel();
}

bool ModelCommand::IsPeerLIT()
{
// Does not support tv-casting-app
return false;
}

0 comments on commit 8978118

Please sign in to comment.