Internal fix

PiperOrigin-RevId: 684640919
This commit is contained in:
Guogang Li
2024-10-10 18:04:17 -07:00
committed by Copybara-Service
parent 05ae5583f8
commit 1255f6a800
6 changed files with 146 additions and 150 deletions
@@ -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<BluetoothSocket*>(
@@ -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<BluetoothSocket*>(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<BluetoothSocket*>(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;
+1
View File
@@ -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",
+49 -57
View File
@@ -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<std::string, std::shared_ptr<MediumSocket>>*
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<BluetoothSocket>(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<DeviceDiscoveryInfo>());
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);
+6 -2
View File
@@ -16,12 +16,16 @@
#define PLATFORM_PUBLIC_BLUETOOTH_CLASSIC_H_
#include <stdbool.h>
#include <memory>
#include <optional>
#include <string>
#include <utility>
#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<api::BluetoothSocket> 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<BlockingQueueStream>()),
@@ -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();
}
@@ -15,10 +15,12 @@
#ifndef PLATFORM_IMPL_WINDOWS_BLUETOOTH_CLASSIC_MEDIUM_H_
#define PLATFORM_IMPL_WINDOWS_BLUETOOTH_CLASSIC_MEDIUM_H_
#include <list>
#include <map>
#include <memory>
#include <string>
#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<std::string, std::unique_ptr<BluetoothDevice>>
absl::flat_hash_map<std::string, std::unique_ptr<BluetoothDevice>>
mac_address_to_bluetooth_device_map_;
// Track removed devices.
absl::flat_hash_map<std::string, std::unique_ptr<BluetoothDevice>>
removed_bluetooth_devices_map_;
BluetoothAdapter& bluetooth_adapter_;
BluetoothAdapter::ScanMode scan_mode_ = BluetoothAdapter::ScanMode::kUnknown;