Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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_adapter_mac.h" | 5 #include "device/bluetooth/bluetooth_adapter_mac.h" |
| 6 | 6 |
| 7 #import <IOBluetooth/objc/IOBluetoothDevice.h> | 7 #import <IOBluetooth/objc/IOBluetoothDevice.h> |
| 8 #import <IOBluetooth/objc/IOBluetoothHostController.h> | 8 #import <IOBluetooth/objc/IOBluetoothHostController.h> |
| 9 #include <stddef.h> | 9 #include <stddef.h> |
| 10 | 10 |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 85 low_energy_central_manager_.reset([[CBCentralManager alloc] | 85 low_energy_central_manager_.reset([[CBCentralManager alloc] |
| 86 initWithDelegate:low_energy_central_manager_delegate_.get() | 86 initWithDelegate:low_energy_central_manager_delegate_.get() |
| 87 queue:dispatch_get_main_queue()]); | 87 queue:dispatch_get_main_queue()]); |
| 88 low_energy_discovery_manager_->SetCentralManager( | 88 low_energy_discovery_manager_->SetCentralManager( |
| 89 low_energy_central_manager_.get()); | 89 low_energy_central_manager_.get()); |
| 90 } | 90 } |
| 91 DCHECK(classic_discovery_manager_.get()); | 91 DCHECK(classic_discovery_manager_.get()); |
| 92 } | 92 } |
| 93 | 93 |
| 94 BluetoothAdapterMac::~BluetoothAdapterMac() { | 94 BluetoothAdapterMac::~BluetoothAdapterMac() { |
| 95 // When devices will be destroyed, they will need this current instance to | |
| 96 // disconnect the gatt connection. To make sure they don't use the mac | |
| 97 // adapter, they should be explicitly disconnected. | |
| 98 for (DevicesMap::const_iterator it = devices_.begin(); it != devices_.end(); | |
| 99 ++it) { | |
| 100 BluetoothDevice* device = it->second; | |
| 101 if (device->IsGattConnected()) { | |
| 102 BluetoothLowEnergyDeviceMac* ble_device = | |
| 103 static_cast<BluetoothLowEnergyDeviceMac*>(device); | |
| 104 ble_device->DisconnectGatt(); | |
| 105 } | |
| 106 } | |
| 107 // Set low_energy_central_manager_ to nil so no devices will try to use | |
|
scheib
2016/03/01 17:38:17
to use it
^^
is owned by
^^
jlebel
2016/03/01 21:44:44
Done.
| |
| 108 // while being destroyed after this method. |devices_| is own by | |
| 109 // BluetoothAdapter. | |
| 110 low_energy_central_manager_.reset(); | |
| 95 } | 111 } |
| 96 | 112 |
| 97 std::string BluetoothAdapterMac::GetAddress() const { | 113 std::string BluetoothAdapterMac::GetAddress() const { |
| 98 return address_; | 114 return address_; |
| 99 } | 115 } |
| 100 | 116 |
| 101 std::string BluetoothAdapterMac::GetName() const { | 117 std::string BluetoothAdapterMac::GetName() const { |
| 102 return name_; | 118 return name_; |
| 103 } | 119 } |
| 104 | 120 |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 214 DVLOG(1) << "Adapter registered a new connection from device with address: " | 230 DVLOG(1) << "Adapter registered a new connection from device with address: " |
| 215 << BluetoothClassicDeviceMac::GetDeviceAddress(device); | 231 << BluetoothClassicDeviceMac::GetDeviceAddress(device); |
| 216 ClassicDeviceAdded(device); | 232 ClassicDeviceAdded(device); |
| 217 } | 233 } |
| 218 | 234 |
| 219 // static | 235 // static |
| 220 bool BluetoothAdapterMac::IsLowEnergyAvailable() { | 236 bool BluetoothAdapterMac::IsLowEnergyAvailable() { |
| 221 return base::mac::IsOSYosemiteOrLater(); | 237 return base::mac::IsOSYosemiteOrLater(); |
| 222 } | 238 } |
| 223 | 239 |
| 240 void BluetoothAdapterMac::RemovePairingDelegateInternal( | |
| 241 BluetoothDevice::PairingDelegate* pairing_delegate) {} | |
| 242 | |
| 224 void BluetoothAdapterMac::SetCentralManagerForTesting( | 243 void BluetoothAdapterMac::SetCentralManagerForTesting( |
| 225 CBCentralManager* central_manager) { | 244 CBCentralManager* central_manager) { |
| 226 CHECK(BluetoothAdapterMac::IsLowEnergyAvailable()); | 245 CHECK(BluetoothAdapterMac::IsLowEnergyAvailable()); |
| 227 central_manager.delegate = low_energy_central_manager_delegate_; | 246 central_manager.delegate = low_energy_central_manager_delegate_; |
| 228 low_energy_central_manager_.reset(central_manager); | 247 low_energy_central_manager_.reset(central_manager, |
| 248 base::scoped_policy::RETAIN); | |
| 229 low_energy_discovery_manager_->SetCentralManager( | 249 low_energy_discovery_manager_->SetCentralManager( |
| 230 low_energy_central_manager_.get()); | 250 low_energy_central_manager_.get()); |
| 231 } | 251 } |
| 232 | 252 |
| 233 void BluetoothAdapterMac::RemovePairingDelegateInternal( | 253 CBCentralManager* BluetoothAdapterMac::GetCentralManagerForTesting() { |
| 234 BluetoothDevice::PairingDelegate* pairing_delegate) { | 254 return low_energy_central_manager_; |
| 235 } | 255 } |
| 236 | 256 |
| 237 void BluetoothAdapterMac::AddDiscoverySession( | 257 void BluetoothAdapterMac::AddDiscoverySession( |
| 238 BluetoothDiscoveryFilter* discovery_filter, | 258 BluetoothDiscoveryFilter* discovery_filter, |
| 239 const base::Closure& callback, | 259 const base::Closure& callback, |
| 240 const DiscoverySessionErrorCallback& error_callback) { | 260 const DiscoverySessionErrorCallback& error_callback) { |
| 241 DVLOG(1) << __func__; | 261 DVLOG(1) << __func__; |
| 242 if (num_discovery_sessions_ > 0) { | 262 if (num_discovery_sessions_ > 0) { |
| 243 DCHECK(IsDiscovering()); | 263 DCHECK(IsDiscovering()); |
| 244 num_discovery_sessions_++; | 264 num_discovery_sessions_++; |
| (...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 447 BluetoothDevice* device_classic = new BluetoothClassicDeviceMac(this, device); | 467 BluetoothDevice* device_classic = new BluetoothClassicDeviceMac(this, device); |
| 448 devices_.set(device_address, make_scoped_ptr(device_classic)); | 468 devices_.set(device_address, make_scoped_ptr(device_classic)); |
| 449 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | 469 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, |
| 450 DeviceAdded(this, device_classic)); | 470 DeviceAdded(this, device_classic)); |
| 451 } | 471 } |
| 452 | 472 |
| 453 void BluetoothAdapterMac::LowEnergyDeviceUpdated( | 473 void BluetoothAdapterMac::LowEnergyDeviceUpdated( |
| 454 CBPeripheral* peripheral, | 474 CBPeripheral* peripheral, |
| 455 NSDictionary* advertisement_data, | 475 NSDictionary* advertisement_data, |
| 456 int rssi) { | 476 int rssi) { |
| 457 std::string device_address = | 477 BluetoothLowEnergyDeviceMac* device_mac = |
| 458 BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral); | 478 GetBluetoothLowEnergyDeviceMac(peripheral); |
| 459 // Try to find device from |devices_| with key |device_address|, | |
| 460 // if has no entry in the map, create new device and insert into |devices_|, | 479 // if has no entry in the map, create new device and insert into |devices_|, |
| 461 // otherwise update the existing device. | 480 // otherwise update the existing device. |
| 462 DevicesMap::const_iterator iter = devices_.find(device_address); | 481 if (!device_mac) { |
| 463 if (iter == devices_.end()) { | |
| 464 VLOG(1) << "LowEnergyDeviceUpdated new device"; | 482 VLOG(1) << "LowEnergyDeviceUpdated new device"; |
| 465 // A new device has been found. | 483 // A new device has been found. |
| 466 BluetoothLowEnergyDeviceMac* device_mac = new BluetoothLowEnergyDeviceMac( | 484 device_mac = new BluetoothLowEnergyDeviceMac(this, peripheral, |
| 467 this, peripheral, advertisement_data, rssi); | 485 advertisement_data, rssi); |
| 486 std::string device_address = | |
| 487 BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral); | |
| 468 devices_.add(device_address, scoped_ptr<BluetoothDevice>(device_mac)); | 488 devices_.add(device_address, scoped_ptr<BluetoothDevice>(device_mac)); |
| 469 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | 489 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, |
| 470 DeviceAdded(this, device_mac)); | 490 DeviceAdded(this, device_mac)); |
| 471 return; | 491 return; |
| 472 } | 492 } |
| 473 | 493 |
| 474 BluetoothLowEnergyDeviceMac* device_mac = | |
| 475 static_cast<BluetoothLowEnergyDeviceMac*>(iter->second); | |
| 476 std::string stored_device_id = device_mac->GetIdentifier(); | 494 std::string stored_device_id = device_mac->GetIdentifier(); |
| 477 std::string updated_device_id = | 495 std::string updated_device_id = |
| 478 BluetoothLowEnergyDeviceMac::GetPeripheralIdentifier(peripheral); | 496 BluetoothLowEnergyDeviceMac::GetPeripheralIdentifier(peripheral); |
| 479 if (stored_device_id != updated_device_id) { | 497 if (stored_device_id != updated_device_id) { |
| 480 VLOG(1) << "LowEnergyDeviceUpdated stored_device_id != updated_device_id: " | 498 VLOG(1) << "LowEnergyDeviceUpdated stored_device_id != updated_device_id: " |
| 481 << std::endl | 499 << std::endl |
| 482 << " " << stored_device_id << std::endl | 500 << " " << stored_device_id << std::endl |
| 483 << " " << updated_device_id; | 501 << " " << updated_device_id; |
| 484 // Collision, two identifiers map to the same hash address. With a 48 bit | 502 // Collision, two identifiers map to the same hash address. With a 48 bit |
| 485 // hash the probability of this occuring with 10,000 devices | 503 // hash the probability of this occuring with 10,000 devices |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 528 } | 546 } |
| 529 } | 547 } |
| 530 | 548 |
| 531 void BluetoothAdapterMac::AddPairedDevices() { | 549 void BluetoothAdapterMac::AddPairedDevices() { |
| 532 // Add any new paired devices. | 550 // Add any new paired devices. |
| 533 for (IOBluetoothDevice* device in [IOBluetoothDevice pairedDevices]) { | 551 for (IOBluetoothDevice* device in [IOBluetoothDevice pairedDevices]) { |
| 534 ClassicDeviceAdded(device); | 552 ClassicDeviceAdded(device); |
| 535 } | 553 } |
| 536 } | 554 } |
| 537 | 555 |
| 556 void BluetoothAdapterMac::CreateGattConnection( | |
| 557 BluetoothLowEnergyDeviceMac* device_mac) { | |
| 558 [low_energy_central_manager_ connectPeripheral:device_mac->peripheral_ | |
| 559 options:nil]; | |
| 560 } | |
| 561 | |
| 562 void BluetoothAdapterMac::DisconnectGatt( | |
| 563 BluetoothLowEnergyDeviceMac* device_mac) { | |
| 564 [low_energy_central_manager_ | |
| 565 cancelPeripheralConnection:device_mac->peripheral_]; | |
| 566 } | |
| 567 | |
| 568 void BluetoothAdapterMac::DidConnectPeripheral(CBPeripheral* peripheral) { | |
| 569 BluetoothLowEnergyDeviceMac* device_mac = | |
| 570 GetBluetoothLowEnergyDeviceMac(peripheral); | |
| 571 if (!device_mac) { | |
| 572 [low_energy_central_manager_ cancelPeripheralConnection:peripheral]; | |
| 573 return; | |
| 574 } | |
| 575 device_mac->DidConnectGatt(); | |
| 576 } | |
| 577 | |
| 578 void BluetoothAdapterMac::DidFailToConnectPeripheral(CBPeripheral* peripheral, | |
| 579 NSError* error) { | |
| 580 BluetoothLowEnergyDeviceMac* device_mac = | |
| 581 GetBluetoothLowEnergyDeviceMac(peripheral); | |
| 582 if (!device_mac) { | |
| 583 [low_energy_central_manager_ cancelPeripheralConnection:peripheral]; | |
| 584 return; | |
| 585 } | |
| 586 VLOG(1) << "Bluetooth error, domain: " << error.domain.UTF8String | |
| 587 << ", error code: " << error.code; | |
| 588 // TODO(http://crbug.com/585894): Need to convert the error. | |
| 589 device_mac->DidFailToConnectGatt(BluetoothClassicDeviceMac::ERROR_UNKNOWN); | |
| 590 } | |
| 591 | |
| 592 void BluetoothAdapterMac::DidDisconnectPeripheral(CBPeripheral* peripheral, | |
| 593 NSError* error) { | |
| 594 BluetoothLowEnergyDeviceMac* device_mac = | |
| 595 GetBluetoothLowEnergyDeviceMac(peripheral); | |
| 596 if (!device_mac) { | |
| 597 [low_energy_central_manager_ cancelPeripheralConnection:peripheral]; | |
| 598 return; | |
| 599 } | |
| 600 // TODO(http://crbug.com/585897): Need to pass the error. | |
| 601 device_mac->DidDisconnectPeripheral(); | |
| 602 } | |
| 603 | |
| 604 BluetoothLowEnergyDeviceMac* | |
| 605 BluetoothAdapterMac::GetBluetoothLowEnergyDeviceMac(CBPeripheral* peripheral) { | |
| 606 std::string device_address = | |
| 607 BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral); | |
| 608 DevicesMap::const_iterator iter = devices_.find(device_address); | |
| 609 if (iter == devices_.end()) { | |
| 610 return nil; | |
| 611 } | |
| 612 return static_cast<BluetoothLowEnergyDeviceMac*>(iter->second); | |
| 613 } | |
| 614 | |
| 538 } // namespace device | 615 } // namespace device |
| OLD | NEW |