| 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 484 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 495 BluetoothDevice* device_classic = GetDevice(device_address); | 495 BluetoothDevice* device_classic = GetDevice(device_address); |
| 496 | 496 |
| 497 // Only notify observers once per device. | 497 // Only notify observers once per device. |
| 498 if (device_classic != nullptr) { | 498 if (device_classic != nullptr) { |
| 499 VLOG(3) << "Updating classic device: " << device_classic->GetAddress(); | 499 VLOG(3) << "Updating classic device: " << device_classic->GetAddress(); |
| 500 device_classic->UpdateTimestamp(); | 500 device_classic->UpdateTimestamp(); |
| 501 return; | 501 return; |
| 502 } | 502 } |
| 503 | 503 |
| 504 device_classic = new BluetoothClassicDeviceMac(this, device); | 504 device_classic = new BluetoothClassicDeviceMac(this, device); |
| 505 devices_.set(device_address, base::WrapUnique(device_classic)); | 505 devices_[device_address] = base::WrapUnique(device_classic); |
| 506 VLOG(1) << "Adding new classic device: " << device_classic->GetAddress(); | 506 VLOG(1) << "Adding new classic device: " << device_classic->GetAddress(); |
| 507 | 507 |
| 508 for (auto& observer : observers_) | 508 for (auto& observer : observers_) |
| 509 observer.DeviceAdded(this, device_classic); | 509 observer.DeviceAdded(this, device_classic); |
| 510 } | 510 } |
| 511 | 511 |
| 512 void BluetoothAdapterMac::LowEnergyDeviceUpdated( | 512 void BluetoothAdapterMac::LowEnergyDeviceUpdated( |
| 513 CBPeripheral* peripheral, | 513 CBPeripheral* peripheral, |
| 514 NSDictionary* advertisement_data, | 514 NSDictionary* advertisement_data, |
| 515 int rssi) { | 515 int rssi) { |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 559 int8_t clamped_tx_power = BluetoothDevice::ClampPower([tx_power intValue]); | 559 int8_t clamped_tx_power = BluetoothDevice::ClampPower([tx_power intValue]); |
| 560 | 560 |
| 561 device_mac->UpdateAdvertisementData( | 561 device_mac->UpdateAdvertisementData( |
| 562 BluetoothDevice::ClampPower(rssi), std::move(advertised_uuids), | 562 BluetoothDevice::ClampPower(rssi), std::move(advertised_uuids), |
| 563 std::move(service_data_map), | 563 std::move(service_data_map), |
| 564 tx_power == nil ? nullptr : &clamped_tx_power); | 564 tx_power == nil ? nullptr : &clamped_tx_power); |
| 565 | 565 |
| 566 if (is_new_device) { | 566 if (is_new_device) { |
| 567 std::string device_address = | 567 std::string device_address = |
| 568 BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral); | 568 BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral); |
| 569 devices_.add(device_address, std::unique_ptr<BluetoothDevice>(device_mac)); | 569 devices_[device_address] = base::WrapUnique(device_mac); |
| 570 for (auto& observer : observers_) | 570 for (auto& observer : observers_) |
| 571 observer.DeviceAdded(this, device_mac); | 571 observer.DeviceAdded(this, device_mac); |
| 572 } else { | 572 } else { |
| 573 for (auto& observer : observers_) | 573 for (auto& observer : observers_) |
| 574 observer.DeviceChanged(this, device_mac); | 574 observer.DeviceChanged(this, device_mac); |
| 575 } | 575 } |
| 576 } | 576 } |
| 577 | 577 |
| 578 // TODO(krstnmnlsn): Implement. crbug.com/511025 | 578 // TODO(krstnmnlsn): Implement. crbug.com/511025 |
| 579 void BluetoothAdapterMac::LowEnergyCentralManagerUpdatedState() {} | 579 void BluetoothAdapterMac::LowEnergyCentralManagerUpdatedState() {} |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 613 GetBluetoothLowEnergyDeviceMac(peripheral); | 613 GetBluetoothLowEnergyDeviceMac(peripheral); |
| 614 const bool is_new_device = device_mac == nullptr; | 614 const bool is_new_device = device_mac == nullptr; |
| 615 | 615 |
| 616 if (!is_new_device && DoesCollideWithKnownDevice(peripheral, device_mac)) { | 616 if (!is_new_device && DoesCollideWithKnownDevice(peripheral, device_mac)) { |
| 617 continue; | 617 continue; |
| 618 } | 618 } |
| 619 if (is_new_device) { | 619 if (is_new_device) { |
| 620 device_mac = new BluetoothLowEnergyDeviceMac(this, peripheral); | 620 device_mac = new BluetoothLowEnergyDeviceMac(this, peripheral); |
| 621 std::string device_address = | 621 std::string device_address = |
| 622 BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral); | 622 BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral); |
| 623 devices_.add(device_address, | 623 devices_[device_address] = base::WrapUnique(device_mac); |
| 624 std::unique_ptr<BluetoothDevice>(device_mac)); | |
| 625 for (auto& observer : observers_) { | 624 for (auto& observer : observers_) { |
| 626 observer.DeviceAdded(this, device_mac); | 625 observer.DeviceAdded(this, device_mac); |
| 627 } | 626 } |
| 628 } | 627 } |
| 629 connected_devices.push_back(device_mac); | 628 connected_devices.push_back(device_mac); |
| 630 } | 629 } |
| 631 return connected_devices; | 630 return connected_devices; |
| 632 } | 631 } |
| 633 | 632 |
| 634 void BluetoothAdapterMac::CreateGattConnection( | 633 void BluetoothAdapterMac::CreateGattConnection( |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 687 VLOG(1) << "Bluetooth error, domain: " << error.domain.UTF8String | 686 VLOG(1) << "Bluetooth error, domain: " << error.domain.UTF8String |
| 688 << ", error code: " << error.code; | 687 << ", error code: " << error.code; |
| 689 } | 688 } |
| 690 device_mac->DidDisconnectPeripheral(error); | 689 device_mac->DidDisconnectPeripheral(error); |
| 691 } | 690 } |
| 692 | 691 |
| 693 BluetoothLowEnergyDeviceMac* | 692 BluetoothLowEnergyDeviceMac* |
| 694 BluetoothAdapterMac::GetBluetoothLowEnergyDeviceMac(CBPeripheral* peripheral) { | 693 BluetoothAdapterMac::GetBluetoothLowEnergyDeviceMac(CBPeripheral* peripheral) { |
| 695 std::string device_address = | 694 std::string device_address = |
| 696 BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral); | 695 BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral); |
| 697 DevicesMap::const_iterator iter = devices_.find(device_address); | 696 auto iter = devices_.find(device_address); |
| 698 if (iter == devices_.end()) { | 697 if (iter == devices_.end()) { |
| 699 return nil; | 698 return nil; |
| 700 } | 699 } |
| 701 return static_cast<BluetoothLowEnergyDeviceMac*>(iter->second); | 700 return static_cast<BluetoothLowEnergyDeviceMac*>(iter->second.get()); |
| 702 } | 701 } |
| 703 | 702 |
| 704 bool BluetoothAdapterMac::DoesCollideWithKnownDevice( | 703 bool BluetoothAdapterMac::DoesCollideWithKnownDevice( |
| 705 CBPeripheral* peripheral, | 704 CBPeripheral* peripheral, |
| 706 BluetoothLowEnergyDeviceMac* device_mac) { | 705 BluetoothLowEnergyDeviceMac* device_mac) { |
| 707 // Check that there are no collisions. | 706 // Check that there are no collisions. |
| 708 std::string stored_device_id = device_mac->GetIdentifier(); | 707 std::string stored_device_id = device_mac->GetIdentifier(); |
| 709 std::string updated_device_id = | 708 std::string updated_device_id = |
| 710 BluetoothLowEnergyDeviceMac::GetPeripheralIdentifier(peripheral); | 709 BluetoothLowEnergyDeviceMac::GetPeripheralIdentifier(peripheral); |
| 711 if (stored_device_id != updated_device_id) { | 710 if (stored_device_id != updated_device_id) { |
| 712 VLOG(1) << "LowEnergyDeviceUpdated stored_device_id != updated_device_id: " | 711 VLOG(1) << "LowEnergyDeviceUpdated stored_device_id != updated_device_id: " |
| 713 << std::endl | 712 << std::endl |
| 714 << " " << stored_device_id << std::endl | 713 << " " << stored_device_id << std::endl |
| 715 << " " << updated_device_id; | 714 << " " << updated_device_id; |
| 716 // Collision, two identifiers map to the same hash address. With a 48 bit | 715 // Collision, two identifiers map to the same hash address. With a 48 bit |
| 717 // hash the probability of this occuring with 10,000 devices | 716 // hash the probability of this occuring with 10,000 devices |
| 718 // simultaneously present is 1e-6 (see | 717 // simultaneously present is 1e-6 (see |
| 719 // https://en.wikipedia.org/wiki/Birthday_problem#Probability_table). We | 718 // https://en.wikipedia.org/wiki/Birthday_problem#Probability_table). We |
| 720 // ignore the second device by returning. | 719 // ignore the second device by returning. |
| 721 return true; | 720 return true; |
| 722 } | 721 } |
| 723 return false; | 722 return false; |
| 724 } | 723 } |
| 725 | 724 |
| 726 } // namespace device | 725 } // namespace device |
| OLD | NEW |