diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/bluetooth.vala | 138 | ||||
-rw-r--r-- | src/bluez.vala | 52 |
2 files changed, 185 insertions, 5 deletions
diff --git a/src/bluetooth.vala b/src/bluetooth.vala index b6d0e55..1952d1c 100644 --- a/src/bluetooth.vala +++ b/src/bluetooth.vala @@ -36,6 +36,144 @@ public class Bluetooth: Object killswitch.try_set_blocked (b); } + public class Device: Object { + public string name { get; construct; } + public bool supports_browsing { get; construct; } + public bool supports_file_transfer { get; construct; } + public Device (string name, + bool supports_browsing, + bool supports_file_transfer) { + Object (name: name, + supports_browsing: supports_browsing, + supports_file_transfer: supports_file_transfer); + } + } + + private static uint16 get_uuid16_from_uuid (string uuid) + { + uint16 uuid16; + + string[] tokens = uuid.split ("-", 1); + if (tokens.length > 0) + uuid16 = (uint16) uint64.parse ("0x"+tokens[0]); + else + uuid16 = 0; + + return uuid16; + } + + protected static bool uuid_supports_file_transfer (string uuid) + { + return get_uuid16_from_uuid (uuid) == 0x1105; // OBEXObjectPush + } + + protected static bool uuid_supports_browsing (string uuid) + { + return get_uuid16_from_uuid (uuid) == 0x1106; // OBEXFileTransfer + } + + public enum DeviceType + { + COMPUTER, + PHONE, + MODEM, + NETWORK, + HEADSET, + HEADPHONES, + VIDEO, + OTHER_AUDIO, + JOYPAD, + KEYPAD, + KEYBOARD, + TABLET, + MOUSE, + PRINTER, + CAMERA + } + + protected static DeviceType class_to_device_type (uint32 c) + { + switch ((c & 0x1f00) >> 8) + { + case 0x01: + return DeviceType.COMPUTER; + + case 0x02: + switch ((c & 0xfc) >> 2) + { + case 0x01: + case 0x02: + case 0x03: + case 0x05: + return DeviceType.PHONE; + + case 0x04: + return DeviceType.MODEM; + } + break; + + case 0x03: + return DeviceType.NETWORK; + + case 0x04: + switch ((c & 0xfc) >> 2) + { + case 0x01: + case 0x02: + return DeviceType.HEADSET; + + case 0x06: + return DeviceType.HEADPHONES; + + case 0x0b: // vcr + case 0x0c: // video camera + case 0x0d: // camcorder + return DeviceType.VIDEO; + + default: + return DeviceType.OTHER_AUDIO; + } + //break; + + case 0x05: + switch ((c & 0xc0) >> 6) + { + case 0x00: + switch ((c & 0x1e) >> 2) + { + case 0x01: + case 0x02: + return DeviceType.JOYPAD; + } + break; + + case 0x01: + return DeviceType.KEYBOARD; + + case 0x02: + switch ((c & 0x1e) >> 2) + { + case 0x05: + return DeviceType.TABLET; + + default: + return DeviceType.MOUSE; + } + } + break; + + case 0x06: + if ((c & 0x80) != 0) + return DeviceType.PRINTER; + if ((c & 0x20) != 0) + return DeviceType.CAMERA; + break; + } + + return 0; + } + + /*** **** Killswitch Implementation ***/ diff --git a/src/bluez.vala b/src/bluez.vala index 0f793cc..b0c8761 100644 --- a/src/bluez.vala +++ b/src/bluez.vala @@ -24,12 +24,15 @@ public class Bluez: Bluetooth { private org.bluez.Manager manager; private org.bluez.Adapter default_adapter; + private HashTable<string,Bluetooth.Device> devices; public Bluez (KillSwitch killswitch) { + base (killswitch); + string default_adapter_object_path = null; - base (killswitch); + this.devices = new HashTable<string,Bluetooth.Device>(str_hash, str_equal); try { @@ -51,17 +54,56 @@ public class Bluez: Bluetooth if (object_path != null) try { message ("using default adapter at %s", object_path); - this.default_adapter = Bus.get_proxy_sync (BusType.SYSTEM, "org.bluez", object_path); - this.default_adapter.property_changed.connect(() => this.on_default_adapter_properties_changed()); + default_adapter = Bus.get_proxy_sync (BusType.SYSTEM, "org.bluez", object_path); + default_adapter.property_changed.connect(() => on_default_adapter_properties_changed()); + + default_adapter.device_created.connect((adapter, path) => add_device (path)); + default_adapter.device_removed.connect((adapter, path) => devices.remove (path)); + foreach (string device_path in default_adapter.list_devices()) + add_device (device_path); } catch (Error e) - { + { critical ("%s", e.message); - } + } this.on_default_adapter_properties_changed (); } + private void add_device (string object_path) + { + try + { + org.bluez.Device device = Bus.get_proxy_sync (BusType.SYSTEM, "org.bluez", object_path); + message ("got device proxy for %s", object_path); + var properties = device.get_properties (); + + Variant v = properties.lookup ("Alias"); + if (v == null) + v = properties.lookup ("Name"); + string name = v == null ? _("Unknown") : v.get_string(); + + bool supports_browsing = false; + v = properties.lookup ("UUIDs"); + message ("%s", v.print(true)); + + bool supports_file_transfer = false; + + //protected static bool uuid_supports_file_transfer (string uuid) + //protected static bool uuid_supports_browsing (string uuid) + + var dev = new Bluetooth.Device (name, + supports_browsing, + supports_file_transfer); + devices.insert (object_path, dev); + message ("devices.size() is %u", devices.size()); + } + catch (Error e) + { + critical ("%s", e.message); + } + } + private void on_default_adapter_properties_changed () { bool is_discoverable = false; |