mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-14 22:56:12 -04:00
Fixed the issues to handle on lost advertisement
PiperOrigin-RevId: 641268507
This commit is contained in:
committed by
Copybara-Service
parent
c3924eb8df
commit
bc66c63a48
@@ -80,13 +80,17 @@ cc_test(
|
||||
],
|
||||
deps = [
|
||||
":ble_v2",
|
||||
"//connections/implementation/mediums:utils",
|
||||
"//internal/platform:base",
|
||||
"//internal/platform:comm",
|
||||
"//internal/platform:test_util",
|
||||
"//internal/platform:types",
|
||||
"//internal/platform:uuid",
|
||||
"//internal/platform/implementation:comm",
|
||||
"//internal/platform/implementation/g3", # buildcleaner: keep
|
||||
"//proto/mediums:ble_frames_cc_proto",
|
||||
"@com_github_protobuf_matchers//protobuf-matchers",
|
||||
"@com_google_absl//absl/base:core_headers",
|
||||
"@com_google_absl//absl/hash:hash_testing",
|
||||
"@com_google_absl//absl/status",
|
||||
"@com_google_absl//absl/status:statusor",
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "absl/container/flat_hash_map.h"
|
||||
#include "absl/status/statusor.h"
|
||||
#include "absl/strings/escaping.h"
|
||||
#include "connections/implementation/flags/nearby_connections_feature_flags.h"
|
||||
@@ -31,6 +32,7 @@
|
||||
#include "connections/implementation/mediums/ble_v2/bloom_filter.h"
|
||||
#include "connections/implementation/mediums/ble_v2/discovered_peripheral_callback.h"
|
||||
#include "connections/implementation/mediums/ble_v2/instant_on_lost_advertisement.h"
|
||||
#include "connections/implementation/mediums/lost_entity_tracker.h"
|
||||
#include "internal/flags/nearby_flags.h"
|
||||
#include "internal/platform/ble_v2.h"
|
||||
#include "internal/platform/byte_array.h"
|
||||
@@ -39,6 +41,7 @@
|
||||
#include "internal/platform/logging.h"
|
||||
#include "internal/platform/multi_thread_executor.h"
|
||||
#include "internal/platform/mutex_lock.h"
|
||||
#include "internal/platform/uuid.h"
|
||||
|
||||
using ::nearby::api::ble_v2::BleAdvertisementData;
|
||||
|
||||
@@ -173,18 +176,27 @@ bool DiscoveredPeripheralTracker::HandleOnLostAdvertisementLocked(
|
||||
<< ": Discarding OnLost advertisement for untracked service_id";
|
||||
return false;
|
||||
}
|
||||
auto advertisements =
|
||||
|
||||
auto gatt_advertisements =
|
||||
gatt_advertisements_[it.second.advertisement_header];
|
||||
for (const auto& advertisement : advertisements) {
|
||||
if (advertisement.IsValid()) {
|
||||
|
||||
// Need to report OnLost for each gatt_advertisement.
|
||||
for (const auto& gatt_advertisement : gatt_advertisements) {
|
||||
BleV2Peripheral lost_peripheral = it.second.peripheral;
|
||||
lost_peripheral.SetId(ByteArray(gatt_advertisement));
|
||||
if (gatt_advertisement.IsValid()) {
|
||||
discovery_cb_it->second.discovered_peripheral_callback
|
||||
.peripheral_lost_cb(peripheral, it.second.service_id,
|
||||
advertisement.GetData(), false);
|
||||
.peripheral_lost_cb(lost_peripheral, it.second.service_id,
|
||||
gatt_advertisement.GetData(),
|
||||
gatt_advertisement.IsFastAdvertisement());
|
||||
NEARBY_LOGS(INFO) << __func__ << ": OnLost triggered for service_id "
|
||||
<< it.second.service_id;
|
||||
return true;
|
||||
}
|
||||
|
||||
ClearGattAdvertisement(gatt_advertisement);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
|
||||
@@ -16,20 +16,31 @@
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "gmock/gmock.h"
|
||||
#include "protobuf-matchers/protocol-buffer-matchers.h"
|
||||
#include "gtest/gtest.h"
|
||||
#include "absl/base/thread_annotations.h"
|
||||
#include "absl/strings/string_view.h"
|
||||
#include "absl/time/time.h"
|
||||
#include "connections/implementation/mediums/ble_v2/advertisement_read_result.h"
|
||||
#include "connections/implementation/mediums/ble_v2/ble_advertisement.h"
|
||||
#include "connections/implementation/mediums/ble_v2/ble_advertisement_header.h"
|
||||
#include "connections/implementation/mediums/ble_v2/ble_utils.h"
|
||||
#include "connections/implementation/mediums/ble_v2/bloom_filter.h"
|
||||
#include "connections/implementation/mediums/ble_v2/instant_on_lost_advertisement.h"
|
||||
#include "connections/implementation/mediums/utils.h"
|
||||
#include "internal/platform/ble_v2.h"
|
||||
#include "internal/platform/bluetooth_adapter.h"
|
||||
#include "internal/platform/byte_array.h"
|
||||
#include "internal/platform/count_down_latch.h"
|
||||
#include "internal/platform/feature_flags.h"
|
||||
#include "internal/platform/implementation/ble_v2.h"
|
||||
#include "internal/platform/medium_environment.h"
|
||||
#include "internal/platform/mutex.h"
|
||||
#include "internal/platform/mutex_lock.h"
|
||||
#include "internal/platform/uuid.h"
|
||||
|
||||
namespace nearby {
|
||||
namespace connections {
|
||||
@@ -74,6 +85,19 @@ ByteArray CreateLegacyBleAdvertisement(const std::string& service_id,
|
||||
data, device_token, BleAdvertisementHeader::kDefaultPsmValue));
|
||||
}
|
||||
|
||||
BleAdvertisementHeader CreateFastBleAdvertisementHeader(
|
||||
const ByteArray& advertisement_bytes) {
|
||||
BloomFilter bloom_filter(
|
||||
std::make_unique<BitSetImpl<
|
||||
BleAdvertisementHeader::kServiceIdBloomFilterByteLength>>());
|
||||
|
||||
return BleAdvertisementHeader(
|
||||
BleAdvertisementHeader::Version::kV2, /*extended_advertisement=*/false,
|
||||
/*num_slots=*/1, ByteArray(bloom_filter),
|
||||
bleutils::GenerateAdvertisementHash(advertisement_bytes),
|
||||
/*psm=*/BleAdvertisementHeader::kDefaultPsmValue);
|
||||
}
|
||||
|
||||
ByteArray CreateBleAdvertisementHeader(const ByteArray& advertisement_hash,
|
||||
int psm,
|
||||
std::vector<std::string>& service_ids) {
|
||||
@@ -812,7 +836,7 @@ TEST_F(DiscoveredPeripheralTrackerTest, LostPeripheralForAdvertisementLost) {
|
||||
EXPECT_EQ(GetFetchAdvertisementCallbackCount(), 1);
|
||||
|
||||
// Then, go through two cycles of onLost. The first cycle should include the
|
||||
// recently discovered eripheral in its 'found' pool. The second one should
|
||||
// recently discovered peripheral in its 'found' pool. The second one should
|
||||
// trigger the onLost callback.
|
||||
discovered_peripheral_tracker_.ProcessLostGattAdvertisements();
|
||||
discovered_peripheral_tracker_.ProcessLostGattAdvertisements();
|
||||
@@ -1024,6 +1048,69 @@ TEST_F(DiscoveredPeripheralTrackerTest, LostPeripheralForInstantOnLost) {
|
||||
EXPECT_TRUE(lost_latch.Await(kWaitDuration).result());
|
||||
}
|
||||
|
||||
TEST_F(DiscoveredPeripheralTrackerTest,
|
||||
LostPeripheralWithFastAdvertisementForInstantOnLost) {
|
||||
ByteArray fast_advertisement_bytes = CreateFastBleAdvertisement(
|
||||
ByteArray(std::string(kData)), ByteArray(std::string(kDeviceToken)));
|
||||
|
||||
// Used to get advertisement hash from fast advertisement bytes.
|
||||
BleAdvertisementHeader ble_advertisement_header =
|
||||
CreateFastBleAdvertisementHeader(fast_advertisement_bytes);
|
||||
|
||||
ByteArray advertisement_hash =
|
||||
ble_advertisement_header.GetAdvertisementHash();
|
||||
|
||||
CountDownLatch found_latch(1);
|
||||
CountDownLatch lost_latch(1);
|
||||
CountDownLatch fetch_latch(1);
|
||||
|
||||
discovered_peripheral_tracker_.StartTracking(
|
||||
std::string(kServiceIdA),
|
||||
{
|
||||
.peripheral_discovered_cb =
|
||||
[&found_latch](BleV2Peripheral peripheral,
|
||||
const std::string& service_id,
|
||||
const ByteArray& advertisement_bytes,
|
||||
bool fast_advertisement) {
|
||||
EXPECT_EQ(advertisement_bytes, ByteArray(std::string(kData)));
|
||||
EXPECT_TRUE(fast_advertisement);
|
||||
found_latch.CountDown();
|
||||
},
|
||||
.peripheral_lost_cb =
|
||||
[&lost_latch](
|
||||
BleV2Peripheral peripheral, const std::string& service_id,
|
||||
const ByteArray& advertisement_bytes,
|
||||
bool fast_advertisement) { lost_latch.CountDown(); },
|
||||
},
|
||||
Uuid(kFastAdvertisementServiceUuid));
|
||||
|
||||
api::ble_v2::BleAdvertisementData advertisement_data{};
|
||||
if (!fast_advertisement_bytes.Empty()) {
|
||||
advertisement_data.service_data.insert(
|
||||
{Uuid(kFastAdvertisementServiceUuid), fast_advertisement_bytes});
|
||||
}
|
||||
|
||||
FindFastAdvertisement(advertisement_data, {}, fetch_latch);
|
||||
|
||||
// We should receive a client callback of a peripheral discovery.
|
||||
fetch_latch.Await(kWaitDuration);
|
||||
ASSERT_TRUE(found_latch.Await(kWaitDuration).result());
|
||||
|
||||
auto advertisement = InstantOnLostAdvertisement::CreateFromHash(
|
||||
advertisement_hash.AsStringView());
|
||||
ASSERT_OK(advertisement);
|
||||
api::ble_v2::BleAdvertisementData loss_advertisement_data{};
|
||||
loss_advertisement_data.service_data.insert(
|
||||
{bleutils::kCopresenceServiceUuid, ByteArray(advertisement->ToBytes())});
|
||||
|
||||
FindAdvertisement(loss_advertisement_data,
|
||||
{ByteArray(advertisement->ToBytes())}, fetch_latch);
|
||||
|
||||
|
||||
// We should receive a client callback of a lost peripheral
|
||||
EXPECT_TRUE(lost_latch.Await(kWaitDuration).result());
|
||||
}
|
||||
|
||||
TEST_F(DiscoveredPeripheralTrackerTest, HandleDummyAdvertisement) {
|
||||
auto flag = nearby::FeatureFlags::Flags{
|
||||
.enable_invoking_legacy_device_discovered_cb = true,
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
|
||||
#include "connections/implementation/p2p_cluster_pcp_handler.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstddef>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
@@ -22,25 +22,46 @@
|
||||
|
||||
#include "absl/functional/bind_front.h"
|
||||
#include "absl/strings/escaping.h"
|
||||
#include "absl/strings/str_format.h"
|
||||
#include "absl/strings/string_view.h"
|
||||
#include "connections/advertising_options.h"
|
||||
#include "connections/discovery_options.h"
|
||||
#include "connections/implementation/base_pcp_handler.h"
|
||||
#include "connections/implementation/ble_advertisement.h"
|
||||
#include "connections/implementation/ble_endpoint_channel.h"
|
||||
#include "connections/implementation/ble_v2_endpoint_channel.h"
|
||||
#include "connections/implementation/bluetooth_device_name.h"
|
||||
#include "connections/implementation/bluetooth_endpoint_channel.h"
|
||||
#include "connections/implementation/bwu_manager.h"
|
||||
#include "connections/implementation/client_proxy.h"
|
||||
#include "connections/implementation/endpoint_channel_manager.h"
|
||||
#include "connections/implementation/endpoint_manager.h"
|
||||
#include "connections/implementation/flags/nearby_connections_feature_flags.h"
|
||||
#include "connections/implementation/injected_bluetooth_device_store.h"
|
||||
#include "connections/implementation/mediums/mediums.h"
|
||||
#include "connections/implementation/mediums/utils.h"
|
||||
#include "connections/implementation/pcp.h"
|
||||
#include "connections/implementation/pcp_handler.h"
|
||||
#include "connections/implementation/wifi_lan_endpoint_channel.h"
|
||||
#include "connections/implementation/wifi_lan_service_info.h"
|
||||
#include "connections/medium_selector.h"
|
||||
#include "connections/out_of_band_connection_metadata.h"
|
||||
#include "connections/power_level.h"
|
||||
#include "connections/status.h"
|
||||
#include "connections/v3/connection_listening_options.h"
|
||||
#include "internal/flags/nearby_flags.h"
|
||||
#include "internal/interop/device.h"
|
||||
#include "internal/platform/ble.h"
|
||||
#include "internal/platform/ble_v2.h"
|
||||
#include "internal/platform/bluetooth_adapter.h"
|
||||
#include "internal/platform/bluetooth_classic.h"
|
||||
#include "internal/platform/byte_array.h"
|
||||
#include "internal/platform/implementation/platform.h"
|
||||
#include "internal/platform/logging.h"
|
||||
#include "internal/platform/nsd_service_info.h"
|
||||
#include "internal/platform/os_name.h"
|
||||
#include "internal/platform/types.h"
|
||||
#include "internal/platform/wifi_lan.h"
|
||||
#include "proto/connections_enums.pb.h"
|
||||
|
||||
namespace nearby {
|
||||
|
||||
Reference in New Issue
Block a user