Skip to content

Commit

Permalink
address PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
barbibulle committed Jan 18, 2024
1 parent 1597035 commit 75e1152
Show file tree
Hide file tree
Showing 6 changed files with 228 additions and 325 deletions.
40 changes: 27 additions & 13 deletions bumble/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -537,11 +537,12 @@ def __int__(self) -> int:

return int(properties)

@staticmethod
@classmethod
def from_advertising_type(
cls: Type[AdvertisingEventProperties],
advertising_type: AdvertisingType,
) -> AdvertisingEventProperties:
return AdvertisingEventProperties(
return cls(
is_connectable=advertising_type.is_connectable,
is_scannable=advertising_type.is_scannable,
is_directed=advertising_type.is_directed,
Expand Down Expand Up @@ -720,6 +721,16 @@ async def set_random_address(self, random_address: Address) -> None:
async def start(
self, duration: float = 0.0, max_advertising_events: int = 0
) -> None:
"""
Start advertising.
Args:
duration: How long to advertise for, in seconds. Use 0 (the default) for
an unlimited duration, unless this advertising set is a High Duty Cycle
Directed Advertisement type.
max_advertising_events: Maximum number of events to advertise for. Use 0
(the default) for an unlimited number of advertisements.
"""
await self.device.send_command(
HCI_LE_Set_Extended_Advertising_Enable_Command(
enable=1,
Expand Down Expand Up @@ -2150,11 +2161,11 @@ async def create_advertising_set(

if periodic_advertising_parameters:
# TODO: call LE Set Periodic Advertising Parameters command
pass
raise NotImplementedError('periodic advertising not yet supported')

if periodic_advertising_data:
# TODO: call LE Set Periodic Advertising Data command
pass
raise NotImplementedError('periodic advertising not yet supported')

except HCI_Error as error:
# Remove the advertising set so that it doesn't stay dangling in the
Expand Down Expand Up @@ -2195,11 +2206,10 @@ def is_advertising(self):
if self.legacy_advertising_set and self.legacy_advertising_set.enabled:
return True

for advertising_set in self.extended_advertising_sets.values():
if advertising_set.enabled:
return True

return False
return any(
advertising_set.enabled
for advertising_set in self.extended_advertising_sets.values()
)

async def start_scanning(
self,
Expand Down Expand Up @@ -3532,7 +3542,10 @@ def on_advertising_set_termination(
number_of_completed_extended_advertising_events,
):
if not (
advertising_set := self.extended_advertising_sets.get(advertising_handle)
advertising_set := (
self.extended_advertising_sets.get(advertising_handle)
or self.legacy_advertising_set
)
):
logger.warning(f'advertising set {advertising_handle} not found')
return
Expand Down Expand Up @@ -3565,9 +3578,9 @@ def on_advertising_set_termination(
lambda _: self.abort_on('flush', advertising_set.start()),
)

self.emit_le_connection(connection)
self._emit_le_connection(connection)

def emit_le_connection(self, connection: Connection) -> None:
def _emit_le_connection(self, connection: Connection) -> None:
# 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):
Expand Down Expand Up @@ -3642,6 +3655,7 @@ def on_connection(
# We were connected via a legacy advertisement.
if self.legacy_advertiser:
own_address_type = self.legacy_advertiser.own_address_type
self.legacy_advertiser = None
else:
# This should not happen, but just in case, pick a default.
logger.warning("connection without an advertiser")
Expand Down Expand Up @@ -3684,7 +3698,7 @@ def on_connection(

if role == HCI_CENTRAL_ROLE or not self.supports_le_extended_advertising:
# We can emit now, we have all the info we need
self.emit_le_connection(connection)
self._emit_le_connection(connection)

@host_event_handler
def on_connection_failure(self, transport, peer_address, error_code):
Expand Down
Loading

0 comments on commit 75e1152

Please sign in to comment.