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