Skip to content

Commit

Permalink
[chip-tool] Add delay commands module to chip-tool with busy-wait com…
Browse files Browse the repository at this point in the history
…mand (#24063)

Co-authored-by: Andrei Litvin <[email protected]>
  • Loading branch information
2 people authored and pull[bot] committed Feb 14, 2024
1 parent a80fbaf commit 2867329
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 0 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.h",
"commands/common/CredentialIssuerCommands.h",
"commands/common/HexConversion.h",
"commands/delay/SleepCommand.cpp",
"commands/discover/DiscoverCommand.cpp",
"commands/discover/DiscoverCommissionablesCommand.cpp",
"commands/discover/DiscoverCommissionersCommand.cpp",
Expand Down
32 changes: 32 additions & 0 deletions examples/chip-tool/commands/delay/Commands.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2022 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 "commands/common/Commands.h"
#include "commands/delay/SleepCommand.h"

void registerCommandsDelay(Commands & commands, CredentialIssuerCommands * credsIssuerConfig)
{
const char * clusterName = "Delay";
commands_list clusterCommands = {
make_unique<SleepCommand>(credsIssuerConfig), //
};

commands.Register(clusterName, clusterCommands);
}
28 changes: 28 additions & 0 deletions examples/chip-tool/commands/delay/SleepCommand.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright (c) 2022 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 "SleepCommand.h"
#include <chrono>
#include <thread>

CHIP_ERROR SleepCommand::RunCommand()
{
std::this_thread::sleep_for(std::chrono::milliseconds(mDurationInMs));
SetCommandExitStatus(CHIP_NO_ERROR);
return CHIP_NO_ERROR;
}
54 changes: 54 additions & 0 deletions examples/chip-tool/commands/delay/SleepCommand.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (c) 2022 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 "../common/CHIPCommand.h"

/**
* This command blocks the event loop processing for a given amount of time.
*
* For example when the event loop is blocked the messages coming-in will not be acked,
* forcing a retransmission on the other side.
*
*/

class SleepCommand : public CHIPCommand
{
public:
SleepCommand(CredentialIssuerCommands * credIssuerCommands) : CHIPCommand("sleep", credIssuerCommands)
{
AddArgument("duration-in-ms", 0, UINT32_MAX, &mDurationInMs,
"Block the event loop processing for duration-in-ms milliseconds.");
}

/////////// CHIPCommand Interface /////////
CHIP_ERROR RunCommand() override;
chip::System::Clock::Timeout GetWaitDuration() const override
{
// The allowed duration of this method is at least as long as the time specified for blocking the
// event loop. In order to not fail on some small delays in processing some extra time before
// failing is added.
constexpr uint16_t mExtraTimeForFailure = 1000;

return chip::System::Clock::Milliseconds32(mDurationInMs + mExtraTimeForFailure);
}

private:
uint32_t mDurationInMs;
};
2 changes: 2 additions & 0 deletions examples/chip-tool/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "commands/common/Commands.h"
#include "commands/example/ExampleCredentialIssuerCommands.h"

#include "commands/delay/Commands.h"
#include "commands/discover/Commands.h"
#include "commands/group/Commands.h"
#include "commands/interactive/Commands.h"
Expand All @@ -36,6 +37,7 @@ int main(int argc, char * argv[])
{
ExampleCredentialIssuerCommands credIssuerCommands;
Commands commands;
registerCommandsDelay(commands, &credIssuerCommands);
registerCommandsDiscover(commands, &credIssuerCommands);
registerCommandsInteractive(commands, &credIssuerCommands);
registerCommandsPayload(commands);
Expand Down

0 comments on commit 2867329

Please sign in to comment.