Skip to content

Commit

Permalink
feat: speed up parsing advertisement data (#11)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: The decode_advertisement_data function is no longer exposed

It is likely nobody was using it since it is internals for parse_advertisement_data, but it was exposed. If this is a problem for you, please open an issue.
  • Loading branch information
bdraco committed Jun 7, 2023
1 parent ae6295f commit 47e2519
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 12 deletions.
8 changes: 1 addition & 7 deletions src/bluetooth_data_tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@

from struct import Struct

from .gap import (
BLEGAPAdvertisement,
BLEGAPType,
decode_advertisement_data,
parse_advertisement_data,
)
from .gap import BLEGAPAdvertisement, BLEGAPType, parse_advertisement_data
from .utils import int_to_bluetooth_address

__version__ = "0.4.0"
Expand All @@ -24,7 +19,6 @@
"short_address",
"BLEGAPType",
"BLEGAPAdvertisement",
"decode_advertisement_data",
"parse_advertisement_data",
]

Expand Down
9 changes: 4 additions & 5 deletions src/bluetooth_data_tools/gap.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ class BLEGAPType(IntEnum):
_bytes = bytes


def decode_advertisement_data(
def _decode_advertisement_data(
encoded_struct: _bytes,
) -> Iterable[Tuple[BLEGAPType, bytes]]:
) -> Iterable[Tuple[int, bytes]]:
"""Decode a BLE GAP AD structure."""
offset = 0
total_length = len(encoded_struct)
Expand Down Expand Up @@ -108,7 +108,7 @@ def decode_advertisement_data(
)
return

yield _BLEGAPType_MAP.get(type_, BLEGAPType.TYPE_UNKNOWN), value
yield type_, value
offset += 1 + length


Expand Down Expand Up @@ -140,8 +140,7 @@ def parse_advertisement_data(
tx_power: int | None = None

for gap_data in data:
for gap_type, gap_value in decode_advertisement_data(gap_data):
gap_type_num = gap_type.value
for gap_type_num, gap_value in _decode_advertisement_data(gap_data):
if gap_type_num == TYPE_SHORT_LOCAL_NAME and not local_name:
local_name = gap_value.decode("utf-8", errors="replace")
elif gap_type_num == TYPE_COMPLETE_LOCAL_NAME:
Expand Down

0 comments on commit 47e2519

Please sign in to comment.