Skip to content

Commit

Permalink
[tcat] Implementation of tcat disconnect command.
Browse files Browse the repository at this point in the history
Implementation of tcat disconnect command in python client.
  • Loading branch information
canisLupus1313 committed Nov 13, 2024
1 parent 79572ec commit 0e97bcb
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/core/radio/ble_secure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,7 @@ void BleSecure::HandleTlsReceive(uint8_t *aBuf, uint16_t aLength)

if (error == kErrorAbort)
{
LogInfo("Disconnecting TCAT client.");
// kErrorAbort indicates that a Disconnect command TLV has been received.
Disconnect();
// BleSecure is not stopped here, it must remain active in advertising state and
Expand Down
2 changes: 1 addition & 1 deletion tools/tcat_ble_client/bbtc.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ async def main():
ds = ThreadDataset()
cli = CLI(ds, args, ble_sstream)
loop = asyncio.get_running_loop()
print('Enter \'help\' to see available commands' ' or \'exit\' to exit the application.')
print('Enter \'help\' to see available commands or \'exit\' to exit the application.')
while True:
user_input = await loop.run_in_executor(None, lambda: input('> '))
if user_input.lower() == 'exit':
Expand Down
23 changes: 21 additions & 2 deletions tools/tcat_ble_client/cli/base_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ def prepare_data(self, args, context):
pass

async def execute_default(self, args, context):
if 'ble_sstream' not in context or context['ble_sstream'] is None:
print("TCAT Device not connected.")
return CommandResultNone()
bless: BleStreamSecure = context['ble_sstream']

print(self.get_log_string())
Expand All @@ -85,6 +88,7 @@ async def execute_default(self, args, context):
return CommandResultTLV(tlv_response)
except DataNotPrepared as err:
print('Command failed', err)
return CommandResultNone()


class HelloCommand(BleCommand):
Expand Down Expand Up @@ -298,7 +302,7 @@ async def execute_default(self, args, context):
response = await bless.send_with_resp(data)
elapsed_time = 1e3 * (time() - elapsed_time)
if not response:
return
return CommandResultNone()

tlv_response = TLV.from_bytes(response)
if tlv_response.value != to_send:
Expand Down Expand Up @@ -352,7 +356,8 @@ def get_help_string(self) -> str:
return 'Perform scan for TCAT devices.'

async def execute_default(self, args, context):
if not (context['ble_sstream'] is None):
if 'ble_sstream' in context and context['ble_sstream'] is not None:
context['ble_sstream'].close()
del context['ble_sstream']

tcat_devices = await ble_scanner.scan_tcat_devices()
Expand All @@ -379,3 +384,17 @@ async def execute_default(self, args, context):
else:
print('Secure channel not established.')
await ble_stream.disconnect()
return CommandResultNone()


class DisconnectCommand(Command):

def get_help_string(self) -> str:
return 'Disconnect client from TCAT device'

async def execute_default(self, args, context):
if 'ble_sstream' not in context or context['ble_sstream'] is None:
print("TCAT Device not connected.")
return CommandResultNone()
await context['ble_sstream'].close()
return CommandResultNone()
8 changes: 5 additions & 3 deletions tools/tcat_ble_client/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@
import shlex
from argparse import ArgumentParser
from ble.ble_stream_secure import BleStreamSecure
from cli.base_commands import (HelpCommand, HelloCommand, CommissionCommand, DecommissionCommand, GetDeviceIdCommand,
GetPskdHash, GetExtPanIDCommand, GetNetworkNameCommand, GetProvisioningUrlCommand,
PingCommand, GetRandomNumberChallenge, ThreadStateCommand, ScanCommand, PresentHash)
from cli.base_commands import (DisconnectCommand, HelpCommand, HelloCommand, CommissionCommand, DecommissionCommand,
GetDeviceIdCommand, GetPskdHash, GetExtPanIDCommand, GetNetworkNameCommand,
GetProvisioningUrlCommand, PingCommand, GetRandomNumberChallenge, ThreadStateCommand,
ScanCommand, PresentHash)
from cli.dataset_commands import (DatasetCommand)
from dataset.dataset import ThreadDataset
from typing import Optional
Expand All @@ -48,6 +49,7 @@ def __init__(self,
'hello': HelloCommand(),
'commission': CommissionCommand(),
'decommission': DecommissionCommand(),
'disconnect': DisconnectCommand(),
'device_id': GetDeviceIdCommand(),
'ext_panid': GetExtPanIDCommand(),
'provisioning_url': GetProvisioningUrlCommand(),
Expand Down
2 changes: 1 addition & 1 deletion tools/tcat_ble_client/dataset/dataset_entries.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ def set(self, args: List[str]):
args[0] = args[0][2:]
pskc = args[0]
if (len(pskc) > self.maxlen * 2):
raise ValueError('Invalid length of Pskc. Can be max ' f'{self.length * 2} hex characters.')
raise ValueError(f'Invalid length of PSKc. Can be max {self.length * 2} hex characters.')
self.data = pskc

def set_from_tlv(self, tlv: TLV):
Expand Down

0 comments on commit 0e97bcb

Please sign in to comment.