bluetooth_device_chromeos.h revision 23730a6e56a168d1879203e4b3819bb36e3d8f1f
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_DEVICE_CHROMEOS_H
6#define DEVICE_BLUETOOTH_BLUETOOTH_DEVICE_CHROMEOS_H
7
8#include <string>
9
10#include "base/memory/scoped_ptr.h"
11#include "base/memory/weak_ptr.h"
12#include "chromeos/dbus/bluetooth_device_client.h"
13#include "dbus/object_path.h"
14#include "device/bluetooth/bluetooth_device.h"
15
16namespace chromeos {
17
18class BluetoothAdapterChromeOS;
19class BluetoothPairingChromeOS;
20
21// The BluetoothDeviceChromeOS class implements BluetoothDevice for the
22// Chrome OS platform.
23class BluetoothDeviceChromeOS
24    : public device::BluetoothDevice {
25 public:
26  // BluetoothDevice override
27  virtual uint32 GetBluetoothClass() const OVERRIDE;
28  virtual std::string GetAddress() const OVERRIDE;
29  virtual VendorIDSource GetVendorIDSource() const OVERRIDE;
30  virtual uint16 GetVendorID() const OVERRIDE;
31  virtual uint16 GetProductID() const OVERRIDE;
32  virtual uint16 GetDeviceID() const OVERRIDE;
33  virtual bool IsPaired() const OVERRIDE;
34  virtual bool IsConnected() const OVERRIDE;
35  virtual bool IsConnectable() const OVERRIDE;
36  virtual bool IsConnecting() const OVERRIDE;
37  virtual UUIDList GetUUIDs() const OVERRIDE;
38  virtual bool ExpectingPinCode() const OVERRIDE;
39  virtual bool ExpectingPasskey() const OVERRIDE;
40  virtual bool ExpectingConfirmation() const OVERRIDE;
41  virtual void Connect(
42      device::BluetoothDevice::PairingDelegate* pairing_delegate,
43      const base::Closure& callback,
44      const ConnectErrorCallback& error_callback) OVERRIDE;
45  virtual void SetPinCode(const std::string& pincode) OVERRIDE;
46  virtual void SetPasskey(uint32 passkey) OVERRIDE;
47  virtual void ConfirmPairing() OVERRIDE;
48  virtual void RejectPairing() OVERRIDE;
49  virtual void CancelPairing() OVERRIDE;
50  virtual void Disconnect(
51      const base::Closure& callback,
52      const ErrorCallback& error_callback) OVERRIDE;
53  virtual void Forget(const ErrorCallback& error_callback) OVERRIDE;
54  virtual void ConnectToService(
55      const std::string& service_uuid,
56      const SocketCallback& callback) OVERRIDE;
57  virtual void ConnectToProfile(
58      device::BluetoothProfile* profile,
59      const base::Closure& callback,
60      const ErrorCallback& error_callback) OVERRIDE;
61  virtual void SetOutOfBandPairingData(
62      const device::BluetoothOutOfBandPairingData& data,
63      const base::Closure& callback,
64      const ErrorCallback& error_callback) OVERRIDE;
65  virtual void ClearOutOfBandPairingData(
66      const base::Closure& callback,
67      const ErrorCallback& error_callback) OVERRIDE;
68
69  // Creates a pairing object with the given delegate |pairing_delegate| and
70  // establishes it as the pairing context for this device. All pairing-related
71  // method calls will be forwarded to this object until it is released.
72  BluetoothPairingChromeOS* BeginPairing(
73      BluetoothDevice::PairingDelegate* pairing_delegate);
74
75  // Releases the current pairing object, any pairing-related method calls will
76  // be ignored.
77  void EndPairing();
78
79  // Returns the current pairing object or NULL if no pairing is in progress.
80  BluetoothPairingChromeOS* GetPairing() const;
81
82 protected:
83   // BluetoothDevice override
84  virtual std::string GetDeviceName() const OVERRIDE;
85
86 private:
87  friend class BluetoothAdapterChromeOS;
88
89  BluetoothDeviceChromeOS(BluetoothAdapterChromeOS* adapter,
90                          const dbus::ObjectPath& object_path);
91  virtual ~BluetoothDeviceChromeOS();
92
93  // Internal method to initiate a connection to this device, and methods called
94  // by dbus:: on completion of the D-Bus method call.
95  void ConnectInternal(bool after_pairing,
96                       const base::Closure& callback,
97                       const ConnectErrorCallback& error_callback);
98  void OnConnect(bool after_pairing,
99                 const base::Closure& callback);
100  void OnConnectError(bool after_pairing,
101                      const ConnectErrorCallback& error_callback,
102                      const std::string& error_name,
103                      const std::string& error_message);
104
105  // Called by dbus:: on completion of the D-Bus method call to pair the device.
106  void OnPair(const base::Closure& callback,
107              const ConnectErrorCallback& error_callback);
108  void OnPairError(const ConnectErrorCallback& error_callback,
109                   const std::string& error_name,
110                   const std::string& error_message);
111
112  // Called by dbus:: on failure of the D-Bus method call to cancel pairing,
113  // there is no matching completion call since we don't do anything special
114  // in that case.
115  void OnCancelPairingError(const std::string& error_name,
116                            const std::string& error_message);
117
118  // Internal method to set the device as trusted. Trusted devices can connect
119  // to us automatically, and we can connect to them after rebooting; it also
120  // causes the device to be remembered by the stack even if not paired.
121  // |success| to the callback indicates whether or not the request succeeded.
122  void SetTrusted();
123  void OnSetTrusted(bool success);
124
125  // Called by dbus:: on completion of the D-Bus method call to disconnect the
126  // device.
127  void OnDisconnect(const base::Closure& callback);
128  void OnDisconnectError(const ErrorCallback& error_callback,
129                         const std::string& error_name,
130                         const std::string& error_message);
131
132  // Called by dbus:: on failure of the D-Bus method call to unpair the device;
133  // there is no matching completion call since this object is deleted in the
134  // process of unpairing.
135  void OnForgetError(const ErrorCallback& error_callback,
136                     const std::string& error_name,
137                     const std::string& error_message);
138
139  // Called by dbus:: on completion of the D-Bus method call to
140  // connect a peofile.
141  void OnConnectProfile(device::BluetoothProfile* profile,
142                        const base::Closure& callback);
143  void OnConnectProfileError(device::BluetoothProfile* profile,
144                             const ErrorCallback& error_callback,
145                             const std::string& error_name,
146                             const std::string& error_message);
147
148  // Returns the object path of the device; used by BluetoothAdapterChromeOS
149  const dbus::ObjectPath& object_path() const { return object_path_; }
150
151  // The adapter that owns this device instance.
152  BluetoothAdapterChromeOS* adapter_;
153
154  // The dbus object path of the device object.
155  dbus::ObjectPath object_path_;
156
157  // Number of ongoing calls to Connect().
158  int num_connecting_calls_;
159
160  // During pairing this is set to an object that we don't own, but on which
161  // we can make method calls to request, display or confirm PIN Codes and
162  // Passkeys. Generally it is the object that owns this one.
163  scoped_ptr<BluetoothPairingChromeOS> pairing_;
164
165  // Note: This should remain the last member so it'll be destroyed and
166  // invalidate its weak pointers before any other members are destroyed.
167  base::WeakPtrFactory<BluetoothDeviceChromeOS> weak_ptr_factory_;
168
169  DISALLOW_COPY_AND_ASSIGN(BluetoothDeviceChromeOS);
170};
171
172}  // namespace chromeos
173
174#endif  // DEVICE_BLUETOOTH_BLUETOOTH_DEVICE_CHROMEOS_H
175