OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "device/bluetooth/bluetooth_device_win.h" | 5 #include "device/bluetooth/bluetooth_device_win.h" |
6 | 6 |
7 #include <memory> | |
8 #include <string> | 7 #include <string> |
9 | 8 |
10 #include "base/logging.h" | 9 #include "base/logging.h" |
11 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
12 #include "base/memory/scoped_vector.h" | |
13 #include "base/sequenced_task_runner.h" | 11 #include "base/sequenced_task_runner.h" |
14 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
15 #include "device/bluetooth/bluetooth_adapter_win.h" | 13 #include "device/bluetooth/bluetooth_adapter_win.h" |
16 #include "device/bluetooth/bluetooth_remote_gatt_service_win.h" | 14 #include "device/bluetooth/bluetooth_remote_gatt_service_win.h" |
17 #include "device/bluetooth/bluetooth_service_record_win.h" | 15 #include "device/bluetooth/bluetooth_service_record_win.h" |
18 #include "device/bluetooth/bluetooth_socket_thread.h" | 16 #include "device/bluetooth/bluetooth_socket_thread.h" |
19 #include "device/bluetooth/bluetooth_socket_win.h" | 17 #include "device/bluetooth/bluetooth_socket_win.h" |
20 #include "device/bluetooth/bluetooth_task_manager_win.h" | 18 #include "device/bluetooth/bluetooth_task_manager_win.h" |
21 #include "device/bluetooth/bluetooth_uuid.h" | 19 #include "device/bluetooth/bluetooth_uuid.h" |
22 | 20 |
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
208 | 206 |
209 void BluetoothDeviceWin::CreateGattConnection( | 207 void BluetoothDeviceWin::CreateGattConnection( |
210 const GattConnectionCallback& callback, | 208 const GattConnectionCallback& callback, |
211 const ConnectErrorCallback& error_callback) { | 209 const ConnectErrorCallback& error_callback) { |
212 // TODO(armansito): Implement. | 210 // TODO(armansito): Implement. |
213 error_callback.Run(ERROR_UNSUPPORTED_DEVICE); | 211 error_callback.Run(ERROR_UNSUPPORTED_DEVICE); |
214 } | 212 } |
215 | 213 |
216 const BluetoothServiceRecordWin* BluetoothDeviceWin::GetServiceRecord( | 214 const BluetoothServiceRecordWin* BluetoothDeviceWin::GetServiceRecord( |
217 const device::BluetoothUUID& uuid) const { | 215 const device::BluetoothUUID& uuid) const { |
218 for (auto iter = service_record_list_.begin(); | 216 for (const auto& record : service_record_list_) |
219 iter != service_record_list_.end(); ++iter) { | 217 if (record->uuid() == uuid) |
220 if ((*iter)->uuid() == uuid) | 218 return record.get(); |
221 return *iter; | 219 |
222 } | 220 return nullptr; |
223 return NULL; | |
224 } | 221 } |
225 | 222 |
226 bool BluetoothDeviceWin::IsEqual( | 223 bool BluetoothDeviceWin::IsEqual( |
227 const BluetoothTaskManagerWin::DeviceState& device_state) { | 224 const BluetoothTaskManagerWin::DeviceState& device_state) { |
228 if (address_ != device_state.address || name_ != device_state.name || | 225 if (address_ != device_state.address || name_ != device_state.name || |
229 bluetooth_class_ != device_state.bluetooth_class || | 226 bluetooth_class_ != device_state.bluetooth_class || |
230 visible_ != device_state.visible || | 227 visible_ != device_state.visible || |
231 connected_ != device_state.connected || | 228 connected_ != device_state.connected || |
232 paired_ != device_state.authenticated) { | 229 paired_ != device_state.authenticated) { |
233 return false; | 230 return false; |
(...skipping 10 matching lines...) Expand all Loading... |
244 new_services.insert(service_record->uuid()); | 241 new_services.insert(service_record->uuid()); |
245 new_service_records[service_record->uuid().canonical_value()] = | 242 new_service_records[service_record->uuid().canonical_value()] = |
246 std::move(service_record); | 243 std::move(service_record); |
247 } | 244 } |
248 | 245 |
249 // Check that no new services have been added or removed. | 246 // Check that no new services have been added or removed. |
250 if (uuids_ != new_services) { | 247 if (uuids_ != new_services) { |
251 return false; | 248 return false; |
252 } | 249 } |
253 | 250 |
254 for (auto iter = service_record_list_.begin(); | 251 for (const auto& service_record : service_record_list_) { |
255 iter != service_record_list_.end(); ++iter) { | |
256 BluetoothServiceRecordWin* service_record = (*iter); | |
257 BluetoothServiceRecordWin* new_service_record = | 252 BluetoothServiceRecordWin* new_service_record = |
258 new_service_records[(*iter)->uuid().canonical_value()].get(); | 253 new_service_records[service_record->uuid().canonical_value()].get(); |
259 if (!service_record->IsEqual(*new_service_record)) | 254 if (!service_record->IsEqual(*new_service_record)) |
260 return false; | 255 return false; |
261 } | 256 } |
262 return true; | 257 return true; |
263 } | 258 } |
264 | 259 |
265 void BluetoothDeviceWin::Update( | 260 void BluetoothDeviceWin::Update( |
266 const BluetoothTaskManagerWin::DeviceState& device_state) { | 261 const BluetoothTaskManagerWin::DeviceState& device_state) { |
267 address_ = device_state.address; | 262 address_ = device_state.address; |
268 // Note: Callers are responsible for providing a canonicalized address. | 263 // Note: Callers are responsible for providing a canonicalized address. |
(...skipping 20 matching lines...) Expand all Loading... |
289 | 284 |
290 void BluetoothDeviceWin::SetVisible(bool visible) { | 285 void BluetoothDeviceWin::SetVisible(bool visible) { |
291 visible_ = visible; | 286 visible_ = visible; |
292 } | 287 } |
293 | 288 |
294 void BluetoothDeviceWin::UpdateServices( | 289 void BluetoothDeviceWin::UpdateServices( |
295 const BluetoothTaskManagerWin::DeviceState& device_state) { | 290 const BluetoothTaskManagerWin::DeviceState& device_state) { |
296 uuids_.clear(); | 291 uuids_.clear(); |
297 service_record_list_.clear(); | 292 service_record_list_.clear(); |
298 | 293 |
299 for (auto iter = device_state.service_record_states.begin(); | 294 for (const auto& record_state : device_state.service_record_states) { |
300 iter != device_state.service_record_states.end(); ++iter) { | 295 auto service_record = base::MakeUnique<BluetoothServiceRecordWin>( |
301 BluetoothServiceRecordWin* service_record = | 296 device_state.address, record_state->name, record_state->sdp_bytes, |
302 new BluetoothServiceRecordWin(device_state.address, (*iter)->name, | 297 record_state->gatt_uuid); |
303 (*iter)->sdp_bytes, (*iter)->gatt_uuid); | |
304 service_record_list_.push_back(service_record); | |
305 uuids_.insert(service_record->uuid()); | 298 uuids_.insert(service_record->uuid()); |
| 299 service_record_list_.push_back(std::move(service_record)); |
306 } | 300 } |
307 | 301 |
308 if (!device_state.is_bluetooth_classic()) | 302 if (!device_state.is_bluetooth_classic()) |
309 UpdateGattServices(device_state.service_record_states); | 303 UpdateGattServices(device_state.service_record_states); |
310 } | 304 } |
311 | 305 |
312 bool BluetoothDeviceWin::IsGattServiceDiscovered(BluetoothUUID& uuid, | 306 bool BluetoothDeviceWin::IsGattServiceDiscovered(BluetoothUUID& uuid, |
313 uint16_t attribute_handle) { | 307 uint16_t attribute_handle) { |
314 for (const auto& gatt_service : gatt_services_) { | 308 for (const auto& gatt_service : gatt_services_) { |
315 uint16_t it_att_handle = | 309 uint16_t it_att_handle = |
316 static_cast<BluetoothRemoteGattServiceWin*>(gatt_service.second.get()) | 310 static_cast<BluetoothRemoteGattServiceWin*>(gatt_service.second.get()) |
317 ->GetAttributeHandle(); | 311 ->GetAttributeHandle(); |
318 BluetoothUUID it_uuid = gatt_service.second->GetUUID(); | 312 BluetoothUUID it_uuid = gatt_service.second->GetUUID(); |
319 if (attribute_handle == it_att_handle && uuid == it_uuid) { | 313 if (attribute_handle == it_att_handle && uuid == it_uuid) { |
320 return true; | 314 return true; |
321 } | 315 } |
322 } | 316 } |
323 return false; | 317 return false; |
324 } | 318 } |
325 | 319 |
326 bool BluetoothDeviceWin::DoesGattServiceExist( | 320 bool BluetoothDeviceWin::DoesGattServiceExist( |
327 const ScopedVector<BluetoothTaskManagerWin::ServiceRecordState>& | 321 const std::vector<std::unique_ptr< |
328 service_state, | 322 BluetoothTaskManagerWin::ServiceRecordState>>& service_state, |
329 BluetoothRemoteGattService* service) { | 323 BluetoothRemoteGattService* service) { |
330 uint16_t attribute_handle = | 324 uint16_t attribute_handle = |
331 static_cast<BluetoothRemoteGattServiceWin*>(service) | 325 static_cast<BluetoothRemoteGattServiceWin*>(service) |
332 ->GetAttributeHandle(); | 326 ->GetAttributeHandle(); |
333 BluetoothUUID uuid = service->GetUUID(); | 327 BluetoothUUID uuid = service->GetUUID(); |
334 auto it = service_state.begin(); | 328 for (const auto& record_state : service_state) { |
335 for (; it != service_state.end(); ++it) { | 329 if (attribute_handle == record_state->attribute_handle && |
336 if (attribute_handle == (*it)->attribute_handle && uuid == (*it)->gatt_uuid) | 330 uuid == record_state->gatt_uuid) { |
337 return true; | 331 return true; |
| 332 } |
338 } | 333 } |
339 return false; | 334 return false; |
340 } | 335 } |
341 | 336 |
342 void BluetoothDeviceWin::UpdateGattServices( | 337 void BluetoothDeviceWin::UpdateGattServices( |
343 const ScopedVector<BluetoothTaskManagerWin::ServiceRecordState>& | 338 const std::vector< |
| 339 std::unique_ptr<BluetoothTaskManagerWin::ServiceRecordState>>& |
344 service_state) { | 340 service_state) { |
345 // First, remove no longer exist GATT service. | 341 // First, remove no longer exist GATT service. |
346 { | 342 { |
347 std::vector<std::string> to_be_removed_services; | 343 std::vector<std::string> to_be_removed_services; |
348 for (const auto& gatt_service : gatt_services_) { | 344 for (const auto& gatt_service : gatt_services_) { |
349 if (!DoesGattServiceExist(service_state, gatt_service.second.get())) { | 345 if (!DoesGattServiceExist(service_state, gatt_service.second.get())) { |
350 to_be_removed_services.push_back(gatt_service.first); | 346 to_be_removed_services.push_back(gatt_service.first); |
351 } | 347 } |
352 } | 348 } |
353 for (const auto& service : to_be_removed_services) { | 349 for (const auto& service : to_be_removed_services) { |
354 std::unique_ptr<BluetoothRemoteGattService> service_ptr = | 350 std::unique_ptr<BluetoothRemoteGattService> service_ptr = |
355 std::move(gatt_services_[service]); | 351 std::move(gatt_services_[service]); |
356 gatt_services_.erase(service); | 352 gatt_services_.erase(service); |
357 } | 353 } |
358 // Update previously discovered services. | 354 // Update previously discovered services. |
359 for (const auto& gatt_service : gatt_services_) { | 355 for (const auto& gatt_service : gatt_services_) { |
360 static_cast<BluetoothRemoteGattServiceWin*>(gatt_service.second.get()) | 356 static_cast<BluetoothRemoteGattServiceWin*>(gatt_service.second.get()) |
361 ->Update(); | 357 ->Update(); |
362 } | 358 } |
363 } | 359 } |
364 | 360 |
365 // Return if no new services have been added. | 361 // Return if no new services have been added. |
366 if (gatt_services_.size() == service_state.size()) | 362 if (gatt_services_.size() == service_state.size()) |
367 return; | 363 return; |
368 | 364 |
369 // Add new services. | 365 // Add new services. |
370 for (auto it = service_state.begin(); it != service_state.end(); ++it) { | 366 for (const auto& record_state : service_state) { |
371 if (!IsGattServiceDiscovered((*it)->gatt_uuid, (*it)->attribute_handle)) { | 367 if (!IsGattServiceDiscovered(record_state->gatt_uuid, |
| 368 record_state->attribute_handle)) { |
372 BluetoothRemoteGattServiceWin* primary_service = | 369 BluetoothRemoteGattServiceWin* primary_service = |
373 new BluetoothRemoteGattServiceWin(this, (*it)->path, (*it)->gatt_uuid, | 370 new BluetoothRemoteGattServiceWin( |
374 (*it)->attribute_handle, true, | 371 this, record_state->path, record_state->gatt_uuid, |
375 nullptr, ui_task_runner_); | 372 record_state->attribute_handle, true, nullptr, ui_task_runner_); |
376 gatt_services_[primary_service->GetIdentifier()] = | 373 gatt_services_[primary_service->GetIdentifier()] = |
377 base::WrapUnique(primary_service); | 374 base::WrapUnique(primary_service); |
378 adapter_->NotifyGattServiceAdded(primary_service); | 375 adapter_->NotifyGattServiceAdded(primary_service); |
379 } | 376 } |
380 } | 377 } |
381 | 378 |
382 adapter_->NotifyGattServicesDiscovered(this); | 379 adapter_->NotifyGattServicesDiscovered(this); |
383 } | 380 } |
384 | 381 |
385 } // namespace device | 382 } // namespace device |
OLD | NEW |