Chromium Code Reviews| 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)); | |
| 506 VLOG(1) << "Adding new classic device: " << device_classic->GetAddress(); | 505 VLOG(1) << "Adding new classic device: " << device_classic->GetAddress(); |
| 506 auto insertion = devices_.insert( | |
| 507 std::make_pair(device_address, std::move(device_classic))); | |
| 508 if (!insertion.second) { | |
| 509 VLOG(1) << "Insertion of device_classic failed."; | |
| 510 return; | |
| 511 } | |
| 507 | 512 |
| 508 for (auto& observer : observers_) | 513 for (auto& observer : observers_) |
| 509 observer.DeviceAdded(this, device_classic); | 514 observer.DeviceAdded(this, insertion.first->second.get()); |
| 510 } | 515 } |
| 511 | 516 |
| 512 void BluetoothAdapterMac::LowEnergyDeviceUpdated( | 517 void BluetoothAdapterMac::LowEnergyDeviceUpdated( |
| 513 CBPeripheral* peripheral, | 518 CBPeripheral* peripheral, |
| 514 NSDictionary* advertisement_data, | 519 NSDictionary* advertisement_data, |
| 515 int rssi) { | 520 int rssi) { |
| 516 BluetoothLowEnergyDeviceMac* device_mac = | 521 BluetoothLowEnergyDeviceMac* device_mac = |
| 517 GetBluetoothLowEnergyDeviceMac(peripheral); | 522 GetBluetoothLowEnergyDeviceMac(peripheral); |
| 518 // If has no entry in the map, create new device and insert into |devices_|, | 523 // If has no entry in the map, create new device and insert into |devices_|, |
| 519 // otherwise update the existing device. | 524 // otherwise update the existing device. |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 559 int8_t clamped_tx_power = BluetoothDevice::ClampPower([tx_power intValue]); | 564 int8_t clamped_tx_power = BluetoothDevice::ClampPower([tx_power intValue]); |
| 560 | 565 |
| 561 device_mac->UpdateAdvertisementData( | 566 device_mac->UpdateAdvertisementData( |
| 562 BluetoothDevice::ClampPower(rssi), std::move(advertised_uuids), | 567 BluetoothDevice::ClampPower(rssi), std::move(advertised_uuids), |
| 563 std::move(service_data_map), | 568 std::move(service_data_map), |
| 564 tx_power == nil ? nullptr : &clamped_tx_power); | 569 tx_power == nil ? nullptr : &clamped_tx_power); |
| 565 | 570 |
| 566 if (is_new_device) { | 571 if (is_new_device) { |
| 567 std::string device_address = | 572 std::string device_address = |
| 568 BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral); | 573 BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral); |
| 569 devices_.add(device_address, std::unique_ptr<BluetoothDevice>(device_mac)); | 574 auto insertion = |
| 575 devices_.insert(std::make_pair(device_address, std::move(device_mac))); | |
| 576 | |
| 577 if (!insertion.second) { | |
| 578 VLOG(1) << "Insertion of device_mac failed."; | |
| 579 return; | |
| 580 } | |
| 570 for (auto& observer : observers_) | 581 for (auto& observer : observers_) |
| 571 observer.DeviceAdded(this, device_mac); | 582 observer.DeviceAdded(this, insertion.first->second.get()); |
| 572 } else { | 583 } else { |
| 573 for (auto& observer : observers_) | 584 for (auto& observer : observers_) |
| 574 observer.DeviceChanged(this, device_mac); | 585 observer.DeviceChanged(this, device_mac); |
| 575 } | 586 } |
| 576 } | 587 } |
| 577 | 588 |
| 578 // TODO(krstnmnlsn): Implement. crbug.com/511025 | 589 // TODO(krstnmnlsn): Implement. crbug.com/511025 |
| 579 void BluetoothAdapterMac::LowEnergyCentralManagerUpdatedState() {} | 590 void BluetoothAdapterMac::LowEnergyCentralManagerUpdatedState() {} |
| 580 | 591 |
| 581 void BluetoothAdapterMac::AddPairedDevices() { | 592 void BluetoothAdapterMac::AddPairedDevices() { |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 613 GetBluetoothLowEnergyDeviceMac(peripheral); | 624 GetBluetoothLowEnergyDeviceMac(peripheral); |
| 614 const bool is_new_device = device_mac == nullptr; | 625 const bool is_new_device = device_mac == nullptr; |
| 615 | 626 |
| 616 if (!is_new_device && DoesCollideWithKnownDevice(peripheral, device_mac)) { | 627 if (!is_new_device && DoesCollideWithKnownDevice(peripheral, device_mac)) { |
| 617 continue; | 628 continue; |
| 618 } | 629 } |
| 619 if (is_new_device) { | 630 if (is_new_device) { |
| 620 device_mac = new BluetoothLowEnergyDeviceMac(this, peripheral); | 631 device_mac = new BluetoothLowEnergyDeviceMac(this, peripheral); |
| 621 std::string device_address = | 632 std::string device_address = |
| 622 BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral); | 633 BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral); |
| 623 devices_.add(device_address, | 634 devices_.insert(std::make_pair(device_address, std::move(device_mac))); |
| 624 std::unique_ptr<BluetoothDevice>(device_mac)); | 635 |
| 636 // this will fail because device_mac is used. Might need to be shared | |
| 637 // because it's in two arrays | |
|
Reilly Grant (use Gerrit)
2016/12/21 22:25:13
This looks like an unfinished TODO in your patch.
dougt
2016/12/22 01:18:02
Done.
Reilly Grant (use Gerrit)
2016/12/22 22:00:52
Do the same insertion check here you do in bluetoo
| |
| 625 for (auto& observer : observers_) { | 638 for (auto& observer : observers_) { |
| 626 observer.DeviceAdded(this, device_mac); | 639 observer.DeviceAdded(this, device_mac); |
| 627 } | 640 } |
| 628 } | 641 } |
| 629 connected_devices.push_back(device_mac); | 642 connected_devices.push_back(device_mac); |
| 630 } | 643 } |
| 631 return connected_devices; | 644 return connected_devices; |
| 632 } | 645 } |
| 633 | 646 |
| 634 void BluetoothAdapterMac::CreateGattConnection( | 647 void BluetoothAdapterMac::CreateGattConnection( |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 691 } | 704 } |
| 692 | 705 |
| 693 BluetoothLowEnergyDeviceMac* | 706 BluetoothLowEnergyDeviceMac* |
| 694 BluetoothAdapterMac::GetBluetoothLowEnergyDeviceMac(CBPeripheral* peripheral) { | 707 BluetoothAdapterMac::GetBluetoothLowEnergyDeviceMac(CBPeripheral* peripheral) { |
| 695 std::string device_address = | 708 std::string device_address = |
| 696 BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral); | 709 BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral); |
| 697 DevicesMap::const_iterator iter = devices_.find(device_address); | 710 DevicesMap::const_iterator iter = devices_.find(device_address); |
| 698 if (iter == devices_.end()) { | 711 if (iter == devices_.end()) { |
| 699 return nil; | 712 return nil; |
| 700 } | 713 } |
| 701 return static_cast<BluetoothLowEnergyDeviceMac*>(iter->second); | 714 return static_cast<BluetoothLowEnergyDeviceMac*>(iter->second.get()); |
| 702 } | 715 } |
| 703 | 716 |
| 704 bool BluetoothAdapterMac::DoesCollideWithKnownDevice( | 717 bool BluetoothAdapterMac::DoesCollideWithKnownDevice( |
| 705 CBPeripheral* peripheral, | 718 CBPeripheral* peripheral, |
| 706 BluetoothLowEnergyDeviceMac* device_mac) { | 719 BluetoothLowEnergyDeviceMac* device_mac) { |
| 707 // Check that there are no collisions. | 720 // Check that there are no collisions. |
| 708 std::string stored_device_id = device_mac->GetIdentifier(); | 721 std::string stored_device_id = device_mac->GetIdentifier(); |
| 709 std::string updated_device_id = | 722 std::string updated_device_id = |
| 710 BluetoothLowEnergyDeviceMac::GetPeripheralIdentifier(peripheral); | 723 BluetoothLowEnergyDeviceMac::GetPeripheralIdentifier(peripheral); |
| 711 if (stored_device_id != updated_device_id) { | 724 if (stored_device_id != updated_device_id) { |
| 712 VLOG(1) << "LowEnergyDeviceUpdated stored_device_id != updated_device_id: " | 725 VLOG(1) << "LowEnergyDeviceUpdated stored_device_id != updated_device_id: " |
| 713 << std::endl | 726 << std::endl |
| 714 << " " << stored_device_id << std::endl | 727 << " " << stored_device_id << std::endl |
| 715 << " " << updated_device_id; | 728 << " " << updated_device_id; |
| 716 // Collision, two identifiers map to the same hash address. With a 48 bit | 729 // Collision, two identifiers map to the same hash address. With a 48 bit |
| 717 // hash the probability of this occuring with 10,000 devices | 730 // hash the probability of this occuring with 10,000 devices |
| 718 // simultaneously present is 1e-6 (see | 731 // simultaneously present is 1e-6 (see |
| 719 // https://en.wikipedia.org/wiki/Birthday_problem#Probability_table). We | 732 // https://en.wikipedia.org/wiki/Birthday_problem#Probability_table). We |
| 720 // ignore the second device by returning. | 733 // ignore the second device by returning. |
| 721 return true; | 734 return true; |
| 722 } | 735 } |
| 723 return false; | 736 return false; |
| 724 } | 737 } |
| 725 | 738 |
| 726 } // namespace device | 739 } // namespace device |
| OLD | NEW |