| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_H_ | 5 #ifndef DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_H_ |
| 6 #define DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_H_ | 6 #define DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_H_ |
| 7 | 7 |
| 8 #include <list> | 8 #include <list> |
| 9 #include <map> | 9 #include <map> |
| 10 #include <set> | 10 #include <set> |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 // this class also provides support for obtaining the list of remote devices | 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 | 29 // known to the adapter, discovering new devices, and providing notification of |
| 30 // updates to device information. | 30 // updates to device information. |
| 31 class BluetoothAdapter : public base::RefCounted<BluetoothAdapter> { | 31 class BluetoothAdapter : public base::RefCounted<BluetoothAdapter> { |
| 32 public: | 32 public: |
| 33 // Interface for observing changes from bluetooth adapters. | 33 // Interface for observing changes from bluetooth adapters. |
| 34 class Observer { | 34 class Observer { |
| 35 public: | 35 public: |
| 36 virtual ~Observer() {} | 36 virtual ~Observer() {} |
| 37 | 37 |
| 38 // Called when the presence of the adapter |adapter| changes, when |present| | 38 // Called when the presence of the adapter |adapter| changes, when |
| 39 // is true the adapter is now present, false means the adapter has been | 39 // |present| is true the adapter is now present, false means the adapter |
| 40 // removed from the system. | 40 // has been removed from the system. |
| 41 virtual void AdapterPresentChanged(BluetoothAdapter* adapter, | 41 virtual void AdapterPresentChanged(BluetoothAdapter* adapter, |
| 42 bool present) {} | 42 bool present) {} |
| 43 | 43 |
| 44 // Called when the radio power state of the adapter |adapter| changes, when | 44 // Called when the radio power state of the adapter |adapter| changes, |
| 45 // |powered| is true the adapter radio is powered, false means the adapter | 45 // when |powered| is true the adapter radio is powered, false means the |
| 46 // radio is off. | 46 // adapter radio is off. |
| 47 virtual void AdapterPoweredChanged(BluetoothAdapter* adapter, | 47 virtual void AdapterPoweredChanged(BluetoothAdapter* adapter, |
| 48 bool powered) {} | 48 bool powered) {} |
| 49 | 49 |
| 50 // Called when the discoverability state of the adapter |adapter| changes, | 50 // Called when the discoverability state of the adapter |adapter| changes, |
| 51 // when |discoverable| is true the adapter is discoverable by other devices, | 51 // when |discoverable| is true the adapter is discoverable by other devices, |
| 52 // false means the adapter is not discoverable. | 52 // false means the adapter is not discoverable. |
| 53 virtual void AdapterDiscoverableChanged(BluetoothAdapter* adapter, | 53 virtual void AdapterDiscoverableChanged(BluetoothAdapter* adapter, |
| 54 bool discoverable) {} | 54 bool discoverable) {} |
| 55 | 55 |
| 56 // Called when the discovering state of the adapter |adapter| changes, when | 56 // Called when the discovering state of the adapter |adapter| changes, |
| 57 // |discovering| is true the adapter is seeking new devices, false means it | 57 // when |discovering| is true the adapter is seeking new devices, false |
| 58 // is not. | 58 // means it is not. |
| 59 virtual void AdapterDiscoveringChanged(BluetoothAdapter* adapter, | 59 virtual void AdapterDiscoveringChanged(BluetoothAdapter* adapter, |
| 60 bool discovering) {} | 60 bool discovering) {} |
| 61 | 61 |
| 62 // Called when a new device |device| is added to the adapter |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| | 63 // either because it has been discovered or a connection made. |device| |
| 64 // should not be cached, instead copy its address. | 64 // should not be cached, instead copy its address. |
| 65 virtual void DeviceAdded(BluetoothAdapter* adapter, | 65 virtual void DeviceAdded(BluetoothAdapter* adapter, |
| 66 BluetoothDevice* device) {} | 66 BluetoothDevice* device) {} |
| 67 | 67 |
| 68 // Called when properties of the device |device| known to the adapter | 68 // Called when properties of the device |device| known to the adapter |
| 69 // |adapter| change. |device| should not be cached, instead copy its | 69 // |adapter| change. |device| should not be cached, instead copy its |
| 70 // address. | 70 // address. |
| 71 virtual void DeviceChanged(BluetoothAdapter* adapter, | 71 virtual void DeviceChanged(BluetoothAdapter* adapter, |
| 72 BluetoothDevice* device) {} | 72 BluetoothDevice* device) {} |
| 73 | 73 |
| 74 // Called when the device |device| is removed from the adapter |adapter|, | 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 | 75 // either as a result of a discovered device being lost between discovering |
| 76 // phases or pairing information deleted. |device| should not be cached. | 76 // phases or pairing information deleted. |device| should not be cached. |
| 77 virtual void DeviceRemoved(BluetoothAdapter* adapter, | 77 virtual void DeviceRemoved(BluetoothAdapter* adapter, |
| 78 BluetoothDevice* device) {} | 78 BluetoothDevice* device) {} |
| 79 }; | 79 }; |
| 80 | 80 |
| 81 // The ErrorCallback is used for methods that can fail in which case it is | 81 // The ErrorCallback is used for methods that can fail in which case it |
| 82 // called, in the success case the callback is simply not called. | 82 // is called, in the success case the callback is simply not called. |
| 83 typedef base::Closure ErrorCallback; | 83 typedef base::Closure ErrorCallback; |
| 84 | 84 |
| 85 // The BluetoothOutOfBandPairingDataCallback is used to return | 85 // The BluetoothOutOfBandPairingDataCallback is used to return |
| 86 // BluetoothOutOfBandPairingData to the caller. | 86 // BluetoothOutOfBandPairingData to the caller. |
| 87 typedef base::Callback<void(const BluetoothOutOfBandPairingData& data)> | 87 typedef base::Callback<void(const BluetoothOutOfBandPairingData& data)> |
| 88 BluetoothOutOfBandPairingDataCallback; | 88 BluetoothOutOfBandPairingDataCallback; |
| 89 | 89 |
| 90 // Adds and removes observers for events on this bluetooth adapter, if | 90 // Adds and removes observers for events on this bluetooth adapter, |
| 91 // monitoring multiple adapters check the |adapter| parameter of observer | 91 // if monitoring multiple adapters check the |adapter| parameter of |
| 92 // methods to determine which adapter is issuing the event. | 92 // observer methods to determine which adapter is issuing the event. |
| 93 virtual void AddObserver(BluetoothAdapter::Observer* observer) = 0; | 93 virtual void AddObserver(BluetoothAdapter::Observer* observer) = 0; |
| 94 virtual void RemoveObserver( | 94 virtual void RemoveObserver( |
| 95 BluetoothAdapter::Observer* observer) = 0; | 95 BluetoothAdapter::Observer* observer) = 0; |
| 96 | 96 |
| 97 // The address of this adapter. The address format is "XX:XX:XX:XX:XX:XX", | 97 // The address of this adapter. The address format is "XX:XX:XX:XX:XX:XX", |
| 98 // where each XX is a hexadecimal number. | 98 // where each XX is a hexadecimal number. |
| 99 virtual std::string GetAddress() const = 0; | 99 virtual std::string GetAddress() const = 0; |
| 100 | 100 |
| 101 // The name of the adapter. | 101 // The name of the adapter. |
| 102 virtual std::string GetName() const = 0; | 102 virtual std::string GetName() const = 0; |
| 103 | 103 |
| 104 // Set the human-readable name of the adapter to |name|. On success, | 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. | 105 // |callback| will be called. On failure, |error_callback| will be called. |
| 106 virtual void SetName(const std::string& name, | 106 virtual void SetName(const std::string& name, |
| 107 const base::Closure& callback, | 107 const base::Closure& callback, |
| 108 const ErrorCallback& error_callback) = 0; | 108 const ErrorCallback& error_callback) = 0; |
| 109 | 109 |
| 110 // Indicates whether the adapter is initialized and ready to use. | 110 // Indicates whether the adapter is initialized and ready to use. |
| 111 virtual bool IsInitialized() const = 0; | 111 virtual bool IsInitialized() const = 0; |
| 112 | 112 |
| 113 // Indicates whether the adapter is actually present on the system, for the | 113 // Indicates whether the adapter is actually present on the system, for |
| 114 // default adapter this indicates whether any adapter is present. An adapter | 114 // the default adapter this indicates whether any adapter is present. An |
| 115 // is only considered present if the address has been obtained. | 115 // adapter is only considered present if the address has been obtained. |
| 116 virtual bool IsPresent() const = 0; | 116 virtual bool IsPresent() const = 0; |
| 117 | 117 |
| 118 // Indicates whether the adapter radio is powered. | 118 // Indicates whether the adapter radio is powered. |
| 119 virtual bool IsPowered() const = 0; | 119 virtual bool IsPowered() const = 0; |
| 120 | 120 |
| 121 // Requests a change to the adapter radio power, setting |powered| to true | 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 | 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. | 123 // will be called. On failure, |error_callback| will be called. |
| 124 virtual void SetPowered(bool powered, | 124 virtual void SetPowered(bool powered, |
| 125 const base::Closure& callback, | 125 const base::Closure& callback, |
| (...skipping 28 matching lines...) Expand all Loading... |
| 154 // device discovery may actually be in progress. Clients can call GetDevices() | 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 | 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 | 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" | 157 // new and lost devices can by implementing the Observer methods "DeviceAdded" |
| 158 // and "DeviceRemoved". | 158 // and "DeviceRemoved". |
| 159 typedef base::Callback<void(scoped_ptr<BluetoothDiscoverySession>)> | 159 typedef base::Callback<void(scoped_ptr<BluetoothDiscoverySession>)> |
| 160 DiscoverySessionCallback; | 160 DiscoverySessionCallback; |
| 161 virtual void StartDiscoverySession(const DiscoverySessionCallback& callback, | 161 virtual void StartDiscoverySession(const DiscoverySessionCallback& callback, |
| 162 const ErrorCallback& error_callback); | 162 const ErrorCallback& error_callback); |
| 163 | 163 |
| 164 // Requests the list of devices from the adapter, all are returned including | 164 // Requests the list of devices from the adapter, all are returned |
| 165 // those currently connected and those paired. Use the returned device | 165 // including those currently connected and those paired. Use the |
| 166 // pointers to determine which they are. | 166 // returned device pointers to determine which they are. |
| 167 typedef std::vector<BluetoothDevice*> DeviceList; | 167 typedef std::vector<BluetoothDevice*> DeviceList; |
| 168 virtual DeviceList GetDevices(); | 168 virtual DeviceList GetDevices(); |
| 169 typedef std::vector<const BluetoothDevice*> ConstDeviceList; | 169 typedef std::vector<const BluetoothDevice*> ConstDeviceList; |
| 170 virtual ConstDeviceList GetDevices() const; | 170 virtual ConstDeviceList GetDevices() const; |
| 171 | 171 |
| 172 // Returns a pointer to the device with the given address |address| or NULL if | 172 // Returns a pointer to the device with the given address |address| or |
| 173 // no such device is known. | 173 // NULL if no such device is known. |
| 174 virtual BluetoothDevice* GetDevice(const std::string& address); | 174 virtual BluetoothDevice* GetDevice(const std::string& address); |
| 175 virtual const BluetoothDevice* GetDevice( | 175 virtual const BluetoothDevice* GetDevice( |
| 176 const std::string& address) const; | 176 const std::string& address) const; |
| 177 | 177 |
| 178 // Requests the local Out Of Band pairing data. | 178 // Requests the local Out Of Band pairing data. |
| 179 virtual void ReadLocalOutOfBandPairingData( | 179 virtual void ReadLocalOutOfBandPairingData( |
| 180 const BluetoothOutOfBandPairingDataCallback& callback, | 180 const BluetoothOutOfBandPairingDataCallback& callback, |
| 181 const ErrorCallback& error_callback) = 0; | 181 const ErrorCallback& error_callback) = 0; |
| 182 | 182 |
| 183 // Possible priorities for AddPairingDelegate(), low is intended for | 183 // Possible priorities for AddPairingDelegate(), low is intended for |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 262 // discovering. This should be called by all platform implementations. | 262 // discovering. This should be called by all platform implementations. |
| 263 void MarkDiscoverySessionsAsInactive(); | 263 void MarkDiscoverySessionsAsInactive(); |
| 264 | 264 |
| 265 // Removes |discovery_session| from |discovery_sessions_|, if its in there. | 265 // Removes |discovery_session| from |discovery_sessions_|, if its in there. |
| 266 // Called by DiscoverySession when an instance is destroyed or becomes | 266 // Called by DiscoverySession when an instance is destroyed or becomes |
| 267 // inactive. | 267 // inactive. |
| 268 void DiscoverySessionBecameInactive( | 268 void DiscoverySessionBecameInactive( |
| 269 BluetoothDiscoverySession* discovery_session); | 269 BluetoothDiscoverySession* discovery_session); |
| 270 | 270 |
| 271 // Devices paired with, connected to, discovered by, or visible to the | 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 | 272 // adapter. The key is the Bluetooth address of the device and the value |
| 273 // the BluetoothDevice object whose lifetime is managed by the adapter | 273 // is the BluetoothDevice object whose lifetime is managed by the |
| 274 // instance. | 274 // adapter instance. |
| 275 typedef std::map<const std::string, BluetoothDevice*> DevicesMap; | 275 typedef std::map<const std::string, BluetoothDevice*> DevicesMap; |
| 276 DevicesMap devices_; | 276 DevicesMap devices_; |
| 277 | 277 |
| 278 // Default pairing delegates registered with the adapter. | 278 // Default pairing delegates registered with the adapter. |
| 279 typedef std::pair<BluetoothDevice::PairingDelegate*, | 279 typedef std::pair<BluetoothDevice::PairingDelegate*, |
| 280 PairingDelegatePriority> PairingDelegatePair; | 280 PairingDelegatePriority> PairingDelegatePair; |
| 281 std::list<PairingDelegatePair> pairing_delegates_; | 281 std::list<PairingDelegatePair> pairing_delegates_; |
| 282 | 282 |
| 283 private: | 283 private: |
| 284 // List of active DiscoverySession objects. This is used to notify sessions to | 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 | 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 | 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 | 287 // will remove itself from this list when it gets destroyed or becomes |
| 288 // inactive by calling DiscoverySessionBecameInactive(), hence no pointers to | 288 // inactive by calling DiscoverySessionBecameInactive(), hence no pointers to |
| 289 // deallocated sessions are kept. | 289 // deallocated sessions are kept. |
| 290 std::set<BluetoothDiscoverySession*> discovery_sessions_; | 290 std::set<BluetoothDiscoverySession*> discovery_sessions_; |
| 291 | 291 |
| 292 // Note: This should remain the last member so it'll be destroyed and | 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. | 293 // invalidate its weak pointers before any other members are destroyed. |
| 294 base::WeakPtrFactory<BluetoothAdapter> weak_ptr_factory_; | 294 base::WeakPtrFactory<BluetoothAdapter> weak_ptr_factory_; |
| 295 }; | 295 }; |
| 296 | 296 |
| 297 } // namespace device | 297 } // namespace device |
| 298 | 298 |
| 299 #endif // DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_H_ | 299 #endif // DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_H_ |
| OLD | NEW |