| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import asyncio |
| 4 |
import logging |
| 5 |
import platform |
| 6 |
from typing import Any |
| 7 |
|
| 8 |
from uart_devices import BluetoothDevice as UARTBluetoothDevice |
| 9 |
from uart_devices import NotAUARTDeviceError |
| 10 |
from usb_devices import BluetoothDevice as USBBluetoothDevice |
| 11 |
from usb_devices import NotAUSBDeviceError |
| 12 |
|
| 13 |
import aiooui |
| 14 |
|
| 15 |
from ..adapters import BluetoothAdapters |
| 16 |
from ..const import EMPTY_MAC_ADDRESS, UNIX_DEFAULT_BLUETOOTH_ADAPTER |
| 17 |
from ..dbus import BlueZDBusObjects |
| 18 |
from ..history import AdvertisementHistory |
| 19 |
from ..models import AdapterDetails |
| 20 |
from .linux_hci import get_adapters_from_hci |
| 21 |
|
| 22 |
_LOGGER = logging.getLogger(__name__) |
| 23 |
|
| 24 |
|
| 25 |
class LinuxAdapters(BluetoothAdapters): |
| 26 |
"""Class for getting the bluetooth adapters on a Linux system.""" |
| 27 |
|
| 28 |
def __init__(self) -> None: |
| 29 |
"""Initialize the adapter.""" |
| 30 |
self._bluez = BlueZDBusObjects() |
| 31 |
self._adapters: dict[str, AdapterDetails] | None = None |
| 32 |
self._devices: dict[str, UARTBluetoothDevice | USBBluetoothDevice] = {} |
| 33 |
self._hci_output: dict[int, dict[str, Any]] | None = None |
| 34 |
|
| 35 |
async def refresh(self) -> None: |
| 36 |
"""Refresh the adapters.""" |
| 37 |
loop = asyncio.get_running_loop() |
| 38 |
load_task = asyncio.create_task(self._bluez.load()) |
| 39 |
adapters_from_hci_future = loop.run_in_executor(None, get_adapters_from_hci) |
| 40 |
futures: list[asyncio.Future[Any]] = [load_task, adapters_from_hci_future] |
| 41 |
if not aiooui.is_loaded(): |
| 42 |
futures.append(aiooui.async_load()) |
| 43 |
await asyncio.gather(*futures) |
| 44 |
self._hci_output = await adapters_from_hci_future |
| 45 |
self._adapters = None # clear cache |
| 46 |
self._devices = {} |
| 47 |
await loop.run_in_executor(None, self._refresh_devices) |
| 48 |
|
| 49 |
def _refresh_devices(self) -> None: |
| 50 |
"""Refresh the devices.""" |
| 51 |
for adapter in self._bluez.adapter_details: |
| 52 |
i = int(adapter[3:]) |
| 53 |
for cls in (USBBluetoothDevice, UARTBluetoothDevice): |
| 54 |
dev = cls(i) |
| 55 |
self._devices[adapter] = dev |
| 56 |
try: |
| 57 |
dev.setup() |
| 58 |
except (NotAUARTDeviceError, NotAUSBDeviceError): |
| 59 |
continue |
| 60 |
except FileNotFoundError: |
| 61 |
break |
| 62 |
except Exception: # pylint: disable=broad-except |
| 63 |
_LOGGER.exception("Unexpected error setting up device hci%s", dev) |
| 64 |
break |
| 65 |
else: |
| 66 |
break |
| 67 |
|
| 68 |
@property |
| 69 |
def history(self) -> dict[str, AdvertisementHistory]: |
| 70 |
"""Get the bluez history.""" |
| 71 |
return self._bluez.history |
| 72 |
|
| 73 |
@property |
| 74 |
def adapters(self) -> dict[str, AdapterDetails]: |
| 75 |
"""Get the adapter details.""" |
| 76 |
manufacturer: str | None |
| 77 |
if self._adapters is None: |
| 78 |
adapters: dict[str, AdapterDetails] = {} |
| 79 |
if self._hci_output: |
| 80 |
for hci_details in self._hci_output.values(): |
| 81 |
name = hci_details["name"] |
| 82 |
mac_address = hci_details["bdaddr"].upper() |
| 83 |
if mac_address == EMPTY_MAC_ADDRESS: |
| 84 |
manufacturer = None |
| 85 |
else: |
| 86 |
manufacturer = aiooui.get_vendor(mac_address) |
| 87 |
adapters[name] = AdapterDetails( |
| 88 |
address=mac_address, |
| 89 |
sw_version=platform.release(), |
| 90 |
hw_version=None, |
| 91 |
passive_scan=False, # assume false if we don't know |
| 92 |
manufacturer=manufacturer, |
| 93 |
product=None, |
| 94 |
vendor_id=None, |
| 95 |
product_id=None, |
| 96 |
adapter_type=None, # Unknown for hci-only adapters |
| 97 |
advertise=hci_details["advertise"], |
| 98 |
powered=hci_details["powered"], |
| 99 |
) |
| 100 |
adapter_details = self._bluez.adapter_details |
| 101 |
for adapter, details in adapter_details.items(): |
| 102 |
if not (adapter1 := details.get("org.bluez.Adapter1")): |
| 103 |
continue |
| 104 |
mac_address = adapter1["Address"] |
| 105 |
device = self._devices[adapter] |
| 106 |
product: str | None = None |
| 107 |
manufacturer = None |
| 108 |
vendor_id: str | None = None |
| 109 |
product_id: str | None = None |
| 110 |
adapter_type: str | None = None |
| 111 |
powered = adapter1["Powered"] |
| 112 |
if isinstance(device, USBBluetoothDevice): |
| 113 |
adapter_type = "usb" |
| 114 |
usb_device = device.usb_device |
| 115 |
if mac_address != EMPTY_MAC_ADDRESS and ( |
| 116 |
usb_device is None |
| 117 |
or usb_device.vendor_id == usb_device.manufacturer |
| 118 |
or usb_device.manufacturer is None |
| 119 |
or usb_device.manufacturer == "Unknown" |
| 120 |
): |
| 121 |
manufacturer = aiooui.get_vendor(mac_address) |
| 122 |
elif usb_device is not None: |
| 123 |
manufacturer = usb_device.manufacturer |
| 124 |
if usb_device is not None: |
| 125 |
product = usb_device.product |
| 126 |
vendor_id = usb_device.vendor_id |
| 127 |
product_id = usb_device.product_id |
| 128 |
elif isinstance(device, UARTBluetoothDevice): |
| 129 |
adapter_type = "uart" |
| 130 |
uart_device = device.uart_device |
| 131 |
if uart_device is None: |
| 132 |
if mac_address != EMPTY_MAC_ADDRESS: |
| 133 |
manufacturer = aiooui.get_vendor(mac_address) |
| 134 |
else: |
| 135 |
product = uart_device.product |
| 136 |
|
| 137 |
if mac_address == EMPTY_MAC_ADDRESS: |
| 138 |
manufacturer = uart_device.manufacturer |
| 139 |
else: |
| 140 |
manufacturer = ( |
| 141 |
aiooui.get_vendor(mac_address) |
| 142 |
or uart_device.manufacturer |
| 143 |
) |
| 144 |
|
| 145 |
adapters[adapter] = AdapterDetails( |
| 146 |
address=mac_address, |
| 147 |
sw_version=platform.release(), |
| 148 |
hw_version=adapter1.get("Modalias"), |
| 149 |
passive_scan="org.bluez.AdvertisementMonitorManager1" in details, |
| 150 |
manufacturer=manufacturer, |
| 151 |
product=product, |
| 152 |
vendor_id=vendor_id, |
| 153 |
product_id=product_id, |
| 154 |
adapter_type=adapter_type, |
| 155 |
advertise="org.bluez.LEAdvertisingManager1" in details, |
| 156 |
powered=powered, |
| 157 |
) |
| 158 |
self._adapters = adapters |
| 159 |
return self._adapters |
| 160 |
|
| 161 |
@property |
| 162 |
def default_adapter(self) -> str: |
| 163 |
"""Get the default adapter.""" |
| 164 |
return UNIX_DEFAULT_BLUETOOTH_ADAPTER |