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

Support non-directed address generation offload #381

Merged
merged 1 commit into from
Jan 28, 2024
Merged
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
20 changes: 17 additions & 3 deletions bumble/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import json
import asyncio
import logging
import secrets
from contextlib import asynccontextmanager, AsyncExitStack, closing
from dataclasses import dataclass
from collections.abc import Iterable
Expand Down Expand Up @@ -995,12 +996,15 @@ def load_from_dict(self, config: Dict[str, Any]) -> None:
irk = config.get('irk')
if irk:
self.irk = bytes.fromhex(irk)
else:
elif self.address != Address(DEVICE_DEFAULT_ADDRESS):
# Construct an IRK from the address bytes
# NOTE: this is not secure, but will always give the same IRK for the same
# address
address_bytes = bytes(self.address)
self.irk = (address_bytes * 3)[:16]
else:
# Fallback - when both IRK and address are not set, randomly generate an IRK.
self.irk = secrets.token_bytes(16)

# Load advertising data
advertising_data = config.get('advertising_data')
Expand Down Expand Up @@ -1582,6 +1586,16 @@ async def refresh_resolving_list(self) -> None:
if self.address_resolution_offload:
await self.send_command(HCI_LE_Clear_Resolving_List_Command())

# Add an empty entry for non-directed address generation.
await self.send_command(
HCI_LE_Add_Device_To_Resolving_List_Command(
peer_identity_address_type=Address.ANY.address_type,
peer_identity_address=Address.ANY,
peer_irk=bytes(16),
local_irk=self.irk,
)
)

for irk, address in resolving_keys:
await self.send_command(
HCI_LE_Add_Device_To_Resolving_List_Command(
Expand Down Expand Up @@ -1682,8 +1696,8 @@ async def start_legacy_advertising(
peer_address = target
peer_address_type = target.address_type
else:
peer_address = Address('00:00:00:00:00:00')
peer_address_type = Address.PUBLIC_DEVICE_ADDRESS
peer_address = Address.ANY
peer_address_type = Address.ANY.address_type

# Set the advertising parameters
await self.send_command(
Expand Down
6 changes: 2 additions & 4 deletions bumble/smp.py
Original file line number Diff line number Diff line change
Expand Up @@ -1993,10 +1993,8 @@ async def on_pairing(
) -> None:
# Store the keys in the key store
if self.device.keystore and identity_address is not None:
self.device.abort_on(
'flush', self.device.update_keys(str(identity_address), keys)
)

# Make sure on_pairing emits after key update.
await self.device.update_keys(str(identity_address), keys)
# Notify the device
self.device.on_pairing(session.connection, identity_address, keys, session.sc)

Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ development =
types-protobuf >= 4.21.0
avatar =
pandora-avatar == 0.0.5
rootcanal == 1.4.0 ; python_version>='3.10'
rootcanal == 1.6.0 ; python_version>='3.10'
documentation =
mkdocs >= 1.4.0
mkdocs-material >= 8.5.6
Expand Down
Loading