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 delay commands module to chip-tool with busy-wait com… #24063

Merged
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
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