Update the GATT reading logic in thread

PiperOrigin-RevId: 793840801
This commit is contained in:
Guogang Li
2025-08-11 16:34:13 -07:00
committed by Copybara-Service
parent 804b481b73
commit f67618f8ff
4 changed files with 193 additions and 85 deletions
@@ -85,6 +85,9 @@ constexpr auto kEnablePayloadManagerToSkipChunkUpdate =
// Enable/Disable payload-received-ack feature.
constexpr auto kEnablePayloadReceivedAck =
flags::Flag<bool>(kConfigPackage, "45425840", false);
// Enable/Disable GATT query for extended advertisement.
constexpr auto kEnableReadGattForExtendedAdvertisement =
flags::Flag<bool>(kConfigPackage, "45718229", false);
// Enable/Disable safe-to-disconnect feature.
constexpr auto kEnableSafeToDisconnect =
flags::Flag<bool>(kConfigPackage, "45425789", false);
@@ -104,9 +107,6 @@ constexpr auto kMediumDefaultMaxTransmitPacketSize =
constexpr auto kMediumMaxAllowedReadBytes =
flags::Flag<int64_t>(kConfigPackage, "45669530", 1048576);
// Enable/Disable payload-received-ack feature.
// Set the safe-to-disconnect version.
// Enable 1. safe-to-disconnect check 2. reserved 3. auto-reconnect 4.
// auto-resume 5. non-distance-constraint-recovery 6. payload_ack
constexpr auto kSafeToDisconnectVersion =
flags::Flag<int64_t>(kConfigPackage, "45425841", 0);
// When true, use stable endpoint ID.
@@ -63,26 +63,55 @@ constexpr absl::Duration kInstantLostAdvertisementTimeout = absl::Seconds(60);
DiscoveredPeripheralTracker::DiscoveredPeripheralTracker(
bool is_extended_advertisement_available)
: is_extended_advertisement_available_(
is_extended_advertisement_available) {
if (NearbyFlags::GetInstance().GetBoolFlag(
: is_fetching_in_thread_(NearbyFlags::GetInstance().GetBoolFlag(
config_package_nearby::nearby_connections_feature::
kEnableGattQueryInThread)) {
kEnableGattQueryInThread)),
is_read_gatt_for_extended_advertisement_enabled_(
NearbyFlags::GetInstance().GetBoolFlag(
config_package_nearby::nearby_connections_feature::
kEnableReadGattForExtendedAdvertisement)),
is_extended_advertisement_available_(
is_extended_advertisement_available) {
LOG(INFO) << __func__ << ": fetch GATT in thread: " << is_fetching_in_thread_
<< ", read GATT for extended advertisement: "
<< is_read_gatt_for_extended_advertisement_enabled_;
if (is_fetching_in_thread_) {
executor_ = std::make_unique<MultiThreadExecutor>(kGattThreadCount);
if (is_read_gatt_for_extended_advertisement_enabled_) {
executor_->Execute([this]() { GattFetchingLoop(); });
}
}
}
DiscoveredPeripheralTracker::~DiscoveredPeripheralTracker() {
if (NearbyFlags::GetInstance().GetBoolFlag(
config_package_nearby::nearby_connections_feature::
kEnableGattQueryInThread)) {
MutexLock lock(&mutex_);
if (executor_ != nullptr) {
executor_->Shutdown();
if (is_fetching_in_thread_) {
if (is_read_gatt_for_extended_advertisement_enabled_) {
Shutdown();
} else {
MutexLock lock(&mutex_);
if (executor_ != nullptr) {
executor_->Shutdown();
}
}
}
}
void DiscoveredPeripheralTracker::Shutdown() {
MutexLock lock(&mutex_);
if (shutting_down_.Set(true)) {
return;
}
{
MutexLock lock(&task_mutex_);
cond_.Notify();
}
if (executor_ != nullptr) {
executor_->Shutdown();
}
}
void DiscoveredPeripheralTracker::StartTracking(
const std::string& service_id, bool include_dct_advertisement, Pcp pcp,
DiscoveredPeripheralCallback discovered_peripheral_callback,
@@ -722,9 +751,7 @@ void DiscoveredPeripheralTracker::HandleAdvertisementHeader(
// Determine whether or not we need to read a fresh GATT advertisement.
if (ShouldReadRawAdvertisementFromServer(advertisement_header)) {
// Determine whether or not we need to read a fresh GATT advertisement.
if (NearbyFlags::GetInstance().GetBoolFlag(
config_package_nearby::nearby_connections_feature::
kEnableGattQueryInThread)) {
if (is_fetching_in_thread_) {
VLOG(1) << ": Handle GATT advertisement header with hash "
<< absl::BytesToHexString(
advertisement_header.GetAdvertisementHash().AsStringView())
@@ -740,13 +767,26 @@ void DiscoveredPeripheralTracker::HandleAdvertisementHeader(
// The situation happens when flag value changed
executor_ = std::make_unique<MultiThreadExecutor>(kGattThreadCount);
}
executor_->Execute(
[this, peripheral, advertisement_header,
advertisement_fetcher = std::move(advertisement_fetcher),
advertisement_data = std::move(advertisement_data)]() mutable {
FetchRawAdvertisementsInThread(peripheral, advertisement_header,
std::move(advertisement_fetcher));
if (is_read_gatt_for_extended_advertisement_enabled_) {
{
MutexLock lock(&task_mutex_);
gatt_fetch_tasks_.push_back({
.peripheral = peripheral,
.advertisement_header = advertisement_header,
.advertisement_fetcher = std::move(advertisement_fetcher),
});
cond_.Notify();
}
} else {
executor_->Execute([this, peripheral, advertisement_header,
advertisement_fetcher =
std::move(advertisement_fetcher)]() mutable {
FetchRawAdvertisementsInThread(peripheral, advertisement_header,
std::move(advertisement_fetcher));
});
}
return;
} else {
std::vector<const ByteArray*> gatt_advertisement_bytes_list =
@@ -930,6 +970,33 @@ void DiscoveredPeripheralTracker::FetchRawAdvertisementsInThread(
}
}
void DiscoveredPeripheralTracker::GattFetchingLoop() {
while (true) {
GattFetchTask task;
{
MutexLock lock(&task_mutex_);
if (shutting_down_) {
gatt_fetch_tasks_.clear();
return;
}
while (gatt_fetch_tasks_.empty() && !shutting_down_) {
cond_.Wait();
}
if (shutting_down_) {
gatt_fetch_tasks_.clear();
return;
}
task = std::move(gatt_fetch_tasks_.front());
gatt_fetch_tasks_.pop_front();
}
FetchRawAdvertisementsInThread(task.peripheral, task.advertisement_header,
std::move(task.advertisement_fetcher));
}
}
void DiscoveredPeripheralTracker::UpdateCommonStateForFoundBleAdvertisement(
const BleAdvertisementHeader& advertisement_header) {
const auto ga_it = gatt_advertisements_.find(advertisement_header);
@@ -16,6 +16,7 @@
#define CORE_INTERNAL_MEDIUMS_BLE_V2_DISCOVERED_PERIPHERAL_TRACKER_H_
#include <array>
#include <deque>
#include <memory>
#include <optional>
#include <string>
@@ -33,8 +34,10 @@
#include "connections/implementation/mediums/ble_v2/discovered_peripheral_callback.h"
#include "connections/implementation/mediums/lost_entity_tracker.h"
#include "connections/implementation/pcp.h"
#include "internal/platform/atomic_boolean.h"
#include "internal/platform/ble_v2.h"
#include "internal/platform/byte_array.h"
#include "internal/platform/condition_variable.h"
#include "internal/platform/implementation/ble_v2.h"
#include "internal/platform/multi_thread_executor.h"
#include "internal/platform/mutex.h"
@@ -106,6 +109,9 @@ class DiscoveredPeripheralTracker {
// any lost peripherals.
void ProcessLostGattAdvertisements() ABSL_LOCKS_EXCLUDED(mutex_);
// Shuts down the GATT fetching thread.
void Shutdown() ABSL_LOCKS_EXCLUDED(mutex_);
private:
using BleAdvertisementSet = absl::flat_hash_set<BleAdvertisement>;
@@ -148,6 +154,12 @@ class DiscoveredPeripheralTracker {
ByteArray instant_on_lost_hash;
};
struct GattFetchTask {
BleV2Peripheral peripheral;
BleAdvertisementHeader advertisement_header;
AdvertisementFetcher advertisement_fetcher;
};
// Clears stale data from any previous sessions.
void ClearDataForServiceId(const std::string& service_id)
ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
@@ -260,6 +272,9 @@ class DiscoveredPeripheralTracker {
const BleAdvertisementHeader& advertisement_header,
AdvertisementFetcher advertisement_fetcher);
// The main loop for the GATT fetching thread.
void GattFetchingLoop();
// Updates `gatt_advertisement_infos_` map no matter whether we read a new
// GATT advertisement by the input `advertisement_header` and 'mac_address`.
void UpdateCommonStateForFoundBleAdvertisement(
@@ -290,6 +305,8 @@ class DiscoveredPeripheralTracker {
ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
Mutex mutex_;
bool is_fetching_in_thread_ = false;
bool is_read_gatt_for_extended_advertisement_enabled_ = true;
bool is_extended_advertisement_available_;
// ------------ SERVICE ID MAPS ------------
@@ -341,6 +358,11 @@ class DiscoveredPeripheralTracker {
std::unique_ptr<MultiThreadExecutor> executor_ ABSL_GUARDED_BY(mutex_) =
nullptr;
Mutex task_mutex_;
AtomicBoolean shutting_down_{false};
ConditionVariable cond_{&task_mutex_};
std::deque<GattFetchTask> gatt_fetch_tasks_ ABSL_GUARDED_BY(task_mutex_);
// Maps an advertisement header's hash with the time it's reported lost.
// Ignores subsequent discovery events for the same advertisement header.
absl::flat_hash_map<std::string, absl::Time> lost_advertisment_infos_
@@ -18,6 +18,7 @@
#include <list>
#include <memory>
#include <string>
#include <tuple>
#include <vector>
#include "gmock/gmock.h"
@@ -161,7 +162,10 @@ class MockDiscoveredPeripheralCallback : public DiscoveredPeripheralCallback {
MOCK_METHOD(void, OnLegacyDeviceDiscovered, (), ());
};
class DiscoveredPeripheralTrackerTest : public testing::TestWithParam<bool> {
class DiscoveredPeripheralTrackerTest
: public testing::TestWithParam<
std::tuple</*kEnableGattQueryInThread=*/bool,
/*kEnableReadGattForExtendedAdvertisement=*/bool>> {
public:
void SetUp() override {
NearbyFlags::GetInstance().OverrideBoolFlagValue(
@@ -174,8 +178,14 @@ class DiscoveredPeripheralTrackerTest : public testing::TestWithParam<bool> {
NearbyFlags::GetInstance().OverrideBoolFlagValue(
config_package_nearby::nearby_connections_feature::
kEnableGattQueryInThread,
GetParam());
std::get<0>(GetParam()));
NearbyFlags::GetInstance().OverrideBoolFlagValue(
config_package_nearby::nearby_connections_feature::
kEnableReadGattForExtendedAdvertisement,
std::get<1>(GetParam()));
MediumEnvironment::Instance().Start();
discovered_peripheral_tracker_ =
std::make_unique<DiscoveredPeripheralTracker>();
adapter_peripheral_ = std::make_unique<BluetoothAdapter>();
adapter_central_ = std::make_unique<BluetoothAdapter>();
ble_peripheral_ = std::make_unique<BleV2Medium>(*adapter_peripheral_);
@@ -183,6 +193,7 @@ class DiscoveredPeripheralTrackerTest : public testing::TestWithParam<bool> {
}
void TearDown() override {
discovered_peripheral_tracker_.reset();
MediumEnvironment::Instance().Stop();
NearbyFlags::GetInstance().ResetOverridedValues();
}
@@ -200,7 +211,7 @@ class DiscoveredPeripheralTrackerTest : public testing::TestWithParam<bool> {
CountDownLatch& fetch_latch) {
BleV2Peripheral peripheral = CreateBlePeripheral();
discovered_peripheral_tracker_.ProcessFoundBleAdvertisement(
discovered_peripheral_tracker_->ProcessFoundBleAdvertisement(
peripheral, advertisement_data,
GetAdvertisementFetcher(fetch_latch, advertisement_bytes_list));
}
@@ -212,7 +223,7 @@ class DiscoveredPeripheralTrackerTest : public testing::TestWithParam<bool> {
CountDownLatch& fetch_latch) {
BleV2Peripheral peripheral = CreateBlePeripheral();
discovered_peripheral_tracker_.ProcessFoundBleAdvertisement(
discovered_peripheral_tracker_->ProcessFoundBleAdvertisement(
peripheral, advertisement_data,
GetAdvertisementFetcher(fetch_latch, advertisement_bytes_list));
}
@@ -223,7 +234,7 @@ class DiscoveredPeripheralTrackerTest : public testing::TestWithParam<bool> {
CountDownLatch& fetch_latch) {
BleV2Peripheral peripheral = CreateBlePeripheral();
discovered_peripheral_tracker_.ProcessFoundBleAdvertisement(
discovered_peripheral_tracker_->ProcessFoundBleAdvertisement(
peripheral, advertisement_data,
GetSlowAdvertisementFetcher(fetch_latch, advertisement_bytes_list));
}
@@ -246,13 +257,6 @@ class DiscoveredPeripheralTrackerTest : public testing::TestWithParam<bool> {
true);
}
void EnableFetchGattAdvertisementInThread() {
NearbyFlags::GetInstance().OverrideBoolFlagValue(
config_package_nearby::nearby_connections_feature::
kEnableGattQueryInThread,
true);
}
protected:
// A stub Advertisement fetcher.
DiscoveredPeripheralTracker::AdvertisementFetcher GetAdvertisementFetcher(
@@ -301,7 +305,7 @@ class DiscoveredPeripheralTrackerTest : public testing::TestWithParam<bool> {
std::unique_ptr<BleV2Medium> ble_central_;
mutable Mutex mutex_;
int fetch_count_ ABSL_GUARDED_BY(mutex_) = 0;
DiscoveredPeripheralTracker discovered_peripheral_tracker_;
std::unique_ptr<DiscoveredPeripheralTracker> discovered_peripheral_tracker_;
};
TEST_P(DiscoveredPeripheralTrackerTest,
@@ -311,7 +315,7 @@ TEST_P(DiscoveredPeripheralTrackerTest,
CountDownLatch found_latch(1);
CountDownLatch fetch_latch(1);
discovered_peripheral_tracker_.StartTracking(
discovered_peripheral_tracker_->StartTracking(
std::string(kServiceIdA), false, Pcp::kP2pPointToPoint,
{
.peripheral_discovered_cb =
@@ -347,7 +351,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, DctAdvertisementPeripheralDiscovered) {
CountDownLatch found_latch(1);
CountDownLatch fetch_latch(1);
discovered_peripheral_tracker_.StartTracking(
discovered_peripheral_tracker_->StartTracking(
std::string(kServiceIdA), true, Pcp::kP2pPointToPoint,
{
.peripheral_discovered_cb =
@@ -387,7 +391,7 @@ TEST_P(DiscoveredPeripheralTrackerTest,
CountDownLatch legacy_found_latch(1);
CountDownLatch fetch_latch(1);
discovered_peripheral_tracker_.StartTracking(
discovered_peripheral_tracker_->StartTracking(
std::string(kServiceIdA), false, Pcp::kP2pPointToPoint,
{.peripheral_discovered_cb =
[&found_latch](
@@ -425,7 +429,7 @@ TEST_P(DiscoveredPeripheralTrackerTest,
std::atomic<int> callback_times = 0;
// 1st tracking.
discovered_peripheral_tracker_.StartTracking(
discovered_peripheral_tracker_->StartTracking(
std::string(kServiceIdA), false, Pcp::kP2pPointToPoint,
{
.peripheral_discovered_cb =
@@ -450,7 +454,7 @@ TEST_P(DiscoveredPeripheralTrackerTest,
FindFastAdvertisement(advertisement_data, {}, fetch_latch);
// 2nd tracking.
discovered_peripheral_tracker_.StartTracking(
discovered_peripheral_tracker_->StartTracking(
std::string(kServiceIdA), false, Pcp::kP2pPointToPoint,
{
.peripheral_discovered_cb =
@@ -469,7 +473,7 @@ TEST_P(DiscoveredPeripheralTrackerTest,
FindFastAdvertisement(advertisement_data, {}, fetch_latch);
// 3rd tracking.
discovered_peripheral_tracker_.StartTracking(
discovered_peripheral_tracker_->StartTracking(
std::string(kServiceIdA), false, Pcp::kP2pPointToPoint,
{
.peripheral_discovered_cb =
@@ -496,7 +500,7 @@ TEST_P(DiscoveredPeripheralTrackerTest,
CountDownLatch fetch_latch(3);
std::atomic<int> callback_times = 0;
discovered_peripheral_tracker_.StartTracking(
discovered_peripheral_tracker_->StartTracking(
std::string(kServiceIdA), false, Pcp::kP2pPointToPoint,
{
.peripheral_discovered_cb =
@@ -541,7 +545,7 @@ TEST_P(DiscoveredPeripheralTrackerTest,
// Start tracking a service ID and then process a discovery containing a valid
// fast advertisement, but under a different service UUID.
discovered_peripheral_tracker_.StartTracking(
discovered_peripheral_tracker_->StartTracking(
std::string(kServiceIdA), false, Pcp::kP2pPointToPoint,
{
.peripheral_discovered_cb =
@@ -584,7 +588,7 @@ TEST_P(DiscoveredPeripheralTrackerTest,
CountDownLatch found_latch_b(1);
CountDownLatch fetch_latch(1);
discovered_peripheral_tracker_.StartTracking(
discovered_peripheral_tracker_->StartTracking(
std::string(kServiceIdA), false, Pcp::kP2pPointToPoint,
{
.peripheral_discovered_cb =
@@ -598,7 +602,7 @@ TEST_P(DiscoveredPeripheralTrackerTest,
},
},
Uuid(kFastAdvertisementServiceUuid));
discovered_peripheral_tracker_.StartTracking(
discovered_peripheral_tracker_->StartTracking(
std::string(kServiceIdB), false, Pcp::kP2pPointToPoint,
{
.peripheral_discovered_cb =
@@ -643,7 +647,7 @@ TEST_P(DiscoveredPeripheralTrackerTest,
CountDownLatch found_latch(1);
CountDownLatch fetch_latch(1);
discovered_peripheral_tracker_.StartTracking(
discovered_peripheral_tracker_->StartTracking(
std::string(kServiceIdA), false, Pcp::kP2pPointToPoint,
{
.peripheral_discovered_cb =
@@ -683,7 +687,7 @@ TEST_P(DiscoveredPeripheralTrackerTest,
CountDownLatch found_latch(1);
CountDownLatch fetch_latch(1);
discovered_peripheral_tracker_.StartTracking(
discovered_peripheral_tracker_->StartTracking(
std::string(kServiceIdA), false, Pcp::kP2pPointToPoint,
{
.peripheral_discovered_cb =
@@ -724,7 +728,7 @@ TEST_P(DiscoveredPeripheralTrackerTest,
CountDownLatch fetch_latch(1);
std::atomic<int> callback_times = 0;
discovered_peripheral_tracker_.StartTracking(
discovered_peripheral_tracker_->StartTracking(
std::string(kServiceIdA), false, Pcp::kP2pPointToPoint,
{
.peripheral_discovered_cb =
@@ -770,7 +774,7 @@ TEST_P(DiscoveredPeripheralTrackerTest,
CountDownLatch fetch_latch(3);
std::atomic<int> callback_times = 0;
discovered_peripheral_tracker_.StartTracking(
discovered_peripheral_tracker_->StartTracking(
std::string(kServiceIdA), false, Pcp::kP2pPointToPoint,
{
.peripheral_discovered_cb =
@@ -818,7 +822,7 @@ TEST_P(DiscoveredPeripheralTrackerTest,
CountDownLatch found_latch(1);
CountDownLatch fetch_latch(1);
discovered_peripheral_tracker_.StartTracking(
discovered_peripheral_tracker_->StartTracking(
std::string(kServiceIdA), false, Pcp::kP2pPointToPoint,
{
.peripheral_discovered_cb =
@@ -855,7 +859,7 @@ TEST_P(DiscoveredPeripheralTrackerTest,
CountDownLatch fetch_latch(1);
std::atomic<int> lost_callback_times = 0;
discovered_peripheral_tracker_.StartTracking(
discovered_peripheral_tracker_->StartTracking(
std::string(kServiceIdA), false, Pcp::kP2pPointToPoint,
{
.peripheral_discovered_cb =
@@ -899,8 +903,8 @@ TEST_P(DiscoveredPeripheralTrackerTest,
// Then, go through two cycles of onLost. The first cycle should include the
// recently discovered peripheral in its 'found' pool. The second one should
// trigger the onLost callback.
discovered_peripheral_tracker_.ProcessLostGattAdvertisements();
discovered_peripheral_tracker_.ProcessLostGattAdvertisements();
discovered_peripheral_tracker_->ProcessLostGattAdvertisements();
discovered_peripheral_tracker_->ProcessLostGattAdvertisements();
// We should receive a client callback of a lost peripheral.
EXPECT_FALSE(lost_latch.Await(kWaitDuration).result());
@@ -918,7 +922,7 @@ TEST_P(DiscoveredPeripheralTrackerTest,
CountDownLatch lost_latch(1);
CountDownLatch fetch_latch(1);
discovered_peripheral_tracker_.StartTracking(
discovered_peripheral_tracker_->StartTracking(
std::string(kServiceIdA), false, Pcp::kP2pPointToPoint,
{
.peripheral_discovered_cb =
@@ -953,7 +957,7 @@ TEST_P(DiscoveredPeripheralTrackerTest,
for (int i = 0; i < 20; i++) {
FindFastAdvertisement(advertisement_data, {fast_advertisement_bytes},
fetch_latch);
discovered_peripheral_tracker_.ProcessLostGattAdvertisements();
discovered_peripheral_tracker_->ProcessLostGattAdvertisements();
}
// We should only receive ONE client callback of a peripheral discovery, ZERO
@@ -975,7 +979,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, LostPeripheralForAdvertisementLost) {
CountDownLatch lost_latch(1);
CountDownLatch fetch_latch(1);
discovered_peripheral_tracker_.StartTracking(
discovered_peripheral_tracker_->StartTracking(
std::string(kServiceIdA), false, Pcp::kP2pPointToPoint,
{
.peripheral_discovered_cb =
@@ -1011,8 +1015,8 @@ TEST_P(DiscoveredPeripheralTrackerTest, LostPeripheralForAdvertisementLost) {
// Then, go through two cycles of onLost. The first cycle should include the
// recently discovered peripheral in its 'found' pool. The second one should
// trigger the onLost callback.
discovered_peripheral_tracker_.ProcessLostGattAdvertisements();
discovered_peripheral_tracker_.ProcessLostGattAdvertisements();
discovered_peripheral_tracker_->ProcessLostGattAdvertisements();
discovered_peripheral_tracker_->ProcessLostGattAdvertisements();
// We should receive a client callback of a lost peripheral
EXPECT_TRUE(lost_latch.Await(kWaitDuration).result());
@@ -1034,7 +1038,7 @@ TEST_P(DiscoveredPeripheralTrackerTest,
CountDownLatch lost_latch_b(1);
CountDownLatch fetch_latch(1);
discovered_peripheral_tracker_.StartTracking(
discovered_peripheral_tracker_->StartTracking(
std::string(kServiceIdA), false, Pcp::kP2pPointToPoint,
{
.peripheral_discovered_cb =
@@ -1053,7 +1057,7 @@ TEST_P(DiscoveredPeripheralTrackerTest,
bool fast_advertisement) { lost_latch_a.CountDown(); },
},
Uuid(kFastAdvertisementServiceUuid));
discovered_peripheral_tracker_.StartTracking(
discovered_peripheral_tracker_->StartTracking(
std::string(kServiceIdB), false, Pcp::kP2pPointToPoint,
{
.peripheral_discovered_cb =
@@ -1094,8 +1098,8 @@ TEST_P(DiscoveredPeripheralTrackerTest,
// Then, go through two cycles of onLost. The first cycle should include the
// recently discovered peripheral in its 'found' pool. The second one should
// trigger the onLost callback.
discovered_peripheral_tracker_.ProcessLostGattAdvertisements();
discovered_peripheral_tracker_.ProcessLostGattAdvertisements();
discovered_peripheral_tracker_->ProcessLostGattAdvertisements();
discovered_peripheral_tracker_->ProcessLostGattAdvertisements();
// We should receive two client callbacks of a lost peripheral from each
// service ID.
@@ -1115,7 +1119,7 @@ TEST_P(DiscoveredPeripheralTrackerTest,
CountDownLatch lost_latch(1);
CountDownLatch fetch_latch(1);
discovered_peripheral_tracker_.StartTracking(
discovered_peripheral_tracker_->StartTracking(
std::string(kServiceIdA), false, Pcp::kP2pPointToPoint,
{
.peripheral_discovered_cb =
@@ -1149,9 +1153,9 @@ TEST_P(DiscoveredPeripheralTrackerTest,
EXPECT_EQ(GetFetchAdvertisementCallbackCount(), 1);
// Then, stop tracking the service ID and go through two cycles of onLost.
discovered_peripheral_tracker_.StopTracking(std::string(kServiceIdA));
discovered_peripheral_tracker_.ProcessLostGattAdvertisements();
discovered_peripheral_tracker_.ProcessLostGattAdvertisements();
discovered_peripheral_tracker_->StopTracking(std::string(kServiceIdA));
discovered_peripheral_tracker_->ProcessLostGattAdvertisements();
discovered_peripheral_tracker_->ProcessLostGattAdvertisements();
// We should NOT receive a client callback of a lost peripheral
EXPECT_FALSE(lost_latch.Await(kWaitDuration).result());
@@ -1169,7 +1173,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, LostPeripheralForInstantOnLost) {
CountDownLatch lost_latch(1);
CountDownLatch fetch_latch(1);
discovered_peripheral_tracker_.StartTracking(
discovered_peripheral_tracker_->StartTracking(
std::string(kServiceIdA), false, Pcp::kP2pPointToPoint,
{
.peripheral_discovered_cb =
@@ -1216,7 +1220,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, LostPeripheralForInstantOnLost) {
// Then, go through a cycle of onLost. Since we triggered a forced loss via
// the instant on los advertisement, the lost call should trigger the onLost
// client callback.
discovered_peripheral_tracker_.ProcessLostGattAdvertisements();
discovered_peripheral_tracker_->ProcessLostGattAdvertisements();
// We should receive a client callback of a lost peripheral
EXPECT_TRUE(lost_latch.Await(kWaitDuration).result());
@@ -1235,7 +1239,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, InstantLostPeripheralForInstantOnLost) {
CountDownLatch lost_latch(1);
CountDownLatch fetch_latch(1);
discovered_peripheral_tracker_.StartTracking(
discovered_peripheral_tracker_->StartTracking(
std::string(kServiceIdA), false, Pcp::kP2pPointToPoint,
{
.peripheral_discovered_cb =
@@ -1282,7 +1286,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, InstantLostPeripheralForInstantOnLost) {
// Then, go through a cycle of onLost. Since we triggered a forced loss via
// the instant on los advertisement, the lost call should trigger the onLost
// client callback.
discovered_peripheral_tracker_.ProcessLostGattAdvertisements();
discovered_peripheral_tracker_->ProcessLostGattAdvertisements();
// We should receive a client callback of a lost peripheral
EXPECT_TRUE(lost_latch.Await(kWaitDuration).result());
@@ -1302,7 +1306,7 @@ TEST_P(DiscoveredPeripheralTrackerTest,
CountDownLatch fetch_latch(1);
MockDiscoveredPeripheralCallback mock_callback;
discovered_peripheral_tracker_.StartTracking(
discovered_peripheral_tracker_->StartTracking(
std::string(kServiceIdA), false, Pcp::kP2pPointToPoint,
{
.peripheral_discovered_cb =
@@ -1356,7 +1360,7 @@ TEST_P(DiscoveredPeripheralTrackerTest,
// Then, go through a cycle of onLost. Since we triggered a forced loss via
// the instant on los advertisement, the lost call should trigger the onLost
// client callback.
discovered_peripheral_tracker_.ProcessLostGattAdvertisements();
discovered_peripheral_tracker_->ProcessLostGattAdvertisements();
// Lost advertisement should not be reported.
CountDownLatch fetch_latch3(1);
@@ -1381,7 +1385,7 @@ TEST_P(DiscoveredPeripheralTrackerTest,
CountDownLatch lost_latch(1);
CountDownLatch fetch_latch(1);
discovered_peripheral_tracker_.StartTracking(
discovered_peripheral_tracker_->StartTracking(
std::string(kServiceIdA), false, Pcp::kP2pPointToPoint,
{
.peripheral_discovered_cb =
@@ -1441,7 +1445,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, HandleDummyAdvertisement) {
CountDownLatch fetch_latch(1);
CountDownLatch legacy_device_found_latch(1);
discovered_peripheral_tracker_.StartTracking(
discovered_peripheral_tracker_->StartTracking(
std::string(kServiceIdA), false, Pcp::kP2pPointToPoint,
{
.peripheral_discovered_cb =
@@ -1477,7 +1481,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, SkipDummyAdvertisement) {
CountDownLatch fetch_latch(1);
CountDownLatch legacy_device_found_latch(1);
discovered_peripheral_tracker_.StartTracking(
discovered_peripheral_tracker_->StartTracking(
std::string(kServiceIdA), false, Pcp::kP2pPointToPoint,
{
.peripheral_discovered_cb =
@@ -1500,7 +1504,11 @@ TEST_P(DiscoveredPeripheralTrackerTest, SkipDummyAdvertisement) {
}
TEST_P(DiscoveredPeripheralTrackerTest, FetchGattAdvertisementInThread) {
EnableFetchGattAdvertisementInThread();
if (!NearbyFlags::GetInstance().GetBoolFlag(
config_package_nearby::nearby_connections_feature::
kEnableGattQueryInThread)) {
return;
}
std::vector<std::string> service_ids = {std::string(kServiceIdA)};
ByteArray advertisement_header_bytes = CreateBleAdvertisementHeader(
GenerateRandomAdvertisementHash(), service_ids);
@@ -1510,7 +1518,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, FetchGattAdvertisementInThread) {
CountDownLatch found_latch(1);
CountDownLatch fetch_latch(1);
discovered_peripheral_tracker_.StartTracking(
discovered_peripheral_tracker_->StartTracking(
std::string(kServiceIdA), false, Pcp::kP2pPointToPoint,
{
.peripheral_discovered_cb =
@@ -1542,7 +1550,12 @@ TEST_P(DiscoveredPeripheralTrackerTest, FetchGattAdvertisementInThread) {
TEST_P(DiscoveredPeripheralTrackerTest,
IgnoreGattAdvertisementResultWhentrackingStoppedInThread) {
EnableFetchGattAdvertisementInThread();
if (!NearbyFlags::GetInstance().GetBoolFlag(
config_package_nearby::nearby_connections_feature::
kEnableGattQueryInThread)) {
return;
}
std::vector<std::string> service_ids = {std::string(kServiceIdA)};
ByteArray advertisement_header_bytes = CreateBleAdvertisementHeader(
GenerateRandomAdvertisementHash(), service_ids);
@@ -1552,7 +1565,7 @@ TEST_P(DiscoveredPeripheralTrackerTest,
CountDownLatch found_latch(1);
CountDownLatch fetch_latch(1);
discovered_peripheral_tracker_.StartTracking(
discovered_peripheral_tracker_->StartTracking(
std::string(kServiceIdA), false, Pcp::kP2pPointToPoint,
{
.peripheral_discovered_cb =
@@ -1578,7 +1591,7 @@ TEST_P(DiscoveredPeripheralTrackerTest,
// We should receive a client callback of a peripheral discovery.
absl::SleepFor(absl::Milliseconds(20));
discovered_peripheral_tracker_.StopTracking(std::string(kServiceIdA));
discovered_peripheral_tracker_->StopTracking(std::string(kServiceIdA));
fetch_latch.Await(kWaitDuration);
EXPECT_FALSE(found_latch.Await(kWaitDuration).result());
EXPECT_EQ(GetFetchAdvertisementCallbackCount(), 1);
@@ -1586,7 +1599,11 @@ TEST_P(DiscoveredPeripheralTrackerTest,
TEST_P(DiscoveredPeripheralTrackerTest,
FetchMultipleGattAdvertisementResultsInThread) {
EnableFetchGattAdvertisementInThread();
if (!NearbyFlags::GetInstance().GetBoolFlag(
config_package_nearby::nearby_connections_feature::
kEnableGattQueryInThread)) {
return;
}
std::vector<std::string> service_ids = {std::string(kServiceIdA)};
ByteArray advertisement_header_bytes = CreateBleAdvertisementHeader(
GenerateRandomAdvertisementHash(), service_ids);
@@ -1601,7 +1618,7 @@ TEST_P(DiscoveredPeripheralTrackerTest,
CountDownLatch found_latch(2);
CountDownLatch fetch_latch(2);
discovered_peripheral_tracker_.StartTracking(
discovered_peripheral_tracker_->StartTracking(
std::string(kServiceIdA), false, Pcp::kP2pPointToPoint,
{
.peripheral_discovered_cb =
@@ -1639,9 +1656,11 @@ TEST_P(DiscoveredPeripheralTrackerTest,
EXPECT_EQ(GetFetchAdvertisementCallbackCount(), 2);
}
INSTANTIATE_TEST_SUITE_P(DiscoveredPeripheralTrackerFlagsTest,
DiscoveredPeripheralTrackerTest,
/*kEnableGattQueryInThread=*/testing::Bool());
INSTANTIATE_TEST_SUITE_P(
DiscoveredPeripheralTrackerFlagsTest, DiscoveredPeripheralTrackerTest,
::testing::Combine(
/*kEnableGattQueryInThread=*/testing::Bool(),
/*kEnableReadGattForExtendedAdvertisement=*/testing::Bool()));
} // namespace