| 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 | 9 |
| 10 #include <string> | 10 #include <string> |
| 11 | 11 |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
| 14 #include "base/containers/hash_tables.h" | 14 #include "base/containers/hash_tables.h" |
| 15 #include "base/location.h" | 15 #include "base/location.h" |
| 16 #include "base/mac/mac_util.h" | 16 #include "base/mac/mac_util.h" |
| 17 #include "base/mac/sdk_forward_declarations.h" | 17 #include "base/mac/sdk_forward_declarations.h" |
| 18 #include "base/memory/scoped_ptr.h" | 18 #include "base/memory/scoped_ptr.h" |
| 19 #include "base/profiler/scoped_tracker.h" | 19 #include "base/profiler/scoped_tracker.h" |
| 20 #include "base/sequenced_task_runner.h" | 20 #include "base/sequenced_task_runner.h" |
| 21 #include "base/single_thread_task_runner.h" | 21 #include "base/single_thread_task_runner.h" |
| 22 #include "base/strings/sys_string_conversions.h" | 22 #include "base/strings/sys_string_conversions.h" |
| 23 #include "base/thread_task_runner_handle.h" | 23 #include "base/thread_task_runner_handle.h" |
| 24 #include "base/time/time.h" | 24 #include "base/time/time.h" |
| 25 #include "device/bluetooth/bluetooth_classic_device_mac.h" | 25 #include "device/bluetooth/bluetooth_classic_device_mac.h" |
| 26 #include "device/bluetooth/bluetooth_discovery_session.h" | 26 #include "device/bluetooth/bluetooth_discovery_session.h" |
| 27 #include "device/bluetooth/bluetooth_gatt_connection_mac.h" |
| 27 #include "device/bluetooth/bluetooth_low_energy_central_manager_delegate.h" | 28 #include "device/bluetooth/bluetooth_low_energy_central_manager_delegate.h" |
| 28 #include "device/bluetooth/bluetooth_socket_mac.h" | 29 #include "device/bluetooth/bluetooth_socket_mac.h" |
| 29 #include "device/bluetooth/bluetooth_uuid.h" | 30 #include "device/bluetooth/bluetooth_uuid.h" |
| 30 | 31 |
| 31 namespace { | 32 namespace { |
| 32 | 33 |
| 33 // The frequency with which to poll the adapter for updates. | 34 // The frequency with which to poll the adapter for updates. |
| 34 const int kPollIntervalMs = 500; | 35 const int kPollIntervalMs = 500; |
| 35 | 36 |
| 36 } // namespace | 37 } // namespace |
| (...skipping 416 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 453 int rssi) { | 454 int rssi) { |
| 454 std::string device_address = | 455 std::string device_address = |
| 455 BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral); | 456 BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral); |
| 456 // Get a reference to the actual device pointer held by |devices_| (if | 457 // Get a reference to the actual device pointer held by |devices_| (if |
| 457 // |device_address| has no entry in the map a NULL pointer is created by the | 458 // |device_address| has no entry in the map a NULL pointer is created by the |
| 458 // std::map [] operator). | 459 // std::map [] operator). |
| 459 BluetoothDevice*& device_reference = devices_[device_address]; | 460 BluetoothDevice*& device_reference = devices_[device_address]; |
| 460 if (!device_reference) { | 461 if (!device_reference) { |
| 461 // A new device has been found. | 462 // A new device has been found. |
| 462 device_reference = | 463 device_reference = |
| 463 new BluetoothLowEnergyDeviceMac(peripheral, advertisement_data, rssi); | 464 new BluetoothLowEnergyDeviceMac(peripheral, advertisement_data, rssi, |
| 465 this, low_energy_central_manager_); |
| 464 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | 466 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, |
| 465 DeviceAdded(this, device_reference)); | 467 DeviceAdded(this, device_reference)); |
| 466 return; | 468 return; |
| 467 } | 469 } |
| 468 | 470 |
| 469 if (static_cast<BluetoothLowEnergyDeviceMac*>(device_reference) | 471 if (static_cast<BluetoothLowEnergyDeviceMac*>(device_reference) |
| 470 ->GetIdentifier() != | 472 ->GetIdentifier() != |
| 471 BluetoothLowEnergyDeviceMac::GetPeripheralIdentifier(peripheral)) { | 473 BluetoothLowEnergyDeviceMac::GetPeripheralIdentifier(peripheral)) { |
| 472 // Collision, two identifiers map to the same hash address. With a 48 bit | 474 // Collision, two identifiers map to the same hash address. With a 48 bit |
| 473 // hash the probability of this occuring with 10,000 devices | 475 // hash the probability of this occuring with 10,000 devices |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 515 } | 517 } |
| 516 } | 518 } |
| 517 | 519 |
| 518 void BluetoothAdapterMac::AddPairedDevices() { | 520 void BluetoothAdapterMac::AddPairedDevices() { |
| 519 // Add any new paired devices. | 521 // Add any new paired devices. |
| 520 for (IOBluetoothDevice* device in [IOBluetoothDevice pairedDevices]) { | 522 for (IOBluetoothDevice* device in [IOBluetoothDevice pairedDevices]) { |
| 521 ClassicDeviceAdded(device); | 523 ClassicDeviceAdded(device); |
| 522 } | 524 } |
| 523 } | 525 } |
| 524 | 526 |
| 527 void BluetoothAdapterMac::DidConnectPeripheral(CBPeripheral* peripheral) { |
| 528 std::string address = |
| 529 BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral); |
| 530 BluetoothLowEnergyDeviceMac* device = |
| 531 static_cast<BluetoothLowEnergyDeviceMac*>(devices_[address]); |
| 532 |
| 533 for (BluetoothDevice::GattConnectionCallback connect_success_callback : |
| 534 device->create_gatt_connection_success_callbacks_) { |
| 535 scoped_ptr<device::BluetoothGattConnection> connection( |
| 536 new BluetoothGattConnectionMac(this, GetAddress())); |
| 537 connect_success_callback.Run(connection.Pass()); |
| 538 } |
| 539 for (ErrorCallback disconnect_error_callback : |
| 540 device->disconnect_error_callbacks_) { |
| 541 disconnect_error_callback.Run(); |
| 542 } |
| 543 device->ClearConnectionCallbacks(); |
| 544 } |
| 545 |
| 546 void BluetoothAdapterMac::DidConnectPeripheralError(CBPeripheral* peripheral, |
| 547 NSError* error) { |
| 548 std::string address = |
| 549 BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral); |
| 550 BluetoothLowEnergyDeviceMac* device = |
| 551 static_cast<BluetoothLowEnergyDeviceMac*>(devices_[address]); |
| 552 |
| 553 for (BluetoothDevice::ConnectErrorCallback connect_error_callback : |
| 554 device->create_gatt_connection_error_callbacks_) { |
| 555 BluetoothDevice::ConnectErrorCode error_code = |
| 556 BluetoothDevice::ConnectErrorCode::ERROR_UNKNOWN; |
| 557 connect_error_callback.Run(error_code); |
| 558 } |
| 559 for (base::Closure disconnect_success_callback : |
| 560 device->disconnect_success_callbacks_) { |
| 561 disconnect_success_callback.Run(); |
| 562 } |
| 563 device->ClearConnectionCallbacks(); |
| 564 } |
| 565 |
| 566 void BluetoothAdapterMac::DidDisconnectPeripheral(CBPeripheral* peripheral, |
| 567 NSError* error) { |
| 568 std::string address = |
| 569 BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral); |
| 570 BluetoothLowEnergyDeviceMac* device = |
| 571 static_cast<BluetoothLowEnergyDeviceMac*>(devices_[address]); |
| 572 |
| 573 for (BluetoothDevice::ConnectErrorCallback connect_error_callback : |
| 574 device->create_gatt_connection_error_callbacks_) { |
| 575 BluetoothDevice::ConnectErrorCode error_code = |
| 576 BluetoothDevice::ConnectErrorCode::ERROR_UNKNOWN; |
| 577 connect_error_callback.Run(error_code); |
| 578 } |
| 579 for (base::Closure disconnect_success_callback : |
| 580 device->disconnect_success_callbacks_) { |
| 581 disconnect_success_callback.Run(); |
| 582 } |
| 583 device->ClearConnectionCallbacks(); |
| 584 } |
| 585 |
| 525 } // namespace device | 586 } // namespace device |
| OLD | NEW |