diff --git a/fastpair/scanning/fastpair/BUILD b/fastpair/scanning/fastpair/BUILD index a6e18638..a1c758a6 100644 --- a/fastpair/scanning/fastpair/BUILD +++ b/fastpair/scanning/fastpair/BUILD @@ -45,6 +45,7 @@ cc_library( "@com_google_absl//absl/functional:any_invocable", "@com_google_absl//absl/functional:bind_front", "@com_google_absl//absl/strings", + "@com_google_absl//absl/synchronization", ], ) diff --git a/fastpair/scanning/fastpair/fast_pair_discoverable_scanner_impl.cc b/fastpair/scanning/fastpair/fast_pair_discoverable_scanner_impl.cc index 8ab560cf..cc3daaa1 100644 --- a/fastpair/scanning/fastpair/fast_pair_discoverable_scanner_impl.cc +++ b/fastpair/scanning/fastpair/fast_pair_discoverable_scanner_impl.cc @@ -25,6 +25,7 @@ #include "absl/functional/bind_front.h" #include "absl/strings/string_view.h" +#include "absl/synchronization/mutex.h" #include "fastpair/common/constant.h" #include "fastpair/common/fast_pair_device.h" #include "fastpair/common/protocol.h" @@ -108,8 +109,10 @@ void FastPairDiscoverableScannerImpl::OnDeviceFound( << ": Device doesn't have any Fast Pair Service Data."; return; } - - model_id_parse_attempts_[peripheral.GetName()] = 1; + { + absl::MutexLock lock(&mutex_); + model_id_parse_attempts_[peripheral.GetName()] = 1; + } NEARBY_LOGS(INFO) << __func__ << ": Attempting to get model ID"; std::vector service_data; std::move(std::begin(fast_pair_service_data), @@ -125,18 +128,20 @@ void FastPairDiscoverableScannerImpl::OnDeviceFound( void FastPairDiscoverableScannerImpl::OnModelIdRetrieved( const std::string& address, const std::optional model_id) { - auto it = model_id_parse_attempts_.find(address); + { + absl::MutexLock lock(&mutex_); + auto it = model_id_parse_attempts_.find(address); - // If there's no entry in the map, the device was lost while parsing. - if (it == model_id_parse_attempts_.end()) { - NEARBY_LOGS(WARNING) - << __func__ - << ": Returning early because device as lost while parsing."; - return; + // If there's no entry in the map, the device was lost while parsing. + if (it == model_id_parse_attempts_.end()) { + NEARBY_LOGS(WARNING) + << __func__ + << ": Returning early because device as lost while parsing."; + return; + } + + model_id_parse_attempts_.erase(it); } - - model_id_parse_attempts_.erase(it); - if (!model_id.has_value()) { NEARBY_LOGS(INFO) << __func__ << ": Returning early because no model id was parsed."; @@ -182,9 +187,11 @@ void FastPairDiscoverableScannerImpl::OnDeviceMetadataRetrieved( "Ignoring this advertisement"; return; } - - FastPairDevice device(model_id, address, Protocol::kFastPairInitialPairing); - NotifyDeviceFound(device); + absl::MutexLock lock(&mutex_); + notified_devices_.insert_or_assign( + address, std::make_unique( + model_id, address, Protocol::kFastPairInitialPairing)); + NotifyDeviceFound(*notified_devices_[address]); } void FastPairDiscoverableScannerImpl::NotifyDeviceFound( @@ -192,23 +199,21 @@ void FastPairDiscoverableScannerImpl::NotifyDeviceFound( NEARBY_LOGS(VERBOSE) << "Notify Device found:" << "BluetoothAddress = " << device.GetBleAddress() << ", Model id = " << device.GetModelId(); - notified_devices_[device.GetBleAddress()] = &device; found_callback_(device); } void FastPairDiscoverableScannerImpl::OnDeviceLost( const BlePeripheral& peripheral) { NEARBY_LOGS(INFO) << __func__ << ": Running lost callback"; - + absl::MutexLock lock(&mutex_); model_id_parse_attempts_.erase(peripheral.GetName()); auto it = notified_devices_.find(peripheral.GetName()); // Don't invoke callback if we didn't notify this device. if (it == notified_devices_.end()) return; - FastPairDevice* notified_device = it->second; + lost_callback_(*it->second); notified_devices_.erase(it); - lost_callback_(*notified_device); } } // namespace fastpair diff --git a/fastpair/scanning/fastpair/fast_pair_discoverable_scanner_impl.h b/fastpair/scanning/fastpair/fast_pair_discoverable_scanner_impl.h index 6fec1981..2b798242 100644 --- a/fastpair/scanning/fastpair/fast_pair_discoverable_scanner_impl.h +++ b/fastpair/scanning/fastpair/fast_pair_discoverable_scanner_impl.h @@ -20,6 +20,7 @@ #include #include +#include "absl/synchronization/mutex.h" #include "fastpair/common/fast_pair_device.h" #include "fastpair/repository/device_metadata.h" #include "fastpair/scanning/fastpair/fast_pair_discoverable_scanner.h" @@ -74,13 +75,15 @@ class FastPairDiscoverableScannerImpl : public FastPairDiscoverableScanner, const std::string model_id, DeviceMetadata& device_metadata); void NotifyDeviceFound(FastPairDevice& device); - + absl::Mutex mutex_; std::shared_ptr scanner_; std::shared_ptr adapter_; DeviceCallback found_callback_; DeviceCallback lost_callback_; - absl::flat_hash_map notified_devices_; - absl::flat_hash_map model_id_parse_attempts_; + absl::flat_hash_map> + notified_devices_ ABSL_GUARDED_BY(mutex_); + absl::flat_hash_map model_id_parse_attempts_ + ABSL_GUARDED_BY(mutex_); ObserverList observer_list_; };