Skip to content

Commit

Permalink
[diag] do not allow running diag send and diag repeat concurrently (
Browse files Browse the repository at this point in the history
openthread#11184)

The current diag module allows users running the 'diag send' and 'diag
repeat' concurrently. The command run later will change the settings
of the command run earlier, which will cause unexpected test results.

This commit does not allow running 'diag send' and 'diag repeat'
concurrently.
  • Loading branch information
zhanglongxia authored Feb 5, 2025
1 parent 62cac82 commit 64e0811
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 33 deletions.
77 changes: 45 additions & 32 deletions src/core/diags/factory_diags.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,9 @@ Diags::Diags(Instance &aInstance)
, mChannel(20)
, mTxPower(0)
, mTxLen(0)
, mCurTxCmd(kTxCmdNone)
, mIsTxPacketSet(false)
, mIsAsyncSend(false)
, mRepeatActive(false)
, mDiagSendOn(false)
, mOutputCallback(nullptr)
, mOutputContext(nullptr)
Expand Down Expand Up @@ -387,14 +387,15 @@ Error Diags::ProcessRepeat(uint8_t aArgsLength, char *aArgs[])
if (StringMatch(aArgs[0], "stop"))
{
otPlatAlarmMilliStop(&GetInstance());
mRepeatActive = false;
mCurTxCmd = kTxCmdNone;
}
else
{
uint32_t txPeriod;
uint8_t txLength;

VerifyOrExit(aArgsLength >= 1, error = kErrorInvalidArgs);
VerifyOrExit(mCurTxCmd == kTxCmdNone, error = kErrorInvalidState);

SuccessOrExit(error = Utils::CmdLineParser::ParseAsUint32(aArgs[0], txPeriod));
mTxPeriod = txPeriod;
Expand All @@ -415,11 +416,10 @@ Error Diags::ProcessRepeat(uint8_t aArgsLength, char *aArgs[])

VerifyOrExit((txLength >= OT_RADIO_FRAME_MIN_SIZE) && (txLength <= OT_RADIO_FRAME_MAX_SIZE),
error = kErrorInvalidArgs);
mTxLen = txLength;

mRepeatActive = true;
uint32_t now = otPlatAlarmMilliGetNow();
otPlatAlarmMilliStartAt(&GetInstance(), now, mTxPeriod);
mTxLen = txLength;
mCurTxCmd = kTxCmdRepeat;
otPlatAlarmMilliStartAt(&GetInstance(), otPlatAlarmMilliGetNow(), mTxPeriod);
}

exit:
Expand All @@ -433,6 +433,7 @@ Error Diags::ProcessSend(uint8_t aArgsLength, char *aArgs[])
uint8_t txLength;

VerifyOrExit(aArgsLength >= 1, error = kErrorInvalidArgs);
VerifyOrExit(mCurTxCmd == kTxCmdNone, error = kErrorInvalidState);

if (StringMatch(aArgs[0], "async"))
{
Expand Down Expand Up @@ -468,6 +469,7 @@ Error Diags::ProcessSend(uint8_t aArgsLength, char *aArgs[])
mTxLen = txLength;

SuccessOrExit(error = TransmitPacket());
mCurTxCmd = kTxCmdSend;

if (!mIsAsyncSend)
{
Expand Down Expand Up @@ -574,12 +576,14 @@ Error Diags::TransmitPacket(void)
}
}

mDiagSendOn = true;
error = Get<Radio>().Transmit(*static_cast<Mac::TxFrame *>(mTxPacket));

if (error == kErrorInvalidState)
error = Get<Radio>().Transmit(*static_cast<Mac::TxFrame *>(mTxPacket));
if (error == kErrorNone)
{
mStats.mSentErrorInvalidStatePackets++;
mDiagSendOn = true;
}
else
{
UpdateTxStats(error);
}

return error;
Expand Down Expand Up @@ -777,7 +781,7 @@ extern "C" void otPlatDiagAlarmFired(otInstance *aInstance) { AsCoreType(aInstan

void Diags::AlarmFired(void)
{
if (mRepeatActive)
if (mCurTxCmd == kTxCmdRepeat)
{
uint32_t now = otPlatAlarmMilliGetNow();

Expand Down Expand Up @@ -872,26 +876,8 @@ void Diags::TransmitDone(Error aError)
IgnoreError(Get<Radio>().Sleep());
}

switch (aError)
{
case kErrorNone:
mStats.mSentSuccessPackets++;
break;

case kErrorChannelAccessFailure:
mStats.mSentErrorCcaPackets++;
break;

case kErrorAbort:
mStats.mSentErrorAbortPackets++;
break;

default:
mStats.mSentErrorOthersPackets++;
break;
}

VerifyOrExit(!mRepeatActive && (mTxPackets > 0));
UpdateTxStats(aError);
VerifyOrExit((mCurTxCmd == kTxCmdSend) && (mTxPackets > 0));

if (mTxPackets > 1)
{
Expand All @@ -901,6 +887,7 @@ void Diags::TransmitDone(Error aError)
else
{
mTxPackets = 0;
mCurTxCmd = kTxCmdNone;

if (!mIsAsyncSend)
{
Expand All @@ -926,6 +913,32 @@ bool Diags::ShouldHandleReceivedFrame(const otRadioFrame &aFrame) const
return ret;
}

void Diags::UpdateTxStats(Error aError)
{
switch (aError)
{
case kErrorNone:
mStats.mSentSuccessPackets++;
break;

case kErrorChannelAccessFailure:
mStats.mSentErrorCcaPackets++;
break;

case kErrorAbort:
mStats.mSentErrorAbortPackets++;
break;

case kErrorInvalidState:
mStats.mSentErrorInvalidStatePackets++;
break;

default:
mStats.mSentErrorOthersPackets++;
break;
}
}

#endif // OPENTHREAD_RADIO

Error Diags::ProcessContinuousWave(uint8_t aArgsLength, char *aArgs[])
Expand Down
10 changes: 9 additions & 1 deletion src/core/diags/factory_diags.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,12 +240,20 @@ class Diags : public InstanceLocator, private NonCopyable
void Output(const char *aFormat, ...);
void ResetTxPacket(void);
void OutputStats(void);
void UpdateTxStats(Error aError);

static bool IsChannelValid(uint8_t aChannel);

static const struct Command sCommands[];

#if OPENTHREAD_FTD || OPENTHREAD_MTD || (OPENTHREAD_RADIO && OPENTHREAD_RADIO_CLI)
enum TxCmd : uint8_t
{
kTxCmdNone,
kTxCmdRepeat,
kTxCmdSend,
};

Stats mStats;

otRadioFrame *mTxPacket;
Expand All @@ -254,10 +262,10 @@ class Diags : public InstanceLocator, private NonCopyable
uint8_t mChannel;
int8_t mTxPower;
uint8_t mTxLen;
TxCmd mCurTxCmd;
bool mIsHeaderUpdated : 1;
bool mIsTxPacketSet : 1;
bool mIsAsyncSend : 1;
bool mRepeatActive : 1;
bool mDiagSendOn : 1;
bool mIsSleepOn : 1;
#endif
Expand Down
12 changes: 12 additions & 0 deletions tests/scripts/expect/cli-diags.exp
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,18 @@ expect_line "Done"
send "diag radio receive\n"
expect_line "Done"

send "diag repeat 100 100\n"
expect "Done"

send "diag send 1 10\n"
expect "Error 13: InvalidState"

send "diag repeat stop\n"
expect "Done"

send "diag send 1 10\n"
expect "Done"

send_user "shortest frame test\n"
send "diag frame 112233\n"
expect "Done"
Expand Down

0 comments on commit 64e0811

Please sign in to comment.