| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/proximity_auth/ble/bluetooth_low_energy_characteristics_fin
der.h" | 5 #include "components/proximity_auth/ble/bluetooth_low_energy_characteristics_fin
der.h" |
| 6 | 6 |
| 7 #include "components/proximity_auth/logging/logging.h" | 7 #include "components/proximity_auth/logging/logging.h" |
| 8 #include "device/bluetooth/bluetooth_adapter.h" | 8 #include "device/bluetooth/bluetooth_adapter.h" |
| 9 #include "device/bluetooth/bluetooth_device.h" | 9 #include "device/bluetooth/bluetooth_device.h" |
| 10 #include "device/bluetooth/bluetooth_gatt_characteristic.h" | 10 #include "device/bluetooth/bluetooth_remote_gatt_characteristic.h" |
| 11 #include "device/bluetooth/bluetooth_uuid.h" | 11 #include "device/bluetooth/bluetooth_uuid.h" |
| 12 | 12 |
| 13 using device::BluetoothAdapter; | 13 using device::BluetoothAdapter; |
| 14 using device::BluetoothDevice; | 14 using device::BluetoothDevice; |
| 15 using device::BluetoothGattCharacteristic; | 15 using device::BluetoothRemoteGattCharacteristic; |
| 16 using device::BluetoothGattService; | 16 using device::BluetoothRemoteGattService; |
| 17 using device::BluetoothUUID; | 17 using device::BluetoothUUID; |
| 18 | 18 |
| 19 namespace proximity_auth { | 19 namespace proximity_auth { |
| 20 | 20 |
| 21 BluetoothLowEnergyCharacteristicsFinder:: | 21 BluetoothLowEnergyCharacteristicsFinder:: |
| 22 BluetoothLowEnergyCharacteristicsFinder( | 22 BluetoothLowEnergyCharacteristicsFinder( |
| 23 scoped_refptr<BluetoothAdapter> adapter, | 23 scoped_refptr<BluetoothAdapter> adapter, |
| 24 BluetoothDevice* device, | 24 BluetoothDevice* device, |
| 25 const RemoteAttribute& remote_service, | 25 const RemoteAttribute& remote_service, |
| 26 const RemoteAttribute& to_peripheral_char, | 26 const RemoteAttribute& to_peripheral_char, |
| (...skipping 26 matching lines...) Expand all Loading... |
| 53 ~BluetoothLowEnergyCharacteristicsFinder() { | 53 ~BluetoothLowEnergyCharacteristicsFinder() { |
| 54 ResetCallbacks(); | 54 ResetCallbacks(); |
| 55 if (adapter_) { | 55 if (adapter_) { |
| 56 adapter_->RemoveObserver(this); | 56 adapter_->RemoveObserver(this); |
| 57 adapter_ = NULL; | 57 adapter_ = NULL; |
| 58 } | 58 } |
| 59 } | 59 } |
| 60 | 60 |
| 61 void BluetoothLowEnergyCharacteristicsFinder::GattCharacteristicAdded( | 61 void BluetoothLowEnergyCharacteristicsFinder::GattCharacteristicAdded( |
| 62 BluetoothAdapter* adapter, | 62 BluetoothAdapter* adapter, |
| 63 BluetoothGattCharacteristic* characteristic) { | 63 BluetoothRemoteGattCharacteristic* characteristic) { |
| 64 PA_LOG(INFO) << "New char found: " | 64 PA_LOG(INFO) << "New char found: " |
| 65 << characteristic->GetUUID().canonical_value(); | 65 << characteristic->GetUUID().canonical_value(); |
| 66 HandleCharacteristicUpdate(characteristic); | 66 HandleCharacteristicUpdate(characteristic); |
| 67 } | 67 } |
| 68 | 68 |
| 69 void BluetoothLowEnergyCharacteristicsFinder::GattDiscoveryCompleteForService( | 69 void BluetoothLowEnergyCharacteristicsFinder::GattDiscoveryCompleteForService( |
| 70 BluetoothAdapter* adapter, | 70 BluetoothAdapter* adapter, |
| 71 BluetoothGattService* service) { | 71 BluetoothRemoteGattService* service) { |
| 72 if (service && service->GetUUID() == remote_service_.uuid) { | 72 if (service && service->GetUUID() == remote_service_.uuid) { |
| 73 PA_LOG(INFO) << "All characteristics discovered for " | 73 PA_LOG(INFO) << "All characteristics discovered for " |
| 74 << remote_service_.uuid.canonical_value(); | 74 << remote_service_.uuid.canonical_value(); |
| 75 | 75 |
| 76 if (to_peripheral_char_.id.empty() || from_peripheral_char_.id.empty()) { | 76 if (to_peripheral_char_.id.empty() || from_peripheral_char_.id.empty()) { |
| 77 if (!error_callback_.is_null()) { | 77 if (!error_callback_.is_null()) { |
| 78 error_callback_.Run(to_peripheral_char_, from_peripheral_char_); | 78 error_callback_.Run(to_peripheral_char_, from_peripheral_char_); |
| 79 ResetCallbacks(); | 79 ResetCallbacks(); |
| 80 } | 80 } |
| 81 } | 81 } |
| 82 } | 82 } |
| 83 } | 83 } |
| 84 | 84 |
| 85 void BluetoothLowEnergyCharacteristicsFinder::ScanRemoteCharacteristics( | 85 void BluetoothLowEnergyCharacteristicsFinder::ScanRemoteCharacteristics( |
| 86 BluetoothDevice* device, | 86 BluetoothDevice* device, |
| 87 const BluetoothUUID& service_uuid) { | 87 const BluetoothUUID& service_uuid) { |
| 88 PA_LOG(INFO) << "Scanning remote characteristics."; | 88 PA_LOG(INFO) << "Scanning remote characteristics."; |
| 89 if (device) { | 89 if (device) { |
| 90 std::vector<BluetoothGattService*> services = device->GetGattServices(); | 90 std::vector<BluetoothRemoteGattService*> services = |
| 91 device->GetGattServices(); |
| 91 PA_LOG(INFO) << device->GetAddress() << " has " << services.size() | 92 PA_LOG(INFO) << device->GetAddress() << " has " << services.size() |
| 92 << " services."; | 93 << " services."; |
| 93 for (const auto& service : services) { | 94 for (const auto& service : services) { |
| 94 if (service->GetUUID() == service_uuid) { | 95 if (service->GetUUID() == service_uuid) { |
| 95 // Right service found, now scaning its characteristics. | 96 // Right service found, now scaning its characteristics. |
| 96 std::vector<device::BluetoothGattCharacteristic*> characteristics = | 97 std::vector<device::BluetoothRemoteGattCharacteristic*> |
| 97 service->GetCharacteristics(); | 98 characteristics = service->GetCharacteristics(); |
| 98 PA_LOG(INFO) << "Service " << service_uuid.canonical_value() << " has " | 99 PA_LOG(INFO) << "Service " << service_uuid.canonical_value() << " has " |
| 99 << characteristics.size() << " characteristics."; | 100 << characteristics.size() << " characteristics."; |
| 100 for (const auto& characteristic : characteristics) { | 101 for (const auto& characteristic : characteristics) { |
| 101 HandleCharacteristicUpdate(characteristic); | 102 HandleCharacteristicUpdate(characteristic); |
| 102 } | 103 } |
| 103 break; | 104 break; |
| 104 } | 105 } |
| 105 } | 106 } |
| 106 } | 107 } |
| 107 } | 108 } |
| 108 | 109 |
| 109 void BluetoothLowEnergyCharacteristicsFinder::HandleCharacteristicUpdate( | 110 void BluetoothLowEnergyCharacteristicsFinder::HandleCharacteristicUpdate( |
| 110 BluetoothGattCharacteristic* characteristic) { | 111 BluetoothRemoteGattCharacteristic* characteristic) { |
| 111 UpdateCharacteristicsStatus(characteristic); | 112 UpdateCharacteristicsStatus(characteristic); |
| 112 | 113 |
| 113 if (!to_peripheral_char_.id.empty() && !from_peripheral_char_.id.empty() && | 114 if (!to_peripheral_char_.id.empty() && !from_peripheral_char_.id.empty() && |
| 114 !success_callback_.is_null()) { | 115 !success_callback_.is_null()) { |
| 115 PA_LOG(INFO) << "Found write and read characteristics on remote device."; | 116 PA_LOG(INFO) << "Found write and read characteristics on remote device."; |
| 116 success_callback_.Run(remote_service_, to_peripheral_char_, | 117 success_callback_.Run(remote_service_, to_peripheral_char_, |
| 117 from_peripheral_char_); | 118 from_peripheral_char_); |
| 118 ResetCallbacks(); | 119 ResetCallbacks(); |
| 119 } | 120 } |
| 120 } | 121 } |
| 121 | 122 |
| 122 void BluetoothLowEnergyCharacteristicsFinder::UpdateCharacteristicsStatus( | 123 void BluetoothLowEnergyCharacteristicsFinder::UpdateCharacteristicsStatus( |
| 123 BluetoothGattCharacteristic* characteristic) { | 124 BluetoothRemoteGattCharacteristic* characteristic) { |
| 124 if (characteristic && | 125 if (characteristic && |
| 125 characteristic->GetService()->GetUUID() == remote_service_.uuid) { | 126 characteristic->GetService()->GetUUID() == remote_service_.uuid) { |
| 126 BluetoothUUID uuid = characteristic->GetUUID(); | 127 BluetoothUUID uuid = characteristic->GetUUID(); |
| 127 if (to_peripheral_char_.uuid == uuid) | 128 if (to_peripheral_char_.uuid == uuid) |
| 128 to_peripheral_char_.id = characteristic->GetIdentifier(); | 129 to_peripheral_char_.id = characteristic->GetIdentifier(); |
| 129 if (from_peripheral_char_.uuid == uuid) | 130 if (from_peripheral_char_.uuid == uuid) |
| 130 from_peripheral_char_.id = characteristic->GetIdentifier(); | 131 from_peripheral_char_.id = characteristic->GetIdentifier(); |
| 131 | 132 |
| 132 BluetoothGattService* service = characteristic->GetService(); | 133 BluetoothRemoteGattService* service = characteristic->GetService(); |
| 133 remote_service_.id = service->GetIdentifier(); | 134 remote_service_.id = service->GetIdentifier(); |
| 134 } | 135 } |
| 135 } | 136 } |
| 136 | 137 |
| 137 void BluetoothLowEnergyCharacteristicsFinder::ResetCallbacks() { | 138 void BluetoothLowEnergyCharacteristicsFinder::ResetCallbacks() { |
| 138 success_callback_.Reset(); | 139 success_callback_.Reset(); |
| 139 error_callback_.Reset(); | 140 error_callback_.Reset(); |
| 140 } | 141 } |
| 141 | 142 |
| 142 } // namespace proximity_auth | 143 } // namespace proximity_auth |
| OLD | NEW |