| 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 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 50 base::WeakPtr<BluetoothAdapterMac> BluetoothAdapterMac::CreateAdapter() { | 50 base::WeakPtr<BluetoothAdapterMac> BluetoothAdapterMac::CreateAdapter() { |
| 51 BluetoothAdapterMac* adapter = new BluetoothAdapterMac(); | 51 BluetoothAdapterMac* adapter = new BluetoothAdapterMac(); |
| 52 adapter->Init(); | 52 adapter->Init(); |
| 53 return adapter->weak_ptr_factory_.GetWeakPtr(); | 53 return adapter->weak_ptr_factory_.GetWeakPtr(); |
| 54 } | 54 } |
| 55 | 55 |
| 56 // static | 56 // static |
| 57 base::WeakPtr<BluetoothAdapterMac> BluetoothAdapterMac::CreateAdapterForTest( | 57 base::WeakPtr<BluetoothAdapterMac> BluetoothAdapterMac::CreateAdapterForTest( |
| 58 std::string name, | 58 std::string name, |
| 59 std::string address, | 59 std::string address, |
| 60 scoped_refptr<base::SequencedTaskRunner> ui_task_runner) { | 60 scoped_refptr<base::SequencedTaskRunner> ui_task_runner, |
| 61 CBCentralManager* central_manager) { |
| 61 BluetoothAdapterMac* adapter = new BluetoothAdapterMac(); | 62 BluetoothAdapterMac* adapter = new BluetoothAdapterMac(); |
| 63 if (central_manager) { |
| 64 adapter->SetCentralManagerForTesting(central_manager); |
| 65 } |
| 62 adapter->InitForTest(ui_task_runner); | 66 adapter->InitForTest(ui_task_runner); |
| 63 adapter->name_ = name; | 67 adapter->name_ = name; |
| 64 adapter->address_ = address; | 68 adapter->address_ = address; |
| 65 return adapter->weak_ptr_factory_.GetWeakPtr(); | 69 return adapter->weak_ptr_factory_.GetWeakPtr(); |
| 66 } | 70 } |
| 67 | 71 |
| 68 // static | 72 // static |
| 69 BluetoothUUID BluetoothAdapterMac::BluetoothUUIDWithCBUUID(CBUUID* uuid) { | 73 BluetoothUUID BluetoothAdapterMac::BluetoothUUIDWithCBUUID(CBUUID* uuid) { |
| 70 // UUIDString only available OS X >= 10.10. | 74 // UUIDString only available OS X >= 10.10. |
| 71 DCHECK(base::mac::IsAtLeastOS10_10()); | 75 DCHECK(base::mac::IsAtLeastOS10_10()); |
| (...skipping 300 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 372 // Begin a low energy discovery session or update it if one is already | 376 // Begin a low energy discovery session or update it if one is already |
| 373 // running. | 377 // running. |
| 374 if (IsLowEnergyAvailable()) | 378 if (IsLowEnergyAvailable()) |
| 375 low_energy_discovery_manager_->StartDiscovery( | 379 low_energy_discovery_manager_->StartDiscovery( |
| 376 BluetoothDevice::UUIDList()); | 380 BluetoothDevice::UUIDList()); |
| 377 } | 381 } |
| 378 return true; | 382 return true; |
| 379 } | 383 } |
| 380 | 384 |
| 381 void BluetoothAdapterMac::Init() { | 385 void BluetoothAdapterMac::Init() { |
| 386 if (IsLowEnergyAvailable()) |
| 387 AddConnectedLowEnergyDevices(); |
| 382 ui_task_runner_ = base::ThreadTaskRunnerHandle::Get(); | 388 ui_task_runner_ = base::ThreadTaskRunnerHandle::Get(); |
| 383 PollAdapter(); | 389 PollAdapter(); |
| 384 } | 390 } |
| 385 | 391 |
| 386 void BluetoothAdapterMac::InitForTest( | 392 void BluetoothAdapterMac::InitForTest( |
| 387 scoped_refptr<base::SequencedTaskRunner> ui_task_runner) { | 393 scoped_refptr<base::SequencedTaskRunner> ui_task_runner) { |
| 394 if (IsLowEnergyAvailable()) |
| 395 AddConnectedLowEnergyDevices(); |
| 388 ui_task_runner_ = ui_task_runner; | 396 ui_task_runner_ = ui_task_runner; |
| 389 } | 397 } |
| 390 | 398 |
| 391 void BluetoothAdapterMac::PollAdapter() { | 399 void BluetoothAdapterMac::PollAdapter() { |
| 392 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/461181 | 400 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/461181 |
| 393 // is fixed. | 401 // is fixed. |
| 394 tracked_objects::ScopedTracker tracking_profile1( | 402 tracked_objects::ScopedTracker tracking_profile1( |
| 395 FROM_HERE_WITH_EXPLICIT_FUNCTION( | 403 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 396 "461181 BluetoothAdapterMac::PollAdapter::Start")); | 404 "461181 BluetoothAdapterMac::PollAdapter::Start")); |
| 397 bool was_present = IsPresent(); | 405 bool was_present = IsPresent(); |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 448 // is fixed. | 456 // is fixed. |
| 449 tracked_objects::ScopedTracker tracking_profile5( | 457 tracked_objects::ScopedTracker tracking_profile5( |
| 450 FROM_HERE_WITH_EXPLICIT_FUNCTION( | 458 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 451 "461181 BluetoothAdapterMac::PollAdapter::RemoveTimedOutDevices")); | 459 "461181 BluetoothAdapterMac::PollAdapter::RemoveTimedOutDevices")); |
| 452 RemoveTimedOutDevices(); | 460 RemoveTimedOutDevices(); |
| 453 | 461 |
| 454 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/461181 | 462 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/461181 |
| 455 // is fixed. | 463 // is fixed. |
| 456 tracked_objects::ScopedTracker tracking_profile6( | 464 tracked_objects::ScopedTracker tracking_profile6( |
| 457 FROM_HERE_WITH_EXPLICIT_FUNCTION( | 465 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 458 "461181 BluetoothAdapterMac::PollAdapter::AddPairedDevices")); | 466 "461181 BluetoothAdapterMac::PollAdapter::AddPairedClassicDevices")); |
| 459 AddPairedDevices(); | 467 AddPairedClassicDevices(); |
| 460 | 468 |
| 461 ui_task_runner_->PostDelayedTask( | 469 ui_task_runner_->PostDelayedTask( |
| 462 FROM_HERE, | 470 FROM_HERE, |
| 463 base::Bind(&BluetoothAdapterMac::PollAdapter, | 471 base::Bind(&BluetoothAdapterMac::PollAdapter, |
| 464 weak_ptr_factory_.GetWeakPtr()), | 472 weak_ptr_factory_.GetWeakPtr()), |
| 465 base::TimeDelta::FromMilliseconds(kPollIntervalMs)); | 473 base::TimeDelta::FromMilliseconds(kPollIntervalMs)); |
| 466 } | 474 } |
| 467 | 475 |
| 468 void BluetoothAdapterMac::ClassicDeviceAdded(IOBluetoothDevice* device) { | 476 void BluetoothAdapterMac::ClassicDeviceAdded(IOBluetoothDevice* device) { |
| 469 std::string device_address = | 477 std::string device_address = |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 557 DeviceAdded(this, device_mac)); | 565 DeviceAdded(this, device_mac)); |
| 558 } else { | 566 } else { |
| 559 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | 567 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, |
| 560 DeviceChanged(this, device_mac)); | 568 DeviceChanged(this, device_mac)); |
| 561 } | 569 } |
| 562 } | 570 } |
| 563 | 571 |
| 564 // TODO(krstnmnlsn): Implement. crbug.com/511025 | 572 // TODO(krstnmnlsn): Implement. crbug.com/511025 |
| 565 void BluetoothAdapterMac::LowEnergyCentralManagerUpdatedState() {} | 573 void BluetoothAdapterMac::LowEnergyCentralManagerUpdatedState() {} |
| 566 | 574 |
| 567 void BluetoothAdapterMac::AddPairedDevices() { | 575 void BluetoothAdapterMac::AddPairedClassicDevices() { |
| 568 // Add any new paired devices. | |
| 569 for (IOBluetoothDevice* device in [IOBluetoothDevice pairedDevices]) { | 576 for (IOBluetoothDevice* device in [IOBluetoothDevice pairedDevices]) { |
| 570 ClassicDeviceAdded(device); | 577 IOBluetoothSDPServiceRecord* pnp_information = |
| 578 [device getServiceRecordForUUID: |
| 579 [IOBluetoothSDPUUID |
| 580 uuid16:kBluetoothSDPUUID16ServiceClassPnPInformation]]; |
| 581 |
| 582 // IOBluetoothDevice instance is a Bluetooth Classic device if it has a PNP |
| 583 // information service SDP entry. |
| 584 if (pnp_information) { |
| 585 ClassicDeviceAdded(device); |
| 586 } |
| 571 } | 587 } |
| 572 } | 588 } |
| 573 | 589 |
| 590 void BluetoothAdapterMac::AddConnectedLowEnergyDevices() { |
| 591 // Look for Device Information Service. BLE devices should implement it. |
| 592 CBUUID* device_information_uuid = [CBUUID UUIDWithString:@"180A"]; |
| 593 |
| 594 #pragma clang diagnostic push |
| 595 #pragma clang diagnostic ignored "-Wpartial-availability" |
| 596 // This list of devices contain both devices that are connected by other apps |
| 597 // or by system. In that case, user will need to connect peripheral using |
| 598 // CBCentralManager's connectPeripheral:options: method. |
| 599 NSArray* connected_peripherals = [low_energy_central_manager_ |
| 600 retrieveConnectedPeripheralsWithServices:@[ device_information_uuid ]]; |
| 601 #pragma clang diagnostic pop |
| 602 |
| 603 for (CBPeripheral* peripheral in connected_peripherals) { |
| 604 BluetoothLowEnergyDeviceMac* device_mac = |
| 605 GetBluetoothLowEnergyDeviceMac(peripheral); |
| 606 |
| 607 if (!device_mac) { |
| 608 device_mac = new BluetoothLowEnergyDeviceMac(this, peripheral); |
| 609 std::string device_address = |
| 610 BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral); |
| 611 devices_.add(device_address, |
| 612 std::unique_ptr<BluetoothDevice>(device_mac)); |
| 613 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, |
| 614 DeviceAdded(this, device_mac)); |
| 615 } |
| 616 } |
| 617 } |
| 618 |
| 574 void BluetoothAdapterMac::CreateGattConnection( | 619 void BluetoothAdapterMac::CreateGattConnection( |
| 575 BluetoothLowEnergyDeviceMac* device_mac) { | 620 BluetoothLowEnergyDeviceMac* device_mac) { |
| 576 [low_energy_central_manager_ connectPeripheral:device_mac->peripheral_ | 621 [low_energy_central_manager_ connectPeripheral:device_mac->peripheral_ |
| 577 options:nil]; | 622 options:nil]; |
| 578 } | 623 } |
| 579 | 624 |
| 580 void BluetoothAdapterMac::DisconnectGatt( | 625 void BluetoothAdapterMac::DisconnectGatt( |
| 581 BluetoothLowEnergyDeviceMac* device_mac) { | 626 BluetoothLowEnergyDeviceMac* device_mac) { |
| 582 [low_energy_central_manager_ | 627 [low_energy_central_manager_ |
| 583 cancelPeripheralConnection:device_mac->peripheral_]; | 628 cancelPeripheralConnection:device_mac->peripheral_]; |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 635 std::string device_address = | 680 std::string device_address = |
| 636 BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral); | 681 BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral); |
| 637 DevicesMap::const_iterator iter = devices_.find(device_address); | 682 DevicesMap::const_iterator iter = devices_.find(device_address); |
| 638 if (iter == devices_.end()) { | 683 if (iter == devices_.end()) { |
| 639 return nil; | 684 return nil; |
| 640 } | 685 } |
| 641 return static_cast<BluetoothLowEnergyDeviceMac*>(iter->second); | 686 return static_cast<BluetoothLowEnergyDeviceMac*>(iter->second); |
| 642 } | 687 } |
| 643 | 688 |
| 644 } // namespace device | 689 } // namespace device |
| OLD | NEW |