[tcat][ble] fixes to connection state mgmt and Disconnect cmd (#10619)

- Fixes to connection state management and handling of Disconnect
  command TLV
  - specifically, this now ensures that TCAT remains on (started)
    after a commissioner disconnects. Earlier, there was the problem
    that the 2nd commissioner couldn't connect anymore.
  - specifically, in ble_secure.cpp the check for if
    (mTcatAgent.IsEnabled()) is removed, since the err =
    mTcatAgent.Connected(mTls) will already check this and raise an
    error if not enabled. If not enabled, the Device is in a wrong
    state to handle TCAT Commissioner commands so now it closes the
    connection right away. That's better than to leave the
    Commissioner in limbo on the TLS connection. The Commissioner can
    now retry again and all will be well again.
- timeout of at most 10 seconds on UDP write operation in simulation
  mode (if longer, the TCAT device isn't reachable and the
  Commissioner now shows the error to the user.) Earlier, it got stuck
  forever.
- Corrects some copy/paste errors in API definitions in comments; adds
  comments where needed to explain.
  - adds whitespace at some places to align format with rest of code
- improved some of the --debug output for the UDP simulation mode of
  the TCAT Commissioner.
This commit is contained in:
Esko Dijk
2024-09-05 19:59:39 -07:00
committed by GitHub
parent e63e9ce86b
commit 4459c54069
8 changed files with 51 additions and 39 deletions
+4 -3
View File
@@ -102,9 +102,6 @@ async def main():
while True:
user_input = await loop.run_in_executor(None, lambda: input('> '))
if user_input.lower() == 'exit':
print('Disconnecting...')
if ble_sstream is not None:
await ble_sstream.close()
break
try:
result: CommandResult = await cli.evaluate_input(user_input)
@@ -113,6 +110,10 @@ async def main():
except Exception as e:
logger.error(e)
print('Disconnecting...')
if ble_sstream is not None:
await ble_sstream.close()
async def get_device_by_args(args):
device = None
@@ -140,6 +140,7 @@ class BleStreamSecure:
async def close(self):
if self.ssl_object.session is not None:
logger.debug('sending Disconnect command TLV')
data = TLV(TcatTLVType.DISCONNECT.value, bytes()).to_bytes()
await self.send(data)
+12 -6
View File
@@ -28,25 +28,31 @@
import logging
import socket
import select
logger = logging.getLogger(__name__)
class UdpStream:
BASE_PORT = 10000
MAX_SERVER_TIMEOUT_SEC = 10
def __init__(self, address, node_id):
self.__receive_buffer = b''
self.__last_recv_time = None
self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.socket.setblocking(False)
self.address = (address, self.BASE_PORT + node_id)
async def send(self, data):
logger.debug(f'sending {data}')
self.socket.sendto(data, self.address)
return len(data)
logger.debug(f'sending {len(data)} bytes: {data}')
return self.socket.sendto(data, self.address)
async def recv(self, bufsize):
message = self.socket.recv(bufsize)
logger.debug(f'retrieved {message}')
return message
ready = select.select([self.socket], [], [], self.MAX_SERVER_TIMEOUT_SEC)
if ready[0]:
data = self.socket.recv(bufsize)
logger.debug(f'received {len(data)} bytes: {data}')
return data
else:
raise Exception('simulation UdpStream recv timeout - likely, TCAT is stopped on TCAT Device')
+2 -2
View File
@@ -181,7 +181,7 @@ class PingCommand(Command):
data = TLV(TcatTLVType.PING.value, to_send).to_bytes()
elapsed_time = time()
response = await bless.send_with_resp(data)
elapsed_time = time() - elapsed_time
elapsed_time = 1e3 * (time() - elapsed_time)
if not response:
return
@@ -189,7 +189,7 @@ class PingCommand(Command):
if tlv_response.value != to_send:
print("Received malformed response.")
print(f"Roundtrip time {elapsed_time} s.")
print(f"Roundtrip time: {elapsed_time} ms")
return CommandResultTLV(tlv_response)