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)); | |
625 for (auto& observer : observers_) { | 635 for (auto& observer : observers_) { |
626 observer.DeviceAdded(this, device_mac); | 636 observer.DeviceAdded(this, device_mac); |
627 } | 637 } |
628 } | 638 } |
629 connected_devices.push_back(device_mac); | 639 connected_devices.push_back(device_mac); |
630 } | 640 } |
631 return connected_devices; | 641 return connected_devices; |
632 } | 642 } |
633 | 643 |
634 void BluetoothAdapterMac::CreateGattConnection( | 644 void BluetoothAdapterMac::CreateGattConnection( |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
691 } | 701 } |
692 | 702 |
693 BluetoothLowEnergyDeviceMac* | 703 BluetoothLowEnergyDeviceMac* |
694 BluetoothAdapterMac::GetBluetoothLowEnergyDeviceMac(CBPeripheral* peripheral) { | 704 BluetoothAdapterMac::GetBluetoothLowEnergyDeviceMac(CBPeripheral* peripheral) { |
695 std::string device_address = | 705 std::string device_address = |
696 BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral); | 706 BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral); |
697 DevicesMap::const_iterator iter = devices_.find(device_address); | 707 DevicesMap::const_iterator iter = devices_.find(device_address); |
698 if (iter == devices_.end()) { | 708 if (iter == devices_.end()) { |
699 return nil; | 709 return nil; |
700 } | 710 } |
701 return static_cast<BluetoothLowEnergyDeviceMac*>(iter->second); | 711 return static_cast<BluetoothLowEnergyDeviceMac*>(iter->second.get()); |
702 } | 712 } |
703 | 713 |
704 bool BluetoothAdapterMac::DoesCollideWithKnownDevice( | 714 bool BluetoothAdapterMac::DoesCollideWithKnownDevice( |
705 CBPeripheral* peripheral, | 715 CBPeripheral* peripheral, |
706 BluetoothLowEnergyDeviceMac* device_mac) { | 716 BluetoothLowEnergyDeviceMac* device_mac) { |
707 // Check that there are no collisions. | 717 // Check that there are no collisions. |
708 std::string stored_device_id = device_mac->GetIdentifier(); | 718 std::string stored_device_id = device_mac->GetIdentifier(); |
709 std::string updated_device_id = | 719 std::string updated_device_id = |
710 BluetoothLowEnergyDeviceMac::GetPeripheralIdentifier(peripheral); | 720 BluetoothLowEnergyDeviceMac::GetPeripheralIdentifier(peripheral); |
711 if (stored_device_id != updated_device_id) { | 721 if (stored_device_id != updated_device_id) { |
712 VLOG(1) << "LowEnergyDeviceUpdated stored_device_id != updated_device_id: " | 722 VLOG(1) << "LowEnergyDeviceUpdated stored_device_id != updated_device_id: " |
713 << std::endl | 723 << std::endl |
714 << " " << stored_device_id << std::endl | 724 << " " << stored_device_id << std::endl |
715 << " " << updated_device_id; | 725 << " " << updated_device_id; |
716 // Collision, two identifiers map to the same hash address. With a 48 bit | 726 // Collision, two identifiers map to the same hash address. With a 48 bit |
717 // hash the probability of this occuring with 10,000 devices | 727 // hash the probability of this occuring with 10,000 devices |
718 // simultaneously present is 1e-6 (see | 728 // simultaneously present is 1e-6 (see |
719 // https://en.wikipedia.org/wiki/Birthday_problem#Probability_table). We | 729 // https://en.wikipedia.org/wiki/Birthday_problem#Probability_table). We |
720 // ignore the second device by returning. | 730 // ignore the second device by returning. |
721 return true; | 731 return true; |
722 } | 732 } |
723 return false; | 733 return false; |
724 } | 734 } |
725 | 735 |
726 } // namespace device | 736 } // namespace device |
OLD | NEW |