mirror of
https://github.com/firestar5683/StarPilot.git
synced 2026-12-02 02:53:41 +08:00
BlueZ: Restore Comma BT advertising Name
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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 = []
|
||||
|
||||
|
||||
+1
-1
@@ -1 +1 @@
|
||||
{"protected_inventory_sha256": "84e64a7c77927010fa3f05e6660e8d3798ab5d393b5f79f728ae97ba827e1cc5", "schema_version": 1}
|
||||
{"protected_inventory_sha256": "047d2cbb252778b0dc65f5c781c55ac74edccc2869555f430ab8df5d1a11f1ba", "schema_version": 1}
|
||||
|
||||
Reference in New Issue
Block a user