| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 <utility> | 5 #include <utility> |
| 6 #include <vector> | 6 #include <vector> |
| 7 | 7 |
| 8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
| 9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "device/bluetooth/device.h" | 10 #include "device/bluetooth/device.h" |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 // safe. | 93 // safe. |
| 94 pending_services_requests_.push_back( | 94 pending_services_requests_.push_back( |
| 95 base::Bind(&Device::GetServicesImpl, base::Unretained(this), callback)); | 95 base::Bind(&Device::GetServicesImpl, base::Unretained(this), callback)); |
| 96 } | 96 } |
| 97 | 97 |
| 98 void Device::GetCharacteristics(const std::string& service_id, | 98 void Device::GetCharacteristics(const std::string& service_id, |
| 99 const GetCharacteristicsCallback& callback) { | 99 const GetCharacteristicsCallback& callback) { |
| 100 device::BluetoothDevice* device = adapter_->GetDevice(GetAddress()); | 100 device::BluetoothDevice* device = adapter_->GetDevice(GetAddress()); |
| 101 DCHECK(device); | 101 DCHECK(device); |
| 102 | 102 |
| 103 if (device->IsGattServicesDiscoveryComplete()) { | 103 device::BluetoothRemoteGattService* service = |
| 104 GetCharacteristicsImpl(service_id, callback); | 104 device->GetGattService(service_id); |
| 105 if (service == nullptr) { |
| 106 callback.Run(base::nullopt); |
| 105 return; | 107 return; |
| 106 } | 108 } |
| 107 | 109 |
| 108 // pending_services_requests_ is owned by Device, so base::Unretained is | 110 std::vector<mojom::CharacteristicInfoPtr> characteristics; |
| 109 // safe. | 111 |
| 110 pending_services_requests_.push_back( | 112 for (const auto* characteristic : service->GetCharacteristics()) { |
| 111 base::Bind(&Device::GetCharacteristicsImpl, base::Unretained(this), | 113 mojom::CharacteristicInfoPtr characteristic_info = |
| 112 service_id, callback)); | 114 mojom::CharacteristicInfo::New(); |
| 115 |
| 116 characteristic_info->id = characteristic->GetIdentifier(); |
| 117 characteristic_info->uuid = characteristic->GetUUID(); |
| 118 characteristic_info->properties = characteristic->GetProperties(); |
| 119 |
| 120 characteristics.push_back(std::move(characteristic_info)); |
| 121 } |
| 122 |
| 123 callback.Run(std::move(characteristics)); |
| 113 } | 124 } |
| 114 | 125 |
| 115 Device::Device(scoped_refptr<device::BluetoothAdapter> adapter, | 126 Device::Device(scoped_refptr<device::BluetoothAdapter> adapter, |
| 116 std::unique_ptr<device::BluetoothGattConnection> connection) | 127 std::unique_ptr<device::BluetoothGattConnection> connection) |
| 117 : adapter_(std::move(adapter)), connection_(std::move(connection)) { | 128 : adapter_(std::move(adapter)), connection_(std::move(connection)) { |
| 118 adapter_->AddObserver(this); | 129 adapter_->AddObserver(this); |
| 119 } | 130 } |
| 120 | 131 |
| 121 void Device::GetServicesImpl(const GetServicesCallback& callback) { | 132 void Device::GetServicesImpl(const GetServicesCallback& callback) { |
| 122 device::BluetoothDevice* device = adapter_->GetDevice(GetAddress()); | 133 device::BluetoothDevice* device = adapter_->GetDevice(GetAddress()); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 136 const device::BluetoothRemoteGattService& service) { | 147 const device::BluetoothRemoteGattService& service) { |
| 137 mojom::ServiceInfoPtr service_info = mojom::ServiceInfo::New(); | 148 mojom::ServiceInfoPtr service_info = mojom::ServiceInfo::New(); |
| 138 | 149 |
| 139 service_info->id = service.GetIdentifier(); | 150 service_info->id = service.GetIdentifier(); |
| 140 service_info->uuid = service.GetUUID(); | 151 service_info->uuid = service.GetUUID(); |
| 141 service_info->is_primary = service.IsPrimary(); | 152 service_info->is_primary = service.IsPrimary(); |
| 142 | 153 |
| 143 return service_info; | 154 return service_info; |
| 144 } | 155 } |
| 145 | 156 |
| 146 void Device::GetCharacteristicsImpl( | |
| 147 const std::string& service_id, | |
| 148 const GetCharacteristicsCallback& callback) { | |
| 149 device::BluetoothDevice* device = adapter_->GetDevice(GetAddress()); | |
| 150 DCHECK(device); | |
| 151 device::BluetoothRemoteGattService* service = | |
| 152 device->GetGattService(service_id); | |
| 153 DCHECK(service); | |
| 154 | |
| 155 std::vector<mojom::CharacteristicInfoPtr> characteristics; | |
| 156 | |
| 157 for (const auto* characteristic : service->GetCharacteristics()) { | |
| 158 mojom::CharacteristicInfoPtr characteristic_info = | |
| 159 mojom::CharacteristicInfo::New(); | |
| 160 | |
| 161 characteristic_info->id = characteristic->GetIdentifier(); | |
| 162 characteristic_info->uuid = characteristic->GetUUID(); | |
| 163 characteristic_info->properties = characteristic->GetProperties(); | |
| 164 | |
| 165 characteristics.push_back(std::move(characteristic_info)); | |
| 166 } | |
| 167 | |
| 168 callback.Run(std::move(characteristics)); | |
| 169 } | |
| 170 | |
| 171 const std::string& Device::GetAddress() { | 157 const std::string& Device::GetAddress() { |
| 172 return connection_->GetDeviceAddress(); | 158 return connection_->GetDeviceAddress(); |
| 173 } | 159 } |
| 174 | 160 |
| 175 } // namespace bluetooth | 161 } // namespace bluetooth |
| OLD | NEW |