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 <string> | 7 #include <string> |
8 | 8 |
9 #include "base/containers/scoped_ptr_hash_map.h" | 9 #include "base/containers/scoped_ptr_hash_map.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/memory/scoped_vector.h" | 11 #include "base/memory/scoped_vector.h" |
12 #include "base/sequenced_task_runner.h" | 12 #include "base/sequenced_task_runner.h" |
13 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
14 #include "device/bluetooth/bluetooth_adapter_win.h" | 14 #include "device/bluetooth/bluetooth_adapter_win.h" |
| 15 #include "device/bluetooth/bluetooth_remote_gatt_service_win.h" |
15 #include "device/bluetooth/bluetooth_service_record_win.h" | 16 #include "device/bluetooth/bluetooth_service_record_win.h" |
16 #include "device/bluetooth/bluetooth_socket_thread.h" | 17 #include "device/bluetooth/bluetooth_socket_thread.h" |
17 #include "device/bluetooth/bluetooth_socket_win.h" | 18 #include "device/bluetooth/bluetooth_socket_win.h" |
18 #include "device/bluetooth/bluetooth_task_manager_win.h" | 19 #include "device/bluetooth/bluetooth_task_manager_win.h" |
19 #include "device/bluetooth/bluetooth_uuid.h" | 20 #include "device/bluetooth/bluetooth_uuid.h" |
20 | 21 |
21 namespace { | 22 namespace { |
22 | 23 |
23 const char kApiUnavailable[] = "This API is not implemented on this platform."; | 24 const char kApiUnavailable[] = "This API is not implemented on this platform."; |
24 | 25 |
(...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
293 | 294 |
294 for (ScopedVector<BluetoothTaskManagerWin::ServiceRecordState>::const_iterator | 295 for (ScopedVector<BluetoothTaskManagerWin::ServiceRecordState>::const_iterator |
295 iter = device_state.service_record_states.begin(); | 296 iter = device_state.service_record_states.begin(); |
296 iter != device_state.service_record_states.end(); ++iter) { | 297 iter != device_state.service_record_states.end(); ++iter) { |
297 BluetoothServiceRecordWin* service_record = | 298 BluetoothServiceRecordWin* service_record = |
298 new BluetoothServiceRecordWin(device_state.address, (*iter)->name, | 299 new BluetoothServiceRecordWin(device_state.address, (*iter)->name, |
299 (*iter)->sdp_bytes, (*iter)->gatt_uuid); | 300 (*iter)->sdp_bytes, (*iter)->gatt_uuid); |
300 service_record_list_.push_back(service_record); | 301 service_record_list_.push_back(service_record); |
301 uuids_.push_back(service_record->uuid()); | 302 uuids_.push_back(service_record->uuid()); |
302 } | 303 } |
| 304 |
| 305 if (!device_state.is_bluetooth_classic()) |
| 306 UpdateGattServices(device_state.service_record_states); |
| 307 } |
| 308 |
| 309 bool BluetoothDeviceWin::IsGattServiceDiscovered(BluetoothUUID& uuid, |
| 310 uint16_t attribute_handle) { |
| 311 GattServiceMap::iterator it = gatt_services_.begin(); |
| 312 for (; it != gatt_services_.end(); it++) { |
| 313 uint16_t it_att_handle = |
| 314 static_cast<BluetoothRemoteGattServiceWin*>(it->second) |
| 315 ->GetAttributeHandle(); |
| 316 BluetoothUUID it_uuid = it->second->GetUUID(); |
| 317 if (attribute_handle == it_att_handle && uuid == it_uuid) { |
| 318 return true; |
| 319 } |
| 320 } |
| 321 return false; |
| 322 } |
| 323 |
| 324 bool BluetoothDeviceWin::DoesGattServiceExist( |
| 325 const ScopedVector<BluetoothTaskManagerWin::ServiceRecordState>& |
| 326 service_state, |
| 327 BluetoothGattService* service) { |
| 328 uint16_t attribute_handle = |
| 329 static_cast<BluetoothRemoteGattServiceWin*>(service) |
| 330 ->GetAttributeHandle(); |
| 331 BluetoothUUID uuid = service->GetUUID(); |
| 332 ScopedVector<BluetoothTaskManagerWin::ServiceRecordState>::const_iterator it = |
| 333 service_state.begin(); |
| 334 for (; it != service_state.end(); ++it) { |
| 335 if (attribute_handle == (*it)->attribute_handle && uuid == (*it)->gatt_uuid) |
| 336 return true; |
| 337 } |
| 338 return false; |
| 339 } |
| 340 |
| 341 void BluetoothDeviceWin::UpdateGattServices( |
| 342 const ScopedVector<BluetoothTaskManagerWin::ServiceRecordState>& |
| 343 service_state) { |
| 344 // First, remove no longer exist GATT service. |
| 345 { |
| 346 std::vector<std::string> to_be_removed_services; |
| 347 for (const auto& gatt_service : gatt_services_) { |
| 348 if (!DoesGattServiceExist(service_state, gatt_service.second)) { |
| 349 to_be_removed_services.push_back(gatt_service.first); |
| 350 } |
| 351 } |
| 352 for (const auto& service : to_be_removed_services) { |
| 353 gatt_services_.erase(service); |
| 354 } |
| 355 // Update previously discovered services. |
| 356 for (auto gatt_service : gatt_services_) { |
| 357 static_cast<BluetoothRemoteGattServiceWin*>(gatt_service.second) |
| 358 ->Update(); |
| 359 } |
| 360 } |
| 361 |
| 362 // Return if no new services have been added. |
| 363 if (gatt_services_.size() == service_state.size()) |
| 364 return; |
| 365 |
| 366 // Add new services. |
| 367 for (ScopedVector<BluetoothTaskManagerWin::ServiceRecordState>::const_iterator |
| 368 it = service_state.begin(); |
| 369 it != service_state.end(); ++it) { |
| 370 if (!IsGattServiceDiscovered((*it)->gatt_uuid, (*it)->attribute_handle)) { |
| 371 BluetoothRemoteGattServiceWin* primary_service = |
| 372 new BluetoothRemoteGattServiceWin(this, (*it)->path, (*it)->gatt_uuid, |
| 373 (*it)->attribute_handle, true, |
| 374 nullptr, ui_task_runner_); |
| 375 gatt_services_.add(primary_service->GetIdentifier(), |
| 376 scoped_ptr<BluetoothGattService>(primary_service)); |
| 377 adapter_->NotifyGattServiceAdded(primary_service); |
| 378 } |
| 379 } |
| 380 |
| 381 adapter_->NotifyGattServicesDiscovered(this); |
303 } | 382 } |
304 | 383 |
305 } // namespace device | 384 } // namespace device |
OLD | NEW |