Skip to content

Commit 63264e1

Browse files
committed
bluez5: Clean up static default properties, re-drop PairDevice class_ parameter
Over the years, this template has accumulated some hacks and bad API which made PairDevice()'s handling of the Modalias/Class/Icon properties buggy and hard to understand: * These are *static* device properties, they are not supposed to change during pairing. * Commit ee29a44 added these as some kind of "dynamic fallback default" when they were not initialized by the caller after AddDevice(). * Commit 59d6af0 silently broke that fallback default by changing AddDevice() to set these device properties to empty strings. * Commit fae4be7 added another really bad API for setting Class in PairDevice()(). That API didn't fit into D-Bus (see commit 8968284 which had to make it a non-default parameter) and also broke the API, and moreover it is totally unintuitive -- the device class has nothing to do with pairing. Clean up all of these: Set the static property defaults in AddDevice() right away, so that the caller can adjust them afterwards. Re-drop the `class_` argument in PairDevice(). Adjust the documentation of AddDevice() to point out that properties should be changed after calling that. Consequently, PairDevice() will stop claiming that the static properties changed. This also gets rid of some redundant code.
1 parent 4d058ed commit 63264e1

File tree

2 files changed

+17
-41
lines changed

2 files changed

+17
-41
lines changed

dbusmock/templates/bluez5.py

Lines changed: 16 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,9 @@ def AddDevice(self, adapter_device_name, device_address, alias):
383383
for the device (e.g. as set on the device itself), and the adapter device
384384
name is the device_name passed to AddAdapter.
385385
386-
This will create a new, unpaired and unconnected device.
386+
This will create a new, unpaired and unconnected device with some default properties
387+
like MOCK_PHONE_CLASS "Class" and a static "Modalias". Especially when working with more
388+
than one device, you may want to change these after creation.
387389
388390
Returns the new object path.
389391
"""
@@ -400,8 +402,8 @@ def AddDevice(self, adapter_device_name, device_address, alias):
400402
"Address": dbus.String(device_address, variant_level=1),
401403
"AddressType": dbus.String("public", variant_level=1),
402404
"Name": dbus.String(alias, variant_level=1),
403-
"Icon": dbus.String("", variant_level=1),
404-
"Class": dbus.UInt32(0, variant_level=1),
405+
"Icon": dbus.String("phone", variant_level=1),
406+
"Class": dbus.UInt32(MOCK_PHONE_CLASS, variant_level=1),
405407
"Appearance": dbus.UInt16(0, variant_level=1),
406408
"UUIDs": dbus.Array([], signature="s", variant_level=1),
407409
"Paired": dbus.Boolean(False, variant_level=1),
@@ -412,7 +414,7 @@ def AddDevice(self, adapter_device_name, device_address, alias):
412414
"Alias": dbus.String(alias, variant_level=1),
413415
"Adapter": dbus.ObjectPath(adapter_path, variant_level=1),
414416
"LegacyPairing": dbus.Boolean(False, variant_level=1),
415-
"Modalias": dbus.String("", variant_level=1),
417+
"Modalias": dbus.String("bluetooth:v000Fp1200d1436", variant_level=1),
416418
"RSSI": dbus.Int16(-79, variant_level=1), # arbitrary
417419
"TxPower": dbus.Int16(0, variant_level=1),
418420
"ManufacturerData": dbus.Array([], signature="a{qv}", variant_level=1),
@@ -455,8 +457,8 @@ def AddDevice(self, adapter_device_name, device_address, alias):
455457
return path
456458

457459

458-
@dbus.service.method(BLUEZ_MOCK_IFACE, in_signature="ssi", out_signature="")
459-
def PairDevice(_self, adapter_device_name, device_address, class_):
460+
@dbus.service.method(BLUEZ_MOCK_IFACE, in_signature="ss", out_signature="")
461+
def PairDevice(_self, adapter_device_name, device_address):
460462
"""Convenience method to mark an existing device as paired.
461463
462464
You have to specify a device address which must be a valid Bluetooth
@@ -499,40 +501,14 @@ def PairDevice(_self, adapter_device_name, device_address, class_):
499501
"00001200-0000-1000-8000-00805f9b34fb",
500502
]
501503

502-
device.props[DEVICE_IFACE]["UUIDs"] = dbus.Array(uuids, variant_level=1)
503-
device.props[DEVICE_IFACE]["Paired"] = dbus.Boolean(True, variant_level=1)
504-
device.props[DEVICE_IFACE]["LegacyPairing"] = dbus.Boolean(True, variant_level=1)
505-
device.props[DEVICE_IFACE]["Blocked"] = dbus.Boolean(False, variant_level=1)
506-
507-
try:
508-
device.props[DEVICE_IFACE]["Modalias"]
509-
except KeyError:
510-
device.AddProperties(
511-
DEVICE_IFACE,
512-
{
513-
"Modalias": dbus.String("bluetooth:v000Fp1200d1436", variant_level=1),
514-
"Class": dbus.UInt32(class_, variant_level=1),
515-
"Icon": dbus.String("phone", variant_level=1),
516-
},
517-
)
518-
519-
device.EmitSignal(
520-
dbus.PROPERTIES_IFACE,
521-
"PropertiesChanged",
522-
"sa{sv}as",
523-
[
524-
DEVICE_IFACE,
525-
{
526-
"UUIDs": dbus.Array(uuids, variant_level=1),
527-
"Paired": dbus.Boolean(True, variant_level=1),
528-
"LegacyPairing": dbus.Boolean(True, variant_level=1),
529-
"Blocked": dbus.Boolean(False, variant_level=1),
530-
"Modalias": dbus.String("bluetooth:v000Fp1200d1436", variant_level=1),
531-
"Class": dbus.UInt32(class_, variant_level=1),
532-
"Icon": dbus.String("phone", variant_level=1),
533-
},
534-
[],
535-
],
504+
device.UpdateProperties(
505+
DEVICE_IFACE,
506+
{
507+
"UUIDs": dbus.Array(uuids, variant_level=1),
508+
"Paired": dbus.Boolean(True, variant_level=1),
509+
"LegacyPairing": dbus.Boolean(True, variant_level=1),
510+
"Blocked": dbus.Boolean(False, variant_level=1),
511+
},
536512
)
537513

538514

tests/test_bluez5.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ def test_pairing_device(self):
180180
self.assertEqual(path, "/org/bluez/" + adapter_name + "/dev_" + address.replace(":", "_"))
181181

182182
# Pair with the device.
183-
self.dbusmock_bluez.PairDevice(adapter_name, address, 5898764)
183+
self.dbusmock_bluez.PairDevice(adapter_name, address)
184184

185185
# Check the device's properties.
186186
out = "\n".join(_run_bluetoothctl("info " + address))

0 commit comments

Comments
 (0)