Skip to content

Commit

Permalink
[diag] count the number of packets that are sent succeed and failed
Browse files Browse the repository at this point in the history
When using the `diag frame -c xxxx` command to enable the CSMA-CA when
transmitting the frame, the command `diag send` won't output any message
the CCA failure happens. It is difficult for users to know whether
the CSMA-CA is actually effective via diag commands.

This commit counts the number of packets that are sent succeed and failed,
outputs the transmision failure reason and do not re-transmit the frame
after it fails to send.
  • Loading branch information
zhanglongxia committed Jan 2, 2025
1 parent 1024a1f commit 2ad1c8f
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 29 deletions.
6 changes: 4 additions & 2 deletions src/core/diags/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,8 @@ Print statistics during diagnostics mode.
```bash
> diag stats
received packets: 10
sent packets: 10
sent success packets: 10
sent failed packets: 0
first received packet: rssi=-65, lqi=101
last received packet: rssi=-64, lqi=98
Done
Expand Down Expand Up @@ -417,7 +418,8 @@ Stop diagnostics mode and print statistics.
```bash
> diag stop
received packets: 10
sent packets: 10
sent success packets: 10
sent failed packets: 0
first received packet: rssi=-65, lqi=101
last received packet: rssi=-61, lqi=98

Expand Down
44 changes: 21 additions & 23 deletions src/core/diags/factory_diags.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,15 @@ Error Diags::ProcessStart(uint8_t aArgsLength, char *aArgs[])
return error;
}

void Diags::OutputStats(void)
{
Output("received packets: %lu\r\nsent success packets: %lu\r\nsent failed packets: %lu\r\n"
"first received packet: rssi=%d, lqi=%d\r\n"
"last received packet: rssi=%d, lqi=%d\r\n",
ToUlong(mStats.mReceivedPackets), ToUlong(mStats.mSentSuccessPackets), ToUlong(mStats.mSentFailedPackets),
mStats.mFirstRssi, mStats.mFirstLqi, mStats.mLastRssi, mStats.mLastLqi);
}

Error Diags::ProcessStats(uint8_t aArgsLength, char *aArgs[])
{
Error error = kErrorNone;
Expand All @@ -504,12 +513,7 @@ Error Diags::ProcessStats(uint8_t aArgsLength, char *aArgs[])
else
{
VerifyOrExit(aArgsLength == 0, error = kErrorInvalidArgs);
Output("received packets: %d\r\nsent packets: %d\r\n"
"first received packet: rssi=%d, lqi=%d\r\n"
"last received packet: rssi=%d, lqi=%d\r\n",
static_cast<int>(mStats.mReceivedPackets), static_cast<int>(mStats.mSentPackets),
static_cast<int>(mStats.mFirstRssi), static_cast<int>(mStats.mFirstLqi),
static_cast<int>(mStats.mLastRssi), static_cast<int>(mStats.mLastLqi));
OutputStats();
}

exit:
Expand All @@ -526,13 +530,8 @@ Error Diags::ProcessStop(uint8_t aArgsLength, char *aArgs[])
Get<Radio>().SetPromiscuous(false);
Get<Mac::SubMac>().SetRxOnWhenIdle(false);

Output("received packets: %d\r\nsent packets: %d\r\n"
"first received packet: rssi=%d, lqi=%d\r\n"
"last received packet: rssi=%d, lqi=%d\r\n"
"\nstop diagnostics mode\r\n",
static_cast<int>(mStats.mReceivedPackets), static_cast<int>(mStats.mSentPackets),
static_cast<int>(mStats.mFirstRssi), static_cast<int>(mStats.mFirstLqi), static_cast<int>(mStats.mLastRssi),
static_cast<int>(mStats.mLastLqi));
OutputStats();
Output("\nstop diagnostics mode\r\n");

return kErrorNone;
}
Expand Down Expand Up @@ -846,19 +845,18 @@ void Diags::TransmitDone(Error aError)

if (aError == kErrorNone)
{
mStats.mSentPackets++;

if (mTxPackets > 1)
{
mTxPackets--;
}
else
{
ExitNow();
}
mStats.mSentSuccessPackets++;
}
else
{
mStats.mSentFailedPackets++;
Output("Failed to transmit the frame, error: %s\r\n", ErrorToString(aError));
}

VerifyOrExit(!mRepeatActive);
VerifyOrExit(mTxPackets > 1);
mTxPackets--;

TransmitPacket();

exit:
Expand Down
4 changes: 3 additions & 1 deletion src/core/diags/factory_diags.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ class Diags : public InstanceLocator, private NonCopyable
struct Stats : public Clearable<Stats>
{
uint32_t mReceivedPackets;
uint32_t mSentPackets;
uint32_t mSentSuccessPackets;
uint32_t mSentFailedPackets;
int8_t mFirstRssi;
uint8_t mFirstLqi;
int8_t mLastRssi;
Expand Down Expand Up @@ -235,6 +236,7 @@ class Diags : public InstanceLocator, private NonCopyable
void Output(const char *aFormat, ...);
void AppendErrorResult(Error aError);
void ResetTxPacket(void);
void OutputStats(void);

static bool IsChannelValid(uint8_t aChannel);

Expand Down
9 changes: 6 additions & 3 deletions tests/scripts/expect/cli-diags.exp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ sleep 2

send "diag stats\n"
expect "received packets: 0"
expect "sent packets: 10"
expect "sent success packets: 10"
expect "sent failed packets: 0"
expect "first received packet: rssi=0, lqi=0"
expect "last received packet: rssi=0, lqi=0"
expect_line "Done"
Expand All @@ -77,7 +78,8 @@ switch_node 1

send "diag stats\n"
expect "received packets: 10"
expect "sent packets: 0"
expect "sent success packets: 0"
expect "sent failed packets: 0"
expect "first received packet: rssi=-20, lqi=0"
expect "last received packet: rssi=-20, lqi=0"
expect_line "Done"
Expand All @@ -100,7 +102,8 @@ switch_node 1

send "diag stats\n"
expect -r {received packets: \d+}
expect "sent packets: 0"
expect "sent success packets: 0"
expect "sent failed packets: 0"
expect "first received packet: rssi=-20, lqi=0"
expect "last received packet: rssi=-20, lqi=0"
expect_line "Done"
Expand Down
2 changes: 2 additions & 0 deletions tools/otci/otci/otci.py
Original file line number Diff line number Diff line change
Expand Up @@ -2613,6 +2613,8 @@ def diag_get_stats(self) -> Dict[str, int]:
result['last_received_packet_rssi'] = int(values[0])
result['last_received_packet_lqi'] = int(values[1])

result['sent_failed_packets'] = int(output[4].split(":")[1])

return result

def diag_stats_clear(self):
Expand Down

0 comments on commit 2ad1c8f

Please sign in to comment.