Fix BlePeripheral pointer protection bug

PiperOrigin-RevId: 452884820
This commit is contained in:
aaronyujiaze
2022-06-03 19:08:15 -07:00
committed by Copybara-Service
parent 47775bba3d
commit 71974b2c0b
3 changed files with 28 additions and 3 deletions
@@ -21,6 +21,7 @@
#include "absl/strings/escaping.h"
#include "absl/synchronization/mutex.h"
#include "internal/platform/implementation/windows/ble_peripheral.h"
#include "internal/platform/implementation/windows/bluetooth_adapter.h"
#include "internal/platform/implementation/windows/utils.h"
#include "internal/platform/logging.h"
@@ -434,7 +435,7 @@ void BleMedium::AdvertisementReceivedHandler(
// Handle all BLE advertisements and determine whether the BLE Medium
// Advertisement Scan Response packet (containing Copresence UUID 0xFEF3 in
// 0x16 Service Data) has been received in the handler
absl::MutexLock lock(&peripheral_map_mutex_);
BluetoothLEAdvertisement advertisement = args.Advertisement();
for (BluetoothLEAdvertisementDataSection service_data :
@@ -463,24 +464,35 @@ void BleMedium::AdvertisementReceivedHandler(
std::string peripheral_name =
uint64_to_mac_address_string(args.BluetoothAddress());
std::unique_ptr<BlePeripheral> peripheral =
std::make_unique<BlePeripheral>();
peripheral->SetName(peripheral_name);
peripheral->SetAdvertisementBytes(advertisement_data);
if (peripheral_map_.contains(peripheral_name)) {
if (peripheral_map_[peripheral_name]->GetAdvertisementBytes(
service_id_) == advertisement_data) {
return;
}
}
BlePeripheral* peripheral_ptr = peripheral.get();
peripheral_map_.emplace(peripheral_name, std::move(peripheral));
// Received Fast Advertisement packet
if (unconsumed_buffer_length <= 27) {
NEARBY_LOGS(INFO)
<< "Sending Fast Advertisement packet for processing.";
advertisement_received_callback_.peripheral_discovered_cb(
/*ble_peripheral*/ *(peripheral.get()), /*service_id*/ service_id_,
/*ble_peripheral*/ *peripheral_ptr, /*service_id*/ service_id_,
/*is_fast_advertisement*/ true);
} else {
// Received Extended Advertising packet
NEARBY_LOGS(INFO)
<< "Sending Extended Advertising packet for processing.";
advertisement_received_callback_.peripheral_discovered_cb(
/*ble_peripheral*/ *(peripheral.get()), /*service_id*/ service_id_,
/*ble_peripheral*/ *peripheral_ptr, /*service_id*/ service_id_,
/*is_fast_advertisement*/ false);
}
}
@@ -19,8 +19,11 @@
#include <functional>
#include <future> // NOLINT
#include <memory>
#include <string>
#include <utility>
#include "absl/container/flat_hash_map.h"
#include "absl/synchronization/mutex.h"
#include "internal/platform/implementation/ble.h"
#include "internal/platform/implementation/bluetooth_adapter.h"
@@ -92,6 +95,12 @@ class BleMedium : public api::BleMedium {
DiscoveredPeripheralCallback advertisement_received_callback_;
// Map to protect the pointer for BlePeripheral because
// DiscoveredPeripheralCallback only keeps the pointer to the object
absl::Mutex peripheral_map_mutex_;
absl::flat_hash_map<std::string, std::unique_ptr<BlePeripheral>>
peripheral_map_ ABSL_GUARDED_BY(peripheral_map_mutex_);
// WinRT objects
::winrt::Windows::Devices::Bluetooth::Advertisement::
BluetoothLEAdvertisementPublisher publisher_;
@@ -102,6 +102,10 @@ void BluetoothSocket::Connect(HostName connectionHostName,
std::make_unique<BluetoothInputStream>(windows_socket_.InputStream());
output_stream_ =
std::make_unique<BluetoothOutputStream>(windows_socket_.OutputStream());
NEARBY_LOGS(INFO) << __func__
<< ": Bluetooth socket successfully connected to "
<< bluetooth_device_->GetName();
}
BluetoothSocket::BluetoothInputStream::BluetoothInputStream(