Skip to content

Commit

Permalink
bluezdbus: Add BLEAK_DBUS_UID override
Browse files Browse the repository at this point in the history
  • Loading branch information
kiwiz committed Dec 23, 2022
1 parent 9c26e49 commit cfa38ed
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to `Semantic Versioning <https://semver.org/spec/v2.0.0

Added
-----
* Added DBUS UID override for BlueZ
* Added return type None to some scanner methods.
* Added optional hack to use Bluetooth address instead of UUID on macOS.
* Added ``BleakScanner.find_device_by_name()`` class method.
Expand Down
4 changes: 2 additions & 2 deletions bleak/backends/bluezdbus/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from .characteristic import BleakGATTCharacteristicBlueZDBus
from .manager import get_global_bluez_manager
from .scanner import BleakScannerBlueZDBus
from .utils import assert_reply
from .utils import assert_reply, get_dbus_authenticator
from .version import BlueZFeatures

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -130,7 +130,7 @@ async def connect(self, dangerous_use_bleak_cache: bool = False, **kwargs) -> bo
# Each BLE connection session needs a new D-Bus connection to avoid a
# BlueZ quirk where notifications are automatically enabled on reconnect.
self._bus = await MessageBus(
bus_type=BusType.SYSTEM, negotiate_unix_fd=True
bus_type=BusType.SYSTEM, negotiate_unix_fd=True, auth=get_dbus_authenticator()
).connect()

def on_connected_changed(connected: bool) -> None:
Expand Down
4 changes: 2 additions & 2 deletions bleak/backends/bluezdbus/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
from .descriptor import BleakGATTDescriptorBlueZDBus
from .service import BleakGATTServiceBlueZDBus
from .signals import MatchRules, add_match
from .utils import assert_reply
from .utils import assert_reply, get_dbus_authenticator

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -187,7 +187,7 @@ async def async_init(self):
# We need to create a new MessageBus each time as
# dbus-next will destory the underlying file descriptors
# when the previous one is closed in its finalizer.
bus = MessageBus(bus_type=BusType.SYSTEM)
bus = MessageBus(bus_type=BusType.SYSTEM, auth=get_dbus_authenticator())
await bus.connect()

try:
Expand Down
12 changes: 12 additions & 0 deletions bleak/backends/bluezdbus/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
import re

from dbus_fast.auth import AuthExternal
from dbus_fast.constants import MessageType
from dbus_fast.message import Message

Expand Down Expand Up @@ -43,3 +44,14 @@ def bdaddr_from_device_path(device_path: str) -> str:
A Bluetooth address as a string.
"""
return ":".join(device_path[-17:].split("_"))


def get_dbus_authenticator():
uid = 0
try:
uid = int(os.environ("BLEAK_DBUS_UID"))
except ValueError as e:
pass
auth = None
if uid:
auth = AuthExternal(uid=uid)
6 changes: 6 additions & 0 deletions docs/backends/linux.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ individual Bleak objects should not be shared between event loops. Otherwise,
RuntimeErrors similar to ``[...] got Future <Future pending> attached to a
different loop`` will be thrown.

User Namespaces
---------------

DBus authentication will fail if the client is invoked from within a user namespace.
To work around this, use the `BLEAK_DBUS_UID` environment variable to hardcode a UID.

API
---

Expand Down

0 comments on commit cfa38ed

Please sign in to comment.