Active BLE connection v3 (#317)

This commit is contained in:
J. Nick Koston 2022-11-28 16:06:13 -10:00 committed by GitHub
parent c3d610e45a
commit e3094db6ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 8 deletions

View File

@ -1174,6 +1174,8 @@ enum BluetoothDeviceRequestType {
BLUETOOTH_DEVICE_REQUEST_TYPE_DISCONNECT = 1; BLUETOOTH_DEVICE_REQUEST_TYPE_DISCONNECT = 1;
BLUETOOTH_DEVICE_REQUEST_TYPE_PAIR = 2; BLUETOOTH_DEVICE_REQUEST_TYPE_PAIR = 2;
BLUETOOTH_DEVICE_REQUEST_TYPE_UNPAIR = 3; BLUETOOTH_DEVICE_REQUEST_TYPE_UNPAIR = 3;
BLUETOOTH_DEVICE_REQUEST_TYPE_CONNECT_V3_WITH_CACHE = 4;
BLUETOOTH_DEVICE_REQUEST_TYPE_CONNECT_V3_WITHOUT_CACHE = 5;
} }
message BluetoothDeviceRequest { message BluetoothDeviceRequest {

File diff suppressed because one or more lines are too long

View File

@ -492,6 +492,8 @@ class APIClient:
on_bluetooth_connection_state: Callable[[bool, int, int], None], on_bluetooth_connection_state: Callable[[bool, int, int], None],
timeout: float = DEFAULT_BLE_TIMEOUT, timeout: float = DEFAULT_BLE_TIMEOUT,
disconnect_timeout: float = DEFAULT_BLE_DISCONNECT_TIMEOUT, disconnect_timeout: float = DEFAULT_BLE_DISCONNECT_TIMEOUT,
version: int = 1,
has_cache: bool = False,
) -> Callable[[], None]: ) -> Callable[[], None]:
self._check_authenticated() self._check_authenticated()
@ -505,11 +507,22 @@ class APIClient:
event.set() event.set()
assert self._connection is not None assert self._connection is not None
if has_cache:
# Version 3 with cache: requestor has services and mtu cached
_LOGGER.debug("%s: Using connection version 3 with cache", address)
request_type = BluetoothDeviceRequestType.CONNECT_V3_WITH_CACHE
elif version >= 3:
# Version 3 without cache: esp will wipe the service list after sending to save memory
_LOGGER.debug("%s: Using connection version 3 without cache", address)
request_type = BluetoothDeviceRequestType.CONNECT_V3_WITHOUT_CACHE
else:
# Older than v3 without cache: esp will hold the service list in memory for the duration
# of the connection. This can crash the esp if the service list is too large.
_LOGGER.debug("%s: Using connection version 1", address)
request_type = BluetoothDeviceRequestType.CONNECT
await self._connection.send_message_callback_response( await self._connection.send_message_callback_response(
BluetoothDeviceRequest( BluetoothDeviceRequest(address=address, request_type=request_type),
address=address,
request_type=BluetoothDeviceRequestType.CONNECT,
),
on_msg, on_msg,
) )
@ -682,12 +695,18 @@ class APIClient:
handle: int, handle: int,
data: bytes, data: bytes,
timeout: float = DEFAULT_BLE_TIMEOUT, timeout: float = DEFAULT_BLE_TIMEOUT,
wait_for_response: bool = True,
) -> None: ) -> None:
req = BluetoothGATTWriteDescriptorRequest() req = BluetoothGATTWriteDescriptorRequest()
req.address = address req.address = address
req.handle = handle req.handle = handle
req.data = data req.data = data
if not wait_for_response:
assert self._connection is not None
await self._connection.send_message(req)
return
await self._send_bluetooth_message_await_response( await self._send_bluetooth_message_await_response(
address, address,
handle, handle,

View File

@ -913,6 +913,8 @@ class BluetoothDeviceRequestType(APIIntEnum):
DISCONNECT = 1 DISCONNECT = 1
PAIR = 2 PAIR = 2
UNPAIR = 3 UNPAIR = 3
CONNECT_V3_WITH_CACHE = 4
CONNECT_V3_WITHOUT_CACHE = 5
class LogLevel(APIIntEnum): class LogLevel(APIIntEnum):