Skip to content

Commit

Permalink
[tcat] Implementation of tcat Ping command.
Browse files Browse the repository at this point in the history
Commit implements Tcat ping command `kTlvPing`.

Signed-off-by: Przemyslaw Bida <[email protected]>
  • Loading branch information
canisLupus1313 committed Jul 5, 2024
1 parent 000f5fc commit cf13269
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 8 deletions.
33 changes: 33 additions & 0 deletions src/core/meshcop/tcat_agent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,21 @@ Error TcatAgent::HandleSingleTlv(const Message &aIncommingMessage, Message &aOut
case kTlvDecommission:
error = HandleDecomission();
break;
case kTlvPing:
if (tlv.IsExtended())
{
error = HandlePing(aIncommingMessage, aOutgoingMessage, offset, length);
if (error == kErrorNone)
{
response = true;
}
}
else
{
LogWarn("Ping TLV is not extendeTLV format.");
error = kErrorParse;
}
break;
default:
error = kErrorInvalidCommand;
}
Expand Down Expand Up @@ -504,6 +519,24 @@ Error TcatAgent::HandleDecomission(void)
return error;
}

Error TcatAgent::HandlePing(const Message &aIncommingMessage,
Message &aOutgoingMessage,
uint16_t aOffset,
uint16_t aLength)
{
Error error = kErrorNone;
ot::ExtendedTlv extTlv;
VerifyOrExit(aLength <= kResponseWithStatusMaxLenght, error = kErrorParse);

extTlv.SetType(kTlvResponseWithPayload);
extTlv.SetLength(aLength);
SuccessOrExit(error = aOutgoingMessage.Append(extTlv));
SuccessOrExit(error = aOutgoingMessage.AppendBytesFromMessage(aIncommingMessage, aOffset, aLength));

exit:
return error;
}

