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/bluez/bluetooth_adapter_bluez.h" | 5 #include "device/bluetooth/bluez/bluetooth_adapter_bluez.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <cstdint> | 8 #include <cstdint> |
9 #include <limits> | 9 #include <limits> |
10 #include <memory> | 10 #include <memory> |
(...skipping 491 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
502 const std::string& identifier) const { | 502 const std::string& identifier) const { |
503 const auto& service = owned_gatt_services_.find(dbus::ObjectPath(identifier)); | 503 const auto& service = owned_gatt_services_.find(dbus::ObjectPath(identifier)); |
504 return service == owned_gatt_services_.end() ? nullptr | 504 return service == owned_gatt_services_.end() ? nullptr |
505 : service->second.get(); | 505 : service->second.get(); |
506 } | 506 } |
507 | 507 |
508 void BluetoothAdapterBlueZ::RemovePairingDelegateInternal( | 508 void BluetoothAdapterBlueZ::RemovePairingDelegateInternal( |
509 BluetoothDevice::PairingDelegate* pairing_delegate) { | 509 BluetoothDevice::PairingDelegate* pairing_delegate) { |
510 // Check if any device is using the pairing delegate. | 510 // Check if any device is using the pairing delegate. |
511 // If so, clear the pairing context which will make any responses no-ops. | 511 // If so, clear the pairing context which will make any responses no-ops. |
512 for (DevicesMap::const_iterator iter = devices_.begin(); | 512 for (auto& item : devices_) { |
513 iter != devices_.end(); ++iter) { | |
514 BluetoothDeviceBlueZ* device_bluez = | 513 BluetoothDeviceBlueZ* device_bluez = |
515 static_cast<BluetoothDeviceBlueZ*>(iter->second); | 514 static_cast<BluetoothDeviceBlueZ*>(item.second.get()); |
516 | 515 |
517 BluetoothPairingBlueZ* pairing = device_bluez->GetPairing(); | 516 BluetoothPairingBlueZ* pairing = device_bluez->GetPairing(); |
518 if (pairing && pairing->GetPairingDelegate() == pairing_delegate) | 517 if (pairing && pairing->GetPairingDelegate() == pairing_delegate) |
519 device_bluez->EndPairing(); | 518 device_bluez->EndPairing(); |
520 } | 519 } |
521 } | 520 } |
522 | 521 |
523 void BluetoothAdapterBlueZ::AdapterAdded(const dbus::ObjectPath& object_path) { | 522 void BluetoothAdapterBlueZ::AdapterAdded(const dbus::ObjectPath& object_path) { |
524 // Set the adapter to the newly added adapter only if no adapter is present. | 523 // Set the adapter to the newly added adapter only if no adapter is present. |
525 if (!IsPresent()) | 524 if (!IsPresent()) |
(...skipping 29 matching lines...) Expand all Loading... |
555 | 554 |
556 void BluetoothAdapterBlueZ::DeviceAdded(const dbus::ObjectPath& object_path) { | 555 void BluetoothAdapterBlueZ::DeviceAdded(const dbus::ObjectPath& object_path) { |
557 DCHECK(bluez::BluezDBusManager::Get()); | 556 DCHECK(bluez::BluezDBusManager::Get()); |
558 bluez::BluetoothDeviceClient::Properties* properties = | 557 bluez::BluetoothDeviceClient::Properties* properties = |
559 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->GetProperties( | 558 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->GetProperties( |
560 object_path); | 559 object_path); |
561 if (!properties || properties->adapter.value() != object_path_) | 560 if (!properties || properties->adapter.value() != object_path_) |
562 return; | 561 return; |
563 DCHECK(IsPresent()); | 562 DCHECK(IsPresent()); |
564 | 563 |
565 BluetoothDeviceBlueZ* device_bluez = new BluetoothDeviceBlueZ( | 564 std::unique_ptr<BluetoothDeviceBlueZ> device_bluez(new BluetoothDeviceBlueZ( |
566 this, object_path, ui_task_runner_, socket_thread_); | 565 this, object_path, ui_task_runner_, socket_thread_)); |
567 DCHECK(devices_.find(device_bluez->GetAddress()) == devices_.end()); | 566 DCHECK(devices_.find(device_bluez->GetAddress()) == devices_.end()); |
568 | 567 |
569 devices_.set(device_bluez->GetAddress(), | 568 auto insertion = devices_.insert( |
570 std::unique_ptr<BluetoothDevice>(device_bluez)); | 569 std::make_pair(device_bluez->GetAddress(), std::move(device_bluez))); |
| 570 if (!insertion.second) { |
| 571 return; |
| 572 } |
571 | 573 |
572 for (auto& observer : observers_) | 574 for (auto& observer : observers_) |
573 observer.DeviceAdded(this, device_bluez); | 575 observer.DeviceAdded(this, insertion.first->second.get()); |
574 } | 576 } |
575 | 577 |
576 void BluetoothAdapterBlueZ::DeviceRemoved(const dbus::ObjectPath& object_path) { | 578 void BluetoothAdapterBlueZ::DeviceRemoved(const dbus::ObjectPath& object_path) { |
577 for (DevicesMap::const_iterator iter = devices_.begin(); | 579 for (DevicesMap::const_iterator iter = devices_.begin(); |
578 iter != devices_.end(); ++iter) { | 580 iter != devices_.end(); ++iter) { |
579 BluetoothDeviceBlueZ* device_bluez = | 581 BluetoothDeviceBlueZ* device_bluez = |
580 static_cast<BluetoothDeviceBlueZ*>(iter->second); | 582 static_cast<BluetoothDeviceBlueZ*>(iter->second.get()); |
581 if (device_bluez->object_path() == object_path) { | 583 if (device_bluez->object_path() == object_path) { |
582 std::unique_ptr<BluetoothDevice> scoped_device = | |
583 devices_.take_and_erase(iter->first); | |
584 | 584 |
585 for (auto& observer : observers_) | 585 for (auto& observer : observers_) |
586 observer.DeviceRemoved(this, device_bluez); | 586 observer.DeviceRemoved(this, device_bluez); |
| 587 |
| 588 devices_.erase(iter); |
| 589 |
587 return; | 590 return; |
588 } | 591 } |
589 } | 592 } |
590 } | 593 } |
591 | 594 |
592 void BluetoothAdapterBlueZ::DevicePropertyChanged( | 595 void BluetoothAdapterBlueZ::DevicePropertyChanged( |
593 const dbus::ObjectPath& object_path, | 596 const dbus::ObjectPath& object_path, |
594 const std::string& property_name) { | 597 const std::string& property_name) { |
595 BluetoothDeviceBlueZ* device_bluez = GetDeviceWithPath(object_path); | 598 BluetoothDeviceBlueZ* device_bluez = GetDeviceWithPath(object_path); |
596 if (!device_bluez) | 599 if (!device_bluez) |
597 return; | 600 return; |
598 | 601 |
599 bluez::BluetoothDeviceClient::Properties* properties = | 602 bluez::BluetoothDeviceClient::Properties* properties = |
600 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->GetProperties( | 603 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->GetProperties( |
601 object_path); | 604 object_path); |
602 | 605 |
603 if (property_name == properties->address.name()) { | 606 if (property_name == properties->address.name()) { |
604 for (DevicesMap::iterator iter = devices_.begin(); iter != devices_.end(); | 607 for (DevicesMap::iterator iter = devices_.begin(); iter != devices_.end(); |
605 ++iter) { | 608 ++iter) { |
606 if (iter->second->GetAddress() == device_bluez->GetAddress()) { | 609 if (iter->second->GetAddress() == device_bluez->GetAddress()) { |
607 std::string old_address = iter->first; | 610 std::string old_address = iter->first; |
608 VLOG(1) << "Device changed address, old: " << old_address | 611 VLOG(1) << "Device changed address, old: " << old_address |
609 << " new: " << device_bluez->GetAddress(); | 612 << " new: " << device_bluez->GetAddress(); |
610 std::unique_ptr<BluetoothDevice> scoped_device = | 613 std::unique_ptr<BluetoothDevice> scoped_device = |
611 devices_.take_and_erase(iter); | 614 std::move(iter->second); |
612 ignore_result(scoped_device.release()); | 615 devices_.erase(iter); |
613 | 616 |
614 DCHECK(devices_.find(device_bluez->GetAddress()) == devices_.end()); | 617 DCHECK(devices_.find(scoped_device->GetAddress()) == devices_.end()); |
615 devices_.set(device_bluez->GetAddress(), | 618 auto insertion = devices_.insert(std::make_pair( |
616 std::unique_ptr<BluetoothDevice>(device_bluez)); | 619 scoped_device->GetAddress(), std::move(scoped_device))); |
617 NotifyDeviceAddressChanged(device_bluez, old_address); | 620 if (!insertion.second) { |
| 621 return; |
| 622 } |
| 623 BluetoothDeviceBlueZ* bluez = |
| 624 static_cast<BluetoothDeviceBlueZ*>(insertion.first->second.get()); |
| 625 NotifyDeviceAddressChanged(bluez, old_address); |
618 break; | 626 break; |
619 } | 627 } |
620 } | 628 } |
621 } | 629 } |
622 | 630 |
623 if (property_name == properties->service_data.name()) | 631 if (property_name == properties->service_data.name()) |
624 device_bluez->UpdateServiceData(); | 632 device_bluez->UpdateServiceData(); |
625 else if (property_name == properties->manufacturer_data.name()) | 633 else if (property_name == properties->manufacturer_data.name()) |
626 device_bluez->UpdateManufacturerData(); | 634 device_bluez->UpdateManufacturerData(); |
627 else if (property_name == properties->advertising_data_flags.name()) | 635 else if (property_name == properties->advertising_data_flags.name()) |
(...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
899 } | 907 } |
900 | 908 |
901 BluetoothDeviceBlueZ* BluetoothAdapterBlueZ::GetDeviceWithPath( | 909 BluetoothDeviceBlueZ* BluetoothAdapterBlueZ::GetDeviceWithPath( |
902 const dbus::ObjectPath& object_path) { | 910 const dbus::ObjectPath& object_path) { |
903 if (!IsPresent()) | 911 if (!IsPresent()) |
904 return nullptr; | 912 return nullptr; |
905 | 913 |
906 for (DevicesMap::const_iterator iter = devices_.begin(); | 914 for (DevicesMap::const_iterator iter = devices_.begin(); |
907 iter != devices_.end(); ++iter) { | 915 iter != devices_.end(); ++iter) { |
908 BluetoothDeviceBlueZ* device_bluez = | 916 BluetoothDeviceBlueZ* device_bluez = |
909 static_cast<BluetoothDeviceBlueZ*>(iter->second); | 917 static_cast<BluetoothDeviceBlueZ*>(iter->second.get()); |
910 if (device_bluez->object_path() == object_path) | 918 if (device_bluez->object_path() == object_path) |
911 return device_bluez; | 919 return device_bluez; |
912 } | 920 } |
913 | 921 |
914 return nullptr; | 922 return nullptr; |
915 } | 923 } |
916 | 924 |
917 BluetoothPairingBlueZ* BluetoothAdapterBlueZ::GetPairing( | 925 BluetoothPairingBlueZ* BluetoothAdapterBlueZ::GetPairing( |
918 const dbus::ObjectPath& object_path) { | 926 const dbus::ObjectPath& object_path) { |
919 DCHECK(IsPresent()); | 927 DCHECK(IsPresent()); |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1034 DiscoveringChanged(false); | 1042 DiscoveringChanged(false); |
1035 | 1043 |
1036 // Move all elements of the original devices list to a new list here, | 1044 // Move all elements of the original devices list to a new list here, |
1037 // leaving the original list empty so that when we send DeviceRemoved(), | 1045 // leaving the original list empty so that when we send DeviceRemoved(), |
1038 // GetDevices() returns no devices. | 1046 // GetDevices() returns no devices. |
1039 DevicesMap devices_swapped; | 1047 DevicesMap devices_swapped; |
1040 devices_swapped.swap(devices_); | 1048 devices_swapped.swap(devices_); |
1041 | 1049 |
1042 for (auto& iter : devices_swapped) { | 1050 for (auto& iter : devices_swapped) { |
1043 for (auto& observer : observers_) | 1051 for (auto& observer : observers_) |
1044 observer.DeviceRemoved(this, iter.second); | 1052 observer.DeviceRemoved(this, iter.second.get()); |
1045 } | 1053 } |
1046 | 1054 |
1047 PresentChanged(false); | 1055 PresentChanged(false); |
1048 } | 1056 } |
1049 | 1057 |
1050 void BluetoothAdapterBlueZ::DiscoverableChanged(bool discoverable) { | 1058 void BluetoothAdapterBlueZ::DiscoverableChanged(bool discoverable) { |
1051 for (auto& observer : observers_) | 1059 for (auto& observer : observers_) |
1052 observer.AdapterDiscoverableChanged(this, discoverable); | 1060 observer.AdapterDiscoverableChanged(this, discoverable); |
1053 } | 1061 } |
1054 | 1062 |
(...skipping 679 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1734 } else if (error_name == bluetooth_adapter::kErrorAlreadyExists) { | 1742 } else if (error_name == bluetooth_adapter::kErrorAlreadyExists) { |
1735 code = BluetoothServiceRecordBlueZ::ErrorCode::ERROR_RECORD_ALREADY_EXISTS; | 1743 code = BluetoothServiceRecordBlueZ::ErrorCode::ERROR_RECORD_ALREADY_EXISTS; |
1736 } else if (error_name == bluetooth_adapter::kErrorNotReady) { | 1744 } else if (error_name == bluetooth_adapter::kErrorNotReady) { |
1737 code = BluetoothServiceRecordBlueZ::ErrorCode::ERROR_ADAPTER_NOT_READY; | 1745 code = BluetoothServiceRecordBlueZ::ErrorCode::ERROR_ADAPTER_NOT_READY; |
1738 } | 1746 } |
1739 | 1747 |
1740 error_callback.Run(code); | 1748 error_callback.Run(code); |
1741 } | 1749 } |
1742 | 1750 |
1743 } // namespace bluez | 1751 } // namespace bluez |
OLD | NEW |