From cf894302767d5ec483b5f8002bb796a284895bbf Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 1 Aug 2013 18:21:05 -0500 Subject: add a bluetooth backend to track bluetooth being enabled, being hard/soft blocked, and its devices. --- src/bluetooth.vala | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 src/bluetooth.vala (limited to 'src/bluetooth.vala') diff --git a/src/bluetooth.vala b/src/bluetooth.vala new file mode 100644 index 0000000..ad5ee09 --- /dev/null +++ b/src/bluetooth.vala @@ -0,0 +1,53 @@ +/* + * Copyright 2013 Canonical Ltd. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + * + * Authors: + * Charles Kerr + */ + +public class Bluetooth: Object +{ + /*** + **** Properties + ***/ + + public bool discoverable { get; protected set; default = false; } + public virtual void try_set_discoverable (bool b) {} + + public bool powered { get; protected set; default = false; } + + public bool blocked { get; protected set; default = true; } + public virtual void try_set_blocked (bool b) { + kill_switch.try_set_blocked (b); + } + + /*** + **** Killswitch Implementation + ***/ + + protected KillSwitch kill_switch; + + public Bluetooth (KillSwitch kill_switch) + { + this.kill_switch = kill_switch; + + message ("changing blocked to %d", (int)!this.kill_switch.blocked); + blocked = this.kill_switch.blocked; + kill_switch.notify["blocked"].connect (() => { + message ("bluetooth changing blocked to %d", (int)kill_switch.blocked); + this.blocked = kill_switch.blocked; + }); + } +} -- cgit v1.2.3 From 5bc8ff5460d1b596649de5de71790aec952430b4 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 1 Aug 2013 18:33:44 -0500 Subject: improve documentation on Killswitch, Bluetooth, and Bluez --- src/bluetooth.vala | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) (limited to 'src/bluetooth.vala') diff --git a/src/bluetooth.vala b/src/bluetooth.vala index ad5ee09..b6d0e55 100644 --- a/src/bluetooth.vala +++ b/src/bluetooth.vala @@ -17,37 +17,35 @@ * Charles Kerr */ +/** + * Base class for the bluetooth backend. + */ public class Bluetooth: Object { - /*** - **** Properties - ***/ - + /* whether or not our system can be seen by other bluetooth devices */ public bool discoverable { get; protected set; default = false; } public virtual void try_set_discoverable (bool b) {} + /* whether or not there are any bluetooth adapters powered up on the system */ public bool powered { get; protected set; default = false; } + /* whether or not bluetooth's been disabled, + either by a software setting or physical hardware switch */ public bool blocked { get; protected set; default = true; } public virtual void try_set_blocked (bool b) { - kill_switch.try_set_blocked (b); + killswitch.try_set_blocked (b); } /*** **** Killswitch Implementation ***/ - protected KillSwitch kill_switch; + private KillSwitch killswitch; - public Bluetooth (KillSwitch kill_switch) + public Bluetooth (KillSwitch killswitch) { - this.kill_switch = kill_switch; - - message ("changing blocked to %d", (int)!this.kill_switch.blocked); - blocked = this.kill_switch.blocked; - kill_switch.notify["blocked"].connect (() => { - message ("bluetooth changing blocked to %d", (int)kill_switch.blocked); - this.blocked = kill_switch.blocked; - }); + this.killswitch = killswitch; + blocked = killswitch.blocked; + killswitch.notify["blocked"].connect (() => blocked = killswitch.blocked ); } } -- cgit v1.2.3 From 9837ce82575d089b52269ff600820d565be3fd22 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Sat, 3 Aug 2013 10:48:02 -0500 Subject: initial support for individual devices in Bluetooth class --- src/bluetooth.vala | 138 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 138 insertions(+) (limited to 'src/bluetooth.vala') 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 ***/ -- cgit v1.2.3 From c1ce02f2b8cd198712606888d08b36f1e7aefe39 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Mon, 5 Aug 2013 16:48:17 -0500 Subject: fully implement the bluez/device backend. in the desktop profile, add menuitems for the devices. --- src/bluetooth.vala | 191 ++++++++++++----------------------------------------- 1 file changed, 41 insertions(+), 150 deletions(-) (limited to 'src/bluetooth.vala') diff --git a/src/bluetooth.vala b/src/bluetooth.vala index 1952d1c..98464a4 100644 --- a/src/bluetooth.vala +++ b/src/bluetooth.vala @@ -17,173 +17,64 @@ * Charles Kerr */ + /** - * Base class for the bluetooth backend. + * Abstract interface for the Bluetooth backend. */ -public class Bluetooth: Object +public interface Bluetooth: Object { - /* whether or not our system can be seen by other bluetooth devices */ - public bool discoverable { get; protected set; default = false; } - public virtual void try_set_discoverable (bool b) {} + /* True if there are any bluetooth adapters powered up on the system. + In short, whether or not this system's bluetooth is "on". */ + public abstract bool powered { get; protected set; } - /* whether or not there are any bluetooth adapters powered up on the system */ - public bool powered { get; protected set; default = false; } + /* True if our system can be seen by other bluetooth devices */ + public abstract bool discoverable { get; protected set; } + public abstract void try_set_discoverable (bool discoverable); - /* whether or not bluetooth's been disabled, - either by a software setting or physical hardware switch */ - public bool blocked { get; protected set; default = true; } - public virtual void try_set_blocked (bool b) { - killswitch.try_set_blocked (b); - } + /* True if bluetooth's blocked. This can be soft-blocked by software and + * hard-blocked physically, eg by a laptop's network killswitch */ + public abstract bool blocked { get; protected set; } - 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; + /* Try to block/unblock bluetooth. This can fail if it's overridden + by the system, eg by a laptop's network killswitch */ + public abstract void try_set_blocked (bool b); - case 0x02: - switch ((c & 0xfc) >> 2) - { - case 0x01: - case 0x02: - case 0x03: - case 0x05: - return DeviceType.PHONE; + /* Get a list of the Device structs that we know about */ + public abstract List get_devices (); - case 0x04: - return DeviceType.MODEM; - } - break; + /* Emitted when one or more of the devices is added, removed, or changed */ + public signal void devices_changed (); - 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; - } + /* Try to connect/disconnect a particular device. + The device_key argument comes from the Device struct */ + public abstract void set_device_connected (uint device_key, bool connected); +} - /*** - **** Killswitch Implementation - ***/ +/** + * Base class for Bluetooth objects that use a killswitch to implement + * the 'discoverable' property. + */ +public abstract class KillswitchBluetooth: Object, Bluetooth +{ private KillSwitch killswitch; - public Bluetooth (KillSwitch killswitch) + public KillswitchBluetooth (KillSwitch killswitch) { + // always sync our 'blocked' property with the one in killswitch this.killswitch = killswitch; blocked = killswitch.blocked; killswitch.notify["blocked"].connect (() => blocked = killswitch.blocked ); } + + public bool powered { get; protected set; default = false; } + public bool discoverable { get; protected set; default = false; } + public bool blocked { get; protected set; default = true; } + public void try_set_blocked (bool b) { killswitch.try_set_blocked (b); } + + // empty implementations + public abstract void try_set_discoverable (bool b); + public abstract List get_devices (); + public abstract void set_device_connected (uint device_key, bool connected); } -- cgit v1.2.3