Skip to content

Commit

Permalink
[diag] add 'diag send async' command support
Browse files Browse the repository at this point in the history
  • Loading branch information
zhanglongxia committed Jan 6, 2025
1 parent 173cb61 commit 8bf50bf
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 12 deletions.
11 changes: 9 additions & 2 deletions src/core/diags/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ The diagnostics module supports common diagnostics features that are listed belo
- [diag stream](#diag-stream-start)
- [diag power](#diag-power)
- [diag powersettings](#diag-powersettings)
- [diag send](#diag-send-packets-length)
- [diag send](#diag-send-async-packets-length)
- [diag repeat](#diag-repeat-delay-length)
- [diag radio](#diag-radio-sleep)
- [diag rawpowersetting](#diag-rawpowersetting)
Expand Down Expand Up @@ -166,16 +166,23 @@ RawPowerSetting: 223344
Done
```

### diag send \<packets\> [length]
### diag send [async] \<packets\> [length]

Transmit a fixed number of packets.

- async: Use the non-blocking mode.
- packets: The number of packets to be sent.
- length: The length of the packet.

Send the frame set by `diag frame` if length is omitted. Otherwise overwrite the frame set by `diag frame` and send a frame of the given length(MUST be in range [3, 127]).

```bash
> diag send 20 100
sending 0x14 packet(s), length 0x64
Done
> diag send async 20 100
sending 0x14 packet(s), length 0x64
Done
```

### diag repeat \<delay\> [length]
Expand Down
30 changes: 27 additions & 3 deletions src/core/diags/factory_diags.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ Diags::Diags(Instance &aInstance)
, mTxPower(0)
, mTxLen(0)
, mIsTxPacketSet(false)
, mIsAsyncSend(false)
, mRepeatActive(false)
, mDiagSendOn(false)
, mOutputCallback(nullptr)
Expand Down Expand Up @@ -436,6 +437,18 @@ Error Diags::ProcessSend(uint8_t aArgsLength, char *aArgs[])

VerifyOrExit(aArgsLength >= 1, error = kErrorInvalidArgs);

if (StringMatch(aArgs[0], "async"))
{
aArgs++;
aArgsLength--;
VerifyOrExit(aArgsLength >= 1, error = kErrorInvalidArgs);
mIsAsyncSend = true;
}
else
{
mIsAsyncSend = false;
}

SuccessOrExit(error = Utils::CmdLineParser::ParseAsUint32(aArgs[0], txPackets));
mTxPackets = txPackets;

Expand All @@ -460,6 +473,11 @@ Error Diags::ProcessSend(uint8_t aArgsLength, char *aArgs[])
Output("sending %#x packet(s), length %#x\r\n", static_cast<int>(mTxPackets), static_cast<int>(mTxLen));
TransmitPacket();

if (!mIsAsyncSend)
{
error = kErrorPending;
}

exit:
return error;
}
Expand Down Expand Up @@ -868,10 +886,16 @@ void Diags::TransmitDone(Error aError)
}

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

TransmitPacket();
if (mTxPackets > 1)
{
mTxPackets--;
TransmitPacket();
}
else if (!mIsAsyncSend)
{
Output("OT_ERROR_NONE");
}

exit:
return;
Expand Down
1 change: 1 addition & 0 deletions src/core/diags/factory_diags.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ class Diags : public InstanceLocator, private NonCopyable
uint8_t mTxLen;
bool mIsHeaderUpdated : 1;
bool mIsTxPacketSet : 1;
bool mIsAsyncSend : 1;
bool mRepeatActive : 1;
bool mDiagSendOn : 1;
#endif
Expand Down
21 changes: 19 additions & 2 deletions tests/scripts/expect/cli-diags.exp
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,25 @@ send "diag send 10 100\n"
expect "sending 0xa packet(s), length 0x64"
expect_line "Done"

send "diag stats\n"
expect "received packets: 0"
expect "sent success packets: 10"
expect "sent error cca packets: 0"
expect "sent error abort packets: 0"
expect "sent error others packets: 0"
expect "first received packet: rssi=0, lqi=0"
expect "last received packet: rssi=0, lqi=0"
expect_line "Done"

send "diag send async 10 100\n"
expect "sending 0xa packet(s), length 0x64"
expect_line "Done"

sleep 2

send "diag stats\n"
expect "received packets: 0"
expect "sent success packets: 10"
expect "sent success packets: 20"
expect "sent error cca packets: 0"
expect "sent error abort packets: 0"
expect "sent error others packets: 0"
Expand All @@ -79,7 +93,7 @@ expect_line "Done"
switch_node 1

send "diag stats\n"
expect "received packets: 10"
expect "received packets: 20"
expect "sent success packets: 0"
expect "sent error cca packets: 0"
expect "sent error abort packets: 0"
Expand Down Expand Up @@ -153,6 +167,9 @@ send "diag repeat 1\n"
expect "length 0x7f"
expect "Done"

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

send_user "send frame with security processed\n"
send "diag frame -s 112233\n"
expect "Done"
Expand Down
11 changes: 6 additions & 5 deletions tools/otci/otci/otci.py
Original file line number Diff line number Diff line change
Expand Up @@ -2562,12 +2562,13 @@ def diag_stream_stop(self):
"""Stop transmitting a stream of characters."""
self.execute_command('diag stream stop')

def diag_send(self, packets: int, length: Optional[int] = None):
def diag_send(self, packets: int, length: Optional[int] = None, is_async: bool = True):
"""Transmit a fixed number of packets."""
if length is None:
command = f'diag send {packets}'
else:
command = f'diag send {packets} {length}'
command = 'diag send '
command += 'async ' if is_async else ''
command += f'{packets} '
command += f'{length}' if length is not None else ''

self.execute_command(command)

def diag_repeat(self, delay: int, length: Optional[int] = None):
Expand Down

0 comments on commit 8bf50bf

Please sign in to comment.