[tcat] Add timeout while connecting over BLE. (#10597)

Adding timeout while handling ble connection establishement in TCAT.
This commit is contained in:
Przemyslaw Bida
2024-09-04 07:49:33 -07:00
committed by Jonathan Hui
parent 12cf1207d2
commit db6393251a
3 changed files with 18 additions and 6 deletions
+4
View File
@@ -91,3 +91,7 @@ class BleStream:
self.__receive_buffer = self.__receive_buffer[bufsize:]
logger.debug(f'retrieved {message}')
return message
async def disconnect(self):
if self.client.is_connected:
await self.client.disconnect()
@@ -34,6 +34,7 @@ import logging
from tlv.tlv import TLV
from tlv.tcat_tlv import TcatTLVType
from time import time
import utils
logger = logging.getLogger(__name__)
@@ -60,7 +61,7 @@ class BleStreamSecure:
if cafile:
self.ssl_context.load_verify_locations(cafile=cafile)
async def do_handshake(self):
async def do_handshake(self, timeout=30.0):
is_debug = logger.getEffectiveLevel() <= logging.DEBUG
self.ssl_object = self.ssl_context.wrap_bio(
incoming=self.incoming,
@@ -68,7 +69,8 @@ class BleStreamSecure:
server_side=False,
server_hostname=None,
)
while True:
start = time()
while (time() - start) < timeout:
try:
if not is_debug:
print('.', end='')
@@ -97,6 +99,10 @@ class BleStreamSecure:
if output:
self.incoming.write(output)
await asyncio.sleep(0.02)
else:
print('TLS Connection timed out.')
return False
return True
async def send(self, bytes):
self.ssl_object.write(bytes)
+6 -4
View File
@@ -257,8 +257,10 @@ class ScanCommand(Command):
keyfile=path.join(cert_path, 'commissioner_key.pem'),
cafile=path.join(cert_path, 'ca_cert.pem'),
)
print('Setting up secure channel...')
await ble_sstream.do_handshake()
print('Done')
context['ble_sstream'] = ble_sstream
if await ble_sstream.do_handshake():
print('Done')
context['ble_sstream'] = ble_sstream
else:
print('Secure channel not established.')
await ble_stream.disconnect()