1 // Copyright (c) 2012 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_ADAPTER_H_
6 #define DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_H_
14 #include "base/callback.h"
15 #include "base/memory/ref_counted.h"
16 #include "base/memory/weak_ptr.h"
17 #include "device/bluetooth/bluetooth_device.h"
21 class BluetoothDiscoverySession
;
23 struct BluetoothOutOfBandPairingData
;
25 // BluetoothAdapter represents a local Bluetooth adapter which may be used to
26 // interact with remote Bluetooth devices. As well as providing support for
27 // determining whether an adapter is present, and whether the radio is powered,
28 // this class also provides support for obtaining the list of remote devices
29 // known to the adapter, discovering new devices, and providing notification of
30 // updates to device information.
31 class BluetoothAdapter
: public base::RefCounted
<BluetoothAdapter
> {
33 // Interface for observing changes from bluetooth adapters.
36 virtual ~Observer() {}
38 // Called when the presence of the adapter |adapter| changes, when |present|
39 // is true the adapter is now present, false means the adapter has been
40 // removed from the system.
41 virtual void AdapterPresentChanged(BluetoothAdapter
* adapter
,
44 // Called when the radio power state of the adapter |adapter| changes, when
45 // |powered| is true the adapter radio is powered, false means the adapter
47 virtual void AdapterPoweredChanged(BluetoothAdapter
* adapter
,
50 // Called when the discoverability state of the adapter |adapter| changes,
51 // when |discoverable| is true the adapter is discoverable by other devices,
52 // false means the adapter is not discoverable.
53 virtual void AdapterDiscoverableChanged(BluetoothAdapter
* adapter
,
56 // Called when the discovering state of the adapter |adapter| changes, when
57 // |discovering| is true the adapter is seeking new devices, false means it
59 virtual void AdapterDiscoveringChanged(BluetoothAdapter
* adapter
,
62 // Called when a new device |device| is added to the adapter |adapter|,
63 // either because it has been discovered or a connection made. |device|
64 // should not be cached, instead copy its address.
65 virtual void DeviceAdded(BluetoothAdapter
* adapter
,
66 BluetoothDevice
* device
) {}
68 // Called when properties of the device |device| known to the adapter
69 // |adapter| change. |device| should not be cached, instead copy its
71 virtual void DeviceChanged(BluetoothAdapter
* adapter
,
72 BluetoothDevice
* device
) {}
74 // Called when the device |device| is removed from the adapter |adapter|,
75 // either as a result of a discovered device being lost between discovering
76 // phases or pairing information deleted. |device| should not be cached.
77 virtual void DeviceRemoved(BluetoothAdapter
* adapter
,
78 BluetoothDevice
* device
) {}
81 // The ErrorCallback is used for methods that can fail in which case it is
82 // called, in the success case the callback is simply not called.
83 typedef base::Closure ErrorCallback
;
85 // The BluetoothOutOfBandPairingDataCallback is used to return
86 // BluetoothOutOfBandPairingData to the caller.
87 typedef base::Callback
<void(const BluetoothOutOfBandPairingData
& data
)>
88 BluetoothOutOfBandPairingDataCallback
;
90 // Adds and removes observers for events on this bluetooth adapter, if
91 // monitoring multiple adapters check the |adapter| parameter of observer
92 // methods to determine which adapter is issuing the event.
93 virtual void AddObserver(BluetoothAdapter::Observer
* observer
) = 0;
94 virtual void RemoveObserver(
95 BluetoothAdapter::Observer
* observer
) = 0;
97 // The address of this adapter. The address format is "XX:XX:XX:XX:XX:XX",
98 // where each XX is a hexadecimal number.
99 virtual std::string
GetAddress() const = 0;
101 // The name of the adapter.
102 virtual std::string
GetName() const = 0;
104 // Set the human-readable name of the adapter to |name|. On success,
105 // |callback| will be called. On failure, |error_callback| will be called.
106 virtual void SetName(const std::string
& name
,
107 const base::Closure
& callback
,
108 const ErrorCallback
& error_callback
) = 0;
110 // Indicates whether the adapter is initialized and ready to use.
111 virtual bool IsInitialized() const = 0;
113 // Indicates whether the adapter is actually present on the system, for the
114 // default adapter this indicates whether any adapter is present. An adapter
115 // is only considered present if the address has been obtained.
116 virtual bool IsPresent() const = 0;
118 // Indicates whether the adapter radio is powered.
119 virtual bool IsPowered() const = 0;
121 // Requests a change to the adapter radio power, setting |powered| to true
122 // will turn on the radio and false will turn it off. On success, callback
123 // will be called. On failure, |error_callback| will be called.
124 virtual void SetPowered(bool powered
,
125 const base::Closure
& callback
,
126 const ErrorCallback
& error_callback
) = 0;
128 // Indicates whether the adapter radio is discoverable.
129 virtual bool IsDiscoverable() const = 0;
131 // Requests that the adapter change its discoverability state. If
132 // |discoverable| is true, then it will be discoverable by other Bluetooth
133 // devices. On successly changing the adapter's discoverability, |callback|
134 // will be called. On failure, |error_callback| will be called.
135 virtual void SetDiscoverable(bool discoverable
,
136 const base::Closure
& callback
,
137 const ErrorCallback
& error_callback
) = 0;
139 // Indicates whether the adapter is currently discovering new devices.
140 virtual bool IsDiscovering() const = 0;
142 // Requests the adapter to start a new discovery session. On success, a new
143 // instance of BluetoothDiscoverySession will be returned to the caller via
144 // |callback| and the adapter will be discovering nearby Bluetooth devices.
145 // The returned BluetoothDiscoverySession is owned by the caller and it's the
146 // owner's responsibility to properly clean it up and stop the session when
147 // device discovery is no longer needed.
149 // If clients desire device discovery to run, they should always call this
150 // method and never make it conditional on the value of IsDiscovering(), as
151 // another client might cause discovery to stop unexpectedly. Hence, clients
152 // should always obtain a BluetoothDiscoverySession and call
153 // BluetoothDiscoverySession::Stop when done. When this method gets called,
154 // device discovery may actually be in progress. Clients can call GetDevices()
155 // and check for those with IsPaired() as false to obtain the list of devices
156 // that have been discovered so far. Otherwise, clients can be notified of all
157 // new and lost devices can by implementing the Observer methods "DeviceAdded"
158 // and "DeviceRemoved".
159 typedef base::Callback
<void(scoped_ptr
<BluetoothDiscoverySession
>)>
160 DiscoverySessionCallback
;
161 virtual void StartDiscoverySession(const DiscoverySessionCallback
& callback
,
162 const ErrorCallback
& error_callback
);
164 // Requests the list of devices from the adapter, all are returned including
165 // those currently connected and those paired. Use the returned device
166 // pointers to determine which they are.
167 typedef std::vector
<BluetoothDevice
*> DeviceList
;
168 virtual DeviceList
GetDevices();
169 typedef std::vector
<const BluetoothDevice
*> ConstDeviceList
;
170 virtual ConstDeviceList
GetDevices() const;
172 // Returns a pointer to the device with the given address |address| or NULL if
173 // no such device is known.
174 virtual BluetoothDevice
* GetDevice(const std::string
& address
);
175 virtual const BluetoothDevice
* GetDevice(
176 const std::string
& address
) const;
178 // Requests the local Out Of Band pairing data.
179 virtual void ReadLocalOutOfBandPairingData(
180 const BluetoothOutOfBandPairingDataCallback
& callback
,
181 const ErrorCallback
& error_callback
) = 0;
183 // Possible priorities for AddPairingDelegate(), low is intended for
184 // permanent UI and high is intended for interactive UI or applications.
185 enum PairingDelegatePriority
{
186 PAIRING_DELEGATE_PRIORITY_LOW
,
187 PAIRING_DELEGATE_PRIORITY_HIGH
190 // Adds a default pairing delegate with priority |priority|, method calls
191 // will be made on |pairing_delegate| for incoming pairing requests if the
192 // priority is higher than any other registered, or for those of the same
193 // priority, the first registered.
195 // |pairing_delegate| must not be freed without first calling
196 // RemovePairingDelegate().
197 virtual void AddPairingDelegate(
198 BluetoothDevice::PairingDelegate
* pairing_delegate
,
199 PairingDelegatePriority priority
);
201 // Removes a previously added pairing delegate.
202 virtual void RemovePairingDelegate(
203 BluetoothDevice::PairingDelegate
* pairing_delegate
);
205 // Returns the first registered pairing delegate with the highest priority,
206 // or NULL if no delegate is registered. Used to select the delegate for
207 // incoming pairing requests.
208 virtual BluetoothDevice::PairingDelegate
* DefaultPairingDelegate();
211 friend class base::RefCounted
<BluetoothAdapter
>;
212 friend class BluetoothDiscoverySession
;
214 virtual ~BluetoothAdapter();
216 // Internal methods for initiating and terminating device discovery sessions.
217 // An implementation of BluetoothAdapter keeps an internal reference count to
218 // make sure that the underlying controller is constantly searching for nearby
219 // devices and retrieving information from them as long as there are clients
220 // who have requested discovery. These methods behave in the following way:
222 // On a call to AddDiscoverySession:
223 // - If there is a pending request to the subsystem, queue this request to
224 // execute once the pending requests are done.
225 // - If the count is 0, issue a request to the subsystem to start
226 // device discovery. On success, increment the count to 1.
227 // - If the count is greater than 0, increment the count and return
229 // As long as the count is non-zero, the underlying controller will be
230 // discovering for devices. This means that Chrome will restart device
231 // scan and inquiry sessions if they ever end, unless these sessions
232 // terminate due to an unexpected reason.
234 // On a call to RemoveDiscoverySession:
235 // - If there is a pending request to the subsystem, queue this request to
236 // execute once the pending requests are done.
237 // - If the count is 0, return failure, as there is no active discovery
239 // - If the count is 1, issue a request to the subsystem to stop device
240 // discovery and decrement the count to 0 on success.
241 // - If the count is greater than 1, decrement the count and return
244 // These methods invoke |callback| for success and |error_callback| for
246 virtual void AddDiscoverySession(const base::Closure
& callback
,
247 const ErrorCallback
& error_callback
) = 0;
248 virtual void RemoveDiscoverySession(const base::Closure
& callback
,
249 const ErrorCallback
& error_callback
) = 0;
251 // Called by RemovePairingDelegate() in order to perform any class-specific
252 // internal functionality necessary to remove the pairing delegate, such as
253 // cleaning up ongoing pairings using it.
254 virtual void RemovePairingDelegateInternal(
255 BluetoothDevice::PairingDelegate
* pairing_delegate
) = 0;
257 // Success callback passed to AddDiscoverySession by StartDiscoverySession.
258 void OnStartDiscoverySession(const DiscoverySessionCallback
& callback
);
260 // Marks all known DiscoverySession instances as inactive. Called by
261 // BluetoothAdapter in the event that the adapter unexpectedly stops
262 // discovering. This should be called by all platform implementations.
263 void MarkDiscoverySessionsAsInactive();
265 // Removes |discovery_session| from |discovery_sessions_|, if its in there.
266 // Called by DiscoverySession when an instance is destroyed or becomes
268 void DiscoverySessionBecameInactive(
269 BluetoothDiscoverySession
* discovery_session
);
271 // Devices paired with, connected to, discovered by, or visible to the
272 // adapter. The key is the Bluetooth address of the device and the value is
273 // the BluetoothDevice object whose lifetime is managed by the adapter
275 typedef std::map
<const std::string
, BluetoothDevice
*> DevicesMap
;
278 // Default pairing delegates registered with the adapter.
279 typedef std::pair
<BluetoothDevice::PairingDelegate
*,
280 PairingDelegatePriority
> PairingDelegatePair
;
281 std::list
<PairingDelegatePair
> pairing_delegates_
;
284 // List of active DiscoverySession objects. This is used to notify sessions to
285 // become inactive in case of an unexpected change to the adapter discovery
286 // state. We keep raw pointers, with the invariant that a DiscoverySession
287 // will remove itself from this list when it gets destroyed or becomes
288 // inactive by calling DiscoverySessionBecameInactive(), hence no pointers to
289 // deallocated sessions are kept.
290 std::set
<BluetoothDiscoverySession
*> discovery_sessions_
;
292 // Note: This should remain the last member so it'll be destroyed and
293 // invalidate its weak pointers before any other members are destroyed.
294 base::WeakPtrFactory
<BluetoothAdapter
> weak_ptr_factory_
;
297 } // namespace device
299 #endif // DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_H_