Home | History | Annotate | Download | only in bluetooth
      1 // Copyright 2013 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_CHROMEOS_H_
      6 #define DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_CHROMEOS_H_
      7 
      8 #include <queue>
      9 #include <string>
     10 
     11 #include "base/memory/weak_ptr.h"
     12 #include "base/sequenced_task_runner.h"
     13 #include "chromeos/dbus/bluetooth_adapter_client.h"
     14 #include "chromeos/dbus/bluetooth_agent_service_provider.h"
     15 #include "chromeos/dbus/bluetooth_device_client.h"
     16 #include "chromeos/dbus/bluetooth_input_client.h"
     17 #include "dbus/object_path.h"
     18 #include "device/bluetooth/bluetooth_adapter.h"
     19 #include "device/bluetooth/bluetooth_device.h"
     20 
     21 namespace device {
     22 class BluetoothSocketThread;
     23 }  // namespace device
     24 
     25 namespace chromeos {
     26 
     27 class BluetoothChromeOSTest;
     28 class BluetoothDeviceChromeOS;
     29 class BluetoothPairingChromeOS;
     30 class BluetoothRemoteGattCharacteristicChromeOS;
     31 class BluetoothRemoteGattDescriptorChromeOS;
     32 class BluetoothRemoteGattServiceChromeOS;
     33 
     34 // The BluetoothAdapterChromeOS class implements BluetoothAdapter for the
     35 // Chrome OS platform.
     36 class BluetoothAdapterChromeOS
     37     : public device::BluetoothAdapter,
     38       public chromeos::BluetoothAdapterClient::Observer,
     39       public chromeos::BluetoothDeviceClient::Observer,
     40       public chromeos::BluetoothInputClient::Observer,
     41       public chromeos::BluetoothAgentServiceProvider::Delegate {
     42  public:
     43   static base::WeakPtr<BluetoothAdapter> CreateAdapter();
     44 
     45   // BluetoothAdapter:
     46   virtual void AddObserver(
     47       device::BluetoothAdapter::Observer* observer) OVERRIDE;
     48   virtual void RemoveObserver(
     49       device::BluetoothAdapter::Observer* observer) OVERRIDE;
     50   virtual std::string GetAddress() const OVERRIDE;
     51   virtual std::string GetName() const OVERRIDE;
     52   virtual void SetName(const std::string& name,
     53                        const base::Closure& callback,
     54                        const ErrorCallback& error_callback) OVERRIDE;
     55   virtual bool IsInitialized() const OVERRIDE;
     56   virtual bool IsPresent() const OVERRIDE;
     57   virtual bool IsPowered() const OVERRIDE;
     58   virtual void SetPowered(
     59       bool powered,
     60       const base::Closure& callback,
     61       const ErrorCallback& error_callback) OVERRIDE;
     62   virtual bool IsDiscoverable() const OVERRIDE;
     63   virtual void SetDiscoverable(
     64       bool discoverable,
     65       const base::Closure& callback,
     66       const ErrorCallback& error_callback) OVERRIDE;
     67   virtual bool IsDiscovering() const OVERRIDE;
     68   virtual void CreateRfcommService(
     69       const device::BluetoothUUID& uuid,
     70       const ServiceOptions& options,
     71       const CreateServiceCallback& callback,
     72       const CreateServiceErrorCallback& error_callback) OVERRIDE;
     73   virtual void CreateL2capService(
     74       const device::BluetoothUUID& uuid,
     75       const ServiceOptions& options,
     76       const CreateServiceCallback& callback,
     77       const CreateServiceErrorCallback& error_callback) OVERRIDE;
     78 
     79   // Locates the device object by object path (the devices map and
     80   // BluetoothDevice methods are by address).
     81   BluetoothDeviceChromeOS* GetDeviceWithPath(
     82       const dbus::ObjectPath& object_path);
     83 
     84   // Announce to observers a change in device state that is not reflected by
     85   // its D-Bus properties.
     86   void NotifyDeviceChanged(BluetoothDeviceChromeOS* device);
     87 
     88   // The following methods are used to send various GATT observer events to
     89   // observers.
     90   void NotifyGattServiceAdded(BluetoothRemoteGattServiceChromeOS* service);
     91   void NotifyGattServiceRemoved(BluetoothRemoteGattServiceChromeOS* service);
     92   void NotifyGattServiceChanged(BluetoothRemoteGattServiceChromeOS* service);
     93   void NotifyGattDiscoveryComplete(BluetoothRemoteGattServiceChromeOS* service);
     94   void NotifyGattCharacteristicAdded(
     95       BluetoothRemoteGattCharacteristicChromeOS* characteristic);
     96   void NotifyGattCharacteristicRemoved(
     97       BluetoothRemoteGattCharacteristicChromeOS* characteristic);
     98   void NotifyGattDescriptorAdded(
     99       BluetoothRemoteGattDescriptorChromeOS* descriptor);
    100   void NotifyGattDescriptorRemoved(
    101       BluetoothRemoteGattDescriptorChromeOS* descriptor);
    102   void NotifyGattCharacteristicValueChanged(
    103       BluetoothRemoteGattCharacteristicChromeOS* characteristic,
    104       const std::vector<uint8>& value);
    105   void NotifyGattDescriptorValueChanged(
    106       BluetoothRemoteGattDescriptorChromeOS* descriptor,
    107       const std::vector<uint8>& value);
    108 
    109   // Returns the object path of the adapter.
    110   const dbus::ObjectPath& object_path() const { return object_path_; }
    111 
    112  protected:
    113   // BluetoothAdapter:
    114   virtual void RemovePairingDelegateInternal(
    115       device::BluetoothDevice::PairingDelegate* pairing_delegate) OVERRIDE;
    116 
    117  private:
    118   friend class BluetoothChromeOSTest;
    119 
    120   // typedef for callback parameters that are passed to AddDiscoverySession
    121   // and RemoveDiscoverySession. This is used to queue incoming requests while
    122   // a call to BlueZ is pending.
    123   typedef std::pair<base::Closure, ErrorCallback> DiscoveryCallbackPair;
    124   typedef std::queue<DiscoveryCallbackPair> DiscoveryCallbackQueue;
    125 
    126   BluetoothAdapterChromeOS();
    127   virtual ~BluetoothAdapterChromeOS();
    128 
    129   // BluetoothAdapterClient::Observer override.
    130   virtual void AdapterAdded(const dbus::ObjectPath& object_path) OVERRIDE;
    131   virtual void AdapterRemoved(const dbus::ObjectPath& object_path) OVERRIDE;
    132   virtual void AdapterPropertyChanged(
    133       const dbus::ObjectPath& object_path,
    134       const std::string& property_name) OVERRIDE;
    135 
    136   // BluetoothDeviceClient::Observer override.
    137   virtual void DeviceAdded(const dbus::ObjectPath& object_path) OVERRIDE;
    138   virtual void DeviceRemoved(const dbus::ObjectPath& object_path) OVERRIDE;
    139   virtual void DevicePropertyChanged(const dbus::ObjectPath& object_path,
    140                                      const std::string& property_name) OVERRIDE;
    141 
    142   // BluetoothInputClient::Observer override.
    143   virtual void InputPropertyChanged(const dbus::ObjectPath& object_path,
    144                                     const std::string& property_name) OVERRIDE;
    145 
    146   // BluetoothAgentServiceProvider::Delegate override.
    147   virtual void Released() OVERRIDE;
    148   virtual void RequestPinCode(const dbus::ObjectPath& device_path,
    149                               const PinCodeCallback& callback) OVERRIDE;
    150   virtual void DisplayPinCode(const dbus::ObjectPath& device_path,
    151                               const std::string& pincode) OVERRIDE;
    152   virtual void RequestPasskey(const dbus::ObjectPath& device_path,
    153                               const PasskeyCallback& callback) OVERRIDE;
    154   virtual void DisplayPasskey(const dbus::ObjectPath& device_path,
    155                               uint32 passkey, uint16 entered) OVERRIDE;
    156   virtual void RequestConfirmation(const dbus::ObjectPath& device_path,
    157                                    uint32 passkey,
    158                                    const ConfirmationCallback& callback)
    159       OVERRIDE;
    160   virtual void RequestAuthorization(const dbus::ObjectPath& device_path,
    161                                     const ConfirmationCallback& callback)
    162       OVERRIDE;
    163   virtual void AuthorizeService(const dbus::ObjectPath& device_path,
    164                                 const std::string& uuid,
    165                                 const ConfirmationCallback& callback) OVERRIDE;
    166   virtual void Cancel() OVERRIDE;
    167 
    168   // Called by dbus:: on completion of the D-Bus method call to register the
    169   // pairing agent.
    170   void OnRegisterAgent();
    171   void OnRegisterAgentError(const std::string& error_name,
    172                             const std::string& error_message);
    173 
    174   // Called by dbus:: on completion of the D-Bus method call to request that
    175   // the pairing agent be made the default.
    176   void OnRequestDefaultAgent();
    177   void OnRequestDefaultAgentError(const std::string& error_name,
    178                                   const std::string& error_message);
    179 
    180   // Internal method to obtain a BluetoothPairingChromeOS object for the device
    181   // with path |object_path|. Returns the existing pairing object if the device
    182   // already has one (usually an outgoing connection in progress) or a new
    183   // pairing object with the default pairing delegate if not. If no default
    184   // pairing object exists, NULL will be returned.
    185   BluetoothPairingChromeOS* GetPairing(const dbus::ObjectPath& object_path);
    186 
    187   // Set the tracked adapter to the one in |object_path|, this object will
    188   // subsequently operate on that adapter until it is removed.
    189   void SetAdapter(const dbus::ObjectPath& object_path);
    190 
    191   // Set the adapter name to one chosen from the system information.
    192   void SetDefaultAdapterName();
    193 
    194   // Remove the currently tracked adapter. IsPresent() will return false after
    195   // this is called.
    196   void RemoveAdapter();
    197 
    198   // Announce to observers a change in the adapter state.
    199   void PoweredChanged(bool powered);
    200   void DiscoverableChanged(bool discoverable);
    201   void DiscoveringChanged(bool discovering);
    202   void PresentChanged(bool present);
    203 
    204   // Called by dbus:: on completion of the discoverable property change.
    205   void OnSetDiscoverable(const base::Closure& callback,
    206                          const ErrorCallback& error_callback,
    207                          bool success);
    208 
    209   // Called by dbus:: on completion of an adapter property change.
    210   void OnPropertyChangeCompleted(const base::Closure& callback,
    211                                  const ErrorCallback& error_callback,
    212                                  bool success);
    213 
    214   // BluetoothAdapter:
    215   virtual void AddDiscoverySession(
    216       const base::Closure& callback,
    217       const ErrorCallback& error_callback) OVERRIDE;
    218   virtual void RemoveDiscoverySession(
    219       const base::Closure& callback,
    220       const ErrorCallback& error_callback) OVERRIDE;
    221 
    222   // Called by dbus:: on completion of the D-Bus method call to start discovery.
    223   void OnStartDiscovery(const base::Closure& callback);
    224   void OnStartDiscoveryError(const base::Closure& callback,
    225                              const ErrorCallback& error_callback,
    226                              const std::string& error_name,
    227                              const std::string& error_message);
    228 
    229   // Called by dbus:: on completion of the D-Bus method call to stop discovery.
    230   void OnStopDiscovery(const base::Closure& callback);
    231   void OnStopDiscoveryError(const ErrorCallback& error_callback,
    232                             const std::string& error_name,
    233                             const std::string& error_message);
    234 
    235   // Processes the queued discovery requests. For each DiscoveryCallbackPair in
    236   // the queue, this method will try to add a new discovery session. This method
    237   // is called whenever a pending D-Bus call to start or stop discovery has
    238   // ended (with either success or failure).
    239   void ProcessQueuedDiscoveryRequests();
    240 
    241   // Number of discovery sessions that have been added.
    242   int num_discovery_sessions_;
    243 
    244   // True, if there is a pending request to start or stop discovery.
    245   bool discovery_request_pending_;
    246 
    247   // List of queued requests to add new discovery sessions. While there is a
    248   // pending request to BlueZ to start or stop discovery, many requests from
    249   // within Chrome to start or stop discovery sessions may occur. We only
    250   // queue requests to add new sessions to be processed later. All requests to
    251   // remove a session while a call is pending immediately return failure. Note
    252   // that since BlueZ keeps its own reference count of applications that have
    253   // requested discovery, dropping our count to 0 won't necessarily result in
    254   // the controller actually stopping discovery if, for example, an application
    255   // other than Chrome, such as bt_console, was also used to start discovery.
    256   DiscoveryCallbackQueue discovery_request_queue_;
    257 
    258   // Object path of the adapter we track.
    259   dbus::ObjectPath object_path_;
    260 
    261   // List of observers interested in event notifications from us.
    262   ObserverList<device::BluetoothAdapter::Observer> observers_;
    263 
    264   // Instance of the D-Bus agent object used for pairing, initialized with
    265   // our own class as its delegate.
    266   scoped_ptr<BluetoothAgentServiceProvider> agent_;
    267 
    268   // UI thread task runner and socket thread object used to create sockets.
    269   scoped_refptr<base::SequencedTaskRunner> ui_task_runner_;
    270   scoped_refptr<device::BluetoothSocketThread> socket_thread_;
    271 
    272   // Note: This should remain the last member so it'll be destroyed and
    273   // invalidate its weak pointers before any other members are destroyed.
    274   base::WeakPtrFactory<BluetoothAdapterChromeOS> weak_ptr_factory_;
    275 
    276   DISALLOW_COPY_AND_ASSIGN(BluetoothAdapterChromeOS);
    277 };
    278 
    279 }  // namespace chromeos
    280 
    281 #endif  // DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_CHROMEOS_H_
    282