From 1255f6a800ad7336ca4480cb7760b9f53e09118b Mon Sep 17 00:00:00 2001 From: Guogang Li Date: Thu, 10 Oct 2024 18:02:48 -0700 Subject: [PATCH] Internal fix PiperOrigin-RevId: 684640919 --- .../mediums/bluetooth_classic.cc | 169 +++++++++--------- internal/platform/BUILD | 1 + internal/platform/bluetooth_classic.cc | 106 +++++------ internal/platform/bluetooth_classic.h | 8 +- .../windows/bluetooth_classic_medium.cc | 4 +- .../windows/bluetooth_classic_medium.h | 8 +- 6 files changed, 146 insertions(+), 150 deletions(-) diff --git a/connections/implementation/mediums/bluetooth_classic.cc b/connections/implementation/mediums/bluetooth_classic.cc index 6eb98ec4..f78ed08f 100644 --- a/connections/implementation/mediums/bluetooth_classic.cc +++ b/connections/implementation/mediums/bluetooth_classic.cc @@ -75,12 +75,12 @@ BluetoothClassic::~BluetoothClassic() { { MutexLock lock(&mutex_); - NEARBY_LOGS(INFO) << "Closing multiplex sockets for " - << multiplex_sockets_.size() << " devices"; + LOG(INFO) << "Closing multiplex sockets for " << multiplex_sockets_.size() + << " devices"; if (is_multiplex_enabled_) { for (auto& [bt_mac, multiplex_socket] : multiplex_sockets_) { - NEARBY_LOGS(INFO) << "Closing multiplex sockets for " - << GetRemoteDevice(bt_mac).GetName(); + LOG(INFO) << "Closing multiplex sockets for " + << GetRemoteDevice(bt_mac).GetName(); multiplex_socket->Shutdown(); } } @@ -104,45 +104,41 @@ bool BluetoothClassic::IsAvailableLocked() const { } bool BluetoothClassic::TurnOnDiscoverability(const std::string& device_name) { - NEARBY_LOGS(INFO) << "Turning on BT discoverability with device_name=" - << device_name; + LOG(INFO) << "Turning on BT discoverability with device_name=" << device_name; MutexLock lock(&mutex_); if (device_name.empty()) { - NEARBY_LOGS(INFO) - << "Refusing to turn on BT discoverability. Empty device name."; + LOG(INFO) << "Refusing to turn on BT discoverability. Empty device name."; return false; } if (!radio_.IsEnabled()) { - NEARBY_LOGS(INFO) << "Can't turn on BT discoverability. BT is off."; + LOG(INFO) << "Can't turn on BT discoverability. BT is off."; return false; } if (!IsAvailableLocked()) { - NEARBY_LOGS(INFO) - << "Can't turn on BT discoverability. BT is not available."; + LOG(INFO) << "Can't turn on BT discoverability. BT is not available."; return false; } if (IsDiscoverable()) { - NEARBY_LOGS(INFO) << "Refusing to turn on BT discoverability; new name='" - << device_name << "'; current name='" - << adapter_.GetName() << "'"; + LOG(INFO) << "Refusing to turn on BT discoverability; new name='" + << device_name << "'; current name='" << adapter_.GetName() + << "'"; return false; } if (!ModifyDeviceName(device_name)) { - NEARBY_LOGS(INFO) - << "Failed to turn on BT discoverability; failed to set name to " - << device_name; + LOG(INFO) << "Failed to turn on BT discoverability; failed to set name to " + << device_name; return false; } if (!ModifyScanMode(ScanMode::kConnectableDiscoverable)) { - NEARBY_LOGS(INFO) << "Failed to turn on BT discoverability; failed to set " - "scan_mode to " - << ScanModeToString(ScanMode::kConnectableDiscoverable); + LOG(INFO) << "Failed to turn on BT discoverability; failed to set " + "scan_mode to " + << ScanModeToString(ScanMode::kConnectableDiscoverable); // Don't forget to perform this rollback of the partial state changes we've // made til now. @@ -150,24 +146,23 @@ bool BluetoothClassic::TurnOnDiscoverability(const std::string& device_name) { return false; } - NEARBY_LOGS(INFO) << "Turned on BT discoverability with device_name=" - << device_name; + LOG(INFO) << "Turned on BT discoverability with device_name=" << device_name; return true; } bool BluetoothClassic::TurnOffDiscoverability() { - NEARBY_LOGS(INFO) << "Turning off Bluetooth discoverability."; + LOG(INFO) << "Turning off Bluetooth discoverability."; MutexLock lock(&mutex_); if (!IsDiscoverable()) { - NEARBY_LOGS(INFO) << "Can't turn off BT discoverability; it is already off"; + LOG(INFO) << "Can't turn off BT discoverability; it is already off"; return false; } RestoreScanMode(); RestoreDeviceName(); - NEARBY_LOGS(INFO) << "Turned Bluetooth discoverability off."; + LOG(INFO) << "Turned Bluetooth discoverability off."; return true; } @@ -200,8 +195,8 @@ bool BluetoothClassic::ModifyScanMode(ScanMode scan_mode) { bool BluetoothClassic::RestoreScanMode() { if (original_scan_mode_ == ScanMode::kUnknown || !adapter_.SetScanMode(original_scan_mode_)) { - NEARBY_LOGS(INFO) << "Failed to restore original Bluetooth scan mode to " - << ScanModeToString(original_scan_mode_); + LOG(INFO) << "Failed to restore original Bluetooth scan mode to " + << ScanModeToString(original_scan_mode_); return false; } @@ -214,8 +209,8 @@ bool BluetoothClassic::RestoreScanMode() { bool BluetoothClassic::RestoreDeviceName() { if (original_device_name_.empty() || !adapter_.SetName(original_device_name_, /* persis= */ true)) { - NEARBY_LOGS(INFO) << "Failed to restore original Bluetooth device name to " - << original_device_name_; + LOG(INFO) << "Failed to restore original Bluetooth device name to " + << original_device_name_; return false; } original_device_name_.clear(); @@ -227,26 +222,24 @@ bool BluetoothClassic::StartDiscovery(const std::string& serviceId, MutexLock lock(&mutex_); if (serviceId.empty()) { - NEARBY_LOGS(INFO) << "Refusing to start discovery; service ID is empty."; + LOG(INFO) << "Refusing to start discovery; service ID is empty."; return false; } if (!radio_.IsEnabled()) { - NEARBY_LOGS(INFO) << "Can't discover BT devices because BT isn't enabled."; + LOG(INFO) << "Can't discover BT devices because BT isn't enabled."; return false; } if (!IsAvailableLocked()) { - NEARBY_LOGS(INFO) - << "Can't discover BT devices because BT isn't available."; + LOG(INFO) << "Can't discover BT devices because BT isn't available."; return false; } if (IsDiscoveringLocked(serviceId)) { - NEARBY_LOGS(INFO) - << "Refusing to start discovery of BT devices because another " - "discovery is already in-progress for service_id=" - << serviceId; + LOG(INFO) << "Refusing to start discovery of BT devices because another " + "discovery is already in-progress for service_id=" + << serviceId; return false; } @@ -283,7 +276,7 @@ bool BluetoothClassic::StartDiscovery(const std::string& serviceId, AddDiscoveryCallback(serviceId, std::move(callback)); if (!medium_->StartDiscovery(std::move(medium_callback))) { - NEARBY_LOGS(INFO) << "Failed to start discovery of BT devices."; + LOG(INFO) << "Failed to start discovery of BT devices."; RemoveDiscoveryCallback(serviceId); return false; } @@ -299,8 +292,7 @@ bool BluetoothClassic::StopDiscovery(const std::string& serviceId) { MutexLock lock(&mutex_); if (!IsDiscoveringLocked(serviceId)) { - NEARBY_LOGS(INFO) - << "Can't stop discovery of BT devices because it never started."; + LOG(INFO) << "Can't stop discovery of BT devices because it never started."; return false; } @@ -308,7 +300,7 @@ bool BluetoothClassic::StopDiscovery(const std::string& serviceId) { if (!HasDiscoveryCallbacks()) { if (!medium_->StopDiscovery()) { - NEARBY_LOGS(INFO) << "Failed to stop discovery of Bluetooth devices."; + LOG(INFO) << "Failed to stop discovery of Bluetooth devices."; return false; } @@ -325,7 +317,7 @@ bool BluetoothClassic::IsDiscoveringLocked(const std::string& serviceId) const { void BluetoothClassic::StopAllDiscovery() { MutexLock lock(&mutex_); if (!medium_->StopDiscovery()) { - NEARBY_LOGS(INFO) << "Failed to stop discovery of Bluetooth devices."; + LOG(INFO) << "Failed to stop discovery of Bluetooth devices."; } RemoveAllDiscoveryCallbacks(); @@ -337,35 +329,35 @@ bool BluetoothClassic::StartAcceptingConnections( MutexLock lock(&mutex_); if (service_id.empty()) { - NEARBY_LOGS(INFO) + LOG(INFO) << "Refusing to start accepting BT connections; service ID is empty."; return false; } if (!radio_.IsEnabled()) { - NEARBY_LOGS(INFO) << "Can't create BT server socket [service=" << service_id - << "]; BT is disabled."; + LOG(INFO) << "Can't create BT server socket [service=" << service_id + << "]; BT is disabled."; return false; } if (!IsAvailableLocked()) { - NEARBY_LOGS(INFO) << "Can't start accepting BT connections [service=" - << service_id << "]; BT not available."; + LOG(INFO) << "Can't start accepting BT connections [service=" << service_id + << "]; BT not available."; return false; } if (IsAcceptingConnectionsLocked(service_id)) { - NEARBY_LOGS(INFO) - << "Refusing to start accepting BT connections [service=" << service_id - << "]; BT server is already in-progress with the same name."; + LOG(INFO) << "Refusing to start accepting BT connections [service=" + << service_id + << "]; BT server is already in-progress with the same name."; return false; } BluetoothServerSocket socket = medium_->ListenForService(service_id, GenerateUuidFromString(service_id)); if (!socket.IsValid()) { - NEARBY_LOGS(INFO) << "Failed to start accepting Bluetooth connections for " - << service_id; + LOG(INFO) << "Failed to start accepting Bluetooth connections for " + << service_id; return false; } @@ -395,11 +387,11 @@ bool BluetoothClassic::StartAcceptingConnections( while (true) { BluetoothSocket client_socket = server_socket.Accept(); if (!client_socket.IsValid()) { - NEARBY_LOGS(INFO) << "Failed to accept connection for " << service_id; + LOG(INFO) << "Failed to accept connection for " << service_id; server_socket.Close(); break; } - NEARBY_LOGS(INFO) << "Accepted connection for " << service_id; + LOG(INFO) << "Accepted connection for " << service_id; bool callback_called = false; { MutexLock lock(&mutex_); @@ -418,8 +410,8 @@ bool BluetoothClassic::StartAcceptingConnections( multiplex_socket); MultiplexSocket::StopListeningForIncomingConnection( service_id, Medium::BLUETOOTH); - NEARBY_LOGS(INFO) << "Multiplex virtaul socket created for " - << client_socket.GetRemoteDevice().GetName(); + LOG(INFO) << "Multiplex virtaul socket created for " + << client_socket.GetRemoteDevice().GetName(); if (callback) { callback(service_id, *(down_cast( @@ -430,7 +422,7 @@ bool BluetoothClassic::StartAcceptingConnections( } } if (callback && !callback_called) { - NEARBY_LOGS(INFO) << "Call back triggered for physical socket."; + LOG(INFO) << "Call back triggered for physical socket."; callback(service_id, std::move(client_socket)); } } @@ -454,15 +446,15 @@ bool BluetoothClassic::StopAcceptingConnections(const std::string& service_id) { MutexLock lock(&mutex_); if (service_id.empty()) { - NEARBY_LOGS(INFO) << "Unable to stop accepting BT connections because the " - "service_id is empty."; + LOG(INFO) << "Unable to stop accepting BT connections because the " + "service_id is empty."; return false; } const auto& it = server_sockets_.find(service_id); if (it == server_sockets_.end()) { - NEARBY_LOGS(INFO) << "Can't stop accepting BT connections for " - << service_id << " because it was never started."; + LOG(INFO) << "Can't stop accepting BT connections for " << service_id + << " because it was never started."; return false; } if (is_multiplex_enabled_) { @@ -487,7 +479,7 @@ bool BluetoothClassic::StopAcceptingConnections(const std::string& service_id) { // Finally, close the BluetoothServerSocket. if (!listening_socket.Close().Ok()) { - NEARBY_LOGS(INFO) << "Failed to close BT server socket for " << service_id; + LOG(INFO) << "Failed to close BT server socket for " << service_id; return false; } @@ -500,8 +492,7 @@ BluetoothSocket BluetoothClassic::Connect(BluetoothDevice& bluetooth_device, { MutexLock lock(&mutex_); if (is_multiplex_enabled_) { - NEARBY_LOGS(INFO) << "multiplex_sockets_ size:" - << multiplex_sockets_.size(); + LOG(INFO) << "multiplex_sockets_ size:" << multiplex_sockets_.size(); auto it = multiplex_sockets_.find(bluetooth_device.GetMacAddress()); if (it != multiplex_sockets_.end()) { MultiplexSocket* multiplex_socket = it->second; @@ -511,9 +502,8 @@ BluetoothSocket BluetoothClassic::Connect(BluetoothDevice& bluetooth_device, // Should not happen. auto* bluetooth_socket = down_cast(virtual_socket); if (bluetooth_socket == nullptr) { - NEARBY_LOGS(INFO) - << "Failed to cast to BluetoothSocket for " << service_id - << " with " << bluetooth_device.GetName(); + LOG(INFO) << "Failed to cast to BluetoothSocket for " << service_id + << " with " << bluetooth_device.GetName(); return BluetoothSocket{}; } return *bluetooth_socket; @@ -525,18 +515,17 @@ BluetoothSocket BluetoothClassic::Connect(BluetoothDevice& bluetooth_device, while (service_id_to_connect_attempts_count_map_[service_id] <= kConnectAttemptsLimit) { if (cancellation_flag->Cancelled()) { - NEARBY_LOGS(WARNING) - << "Attempt #" - << service_id_to_connect_attempts_count_map_[service_id] - << ": Cannot start creating client BT socket due to cancel."; + LOG(WARNING) << "Attempt #" + << service_id_to_connect_attempts_count_map_[service_id] + << ": Cannot start creating client BT socket due to cancel."; return BluetoothSocket{}; } auto wrapper_result = AttemptToConnect(bluetooth_device, service_id, cancellation_flag); - NEARBY_LOGS(INFO) << "Attempt #" - << service_id_to_connect_attempts_count_map_[service_id] - << " to connect: " << wrapper_result.IsValid(); + LOG(INFO) << "Attempt #" + << service_id_to_connect_attempts_count_map_[service_id] + << " to connect: " << wrapper_result.IsValid(); if (wrapper_result.IsValid()) { return wrapper_result; } @@ -544,8 +533,7 @@ BluetoothSocket BluetoothClassic::Connect(BluetoothDevice& bluetooth_device, service_id_to_connect_attempts_count_map_[service_id]++; } - NEARBY_LOGS(WARNING) << "Giving up after " << kConnectAttemptsLimit - << " attempts"; + LOG(WARNING) << "Giving up after " << kConnectAttemptsLimit << " attempts"; return BluetoothSocket{}; } @@ -553,26 +541,31 @@ BluetoothSocket BluetoothClassic::AttemptToConnect( BluetoothDevice& bluetooth_device, const std::string& service_id, CancellationFlag* cancellation_flag) { MutexLock lock(&mutex_); - NEARBY_LOGS(INFO) << "BluetoothClassic::Connect: service_id=" << service_id - << ", device=" << &bluetooth_device; + LOG(INFO) << "BluetoothClassic::Connect: service_id=" << service_id + << ", device=" << &bluetooth_device; // Socket to return. To allow for NRVO to work, it has to be a single object. BluetoothSocket socket{}; if (service_id.empty()) { - NEARBY_LOGS(INFO) + LOG(WARNING) << "Refusing to create client BT socket because service_id is empty."; return socket; } if (!radio_.IsEnabled()) { - NEARBY_LOGS(INFO) << "Can't create client BT socket [service=" << service_id - << "]: BT isn't enabled."; + LOG(WARNING) << "Can't create client BT socket [service=" << service_id + << "]: BT isn't enabled."; return socket; } if (!IsAvailableLocked()) { - NEARBY_LOGS(INFO) << "Can't create client BT socket [service=" << service_id - << "]; BT isn't available."; + LOG(WARNING) << "Can't create client BT socket [service=" << service_id + << "]; BT isn't available."; + return socket; + } + + if (!bluetooth_device.IsValid()) { + LOG(WARNING) << "Bluetooth device is not valid."; return socket; } @@ -583,8 +576,7 @@ BluetoothSocket BluetoothClassic::AttemptToConnect( // `ConnectToService`, return an empty socket. There is no need for a // CancellationFlagListener because the attempt logic is not asynchronous. if (!socket.IsValid() || cancellation_flag->Cancelled()) { - NEARBY_LOGS(INFO) << "Failed to Connect via BT [service=" << service_id - << "]"; + LOG(INFO) << "Failed to Connect via BT [service=" << service_id << "]"; return BluetoothSocket{}; } @@ -599,12 +591,11 @@ BluetoothSocket BluetoothClassic::AttemptToConnect( // Should not happen. auto* bluetooth_socket = down_cast(virtual_socket); if (bluetooth_socket == nullptr) { - NEARBY_LOGS(INFO) << "Failed to cast to BluetoothSocket for " - << service_id << " with " << bluetooth_device.GetName(); + LOG(INFO) << "Failed to cast to BluetoothSocket for " << service_id + << " with " << bluetooth_device.GetName(); return BluetoothSocket{}; } - NEARBY_LOGS(INFO) << "Multiplex socket created for " - << bluetooth_device.GetName(); + LOG(INFO) << "Multiplex socket created for " << bluetooth_device.GetName(); multiplex_sockets_.emplace(bluetooth_device.GetMacAddress(), multiplex_socket); return *bluetooth_socket; diff --git a/internal/platform/BUILD b/internal/platform/BUILD index 36ae5f1d..ddf31bc9 100644 --- a/internal/platform/BUILD +++ b/internal/platform/BUILD @@ -333,6 +333,7 @@ cc_library( "//internal/platform/implementation:platform", "//internal/platform/implementation:wifi_utils", # TODO: Support WebRTC + "@com_google_absl//absl/base:core_headers", "@com_google_absl//absl/container:flat_hash_map", "@com_google_absl//absl/container:flat_hash_set", "@com_google_absl//absl/functional:any_invocable", diff --git a/internal/platform/bluetooth_classic.cc b/internal/platform/bluetooth_classic.cc index 8b072007..20d0e305 100644 --- a/internal/platform/bluetooth_classic.cc +++ b/internal/platform/bluetooth_classic.cc @@ -33,7 +33,7 @@ using location::nearby::proto::connections::Medium; MediumSocket* BluetoothSocket::CreateVirtualSocket(OutputStream* outputstream) { if (IsVirtualSocket()) { - NEARBY_LOGS(WARNING) + LOG(WARNING) << "Creating the virtual socket on a virtual socket is not allowed."; return nullptr; } @@ -47,107 +47,99 @@ MediumSocket* BluetoothSocket::CreateVirtualSocket( absl::flat_hash_map>* virtual_sockets_ptr) { if (IsVirtualSocket()) { - NEARBY_LOGS(WARNING) + LOG(WARNING) << "Creating the virtual socket on a virtual socket is not allowed."; return nullptr; } auto virtual_socket = std::make_shared(outputstream); virtual_socket->impl_ = this->impl_; - NEARBY_LOGS(WARNING) << "Created the virtual socket for Medium: " - << Medium_Name(virtual_socket->GetMedium()); + LOG(WARNING) << "Created the virtual socket for Medium: " + << Medium_Name(virtual_socket->GetMedium()); if (virtual_sockets_ptr_ == nullptr) { virtual_sockets_ptr_ = virtual_sockets_ptr; } (*virtual_sockets_ptr_)[salted_service_id_hash_key] = virtual_socket; - NEARBY_LOGS(INFO) << "virtual_sockets_ size: " - << virtual_sockets_ptr_->size(); + LOG(INFO) << "virtual_sockets_ size: " << virtual_sockets_ptr_->size(); return virtual_socket.get(); } BluetoothClassicMedium::~BluetoothClassicMedium() { - NEARBY_LOGS(INFO) << "~BluetoothClassicMedium: observer_list_ size: " - << observer_list_.size(); + LOG(INFO) << "~BluetoothClassicMedium: observer_list_ size: " + << observer_list_.size(); if (!observer_list_.empty()) { impl_->RemoveObserver(this); } StopDiscovery(); - NEARBY_LOGS(INFO) << "eof ~BluetoothClassicMedium"; + LOG(INFO) << "eof ~BluetoothClassicMedium"; } BluetoothSocket BluetoothClassicMedium::ConnectToService( BluetoothDevice& remote_device, const std::string& service_uuid, CancellationFlag* cancellation_flag) { - NEARBY_LOGS(INFO) << "BluetoothClassicMedium::ConnectToService: " - "service_uuid=" - << service_uuid - << ", device=" << remote_device.GetMacAddress() - << ", [impl=" << &remote_device.GetImpl() << "]"; + LOG(INFO) << "BluetoothClassicMedium::ConnectToService: " + "service_uuid=" + << service_uuid << ", device=" << remote_device.GetMacAddress() + << ", [impl=" << &remote_device.GetImpl() << "]"; return BluetoothSocket(impl_->ConnectToService( remote_device.GetImpl(), service_uuid, cancellation_flag)); } bool BluetoothClassicMedium::StartDiscovery(DiscoveryCallback callback) { - NEARBY_LOGS(INFO) << "BluetoothClassicMedium::StartDiscovery"; + LOG(INFO) << "BluetoothClassicMedium::StartDiscovery"; MutexLock lock(&mutex_); if (discovery_enabled_) { - NEARBY_LOGS(INFO) << "BT Discovery already enabled; impl=" << &GetImpl(); + LOG(INFO) << "BT Discovery already enabled; impl=" << &GetImpl(); return false; } bool success = impl_->StartDiscovery({ .device_discovered_cb = [this](api::BluetoothDevice& device) { - NEARBY_VLOG(1) << "BT .device_discovered_cb for " - << device.GetName(); + VLOG(1) << "BT .device_discovered_cb for " << device.GetName(); MutexLock lock(&mutex_); auto pair = devices_.emplace( &device, std::make_unique()); auto& context = *pair.first->second; if (!pair.second) { - NEARBY_LOGS(INFO) - << "Adding (again) device=" << context.device.GetMacAddress() - << ",impl=" << &device; + LOG(INFO) << "Adding (again) device=" + << context.device.GetMacAddress() + << ",impl=" << &device; return; } context.device = BluetoothDevice(&device); - NEARBY_LOGS(INFO) - << "Adding device=" << context.device.GetMacAddress() - << ",impl=" << &device; + LOG(INFO) << "Adding device=" << context.device.GetMacAddress() + << ",impl=" << &device; if (!discovery_enabled_) return; discovery_callback_.device_discovered_cb(context.device); }, .device_name_changed_cb = [this](api::BluetoothDevice& device) { - NEARBY_VLOG(1) << "BT .device_name_changed_cb for " - << device.GetName(); + VLOG(1) << "BT .device_name_changed_cb for " << device.GetName(); MutexLock lock(&mutex_); // If the device is not already in devices_, we should not be able // to change its name. if (devices_.find(&device) == devices_.end()) return; auto& context = *devices_[&device]; - NEARBY_LOGS(INFO) - << "Renaming device=" << context.device.GetMacAddress() - << ",impl=" << &device; + LOG(INFO) << "Renaming device=" << context.device.GetMacAddress() + << ",impl=" << &device; if (!discovery_enabled_) return; discovery_callback_.device_name_changed_cb(context.device); }, .device_lost_cb = [this](api::BluetoothDevice& device) { - NEARBY_VLOG(1) << "BT .device_lost_cb for " - << device.GetMacAddress(); + VLOG(1) << "BT .device_lost_cb for " << device.GetMacAddress(); MutexLock lock(&mutex_); auto item = devices_.extract(&device); if (!item) { - NEARBY_LOGS(WARNING) - << "Removing unknown device: " << device.GetMacAddress(); + LOG(WARNING) << "Removing unknown device: " + << device.GetMacAddress(); return; } auto& context = *item.mapped(); - NEARBY_LOGS(INFO) - << "Removing device=" << context.device.GetMacAddress() - << ",impl=" << &device; + LOG(INFO) << "Removing device=" << context.device.GetMacAddress() + << ",impl=" << &device; if (!discovery_enabled_) return; discovery_callback_.device_lost_cb(context.device); }, @@ -157,53 +149,53 @@ bool BluetoothClassicMedium::StartDiscovery(DiscoveryCallback callback) { devices_.clear(); discovery_enabled_ = true; } - NEARBY_LOGS(INFO) << "BT StartDiscovery result:" << success - << ", impl=" << &GetImpl(); + LOG(INFO) << "BT StartDiscovery result:" << success + << ", impl=" << &GetImpl(); return success; } bool BluetoothClassicMedium::StopDiscovery() { - NEARBY_LOGS(INFO) << "BT StopDiscovery; impl=" << &GetImpl(); + LOG(INFO) << "BT StopDiscovery; impl=" << &GetImpl(); MutexLock lock(&mutex_); if (!discovery_enabled_) return true; discovery_enabled_ = false; discovery_callback_ = {}; devices_.clear(); - NEARBY_LOGS(INFO) << "BT Discovery disabled: impl=" << &GetImpl(); + LOG(INFO) << "BT Discovery disabled: impl=" << &GetImpl(); return impl_->StopDiscovery(); } void BluetoothClassicMedium::AddObserver(Observer* observer) { - NEARBY_LOGS(INFO) << "BT AddObserver; impl=" << &GetImpl(); + LOG(INFO) << "BT AddObserver; impl=" << &GetImpl(); MutexLock lock(&mutex_); if (observer_list_.empty()) { impl_->AddObserver(this); } observer_list_.AddObserver(observer); - NEARBY_LOGS(INFO) << "BT AddObserver done"; + LOG(INFO) << "BT AddObserver done"; } void BluetoothClassicMedium::RemoveObserver(Observer* observer) { - NEARBY_LOGS(INFO) << "BT RemoveObserver; impl=" << &GetImpl(); + LOG(INFO) << "BT RemoveObserver; impl=" << &GetImpl(); MutexLock lock(&mutex_); observer_list_.RemoveObserver(observer); if (observer_list_.empty()) { impl_->RemoveObserver(this); } - NEARBY_LOGS(INFO) << "BT RemoveObserver done"; + LOG(INFO) << "BT RemoveObserver done"; } // api::BluetoothClassicMedium::Observer methods void BluetoothClassicMedium::DeviceAdded(api::BluetoothDevice& device) { - NEARBY_VLOG(1) << "BT DeviceAdded; name=" << device.GetName() - << ", address=" << device.GetMacAddress(); + VLOG(1) << "BT DeviceAdded; name=" << device.GetName() + << ", address=" << device.GetMacAddress(); BluetoothDevice bt_device(&device); for (auto* observer : observer_list_.GetObservers()) { observer->DeviceAdded(bt_device); } } void BluetoothClassicMedium::DeviceRemoved(api::BluetoothDevice& device) { - NEARBY_VLOG(1) << "BT DeviceRemoved; name=" << device.GetName() - << ", address=" << device.GetMacAddress(); + VLOG(1) << "BT DeviceRemoved; name=" << device.GetName() + << ", address=" << device.GetMacAddress(); BluetoothDevice bt_device(&device); for (auto* observer : observer_list_.GetObservers()) { observer->DeviceRemoved(bt_device); @@ -211,9 +203,9 @@ void BluetoothClassicMedium::DeviceRemoved(api::BluetoothDevice& device) { } void BluetoothClassicMedium::DeviceAddressChanged( api::BluetoothDevice& device, absl::string_view old_address) { - NEARBY_VLOG(1) << "BT DeviceAddressChanged; name=" << device.GetName() - << ", address=" << device.GetMacAddress() - << ", old_address=" << old_address; + VLOG(1) << "BT DeviceAddressChanged; name=" << device.GetName() + << ", address=" << device.GetMacAddress() + << ", old_address=" << old_address; BluetoothDevice bt_device(&device); for (auto* observer : observer_list_.GetObservers()) { observer->DeviceAddressChanged(bt_device, old_address); @@ -221,9 +213,9 @@ void BluetoothClassicMedium::DeviceAddressChanged( } void BluetoothClassicMedium::DevicePairedChanged(api::BluetoothDevice& device, bool new_paired_status) { - NEARBY_VLOG(1) << "BT DevicePairedChanged; name=" << device.GetName() - << ", address=" << device.GetMacAddress() - << ", status=" << new_paired_status; + VLOG(1) << "BT DevicePairedChanged; name=" << device.GetName() + << ", address=" << device.GetMacAddress() + << ", status=" << new_paired_status; BluetoothDevice bt_device(&device); for (auto* observer : observer_list_.GetObservers()) { observer->DevicePairedChanged(bt_device, new_paired_status); @@ -231,9 +223,9 @@ void BluetoothClassicMedium::DevicePairedChanged(api::BluetoothDevice& device, } void BluetoothClassicMedium::DeviceConnectedStateChanged( api::BluetoothDevice& device, bool connected) { - NEARBY_VLOG(1) << "BT DeviceConnectedStateChanged: name=" << device.GetName() - << ", address=" << device.GetMacAddress() - << ", connected=" << connected; + VLOG(1) << "BT DeviceConnectedStateChanged: name=" << device.GetName() + << ", address=" << device.GetMacAddress() + << ", connected=" << connected; BluetoothDevice bt_device(&device); for (auto* observer : observer_list_.GetObservers()) { observer->DeviceConnectedStateChanged(bt_device, connected); diff --git a/internal/platform/bluetooth_classic.h b/internal/platform/bluetooth_classic.h index fe883211..6e47dade 100644 --- a/internal/platform/bluetooth_classic.h +++ b/internal/platform/bluetooth_classic.h @@ -16,12 +16,16 @@ #define PLATFORM_PUBLIC_BLUETOOTH_CLASSIC_H_ #include + #include #include #include #include +#include "absl/base/thread_annotations.h" #include "absl/container/flat_hash_map.h" +#include "absl/functional/any_invocable.h" +#include "absl/strings/string_view.h" #include "internal/base/observer_list.h" #include "internal/platform/blocking_queue_stream.h" #include "internal/platform/bluetooth_adapter.h" @@ -48,12 +52,12 @@ class BluetoothSocket : public MediumSocket { BluetoothSocket(const BluetoothSocket&) = default; BluetoothSocket& operator=(const BluetoothSocket&) = default; -// Creates a physical BluetoothSocket from a platform implementation. + // Creates a physical BluetoothSocket from a platform implementation. explicit BluetoothSocket(std::unique_ptr socket) : MediumSocket(location::nearby::proto::connections::Medium::BLUETOOTH), impl_(socket.release()) {} -// Creates a virtual BluetoothSocket from a virtual output stream. + // Creates a virtual BluetoothSocket from a virtual output stream. explicit BluetoothSocket(OutputStream* virtual_output_stream) : MediumSocket(location::nearby::proto::connections::Medium::BLUETOOTH), blocking_queue_input_stream_(std::make_shared()), diff --git a/internal/platform/implementation/windows/bluetooth_classic_medium.cc b/internal/platform/implementation/windows/bluetooth_classic_medium.cc index dfe9c721..707e2ddd 100644 --- a/internal/platform/implementation/windows/bluetooth_classic_medium.cc +++ b/internal/platform/implementation/windows/bluetooth_classic_medium.cc @@ -509,6 +509,7 @@ bool BluetoothClassicMedium::StartScanning() { } mac_address_to_bluetooth_device_map_.clear(); + removed_bluetooth_devices_map_.clear(); // The Start method can only be called when the DeviceWatcher is in the // Created, Stopped or Aborted state. @@ -720,7 +721,8 @@ winrt::fire_and_forget BluetoothClassicMedium::DeviceWatcher_Removed( observer->DeviceRemoved(*mac_address_to_bluetooth_device_map_[mac_address]); } - mac_address_to_bluetooth_device_map_.erase(mac_address); + auto node = mac_address_to_bluetooth_device_map_.extract(mac_address); + removed_bluetooth_devices_map_[mac_address] = std::move(node.mapped()); return winrt::fire_and_forget(); } diff --git a/internal/platform/implementation/windows/bluetooth_classic_medium.h b/internal/platform/implementation/windows/bluetooth_classic_medium.h index 9465f83d..d90e071a 100644 --- a/internal/platform/implementation/windows/bluetooth_classic_medium.h +++ b/internal/platform/implementation/windows/bluetooth_classic_medium.h @@ -15,10 +15,12 @@ #ifndef PLATFORM_IMPL_WINDOWS_BLUETOOTH_CLASSIC_MEDIUM_H_ #define PLATFORM_IMPL_WINDOWS_BLUETOOTH_CLASSIC_MEDIUM_H_ +#include #include #include #include +#include "absl/container/flat_hash_map.h" #include "internal/base/observer_list.h" #include "internal/platform/cancellation_flag.h" #include "internal/platform/implementation/bluetooth_adapter.h" @@ -150,9 +152,13 @@ class BluetoothClassicMedium : public api::BluetoothClassicMedium { std::string service_uuid_; // Map MAC address to bluetooth device. - std::map> + absl::flat_hash_map> mac_address_to_bluetooth_device_map_; + // Track removed devices. + absl::flat_hash_map> + removed_bluetooth_devices_map_; + BluetoothAdapter& bluetooth_adapter_; BluetoothAdapter::ScanMode scan_mode_ = BluetoothAdapter::ScanMode::kUnknown;