Skip to content

Commit

Permalink
[posix] check if the RCP supports specified diag commands
Browse files Browse the repository at this point in the history
  • Loading branch information
zhanglongxia committed Jun 17, 2024
1 parent 6bc3b4d commit 460a91e
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 11 deletions.
25 changes: 25 additions & 0 deletions src/posix/platform/README_RCP_CAPS_DIAG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ This module provides diag commands for checking RCP capabilities.

## Command List

- [diagcmd](#diagcmd)
- [spinel](#spinel)

## Command Details
Expand All @@ -30,3 +31,27 @@ Optional :
PROP_VALUE_GET PHY_CCA_THRESHOLD -------------------------- OK
Done
```

### spinel

Check which diag commands RCP supports.

```bash
> diag rcpcaps diagcmd

Diag Commands :
diag channel 20 ------------------------------------------- OK
diag cw start --------------------------------------------- OK
diag cw stop ---------------------------------------------- OK
diag power 10 --------------------------------------------- OK
diag echo 112233 ------------------------------------------ OK
diag echo -n 200 ------------------------------------------ OK
diag stream start ----------------------------------------- OK
diag stream stop ------------------------------------------ OK
diag rawpowersetting enable ------------------------------- OK
diag rawpowersetting 112233 ------------------------------- OK
diag rawpowersetting -------------------------------------- OK
diag powersettings 20 ------------------------------------- NotFound
diag rawpowersetting disable ------------------------------ OK
Done
```
83 changes: 72 additions & 11 deletions src/posix/platform/rcp_caps_diag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ otError RcpCapsDiag::DiagProcess(char *aArgs[], uint8_t aArgsLength, char *aOutp
{
ProcessSpinel();
}
else if (strcmp(aArgs[1], "diagcmd") == 0)
{
ProcessDiagCommands();
}
else
{
error = OT_ERROR_INVALID_COMMAND;
Expand Down Expand Up @@ -137,22 +141,79 @@ void RcpCapsDiag::TestSpinelCommands(Category aCategory)
}
}

void RcpCapsDiag::ProcessDiagCommands(void)
{
static const char *kDiagCommands[] = {"channel 20",
"cw start",
"cw stop",
"power 10",
"echo 112233",
"echo -n 100",
"stream start",
"stream stop",
"rawpowersetting enable",
"rawpowersetting 112233",
"rawpowersetting",
"powersettings 20",
"rawpowersetting disable"};

Output("\r\nDiag Commands :\r\n");

for (uint16_t i = 0; i < OT_ARRAY_LENGTH(kDiagCommands); i++)
{
TestDiagCommand(kDiagCommands[i]);
}
}

void RcpCapsDiag::TestDiagCommand(const char *aCommand)
{
char cmd[OPENTHREAD_CONFIG_DIAG_CMD_LINE_BUFFER_SIZE];
char output[OPENTHREAD_CONFIG_DIAG_OUTPUT_BUFFER_SIZE] = {0};
otError error;

snprintf(cmd, sizeof(cmd), "diag %s", aCommand);
SuccessOrExit(error = mRadioSpinel.PlatDiagProcess(aCommand, output, sizeof(output)));

if (strstr(output, "not supported") != nullptr)
{
error = kErrorInvalidCommand;
}
else if ((strstr(output, "failed") != nullptr) && (strstr(output, "status") != nullptr))
{
char *str = strstr(output, "status");
VerifyOrExit(sscanf(str, "status %x", reinterpret_cast<uint32_t *>(&error)) == 1, error = OT_ERROR_FAILED);
}

exit:
OutputFormat(cmd, otThreadErrorToString(error));
}

void RcpCapsDiag::OutputFormat(const char *aName, const char *aValue)
{
static constexpr uint8_t kMaxLength = 56;
static const char kPadding[] = "----------------------------------------------------------";
uint16_t actualLength = static_cast<uint16_t>(strlen(aName));
uint16_t paddingOffset = (actualLength > kMaxLength) ? kMaxLength : actualLength;

static_assert(kMaxLength < sizeof(kPadding), "Padding bytes are too short");

Output("%.*s %s %s\r\n", kMaxLength, aName, &kPadding[paddingOffset], aValue);
}

void RcpCapsDiag::OutputResult(const SpinelEntry &aEntry, otError error)
{
static constexpr uint8_t kSpaceLength = 1;
static constexpr uint8_t kMaxCommandStringLength = 20;
static constexpr uint8_t kMaxKeyStringLength = 35;
static constexpr uint16_t kMaxLength = kMaxCommandStringLength + kMaxKeyStringLength + kSpaceLength;
static const char kPadding[] = "----------------------------------------------------------";
const char *commandString = spinel_command_to_cstr(aEntry.mCommand);
const char *keyString = spinel_prop_key_to_cstr(aEntry.mKey);
uint16_t actualLength = static_cast<uint16_t>(strlen(commandString) + strlen(keyString) + kSpaceLength);
uint16_t paddingOffset = (actualLength > kMaxLength) ? kMaxLength : actualLength;

static_assert(kMaxLength < sizeof(kPadding), "Padding bytes are too short");

Output("%.*s %.*s %s %s\r\n", kMaxCommandStringLength, commandString, kMaxKeyStringLength, keyString,
&kPadding[paddingOffset], otThreadErrorToString(error));
static constexpr uint16_t kMaxLength =
kMaxCommandStringLength + kMaxKeyStringLength + kSpaceLength + 1 /* size of '\0' */;
char buffer[kMaxLength] = {0};
const char *commandString = spinel_command_to_cstr(aEntry.mCommand);
const char *keyString = spinel_prop_key_to_cstr(aEntry.mKey);

snprintf(buffer, sizeof(buffer), "%.*s %.*s", kMaxCommandStringLength, commandString, kMaxKeyStringLength,
keyString);
OutputFormat(buffer, otThreadErrorToString(error));
}

void RcpCapsDiag::Output(const char *aFormat, ...)
Expand Down
3 changes: 3 additions & 0 deletions src/posix/platform/rcp_caps_diag.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,10 @@ class RcpCapsDiag
};

void ProcessSpinel(void);
void ProcessDiagCommands(void);
void TestSpinelCommands(Category aCategory);
void TestDiagCommand(const char *aCommand);
void OutputFormat(const char *aName, const char *aValue);
void OutputResult(const SpinelEntry &aEntry, otError error);
void Output(const char *aFormat, ...);

Expand Down

0 comments on commit 460a91e

Please sign in to comment.