ble: drop the 'event' argument from device-added

An 'event' boolean is less than obvious ('is-event' may be better). This
signalled the difference between a device-added during Manager startup and the
device-added at runtime (i.e. device goes online).

We don't need that differentiation. The manager adds all existing devices
immediately after connnect_to_bluez(). All we have to do is run through the
device list, add them locally and then subscribe to the signal.

This keeps the is-event confusion within one file only instead of spreading it
across two and an internal API. And we can re-name it to hotplugged.
This commit is contained in:
Peter Hutterer 2018-02-11 20:57:26 +10:00 committed by Benjamin Tissoires
parent 2ff4f8bca6
commit 637f539194
2 changed files with 18 additions and 12 deletions

View File

@ -231,8 +231,6 @@ class Tuhi(GObject.Object):
self.server.connect('search-start-requested', self._on_start_search_requested)
self.server.connect('search-stop-requested', self._on_stop_search_requested)
self.bluez = BlueZDeviceManager()
self.bluez.connect('device-added', self._on_bluez_device_updated)
self.bluez.connect('device-updated', self._on_bluez_device_updated)
self.bluez.connect('discovery-started', self._on_bluez_discovery_started)
self.bluez.connect('discovery-stopped', self._on_bluez_discovery_stopped)
@ -245,6 +243,11 @@ class Tuhi(GObject.Object):
def _on_tuhi_bus_name_acquired(self, dbus_server):
self.bluez.connect_to_bluez()
for dev in self.bluez.devices:
self._add_device(self.bluez, dev)
self.bluez.connect('device-added', self._on_bluez_device_updated)
self.bluez.connect('device-updated', self._on_bluez_device_updated)
def _on_tuhi_bus_name_lost(self, dbus_server):
self.mainloop.quit()
@ -282,7 +285,7 @@ class Tuhi(GObject.Object):
# restart discovery if some users are already in the listening mode
self._on_listening_updated(None, None)
def _on_bluez_device_updated(self, manager, bluez_device, event=True):
def _add_device(self, manager, bluez_device, hotplugged=False):
uuid = None
# check if the device is already known by us
@ -295,11 +298,11 @@ class Tuhi(GObject.Object):
if uuid is None and bluez_device.vendor_id not in WACOM_COMPANY_IDS:
return
# if event is set, the device has been 'hotplugged' in the bluez stack
# so ManufacturerData is reliable. Else, consider the device not in
# the register mode
# if the device has been 'hotplugged' in the bluez stack,
# ManufacturerData is reliable. Else, consider the device not in
# register mode
register_mode = False
if event:
if hotplugged:
register_mode = Tuhi._device_in_register_mode(bluez_device)
if not register_mode:
@ -323,6 +326,9 @@ class Tuhi(GObject.Object):
elif d.listening:
d.listen()
def _on_bluez_device_updated(self, manager, bluez_device):
self._add_device(manager, bluez_device, True)
def _on_listening_updated(self, tuhi_dbus_device, pspec):
listen = self._search_stop_handler is not None
for dev in self.devices.values():

View File

@ -291,7 +291,7 @@ class BlueZDeviceManager(GObject.Object):
'''
__gsignals__ = {
'device-added':
(GObject.SignalFlags.RUN_FIRST, None, (GObject.TYPE_PYOBJECT, GObject.TYPE_BOOLEAN)),
(GObject.SignalFlags.RUN_FIRST, None, (GObject.TYPE_PYOBJECT,)),
'device-updated':
(GObject.SignalFlags.RUN_FIRST, None, (GObject.TYPE_PYOBJECT,)),
'discovery-started':
@ -326,7 +326,7 @@ class BlueZDeviceManager(GObject.Object):
# object path length and process them in order, this way we're
# guaranteed that the objects we need already exist.
for obj in self._om.get_objects():
self._process_object(obj, event=False)
self._process_object(obj)
def _discovery_timeout_expired(self):
self.stop_discovery()
@ -428,7 +428,7 @@ class BlueZDeviceManager(GObject.Object):
if obj.get_interface(ORG_BLUEZ_ADAPTER1) is not None:
self._process_adapter(obj)
elif obj.get_interface(ORG_BLUEZ_DEVICE1) is not None:
self._process_device(obj, event)
self._process_device(obj)
elif obj.get_interface(ORG_BLUEZ_GATTCHARACTERISTIC1) is not None:
return True
@ -438,11 +438,11 @@ class BlueZDeviceManager(GObject.Object):
objpath = obj.get_object_path()
logger.debug(f'Adapter: {objpath}')
def _process_device(self, obj, event=True):
def _process_device(self, obj):
dev = BlueZDevice(self._om, obj)
self.devices.append(dev)
dev.connect('updated', self._on_device_updated)
self.emit('device-added', dev, event)
self.emit('device-added', dev)
def _process_characteristic(self, obj):
objpath = obj.get_object_path()