| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 COMPONENTS_PROXIMITY_AUTH_BLE_BLUETOOTH_LOW_ENERGY_CONNECTION_FINDER_H | 5 #ifndef COMPONENTS_PROXIMITY_AUTH_BLE_BLUETOOTH_LOW_ENERGY_CONNECTION_FINDER_H |
| 6 #define COMPONENTS_PROXIMITY_AUTH_BLE_BLUETOOTH_LOW_ENERGY_CONNECTION_FINDER_H | 6 #define COMPONENTS_PROXIMITY_AUTH_BLE_BLUETOOTH_LOW_ENERGY_CONNECTION_FINDER_H |
| 7 | 7 |
| 8 #include <set> | 8 #include <set> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/callback.h" | 11 #include "base/callback.h" |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
| 16 #include "components/proximity_auth/connection.h" |
| 16 #include "components/proximity_auth/connection_finder.h" | 17 #include "components/proximity_auth/connection_finder.h" |
| 18 #include "components/proximity_auth/connection_observer.h" |
| 17 #include "device/bluetooth/bluetooth_adapter.h" | 19 #include "device/bluetooth/bluetooth_adapter.h" |
| 18 #include "device/bluetooth/bluetooth_device.h" | 20 #include "device/bluetooth/bluetooth_device.h" |
| 19 #include "device/bluetooth/bluetooth_discovery_session.h" | 21 #include "device/bluetooth/bluetooth_discovery_session.h" |
| 20 #include "device/bluetooth/bluetooth_gatt_connection.h" | 22 #include "device/bluetooth/bluetooth_gatt_connection.h" |
| 21 | 23 |
| 22 namespace proximity_auth { | 24 namespace proximity_auth { |
| 23 | 25 |
| 24 // This ConnectionFinder implementation is specialized in finding a Bluetooth | 26 // This ConnectionFinder implementation is specialized in finding a Bluetooth |
| 25 // Low Energy remote device. | 27 // Low Energy remote device. |
| 26 class BluetoothLowEnergyConnectionFinder | 28 class BluetoothLowEnergyConnectionFinder |
| 27 : public ConnectionFinder, | 29 : public ConnectionFinder, |
| 30 public ConnectionObserver, |
| 28 public device::BluetoothAdapter::Observer { | 31 public device::BluetoothAdapter::Observer { |
| 29 public: | 32 public: |
| 30 BluetoothLowEnergyConnectionFinder(const std::string& remote_service_uuid); | 33 BluetoothLowEnergyConnectionFinder( |
| 34 const std::string& remote_service_uuid, |
| 35 const std::string& to_peripheral_char_uuid, |
| 36 const std::string& from_peripheral_char_uuid); |
| 31 ~BluetoothLowEnergyConnectionFinder() override; | 37 ~BluetoothLowEnergyConnectionFinder() override; |
| 32 | 38 |
| 33 // Finds a connection to the remote device. Only the first one is functional. | 39 // Finds a connection to the remote device. Only the first one is functional. |
| 34 void Find(const device::BluetoothDevice::GattConnectionCallback& | |
| 35 connection_callback); | |
| 36 void Find(const ConnectionCallback& connection_callback) override; | 40 void Find(const ConnectionCallback& connection_callback) override; |
| 37 | 41 |
| 38 // Closes the connection and forgets the device. | 42 // Closes the connection and forgets the device. |
| 39 void CloseConnection(scoped_ptr<device::BluetoothGattConnection> connection); | 43 void CloseGattConnection( |
| 44 scoped_ptr<device::BluetoothGattConnection> gatt_connection); |
| 45 |
| 46 // proximity_auth::ConnectionObserver: |
| 47 void OnConnectionStatusChanged(Connection* connection, |
| 48 Connection::Status old_status, |
| 49 Connection::Status new_status) override; |
| 40 | 50 |
| 41 protected: | 51 protected: |
| 42 // device::BluetoothAdapter::Observer: | 52 // device::BluetoothAdapter::Observer: |
| 43 void DeviceAdded(device::BluetoothAdapter* adapter, | 53 void DeviceAdded(device::BluetoothAdapter* adapter, |
| 44 device::BluetoothDevice* device) override; | 54 device::BluetoothDevice* device) override; |
| 45 void DeviceChanged(device::BluetoothAdapter* adapter, | 55 void DeviceChanged(device::BluetoothAdapter* adapter, |
| 46 device::BluetoothDevice* device) override; | 56 device::BluetoothDevice* device) override; |
| 47 void DeviceRemoved(device::BluetoothAdapter* adapter, | 57 void DeviceRemoved(device::BluetoothAdapter* adapter, |
| 48 device::BluetoothDevice* device) override; | 58 device::BluetoothDevice* device) override; |
| 49 | 59 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 71 // Callback called when there is an error stopping |discovery_session_|. | 81 // Callback called when there is an error stopping |discovery_session_|. |
| 72 void OnStopDiscoverySessionError(); | 82 void OnStopDiscoverySessionError(); |
| 73 | 83 |
| 74 // Stops the discovery session given by |discovery_session_|. | 84 // Stops the discovery session given by |discovery_session_|. |
| 75 void StopDiscoverySession(); | 85 void StopDiscoverySession(); |
| 76 | 86 |
| 77 // Checks if a service with |service_uuid| is offered by |remote_device|. | 87 // Checks if a service with |service_uuid| is offered by |remote_device|. |
| 78 bool HasService(device::BluetoothDevice* remote_device); | 88 bool HasService(device::BluetoothDevice* remote_device); |
| 79 | 89 |
| 80 // Callback called when there is an error creating the connection. | 90 // Callback called when there is an error creating the connection. |
| 81 void OnCreateConnectionError( | 91 void OnCreateGattConnectionError( |
| 82 std::string device_address, | 92 std::string device_address, |
| 83 device::BluetoothDevice::ConnectErrorCode error_code); | 93 device::BluetoothDevice::ConnectErrorCode error_code); |
| 84 | 94 |
| 85 // Callback called when the connection is created. | 95 // Callback called when a GATT connection is created. |
| 86 void OnConnectionCreated( | 96 void OnGattConnectionCreated( |
| 87 scoped_ptr<device::BluetoothGattConnection> connection); | 97 scoped_ptr<device::BluetoothGattConnection> gatt_connection); |
| 88 | 98 |
| 89 // Creates a GATT connection with |remote_device|, |connection_callback_| will | 99 // Creates a GATT connection with |remote_device|, |connection_callback_| will |
| 90 // be called once the connection is established. | 100 // be called once the connection is established. |
| 91 void CreateConnection(device::BluetoothDevice* remote_device); | 101 void CreateGattConnection(device::BluetoothDevice* remote_device); |
| 102 |
| 103 // Creates a connection with |remote_device|. |
| 104 scoped_ptr<Connection> CreateConnection( |
| 105 scoped_ptr<device::BluetoothGattConnection> gatt_connection); |
| 92 | 106 |
| 93 // The uuid of the service it looks for to establish a GattConnection. | 107 // The uuid of the service it looks for to establish a GattConnection. |
| 94 device::BluetoothUUID remote_service_uuid_; | 108 device::BluetoothUUID remote_service_uuid_; |
| 95 | 109 |
| 110 // Characteristic used to send data to the remote device. |
| 111 device::BluetoothUUID to_peripheral_char_uuid_; |
| 112 |
| 113 // Characteristic used to receive data from the remote device. |
| 114 device::BluetoothUUID from_peripheral_char_uuid_; |
| 115 |
| 96 // The Bluetooth adapter over which the Bluetooth connection will be made. | 116 // The Bluetooth adapter over which the Bluetooth connection will be made. |
| 97 scoped_refptr<device::BluetoothAdapter> adapter_; | 117 scoped_refptr<device::BluetoothAdapter> adapter_; |
| 98 | 118 |
| 99 // The discovery session associated to this object. | 119 // The discovery session associated to this object. |
| 100 scoped_ptr<device::BluetoothDiscoverySession> discovery_session_; | 120 scoped_ptr<device::BluetoothDiscoverySession> discovery_session_; |
| 101 | 121 |
| 102 // True if a connection was established to a remote device that has the | 122 // True if a connection was established to a remote device that has the |
| 103 // service |remote_service_uuid|. | 123 // service |remote_service_uuid|. |
| 104 bool connected_; | 124 bool connected_; |
| 105 | 125 |
| 126 // The connection with |remote_device|. |
| 127 scoped_ptr<Connection> connection_; |
| 128 |
| 106 // Callback called when the connection is established. | 129 // Callback called when the connection is established. |
| 107 device::BluetoothDevice::GattConnectionCallback connection_callback_; | 130 // device::BluetoothDevice::GattConnectionCallback connection_callback_; |
| 131 ConnectionCallback connection_callback_; |
| 108 | 132 |
| 109 // The set of devices this connection finder has tried to connect to. | 133 // The set of devices this connection finder has tried to connect to. |
| 110 std::set<device::BluetoothDevice*> pending_connections_; | 134 std::set<device::BluetoothDevice*> pending_connections_; |
| 111 | 135 |
| 112 base::WeakPtrFactory<BluetoothLowEnergyConnectionFinder> weak_ptr_factory_; | 136 base::WeakPtrFactory<BluetoothLowEnergyConnectionFinder> weak_ptr_factory_; |
| 113 | 137 |
| 114 DISALLOW_COPY_AND_ASSIGN(BluetoothLowEnergyConnectionFinder); | 138 DISALLOW_COPY_AND_ASSIGN(BluetoothLowEnergyConnectionFinder); |
| 115 }; | 139 }; |
| 116 | 140 |
| 117 } // namespace proximity_auth | 141 } // namespace proximity_auth |
| 118 | 142 |
| 119 #endif // COMPONENTS_PROXIMITY_AUTH_BLE_BLUETOOTH_CONNECTION_FINDER_H | 143 #endif // COMPONENTS_PROXIMITY_AUTH_BLE_BLUETOOTH_CONNECTION_FINDER_H |
| OLD | NEW |