Error TcatAgent::HandleStartThreadInterface(void)
{
Error error;
Expand Down
4 changes: 3 additions & 1 deletion src/core/meshcop/tcat_agent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ class TcatAgent : public InstanceLocator, private NonCopyable
Error HandleSingleTlv(const Message &aIncommingMessage, Message &aOutgoingMessage);
Error HandleSetActiveOperationalDataset(const Message &aIncommingMessage, uint16_t aOffset, uint16_t aLength);
Error HandleDecomission(void);
Error HandlePing(const Message &aIncommingMessage, Message &aOutgoingMessage, uint16_t aOffset, uint16_t aLength);
Error HandleStartThreadInterface(void);

bool CheckCommandClassAuthorizationFlags(CommandClassFlags aCommissionerCommandClassFlags,
Expand All @@ -361,7 +362,8 @@ class TcatAgent : public InstanceLocator, private NonCopyable
bool CanProcessTlv(uint8_t aTlvType) const;
CommandClass GetCommandClass(uint8_t aTlvType) const;

static constexpr uint16_t kJoinerUdpPort = OPENTHREAD_CONFIG_JOINER_UDP_PORT;
static constexpr uint16_t kJoinerUdpPort = OPENTHREAD_CONFIG_JOINER_UDP_PORT;
static constexpr uint16_t kResponseWithStatusMaxLenght = 512;

JoinerPskd mJoinerPskd;
const VendorInfo *mVendorInfo;
Expand Down
12 changes: 12 additions & 0 deletions tests/scripts/expect/cli-tcat.exp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ send "thread start\n"
expect_line "\tTYPE:\tRESPONSE_W_STATUS"
expect_line "\tVALUE:\t0x00"

send "ping\n"
expect_line "\tTYPE:\tRESPONSE_W_PAYLOAD"
expect_line "\tLEN:\t10"

send "ping 255\n"
expect_line "\tTYPE:\tRESPONSE_W_PAYLOAD"
expect_line "\tLEN:\t255"

send "ping 512\n"
expect_line "\tTYPE:\tRESPONSE_W_PAYLOAD"
expect_line "\tLEN:\t512"

send "exit\n"
expect eof

Expand Down
34 changes: 34 additions & 0 deletions tools/tcat_ble_client/cli/base_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
from dataset.dataset import ThreadDataset
from utils import select_device_by_user_input
from os import path
from time import time
from secrets import token_bytes


class HelpCommand(Command):
Expand Down Expand Up @@ -103,6 +105,38 @@ async def execute_default(self, args, context):
return CommandResultTLV(tlv_response)


class PingCommand(Command):

def get_help_string(self) -> str:
return 'Echo request to device.'

async def execute_default(self, args, context):
bless: BleStreamSecure = context['ble_sstream']
size = 10
max_payload = 512
if len(args) > 0:
size = int(args[0])
if size > max_payload:
print(f'Payload to large. Maximum supported value is {max_payload}')
return
to_send = token_bytes(size)
data = TLV(TcatTLVType.PING.value, to_send).to_bytes_extended()
elapsed_time = time()
response = await bless.send_with_resp(data)
print(response)
elapsed_time = time() - elapsed_time
if not response:
return

tlv_response = TLV.from_bytes(response)
if tlv_response.value != to_send:
print("Received malformed response.")

print(f"Roundtrip time {elapsed_time} s.")

return CommandResultTLV(tlv_response)


class ThreadStartCommand(Command):

def get_help_string(self) -> str:
Expand Down
5 changes: 3 additions & 2 deletions tools/tcat_ble_client/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
import readline
import shlex
from ble.ble_stream_secure import BleStreamSecure
from cli.base_commands import (HelpCommand, HelloCommand, CommissionCommand, DecommissionCommand, ThreadStateCommand,
ScanCommand)
from cli.base_commands import (HelpCommand, HelloCommand, CommissionCommand, DecommissionCommand, PingCommand,
ThreadStateCommand, ScanCommand)
from cli.dataset_commands import (DatasetCommand)
from dataset.dataset import ThreadDataset
from typing import Optional
Expand All @@ -44,6 +44,7 @@ def __init__(self, dataset: ThreadDataset, ble_sstream: Optional[BleStreamSecure
'hello': HelloCommand(),
'commission': CommissionCommand(),
'decommission': DecommissionCommand(),
'ping': PingCommand(),
'dataset': DatasetCommand(),
'thread': ThreadStateCommand(),
'scan': ScanCommand(),
Expand Down
1 change: 1 addition & 0 deletions tools/tcat_ble_client/tlv/tcat_tlv.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class TcatTLVType(Enum):
RESPONSE_W_STATUS = 0x01
RESPONSE_W_PAYLOAD = 0x02
DISCONNECT = 0x09
PING = 0x0A
ACTIVE_DATASET = 0x20
DECOMMISSION = 0x60
APPLICATION = 0x82
Expand Down
16 changes: 11 additions & 5 deletions tools/tcat_ble_client/tlv/tlv.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,20 @@ def from_bytes(data: bytes) -> TLV:
def set_from_bytes(self, data: bytes):
self.type = data[0]
header_len = 2
size_offset = 1
if data[1] == 0xFF:
header_len = 4
length = int.from_bytes(data[1:header_len], byteorder='big')
size_offset = 2
length = int.from_bytes(data[size_offset:header_len], byteorder='big')
self.value = data[header_len:header_len + length]

def to_bytes(self) -> bytes:
has_long_header = len(self.value) >= 255
header_len = 4 if has_long_header else 2
len_bytes = len(self.value).to_bytes(header_len - 1, byteorder='big')
header = bytes([self.type]) + len_bytes
len_bytes = len(self.value).to_bytes(1, byteorder='big')
header = self.type.to_bytes(1, byteorder='big') + len_bytes
return header + self.value

def to_bytes_extended(self) -> bytes:
len_bytes = len(self.value).to_bytes(2, byteorder='big')
type = self.type << 8 | 255
header = type.to_bytes(2, byteorder='big') + len_bytes
return header + self.value

0 comments on commit cf13269

Please sign in to comment.