Chromium Code Reviews| 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> | 7 #include <memory> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/containers/scoped_ptr_hash_map.h" | |
| 11 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/memory/ptr_util.h" | |
| 12 #include "base/memory/scoped_vector.h" | 12 #include "base/memory/scoped_vector.h" |
| 13 #include "base/sequenced_task_runner.h" | 13 #include "base/sequenced_task_runner.h" |
| 14 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
| 15 #include "device/bluetooth/bluetooth_adapter_win.h" | 15 #include "device/bluetooth/bluetooth_adapter_win.h" |
| 16 #include "device/bluetooth/bluetooth_remote_gatt_service_win.h" | 16 #include "device/bluetooth/bluetooth_remote_gatt_service_win.h" |
| 17 #include "device/bluetooth/bluetooth_service_record_win.h" | 17 #include "device/bluetooth/bluetooth_service_record_win.h" |
| 18 #include "device/bluetooth/bluetooth_socket_thread.h" | 18 #include "device/bluetooth/bluetooth_socket_thread.h" |
| 19 #include "device/bluetooth/bluetooth_socket_win.h" | 19 #include "device/bluetooth/bluetooth_socket_win.h" |
| 20 #include "device/bluetooth/bluetooth_task_manager_win.h" | 20 #include "device/bluetooth/bluetooth_task_manager_win.h" |
| 21 #include "device/bluetooth/bluetooth_uuid.h" | 21 #include "device/bluetooth/bluetooth_uuid.h" |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 44 } | 44 } |
| 45 | 45 |
| 46 BluetoothDeviceWin::~BluetoothDeviceWin() { | 46 BluetoothDeviceWin::~BluetoothDeviceWin() { |
| 47 // Explicitly take and erase GATT services one by one to ensure that calling | 47 // Explicitly take and erase GATT services one by one to ensure that calling |
| 48 // GetGattService on removed service in GattServiceRemoved returns null. | 48 // GetGattService on removed service in GattServiceRemoved returns null. |
| 49 std::vector<std::string> service_keys; | 49 std::vector<std::string> service_keys; |
| 50 for (const auto& gatt_service : gatt_services_) { | 50 for (const auto& gatt_service : gatt_services_) { |
| 51 service_keys.push_back(gatt_service.first); | 51 service_keys.push_back(gatt_service.first); |
| 52 } | 52 } |
| 53 for (const auto& key : service_keys) { | 53 for (const auto& key : service_keys) { |
| 54 gatt_services_.take_and_erase(key); | 54 std::unique_ptr<BluetoothRemoteGattService> service = |
| 55 std::move(gatt_services_[key]); | |
| 56 gatt_services_.erase(key); | |
| 55 } | 57 } |
| 56 } | 58 } |
| 57 | 59 |
| 58 uint32_t BluetoothDeviceWin::GetBluetoothClass() const { | 60 uint32_t BluetoothDeviceWin::GetBluetoothClass() const { |
| 59 return bluetooth_class_; | 61 return bluetooth_class_; |
| 60 } | 62 } |
| 61 | 63 |
| 62 std::string BluetoothDeviceWin::GetAddress() const { | 64 std::string BluetoothDeviceWin::GetAddress() const { |
| 63 return address_; | 65 return address_; |
| 64 } | 66 } |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 206 | 208 |
| 207 void BluetoothDeviceWin::CreateGattConnection( | 209 void BluetoothDeviceWin::CreateGattConnection( |
| 208 const GattConnectionCallback& callback, | 210 const GattConnectionCallback& callback, |
| 209 const ConnectErrorCallback& error_callback) { | 211 const ConnectErrorCallback& error_callback) { |
| 210 // TODO(armansito): Implement. | 212 // TODO(armansito): Implement. |
| 211 error_callback.Run(ERROR_UNSUPPORTED_DEVICE); | 213 error_callback.Run(ERROR_UNSUPPORTED_DEVICE); |
| 212 } | 214 } |
| 213 | 215 |
| 214 const BluetoothServiceRecordWin* BluetoothDeviceWin::GetServiceRecord( | 216 const BluetoothServiceRecordWin* BluetoothDeviceWin::GetServiceRecord( |
| 215 const device::BluetoothUUID& uuid) const { | 217 const device::BluetoothUUID& uuid) const { |
| 216 for (ServiceRecordList::const_iterator iter = service_record_list_.begin(); | 218 for (auto iter = service_record_list_.begin(); |
| 217 iter != service_record_list_.end(); | 219 iter != service_record_list_.end(); ++iter) { |
| 218 ++iter) { | |
| 219 if ((*iter)->uuid() == uuid) | 220 if ((*iter)->uuid() == uuid) |
| 220 return *iter; | 221 return *iter; |
| 221 } | 222 } |
| 222 return NULL; | 223 return NULL; |
| 223 } | 224 } |
| 224 | 225 |
| 225 bool BluetoothDeviceWin::IsEqual( | 226 bool BluetoothDeviceWin::IsEqual( |
| 226 const BluetoothTaskManagerWin::DeviceState& device_state) { | 227 const BluetoothTaskManagerWin::DeviceState& device_state) { |
| 227 if (address_ != device_state.address || name_ != device_state.name || | 228 if (address_ != device_state.address || name_ != device_state.name || |
| 228 bluetooth_class_ != device_state.bluetooth_class || | 229 bluetooth_class_ != device_state.bluetooth_class || |
| 229 visible_ != device_state.visible || | 230 visible_ != device_state.visible || |
| 230 connected_ != device_state.connected || | 231 connected_ != device_state.connected || |
| 231 paired_ != device_state.authenticated) { | 232 paired_ != device_state.authenticated) { |
| 232 return false; | 233 return false; |
| 233 } | 234 } |
| 234 | 235 |
| 235 // Checks service collection | 236 // Checks service collection |
| 236 typedef base::ScopedPtrHashMap<std::string, | |
| 237 std::unique_ptr<BluetoothServiceRecordWin>> | |
| 238 ServiceRecordMap; | |
| 239 | |
| 240 UUIDSet new_services; | 237 UUIDSet new_services; |
| 241 ServiceRecordMap new_service_records; | 238 std::unordered_map<std::string, std::unique_ptr<BluetoothServiceRecordWin>> |
| 242 for (ScopedVector<BluetoothTaskManagerWin::ServiceRecordState>::const_iterator | 239 new_service_records; |
| 243 iter = device_state.service_record_states.begin(); | 240 for (auto iter = device_state.service_record_states.begin(); |
| 244 iter != device_state.service_record_states.end(); ++iter) { | 241 iter != device_state.service_record_states.end(); ++iter) { |
| 245 BluetoothServiceRecordWin* service_record = new BluetoothServiceRecordWin( | 242 std::unique_ptr<BluetoothServiceRecordWin> service_record = |
|
Reilly Grant (use Gerrit)
2016/12/28 22:48:01
auto service_record =
Avi (use Gerrit)
2017/01/02 19:42:26
Done.
| |
| 246 address_, (*iter)->name, (*iter)->sdp_bytes, (*iter)->gatt_uuid); | 243 base::MakeUnique<BluetoothServiceRecordWin>( |
| 244 address_, (*iter)->name, (*iter)->sdp_bytes, (*iter)->gatt_uuid); | |
| 247 new_services.insert(service_record->uuid()); | 245 new_services.insert(service_record->uuid()); |
| 248 new_service_records.set( | 246 new_service_records[service_record->uuid().canonical_value()] = |
| 249 service_record->uuid().canonical_value(), | 247 std::move(service_record); |
| 250 std::unique_ptr<BluetoothServiceRecordWin>(service_record)); | |
| 251 } | 248 } |
| 252 | 249 |
| 253 // Check that no new services have been added or removed. | 250 // Check that no new services have been added or removed. |
| 254 if (uuids_ != new_services) { | 251 if (uuids_ != new_services) { |
| 255 return false; | 252 return false; |
| 256 } | 253 } |
| 257 | 254 |
| 258 for (ServiceRecordList::const_iterator iter = service_record_list_.begin(); | 255 for (auto iter = service_record_list_.begin(); |
| 259 iter != service_record_list_.end(); ++iter) { | 256 iter != service_record_list_.end(); ++iter) { |
| 260 BluetoothServiceRecordWin* service_record = (*iter); | 257 BluetoothServiceRecordWin* service_record = (*iter); |
| 261 BluetoothServiceRecordWin* new_service_record = | 258 BluetoothServiceRecordWin* new_service_record = |
| 262 new_service_records.get((*iter)->uuid().canonical_value()); | 259 new_service_records[(*iter)->uuid().canonical_value()].get(); |
| 263 if (!service_record->IsEqual(*new_service_record)) | 260 if (!service_record->IsEqual(*new_service_record)) |
| 264 return false; | 261 return false; |
| 265 } | 262 } |
| 266 return true; | 263 return true; |
| 267 } | 264 } |
| 268 | 265 |
| 269 void BluetoothDeviceWin::Update( | 266 void BluetoothDeviceWin::Update( |
| 270 const BluetoothTaskManagerWin::DeviceState& device_state) { | 267 const BluetoothTaskManagerWin::DeviceState& device_state) { |
| 271 address_ = device_state.address; | 268 address_ = device_state.address; |
| 272 // Note: Callers are responsible for providing a canonicalized address. | 269 // Note: Callers are responsible for providing a canonicalized address. |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 293 | 290 |
| 294 void BluetoothDeviceWin::SetVisible(bool visible) { | 291 void BluetoothDeviceWin::SetVisible(bool visible) { |
| 295 visible_ = visible; | 292 visible_ = visible; |
| 296 } | 293 } |
| 297 | 294 |
| 298 void BluetoothDeviceWin::UpdateServices( | 295 void BluetoothDeviceWin::UpdateServices( |
| 299 const BluetoothTaskManagerWin::DeviceState& device_state) { | 296 const BluetoothTaskManagerWin::DeviceState& device_state) { |
| 300 uuids_.clear(); | 297 uuids_.clear(); |
| 301 service_record_list_.clear(); | 298 service_record_list_.clear(); |
| 302 | 299 |
| 303 for (ScopedVector<BluetoothTaskManagerWin::ServiceRecordState>::const_iterator | 300 for (auto iter = device_state.service_record_states.begin(); |
| 304 iter = device_state.service_record_states.begin(); | |
| 305 iter != device_state.service_record_states.end(); ++iter) { | 301 iter != device_state.service_record_states.end(); ++iter) { |
| 306 BluetoothServiceRecordWin* service_record = | 302 BluetoothServiceRecordWin* service_record = |
| 307 new BluetoothServiceRecordWin(device_state.address, (*iter)->name, | 303 new BluetoothServiceRecordWin(device_state.address, (*iter)->name, |
| 308 (*iter)->sdp_bytes, (*iter)->gatt_uuid); | 304 (*iter)->sdp_bytes, (*iter)->gatt_uuid); |
| 309 service_record_list_.push_back(service_record); | 305 service_record_list_.push_back(service_record); |
| 310 uuids_.insert(service_record->uuid()); | 306 uuids_.insert(service_record->uuid()); |
| 311 } | 307 } |
| 312 | 308 |
| 313 if (!device_state.is_bluetooth_classic()) | 309 if (!device_state.is_bluetooth_classic()) |
| 314 UpdateGattServices(device_state.service_record_states); | 310 UpdateGattServices(device_state.service_record_states); |
| 315 } | 311 } |
| 316 | 312 |
| 317 bool BluetoothDeviceWin::IsGattServiceDiscovered(BluetoothUUID& uuid, | 313 bool BluetoothDeviceWin::IsGattServiceDiscovered(BluetoothUUID& uuid, |
| 318 uint16_t attribute_handle) { | 314 uint16_t attribute_handle) { |
| 319 GattServiceMap::iterator it = gatt_services_.begin(); | 315 for (const auto& gatt_service : gatt_services_) { |
| 320 for (; it != gatt_services_.end(); it++) { | |
| 321 uint16_t it_att_handle = | 316 uint16_t it_att_handle = |
| 322 static_cast<BluetoothRemoteGattServiceWin*>(it->second) | 317 static_cast<BluetoothRemoteGattServiceWin*>(gatt_service.second.get()) |
| 323 ->GetAttributeHandle(); | 318 ->GetAttributeHandle(); |
| 324 BluetoothUUID it_uuid = it->second->GetUUID(); | 319 BluetoothUUID it_uuid = gatt_service.second->GetUUID(); |
| 325 if (attribute_handle == it_att_handle && uuid == it_uuid) { | 320 if (attribute_handle == it_att_handle && uuid == it_uuid) { |
| 326 return true; | 321 return true; |
| 327 } | 322 } |
| 328 } | 323 } |
| 329 return false; | 324 return false; |
| 330 } | 325 } |
| 331 | 326 |
| 332 bool BluetoothDeviceWin::DoesGattServiceExist( | 327 bool BluetoothDeviceWin::DoesGattServiceExist( |
| 333 const ScopedVector<BluetoothTaskManagerWin::ServiceRecordState>& | 328 const ScopedVector<BluetoothTaskManagerWin::ServiceRecordState>& |
| 334 service_state, | 329 service_state, |
| 335 BluetoothRemoteGattService* service) { | 330 BluetoothRemoteGattService* service) { |
| 336 uint16_t attribute_handle = | 331 uint16_t attribute_handle = |
| 337 static_cast<BluetoothRemoteGattServiceWin*>(service) | 332 static_cast<BluetoothRemoteGattServiceWin*>(service) |
| 338 ->GetAttributeHandle(); | 333 ->GetAttributeHandle(); |
| 339 BluetoothUUID uuid = service->GetUUID(); | 334 BluetoothUUID uuid = service->GetUUID(); |
| 340 ScopedVector<BluetoothTaskManagerWin::ServiceRecordState>::const_iterator it = | 335 auto it = service_state.begin(); |
| 341 service_state.begin(); | |
| 342 for (; it != service_state.end(); ++it) { | 336 for (; it != service_state.end(); ++it) { |
| 343 if (attribute_handle == (*it)->attribute_handle && uuid == (*it)->gatt_uuid) | 337 if (attribute_handle == (*it)->attribute_handle && uuid == (*it)->gatt_uuid) |
| 344 return true; | 338 return true; |
| 345 } | 339 } |
| 346 return false; | 340 return false; |
| 347 } | 341 } |
| 348 | 342 |
| 349 void BluetoothDeviceWin::UpdateGattServices( | 343 void BluetoothDeviceWin::UpdateGattServices( |
| 350 const ScopedVector<BluetoothTaskManagerWin::ServiceRecordState>& | 344 const ScopedVector<BluetoothTaskManagerWin::ServiceRecordState>& |
| 351 service_state) { | 345 service_state) { |
| 352 // First, remove no longer exist GATT service. | 346 // First, remove no longer exist GATT service. |
| 353 { | 347 { |
| 354 std::vector<std::string> to_be_removed_services; | 348 std::vector<std::string> to_be_removed_services; |
| 355 for (const auto& gatt_service : gatt_services_) { | 349 for (const auto& gatt_service : gatt_services_) { |
| 356 if (!DoesGattServiceExist(service_state, gatt_service.second)) { | 350 if (!DoesGattServiceExist(service_state, gatt_service.second.get())) { |
| 357 to_be_removed_services.push_back(gatt_service.first); | 351 to_be_removed_services.push_back(gatt_service.first); |
| 358 } | 352 } |
| 359 } | 353 } |
| 360 for (const auto& service : to_be_removed_services) { | 354 for (const auto& service : to_be_removed_services) { |
| 361 gatt_services_.take_and_erase(service); | 355 std::unique_ptr<BluetoothRemoteGattService> service_ptr = |
| 356 std::move(gatt_services_[service]); | |
| 357 gatt_services_.erase(service); | |
| 362 } | 358 } |
| 363 // Update previously discovered services. | 359 // Update previously discovered services. |
| 364 for (auto gatt_service : gatt_services_) { | 360 for (const auto& gatt_service : gatt_services_) { |
| 365 static_cast<BluetoothRemoteGattServiceWin*>(gatt_service.second) | 361 static_cast<BluetoothRemoteGattServiceWin*>(gatt_service.second.get()) |
| 366 ->Update(); | 362 ->Update(); |
| 367 } | 363 } |
| 368 } | 364 } |
| 369 | 365 |
| 370 // Return if no new services have been added. | 366 // Return if no new services have been added. |
| 371 if (gatt_services_.size() == service_state.size()) | 367 if (gatt_services_.size() == service_state.size()) |
| 372 return; | 368 return; |
| 373 | 369 |
| 374 // Add new services. | 370 // Add new services. |
| 375 for (ScopedVector<BluetoothTaskManagerWin::ServiceRecordState>::const_iterator | 371 for (auto it = service_state.begin(); it != service_state.end(); ++it) { |
| 376 it = service_state.begin(); | |
| 377 it != service_state.end(); ++it) { | |
| 378 if (!IsGattServiceDiscovered((*it)->gatt_uuid, (*it)->attribute_handle)) { | 372 if (!IsGattServiceDiscovered((*it)->gatt_uuid, (*it)->attribute_handle)) { |
| 379 BluetoothRemoteGattServiceWin* primary_service = | 373 BluetoothRemoteGattServiceWin* primary_service = |
| 380 new BluetoothRemoteGattServiceWin(this, (*it)->path, (*it)->gatt_uuid, | 374 new BluetoothRemoteGattServiceWin(this, (*it)->path, (*it)->gatt_uuid, |
| 381 (*it)->attribute_handle, true, | 375 (*it)->attribute_handle, true, |
| 382 nullptr, ui_task_runner_); | 376 nullptr, ui_task_runner_); |
| 383 gatt_services_.add( | 377 gatt_services_[primary_service->GetIdentifier()] = |
| 384 primary_service->GetIdentifier(), | 378 base::WrapUnique(primary_service); |
| 385 std::unique_ptr<BluetoothRemoteGattService>(primary_service)); | |
| 386 adapter_->NotifyGattServiceAdded(primary_service); | 379 adapter_->NotifyGattServiceAdded(primary_service); |
| 387 } | 380 } |
| 388 } | 381 } |
| 389 | 382 |
| 390 adapter_->NotifyGattServicesDiscovered(this); | 383 adapter_->NotifyGattServicesDiscovered(this); |
| 391 } | 384 } |
| 392 | 385 |
| 393 } // namespace device | 386 } // namespace device |
| OLD | NEW |