From 1e386423679aec9cf5c94c7dc1218ea468e66d6d Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Sun, 14 Feb 2016 14:45:23 -0600 Subject: in bluez.vala, watch for org.bluez to be owned/unowned on the system bus --- src/bluez.vala | 45 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 34 insertions(+), 11 deletions(-) diff --git a/src/bluez.vala b/src/bluez.vala index 80682cc..808a1cf 100644 --- a/src/bluez.vala +++ b/src/bluez.vala @@ -24,6 +24,7 @@ */ public class Bluez: Bluetooth, Object { + uint name_watch_id = 0; uint next_device_id = 1; ObjectManager manager; @@ -55,15 +56,6 @@ public class Bluez: Bluetooth, Object public Bluez (KillSwitch? killswitch) { - try - { - bus = Bus.get_sync (BusType.SYSTEM); - } - catch (Error e) - { - critical (@"$(e.message)"); - } - if ((killswitch != null) && (killswitch.is_valid())) { this.killswitch = killswitch; @@ -71,17 +63,44 @@ public class Bluez: Bluetooth, Object update_enabled (); } - reset_manager (); + name_watch_id = Bus.watch_name(BusType.SYSTEM, + "org.bluez", + BusNameWatcherFlags.AUTO_START, + on_bluez_appeared, + on_bluez_vanished); + } + + ~Bluez() + { + Bus.unwatch_name(name_watch_id); } - private void reset_manager () + private void on_bluez_appeared (GLib.DBusConnection connection, string name, string name_owner) + { + debug(@"name $name owned by $name_owner, setting up bluez proxies"); + + bus = connection; + + reset_bluez_lookup_vars(); + reset_manager(); + } + + private void on_bluez_vanished (GLib.DBusConnection connection, string name) + { + reset_bluez_lookup_vars(); + } + + private void reset_bluez_lookup_vars () { id_to_path = new HashTable (direct_hash, direct_equal); id_to_device = new HashTable (direct_hash, direct_equal); path_to_id = new HashTable (str_hash, str_equal); path_to_adapter_proxy = new HashTable (str_hash, str_equal); path_to_device_proxy = new HashTable (str_hash, str_equal); + } + private void reset_manager() + { try { manager = bus.get_proxy_sync ("org.bluez", "/"); @@ -134,6 +153,8 @@ public class Bluez: Bluetooth, Object private void update_adapter (ObjectPath object_path) { + debug(@"bluez5 calling update_adapter for $object_path"); + // Create a proxy if we don't have one var adapter_proxy = path_to_adapter_proxy.lookup (object_path); if (adapter_proxy == null) @@ -232,6 +253,8 @@ public class Bluez: Bluetooth, Object */ private void update_device (ObjectPath object_path) { + debug(@"bluez5 calling update_device for $object_path"); + // Create a proxy if we don't have one var device_proxy = path_to_device_proxy.lookup (object_path); if (device_proxy == null) -- cgit v1.2.3 From 5262691ed74c99daefb848d1038eda63217eca0e Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Sun, 14 Feb 2016 14:45:44 -0600 Subject: (trivial) remove trailing whitespace --- src/bluez.vala | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/bluez.vala b/src/bluez.vala index 808a1cf..f7121d7 100644 --- a/src/bluez.vala +++ b/src/bluez.vala @@ -20,7 +20,7 @@ /** - * Bluetooth implementaion which uses org.bluez on DBus + * Bluetooth implementaion which uses org.bluez on DBus */ public class Bluez: Bluetooth, Object { @@ -285,7 +285,7 @@ public class Bluez: Bluetooth, Object var v = device_proxy.get_cached_property ("Class"); if (v == null) type = Device.Type.OTHER; - else + else type = Device.class_to_device_type (v.get_uint32()); // look up the device's human-readable name -- cgit v1.2.3 From 55bd7905ec797fa7477969a3405f7f5b7c43cfd3 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Sun, 14 Feb 2016 14:46:38 -0600 Subject: in bluez.vala, create a symbolic name for bluez' busname --- src/bluez.vala | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/bluez.vala b/src/bluez.vala index f7121d7..b3d96dd 100644 --- a/src/bluez.vala +++ b/src/bluez.vala @@ -27,6 +27,7 @@ public class Bluez: Bluetooth, Object uint name_watch_id = 0; uint next_device_id = 1; ObjectManager manager; + static const string BLUEZ_BUSNAME = "org.bluez"; private bool _powered = false; @@ -64,7 +65,7 @@ public class Bluez: Bluetooth, Object } name_watch_id = Bus.watch_name(BusType.SYSTEM, - "org.bluez", + BLUEZ_BUSNAME, BusNameWatcherFlags.AUTO_START, on_bluez_appeared, on_bluez_vanished); @@ -103,7 +104,7 @@ public class Bluez: Bluetooth, Object { try { - manager = bus.get_proxy_sync ("org.bluez", "/"); + manager = bus.get_proxy_sync (BLUEZ_BUSNAME, "/"); // Find the adapters and watch for changes manager.interfaces_added.connect ((object_path, interfaces_and_properties) => { @@ -160,7 +161,7 @@ public class Bluez: Bluetooth, Object if (adapter_proxy == null) { try { - adapter_proxy = bus.get_proxy_sync ("org.bluez", object_path); + adapter_proxy = bus.get_proxy_sync (BLUEZ_BUSNAME, object_path); } catch (Error e) { critical (@"$(e.message)"); return; @@ -260,7 +261,7 @@ public class Bluez: Bluetooth, Object if (device_proxy == null) { try { - device_proxy = bus.get_proxy_sync ("org.bluez", object_path); + device_proxy = bus.get_proxy_sync (BLUEZ_BUSNAME, object_path); } catch (Error e) { critical (@"$(e.message)"); return; -- cgit v1.2.3 From 4fc9eb374f299b473f9ebf27dbb80a4b7f8a726e Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Sun, 14 Feb 2016 14:52:35 -0600 Subject: in bluez.vala, update our state properties when org.bluez vanishes from the bus --- src/bluez.vala | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/bluez.vala b/src/bluez.vala index b3d96dd..3e9df06 100644 --- a/src/bluez.vala +++ b/src/bluez.vala @@ -76,9 +76,9 @@ public class Bluez: Bluetooth, Object Bus.unwatch_name(name_watch_id); } - private void on_bluez_appeared (GLib.DBusConnection connection, string name, string name_owner) + private void on_bluez_appeared (DBusConnection connection, string name, string name_owner) { - debug(@"name $name owned by $name_owner, setting up bluez proxies"); + debug(@"$name owned by $name_owner, setting up bluez proxies"); bus = connection; @@ -86,9 +86,16 @@ public class Bluez: Bluetooth, Object reset_manager(); } - private void on_bluez_vanished (GLib.DBusConnection connection, string name) + private void on_bluez_vanished (DBusConnection connection, string name) { + debug(@"$name vanished from the bus"); + reset_bluez_lookup_vars(); + + devices_changed (); + update_combined_adapter_state (); + update_connected (); + update_enabled (); } private void reset_bluez_lookup_vars () -- cgit v1.2.3 From 20ea5744b5e5deaf7026e1a267fcfd99843580d2 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Sun, 14 Feb 2016 15:05:19 -0600 Subject: in bluez.vala, silence a startup g_warning by calling init_bluez_state_vars() before we start listening for org.bluez on the bus --- src/bluez.vala | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/bluez.vala b/src/bluez.vala index 3e9df06..b74b53a 100644 --- a/src/bluez.vala +++ b/src/bluez.vala @@ -57,6 +57,8 @@ public class Bluez: Bluetooth, Object public Bluez (KillSwitch? killswitch) { + init_bluez_state_vars (); + if ((killswitch != null) && (killswitch.is_valid())) { this.killswitch = killswitch; @@ -82,7 +84,7 @@ public class Bluez: Bluetooth, Object bus = connection; - reset_bluez_lookup_vars(); + init_bluez_state_vars(); reset_manager(); } @@ -90,15 +92,10 @@ public class Bluez: Bluetooth, Object { debug(@"$name vanished from the bus"); - reset_bluez_lookup_vars(); - - devices_changed (); - update_combined_adapter_state (); - update_connected (); - update_enabled (); + reset_bluez(); } - private void reset_bluez_lookup_vars () + private void init_bluez_state_vars () { id_to_path = new HashTable (direct_hash, direct_equal); id_to_device = new HashTable (direct_hash, direct_equal); @@ -107,6 +104,16 @@ public class Bluez: Bluetooth, Object path_to_device_proxy = new HashTable (str_hash, str_equal); } + private void reset_bluez () + { + init_bluez_state_vars (); + + devices_changed (); + update_combined_adapter_state (); + update_connected (); + update_enabled (); + } + private void reset_manager() { try -- cgit v1.2.3