| 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 #include "components/proximity_auth/ble/bluetooth_low_energy_characteristics_fin
der.h" | |
| 6 | |
| 7 #include "components/proximity_auth/logging/logging.h" | |
| 8 #include "device/bluetooth/bluetooth_adapter.h" | |
| 9 #include "device/bluetooth/bluetooth_device.h" | |
| 10 #include "device/bluetooth/bluetooth_remote_gatt_characteristic.h" | |
| 11 #include "device/bluetooth/bluetooth_uuid.h" | |
| 12 | |
| 13 using device::BluetoothAdapter; | |
| 14 using device::BluetoothDevice; | |
| 15 using device::BluetoothRemoteGattCharacteristic; | |
| 16 using device::BluetoothRemoteGattService; | |
| 17 using device::BluetoothUUID; | |
| 18 | |
| 19 namespace proximity_auth { | |
| 20 | |
| 21 BluetoothLowEnergyCharacteristicsFinder:: | |
| 22 BluetoothLowEnergyCharacteristicsFinder( | |
| 23 scoped_refptr<BluetoothAdapter> adapter, | |
| 24 BluetoothDevice* device, | |
| 25 const RemoteAttribute& remote_service, | |
| 26 const RemoteAttribute& to_peripheral_char, | |
| 27 const RemoteAttribute& from_peripheral_char, | |
| 28 const SuccessCallback& success_callback, | |
| 29 const ErrorCallback& error_callback) | |
| 30 : adapter_(adapter), | |
| 31 remote_service_(remote_service), | |
| 32 to_peripheral_char_(to_peripheral_char), | |
| 33 from_peripheral_char_(from_peripheral_char), | |
| 34 success_callback_(success_callback), | |
| 35 error_callback_(error_callback) { | |
| 36 if (!adapter_) { | |
| 37 error_callback_.Run(to_peripheral_char_, from_peripheral_char_); | |
| 38 ResetCallbacks(); | |
| 39 return; | |
| 40 } | |
| 41 | |
| 42 adapter_->AddObserver(this); | |
| 43 ScanRemoteCharacteristics(device, remote_service_.uuid); | |
| 44 | |
| 45 // TODO(sacomoto): implement a timeout for characteristic discovery. | |
| 46 } | |
| 47 | |
| 48 BluetoothLowEnergyCharacteristicsFinder:: | |
| 49 BluetoothLowEnergyCharacteristicsFinder() { | |
| 50 } | |
| 51 | |
| 52 BluetoothLowEnergyCharacteristicsFinder:: | |
| 53 ~BluetoothLowEnergyCharacteristicsFinder() { | |
| 54 ResetCallbacks(); | |
| 55 if (adapter_) { | |
| 56 adapter_->RemoveObserver(this); | |
| 57 adapter_ = NULL; | |
| 58 } | |
| 59 } | |
| 60 | |
| 61 void BluetoothLowEnergyCharacteristicsFinder::GattCharacteristicAdded( | |
| 62 BluetoothAdapter* adapter, | |
| 63 BluetoothRemoteGattCharacteristic* characteristic) { | |
| 64 PA_LOG(INFO) << "New char found: " | |
| 65 << characteristic->GetUUID().canonical_value(); | |
| 66 HandleCharacteristicUpdate(characteristic); | |
| 67 } | |
| 68 | |
| 69 void BluetoothLowEnergyCharacteristicsFinder::GattDiscoveryCompleteForService( | |
| 70 BluetoothAdapter* adapter, | |
| 71 BluetoothRemoteGattService* service) { | |
| 72 if (service && service->GetUUID() == remote_service_.uuid) { | |
| 73 PA_LOG(INFO) << "All characteristics discovered for " | |
| 74 << remote_service_.uuid.canonical_value(); | |
| 75 | |
| 76 if (to_peripheral_char_.id.empty() || from_peripheral_char_.id.empty()) { | |
| 77 if (!error_callback_.is_null()) { | |
| 78 error_callback_.Run(to_peripheral_char_, from_peripheral_char_); | |
| 79 ResetCallbacks(); | |
| 80 } | |
| 81 } | |
| 82 } | |
| 83 } | |
| 84 | |
| 85 void BluetoothLowEnergyCharacteristicsFinder::ScanRemoteCharacteristics( | |
| 86 BluetoothDevice* device, | |
| 87 const BluetoothUUID& service_uuid) { | |
| 88 PA_LOG(INFO) << "Scanning remote characteristics."; | |
| 89 if (device) { | |
| 90 std::vector<BluetoothRemoteGattService*> services = | |
| 91 device->GetGattServices(); | |
| 92 PA_LOG(INFO) << device->GetAddress() << " has " << services.size() | |
| 93 << " services."; | |
| 94 for (const auto* service : services) { | |
| 95 if (service->GetUUID() == service_uuid) { | |
| 96 // Right service found, now scaning its characteristics. | |
| 97 std::vector<device::BluetoothRemoteGattCharacteristic*> | |
| 98 characteristics = service->GetCharacteristics(); | |
| 99 PA_LOG(INFO) << "Service " << service_uuid.canonical_value() << " has " | |
| 100 << characteristics.size() << " characteristics."; | |
| 101 for (auto* characteristic : characteristics) { | |
| 102 HandleCharacteristicUpdate(characteristic); | |
| 103 } | |
| 104 break; | |
| 105 } | |
| 106 } | |
| 107 } | |
| 108 } | |
| 109 | |
| 110 void BluetoothLowEnergyCharacteristicsFinder::HandleCharacteristicUpdate( | |
| 111 BluetoothRemoteGattCharacteristic* characteristic) { | |
| 112 UpdateCharacteristicsStatus(characteristic); | |
| 113 | |
| 114 if (!to_peripheral_char_.id.empty() && !from_peripheral_char_.id.empty() && | |
| 115 !success_callback_.is_null()) { | |
| 116 PA_LOG(INFO) << "Found write and read characteristics on remote device."; | |
| 117 success_callback_.Run(remote_service_, to_peripheral_char_, | |
| 118 from_peripheral_char_); | |
| 119 ResetCallbacks(); | |
| 120 } | |
| 121 } | |
| 122 | |
| 123 void BluetoothLowEnergyCharacteristicsFinder::UpdateCharacteristicsStatus( | |
| 124 BluetoothRemoteGattCharacteristic* characteristic) { | |
| 125 if (characteristic && | |
| 126 characteristic->GetService()->GetUUID() == remote_service_.uuid) { | |
| 127 BluetoothUUID uuid = characteristic->GetUUID(); | |
| 128 if (to_peripheral_char_.uuid == uuid) | |
| 129 to_peripheral_char_.id = characteristic->GetIdentifier(); | |
| 130 if (from_peripheral_char_.uuid == uuid) | |
| 131 from_peripheral_char_.id = characteristic->GetIdentifier(); | |
| 132 | |
| 133 BluetoothRemoteGattService* service = characteristic->GetService(); | |
| 134 remote_service_.id = service->GetIdentifier(); | |
| 135 } | |
| 136 } | |
| 137 | |
| 138 void BluetoothLowEnergyCharacteristicsFinder::ResetCallbacks() { | |
| 139 success_callback_.Reset(); | |
| 140 error_callback_.Reset(); | |
| 141 } | |
| 142 | |
| 143 } // namespace proximity_auth | |
| OLD | NEW |