Persistent whitelist for Bluetooth low energy devices.
[chromium-blink-merge.git] / components / proximity_auth / ble / bluetooth_low_energy_connection_finder.h
blob0b22598cf31133e73ec0f9a2034c77db0aafb353
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
8 #include <set>
9 #include <string>
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 class BluetoothLowEnergyDeviceWhitelist;
29 // This ConnectionFinder implementation is specialized in finding a Bluetooth
30 // Low Energy remote device.
31 class BluetoothLowEnergyConnectionFinder
32 : public ConnectionFinder,
33 public ConnectionObserver,
34 public device::BluetoothAdapter::Observer {
35 public:
36 BluetoothLowEnergyConnectionFinder(
37 const std::string& remote_service_uuid,
38 const std::string& to_peripheral_char_uuid,
39 const std::string& from_peripheral_char_uuid,
40 const BluetoothLowEnergyDeviceWhitelist* device_whitelist,
41 int max_number_of_tries);
42 ~BluetoothLowEnergyConnectionFinder() override;
44 // Finds a connection to the remote device. Only the first one is functional.
45 void Find(const ConnectionCallback& connection_callback) override;
47 // proximity_auth::ConnectionObserver:
48 void OnConnectionStatusChanged(Connection* connection,
49 Connection::Status old_status,
50 Connection::Status new_status) override;
52 // device::BluetoothAdapter::Observer:
53 void AdapterPoweredChanged(device::BluetoothAdapter* adapter,
54 bool powered) override;
55 void DeviceAdded(device::BluetoothAdapter* adapter,
56 device::BluetoothDevice* device) override;
57 void DeviceChanged(device::BluetoothAdapter* adapter,
58 device::BluetoothDevice* device) override;
59 void DeviceRemoved(device::BluetoothAdapter* adapter,
60 device::BluetoothDevice* device) override;
62 protected:
63 // Closes the GATT connection. Virtual for testing.
64 virtual void CloseGattConnection(
65 scoped_ptr<device::BluetoothGattConnection> gatt_connection);
67 // Creates a proximity_auth::Connection based on |gatt_connection|. Exposed
68 // for testing.
69 virtual scoped_ptr<Connection> CreateConnection(
70 scoped_ptr<device::BluetoothGattConnection> gatt_connection);
72 // Sets |delay_after_gatt_connection_| for testing.
73 void SetDelayForTesting(base::TimeDelta delay);
75 private:
76 // Callback to be called when the Bluetooth adapter is initialized.
77 void OnAdapterInitialized(scoped_refptr<device::BluetoothAdapter> adapter);
79 // Checks if |remote_device| contains |remote_service_uuid| and creates a
80 // connection in that case.
81 void HandleDeviceUpdated(device::BluetoothDevice* remote_device);
83 // Callback called when a new discovery session is started.
84 void OnDiscoverySessionStarted(
85 scoped_ptr<device::BluetoothDiscoverySession> discovery_session);
87 // Callback called when there is an error starting a new discovery session.
88 void OnStartDiscoverySessionError();
90 // Starts a discovery session for |adapter_|.
91 void StartDiscoverySession();
93 // Callback called when |discovery_session_| is stopped.
94 void OnDiscoverySessionStopped();
96 // Callback called when there is an error stopping |discovery_session_|.
97 void OnStopDiscoverySessionError();
99 // Stops the discovery session given by |discovery_session_|.
100 void StopDiscoverySession();
102 // Checks if a service with |service_uuid| is offered by |remote_device|.
103 bool HasService(device::BluetoothDevice* remote_device);
105 // Callback called when there is an error creating the connection.
106 void OnCreateGattConnectionError(
107 std::string device_address,
108 device::BluetoothDevice::ConnectErrorCode error_code);
110 // Callback called when a GATT connection is created.
111 void OnGattConnectionCreated(
112 scoped_ptr<device::BluetoothGattConnection> gatt_connection);
114 // Creates a GATT connection with |remote_device|, |connection_callback_| will
115 // be called once the connection is established.
116 void CreateGattConnection(device::BluetoothDevice* remote_device);
118 // Creates a BluetoothLowEnergyconnection object and adds the necessary
119 // observers.
120 void CompleteConnection();
122 // Restarts the discovery session after creating |connection_| fails.
123 void RestartDiscoverySessionWhenReady();
125 // Returns the device with |device_address|.
126 device::BluetoothDevice* GetDevice(std::string device_address);
128 // The uuid of the service it looks for to establish a GattConnection.
129 device::BluetoothUUID remote_service_uuid_;
131 // Characteristic used to send data to the remote device.
132 device::BluetoothUUID to_peripheral_char_uuid_;
134 // Characteristic used to receive data from the remote device.
135 device::BluetoothUUID from_peripheral_char_uuid_;
137 // Devices in |device_whitelist_| don't need to have |remote_service_uuid_|
138 // cached or advertised. Not owned, must outlive this instance.
139 const BluetoothLowEnergyDeviceWhitelist* device_whitelist_;
141 // The Bluetooth adapter over which the Bluetooth connection will be made.
142 scoped_refptr<device::BluetoothAdapter> adapter_;
144 // The discovery session associated to this object.
145 scoped_ptr<device::BluetoothDiscoverySession> discovery_session_;
147 // True if a connection was established with a paired remote device that has
148 // the service |remote_service_uuid_|.
149 bool connected_;
151 // The remote device |gatt_connection_| was created with.
152 RemoteDevice remote_device_;
154 // The GATT connection with |remote_device|.
155 scoped_ptr<device::BluetoothGattConnection> gatt_connection_;
157 // The connection with |remote_device|.
158 scoped_ptr<Connection> connection_;
160 // Callback called when the connection is established.
161 // device::BluetoothDevice::GattConnectionCallback connection_callback_;
162 ConnectionCallback connection_callback_;
164 // The set of devices this connection finder has tried to connect to.
165 std::set<device::BluetoothDevice*> pending_connections_;
167 // BluetoothLowEnergyConnection parameter.
168 int max_number_of_tries_;
170 // Necessary delay after a GATT connection is created and before any
171 // read/write request is sent to the characteristics.
172 base::TimeDelta delay_after_gatt_connection_;
174 base::WeakPtrFactory<BluetoothLowEnergyConnectionFinder> weak_ptr_factory_;
176 DISALLOW_COPY_AND_ASSIGN(BluetoothLowEnergyConnectionFinder);
179 } // namespace proximity_auth
181 #endif // COMPONENTS_PROXIMITY_AUTH_BLE_BLUETOOTH_CONNECTION_FINDER_H