| 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 "components/arc/bluetooth/arc_bluetooth_bridge.h" | 5 #include "components/arc/bluetooth/arc_bluetooth_bridge.h" |
| 6 | 6 |
| 7 #include <fcntl.h> | 7 #include <fcntl.h> |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <iomanip> | 10 #include <iomanip> |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 VLOG(1) << "registering bluetooth adapter"; | 65 VLOG(1) << "registering bluetooth adapter"; |
| 66 BluetoothAdapterFactory::GetAdapter(base::Bind( | 66 BluetoothAdapterFactory::GetAdapter(base::Bind( |
| 67 &ArcBluetoothBridge::OnAdapterInitialized, weak_factory_.GetWeakPtr())); | 67 &ArcBluetoothBridge::OnAdapterInitialized, weak_factory_.GetWeakPtr())); |
| 68 } else { | 68 } else { |
| 69 VLOG(1) << "no bluetooth adapter available"; | 69 VLOG(1) << "no bluetooth adapter available"; |
| 70 } | 70 } |
| 71 | 71 |
| 72 arc_bridge_service()->AddObserver(this); | 72 arc_bridge_service()->AddObserver(this); |
| 73 } | 73 } |
| 74 | 74 |
| 75 ArcBluetoothBridge::ArcBluetoothBridge(ArcBridgeService* bridge_service, | |
| 76 bool test_flag) | |
| 77 : ArcService(bridge_service), binding_(this), weak_factory_(this) { | |
| 78 if (!test_flag) { | |
| 79 LOG(ERROR) << "Test Constructor got called with test = false"; | |
| 80 } | |
| 81 arc_bridge_service()->AddObserver(this); | |
| 82 } | |
| 83 | |
| 84 ArcBluetoothBridge::~ArcBluetoothBridge() { | 75 ArcBluetoothBridge::~ArcBluetoothBridge() { |
| 85 arc_bridge_service()->RemoveObserver(this); | 76 arc_bridge_service()->RemoveObserver(this); |
| 86 | 77 |
| 87 if (bluetooth_adapter_) | 78 if (bluetooth_adapter_) |
| 88 bluetooth_adapter_->RemoveObserver(this); | 79 bluetooth_adapter_->RemoveObserver(this); |
| 89 } | 80 } |
| 90 | 81 |
| 91 void ArcBluetoothBridge::OnAdapterInitialized( | 82 void ArcBluetoothBridge::OnAdapterInitialized( |
| 92 scoped_refptr<BluetoothAdapter> adapter) { | 83 scoped_refptr<BluetoothAdapter> adapter) { |
| 93 bluetooth_adapter_ = adapter; | 84 bluetooth_adapter_ = adapter; |
| 94 bluetooth_adapter_->AddObserver(this); | 85 bluetooth_adapter_->AddObserver(this); |
| 95 } | 86 } |
| 96 | 87 |
| 97 void ArcBluetoothBridge::OnBluetoothInstanceReady() { | 88 void ArcBluetoothBridge::OnBluetoothInstanceReady() { |
| 98 mojom::BluetoothInstance* bluetooth_instance = | 89 mojom::BluetoothInstance* bluetooth_instance = |
| 99 arc_bridge_service()->bluetooth_instance(); | 90 arc_bridge_service()->bluetooth_instance(); |
| 100 if (!bluetooth_instance) { | 91 if (!bluetooth_instance) { |
| 101 LOG(ERROR) << "OnBluetoothInstanceReady called, " | 92 LOG(ERROR) << "OnBluetoothInstanceReady called, " |
| 102 << "but no bluetooth instance found"; | 93 << "but no bluetooth instance found"; |
| 103 return; | 94 return; |
| 104 } | 95 } |
| 105 | 96 |
| 106 bluetooth_instance_ = base::WrapUnique(bluetooth_instance); | 97 arc_bridge_service()->bluetooth_instance()->Init( |
| 107 bluetooth_version_ = arc_bridge_service()->bluetooth_version(); | 98 binding_.CreateInterfacePtrAndBind()); |
| 108 bluetooth_instance_->Init(binding_.CreateInterfacePtrAndBind()); | |
| 109 } | 99 } |
| 110 | 100 |
| 111 void ArcBluetoothBridge::AdapterPresentChanged(BluetoothAdapter* adapter, | 101 void ArcBluetoothBridge::AdapterPresentChanged(BluetoothAdapter* adapter, |
| 112 bool present) { | 102 bool present) { |
| 113 // If the adapter goes away, remove ourselves as an observer. | 103 // If the adapter goes away, remove ourselves as an observer. |
| 114 if (!present && adapter == bluetooth_adapter_) { | 104 if (!present && adapter == bluetooth_adapter_) { |
| 115 adapter->RemoveObserver(this); | 105 adapter->RemoveObserver(this); |
| 116 bluetooth_adapter_ = nullptr; | 106 bluetooth_adapter_ = nullptr; |
| 117 } | 107 } |
| 118 } | 108 } |
| (...skipping 11 matching lines...) Expand all Loading... |
| 130 BluetoothDevice* device) { | 120 BluetoothDevice* device) { |
| 131 if (!HasBluetoothInstance()) | 121 if (!HasBluetoothInstance()) |
| 132 return; | 122 return; |
| 133 | 123 |
| 134 if (device->IsConnected()) | 124 if (device->IsConnected()) |
| 135 return; | 125 return; |
| 136 | 126 |
| 137 mojo::Array<mojom::BluetoothPropertyPtr> properties = | 127 mojo::Array<mojom::BluetoothPropertyPtr> properties = |
| 138 GetDeviceProperties(mojom::BluetoothPropertyType::ALL, device); | 128 GetDeviceProperties(mojom::BluetoothPropertyType::ALL, device); |
| 139 | 129 |
| 140 bluetooth_instance_->OnDeviceFound(std::move(properties)); | 130 arc_bridge_service()->bluetooth_instance()->OnDeviceFound( |
| 131 std::move(properties)); |
| 141 | 132 |
| 142 if (bluetooth_version_ < kMinBtleVersion) { | 133 if (arc_bridge_service()->bluetooth_version() < kMinBtleVersion) { |
| 143 LOG(WARNING) << "Bluetooth instance is too old and does not support BTLE"; | 134 LOG(WARNING) << "Bluetooth instance is too old and does not support BTLE"; |
| 144 return; | 135 return; |
| 145 } | 136 } |
| 146 | 137 |
| 147 mojom::BluetoothAddressPtr addr = | 138 mojom::BluetoothAddressPtr addr = |
| 148 mojom::BluetoothAddress::From(device->GetAddress()); | 139 mojom::BluetoothAddress::From(device->GetAddress()); |
| 149 int rssi = device->GetInquiryRSSI(); | 140 int rssi = device->GetInquiryRSSI(); |
| 150 mojo::Array<mojom::BluetoothAdvertisingDataPtr> adv_data = | 141 mojo::Array<mojom::BluetoothAdvertisingDataPtr> adv_data = |
| 151 GetAdvertisingData(device); | 142 GetAdvertisingData(device); |
| 152 bluetooth_instance_->OnLEDeviceFound(std::move(addr), rssi, | 143 arc_bridge_service()->bluetooth_instance()->OnLEDeviceFound( |
| 153 std::move(adv_data)); | 144 std::move(addr), rssi, std::move(adv_data)); |
| 154 } | 145 } |
| 155 | 146 |
| 156 void ArcBluetoothBridge::DeviceChanged(BluetoothAdapter* adapter, | 147 void ArcBluetoothBridge::DeviceChanged(BluetoothAdapter* adapter, |
| 157 BluetoothDevice* device) { | 148 BluetoothDevice* device) { |
| 158 // TODO(smbarber): device properties changed; inform the container. | 149 // TODO(smbarber): device properties changed; inform the container. |
| 159 } | 150 } |
| 160 | 151 |
| 161 void ArcBluetoothBridge::DeviceAddressChanged(BluetoothAdapter* adapter, | 152 void ArcBluetoothBridge::DeviceAddressChanged(BluetoothAdapter* adapter, |
| 162 BluetoothDevice* device, | 153 BluetoothDevice* device, |
| 163 const std::string& old_address) { | 154 const std::string& old_address) { |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 206 BluetoothDevice* device, | 197 BluetoothDevice* device, |
| 207 BluetoothRemoteGattService* service) { | 198 BluetoothRemoteGattService* service) { |
| 208 // Placeholder for GATT client functionality | 199 // Placeholder for GATT client functionality |
| 209 } | 200 } |
| 210 | 201 |
| 211 void ArcBluetoothBridge::GattServicesDiscovered(BluetoothAdapter* adapter, | 202 void ArcBluetoothBridge::GattServicesDiscovered(BluetoothAdapter* adapter, |
| 212 BluetoothDevice* device) { | 203 BluetoothDevice* device) { |
| 213 if (!HasBluetoothInstance()) | 204 if (!HasBluetoothInstance()) |
| 214 return; | 205 return; |
| 215 | 206 |
| 216 if (bluetooth_version_ < kMinBtleVersion) { | 207 if (arc_bridge_service()->bluetooth_version() < kMinBtleVersion) { |
| 217 LOG(WARNING) << "Bluetooth instance is too old and does not support BTLE"; | 208 LOG(WARNING) << "Bluetooth instance is too old and does not support BTLE"; |
| 218 return; | 209 return; |
| 219 } | 210 } |
| 220 | 211 |
| 221 mojom::BluetoothAddressPtr addr = | 212 mojom::BluetoothAddressPtr addr = |
| 222 mojom::BluetoothAddress::From(device->GetAddress()); | 213 mojom::BluetoothAddress::From(device->GetAddress()); |
| 223 | 214 |
| 224 bluetooth_instance_->OnSearchComplete( | 215 arc_bridge_service()->bluetooth_instance()->OnSearchComplete( |
| 225 std::move(addr), mojom::BluetoothGattStatus::GATT_SUCCESS); | 216 std::move(addr), mojom::BluetoothGattStatus::GATT_SUCCESS); |
| 226 } | 217 } |
| 227 | 218 |
| 228 void ArcBluetoothBridge::GattDiscoveryCompleteForService( | 219 void ArcBluetoothBridge::GattDiscoveryCompleteForService( |
| 229 BluetoothAdapter* adapter, | 220 BluetoothAdapter* adapter, |
| 230 BluetoothRemoteGattService* service) { | 221 BluetoothRemoteGattService* service) { |
| 231 // Placeholder for GATT client functionality | 222 // Placeholder for GATT client functionality |
| 232 } | 223 } |
| 233 | 224 |
| 234 void ArcBluetoothBridge::GattServiceChanged( | 225 void ArcBluetoothBridge::GattServiceChanged( |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 300 } | 291 } |
| 301 | 292 |
| 302 void ArcBluetoothBridge::GetAdapterProperty(mojom::BluetoothPropertyType type) { | 293 void ArcBluetoothBridge::GetAdapterProperty(mojom::BluetoothPropertyType type) { |
| 303 DCHECK(bluetooth_adapter_); | 294 DCHECK(bluetooth_adapter_); |
| 304 if (!HasBluetoothInstance()) | 295 if (!HasBluetoothInstance()) |
| 305 return; | 296 return; |
| 306 | 297 |
| 307 mojo::Array<mojom::BluetoothPropertyPtr> properties = | 298 mojo::Array<mojom::BluetoothPropertyPtr> properties = |
| 308 GetAdapterProperties(type); | 299 GetAdapterProperties(type); |
| 309 | 300 |
| 310 bluetooth_instance_->OnAdapterProperties(mojom::BluetoothStatus::SUCCESS, | 301 arc_bridge_service()->bluetooth_instance()->OnAdapterProperties( |
| 311 std::move(properties)); | 302 mojom::BluetoothStatus::SUCCESS, std::move(properties)); |
| 312 } | 303 } |
| 313 | 304 |
| 314 void ArcBluetoothBridge::SetAdapterProperty( | 305 void ArcBluetoothBridge::SetAdapterProperty( |
| 315 mojom::BluetoothPropertyPtr property) { | 306 mojom::BluetoothPropertyPtr property) { |
| 316 DCHECK(bluetooth_adapter_); | 307 DCHECK(bluetooth_adapter_); |
| 317 if (!HasBluetoothInstance()) | 308 if (!HasBluetoothInstance()) |
| 318 return; | 309 return; |
| 319 | 310 |
| 320 // TODO(smbarber): Implement SetAdapterProperty | 311 // TODO(smbarber): Implement SetAdapterProperty |
| 321 bluetooth_instance_->OnAdapterProperties( | 312 arc_bridge_service()->bluetooth_instance()->OnAdapterProperties( |
| 322 mojom::BluetoothStatus::FAIL, | 313 mojom::BluetoothStatus::FAIL, |
| 323 mojo::Array<mojom::BluetoothPropertyPtr>::New(0)); | 314 mojo::Array<mojom::BluetoothPropertyPtr>::New(0)); |
| 324 } | 315 } |
| 325 | 316 |
| 326 void ArcBluetoothBridge::GetRemoteDeviceProperty( | 317 void ArcBluetoothBridge::GetRemoteDeviceProperty( |
| 327 mojom::BluetoothAddressPtr remote_addr, | 318 mojom::BluetoothAddressPtr remote_addr, |
| 328 mojom::BluetoothPropertyType type) { | 319 mojom::BluetoothPropertyType type) { |
| 329 DCHECK(bluetooth_adapter_); | 320 DCHECK(bluetooth_adapter_); |
| 330 if (!HasBluetoothInstance()) | 321 if (!HasBluetoothInstance()) |
| 331 return; | 322 return; |
| 332 | 323 |
| 333 std::string addr_str = remote_addr->To<std::string>(); | 324 std::string addr_str = remote_addr->To<std::string>(); |
| 334 BluetoothDevice* device = bluetooth_adapter_->GetDevice(addr_str); | 325 BluetoothDevice* device = bluetooth_adapter_->GetDevice(addr_str); |
| 335 | 326 |
| 336 mojo::Array<mojom::BluetoothPropertyPtr> properties = | 327 mojo::Array<mojom::BluetoothPropertyPtr> properties = |
| 337 GetDeviceProperties(type, device); | 328 GetDeviceProperties(type, device); |
| 338 mojom::BluetoothStatus status = mojom::BluetoothStatus::SUCCESS; | 329 mojom::BluetoothStatus status = mojom::BluetoothStatus::SUCCESS; |
| 339 | 330 |
| 340 if (!device) { | 331 if (!device) { |
| 341 VLOG(1) << __func__ << ": device " << addr_str << " not available"; | 332 VLOG(1) << __func__ << ": device " << addr_str << " not available"; |
| 342 status = mojom::BluetoothStatus::FAIL; | 333 status = mojom::BluetoothStatus::FAIL; |
| 343 } | 334 } |
| 344 | 335 |
| 345 bluetooth_instance_->OnRemoteDeviceProperties(status, std::move(remote_addr), | 336 arc_bridge_service()->bluetooth_instance()->OnRemoteDeviceProperties( |
| 346 std::move(properties)); | 337 status, std::move(remote_addr), std::move(properties)); |
| 347 } | 338 } |
| 348 | 339 |
| 349 void ArcBluetoothBridge::SetRemoteDeviceProperty( | 340 void ArcBluetoothBridge::SetRemoteDeviceProperty( |
| 350 mojom::BluetoothAddressPtr remote_addr, | 341 mojom::BluetoothAddressPtr remote_addr, |
| 351 mojom::BluetoothPropertyPtr property) { | 342 mojom::BluetoothPropertyPtr property) { |
| 352 DCHECK(bluetooth_adapter_); | 343 DCHECK(bluetooth_adapter_); |
| 353 if (!HasBluetoothInstance()) | 344 if (!HasBluetoothInstance()) |
| 354 return; | 345 return; |
| 355 | 346 |
| 356 // TODO(smbarber): Implement SetRemoteDeviceProperty | 347 // TODO(smbarber): Implement SetRemoteDeviceProperty |
| 357 bluetooth_instance_->OnRemoteDeviceProperties( | 348 arc_bridge_service()->bluetooth_instance()->OnRemoteDeviceProperties( |
| 358 mojom::BluetoothStatus::FAIL, std::move(remote_addr), | 349 mojom::BluetoothStatus::FAIL, std::move(remote_addr), |
| 359 mojo::Array<mojom::BluetoothPropertyPtr>::New(0)); | 350 mojo::Array<mojom::BluetoothPropertyPtr>::New(0)); |
| 360 } | 351 } |
| 361 | 352 |
| 362 void ArcBluetoothBridge::GetRemoteServiceRecord( | 353 void ArcBluetoothBridge::GetRemoteServiceRecord( |
| 363 mojom::BluetoothAddressPtr remote_addr, | 354 mojom::BluetoothAddressPtr remote_addr, |
| 364 mojom::BluetoothUUIDPtr uuid) { | 355 mojom::BluetoothUUIDPtr uuid) { |
| 365 // TODO(smbarber): Implement GetRemoteServiceRecord | 356 // TODO(smbarber): Implement GetRemoteServiceRecord |
| 366 } | 357 } |
| 367 | 358 |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 417 : mojom::BluetoothAdapterState::OFF); | 408 : mojom::BluetoothAdapterState::OFF); |
| 418 } | 409 } |
| 419 | 410 |
| 420 void ArcBluetoothBridge::OnDiscoveryStarted( | 411 void ArcBluetoothBridge::OnDiscoveryStarted( |
| 421 std::unique_ptr<BluetoothDiscoverySession> session) { | 412 std::unique_ptr<BluetoothDiscoverySession> session) { |
| 422 if (!HasBluetoothInstance()) | 413 if (!HasBluetoothInstance()) |
| 423 return; | 414 return; |
| 424 | 415 |
| 425 discovery_session_ = std::move(session); | 416 discovery_session_ = std::move(session); |
| 426 | 417 |
| 427 bluetooth_instance_->OnDiscoveryStateChanged( | 418 arc_bridge_service()->bluetooth_instance()->OnDiscoveryStateChanged( |
| 428 mojom::BluetoothDiscoveryState::STARTED); | 419 mojom::BluetoothDiscoveryState::STARTED); |
| 429 | 420 |
| 430 SendCachedDevicesFound(); | 421 SendCachedDevicesFound(); |
| 431 } | 422 } |
| 432 | 423 |
| 433 void ArcBluetoothBridge::OnDiscoveryStopped() { | 424 void ArcBluetoothBridge::OnDiscoveryStopped() { |
| 434 if (!HasBluetoothInstance()) | 425 if (!HasBluetoothInstance()) |
| 435 return; | 426 return; |
| 436 | 427 |
| 437 discovery_session_.reset(); | 428 discovery_session_.reset(); |
| 438 | 429 |
| 439 bluetooth_instance_->OnDiscoveryStateChanged( | 430 arc_bridge_service()->bluetooth_instance()->OnDiscoveryStateChanged( |
| 440 mojom::BluetoothDiscoveryState::STOPPED); | 431 mojom::BluetoothDiscoveryState::STOPPED); |
| 441 } | 432 } |
| 442 | 433 |
| 443 void ArcBluetoothBridge::CreateBond(mojom::BluetoothAddressPtr addr, | 434 void ArcBluetoothBridge::CreateBond(mojom::BluetoothAddressPtr addr, |
| 444 int32_t transport) { | 435 int32_t transport) { |
| 445 std::string addr_str = addr->To<std::string>(); | 436 std::string addr_str = addr->To<std::string>(); |
| 446 BluetoothDevice* device = bluetooth_adapter_->GetDevice(addr_str); | 437 BluetoothDevice* device = bluetooth_adapter_->GetDevice(addr_str); |
| 447 if (!device || !device->IsPairable()) { | 438 if (!device || !device->IsPairable()) { |
| 448 VLOG(1) << __func__ << ": device " << addr_str | 439 VLOG(1) << __func__ << ": device " << addr_str |
| 449 << " is no longer valid or pairable"; | 440 << " is no longer valid or pairable"; |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 538 void ArcBluetoothBridge::StopLEScan() { | 529 void ArcBluetoothBridge::StopLEScan() { |
| 539 CancelDiscovery(); | 530 CancelDiscovery(); |
| 540 } | 531 } |
| 541 | 532 |
| 542 void ArcBluetoothBridge::OnGattConnectStateChanged( | 533 void ArcBluetoothBridge::OnGattConnectStateChanged( |
| 543 mojom::BluetoothAddressPtr addr, | 534 mojom::BluetoothAddressPtr addr, |
| 544 bool connected) const { | 535 bool connected) const { |
| 545 if (!HasBluetoothInstance()) | 536 if (!HasBluetoothInstance()) |
| 546 return; | 537 return; |
| 547 | 538 |
| 548 if (bluetooth_version_ < kMinBtleVersion) { | 539 if (arc_bridge_service()->bluetooth_version() < kMinBtleVersion) { |
| 549 LOG(WARNING) << "Bluetooth instance is too old and does not support BTLE"; | 540 LOG(WARNING) << "Bluetooth instance is too old and does not support BTLE"; |
| 550 return; | 541 return; |
| 551 } | 542 } |
| 552 | 543 |
| 553 DCHECK(addr); | 544 DCHECK(addr); |
| 554 | 545 |
| 555 bluetooth_instance_->OnLEConnectionStateChange( | 546 arc_bridge_service()->bluetooth_instance()->OnLEConnectionStateChange( |
| 556 std::move(addr), connected); | 547 std::move(addr), connected); |
| 557 } | 548 } |
| 558 | 549 |
| 559 void ArcBluetoothBridge::OnGattConnected( | 550 void ArcBluetoothBridge::OnGattConnected( |
| 560 mojom::BluetoothAddressPtr addr, | 551 mojom::BluetoothAddressPtr addr, |
| 561 std::unique_ptr<BluetoothGattConnection> connection) const { | 552 std::unique_ptr<BluetoothGattConnection> connection) const { |
| 562 OnGattConnectStateChanged(std::move(addr), true); | 553 OnGattConnectStateChanged(std::move(addr), true); |
| 563 } | 554 } |
| 564 | 555 |
| 565 void ArcBluetoothBridge::OnGattConnectError( | 556 void ArcBluetoothBridge::OnGattConnectError( |
| (...skipping 10 matching lines...) Expand all Loading... |
| 576 void ArcBluetoothBridge::ConnectLEDevice( | 567 void ArcBluetoothBridge::ConnectLEDevice( |
| 577 mojom::BluetoothAddressPtr remote_addr) { | 568 mojom::BluetoothAddressPtr remote_addr) { |
| 578 if (!HasBluetoothInstance()) | 569 if (!HasBluetoothInstance()) |
| 579 return; | 570 return; |
| 580 | 571 |
| 581 BluetoothDevice* device = | 572 BluetoothDevice* device = |
| 582 bluetooth_adapter_->GetDevice(remote_addr->To<std::string>()); | 573 bluetooth_adapter_->GetDevice(remote_addr->To<std::string>()); |
| 583 DCHECK(device); | 574 DCHECK(device); |
| 584 | 575 |
| 585 if (device->IsConnected()) { | 576 if (device->IsConnected()) { |
| 586 bluetooth_instance_->OnLEConnectionStateChange(std::move(remote_addr), | 577 arc_bridge_service()->bluetooth_instance()->OnLEConnectionStateChange( |
| 587 true); | 578 std::move(remote_addr), true); |
| 588 return; | 579 return; |
| 589 } | 580 } |
| 590 | 581 |
| 591 // Also pass disconnect callback in error case | 582 // Also pass disconnect callback in error case |
| 592 // since it would be disconnected anyway. | 583 // since it would be disconnected anyway. |
| 593 mojom::BluetoothAddressPtr remote_addr_clone = remote_addr.Clone(); | 584 mojom::BluetoothAddressPtr remote_addr_clone = remote_addr.Clone(); |
| 594 device->CreateGattConnection( | 585 device->CreateGattConnection( |
| 595 base::Bind(&ArcBluetoothBridge::OnGattConnected, | 586 base::Bind(&ArcBluetoothBridge::OnGattConnected, |
| 596 weak_factory_.GetWeakPtr(), base::Passed(&remote_addr)), | 587 weak_factory_.GetWeakPtr(), base::Passed(&remote_addr)), |
| 597 base::Bind(&ArcBluetoothBridge::OnGattConnectError, | 588 base::Bind(&ArcBluetoothBridge::OnGattConnectError, |
| 598 weak_factory_.GetWeakPtr(), base::Passed(&remote_addr_clone))); | 589 weak_factory_.GetWeakPtr(), base::Passed(&remote_addr_clone))); |
| 599 } | 590 } |
| 600 | 591 |
| 601 void ArcBluetoothBridge::DisconnectLEDevice( | 592 void ArcBluetoothBridge::DisconnectLEDevice( |
| 602 mojom::BluetoothAddressPtr remote_addr) { | 593 mojom::BluetoothAddressPtr remote_addr) { |
| 603 if (!HasBluetoothInstance()) | 594 if (!HasBluetoothInstance()) |
| 604 return; | 595 return; |
| 605 | 596 |
| 606 BluetoothDevice* device = | 597 BluetoothDevice* device = |
| 607 bluetooth_adapter_->GetDevice(remote_addr->To<std::string>()); | 598 bluetooth_adapter_->GetDevice(remote_addr->To<std::string>()); |
| 608 DCHECK(device); | 599 DCHECK(device); |
| 609 | 600 |
| 610 if (!device->IsConnected()) { | 601 if (!device->IsConnected()) { |
| 611 bluetooth_instance_->OnLEConnectionStateChange(std::move(remote_addr), | 602 arc_bridge_service()->bluetooth_instance()->OnLEConnectionStateChange( |
| 612 false); | 603 std::move(remote_addr), false); |
| 613 return; | 604 return; |
| 614 } | 605 } |
| 615 | 606 |
| 616 mojom::BluetoothAddressPtr remote_addr_clone = remote_addr.Clone(); | 607 mojom::BluetoothAddressPtr remote_addr_clone = remote_addr.Clone(); |
| 617 device->Disconnect( | 608 device->Disconnect( |
| 618 base::Bind(&ArcBluetoothBridge::OnGattDisconnected, | 609 base::Bind(&ArcBluetoothBridge::OnGattDisconnected, |
| 619 weak_factory_.GetWeakPtr(), base::Passed(&remote_addr)), | 610 weak_factory_.GetWeakPtr(), base::Passed(&remote_addr)), |
| 620 base::Bind(&ArcBluetoothBridge::OnGattDisconnected, | 611 base::Bind(&ArcBluetoothBridge::OnGattDisconnected, |
| 621 weak_factory_.GetWeakPtr(), base::Passed(&remote_addr_clone))); | 612 weak_factory_.GetWeakPtr(), base::Passed(&remote_addr_clone))); |
| 622 } | 613 } |
| 623 | 614 |
| 624 void ArcBluetoothBridge::SearchService(mojom::BluetoothAddressPtr remote_addr) { | 615 void ArcBluetoothBridge::SearchService(mojom::BluetoothAddressPtr remote_addr) { |
| 625 if (!HasBluetoothInstance()) | 616 if (!HasBluetoothInstance()) |
| 626 return; | 617 return; |
| 627 | 618 |
| 628 BluetoothDevice* device = | 619 BluetoothDevice* device = |
| 629 bluetooth_adapter_->GetDevice(remote_addr->To<std::string>()); | 620 bluetooth_adapter_->GetDevice(remote_addr->To<std::string>()); |
| 630 DCHECK(device); | 621 DCHECK(device); |
| 631 | 622 |
| 632 // Call the callback if discovery is completed | 623 // Call the callback if discovery is completed |
| 633 if (device->IsGattServicesDiscoveryComplete()) { | 624 if (device->IsGattServicesDiscoveryComplete()) { |
| 634 bluetooth_instance_->OnSearchComplete( | 625 arc_bridge_service()->bluetooth_instance()->OnSearchComplete( |
| 635 std::move(remote_addr), mojom::BluetoothGattStatus::GATT_SUCCESS); | 626 std::move(remote_addr), mojom::BluetoothGattStatus::GATT_SUCCESS); |
| 636 return; | 627 return; |
| 637 } | 628 } |
| 638 | 629 |
| 639 // Discard result. Will call the callback when discovery is completed. | 630 // Discard result. Will call the callback when discovery is completed. |
| 640 device->GetGattServices(); | 631 device->GetGattServices(); |
| 641 } | 632 } |
| 642 | 633 |
| 643 void ArcBluetoothBridge::OnStartLEListenDone( | 634 void ArcBluetoothBridge::OnStartLEListenDone( |
| 644 const StartLEListenCallback& callback, | 635 const StartLEListenCallback& callback, |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 750 db.push_back(std::move(characteristic_element)); | 741 db.push_back(std::move(characteristic_element)); |
| 751 | 742 |
| 752 for (auto descriptor : characteristic->GetDescriptors()) { | 743 for (auto descriptor : characteristic->GetDescriptors()) { |
| 753 db.push_back(CreateGattDBElement<device::BluetoothRemoteGattDescriptor>( | 744 db.push_back(CreateGattDBElement<device::BluetoothRemoteGattDescriptor>( |
| 754 mojom::BluetoothGattDBAttributeType::BTGATT_DB_DESCRIPTOR, | 745 mojom::BluetoothGattDBAttributeType::BTGATT_DB_DESCRIPTOR, |
| 755 descriptor)); | 746 descriptor)); |
| 756 } | 747 } |
| 757 } | 748 } |
| 758 } | 749 } |
| 759 | 750 |
| 760 bluetooth_instance_->OnGetGattDB(std::move(remote_addr), std::move(db)); | 751 arc_bridge_service()->bluetooth_instance()->OnGetGattDB( |
| 752 std::move(remote_addr), std::move(db)); |
| 761 } | 753 } |
| 762 | 754 |
| 763 // Find Gatt Service/Characteristic/Descriptor from std::vector from UUID. | 755 // Find Gatt Service/Characteristic/Descriptor from std::vector from UUID. |
| 764 template <class T> | 756 template <class T> |
| 765 T* ArcBluetoothBridge::FindGattObjectFromUuid( | 757 T* ArcBluetoothBridge::FindGattObjectFromUuid( |
| 766 const std::vector<T*> gatt_objs, | 758 const std::vector<T*> gatt_objs, |
| 767 const device::BluetoothUUID uuid) const { | 759 const device::BluetoothUUID uuid) const { |
| 768 auto it = std::find_if(gatt_objs.begin(), gatt_objs.end(), | 760 auto it = std::find_if(gatt_objs.begin(), gatt_objs.end(), |
| 769 [&](T* obj) { return obj->GetUUID() == uuid; }); | 761 [&](T* obj) { return obj->GetUUID() == uuid; }); |
| 770 if (it == gatt_objs.end()) | 762 if (it == gatt_objs.end()) |
| (...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 998 } | 990 } |
| 999 | 991 |
| 1000 void ArcBluetoothBridge::OnDiscoveryError() { | 992 void ArcBluetoothBridge::OnDiscoveryError() { |
| 1001 LOG(WARNING) << "failed to change discovery state"; | 993 LOG(WARNING) << "failed to change discovery state"; |
| 1002 } | 994 } |
| 1003 | 995 |
| 1004 void ArcBluetoothBridge::OnPairing(mojom::BluetoothAddressPtr addr) const { | 996 void ArcBluetoothBridge::OnPairing(mojom::BluetoothAddressPtr addr) const { |
| 1005 if (!HasBluetoothInstance()) | 997 if (!HasBluetoothInstance()) |
| 1006 return; | 998 return; |
| 1007 | 999 |
| 1008 bluetooth_instance_->OnBondStateChanged(mojom::BluetoothStatus::SUCCESS, | 1000 arc_bridge_service()->bluetooth_instance()->OnBondStateChanged( |
| 1009 std::move(addr), | 1001 mojom::BluetoothStatus::SUCCESS, std::move(addr), |
| 1010 mojom::BluetoothBondState::BONDING); | 1002 mojom::BluetoothBondState::BONDING); |
| 1011 } | 1003 } |
| 1012 | 1004 |
| 1013 void ArcBluetoothBridge::OnPairedDone(mojom::BluetoothAddressPtr addr) const { | 1005 void ArcBluetoothBridge::OnPairedDone(mojom::BluetoothAddressPtr addr) const { |
| 1014 if (!HasBluetoothInstance()) | 1006 if (!HasBluetoothInstance()) |
| 1015 return; | 1007 return; |
| 1016 | 1008 |
| 1017 bluetooth_instance_->OnBondStateChanged(mojom::BluetoothStatus::SUCCESS, | 1009 arc_bridge_service()->bluetooth_instance()->OnBondStateChanged( |
| 1018 std::move(addr), | 1010 mojom::BluetoothStatus::SUCCESS, std::move(addr), |
| 1019 mojom::BluetoothBondState::BONDED); | 1011 mojom::BluetoothBondState::BONDED); |
| 1020 } | 1012 } |
| 1021 | 1013 |
| 1022 void ArcBluetoothBridge::OnPairedError( | 1014 void ArcBluetoothBridge::OnPairedError( |
| 1023 mojom::BluetoothAddressPtr addr, | 1015 mojom::BluetoothAddressPtr addr, |
| 1024 BluetoothDevice::ConnectErrorCode error_code) const { | 1016 BluetoothDevice::ConnectErrorCode error_code) const { |
| 1025 if (!HasBluetoothInstance()) | 1017 if (!HasBluetoothInstance()) |
| 1026 return; | 1018 return; |
| 1027 | 1019 |
| 1028 bluetooth_instance_->OnBondStateChanged(mojom::BluetoothStatus::FAIL, | 1020 arc_bridge_service()->bluetooth_instance()->OnBondStateChanged( |
| 1029 std::move(addr), | 1021 mojom::BluetoothStatus::FAIL, std::move(addr), |
| 1030 mojom::BluetoothBondState::NONE); | 1022 mojom::BluetoothBondState::NONE); |
| 1031 } | 1023 } |
| 1032 | 1024 |
| 1033 void ArcBluetoothBridge::OnForgetDone(mojom::BluetoothAddressPtr addr) const { | 1025 void ArcBluetoothBridge::OnForgetDone(mojom::BluetoothAddressPtr addr) const { |
| 1034 if (!HasBluetoothInstance()) | 1026 if (!HasBluetoothInstance()) |
| 1035 return; | 1027 return; |
| 1036 | 1028 |
| 1037 bluetooth_instance_->OnBondStateChanged(mojom::BluetoothStatus::SUCCESS, | 1029 arc_bridge_service()->bluetooth_instance()->OnBondStateChanged( |
| 1038 std::move(addr), | 1030 mojom::BluetoothStatus::SUCCESS, std::move(addr), |
| 1039 mojom::BluetoothBondState::NONE); | 1031 mojom::BluetoothBondState::NONE); |
| 1040 } | 1032 } |
| 1041 | 1033 |
| 1042 void ArcBluetoothBridge::OnForgetError(mojom::BluetoothAddressPtr addr) const { | 1034 void ArcBluetoothBridge::OnForgetError(mojom::BluetoothAddressPtr addr) const { |
| 1043 if (!HasBluetoothInstance()) | 1035 if (!HasBluetoothInstance()) |
| 1044 return; | 1036 return; |
| 1045 | 1037 |
| 1046 BluetoothDevice* device = | 1038 BluetoothDevice* device = |
| 1047 bluetooth_adapter_->GetDevice(addr->To<std::string>()); | 1039 bluetooth_adapter_->GetDevice(addr->To<std::string>()); |
| 1048 mojom::BluetoothBondState bond_state = mojom::BluetoothBondState::NONE; | 1040 mojom::BluetoothBondState bond_state = mojom::BluetoothBondState::NONE; |
| 1049 if (device && device->IsPaired()) { | 1041 if (device && device->IsPaired()) { |
| 1050 bond_state = mojom::BluetoothBondState::BONDED; | 1042 bond_state = mojom::BluetoothBondState::BONDED; |
| 1051 } | 1043 } |
| 1052 bluetooth_instance_->OnBondStateChanged(mojom::BluetoothStatus::FAIL, | 1044 arc_bridge_service()->bluetooth_instance()->OnBondStateChanged( |
| 1053 std::move(addr), bond_state); | 1045 mojom::BluetoothStatus::FAIL, std::move(addr), bond_state); |
| 1054 } | 1046 } |
| 1055 | 1047 |
| 1056 mojo::Array<mojom::BluetoothPropertyPtr> | 1048 mojo::Array<mojom::BluetoothPropertyPtr> |
| 1057 ArcBluetoothBridge::GetDeviceProperties(mojom::BluetoothPropertyType type, | 1049 ArcBluetoothBridge::GetDeviceProperties(mojom::BluetoothPropertyType type, |
| 1058 BluetoothDevice* device) const { | 1050 BluetoothDevice* device) const { |
| 1059 mojo::Array<mojom::BluetoothPropertyPtr> properties; | 1051 mojo::Array<mojom::BluetoothPropertyPtr> properties; |
| 1060 | 1052 |
| 1061 if (!device) { | 1053 if (!device) { |
| 1062 return properties; | 1054 return properties; |
| 1063 } | 1055 } |
| (...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1257 return; | 1249 return; |
| 1258 | 1250 |
| 1259 BluetoothAdapter::DeviceList devices = bluetooth_adapter_->GetDevices(); | 1251 BluetoothAdapter::DeviceList devices = bluetooth_adapter_->GetDevices(); |
| 1260 for (auto device : devices) { | 1252 for (auto device : devices) { |
| 1261 if (device->IsPaired()) | 1253 if (device->IsPaired()) |
| 1262 continue; | 1254 continue; |
| 1263 | 1255 |
| 1264 mojo::Array<mojom::BluetoothPropertyPtr> properties = | 1256 mojo::Array<mojom::BluetoothPropertyPtr> properties = |
| 1265 GetDeviceProperties(mojom::BluetoothPropertyType::ALL, device); | 1257 GetDeviceProperties(mojom::BluetoothPropertyType::ALL, device); |
| 1266 | 1258 |
| 1267 bluetooth_instance_->OnDeviceFound(std::move(properties)); | 1259 arc_bridge_service()->bluetooth_instance()->OnDeviceFound( |
| 1260 std::move(properties)); |
| 1268 | 1261 |
| 1269 if (bluetooth_version_ >= kMinBtleVersion) { | 1262 if (arc_bridge_service()->bluetooth_version() >= kMinBtleVersion) { |
| 1270 mojom::BluetoothAddressPtr addr = | 1263 mojom::BluetoothAddressPtr addr = |
| 1271 mojom::BluetoothAddress::From(device->GetAddress()); | 1264 mojom::BluetoothAddress::From(device->GetAddress()); |
| 1272 int rssi = device->GetInquiryRSSI(); | 1265 int rssi = device->GetInquiryRSSI(); |
| 1273 mojo::Array<mojom::BluetoothAdvertisingDataPtr> adv_data = | 1266 mojo::Array<mojom::BluetoothAdvertisingDataPtr> adv_data = |
| 1274 GetAdvertisingData(device); | 1267 GetAdvertisingData(device); |
| 1275 bluetooth_instance_->OnLEDeviceFound(std::move(addr), rssi, | 1268 arc_bridge_service()->bluetooth_instance()->OnLEDeviceFound( |
| 1276 std::move(adv_data)); | 1269 std::move(addr), rssi, std::move(adv_data)); |
| 1277 } | 1270 } |
| 1278 } | 1271 } |
| 1279 } | 1272 } |
| 1280 | 1273 |
| 1281 bool ArcBluetoothBridge::HasBluetoothInstance() const { | 1274 bool ArcBluetoothBridge::HasBluetoothInstance() const { |
| 1282 if (!bluetooth_instance_) { | 1275 if (!arc_bridge_service()->bluetooth_instance()) { |
| 1283 LOG(WARNING) << "no Bluetooth instance available"; | 1276 LOG(WARNING) << "no Bluetooth instance available"; |
| 1284 return false; | 1277 return false; |
| 1285 } | 1278 } |
| 1286 | 1279 |
| 1287 return true; | 1280 return true; |
| 1288 } | 1281 } |
| 1289 | 1282 |
| 1290 void ArcBluetoothBridge::SendCachedPairedDevices() const { | 1283 void ArcBluetoothBridge::SendCachedPairedDevices() const { |
| 1291 DCHECK(bluetooth_adapter_); | 1284 DCHECK(bluetooth_adapter_); |
| 1292 if (!HasBluetoothInstance()) | 1285 if (!HasBluetoothInstance()) |
| 1293 return; | 1286 return; |
| 1294 | 1287 |
| 1295 BluetoothAdapter::DeviceList devices = bluetooth_adapter_->GetDevices(); | 1288 BluetoothAdapter::DeviceList devices = bluetooth_adapter_->GetDevices(); |
| 1296 for (auto device : devices) { | 1289 for (auto device : devices) { |
| 1297 if (!device->IsPaired()) | 1290 if (!device->IsPaired()) |
| 1298 continue; | 1291 continue; |
| 1299 | 1292 |
| 1300 mojo::Array<mojom::BluetoothPropertyPtr> properties = | 1293 mojo::Array<mojom::BluetoothPropertyPtr> properties = |
| 1301 GetDeviceProperties(mojom::BluetoothPropertyType::ALL, device); | 1294 GetDeviceProperties(mojom::BluetoothPropertyType::ALL, device); |
| 1302 | 1295 |
| 1303 bluetooth_instance_->OnDeviceFound(std::move(properties)); | 1296 arc_bridge_service()->bluetooth_instance()->OnDeviceFound( |
| 1297 std::move(properties)); |
| 1304 | 1298 |
| 1305 mojom::BluetoothAddressPtr addr = | 1299 mojom::BluetoothAddressPtr addr = |
| 1306 mojom::BluetoothAddress::From(device->GetAddress()); | 1300 mojom::BluetoothAddress::From(device->GetAddress()); |
| 1307 | 1301 |
| 1308 if (bluetooth_version_ >= kMinBtleVersion) { | 1302 if (arc_bridge_service()->bluetooth_version() >= kMinBtleVersion) { |
| 1309 int rssi = device->GetInquiryRSSI(); | 1303 int rssi = device->GetInquiryRSSI(); |
| 1310 mojo::Array<mojom::BluetoothAdvertisingDataPtr> adv_data = | 1304 mojo::Array<mojom::BluetoothAdvertisingDataPtr> adv_data = |
| 1311 GetAdvertisingData(device); | 1305 GetAdvertisingData(device); |
| 1312 bluetooth_instance_->OnLEDeviceFound(addr->Clone(), rssi, | 1306 arc_bridge_service()->bluetooth_instance()->OnLEDeviceFound( |
| 1313 std::move(adv_data)); | 1307 addr->Clone(), rssi, std::move(adv_data)); |
| 1314 } | 1308 } |
| 1315 | 1309 |
| 1316 // OnBondStateChanged must be called with mojom::BluetoothBondState::BONDING | 1310 // OnBondStateChanged must be called with mojom::BluetoothBondState::BONDING |
| 1317 // to | 1311 // to |
| 1318 // make sure the bond state machine on Android is ready to take the | 1312 // make sure the bond state machine on Android is ready to take the |
| 1319 // pair-done event. Otherwise the pair-done event will be dropped as an | 1313 // pair-done event. Otherwise the pair-done event will be dropped as an |
| 1320 // invalid change of paired status. | 1314 // invalid change of paired status. |
| 1321 OnPairing(addr->Clone()); | 1315 OnPairing(addr->Clone()); |
| 1322 OnPairedDone(std::move(addr)); | 1316 OnPairedDone(std::move(addr)); |
| 1323 } | 1317 } |
| 1324 } | 1318 } |
| 1325 | 1319 |
| 1326 } // namespace arc | 1320 } // namespace arc |
| OLD | NEW |