Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HAP: wait for pairing event #551

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One issue here is that this only remembers paired connections when the pairing happens while the service is running. But what about connections from devices that were previously paired and bonded?

Copy link
Contributor Author

@wescande wescande Sep 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume that setting up HAS is done before we connect to a remote device.

If an already paired device reconnect, it should go in this if:

            if connection.peer_resolvable_address:
                self.on_incoming_paired_connection(connection)

directly

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())
wescande marked this conversation as resolved.
Show resolved Hide resolved

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
Loading