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_CHARACTERISTICS_FINDER_H
6 #define COMPONENTS_PROXIMITY_AUTH_BLE_BLUETOOTH_LOW_ENERGY_CHARACTERISTICS_FINDER_H
8 #include "base/callback.h"
9 #include "base/macros.h"
10 #include "base/memory/ref_counted.h"
11 #include "components/proximity_auth/ble/remote_attribute.h"
12 #include "device/bluetooth/bluetooth_adapter.h"
13 #include "device/bluetooth/bluetooth_device.h"
14 #include "device/bluetooth/bluetooth_gatt_characteristic.h"
15 #include "device/bluetooth/bluetooth_uuid.h"
17 namespace proximity_auth
{
19 // Looks for given characteristics in a remote device, for which a GATT
20 // connection was already established. In the current BLE connection protocol
21 // (device::BluetoothDevice::CreateGattConnection), remote characteristic
22 // discovery starts immediatelly after a GATT connection was established. So,
23 // this class simply adds an observer for a characteristic discovery event and
24 // call |success_callback_| once all necessary characteristics were discovered.
25 class BluetoothLowEnergyCharacteristicsFinder
26 : public device::BluetoothAdapter::Observer
{
28 // This callbacks takes as arguments (in this order): |remote_service_|,
29 // |to_peripheral_char_| and |from_peripheral_char_|. Note that, since this is
30 // called after the characteristics were discovered, their id field (e.g.
31 // to_peripheral_char_.id) will be non-blank.
32 typedef base::Callback
<void(const RemoteAttribute
&,
33 const RemoteAttribute
&,
34 const RemoteAttribute
&)> SuccessCallback
;
36 // This callback takes as arguments (in this order): |to_peripheral_char_| and
37 // |from_peripheral_char_|. A blank id field in the characteristics indicate
38 // that the characteristics was not found in the remote service.
39 // TODO(sacomoto): Remove RemoteAttributes and add an error message instead.
40 // The caller of this object should not care if only a subset of the
41 // characteristics was found. See crbug.com/495511.
42 typedef base::Callback
<void(const RemoteAttribute
&, const RemoteAttribute
&)>
45 // Constructs the object and registers itself as an observer for |adapter|,
46 // waiting for |to_peripheral_char| and |from_peripheral_char| to be found.
47 // When both characteristics were found |success_callback| is called. After
48 // all characteristics of |service| were discovered, if |from_periphral_char|
49 // or |to_peripheral| was not found, it calls |error_callback|. The object
50 // will perform at most one call of the callbacks.
51 BluetoothLowEnergyCharacteristicsFinder(
52 scoped_refptr
<device::BluetoothAdapter
> adapter
,
53 device::BluetoothDevice
* device
,
54 const RemoteAttribute
& remote_service
,
55 const RemoteAttribute
& to_peripheral_char
,
56 const RemoteAttribute
& from_peripheral_char
,
57 const SuccessCallback
& success_callback
,
58 const ErrorCallback
& error_callback
);
60 ~BluetoothLowEnergyCharacteristicsFinder() override
;
63 // device::BluetoothAdapter::Observer:
64 void GattDiscoveryCompleteForService(
65 device::BluetoothAdapter
* adapter
,
66 device::BluetoothGattService
* service
) override
;
67 void GattCharacteristicAdded(
68 device::BluetoothAdapter
* adapter
,
69 device::BluetoothGattCharacteristic
* characteristic
) override
;
71 // For testing. Used to mock this class.
72 BluetoothLowEnergyCharacteristicsFinder();
75 // Handles the discovery of a new characteristic.
76 void HandleCharacteristicUpdate(
77 device::BluetoothGattCharacteristic
* characteristic
);
79 // Scans the remote chracteristics of the service with |uuid| in |device|
80 // calling HandleCharacteristicUpdate() for each of them.
81 void ScanRemoteCharacteristics(device::BluetoothDevice
* device
,
82 const device::BluetoothUUID
& uuid
);
84 // Updates the value of |to_peripheral_char_| and
85 // |from_peripheral_char_|
86 // when |characteristic| was found.
87 void UpdateCharacteristicsStatus(
88 device::BluetoothGattCharacteristic
* characteristic
);
90 // Resets |success_callback_| and |success_callback_|. This should be called
91 // whenever a callback is called to avoid multiple callbacks calls.
92 void ResetCallbacks();
94 // The Bluetooth adapter where the connection was established.
95 scoped_refptr
<device::BluetoothAdapter
> adapter_
;
97 // Remote service the |connection_| was established with.
98 RemoteAttribute remote_service_
;
100 // Characteristic used to receive data from the remote device.
101 RemoteAttribute to_peripheral_char_
;
103 // Characteristic used to receive data from the remote device.
104 RemoteAttribute from_peripheral_char_
;
106 // Called when all characteristics were found.
107 SuccessCallback success_callback_
;
109 // Called when there is an error.
110 ErrorCallback error_callback_
;
112 DISALLOW_COPY_AND_ASSIGN(BluetoothLowEnergyCharacteristicsFinder
);
115 } // namespace proximity_auth
117 #endif // COMPONENTS_PROXIMITY_AUTH_BLE_BLUETOOTH_CHARACTERISTICS_FINDER_H