Skip to content

Commit

Permalink
feat: small speed up to gap parser
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco committed Dec 21, 2023
1 parent 023979a commit d5f459d
Showing 1 changed file with 15 additions and 18 deletions.
33 changes: 15 additions & 18 deletions src/bluetooth_data_tools/gap.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,31 +210,28 @@ def _uncached_parse_advertisement_data(
for gap_data in data:
offset = 0
total_length = len(gap_data)
while offset < total_length:
try:
length = gap_data[offset]
if not length:
if offset + 2 < total_length:
# Maybe zero padding
offset += 1
continue
break
gap_type_num = gap_data[offset + 1]
if not gap_type_num:
break
start = offset + 2
end = start + length - 1
gap_value = gap_data[start:end]
except IndexError as ex:
while offset + 1 < total_length:
length = gap_data[offset]
if not length:
if offset + 2 < total_length:
# Maybe zero padding
offset += 1
continue
break
gap_type_num = gap_data[offset + 1]
if not gap_type_num:
break
start = offset + 2
end = start + length - 1
if total_length < end:
_LOGGER.debug(
"Invalid BLE GAP AD structure at offset %s: %s (%s)",
offset,
gap_data,
ex,
)
offset += 1 + length
continue

gap_value = gap_data[start:end]
offset += 1 + length
if end - start == 0:
continue
Expand Down

0 comments on commit d5f459d

Please sign in to comment.