Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(904)

Side by Side Diff: device/bluetooth/bluetooth_adapter_mac.mm

Issue 1538173003: Implementing GATT connection/disconnect on OS X. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixing the patch. Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
86 low_energy_central_manager_.reset([[aClass alloc] 86 low_energy_central_manager_.reset([[aClass alloc]
87 initWithDelegate:low_energy_central_manager_delegate_.get() 87 initWithDelegate:low_energy_central_manager_delegate_.get()
88 queue:dispatch_get_main_queue()]); 88 queue:dispatch_get_main_queue()]);
89 low_energy_discovery_manager_->SetCentralManager( 89 low_energy_discovery_manager_->SetCentralManager(
90 low_energy_central_manager_.get()); 90 low_energy_central_manager_.get());
91 } 91 }
92 DCHECK(classic_discovery_manager_.get()); 92 DCHECK(classic_discovery_manager_.get());
93 } 93 }
94 94
95 BluetoothAdapterMac::~BluetoothAdapterMac() { 95 BluetoothAdapterMac::~BluetoothAdapterMac() {
96 // Need to make sure the devices are destroyed before CBCentralManage is
msarda 2016/02/22 14:01:06 Side note: Note that simply clearing the devices v
jlebel 2016/02/24 16:54:54 Done.
97 // released.
98 devices_.clear();
96 } 99 }
97 100
98 std::string BluetoothAdapterMac::GetAddress() const { 101 std::string BluetoothAdapterMac::GetAddress() const {
99 return address_; 102 return address_;
100 } 103 }
101 104
102 std::string BluetoothAdapterMac::GetName() const { 105 std::string BluetoothAdapterMac::GetName() const {
103 return name_; 106 return name_;
104 } 107 }
105 108
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
215 DVLOG(1) << "Adapter registered a new connection from device with address: " 218 DVLOG(1) << "Adapter registered a new connection from device with address: "
216 << BluetoothClassicDeviceMac::GetDeviceAddress(device); 219 << BluetoothClassicDeviceMac::GetDeviceAddress(device);
217 ClassicDeviceAdded(device); 220 ClassicDeviceAdded(device);
218 } 221 }
219 222
220 // static 223 // static
221 bool BluetoothAdapterMac::IsLowEnergyAvailable() { 224 bool BluetoothAdapterMac::IsLowEnergyAvailable() {
222 return base::mac::IsOSYosemiteOrLater(); 225 return base::mac::IsOSYosemiteOrLater();
223 } 226 }
224 227
228 void BluetoothAdapterMac::RemovePairingDelegateInternal(
229 BluetoothDevice::PairingDelegate* pairing_delegate) {}
230
225 void BluetoothAdapterMac::SetCentralManagerForTesting( 231 void BluetoothAdapterMac::SetCentralManagerForTesting(
226 CBCentralManager* central_manager) { 232 CBCentralManager* central_manager) {
227 CHECK(BluetoothAdapterMac::IsLowEnergyAvailable()); 233 CHECK(BluetoothAdapterMac::IsLowEnergyAvailable());
228 [central_manager performSelector:@selector(setDelegate:) 234 [central_manager setDelegate:low_energy_central_manager_delegate_];
229 withObject:low_energy_central_manager_delegate_]; 235 low_energy_central_manager_.reset(central_manager,
230 low_energy_central_manager_.reset(central_manager); 236 base::scoped_policy::RETAIN);
231 low_energy_discovery_manager_->SetCentralManager( 237 low_energy_discovery_manager_->SetCentralManager(
232 low_energy_central_manager_.get()); 238 low_energy_central_manager_.get());
233 } 239 }
234 240
235 void BluetoothAdapterMac::RemovePairingDelegateInternal( 241 CBCentralManager* BluetoothAdapterMac::GetCentralManagerForTesting() {
236 BluetoothDevice::PairingDelegate* pairing_delegate) { 242 return low_energy_central_manager_;
237 } 243 }
238 244
239 void BluetoothAdapterMac::AddDiscoverySession( 245 void BluetoothAdapterMac::AddDiscoverySession(
240 BluetoothDiscoveryFilter* discovery_filter, 246 BluetoothDiscoveryFilter* discovery_filter,
241 const base::Closure& callback, 247 const base::Closure& callback,
242 const DiscoverySessionErrorCallback& error_callback) { 248 const DiscoverySessionErrorCallback& error_callback) {
243 DVLOG(1) << __func__; 249 DVLOG(1) << __func__;
244 if (num_discovery_sessions_ > 0) { 250 if (num_discovery_sessions_ > 0) {
245 DCHECK(IsDiscovering()); 251 DCHECK(IsDiscovering());
246 num_discovery_sessions_++; 252 num_discovery_sessions_++;
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after
449 BluetoothDevice* device_classic = new BluetoothClassicDeviceMac(this, device); 455 BluetoothDevice* device_classic = new BluetoothClassicDeviceMac(this, device);
450 devices_.set(device_address, make_scoped_ptr(device_classic)); 456 devices_.set(device_address, make_scoped_ptr(device_classic));
451 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, 457 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
452 DeviceAdded(this, device_classic)); 458 DeviceAdded(this, device_classic));
453 } 459 }
454 460
455 void BluetoothAdapterMac::LowEnergyDeviceUpdated( 461 void BluetoothAdapterMac::LowEnergyDeviceUpdated(
456 CBPeripheral* peripheral, 462 CBPeripheral* peripheral,
457 NSDictionary* advertisement_data, 463 NSDictionary* advertisement_data,
458 int rssi) { 464 int rssi) {
459 std::string device_address = 465 BluetoothLowEnergyDeviceMac* device_mac =
460 BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral); 466 GetBluetoothLowEnergyDeviceMac(peripheral);
461 // Try to find device from |devices_| with key |device_address|,
462 // if has no entry in the map, create new device and insert into |devices_|, 467 // if has no entry in the map, create new device and insert into |devices_|,
463 // otherwise update the existing device. 468 // otherwise update the existing device.
464 DevicesMap::const_iterator iter = devices_.find(device_address); 469 if (!device_mac) {
465 if (iter == devices_.end()) {
466 VLOG(1) << "LowEnergyDeviceUpdated new device"; 470 VLOG(1) << "LowEnergyDeviceUpdated new device";
467 // A new device has been found. 471 // A new device has been found.
468 BluetoothLowEnergyDeviceMac* device_mac = new BluetoothLowEnergyDeviceMac( 472 device_mac = new BluetoothLowEnergyDeviceMac(this, peripheral,
469 this, peripheral, advertisement_data, rssi); 473 advertisement_data, rssi);
474 std::string device_address =
475 BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral);
470 devices_.add(device_address, scoped_ptr<BluetoothDevice>(device_mac)); 476 devices_.add(device_address, scoped_ptr<BluetoothDevice>(device_mac));
471 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, 477 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
472 DeviceAdded(this, device_mac)); 478 DeviceAdded(this, device_mac));
473 return; 479 return;
474 } 480 }
475 481
476 BluetoothLowEnergyDeviceMac* device_mac =
477 static_cast<BluetoothLowEnergyDeviceMac*>(iter->second);
478 std::string stored_device_id = device_mac->GetIdentifier(); 482 std::string stored_device_id = device_mac->GetIdentifier();
479 std::string updated_device_id = 483 std::string updated_device_id =
480 BluetoothLowEnergyDeviceMac::GetPeripheralIdentifier(peripheral); 484 BluetoothLowEnergyDeviceMac::GetPeripheralIdentifier(peripheral);
481 if (stored_device_id != updated_device_id) { 485 if (stored_device_id != updated_device_id) {
482 VLOG(1) << "LowEnergyDeviceUpdated stored_device_id != updated_device_id: " 486 VLOG(1) << "LowEnergyDeviceUpdated stored_device_id != updated_device_id: "
483 << std::endl 487 << std::endl
484 << " " << stored_device_id << std::endl 488 << " " << stored_device_id << std::endl
485 << " " << updated_device_id; 489 << " " << updated_device_id;
486 // Collision, two identifiers map to the same hash address. With a 48 bit 490 // Collision, two identifiers map to the same hash address. With a 48 bit
487 // hash the probability of this occuring with 10,000 devices 491 // hash the probability of this occuring with 10,000 devices
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
530 } 534 }
531 } 535 }
532 536
533 void BluetoothAdapterMac::AddPairedDevices() { 537 void BluetoothAdapterMac::AddPairedDevices() {
534 // Add any new paired devices. 538 // Add any new paired devices.
535 for (IOBluetoothDevice* device in [IOBluetoothDevice pairedDevices]) { 539 for (IOBluetoothDevice* device in [IOBluetoothDevice pairedDevices]) {
536 ClassicDeviceAdded(device); 540 ClassicDeviceAdded(device);
537 } 541 }
538 } 542 }
539 543
544 void BluetoothAdapterMac::CreateGattConnection(
545 BluetoothLowEnergyDeviceMac* device_mac) {
546 [low_energy_central_manager_ connectPeripheral:device_mac->peripheral_
547 options:nil];
548 }
549
550 void BluetoothAdapterMac::DisconnectGatt(
551 BluetoothLowEnergyDeviceMac* device_mac) {
552 [low_energy_central_manager_
553 cancelPeripheralConnection:device_mac->peripheral_];
554 }
555
556 void BluetoothAdapterMac::DidConnectPeripheral(CBPeripheral* peripheral) {
557 BluetoothLowEnergyDeviceMac* device_mac =
558 GetBluetoothLowEnergyDeviceMac(peripheral);
559 if (!device_mac) {
560 [low_energy_central_manager_ cancelPeripheralConnection:peripheral];
561 return;
562 }
563 device_mac->DidConnectGatt();
564 }
565
566 void BluetoothAdapterMac::DidFailToConnectPeripheral(CBPeripheral* peripheral,
567 NSError* error) {
568 BluetoothLowEnergyDeviceMac* device_mac =
569 GetBluetoothLowEnergyDeviceMac(peripheral);
570 if (!device_mac) {
571 [low_energy_central_manager_ cancelPeripheralConnection:peripheral];
572 return;
573 }
574 VLOG(1) << "Bluetooth error, domain: " << error.domain.UTF8String
575 << ", error code: " << error.code;
576 // TODO(http://crbug.com/585894): Need to convert the error.
577 device_mac->DidFailToConnectGatt(BluetoothClassicDeviceMac::ERROR_UNKNOWN);
578 }
579
580 void BluetoothAdapterMac::DidDisconnectPeripheral(CBPeripheral* peripheral,
581 NSError* error) {
582 BluetoothLowEnergyDeviceMac* device_mac =
583 GetBluetoothLowEnergyDeviceMac(peripheral);
584 if (!device_mac) {
585 [low_energy_central_manager_ cancelPeripheralConnection:peripheral];
586 return;
587 }
588 // TODO(http://crbug.com/585897): Need to pass the error.
589 device_mac->DidDisconnectPeripheral();
590 }
591
592 BluetoothLowEnergyDeviceMac*
593 BluetoothAdapterMac::GetBluetoothLowEnergyDeviceMac(CBPeripheral* peripheral) {
594 std::string device_address =
595 BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral);
596 DevicesMap::const_iterator iter = devices_.find(device_address);
597 if (iter == devices_.end()) {
598 return nil;
599 }
600 return static_cast<BluetoothLowEnergyDeviceMac*>(iter->second);
601 }
602
540 } // namespace device 603 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698