From 20014c72bf8ca02d9d576c708547226215e37426 Mon Sep 17 00:00:00 2001 From: Marcin K Szczodrak Date: Mon, 22 Aug 2016 14:08:45 -0700 Subject: [PATCH] add cli promiscuous (#445) --- src/cli/README.md | 29 ++++++++++++ src/cli/cli.cpp | 102 +++++++++++++++++++++++++++++++++++++++++++ src/cli/cli.hpp | 2 + src/cli/cli_uart.hpp | 2 +- 4 files changed, 134 insertions(+), 1 deletion(-) diff --git a/src/cli/README.md b/src/cli/README.md index a62303988..476f828fc 100644 --- a/src/cli/README.md +++ b/src/cli/README.md @@ -33,6 +33,7 @@ OpenThread test scripts use the CLI to execute test cases. * [ping](#ping) * [pollperiod](#pollperiod) * [prefix](#prefix) +* [promiscuous](#promiscuous) * [releaserouterid](#releaserouterid) * [reset](#reset) * [rloc16](#rloc16) @@ -613,6 +614,34 @@ Invalidate a prefix in the Network Data. Done ``` +### promiscuous + +Get radio promiscuous property. + +```bash +> promiscuous +Disabled +Done +``` + +### promiscuous enable + +Enable radio promiscuous operation and print raw packet content. + +```bash +> promiscuous enable +Done +``` + +### promiscuous disable + +Disable radio promiscuous operation. + +```bash +> promiscuous disable +Done +``` + ### releaserouterid \ Release a Router ID that has been allocated by the device in the Leader role. diff --git a/src/cli/cli.cpp b/src/cli/cli.cpp index 2f774c5a1..06762f933 100644 --- a/src/cli/cli.cpp +++ b/src/cli/cli.cpp @@ -81,6 +81,7 @@ const struct Command Interpreter::sCommands[] = { "parent", &ProcessParent }, { "ping", &ProcessPing }, { "pollperiod", &ProcessPollPeriod }, + { "promiscuous", &ProcessPromiscuous }, { "prefix", &ProcessPrefix }, { "releaserouterid", &ProcessReleaseRouterId }, { "reset", &ProcessReset }, @@ -1058,6 +1059,107 @@ exit: AppendResult(error); } +void Interpreter::ProcessPromiscuous(int argc, char *argv[]) +{ + ThreadError error = kThreadError_None; + + if (argc == 0) + { + if (otIsLinkPromiscuous() && otPlatRadioGetPromiscuous()) + { + sServer->OutputFormat("Enabled\r\n"); + } + else + { + sServer->OutputFormat("Disabled\r\n"); + } + } + else + { + if (strcmp(argv[0], "enable") == 0) + { + otSetLinkPcapCallback(&HandleLinkPcapReceive, NULL); + SuccessOrExit(error = otSetLinkPromiscuous(true)); + } + else if (strcmp(argv[0], "disable") == 0) + { + otSetLinkPcapCallback(NULL, NULL); + SuccessOrExit(error = otSetLinkPromiscuous(false)); + } + } + +exit: + AppendResult(error); +} + +void Interpreter::HandleLinkPcapReceive(const RadioPacket *aFrame, void *aContext) +{ + sServer->OutputFormat("\r\n"); + + for (size_t i = 0; i < 44; i++) + { + sServer->OutputFormat("="); + } + + sServer->OutputFormat("[len = %3u]", aFrame->mLength); + + for (size_t i = 0; i < 28; i++) + { + sServer->OutputFormat("="); + } + + sServer->OutputFormat("\r\n"); + + for (size_t i = 0; i < aFrame->mLength; i += 16) + { + sServer->OutputFormat("|"); + + for (size_t j = 0; j < 16; j++) + { + if (i + j < aFrame->mLength) + { + sServer->OutputFormat(" %02X", aFrame->mPsdu[i + j]); + } + else + { + sServer->OutputFormat(" .."); + } + } + + sServer->OutputFormat("|"); + + for (size_t j = 0; j < 16; j++) + { + if (i + j < aFrame->mLength) + { + if (31 < aFrame->mPsdu[i + j] && aFrame->mPsdu[i + j] < 127) + { + sServer->OutputFormat(" %c", aFrame->mPsdu[i + j]); + } + else + { + sServer->OutputFormat(" ?"); + } + } + else + { + sServer->OutputFormat(" ."); + } + } + + sServer->OutputFormat("|\r\n"); + } + + for (size_t i = 0; i < 83; i++) + { + sServer->OutputFormat("-"); + } + + sServer->OutputFormat("\r\n"); + + (void) aContext; +} + ThreadError Interpreter::ProcessPrefixAdd(int argc, char *argv[]) { ThreadError error = kThreadError_None; diff --git a/src/cli/cli.hpp b/src/cli/cli.hpp index c9a5daeb6..7471c4c81 100644 --- a/src/cli/cli.hpp +++ b/src/cli/cli.hpp @@ -163,6 +163,7 @@ private: static ThreadError ProcessPrefixAdd(int argc, char *argv[]); static ThreadError ProcessPrefixRemove(int argc, char *argv[]); static ThreadError ProcessPrefixList(void); + static void ProcessPromiscuous(int argc, char *argv[]); static void ProcessReleaseRouterId(int argc, char *argv[]); static void ProcessReset(int argc, char *argv[]); static void ProcessRoute(int argc, char *argv[]); @@ -187,6 +188,7 @@ private: static void HandlePingTimer(void *aContext); static void HandleActiveScanResult(otActiveScanResult *aResult, void *aContext); static void HandleNetifStateChanged(uint32_t aFlags, void *aContext); + static void HandleLinkPcapReceive(const RadioPacket *aFrame, void *aContext); static const struct Command sCommands[]; static otNetifAddress sAddress; diff --git a/src/cli/cli_uart.hpp b/src/cli/cli_uart.hpp index bf7126b98..c5faa2e47 100644 --- a/src/cli/cli_uart.hpp +++ b/src/cli/cli_uart.hpp @@ -79,7 +79,7 @@ private: enum { kRxBufferSize = 128, - kTxBufferSize = 512, + kTxBufferSize = 1024, kMaxLineLength = 128, };