device: commit LE connection **before** reading it's PHY

This commit is contained in:
Abel Lucas
2022-12-15 20:45:53 +00:00
parent ce049865a4
commit ea0a7e2347

View File

@@ -2368,30 +2368,13 @@ class Device(CompositeEventEmitter):
self.advertising_own_address_type = None self.advertising_own_address_type = None
self.advertising = False self.advertising = False
# Create and notify of the new connection asynchronously
async def new_connection():
# Figure out which PHY we're connected with
if self.host.supports_command(HCI_LE_READ_PHY_COMMAND):
result = await asyncio.shield(
self.send_command(
HCI_LE_Read_PHY_Command(
connection_handle=connection_handle
),
check_result=True,
)
)
phy = ConnectionPHY(
result.return_parameters.tx_phy, result.return_parameters.rx_phy
)
else:
phy = ConnectionPHY(HCI_LE_1M_PHY, HCI_LE_1M_PHY)
self_address = self.random_address
if own_address_type in ( if own_address_type in (
OwnAddressType.PUBLIC, OwnAddressType.PUBLIC,
OwnAddressType.RESOLVABLE_OR_PUBLIC, OwnAddressType.RESOLVABLE_OR_PUBLIC,
): ):
self_address = self.public_address self_address = self.public_address
else:
self_address = self.random_address
# Create a new connection # Create a new connection
connection = Connection( connection = Connection(
@@ -2403,14 +2386,32 @@ class Device(CompositeEventEmitter):
peer_resolvable_address, peer_resolvable_address,
role, role,
connection_parameters, connection_parameters,
phy, ConnectionPHY(HCI_LE_1M_PHY, HCI_LE_1M_PHY),
) )
self.connections[connection_handle] = connection self.connections[connection_handle] = connection
# If supported, read which PHY we're connected with before
# notifying listeners of the new connection.
if self.host.supports_command(HCI_LE_READ_PHY_COMMAND):
async def read_phy():
result = await self.send_command(
HCI_LE_Read_PHY_Command(
connection_handle=connection_handle
),
check_result=True,
)
connection.phy = ConnectionPHY(
result.return_parameters.tx_phy, result.return_parameters.rx_phy
)
# Emit an event to notify listeners of the new connection # Emit an event to notify listeners of the new connection
self.emit('connection', connection) self.emit('connection', connection)
self.abort_on('flush', new_connection()) # Do so asynchronously to not block the current event handler
connection.abort_on('disconnection', read_phy())
else:
# Emit an event to notify listeners of the new connection
self.emit('connection', connection)
@host_event_handler @host_event_handler
def on_connection_failure(self, transport, peer_address, error_code): def on_connection_failure(self, transport, peer_address, error_code):