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