| 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/sdk_forward_declarations.h" | 16 #include "base/mac/sdk_forward_declarations.h" |
| 17 #include "base/memory/scoped_ptr.h" | 17 #include "base/memory/scoped_ptr.h" |
| 18 #include "base/profiler/scoped_tracker.h" | 18 #include "base/profiler/scoped_tracker.h" |
| 19 #include "base/sequenced_task_runner.h" | 19 #include "base/sequenced_task_runner.h" |
| 20 #include "base/single_thread_task_runner.h" | 20 #include "base/single_thread_task_runner.h" |
| 21 #include "base/strings/sys_string_conversions.h" | 21 #include "base/strings/sys_string_conversions.h" |
| 22 #include "base/thread_task_runner_handle.h" | 22 #include "base/thread_task_runner_handle.h" |
| 23 #include "base/time/time.h" | 23 #include "base/time/time.h" |
| 24 #include "device/bluetooth/bluetooth_device_mac.h" | 24 #include "device/bluetooth/bluetooth_classic_device_mac.h" |
| 25 #include "device/bluetooth/bluetooth_discovery_session.h" | 25 #include "device/bluetooth/bluetooth_discovery_session.h" |
| 26 #include "device/bluetooth/bluetooth_socket_mac.h" | 26 #include "device/bluetooth/bluetooth_socket_mac.h" |
| 27 #include "device/bluetooth/bluetooth_uuid.h" | 27 #include "device/bluetooth/bluetooth_uuid.h" |
| 28 | 28 |
| 29 namespace { | 29 namespace { |
| 30 | 30 |
| 31 // The frequency with which to poll the adapter for updates. | 31 // The frequency with which to poll the adapter for updates. |
| 32 const int kPollIntervalMs = 500; | 32 const int kPollIntervalMs = 500; |
| 33 | 33 |
| 34 // The length of time that must elapse since the last Inquiry response before a | 34 // The length of time that must elapse since the last Inquiry response before a |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 } | 164 } |
| 165 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, | 165 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, |
| 166 observers_, | 166 observers_, |
| 167 AdapterDiscoveringChanged(this, false)); | 167 AdapterDiscoveringChanged(this, false)); |
| 168 } | 168 } |
| 169 | 169 |
| 170 void BluetoothAdapterMac::DeviceConnected(IOBluetoothDevice* device) { | 170 void BluetoothAdapterMac::DeviceConnected(IOBluetoothDevice* device) { |
| 171 // TODO(isherman): Investigate whether this method can be replaced with a call | 171 // TODO(isherman): Investigate whether this method can be replaced with a call |
| 172 // to +registerForConnectNotifications:selector:. | 172 // to +registerForConnectNotifications:selector:. |
| 173 DVLOG(1) << "Adapter registered a new connection from device with address: " | 173 DVLOG(1) << "Adapter registered a new connection from device with address: " |
| 174 << BluetoothDeviceMac::GetDeviceAddress(device); | 174 << BluetoothClassicDeviceMac::GetDeviceAddress(device); |
| 175 ClassicDeviceAdded(device); | 175 ClassicDeviceAdded(device); |
| 176 } | 176 } |
| 177 | 177 |
| 178 void BluetoothAdapterMac::RemovePairingDelegateInternal( | 178 void BluetoothAdapterMac::RemovePairingDelegateInternal( |
| 179 BluetoothDevice::PairingDelegate* pairing_delegate) { | 179 BluetoothDevice::PairingDelegate* pairing_delegate) { |
| 180 } | 180 } |
| 181 | 181 |
| 182 void BluetoothAdapterMac::AddDiscoverySession( | 182 void BluetoothAdapterMac::AddDiscoverySession( |
| 183 BluetoothDiscoveryFilter* discovery_filter, | 183 BluetoothDiscoveryFilter* discovery_filter, |
| 184 const base::Closure& callback, | 184 const base::Closure& callback, |
| (...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 363 UpdateDevices(); | 363 UpdateDevices(); |
| 364 | 364 |
| 365 ui_task_runner_->PostDelayedTask( | 365 ui_task_runner_->PostDelayedTask( |
| 366 FROM_HERE, | 366 FROM_HERE, |
| 367 base::Bind(&BluetoothAdapterMac::PollAdapter, | 367 base::Bind(&BluetoothAdapterMac::PollAdapter, |
| 368 weak_ptr_factory_.GetWeakPtr()), | 368 weak_ptr_factory_.GetWeakPtr()), |
| 369 base::TimeDelta::FromMilliseconds(kPollIntervalMs)); | 369 base::TimeDelta::FromMilliseconds(kPollIntervalMs)); |
| 370 } | 370 } |
| 371 | 371 |
| 372 void BluetoothAdapterMac::ClassicDeviceAdded(IOBluetoothDevice* device) { | 372 void BluetoothAdapterMac::ClassicDeviceAdded(IOBluetoothDevice* device) { |
| 373 std::string device_address = BluetoothDeviceMac::GetDeviceAddress(device); | 373 std::string device_address = |
| 374 BluetoothClassicDeviceMac::GetDeviceAddress(device); |
| 374 | 375 |
| 375 // Only notify observers once per device. | 376 // Only notify observers once per device. |
| 376 if (devices_.count(device_address)) | 377 if (devices_.count(device_address)) |
| 377 return; | 378 return; |
| 378 | 379 |
| 379 devices_[device_address] = new BluetoothDeviceMac(device); | 380 devices_[device_address] = new BluetoothClassicDeviceMac(device); |
| 380 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, | 381 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, |
| 381 observers_, | 382 observers_, |
| 382 DeviceAdded(this, devices_[device_address])); | 383 DeviceAdded(this, devices_[device_address])); |
| 383 } | 384 } |
| 384 | 385 |
| 385 // TODO(krstnmnlsn): This method to be implemented as soon as UpdateDevices can | 386 // TODO(krstnmnlsn): This method to be implemented as soon as UpdateDevices can |
| 386 // handle instances of LowEnergyBluetoothDevice in |devices_|. crbug.com/498009 | 387 // handle instances of LowEnergyBluetoothDevice in |devices_|. crbug.com/498009 |
| 387 void BluetoothAdapterMac::LowEnergyDeviceUpdated( | 388 void BluetoothAdapterMac::LowEnergyDeviceUpdated( |
| 388 CBPeripheral* peripheral, | 389 CBPeripheral* peripheral, |
| 389 NSDictionary* advertisementData, | 390 NSDictionary* advertisementData, |
| 390 int rssi) { | 391 int rssi) { |
| 391 } | 392 } |
| 392 | 393 |
| 393 // TODO(krstnmnlsn): This method assumes all BluetoothDevices in devices_ are | 394 // TODO(krstnmnlsn): This method assumes all BluetoothDevices in devices_ are |
| 394 // instances of BluetoothDeviceMac. Add support for low energy devices. | 395 // instances of BluetoothDeviceMac. Add support for low energy devices. |
| 395 // crbug.com/498009 | 396 // crbug.com/498009 |
| 396 void BluetoothAdapterMac::UpdateDevices() { | 397 void BluetoothAdapterMac::UpdateDevices() { |
| 397 // Notify observers if any previously seen devices are no longer available, | 398 // Notify observers if any previously seen devices are no longer available, |
| 398 // i.e. if they are no longer paired, connected, nor recently discovered via | 399 // i.e. if they are no longer paired, connected, nor recently discovered via |
| 399 // an inquiry. | 400 // an inquiry. |
| 400 std::set<std::string> removed_devices; | 401 std::set<std::string> removed_devices; |
| 401 for (DevicesMap::iterator it = devices_.begin(); it != devices_.end(); ++it) { | 402 for (DevicesMap::iterator it = devices_.begin(); it != devices_.end(); ++it) { |
| 402 BluetoothDevice* device = it->second; | 403 BluetoothDevice* device = it->second; |
| 403 if (device->IsPaired() || device->IsConnected()) | 404 if (device->IsPaired() || device->IsConnected()) |
| 404 continue; | 405 continue; |
| 405 | 406 |
| 406 NSDate* last_inquiry_update = | 407 NSDate* last_inquiry_update = |
| 407 static_cast<BluetoothDeviceMac*>(device)->GetLastInquiryUpdate(); | 408 static_cast<BluetoothClassicDeviceMac*>(device)->GetLastInquiryUpdate(); |
| 408 if (last_inquiry_update && | 409 if (last_inquiry_update && |
| 409 -[last_inquiry_update timeIntervalSinceNow] < kDiscoveryTimeoutSec) | 410 -[last_inquiry_update timeIntervalSinceNow] < kDiscoveryTimeoutSec) |
| 410 continue; | 411 continue; |
| 411 | 412 |
| 412 FOR_EACH_OBSERVER( | 413 FOR_EACH_OBSERVER( |
| 413 BluetoothAdapter::Observer, observers_, DeviceRemoved(this, device)); | 414 BluetoothAdapter::Observer, observers_, DeviceRemoved(this, device)); |
| 414 delete device; | 415 delete device; |
| 415 removed_devices.insert(it->first); | 416 removed_devices.insert(it->first); |
| 416 // The device will be erased from the map in the loop immediately below. | 417 // The device will be erased from the map in the loop immediately below. |
| 417 } | 418 } |
| 418 for (const std::string& device_address : removed_devices) { | 419 for (const std::string& device_address : removed_devices) { |
| 419 size_t num_removed = devices_.erase(device_address); | 420 size_t num_removed = devices_.erase(device_address); |
| 420 DCHECK_EQ(num_removed, 1U); | 421 DCHECK_EQ(num_removed, 1U); |
| 421 } | 422 } |
| 422 | 423 |
| 423 // Add any new paired devices. | 424 // Add any new paired devices. |
| 424 for (IOBluetoothDevice* device in [IOBluetoothDevice pairedDevices]) { | 425 for (IOBluetoothDevice* device in [IOBluetoothDevice pairedDevices]) { |
| 425 ClassicDeviceAdded(device); | 426 ClassicDeviceAdded(device); |
| 426 } | 427 } |
| 427 } | 428 } |
| 428 | 429 |
| 429 } // namespace device | 430 } // namespace device |
| OLD | NEW |