OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #ifndef CHROME_BROWSER_CHROMEOS_BLUETOOTH_BLUETOOTH_ADAPTER_H_ | |
6 #define CHROME_BROWSER_CHROMEOS_BLUETOOTH_BLUETOOTH_ADAPTER_H_ | |
7 | |
8 #include <map> | |
9 #include <string> | |
10 #include <vector> | |
11 | |
12 #include "base/basictypes.h" | |
13 #include "base/callback.h" | |
14 #include "base/memory/ref_counted.h" | |
15 #include "base/observer_list.h" | |
16 #include "chromeos/dbus/bluetooth_adapter_client.h" | |
17 #include "chromeos/dbus/bluetooth_device_client.h" | |
18 #include "chromeos/dbus/bluetooth_manager_client.h" | |
19 #include "chromeos/dbus/bluetooth_out_of_band_client.h" | |
20 #include "dbus/object_path.h" | |
21 | |
22 namespace chromeos { | |
23 | |
24 class BluetoothDevice; | |
25 | |
26 // The BluetoothAdapter class represents a local Bluetooth adapter which | |
27 // may be used to interact with remote Bluetooth devices. As well as | |
28 // providing support for determining whether an adapter is present, and | |
29 // whether the radio is powered, this class also provides support for | |
30 // obtaining the list of remote devices known to the adapter, discovering | |
31 // new devices, and providing notification of updates to device information. | |
32 // | |
33 // The class may be instantiated for either a specific adapter, or for the | |
34 // generic "default adapter" which may change depending on availability. | |
35 class BluetoothAdapter : public base::RefCounted<BluetoothAdapter>, | |
36 public BluetoothManagerClient::Observer, | |
37 public BluetoothAdapterClient::Observer, | |
38 public BluetoothDeviceClient::Observer { | |
39 public: | |
40 // Interface for observing changes from bluetooth adapters. | |
41 class Observer { | |
42 public: | |
43 virtual ~Observer() {} | |
44 | |
45 // Called when the presence of the adapter |adapter| changes, when | |
46 // |present| is true the adapter is now present, false means the adapter | |
47 // has been removed from the system. | |
48 virtual void AdapterPresentChanged(BluetoothAdapter* adapter, | |
49 bool present) {} | |
50 | |
51 // Called when the radio power state of the adapter |adapter| changes, | |
52 // when |powered| is true the adapter radio is powered, false means the | |
53 // adapter radio is off. | |
54 virtual void AdapterPoweredChanged(BluetoothAdapter* adapter, | |
55 bool powered) {} | |
56 | |
57 // Called when the discovering state of the adapter |adapter| changes, | |
58 // when |discovering| is true the adapter is seeking new devices, false | |
59 // means it is not. Note that device discovery involves both states when | |
60 // the adapter is seeking new devices and states when it is not because | |
61 // it is interrogating the devices it found. | |
62 virtual void AdapterDiscoveringChanged(BluetoothAdapter* adapter, | |
63 bool discovering) {} | |
64 | |
65 // Called when a new device |device| is added to the adapter |adapter|, | |
66 // either because it has been discovered or a connection made. |device| | |
67 // should not be cached, instead copy its address. | |
68 virtual void DeviceAdded(BluetoothAdapter* adapter, | |
69 BluetoothDevice* device) {} | |
70 | |
71 // Called when properties of the device |device| known to the adapter | |
72 // |adapter| change. |device| should not be cached, instead copy its | |
73 // address. | |
74 virtual void DeviceChanged(BluetoothAdapter* adapter, | |
75 BluetoothDevice* device) {} | |
76 | |
77 // Called when the device |device| is removed from the adapter |adapter|, | |
78 // either as a result of a discovered device being lost between discovering | |
79 // phases or pairing information deleted. |device| should not be cached. | |
80 virtual void DeviceRemoved(BluetoothAdapter* adapter, | |
81 BluetoothDevice* device) {} | |
82 }; | |
83 | |
84 // Adds and removes observers for events on this bluetooth adapter, | |
85 // if monitoring multiple adapters check the |adapter| parameter of | |
86 // observer methods to determine which adapter is issuing the event. | |
87 void AddObserver(Observer* observer); | |
88 void RemoveObserver(Observer* observer); | |
89 | |
90 // The ErrorCallback is used for methods that can fail in which case it | |
91 // is called, in the success case the callback is simply not called. | |
92 typedef base::Callback<void()> ErrorCallback; | |
93 | |
94 // The BluetoothOutOfBandPairingDataCallback is used to return | |
95 // BluetoothOutOfBandPairingData to the caller. | |
96 typedef base::Callback<void(const BluetoothOutOfBandPairingData& data)> | |
97 BluetoothOutOfBandPairingDataCallback; | |
98 | |
99 // The address of this adapter. The address format is "XX:XX:XX:XX:XX:XX", | |
100 // where each XX is a hexadecimal number. | |
101 const std::string& address() const { return address_; } | |
102 | |
103 // The name of the adapter. | |
104 const std::string& name() const { return name_; } | |
105 | |
106 // Indicates whether the adapter is actually present on the system, for | |
107 // the default adapter this indicates whether any adapter is present. | |
108 virtual bool IsPresent() const; | |
109 | |
110 // Indicates whether the adapter radio is powered. | |
111 virtual bool IsPowered() const; | |
112 | |
113 // Requests a change to the adapter radio power, setting |powered| to true | |
114 // will turn on the radio and false will turn it off. On success, callback | |
115 // will be called. On failure, |error_callback| will be called. | |
116 void SetPowered(bool powered, | |
117 const base::Closure& callback, | |
118 const ErrorCallback& error_callback); | |
119 | |
120 // Indicates whether the adapter is currently discovering new devices, | |
121 // note that a typical discovery process has phases of this being true | |
122 // followed by phases of being false when the adapter interrogates the | |
123 // devices found. | |
124 virtual bool IsDiscovering() const; | |
125 | |
126 // Requests that the adapter either begin discovering new devices when | |
127 // |discovering| is true, or cease any discovery when false. On success, | |
128 // callback will be called. On failure, |error_callback| will be called. | |
129 virtual void SetDiscovering(bool discovering, | |
130 const base::Closure& callback, | |
131 const ErrorCallback& error_callback); | |
132 | |
133 // Requests the list of devices from the adapter, all are returned | |
134 // including those currently connected and those paired. Use the | |
135 // returned device pointers to determine which they are. | |
136 typedef std::vector<BluetoothDevice*> DeviceList; | |
137 virtual DeviceList GetDevices(); | |
138 typedef std::vector<const BluetoothDevice*> ConstDeviceList; | |
139 virtual ConstDeviceList GetDevices() const; | |
140 | |
141 // Returns a pointer to the device with the given address |address| or | |
142 // NULL if no such device is known. | |
143 virtual BluetoothDevice* GetDevice(const std::string& address); | |
144 virtual const BluetoothDevice* GetDevice(const std::string& address) const; | |
145 | |
146 // Requests the local Out Of Band pairing data. | |
147 virtual void ReadLocalOutOfBandPairingData( | |
148 const BluetoothOutOfBandPairingDataCallback& callback, | |
149 const ErrorCallback& error_callback); | |
150 | |
151 // Returns the shared instance for the default adapter, whichever that may | |
152 // be at the time. Use IsPresent() and the AdapterPresentChanged() observer | |
153 // method to determine whether an adapter is actually available or not. | |
154 static scoped_refptr<BluetoothAdapter> DefaultAdapter(); | |
155 | |
156 // Creates an instance for a specific adapter named by |address|, which | |
157 // may be the bluetooth address of the adapter or a device name such as | |
158 // "hci0". | |
159 static BluetoothAdapter* Create(const std::string& address); | |
160 | |
161 private: | |
162 friend class base::RefCounted<BluetoothAdapter>; | |
163 friend class BluetoothDevice; | |
164 friend class MockBluetoothAdapter; | |
165 | |
166 BluetoothAdapter(); | |
167 virtual ~BluetoothAdapter(); | |
168 | |
169 // Obtains the default adapter object path from the Bluetooth Daemon | |
170 // and tracks future changes to it. | |
171 void TrackDefaultAdapter(); | |
172 | |
173 // Obtains the object paht for the adapter named by |address| from the | |
174 // Bluetooth Daemon. | |
175 void FindAdapter(const std::string& address); | |
176 | |
177 // Called by dbus:: in response to the method call sent by both | |
178 // DefaultAdapter() and FindAdapter(), |object_path| will contain the | |
179 // dbus object path of the requested adapter when |success| is true. | |
180 void AdapterCallback(const dbus::ObjectPath& adapter_path, bool success); | |
181 | |
182 // BluetoothManagerClient::Observer override. | |
183 // | |
184 // Called when the default local bluetooth adapter changes. | |
185 // |object_path| is the dbus object path of the new default adapter. | |
186 // Not called if all adapters are removed. | |
187 virtual void DefaultAdapterChanged(const dbus::ObjectPath& adapter_path) | |
188 OVERRIDE; | |
189 | |
190 // BluetoothManagerClient::Observer override. | |
191 // | |
192 // Called when a local bluetooth adapter is removed. | |
193 // |object_path| is the dbus object path of the adapter. | |
194 virtual void AdapterRemoved(const dbus::ObjectPath& adapter_path) OVERRIDE; | |
195 | |
196 // Changes the tracked adapter to the dbus object path |adapter_path|, | |
197 // clearing information from the previously tracked adapter and updating | |
198 // to the new adapter. | |
199 void ChangeAdapter(const dbus::ObjectPath& adapter_path); | |
200 | |
201 // Clears the tracked adapter information. | |
202 void RemoveAdapter(); | |
203 | |
204 // Called by dbus:: in response to the method call send by SetPowered(). | |
205 // |callback| and |error_callback| are the callbacks passed to SetPowered(). | |
206 void OnSetPowered(const base::Closure& callback, | |
207 const ErrorCallback& error_callback, | |
208 bool success); | |
209 | |
210 // Updates the tracked state of the adapter's radio power to |powered| | |
211 // and notifies observers. Called on receipt of a property changed signal, | |
212 // and directly using values obtained from properties. | |
213 void PoweredChanged(bool powered); | |
214 | |
215 // Called by dbus:: in response to the method calls send by SetDiscovering(). | |
216 // |callback| and |error_callback| are the callbacks passed to | |
217 // SetDiscovering(). | |
218 void OnStartDiscovery(const base::Closure& callback, | |
219 const ErrorCallback& error_callback, | |
220 const dbus::ObjectPath& adapter_path, | |
221 bool success); | |
222 void OnStopDiscovery(const base::Closure& callback, | |
223 const ErrorCallback& error_callback, | |
224 const dbus::ObjectPath& adapter_path, | |
225 bool success); | |
226 | |
227 // Updates the tracked state of the adapter's discovering state to | |
228 // |discovering| and notifies observers. Called on receipt of a property | |
229 // changed signal, and directly using values obtained from properties. | |
230 void DiscoveringChanged(bool discovering); | |
231 | |
232 // Called by dbus:: in response to the ReadLocalData method call. | |
233 void OnReadLocalData(const BluetoothOutOfBandPairingDataCallback& callback, | |
234 const ErrorCallback& error_callback, | |
235 const BluetoothOutOfBandPairingData& data, | |
236 bool success); | |
237 | |
238 // BluetoothAdapterClient::Observer override. | |
239 // | |
240 // Called when the adapter with object path |adapter_path| has a | |
241 // change in value of the property named |property_name|. | |
242 virtual void AdapterPropertyChanged(const dbus::ObjectPath& adapter_path, | |
243 const std::string& property_name) | |
244 OVERRIDE; | |
245 | |
246 // BluetoothDeviceClient::Observer override. | |
247 // | |
248 // Called when the device with object path |device_path| has a | |
249 // change in value of the property named |property_name|. | |
250 virtual void DevicePropertyChanged(const dbus::ObjectPath& device_path, | |
251 const std::string& property_name) | |
252 OVERRIDE; | |
253 | |
254 // Updates information on the device with object path |device_path|, | |
255 // adding it to the |devices_| map if not already present. | |
256 void UpdateDevice(const dbus::ObjectPath& device_path); | |
257 | |
258 // Clears the |devices_| list, notifying obsevers of the device removal. | |
259 void ClearDevices(); | |
260 | |
261 // BluetoothAdapterClient::Observer override. | |
262 // | |
263 // Called when the adapter with object path |object_path| has a | |
264 // new known device with object path |object_path|. | |
265 virtual void DeviceCreated(const dbus::ObjectPath& adapter_path, | |
266 const dbus::ObjectPath& device_path) OVERRIDE; | |
267 | |
268 // BluetoothAdapterClient::Observer override. | |
269 // | |
270 // Called when the adapter with object path |object_path| removes | |
271 // the known device with object path |object_path|. | |
272 virtual void DeviceRemoved(const dbus::ObjectPath& adapter_path, | |
273 const dbus::ObjectPath& device_path) OVERRIDE; | |
274 | |
275 // Updates the adapter |devices_| list, adding or updating devices using | |
276 // the object paths in the|devices| list. This doesn't remove devices, | |
277 // relying instead on the DeviceRemoved() signal for that. Called on | |
278 // receipt of a property changed signal, and directly using values obtained | |
279 // from properties. | |
280 void DevicesChanged(const std::vector<dbus::ObjectPath>& devices); | |
281 | |
282 // Clears discovered devices from the |devices_| list, notifying | |
283 // observers, and leaving only those devices with a dbus object path. | |
284 void ClearDiscoveredDevices(); | |
285 | |
286 // BluetoothAdapterClient::Observer override. | |
287 // | |
288 // Called when the adapter with object path |object_path| discovers | |
289 // a new remote device with address |address| and properties | |
290 // |properties|, there is no device object path until connected. | |
291 // | |
292 // |properties| supports only value() calls, not Get() or Set(), and | |
293 // should be copied if needed. | |
294 virtual void DeviceFound( | |
295 const dbus::ObjectPath& adapter_path, const std::string& address, | |
296 const BluetoothDeviceClient::Properties& properties) OVERRIDE; | |
297 | |
298 // BluetoothAdapterClient::Observer override. | |
299 // | |
300 // Called when the adapter with object path |object_path| can no | |
301 // longer communicate with the discovered removed device with | |
302 // address |address|. | |
303 virtual void DeviceDisappeared(const dbus::ObjectPath& object_path, | |
304 const std::string& address) OVERRIDE; | |
305 | |
306 // List of observers interested in event notifications from us. | |
307 ObserverList<BluetoothAdapter::Observer> observers_; | |
308 | |
309 // Object path of adapter for this instance, this is fixed at creation time | |
310 // unless |track_default_| is true in which case we update it to always | |
311 // point at the default adapter. | |
312 bool track_default_; | |
313 dbus::ObjectPath object_path_; | |
314 | |
315 // Address of the adapter. | |
316 std::string address_; | |
317 | |
318 // Name of the adapter. | |
319 std::string name_; | |
320 | |
321 // Tracked adapter state, cached locally so we only send change notifications | |
322 // to observers on a genuine change. | |
323 bool powered_; | |
324 bool discovering_; | |
325 | |
326 // Devices paired with, connected to, discovered by, or visible to the | |
327 // adapter. The key is the Bluetooth address of the device and the value | |
328 // is the BluetoothDevice object whose lifetime is managed by the adapter | |
329 // instance. | |
330 typedef std::map<const std::string, BluetoothDevice*> DevicesMap; | |
331 DevicesMap devices_; | |
332 | |
333 // Note: This should remain the last member so it'll be destroyed and | |
334 // invalidate its weak pointers before any other members are destroyed. | |
335 base::WeakPtrFactory<BluetoothAdapter> weak_ptr_factory_; | |
336 | |
337 DISALLOW_COPY_AND_ASSIGN(BluetoothAdapter); | |
338 }; | |
339 | |
340 } // namespace chromeos | |
341 | |
342 #endif // CHROME_BROWSER_CHROMEOS_BLUETOOTH_BLUETOOTH_ADAPTER_H_ | |
OLD | NEW |