Skip to content

Commit

Permalink
[chip-tool] Implement wait-for-device command
Browse files Browse the repository at this point in the history
  • Loading branch information
erjiaqing committed Apr 16, 2024
1 parent d569a92 commit 1052239
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 3 deletions.
8 changes: 7 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,13 @@ class ModelCommand : public CHIPCommand
void Shutdown() override;

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

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

chip::Optional<uint16_t> mTimeout;

Expand Down
4 changes: 2 additions & 2 deletions examples/chip-tool/commands/common/ChipToolCheckInDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ class CheckInCompleteCallback
/**
* @brief Callback used to let the application know that a check-in message was received and validated.
*
* The callback will be executed in CHIP main loop. Implementations avoid blocking in this callback.
* The callback will be executed in CHIP main loop. Implementations should avoid blocking in this callback.
*
* @param[in] clientInfo - ICDClientInfo object representing the state associated with the
* node that sent the check-in message.
*/
virtual void OnCheckInComplete(const chip::app::ICDClientInfo & clientInfo);
virtual void OnCheckInComplete(const chip::app::ICDClientInfo & clientInfo) = 0;
};

class ChipToolCheckInDelegate : public chip::app::CheckInDelegate
Expand Down
44 changes: 44 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,12 +65,55 @@ 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());
RegisterOnCheckInCompleteCallback(this);
ChipLogError(chipTool, "Please trigger the device active mode.");
return CHIP_NO_ERROR;
}

void ICDWaitForDeviceCommand::OnCheckInComplete(const chip::app::ICDClientInfo & 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.");
UnregisterOnCheckInCompleteCallback(this);

CHIP_ERROR err = ClusterCommand::RunCommand();
if (err != CHIP_NO_ERROR)
{
SetCommandExitStatus(err);
return;
}
}

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

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

commands_list list = {
make_unique<ICDListCommand>(credsIssuerConfig),
make_unique<ICDWaitForDeviceCommand>(credsIssuerConfig),
};

commands.RegisterCommandSet(name, list, "Commands for client-side ICD management.");
Expand Down
27 changes: 27 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/tests/suites/commands/interaction_model/InteractionModel.h>
#include <commands/common/ChipToolCheckInDelegate.h>
#include <lib/support/Span.h>

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

class ICDWaitForDeviceCommand : public ClusterCommand, public CheckInCompleteCallback
{
public:
ICDWaitForDeviceCommand(CredentialIssuerCommands * credIssuerCmds) : ClusterCommand("wait-for-device", credIssuerCmds)
{
ModelCommand::AddArguments(/*skipEndpoints=*/true);
AddArgument("stay-active-duration-seconds", 1, UINT32_MAX, &mStayActiveDurationSeconds,
"The 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;
};

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

0 comments on commit 1052239

Please sign in to comment.