Skip to content

Commit

Permalink
[diag] add 'diag radio receive filter' command support
Browse files Browse the repository at this point in the history
  • Loading branch information
zhanglongxia committed Dec 16, 2024
1 parent 971f05f commit 78febc3
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 23 deletions.
33 changes: 33 additions & 0 deletions src/core/diags/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,39 @@ Set the radio to receive mode and receive a specified number of frames.
Done
```

### diag radio receive filter enable

Enable the diag module to receive only frames with a specified destination address.

```bash
> diag radio receive filter enable
Done
```

### diag radio receive filter disable

Disable the diag module from receiving only frames with a specified destination address.

```bash
> diag radio receive filter disable
Done
```

### diag radio receive filter \<destaddress\>

Set the destination address of the radio receive filter.

- destaddress: The destination mac address. It can be a short, extended or empty mac address. Input '-' to specify an empty mac address.

```bash
> diag radio receive filter -
Done
> diag radio receive filter 0x0a17
Done
> diag radio receive filter dead00beef00cafe
Done
```

### diag radio state

Return the state of the radio.
Expand Down
129 changes: 113 additions & 16 deletions src/core/diags/factory_diags.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -657,31 +657,81 @@ Error Diags::ProcessRadio(uint8_t aArgsLength, char *aArgs[])
ExitNow();
}

if (StringMatch(aArgs[0], "async"))
if (StringMatch(aArgs[0], "filter"))
{
aArgs++;
aArgsLength--;
receiveConfig.mIsAsyncCommand = true;
}

VerifyOrExit(aArgsLength > 0);
SuccessOrExit(error = Utils::CmdLineParser::ParseAsUint16(aArgs[0], receiveConfig.mNumFrames));
aArgs++;
aArgsLength--;
VerifyOrExit(aArgsLength > 0);

if (aArgsLength > 0)
{
SuccessOrExit(error = ParseReceiveConfigFormat(aArgs[0], receiveConfig));
if (StringMatch(aArgs[0], "enable"))
{
mReceiveConfig.mIsFilterEnabled = true;
error = kErrorNone;
}
else if (StringMatch(aArgs[0], "disable"))
{
mReceiveConfig.mIsFilterEnabled = false;
error = kErrorNone;
}
else
{
Mac::Address dstAddress;

if (StringMatch(aArgs[0], "-"))
{
dstAddress.SetNone();
error = kErrorNone;
}
else if (strlen(aArgs[0]) == 2 * sizeof(Mac::ExtAddress))
{
Mac::ExtAddress extAddress;

SuccessOrExit(error = Utils::CmdLineParser::ParseAsHexString(aArgs[0], extAddress.m8));
mReceiveConfig.mFilterAddress.SetExtended(extAddress);
}
else
{
Mac::ShortAddress shortAddress;

SuccessOrExit(error = Utils::CmdLineParser::ParseAsUint16(aArgs[0], shortAddress));
mReceiveConfig.mFilterAddress.SetShort(shortAddress);
}
}
}
else
{
if (StringMatch(aArgs[0], "async"))
{
aArgs++;
aArgsLength--;
receiveConfig.mIsAsyncCommand = true;
}

SuccessOrExit(error = RadioReceive());
VerifyOrExit(aArgsLength > 0);
SuccessOrExit(error = Utils::CmdLineParser::ParseAsUint16(aArgs[0], receiveConfig.mNumFrames));
aArgs++;
aArgsLength--;

receiveConfig.mIsEnabled = true;
mReceiveConfig = receiveConfig;
if (aArgsLength > 0)
{
SuccessOrExit(error = ParseReceiveConfigFormat(aArgs[0], receiveConfig));
}

if (!mReceiveConfig.mIsAsyncCommand)
{
error = kErrorPending;
SuccessOrExit(error = RadioReceive());

mReceiveConfig.mIsEnabled = true;
mReceiveConfig.mIsAsyncCommand = receiveConfig.mIsAsyncCommand;
mReceiveConfig.mShowRssi = receiveConfig.mShowRssi;
mReceiveConfig.mShowLqi = receiveConfig.mShowLqi;
mReceiveConfig.mShowPsdu = receiveConfig.mShowPsdu;
mReceiveConfig.mReceiveCount = receiveConfig.mReceiveCount;
mReceiveConfig.mNumFrames = receiveConfig.mNumFrames;

if (!mReceiveConfig.mIsAsyncCommand)
{
error = kErrorPending;
}
}
}
else if (StringMatch(aArgs[0], "state"))
Expand Down Expand Up @@ -790,6 +840,11 @@ void Diags::ReceiveDone(otRadioFrame *aFrame, Error aError)
{
if (aError == kErrorNone)
{
if (mReceiveConfig.mIsFilterEnabled)
{
VerifyOrExit(ShouldHandleReceivedFrame(*aFrame));
}

OutputReceivedFrame(aFrame);

// for sensitivity test, only record the rssi and lqi for the first and last packet
Expand All @@ -806,6 +861,9 @@ void Diags::ReceiveDone(otRadioFrame *aFrame, Error aError)
}

otPlatDiagRadioReceived(&GetInstance(), aFrame, aError);

exit:
return;
}

void Diags::TransmitDone(Error aError)
Expand Down Expand Up @@ -834,6 +892,45 @@ void Diags::TransmitDone(Error aError)
return;
}

bool Diags::AddressMatch(const Mac::Address &aAddressA, const Mac::Address &aAddressB)
{
bool ret = false;

VerifyOrExit(aAddressA.GetType() == aAddressB.GetType());

switch (aAddressA.GetType())
{
case Mac::Address::kTypeNone:
ret = true;
break;

case Mac::Address::kTypeShort:
VerifyOrExit(aAddressA.GetShort() != aAddressB.GetShort(), ret = true);
break;

case Mac::Address::kTypeExtended:
ret = (memcmp(aAddressA.GetExtended().m8, aAddressB.GetExtended().m8, sizeof(Mac::ExtAddress)) == 0);
break;
}

exit:
return ret;
}

bool Diags::ShouldHandleReceivedFrame(const otRadioFrame &aFrame)
{
bool ret = false;
const Mac::RxFrame *frame = static_cast<const Mac::RxFrame *>(&aFrame);
Mac::Address dstAddress;

VerifyOrExit(frame->GetDstAddr(dstAddress) == kErrorNone);
VerifyOrExit(AddressMatch(dstAddress, mReceiveConfig.mFilterAddress));
ret = true;

exit:
return ret;
}

#endif // OPENTHREAD_RADIO

Error Diags::ProcessContinuousWave(uint8_t aArgsLength, char *aArgs[])
Expand Down
22 changes: 15 additions & 7 deletions src/core/diags/factory_diags.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#include "common/locator.hpp"
#include "common/non_copyable.hpp"
#include "common/string.hpp"
#include "mac/mac_types.hpp"

namespace ot {
namespace FactoryDiags {
Expand Down Expand Up @@ -185,18 +186,23 @@ class Diags : public InstanceLocator, private NonCopyable
, mShowRssi(true)
, mShowLqi(true)
, mShowPsdu(false)
, mIsFilterEnabled(false)
, mReceiveCount(0)
, mNumFrames(0)
, mFilterAddress()
{
}

bool mIsEnabled : 1;
bool mIsAsyncCommand : 1;
bool mShowRssi : 1;
bool mShowLqi : 1;
bool mShowPsdu : 1;
uint16_t mReceiveCount;
uint16_t mNumFrames;
bool mIsEnabled : 1;
bool mIsAsyncCommand : 1;
bool mShowRssi : 1;
bool mShowLqi : 1;
bool mShowPsdu : 1;
bool mIsFilterEnabled : 1;

uint16_t mReceiveCount;
uint16_t mNumFrames;
Mac::Address mFilterAddress;
};

Error ParseCmd(char *aString, uint8_t &aArgsLength, char *aArgs[]);
Expand All @@ -223,6 +229,8 @@ class Diags : public InstanceLocator, private NonCopyable
Error ParseReceiveConfigFormat(const char *aFormat, ReceiveConfig &aConfig);
Error RadioReceive(void);
void OutputReceivedFrame(const otRadioFrame *aFrame);
bool ShouldHandleReceivedFrame(const otRadioFrame &aFrame);
bool AddressMatch(const Mac::Address &aAddressA, const Mac::Address &aAddressB);

void TransmitPacket(void);
void Output(const char *aFormat, ...);
Expand Down

0 comments on commit 78febc3

Please sign in to comment.