From 410826296162fa35cd9a8ef798b9ac8ca0115ac8 Mon Sep 17 00:00:00 2001 From: Vivien Nicolas Date: Wed, 4 Jan 2023 19:50:03 +0100 Subject: [PATCH] [chip-tool] Add delay commands module to chip-tool with busy-wait command (#24063) Co-authored-by: Andrei Litvin --- examples/chip-tool/BUILD.gn | 1 + examples/chip-tool/commands/delay/Commands.h | 32 +++++++++++ .../chip-tool/commands/delay/SleepCommand.cpp | 28 ++++++++++ .../chip-tool/commands/delay/SleepCommand.h | 54 +++++++++++++++++++ examples/chip-tool/main.cpp | 2 + 5 files changed, 117 insertions(+) create mode 100644 examples/chip-tool/commands/delay/Commands.h create mode 100644 examples/chip-tool/commands/delay/SleepCommand.cpp create mode 100644 examples/chip-tool/commands/delay/SleepCommand.h diff --git a/examples/chip-tool/BUILD.gn b/examples/chip-tool/BUILD.gn index 86e003a69201c4..4b2a0cee169cb6 100644 --- a/examples/chip-tool/BUILD.gn +++ b/examples/chip-tool/BUILD.gn @@ -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", diff --git a/examples/chip-tool/commands/delay/Commands.h b/examples/chip-tool/commands/delay/Commands.h new file mode 100644 index 00000000000000..337de3b14d217a --- /dev/null +++ b/examples/chip-tool/commands/delay/Commands.h @@ -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(credsIssuerConfig), // + }; + + commands.Register(clusterName, clusterCommands); +} diff --git a/examples/chip-tool/commands/delay/SleepCommand.cpp b/examples/chip-tool/commands/delay/SleepCommand.cpp new file mode 100644 index 00000000000000..de1db49b4b3f5f --- /dev/null +++ b/examples/chip-tool/commands/delay/SleepCommand.cpp @@ -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 +#include + +CHIP_ERROR SleepCommand::RunCommand() +{ + std::this_thread::sleep_for(std::chrono::milliseconds(mDurationInMs)); + SetCommandExitStatus(CHIP_NO_ERROR); + return CHIP_NO_ERROR; +} diff --git a/examples/chip-tool/commands/delay/SleepCommand.h b/examples/chip-tool/commands/delay/SleepCommand.h new file mode 100644 index 00000000000000..aa1cddcbf94be8 --- /dev/null +++ b/examples/chip-tool/commands/delay/SleepCommand.h @@ -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; +}; diff --git a/examples/chip-tool/main.cpp b/examples/chip-tool/main.cpp index c9a391b25e5f98..0ca5b243c9243e 100644 --- a/examples/chip-tool/main.cpp +++ b/examples/chip-tool/main.cpp @@ -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" @@ -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);