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" |
| 11 #include "device/bluetooth/public/interfaces/gatt_result_type_converter.h" |
11 #include "mojo/public/cpp/bindings/strong_binding.h" | 12 #include "mojo/public/cpp/bindings/strong_binding.h" |
12 | 13 |
13 namespace bluetooth { | 14 namespace bluetooth { |
14 Device::~Device() { | 15 Device::~Device() { |
15 adapter_->RemoveObserver(this); | 16 adapter_->RemoveObserver(this); |
16 } | 17 } |
17 | 18 |
18 // static | 19 // static |
19 void Device::Create(scoped_refptr<device::BluetoothAdapter> adapter, | 20 void Device::Create(scoped_refptr<device::BluetoothAdapter> adapter, |
20 std::unique_ptr<device::BluetoothGattConnection> connection, | 21 std::unique_ptr<device::BluetoothGattConnection> connection, |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 characteristic_info->id = characteristic->GetIdentifier(); | 117 characteristic_info->id = characteristic->GetIdentifier(); |
117 characteristic_info->uuid = characteristic->GetUUID(); | 118 characteristic_info->uuid = characteristic->GetUUID(); |
118 characteristic_info->properties = characteristic->GetProperties(); | 119 characteristic_info->properties = characteristic->GetProperties(); |
119 | 120 |
120 characteristics.push_back(std::move(characteristic_info)); | 121 characteristics.push_back(std::move(characteristic_info)); |
121 } | 122 } |
122 | 123 |
123 callback.Run(std::move(characteristics)); | 124 callback.Run(std::move(characteristics)); |
124 } | 125 } |
125 | 126 |
| 127 void Device::ReadValueForCharacteristic( |
| 128 const std::string& service_id, |
| 129 const std::string& characteristic_id, |
| 130 const ReadValueForCharacteristicCallback& callback) { |
| 131 device::BluetoothDevice* device = adapter_->GetDevice(GetAddress()); |
| 132 DCHECK(device); |
| 133 |
| 134 device::BluetoothRemoteGattService* service = |
| 135 device->GetGattService(service_id); |
| 136 if (service == nullptr) { |
| 137 callback.Run(mojom::GattResult::SERVICE_NOT_FOUND, |
| 138 base::nullopt /* value */); |
| 139 return; |
| 140 } |
| 141 |
| 142 device::BluetoothRemoteGattCharacteristic* characteristic = |
| 143 service->GetCharacteristic(characteristic_id); |
| 144 if (characteristic == nullptr) { |
| 145 callback.Run(mojom::GattResult::CHARACTERISTIC_NOT_FOUND, |
| 146 base::nullopt /* value */); |
| 147 return; |
| 148 } |
| 149 |
| 150 characteristic->ReadRemoteCharacteristic( |
| 151 base::Bind(&Device::OnReadRemoteCharacteristic, |
| 152 weak_ptr_factory_.GetWeakPtr(), callback), |
| 153 base::Bind(&Device::OnReadRemoteCharacteristicError, |
| 154 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 155 } |
| 156 |
| 157 void Device::WriteValueForCharacteristic( |
| 158 const std::string& service_id, |
| 159 const std::string& characteristic_id, |
| 160 const std::vector<uint8_t>& value, |
| 161 const WriteValueForCharacteristicCallback& callback) { |
| 162 device::BluetoothDevice* device = adapter_->GetDevice(GetAddress()); |
| 163 DCHECK(device); |
| 164 |
| 165 device::BluetoothRemoteGattService* service = |
| 166 device->GetGattService(service_id); |
| 167 if (service == nullptr) { |
| 168 callback.Run(mojom::GattResult::SERVICE_NOT_FOUND); |
| 169 return; |
| 170 } |
| 171 |
| 172 device::BluetoothRemoteGattCharacteristic* characteristic = |
| 173 service->GetCharacteristic(characteristic_id); |
| 174 if (characteristic == nullptr) { |
| 175 callback.Run(mojom::GattResult::CHARACTERISTIC_NOT_FOUND); |
| 176 return; |
| 177 } |
| 178 |
| 179 characteristic->WriteRemoteCharacteristic( |
| 180 value, base::Bind(&Device::OnWriteRemoteCharacteristic, |
| 181 weak_ptr_factory_.GetWeakPtr(), callback), |
| 182 base::Bind(&Device::OnWriteRemoteCharacteristicError, |
| 183 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 184 } |
| 185 |
| 186 void Device::GetDescriptors(const std::string& service_id, |
| 187 const std::string& characteristic_id, |
| 188 const GetDescriptorsCallback& callback) { |
| 189 device::BluetoothDevice* device = adapter_->GetDevice(GetAddress()); |
| 190 if (!device) { |
| 191 callback.Run(base::nullopt); |
| 192 return; |
| 193 } |
| 194 |
| 195 device::BluetoothRemoteGattService* service = |
| 196 device->GetGattService(service_id); |
| 197 if (!service) { |
| 198 callback.Run(base::nullopt); |
| 199 return; |
| 200 } |
| 201 |
| 202 device::BluetoothRemoteGattCharacteristic* characteristic = |
| 203 service->GetCharacteristic(characteristic_id); |
| 204 if (!characteristic) { |
| 205 callback.Run(base::nullopt); |
| 206 return; |
| 207 } |
| 208 |
| 209 std::vector<mojom::DescriptorInfoPtr> descriptors; |
| 210 |
| 211 for (const auto* descriptor : characteristic->GetDescriptors()) { |
| 212 mojom::DescriptorInfoPtr descriptor_info = mojom::DescriptorInfo::New(); |
| 213 |
| 214 descriptor_info->id = descriptor->GetIdentifier(); |
| 215 descriptor_info->uuid = descriptor->GetUUID(); |
| 216 descriptors.push_back(std::move(descriptor_info)); |
| 217 } |
| 218 |
| 219 callback.Run(std::move(descriptors)); |
| 220 } |
| 221 |
126 Device::Device(scoped_refptr<device::BluetoothAdapter> adapter, | 222 Device::Device(scoped_refptr<device::BluetoothAdapter> adapter, |
127 std::unique_ptr<device::BluetoothGattConnection> connection) | 223 std::unique_ptr<device::BluetoothGattConnection> connection) |
128 : adapter_(std::move(adapter)), connection_(std::move(connection)) { | 224 : adapter_(std::move(adapter)), |
| 225 connection_(std::move(connection)), |
| 226 weak_ptr_factory_(this) { |
129 adapter_->AddObserver(this); | 227 adapter_->AddObserver(this); |
130 } | 228 } |
131 | 229 |
132 void Device::GetServicesImpl(const GetServicesCallback& callback) { | 230 void Device::GetServicesImpl(const GetServicesCallback& callback) { |
133 device::BluetoothDevice* device = adapter_->GetDevice(GetAddress()); | 231 device::BluetoothDevice* device = adapter_->GetDevice(GetAddress()); |
134 DCHECK(device); | 232 DCHECK(device); |
135 | 233 |
136 std::vector<mojom::ServiceInfoPtr> services; | 234 std::vector<mojom::ServiceInfoPtr> services; |
137 | 235 |
138 for (const device::BluetoothRemoteGattService* service : | 236 for (const device::BluetoothRemoteGattService* service : |
139 device->GetGattServices()) { | 237 device->GetGattServices()) { |
140 services.push_back(ConstructServiceInfoStruct(*service)); | 238 services.push_back(ConstructServiceInfoStruct(*service)); |
141 } | 239 } |
142 | 240 |
143 callback.Run(std::move(services)); | 241 callback.Run(std::move(services)); |
144 } | 242 } |
145 | 243 |
146 mojom::ServiceInfoPtr Device::ConstructServiceInfoStruct( | 244 mojom::ServiceInfoPtr Device::ConstructServiceInfoStruct( |
147 const device::BluetoothRemoteGattService& service) { | 245 const device::BluetoothRemoteGattService& service) { |
148 mojom::ServiceInfoPtr service_info = mojom::ServiceInfo::New(); | 246 mojom::ServiceInfoPtr service_info = mojom::ServiceInfo::New(); |
149 | 247 |
150 service_info->id = service.GetIdentifier(); | 248 service_info->id = service.GetIdentifier(); |
151 service_info->uuid = service.GetUUID(); | 249 service_info->uuid = service.GetUUID(); |
152 service_info->is_primary = service.IsPrimary(); | 250 service_info->is_primary = service.IsPrimary(); |
153 | 251 |
154 return service_info; | 252 return service_info; |
155 } | 253 } |
156 | 254 |
| 255 void Device::OnReadRemoteCharacteristic( |
| 256 const ReadValueForCharacteristicCallback& callback, |
| 257 const std::vector<uint8_t>& value) { |
| 258 callback.Run(mojom::GattResult::SUCCESS, std::move(value)); |
| 259 } |
| 260 |
| 261 void Device::OnReadRemoteCharacteristicError( |
| 262 const ReadValueForCharacteristicCallback& callback, |
| 263 device::BluetoothGattService::GattErrorCode error_code) { |
| 264 callback.Run(mojo::ConvertTo<mojom::GattResult>(error_code), |
| 265 base::nullopt /* value */); |
| 266 } |
| 267 |
| 268 void Device::OnWriteRemoteCharacteristic( |
| 269 const WriteValueForCharacteristicCallback& callback) { |
| 270 callback.Run(mojom::GattResult::SUCCESS); |
| 271 } |
| 272 |
| 273 void Device::OnWriteRemoteCharacteristicError( |
| 274 const WriteValueForCharacteristicCallback& callback, |
| 275 device::BluetoothGattService::GattErrorCode error_code) { |
| 276 callback.Run(mojo::ConvertTo<mojom::GattResult>(error_code)); |
| 277 } |
| 278 |
157 const std::string& Device::GetAddress() { | 279 const std::string& Device::GetAddress() { |
158 return connection_->GetDeviceAddress(); | 280 return connection_->GetDeviceAddress(); |
159 } | 281 } |
160 | 282 |
161 } // namespace bluetooth | 283 } // namespace bluetooth |
OLD | NEW |