From 825018c0d8cda8ca3949891893f73fb944e6741f Mon Sep 17 00:00:00 2001 From: Danny Date: Tue, 17 Nov 2026 14:26:07 -0800 Subject: [PATCH] BlueZ: Restore Comma BT advertising Name --- openpilot/starpilot/bluetooth/owner.py | 13 ++++- .../starpilot/bluetooth/tests/test_owner.py | 51 +++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/openpilot/starpilot/bluetooth/owner.py b/openpilot/starpilot/bluetooth/owner.py index f69b39ecc4..bcb1359423 100644 --- a/openpilot/starpilot/bluetooth/owner.py +++ b/openpilot/starpilot/bluetooth/owner.py @@ -6,6 +6,7 @@ import fcntl import json import os import re +import socket import subprocess import tempfile import threading @@ -257,9 +258,17 @@ class BlueZ: 'audioSink': '0000110b-0000-1000-8000-00805f9b34fb' in profiles}) return devices + def ensure_name(self, path: str, adapter: dict) -> dict: + # Without hostnamed, BlueZ names the adapter "BlueZ x.y" + name = socket.gethostname() + if not path or not name or not str(adapter.get('Alias') or 'BlueZ').startswith('BlueZ'): + return adapter + self._call(path, 'org.freedesktop.DBus.Properties', 'Set', 'ssv', (ADAPTER, 'Alias', ('s', name))) + return dict(adapter, Alias=name) + def snapshot(self) -> dict: objects = self._objects() - _, adapter = self._adapter(objects) + adapter = self.ensure_name(*self._adapter(objects)) devices = sorted(self._devices(objects), key=lambda item: (not item['connected'], not item['paired'], item['name'])) return {'adapter': bool(adapter), 'powered': adapter.get('Powered') is True, 'discovering': adapter.get('Discovering') is True, @@ -323,6 +332,8 @@ class BlueZ: set_class = staticmethod(set_device_class) def set_pairing_visibility(self, path: str, *, pairable: bool, discoverable: bool) -> None: + if discoverable: + self.ensure_name(path, self._adapter(self._objects())[1]) address = DBusAddress(path, bus_name=BLUEZ, interface=ADAPTER) for key, value in (('Pairable', pairable), ('Discoverable', discoverable)): reply = self.router.send_and_get_reply(Properties(address).set(key, 'b', value), timeout=10.0) diff --git a/openpilot/starpilot/bluetooth/tests/test_owner.py b/openpilot/starpilot/bluetooth/tests/test_owner.py index 21529218fa..36308a32b7 100644 --- a/openpilot/starpilot/bluetooth/tests/test_owner.py +++ b/openpilot/starpilot/bluetooth/tests/test_owner.py @@ -788,6 +788,57 @@ class BluetoothOwnerTest(unittest.TestCase): self.assertIn(('/org/bluez/hci0', 'org.bluez.Adapter1', 'RemoveDevice', 'o', ('/org/bluez/hci0/dev_AA_BB_CC_DD_EE_FF',)), calls) + def test_bluez_replaces_default_adapter_name_with_hostname(self): + bluez = BlueZ.__new__(BlueZ) + calls = [] + adapter = {'Powered': ('b', True), 'Alias': ('s', 'BlueZ 5.72')} + objects = {'/org/bluez/hci0': {'org.bluez.Adapter1': adapter}} + + def call(path, interface, member, signature=None, body=()): + calls.append((path, interface, member, signature, body)) + if member == 'Set': + adapter['Alias'] = body[2] + return (objects,) if member == 'GetManagedObjects' else () + + self.enterContext(patch.object(bluez, '_call', call)) + self.enterContext(patch('openpilot.starpilot.bluetooth.owner.socket.gethostname', return_value='comma-5eb343c')) + rename = ('/org/bluez/hci0', 'org.freedesktop.DBus.Properties', 'Set', 'ssv', + ('org.bluez.Adapter1', 'Alias', ('s', 'comma-5eb343c'))) + self.assertEqual(bluez.snapshot()['adapterName'], 'comma-5eb343c') + self.assertEqual(calls.count(rename), 1) + self.assertEqual(bluez.snapshot()['adapterName'], 'comma-5eb343c') + self.assertEqual(calls.count(rename), 1) + + adapter['Alias'] = ('s', 'BlueZ 5.72') + bluez.set_pairing_visibility = BlueZ.set_pairing_visibility.__get__(bluez) + router = Mock() + router.send_and_get_reply.return_value.header.message_type = MessageType.method_return + bluez.router = router + bluez.set_pairing_visibility('/org/bluez/hci0', pairable=True, discoverable=True) + self.assertEqual(calls.count(rename), 2) + bluez.set_pairing_visibility('/org/bluez/hci0', pairable=False, discoverable=False) + self.assertEqual(calls.count(rename), 2) + + def test_bluez_keeps_custom_adapter_name_and_reports_rename_failure(self): + bluez = BlueZ.__new__(BlueZ) + calls = [] + objects = {'/org/bluez/hci0': {'org.bluez.Adapter1': {'Alias': ('s', 'My comma')}}} + + def call(path, interface, member, signature=None, body=()): + calls.append(member) + if member == 'Set': + raise BluetoothRejected('org.bluez.Error.Failed') + return (objects,) if member == 'GetManagedObjects' else () + + self.enterContext(patch.object(bluez, '_call', call)) + self.enterContext(patch('openpilot.starpilot.bluetooth.owner.socket.gethostname', return_value='comma-5eb343c')) + self.assertEqual(bluez.snapshot()['adapterName'], 'My comma') + self.assertNotIn('Set', calls) + objects['/org/bluez/hci0']['org.bluez.Adapter1']['Alias'] = ('s', 'BlueZ 5.72') + with self.assertRaises(BluetoothRejected): + bluez.snapshot() + self.assertIn('Set', calls) + def test_each_bluez_close_releases_router_and_underlying_connection(self): closed = []