Skip to content

Commit

Permalink
HAP: wait for pairing event
Browse files Browse the repository at this point in the history
  • Loading branch information
wescande committed Sep 11, 2024
1 parent 00e660d commit d49c0a5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 18 deletions.
45 changes: 27 additions & 18 deletions bumble/profiles/hap.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from bumble.hci import Address
from dataclasses import dataclass, field
import logging
from typing import Dict, List, Optional, Set, Union
from typing import Any, Dict, List, Optional, Set, Union


# -----------------------------------------------------------------------------
Expand Down Expand Up @@ -272,24 +272,12 @@ def on_connection(connection: Connection) -> None:
def on_disconnection(_reason) -> None:
self.currently_connected_clients.remove(connection)

# TODO Should we filter on device bonded && device is HAP ?
self.currently_connected_clients.add(connection)
if (
connection.peer_address
not in self.preset_changed_operations_history_per_device
):
self.preset_changed_operations_history_per_device[
connection.peer_address
] = []
return

async def on_connection_async() -> None:
# Send all the PresetChangedOperation that occur when not connected
await self._preset_changed_operation(connection)
# Update the active preset index if needed
await self.notify_active_preset_for_connection(connection)
@connection.on('pairing') # type: ignore
def on_pairing(*_: Any) -> None:
self.on_incoming_paired_connection(connection)

connection.abort_on('disconnection', on_connection_async())
if connection.peer_resolvable_address:
self.on_incoming_paired_connection(connection)

self.hearing_aid_features_characteristic = gatt.Characteristic(
uuid=gatt.GATT_HEARING_AID_FEATURES_CHARACTERISTIC,
Expand Down Expand Up @@ -326,6 +314,27 @@ async def on_connection_async() -> None:
]
)

def on_incoming_paired_connection(self, connection: Connection):
'''Setup initial operations to handle a remote bonded HAP device'''
# TODO Should we filter on HAP device only ?
self.currently_connected_clients.add(connection)
if (
connection.peer_address
not in self.preset_changed_operations_history_per_device
):
self.preset_changed_operations_history_per_device[
connection.peer_address
] = []
return

async def on_connection_async() -> None:
# Send all the PresetChangedOperation that occur when not connected
await self._preset_changed_operation(connection)
# Update the active preset index if needed
await self.notify_active_preset_for_connection(connection)

connection.abort_on('disconnection', on_connection_async())

def _on_read_active_preset_index(
self, __connection__: Optional[Connection]
) -> bytes:
Expand Down
5 changes: 5 additions & 0 deletions tests/hap_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from bumble import att, device
from bumble.profiles import hap
from .test_utils import TwoDevices
from bumble.keys import PairingKeys

# -----------------------------------------------------------------------------
# Logging
Expand Down Expand Up @@ -86,6 +87,10 @@ async def hap_client():
devices.connections[0].encryption = 1 # type: ignore
devices.connections[1].encryption = 1 # type: ignore

devices[0].on_pairing(
devices.connections[0], devices.connections[0].peer_address, PairingKeys(), True
)

peer = device.Peer(devices.connections[1]) # type: ignore
hap_client = await peer.discover_service_and_create_proxy(
hap.HearingAccessServiceProxy
Expand Down

0 comments on commit d49c0a5

Please sign in to comment.