aboutsummaryrefslogtreecommitdiff
path: root/src/bluez.vala
diff options
context:
space:
mode:
authorCharles Kerr <charles.kerr@canonical.com>2013-08-05 18:24:41 -0500
committerCharles Kerr <charles.kerr@canonical.com>2013-08-05 18:24:41 -0500
commit76efb05ae0ad6111adb1cb127069d97bf2eb61da (patch)
treebbb9d467ce95fc7b82c5d6fa844d6291b3dedcbe /src/bluez.vala
parent77e298f113541feacb27c1deca14ec953b6559b0 (diff)
downloadayatana-indicator-bluetooth-76efb05ae0ad6111adb1cb127069d97bf2eb61da.tar.gz
ayatana-indicator-bluetooth-76efb05ae0ad6111adb1cb127069d97bf2eb61da.tar.bz2
ayatana-indicator-bluetooth-76efb05ae0ad6111adb1cb127069d97bf2eb61da.zip
copyediting: readability + grouping related methods together
Diffstat (limited to 'src/bluez.vala')
-rw-r--r--src/bluez.vala177
1 files changed, 106 insertions, 71 deletions
diff --git a/src/bluez.vala b/src/bluez.vala
index fa4b9ea..046362b 100644
--- a/src/bluez.vala
+++ b/src/bluez.vala
@@ -22,13 +22,21 @@
*/
public class Bluez: KillswitchBluetooth
{
- private org.bluez.Manager manager;
- private org.bluez.Adapter default_adapter;
- private HashTable<string,org.bluez.Device> path_to_proxy;
- private HashTable<string,uint> path_to_id;
- private HashTable<uint,string> id_to_path;
- private HashTable<uint,Device> id_to_device;
- private uint next_device_id = 1;
+ uint next_device_id = 1;
+ org.bluez.Manager manager;
+ org.bluez.Adapter default_adapter;
+
+ /* maps an org.bluez.Device's object_path to the org.bluez.Device proxy */
+ HashTable<string,org.bluez.Device> path_to_proxy;
+
+ /* maps an org.bluez.Device's object_path to our arbitrary unique id */
+ HashTable<string,uint> path_to_id;
+
+ /* maps our arbitrary unique id to an org.bluez.Device's object path */
+ HashTable<uint,string> id_to_path;
+
+ /* maps our arbitrary unique id to a Bluetooth.Device struct for public consumption */
+ HashTable<uint,Device> id_to_device;
public Bluez (KillSwitch killswitch)
{
@@ -44,8 +52,10 @@ public class Bluez: KillswitchBluetooth
try
{
manager = Bus.get_proxy_sync (BusType.SYSTEM, "org.bluez", "/");
- manager.default_adapter_changed.connect ((object_path) => on_default_adapter_changed (object_path));
+
+ // get the current default adapter, and watch for future default adapters
adapter_path = manager.default_adapter ();
+ manager.default_adapter_changed.connect ((object_path) => on_default_adapter_changed (object_path));
}
catch (Error e)
{
@@ -80,9 +90,37 @@ public class Bluez: KillswitchBluetooth
critical (@"$(e.message)");
}
- this.on_default_adapter_properties_changed ();
+ on_default_adapter_properties_changed ();
+ }
+
+ private void on_default_adapter_properties_changed ()
+ {
+ bool is_discoverable = false;
+ bool is_powered = false;
+
+ if (default_adapter != null) try
+ {
+ var properties = default_adapter.get_properties();
+
+ var v = properties.lookup("Discoverable");
+ is_discoverable = (v != null) && v.get_boolean ();
+
+ v = properties.lookup("Powered");
+ is_powered = (v != null) && v.get_boolean ();
+ }
+ catch (Error e)
+ {
+ critical (@"$(e.message)");
+ }
+
+ powered = is_powered;
+ discoverable = is_discoverable;
}
+ ////
+ //// bluetooth device UUIDs
+ ////
+
private static uint16 get_uuid16_from_uuid_string (string uuid)
{
uint16 uuid16;
@@ -116,10 +154,14 @@ public class Bluez: KillswitchBluetooth
return false;
}
- /* headsets, audio sinks, and input devices are connectable.
+ ////
+ //// Connectable Interfaces
+ ////
+
+ /* Headsets, Audio Sinks, and Input devices are connectable.
*
- * TODO: this duplicates the behavior of the indicator from when it used
- * gnome-bluetooth as a backend. Are there other interfaces we care about? */
+ * This continues the behavior of the old gnome-bluetooth indicator.
+ * But are there other interfaces we care about? */
private DBusInterfaceInfo[] get_connectable_interfaces (DBusProxy device)
{
DBusInterfaceInfo[] connectable_interfaces = {};
@@ -157,36 +199,54 @@ public class Bluez: KillswitchBluetooth
private bool device_is_connectable (DBusProxy device)
{
- var connectable_interfaces = get_connectable_interfaces (device);
- return connectable_interfaces.length > 0;
+ return get_connectable_interfaces (device).length > 0;
}
- private void device_connect (DBusProxy proxy)
+ // call "Connect" on the specified interface
+ private void device_connect_on_interface (DBusProxy proxy, string interface_name)
{
- var connection = proxy.get_connection ();
+ var bus = proxy.get_connection ();
var object_path = proxy.get_object_path ();
+ debug (@"trying to connect to $object_path: $(interface_name)");
+
+ try
+ {
+ bus.call_sync ("org.bluez", object_path, interface_name,
+ "Connect", null, null, DBusCallFlags.NONE, -1);
+ }
+ catch (Error e)
+ {
+ debug (@"Unable to call $interface_name.Connect() on $object_path: $(e.message)");
+ }
+ }
+
+ private void device_connect (org.bluez.Device device)
+ {
+ DBusProxy proxy = device as DBusProxy;
+
+ // call "Connect" on all the interfaces that support it
foreach (var i in get_connectable_interfaces (proxy))
+ device_connect_on_interface (proxy, i.name);
+ }
+
+ private void device_disconnect (org.bluez.Device device)
+ {
+ try
{
- try
- {
- debug (@"trying to connect to $object_path: $(i.name)");
- connection.call_sync ("org.bluez",
- object_path,
- i.name,
- "Connect",
- null,
- null,
- DBusCallFlags.NONE,
- -1);
- }
- catch (Error e)
- {
- debug (@"Unable to call $(i.name).Connect() on $(proxy.get_object_path()): $(e.message)");
- }
+ device.disconnect ();
+ }
+ catch (Error e)
+ {
+ var object_path = (device as DBusProxy).get_object_path ();
+ critical (@"Unable to disconnect $object_path: $(e.message)");
}
}
+ ////
+ //// Device Upkeep
+ ////
+
private void add_device (string object_path)
{
if (!path_to_proxy.contains (object_path))
@@ -205,6 +265,12 @@ public class Bluez: KillswitchBluetooth
}
}
+ /* Update our public Device struct from the org.bluez.Device's properties.
+ *
+ * This is called when we first walk through bluez' Devices on startup,
+ * when the org.bluez.Adapter gets a new device,
+ * and when a device's properties change s.t. we need to rebuild the proxy.
+ */
private void update_device (org.bluez.Device device_proxy)
{
HashTable<string, GLib.Variant> properties;
@@ -283,6 +349,10 @@ public class Bluez: KillswitchBluetooth
devices_changed ();
}
+ ////
+ //// Public API
+ ////
+
public override void set_device_connected (uint id, bool connected)
{
var device = id_to_device.lookup (id);
@@ -292,52 +362,17 @@ public class Bluez: KillswitchBluetooth
if ((proxy != null) && (device != null) && (device.is_connected != connected))
{
if (connected)
- {
- device_connect (proxy as DBusProxy);
- }
- else // disconnect
- {
- try
- {
- proxy.disconnect ();
- }
- catch (Error e)
- {
- critical (@"Unable to disconnect $object_path: $(e.message)");
- }
- }
+ device_connect (proxy);
+ else
+ device_disconnect (proxy);
}
}
- private void on_default_adapter_properties_changed ()
- {
- bool is_discoverable = false;
- bool is_powered = false;
-
- if (this.default_adapter != null) try
- {
- var properties = this.default_adapter.get_properties();
-
- var v = properties.lookup("Discoverable");
- is_discoverable = (v != null) && v.get_boolean ();
-
- v = properties.lookup("Powered");
- is_powered = (v != null) && v.get_boolean ();
- }
- catch (Error e)
- {
- critical (@"$(e.message)");
- }
-
- this.powered = is_powered;
- this.discoverable = is_discoverable;
- }
-
public override void try_set_discoverable (bool b)
{
if (discoverable != b) try
{
- this.default_adapter.set_property ("Discoverable", new Variant.boolean(b));
+ default_adapter.set_property ("Discoverable", new Variant.boolean(b));
}
catch (Error e)
{