|
| 1 | +""" |
| 2 | +Agent |
| 3 | +----- |
| 4 | +
|
| 5 | +This module contains types associated with the BlueZ D-Bus `agent api |
| 6 | +<https://github.com/bluez/bluez/blob/master/doc/agent-api.txt>`. |
| 7 | +""" |
| 8 | + |
| 9 | +import asyncio |
| 10 | +import contextlib |
| 11 | +import logging |
| 12 | +import os |
| 13 | +from typing import Set, no_type_check |
| 14 | + |
| 15 | +from dbus_fast import DBusError, Message |
| 16 | +from dbus_fast.aio import MessageBus |
| 17 | +from dbus_fast.service import ServiceInterface, method |
| 18 | + |
| 19 | +from bleak.backends.device import BLEDevice |
| 20 | + |
| 21 | +from ...agent import BaseBleakAgentCallbacks |
| 22 | +from . import defs |
| 23 | +from .manager import get_global_bluez_manager |
| 24 | +from .utils import assert_reply |
| 25 | + |
| 26 | +logger = logging.getLogger(__name__) |
| 27 | + |
| 28 | + |
| 29 | +class Agent(ServiceInterface): |
| 30 | + """ |
| 31 | + Implementation of the org.bluez.Agent1 D-Bus interface. |
| 32 | + """ |
| 33 | + |
| 34 | + def __init__(self, callbacks: BaseBleakAgentCallbacks): |
| 35 | + """ |
| 36 | + Args: |
| 37 | + """ |
| 38 | + super().__init__(defs.AGENT_INTERFACE) |
| 39 | + self._callbacks = callbacks |
| 40 | + self._tasks: Set[asyncio.Task] = set() |
| 41 | + |
| 42 | + async def _create_ble_device(self, device_path: str) -> BLEDevice: |
| 43 | + manager = await get_global_bluez_manager() |
| 44 | + props = manager.get_device_props(device_path) |
| 45 | + return BLEDevice( |
| 46 | + props["Address"], props["Alias"], {"path": device_path, "props": props} |
| 47 | + ) |
| 48 | + |
| 49 | + @method() |
| 50 | + def Release(self): |
| 51 | + logger.debug("Release") |
| 52 | + |
| 53 | + # REVISIT: mypy is broke, so we have to add redundant @no_type_check |
| 54 | + # https://github.com/python/mypy/issues/6583 |
| 55 | + |
| 56 | + @method() |
| 57 | + @no_type_check |
| 58 | + async def RequestPinCode(self, device: "o") -> "s": # noqa: F821 |
| 59 | + logger.debug("RequestPinCode %s", device) |
| 60 | + raise NotImplementedError |
| 61 | + |
| 62 | + @method() |
| 63 | + @no_type_check |
| 64 | + async def DisplayPinCode(self, device: "o", pincode: "s"): # noqa: F821 |
| 65 | + logger.debug("DisplayPinCode %s %s", device, pincode) |
| 66 | + raise NotImplementedError |
| 67 | + |
| 68 | + @method() |
| 69 | + @no_type_check |
| 70 | + async def RequestPasskey(self, device: "o") -> "u": # noqa: F821 |
| 71 | + logger.debug("RequestPasskey %s", device) |
| 72 | + |
| 73 | + ble_device = await self._create_ble_device(device) |
| 74 | + |
| 75 | + task = asyncio.create_task(self._callbacks.request_pin(ble_device)) |
| 76 | + self._tasks.add(task) |
| 77 | + |
| 78 | + try: |
| 79 | + pin = await task |
| 80 | + except asyncio.CancelledError: |
| 81 | + raise DBusError("org.bluez.Error.Canceled", "task canceled") |
| 82 | + finally: |
| 83 | + self._tasks.remove(task) |
| 84 | + |
| 85 | + if not pin: |
| 86 | + raise DBusError("org.bluez.Error.Rejected", "user rejected") |
| 87 | + |
| 88 | + return int(pin) |
| 89 | + |
| 90 | + @method() |
| 91 | + @no_type_check |
| 92 | + async def DisplayPasskey( |
| 93 | + self, device: "o", passkey: "u", entered: "q" # noqa: F821 |
| 94 | + ): |
| 95 | + passkey = f"{passkey:06}" |
| 96 | + logger.debug("DisplayPasskey %s %s %d", device, passkey, entered) |
| 97 | + raise NotImplementedError |
| 98 | + |
| 99 | + @method() |
| 100 | + @no_type_check |
| 101 | + async def RequestConfirmation(self, device: "o", passkey: "u"): # noqa: F821 |
| 102 | + passkey = f"{passkey:06}" |
| 103 | + logger.debug("RequestConfirmation %s %s", device, passkey) |
| 104 | + raise NotImplementedError |
| 105 | + |
| 106 | + @method() |
| 107 | + @no_type_check |
| 108 | + async def RequestAuthorization(self, device: "o"): # noqa: F821 |
| 109 | + logger.debug("RequestAuthorization %s", device) |
| 110 | + raise NotImplementedError |
| 111 | + |
| 112 | + @method() |
| 113 | + @no_type_check |
| 114 | + async def AuthorizeService(self, device: "o", uuid: "s"): # noqa: F821 |
| 115 | + logger.debug("AuthorizeService %s", device, uuid) |
| 116 | + raise NotImplementedError |
| 117 | + |
| 118 | + @method() |
| 119 | + @no_type_check |
| 120 | + def Cancel(self): # noqa: F821 |
| 121 | + logger.debug("Cancel") |
| 122 | + for t in self._tasks: |
| 123 | + t.cancel() |
| 124 | + |
| 125 | + |
| 126 | +@contextlib.asynccontextmanager |
| 127 | +async def bluez_agent(bus: MessageBus, callbacks: BaseBleakAgentCallbacks): |
| 128 | + agent = Agent(callbacks) |
| 129 | + |
| 130 | + # REVISIT: implement passing capability if needed |
| 131 | + # "DisplayOnly", "DisplayYesNo", "KeyboardOnly", "NoInputNoOutput", "KeyboardDisplay" |
| 132 | + capability = "" |
| 133 | + |
| 134 | + # this should be a unique path to allow multiple python interpreters |
| 135 | + # running bleak and multiple agents at the same time |
| 136 | + agent_path = f"/org/bleak/agent/{os.getpid()}/{id(agent)}" |
| 137 | + |
| 138 | + bus.export(agent_path, agent) |
| 139 | + |
| 140 | + try: |
| 141 | + reply = await bus.call( |
| 142 | + Message( |
| 143 | + destination=defs.BLUEZ_SERVICE, |
| 144 | + path="/org/bluez", |
| 145 | + interface=defs.AGENT_MANAGER_INTERFACE, |
| 146 | + member="RegisterAgent", |
| 147 | + signature="os", |
| 148 | + body=[agent_path, capability], |
| 149 | + ) |
| 150 | + ) |
| 151 | + |
| 152 | + assert_reply(reply) |
| 153 | + |
| 154 | + try: |
| 155 | + yield |
| 156 | + finally: |
| 157 | + reply = await bus.call( |
| 158 | + Message( |
| 159 | + destination=defs.BLUEZ_SERVICE, |
| 160 | + path="/org/bluez", |
| 161 | + interface=defs.AGENT_MANAGER_INTERFACE, |
| 162 | + member="UnregisterAgent", |
| 163 | + signature="o", |
| 164 | + body=[agent_path], |
| 165 | + ) |
| 166 | + ) |
| 167 | + |
| 168 | + assert_reply(reply) |
| 169 | + |
| 170 | + finally: |
| 171 | + bus.unexport(agent_path, agent) |
0 commit comments