| 1 |
"""Bluetooth utils.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
from functools import lru_cache |
| 6 |
from struct import Struct |
| 7 |
|
| 8 |
try: |
| 9 |
from ._utils_impl import ( |
| 10 |
_int_to_bluetooth_address, |
| 11 |
_mac_to_int, |
| 12 |
) |
| 13 |
|
| 14 |
|
| 15 |
except ImportError: |
| 16 |
|
| 17 |
def _int_to_bluetooth_address(address: int) -> str: |
| 18 |
"""Convert an integer to a bluetooth address.""" |
| 19 |
mac_hex = f"{address:012X}" |
| 20 |
return f"{mac_hex[0:2]}:{mac_hex[2:4]}:{mac_hex[4:6]}:{mac_hex[6:8]}:{mac_hex[8:10]}:{mac_hex[10:12]}" |
| 21 |
|
| 22 |
_HEX_DIGITS = frozenset("0123456789abcdefABCDEF") |
| 23 |
|
| 24 |
def _mac_to_int(address: str) -> int: |
| 25 |
"""Convert a mac address to an integer. |
| 26 |
|
| 27 |
Mirrors the native parser: accepts the 17-char separated form |
| 28 |
("AA:BB:CC:DD:EE:FF" or the Windows "AA-BB-CC-DD-EE-FF" form) and |
| 29 |
the 12-char unseparated form ("AABBCCDDEEFF"). Any other length, a |
| 30 |
separator in the wrong position, an unexpected separator character, |
| 31 |
or a non-hex digit raises ValueError. |
| 32 |
""" |
| 33 |
length = len(address) |
| 34 |
if length == 17: |
| 35 |
if any(address[i] not in ":-" for i in (2, 5, 8, 11, 14)): |
| 36 |
raise ValueError(f"Invalid MAC address: {address!r}") |
| 37 |
hex_part = ( |
| 38 |
address[0:2] |
| 39 |
+ address[3:5] |
| 40 |
+ address[6:8] |
| 41 |
+ address[9:11] |
| 42 |
+ address[12:14] |
| 43 |
+ address[15:17] |
| 44 |
) |
| 45 |
elif length == 12: |
| 46 |
hex_part = address |
| 47 |
else: |
| 48 |
raise ValueError(f"Invalid MAC address: {address!r}") |
| 49 |
if not _HEX_DIGITS.issuperset(hex_part): |
| 50 |
raise ValueError(f"Invalid MAC address: {address!r}") |
| 51 |
return int(hex_part, 16) |
| 52 |
|
| 53 |
|
| 54 |
int_to_bluetooth_address = lru_cache(maxsize=256)(_int_to_bluetooth_address) |
| 55 |
mac_to_int = lru_cache(maxsize=256)(_mac_to_int) |
| 56 |
|
| 57 |
|
| 58 |
def short_address(address: str) -> str: |
| 59 |
"""Convert a Bluetooth address to a short address.""" |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
return address.replace(":", "").replace("-", "").upper()[-4:] |
| 65 |
|
| 66 |
|
| 67 |
@lru_cache(maxsize=512) |
| 68 |
def human_readable_name(name: str | None, local_name: str, address: str) -> str: |
| 69 |
"""Return a human readable name for the given name, local_name, and address.""" |
| 70 |
return f"{name or local_name} ({short_address(address)})" |
| 71 |
|
| 72 |
|
| 73 |
def newest_manufacturer_data(manufacturer_data: dict[int, bytes]) -> bytes | None: |
| 74 |
"""Return the raw data from manufacturer data.""" |
| 75 |
if not manufacturer_data: |
| 76 |
return None |
| 77 |
return manufacturer_data[next(reversed(manufacturer_data))] |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
_pack_manufacturer_prefix = Struct("<2xH").pack |
| 84 |
|
| 85 |
|
| 86 |
def manufacturer_data_to_raw(manufacturer_id: int, manufacturer_data: bytes) -> bytes: |
| 87 |
"""Return the raw data from manufacturer data.""" |
| 88 |
return _pack_manufacturer_prefix(manufacturer_id) + manufacturer_data |