From 95a4c338e0f85bd36fbb117d7aab253191380506 Mon Sep 17 00:00:00 2001 From: Yakun Xu Date: Wed, 28 Aug 2024 09:35:04 +0800 Subject: [PATCH] [diag] support send security processed frames (#10640) This commit extends the `diag frame` command by adding an argument `-s` to indicate whether the frame has been encrypted or not, so that the diag commands can be used to test frames having security processed in the host. --- src/core/diags/README.md | 7 +++++- src/core/diags/factory_diags.cpp | 39 +++++++++++++++++++++++++++--- src/core/diags/factory_diags.hpp | 1 + tests/scripts/expect/cli-diags.exp | 7 ++++++ 4 files changed, 49 insertions(+), 5 deletions(-) diff --git a/src/core/diags/README.md b/src/core/diags/README.md index ee447b3f9aa..7af2f608455 100644 --- a/src/core/diags/README.md +++ b/src/core/diags/README.md @@ -10,6 +10,7 @@ The diagnostics module supports common diagnostics features that are listed belo - [diag start](#diag-start) - [diag channel](#diag-channel) - [diag cw](#diag-cw-start) +- [diag frame](#diag-frame) - [diag stream](#diag-stream-start) - [diag power](#diag-power) - [diag powersettings](#diag-powersettings) @@ -77,10 +78,14 @@ Stop transmitting continuous carrier wave. Done ``` -### diag frame \ +### diag frame + +Usage: `diag frame [-s] ` Set the frame (hex encoded) to be used by `diag send` and `diag repeat`. The frame may be overwritten by `diag send` and `diag repeat`. +Specify `-s` to skip security processing in radio layer. + ```bash > diag frame 11223344 Done diff --git a/src/core/diags/factory_diags.cpp b/src/core/diags/factory_diags.cpp index b6fe8645bb5..b0691e10da9 100644 --- a/src/core/diags/factory_diags.cpp +++ b/src/core/diags/factory_diags.cpp @@ -33,6 +33,7 @@ #include "factory_diags.hpp" #include "common/error.hpp" +#include "openthread/platform/radio.h" #if OPENTHREAD_CONFIG_DIAG_ENABLE @@ -216,18 +217,47 @@ Diags::Diags(Instance &aInstance) mStats.Clear(); } +void Diags::ResetTxPacket(void) +{ + mTxPacket->mInfo.mTxInfo.mTxDelayBaseTime = 0; + mTxPacket->mInfo.mTxInfo.mTxDelay = 0; + mTxPacket->mInfo.mTxInfo.mMaxCsmaBackoffs = 0; + mTxPacket->mInfo.mTxInfo.mMaxFrameRetries = 0; + mTxPacket->mInfo.mTxInfo.mRxChannelAfterTxDone = mChannel; + mTxPacket->mInfo.mTxInfo.mTxPower = OT_RADIO_POWER_INVALID; + mTxPacket->mInfo.mTxInfo.mIsHeaderUpdated = false; + mTxPacket->mInfo.mTxInfo.mIsARetx = false; + mTxPacket->mInfo.mTxInfo.mCsmaCaEnabled = false; + mTxPacket->mInfo.mTxInfo.mCslPresent = false; + mTxPacket->mInfo.mTxInfo.mIsSecurityProcessed = false; +} + Error Diags::ProcessFrame(uint8_t aArgsLength, char *aArgs[]) { - Error error = kErrorNone; - uint16_t size = OT_RADIO_FRAME_MAX_SIZE; + Error error = kErrorNone; + uint16_t size = OT_RADIO_FRAME_MAX_SIZE; + bool securityProcessed = false; + + if (aArgsLength >= 1) + { + if (StringMatch(aArgs[0], "-s")) + { + securityProcessed = true; + aArgs++; + aArgsLength--; + } + } VerifyOrExit(aArgsLength == 1, error = kErrorInvalidArgs); SuccessOrExit(error = Utils::CmdLineParser::ParseAsHexString(aArgs[0], size, mTxPacket->mPsdu)); VerifyOrExit(size <= OT_RADIO_FRAME_MAX_SIZE, error = kErrorInvalidArgs); VerifyOrExit(size >= OT_RADIO_FRAME_MIN_SIZE, error = kErrorInvalidArgs); - mTxPacket->mLength = size; - mIsTxPacketSet = true; + + ResetTxPacket(); + mTxPacket->mInfo.mTxInfo.mIsSecurityProcessed = securityProcessed; + mTxPacket->mLength = size; + mIsTxPacketSet = true; exit: AppendErrorResult(error); @@ -468,6 +498,7 @@ void Diags::TransmitPacket(void) if (!mIsTxPacketSet) { + ResetTxPacket(); mTxPacket->mLength = mTxLen; for (uint8_t i = 0; i < mTxLen; i++) diff --git a/src/core/diags/factory_diags.hpp b/src/core/diags/factory_diags.hpp index 01864786d20..db9fd8583ad 100644 --- a/src/core/diags/factory_diags.hpp +++ b/src/core/diags/factory_diags.hpp @@ -210,6 +210,7 @@ class Diags : public InstanceLocator, private NonCopyable void TransmitPacket(void); void Output(const char *aFormat, ...); void AppendErrorResult(Error aError); + void ResetTxPacket(void); static Error ParseLong(char *aString, long &aLong); static Error ParseBool(char *aString, bool &aBool); diff --git a/tests/scripts/expect/cli-diags.exp b/tests/scripts/expect/cli-diags.exp index b9ab58071a6..3f26628d012 100755 --- a/tests/scripts/expect/cli-diags.exp +++ b/tests/scripts/expect/cli-diags.exp @@ -152,6 +152,13 @@ send "diag repeat 1\n" expect "length 0x7f" expect "Done" +send_user "send frame with security processed\n" +send "diag frame -s 112233\n" +expect "Done" +send "diag send 1\n" +expect "length 0x3" +expect "Done" + send "diag repeat stop\n" expect "Done"