Skip to content

Commit

Permalink
bluez5: Clean up static default properties, re-drop PairDevice class_…
Browse files Browse the repository at this point in the history
… 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.
  • Loading branch information
martinpitt committed Dec 23, 2023
1 parent 4d058ed commit 63264e1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 41 deletions.
56 changes: 16 additions & 40 deletions dbusmock/templates/bluez5.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,9 @@ def AddDevice(self, adapter_device_name, device_address, alias):
for the device (e.g. as set on the device itself), and the adapter device
name is the device_name passed to AddAdapter.
This will create a new, unpaired and unconnected device.
This will create a new, unpaired and unconnected device with some default properties
like MOCK_PHONE_CLASS "Class" and a static "Modalias". Especially when working with more
than one device, you may want to change these after creation.
Returns the new object path.
"""
Expand All @@ -400,8 +402,8 @@ def AddDevice(self, adapter_device_name, device_address, alias):
"Address": dbus.String(device_address, variant_level=1),
"AddressType": dbus.String("public", variant_level=1),
"Name": dbus.String(alias, variant_level=1),
"Icon": dbus.String("", variant_level=1),
"Class": dbus.UInt32(0, variant_level=1),
"Icon": dbus.String("phone", variant_level=1),
"Class": dbus.UInt32(MOCK_PHONE_CLASS, variant_level=1),
"Appearance": dbus.UInt16(0, variant_level=1),
"UUIDs": dbus.Array([], signature="s", variant_level=1),
"Paired": dbus.Boolean(False, variant_level=1),
Expand All @@ -412,7 +414,7 @@ def AddDevice(self, adapter_device_name, device_address, alias):
"Alias": dbus.String(alias, variant_level=1),
"Adapter": dbus.ObjectPath(adapter_path, variant_level=1),
"LegacyPairing": dbus.Boolean(False, variant_level=1),
"Modalias": dbus.String("", variant_level=1),
"Modalias": dbus.String("bluetooth:v000Fp1200d1436", variant_level=1),
"RSSI": dbus.Int16(-79, variant_level=1), # arbitrary
"TxPower": dbus.Int16(0, variant_level=1),
"ManufacturerData": dbus.Array([], signature="a{qv}", variant_level=1),
Expand Down Expand Up @@ -455,8 +457,8 @@ def AddDevice(self, adapter_device_name, device_address, alias):
return path


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

device.props[DEVICE_IFACE]["UUIDs"] = dbus.Array(uuids, variant_level=1)
device.props[DEVICE_IFACE]["Paired"] = dbus.Boolean(True, variant_level=1)
device.props[DEVICE_IFACE]["LegacyPairing"] = dbus.Boolean(True, variant_level=1)
device.props[DEVICE_IFACE]["Blocked"] = dbus.Boolean(False, variant_level=1)

try:
device.props[DEVICE_IFACE]["Modalias"]
except KeyError:
device.AddProperties(
DEVICE_IFACE,
{
"Modalias": dbus.String("bluetooth:v000Fp1200d1436", variant_level=1),
"Class": dbus.UInt32(class_, variant_level=1),
"Icon": dbus.String("phone", variant_level=1),
},
)

device.EmitSignal(
dbus.PROPERTIES_IFACE,
"PropertiesChanged",
"sa{sv}as",
[
DEVICE_IFACE,
{
"UUIDs": dbus.Array(uuids, variant_level=1),
"Paired": dbus.Boolean(True, variant_level=1),
"LegacyPairing": dbus.Boolean(True, variant_level=1),
"Blocked": dbus.Boolean(False, variant_level=1),
"Modalias": dbus.String("bluetooth:v000Fp1200d1436", variant_level=1),
"Class": dbus.UInt32(class_, variant_level=1),
"Icon": dbus.String("phone", variant_level=1),
},
[],
],
device.UpdateProperties(
DEVICE_IFACE,
{
"UUIDs": dbus.Array(uuids, variant_level=1),
"Paired": dbus.Boolean(True, variant_level=1),
"LegacyPairing": dbus.Boolean(True, variant_level=1),
"Blocked": dbus.Boolean(False, variant_level=1),
},
)


Expand Down
2 changes: 1 addition & 1 deletion tests/test_bluez5.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def test_pairing_device(self):
self.assertEqual(path, "/org/bluez/" + adapter_name + "/dev_" + address.replace(":", "_"))

# Pair with the device.
self.dbusmock_bluez.PairDevice(adapter_name, address, 5898764)
self.dbusmock_bluez.PairDevice(adapter_name, address)

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

0 comments on commit 63264e1

Please sign in to comment.