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