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
  • Loading branch information
vivien-apple committed Dec 13, 2022
1 parent 3f6e635 commit 1fdc183
Show file tree
Hide file tree
Showing 5 changed files with 109 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 @@ -58,6 +58,7 @@ static_library("chip-tool-utils") {
"commands/common/Commands.cpp",
"commands/common/Commands.h",
"commands/common/CredentialIssuerCommands.h",
"commands/delay/BusyWaitCommand.cpp",
"commands/discover/DiscoverCommand.cpp",
"commands/discover/DiscoverCommissionablesCommand.cpp",
"commands/discover/DiscoverCommissionersCommand.cpp",
Expand Down
33 changes: 33 additions & 0 deletions examples/chip-tool/commands/delay/BusyWaitCommand.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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 "BusyWaitCommand.h"

CHIP_ERROR BusyWaitCommand::RunCommand()
{
auto & clock = chip::System::SystemClock();
auto start = clock.GetMonotonicTimestamp();
chip::System::Clock::Milliseconds32 durationInMs(mDurationInMs);
while (clock.GetMonotonicTimestamp() - start < durationInMs)
{
// nothing to do.
};

SetCommandExitStatus(CHIP_NO_ERROR);
return CHIP_NO_ERROR;
}
41 changes: 41 additions & 0 deletions examples/chip-tool/commands/delay/BusyWaitCommand.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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"

class BusyWaitCommand : public CHIPCommand
{
public:
BusyWaitCommand(CredentialIssuerCommands * credIssuerCommands) : CHIPCommand("busy-wait", credIssuerCommands)
{
AddArgument("duration-in-ms", 0, UINT32_MAX, &mDurationInMs);
}

/////////// CHIPCommand Interface /////////
CHIP_ERROR RunCommand() override;
chip::System::Clock::Timeout GetWaitDuration() const override
{
return chip::System::Clock::Milliseconds32(mDurationInMs + 1000 /* add some extra time */);
}

private:
static void RunQueuedCommand(intptr_t commandArg);
uint32_t mDurationInMs;
};
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/BusyWaitCommand.h"

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

commands.Register(clusterName, clusterCommands);
}
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 1fdc183

Please sign in to comment.