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
3 // found in the LICENSE file.
5 #ifndef COMPONENTS_PROXIMITY_AUTH_BLE_BLUETOOTH_LOW_ENERGY_CONNECTION_FINDER_H
6 #define COMPONENTS_PROXIMITY_AUTH_BLE_BLUETOOTH_LOW_ENERGY_CONNECTION_FINDER_H
11 #include "base/callback.h"
12 #include "base/macros.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/weak_ptr.h"
16 #include "components/proximity_auth/connection.h"
17 #include "components/proximity_auth/connection_finder.h"
18 #include "components/proximity_auth/connection_observer.h"
19 #include "components/proximity_auth/remote_device.h"
20 #include "device/bluetooth/bluetooth_adapter.h"
21 #include "device/bluetooth/bluetooth_device.h"
22 #include "device/bluetooth/bluetooth_discovery_session.h"
23 #include "device/bluetooth/bluetooth_gatt_connection.h"
25 namespace proximity_auth
{
27 // This ConnectionFinder implementation is specialized in finding a Bluetooth
28 // Low Energy remote device.
29 class BluetoothLowEnergyConnectionFinder
30 : public ConnectionFinder
,
31 public ConnectionObserver
,
32 public device::BluetoothAdapter::Observer
{
34 BluetoothLowEnergyConnectionFinder(
35 const std::string
& remote_service_uuid
,
36 const std::string
& to_peripheral_char_uuid
,
37 const std::string
& from_peripheral_char_uuid
,
38 int max_number_of_tries
);
39 ~BluetoothLowEnergyConnectionFinder() override
;
41 // Finds a connection to the remote device. Only the first one is functional.
42 void Find(const ConnectionCallback
& connection_callback
) override
;
44 // proximity_auth::ConnectionObserver:
45 void OnConnectionStatusChanged(Connection
* connection
,
46 Connection::Status old_status
,
47 Connection::Status new_status
) override
;
49 // device::BluetoothAdapter::Observer:
50 void AdapterPoweredChanged(device::BluetoothAdapter
* adapter
,
51 bool powered
) override
;
52 void DeviceAdded(device::BluetoothAdapter
* adapter
,
53 device::BluetoothDevice
* device
) override
;
54 void DeviceChanged(device::BluetoothAdapter
* adapter
,
55 device::BluetoothDevice
* device
) override
;
56 void DeviceRemoved(device::BluetoothAdapter
* adapter
,
57 device::BluetoothDevice
* device
) override
;
60 // Closes the GATT connection. Virtual for testing.
61 virtual void CloseGattConnection(
62 scoped_ptr
<device::BluetoothGattConnection
> gatt_connection
);
64 // Creates a proximity_auth::Connection based on |gatt_connection|. Exposed
66 virtual scoped_ptr
<Connection
> CreateConnection(
67 scoped_ptr
<device::BluetoothGattConnection
> gatt_connection
);
69 // Sets |delay_after_gatt_connection_| for testing.
70 void SetDelayForTesting(base::TimeDelta delay
);
73 // Callback to be called when the Bluetooth adapter is initialized.
74 void OnAdapterInitialized(scoped_refptr
<device::BluetoothAdapter
> adapter
);
76 // Checks if |remote_device| contains |remote_service_uuid| and creates a
77 // connection in that case.
78 void HandleDeviceUpdated(device::BluetoothDevice
* remote_device
);
80 // Callback called when a new discovery session is started.
81 void OnDiscoverySessionStarted(
82 scoped_ptr
<device::BluetoothDiscoverySession
> discovery_session
);
84 // Callback called when there is an error starting a new discovery session.
85 void OnStartDiscoverySessionError();
87 // Starts a discovery session for |adapter_|.
88 void StartDiscoverySession();
90 // Callback called when |discovery_session_| is stopped.
91 void OnDiscoverySessionStopped();
93 // Callback called when there is an error stopping |discovery_session_|.
94 void OnStopDiscoverySessionError();
96 // Stops the discovery session given by |discovery_session_|.
97 void StopDiscoverySession();
99 // Checks if a service with |service_uuid| is offered by |remote_device|.
100 bool HasService(device::BluetoothDevice
* remote_device
);
102 // Callback called when there is an error creating the connection.
103 void OnCreateGattConnectionError(
104 std::string device_address
,
105 device::BluetoothDevice::ConnectErrorCode error_code
);
107 // Callback called when a GATT connection is created.
108 void OnGattConnectionCreated(
109 scoped_ptr
<device::BluetoothGattConnection
> gatt_connection
);
111 // Creates a GATT connection with |remote_device|, |connection_callback_| will
112 // be called once the connection is established.
113 void CreateGattConnection(device::BluetoothDevice
* remote_device
);
115 // Creates a BluetoothLowEnergyconnection object and adds the necessary
117 void CompleteConnection();
119 // Restarts the discovery session after creating |connection_| fails.
120 void RestartDiscoverySessionWhenReady();
122 // Returns the device with |device_address|.
123 device::BluetoothDevice
* GetDevice(std::string device_address
);
125 // The uuid of the service it looks for to establish a GattConnection.
126 device::BluetoothUUID remote_service_uuid_
;
128 // Characteristic used to send data to the remote device.
129 device::BluetoothUUID to_peripheral_char_uuid_
;
131 // Characteristic used to receive data from the remote device.
132 device::BluetoothUUID from_peripheral_char_uuid_
;
134 // The Bluetooth adapter over which the Bluetooth connection will be made.
135 scoped_refptr
<device::BluetoothAdapter
> adapter_
;
137 // The discovery session associated to this object.
138 scoped_ptr
<device::BluetoothDiscoverySession
> discovery_session_
;
140 // True if a connection was established with a paired remote device that has
141 // the service |remote_service_uuid_|.
144 // The remote device |gatt_connection_| was created with.
145 RemoteDevice remote_device_
;
147 // The GATT connection with |remote_device|.
148 scoped_ptr
<device::BluetoothGattConnection
> gatt_connection_
;
150 // The connection with |remote_device|.
151 scoped_ptr
<Connection
> connection_
;
153 // Callback called when the connection is established.
154 // device::BluetoothDevice::GattConnectionCallback connection_callback_;
155 ConnectionCallback connection_callback_
;
157 // The set of devices this connection finder has tried to connect to.
158 std::set
<device::BluetoothDevice
*> pending_connections_
;
160 // BluetoothLowEnergyConnection parameter.
161 int max_number_of_tries_
;
163 // Necessary delay after a GATT connection is created and before any
164 // read/write request is sent to the characteristics.
165 base::TimeDelta delay_after_gatt_connection_
;
167 base::WeakPtrFactory
<BluetoothLowEnergyConnectionFinder
> weak_ptr_factory_
;
169 DISALLOW_COPY_AND_ASSIGN(BluetoothLowEnergyConnectionFinder
);
172 } // namespace proximity_auth
174 #endif // COMPONENTS_PROXIMITY_AUTH_BLE_BLUETOOTH_CONNECTION_FINDER_H