-
Notifications
You must be signed in to change notification settings - Fork 2k
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
[shell] Add otcli pass-through commands for direct control over the OpenThread interface. #2371
Merged
+231
−2
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# CHIP Shell - OpenThread CLI pass-through | ||
|
||
The CHIP Shell CLI can execute pass-through commands to the | ||
[OpenThread cli](https://github.com/openthread/openthread/blob/master/src/cli/README.md) | ||
directly. | ||
|
||
## Setup | ||
|
||
### Embedded | ||
|
||
On embedded platforms, the otcli commands are available simply when OpenThread | ||
support is enabled. | ||
|
||
### Linux | ||
|
||
On embedded Linux platforms, otcli commands require installation of some | ||
OpenThread daemons: | ||
|
||
``` | ||
# Start Border Router agent | ||
sudo /usr/local/sbin/otbr-agent -d6 -v -I wpan0 spinel+hdlc+forkpty:///usr/local/bin/ot-rcp\?forkpty-arg=5 | ||
``` | ||
|
||
If this command is not available, the follow instructions will build and install | ||
it: | ||
|
||
``` | ||
# Build OpenThread Interface (simulation used as example -- alternatively could be RCP hardware) | ||
cd third_party/openthread/repo | ||
./script/bootstrap | ||
mkdir build && cd build | ||
cmake .. -DCMAKE_INSTALL_PREFIX=/usr/local -DOT_PLATFORM=simulation -GNinja | ||
ninja -j8 && sudo ninja install | ||
|
||
# Build Border Router functionality | ||
cd third_party/ot-br-posix/repo | ||
./script/bootstrap | ||
mkdir build && cd build | ||
cmake .. -DOTBR_DBUS=ON -GNinja -DCMAKE_INSTALL_PREFIX=/usr/local - | ||
ninja -j8 && sudo ninja install | ||
|
||
# Start Border Router agent | ||
sudo /usr/local/sbin/otbr-agent -d6 -v -I wpan0 spinel+hdlc+forkpty:///usr/local/bin/ot-rcp\?forkpty-arg=5 | ||
|
||
# In a new shell, at top-level of CHIP repo, test Thread device layer is operational | ||
./bootstrap | ||
mkdir build && cd build | ||
../configure --with-device-layer=linux | ||
make -C src/platform/tests TestThreadStackMgr | ||
sudo ./src/platform/tests/TestThreadStackMgr | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
/* | ||
* | ||
* Copyright (c) 2020 Project CHIP Authors | ||
* | ||
* 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 <core/CHIPCore.h> | ||
#include <platform/CHIPDeviceLayer.h> | ||
|
||
#if CHIP_ENABLE_OPENTHREAD | ||
|
||
#include <stdio.h> | ||
|
||
#include <platform/ThreadStackManager.h> | ||
#include <shell/shell.h> | ||
#include <support/CHIPArgParser.hpp> | ||
#include <support/CHIPMem.h> | ||
#include <support/CodeUtils.h> | ||
|
||
#if CHIP_TARGET_STYLE_EMBEDDED | ||
#include <openthread/cli.h> | ||
#include <openthread/instance.h> | ||
#include <openthread/ip6.h> | ||
#include <openthread/link.h> | ||
#include <openthread/thread.h> | ||
#else | ||
#include <sys/types.h> | ||
#include <sys/wait.h> | ||
#include <unistd.h> | ||
#endif | ||
|
||
using namespace chip; | ||
using namespace chip::Shell; | ||
using namespace chip::Platform; | ||
using namespace chip::DeviceLayer; | ||
using namespace chip::Logging; | ||
using namespace chip::ArgParser; | ||
|
||
static chip::Shell::Shell sShellOtcliSubcommands; | ||
|
||
int cmd_otcli_help_iterator(shell_command_t * command, void * arg) | ||
{ | ||
streamer_printf(streamer_get(), " %-15s %s\n\r", command->cmd_name, command->cmd_help); | ||
return 0; | ||
} | ||
|
||
int cmd_otcli_help(int argc, char ** argv) | ||
{ | ||
sShellOtcliSubcommands.ForEachCommand(cmd_otcli_help_iterator, NULL); | ||
return 0; | ||
} | ||
|
||
#if CHIP_TARGET_STYLE_EMBEDDED | ||
|
||
int cmd_otcli_dispatch(int argc, char ** argv) | ||
{ | ||
CHIP_ERROR error = CHIP_NO_ERROR; | ||
|
||
// From OT CLI internal lib, kMaxLineLength = 128 | ||
#define kMaxLineLength 128 | ||
char buff[kMaxLineLength] = { 0 }; | ||
char * buff_ptr = buff; | ||
int i = 0; | ||
|
||
VerifyOrExit(argc > 0, error = CHIP_ERROR_INVALID_ARGUMENT); | ||
|
||
for (i = 0; i < argc; i++) | ||
{ | ||
size_t arg_len = strlen(argv[i]); | ||
|
||
/* Make sure that the next argument won't overflow the buffer */ | ||
VerifyOrExit(buff_ptr + arg_len < buff + kMaxLineLength, error = CHIP_ERROR_BUFFER_TOO_SMALL); | ||
|
||
strncpy(buff_ptr, argv[i], arg_len); | ||
buff_ptr += arg_len; | ||
|
||
/* Make sure that there is enough buffer for a space char */ | ||
if (buff_ptr + sizeof(char) < buff + kMaxLineLength) | ||
{ | ||
strncpy(buff_ptr, " ", sizeof(char)); | ||
buff_ptr++; | ||
} | ||
} | ||
buff_ptr = 0; | ||
|
||
otCliConsoleInputLine(buff, buff_ptr - buff); | ||
exit: | ||
return error; | ||
} | ||
|
||
#elif CHIP_TARGET_STYLE_UNIX | ||
|
||
int cmd_otcli_dispatch(int argc, char ** argv) | ||
{ | ||
CHIP_ERROR error = CHIP_NO_ERROR; | ||
|
||
int pid; | ||
uid_t euid = geteuid(); | ||
char ctl_command[] = "/usr/local/sbin/ot-ctl"; | ||
|
||
// Must run as sudo. | ||
if (euid != 0) | ||
{ | ||
streamer_printf(streamer_get(), "Error otcli: requires running chip-shell as sudo\n\r"); | ||
error = CHIP_ERROR_INCORRECT_STATE; | ||
ExitNow(); | ||
} | ||
|
||
VerifyOrExit(argc > 0, error = CHIP_ERROR_INVALID_ARGUMENT); | ||
|
||
// Fork and execute the command. | ||
pid = fork(); | ||
VerifyOrExit(pid != -1, error = CHIP_ERROR_INCORRECT_STATE); | ||
|
||
if (pid == 0) | ||
{ | ||
// Child process to execute the command with provided arguments | ||
--argv; // Restore access to entry [0] containing the command; | ||
argv[0] = ctl_command; | ||
if (execvp(ctl_command, argv) < 0) | ||
{ | ||
streamer_printf(streamer_get(), "Error exec %s: %s\n", ctl_command, strerror(errno)); | ||
} | ||
exit(errno); | ||
} | ||
else | ||
{ | ||
// Parent process to wait on child. | ||
int status; | ||
wait(&status); | ||
error = (status) ? CHIP_ERROR_INCORRECT_STATE : CHIP_NO_ERROR; | ||
} | ||
|
||
exit: | ||
return error; | ||
} | ||
|
||
#endif // CHIP_TARGET_STYLE_UNIX | ||
|
||
static const shell_command_t cmds_otcli_root = { &cmd_otcli_dispatch, "otcli", "Dispatch OpenThread CLI command" }; | ||
|
||
#if CHIP_TARGET_STYLE_EMBEDDED | ||
static int OnOtCliInitialized(const char * aBuf, uint16_t aBufLength, void * aContext) | ||
{ | ||
ChipLogProgress(chipTool, "%s", aBuf); | ||
return 0; | ||
} | ||
#endif | ||
|
||
#endif // CHIP_ENABLE_OPENTHREAD | ||
|
||
void cmd_otcli_init(void) | ||
{ | ||
#if CHIP_ENABLE_OPENTHREAD | ||
#if CHIP_TARGET_STYLE_EMBEDDED | ||
otCliConsoleInit(GetOtInstance(), &OnOtCliInitialized, NULL); | ||
#endif | ||
|
||
// Register the root otcli command with the top-level shell. | ||
shell_register(&cmds_otcli_root, 1); | ||
#endif // CHIP_ENABLE_OPENTHREAD | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will probably needs more configurability in the future. Could use execvp().
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I switched to
execvp()
, but when running sudo, it still has trouble finding simplyot-cli
, even if I addexport PATH=$PATH:/usr/local/sbin
to/root/.bashrc
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a sudo policy biting:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe add a TODO for now to add some way to override (could just be a command line argument).