Add scanning logic for DCT

PiperOrigin-RevId: 736888954
This commit is contained in:
Guogang Li
2025-03-14 09:33:05 -07:00
committed by Copybara-Service
parent 2ea692213d
commit f7e97d23fe
13 changed files with 374 additions and 110 deletions
+1
View File
@@ -156,6 +156,7 @@ cc_test(
":mediums", ":mediums",
":utils", ":utils",
"//connections:core_types", "//connections:core_types",
"//connections/implementation:types",
"//connections/implementation/flags:connections_flags", "//connections/implementation/flags:connections_flags",
"//connections/implementation/mediums/ble_v2", "//connections/implementation/mediums/ble_v2",
"//internal/flags:nearby_flags", "//internal/flags:nearby_flags",
@@ -55,6 +55,7 @@ cc_library(
"//internal/platform:base", "//internal/platform:base",
"//internal/platform:logging", "//internal/platform:logging",
"//internal/platform:util", "//internal/platform:util",
"@com_google_absl//absl/strings:string_view",
], ],
) )
@@ -18,14 +18,19 @@
#include <optional> #include <optional>
#include <string> #include <string>
#include "absl/strings/string_view.h"
#include "internal/platform/byte_array.h" #include "internal/platform/byte_array.h"
#include "internal/platform/stream_reader.h" #include "internal/platform/stream_reader.h"
namespace nearby::connections::advertisements { namespace nearby::connections::advertisements {
namespace {
constexpr absl::string_view kFakeEncryptionKey =
"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f";
}
// Should always match the protocol implementation to read device name.
// LINT.IfChange
std::optional<std::string> ReadDeviceName(const ByteArray& endpoint_info) { std::optional<std::string> ReadDeviceName(const ByteArray& endpoint_info) {
// Should always match the protocol implementation to read device name.
// LINT.IfChange
StreamReader reader(endpoint_info); StreamReader reader(endpoint_info);
std::optional<uint8_t> version = reader.ReadBits(3); std::optional<uint8_t> version = reader.ReadBits(3);
if (!version.has_value() || *version > 1) { if (!version.has_value() || *version > 1) {
@@ -57,7 +62,19 @@ std::optional<std::string> ReadDeviceName(const ByteArray& endpoint_info) {
} }
return std::string(*device_name); return std::string(*device_name);
// LINT.ThenChange(//depot/google3/third_party/nearby/sharing/advertisement.cc)
} }
std::string BuildEndpointInfo(const std::string& device_name) {
std::string endpoint_info;
endpoint_info.reserve(18 + device_name.size());
// VERSION | HAS_DEVICE_NAME | DEVICE_TYPE
endpoint_info.push_back(0x22);
// salt and encrypted_metadata_key
endpoint_info.append(kFakeEncryptionKey.data(), kFakeEncryptionKey.size());
endpoint_info.push_back(device_name.size());
endpoint_info.append(device_name);
return endpoint_info;
}
// LINT.ThenChange(//depot/google3/third_party/nearby/sharing/advertisement.cc)
} // namespace nearby::connections::advertisements } // namespace nearby::connections::advertisements
@@ -25,6 +25,9 @@ namespace nearby::connections::advertisements {
// Reads the device name from the endpoint info. // Reads the device name from the endpoint info.
std::optional<std::string> ReadDeviceName(const ByteArray& endpoint_info); std::optional<std::string> ReadDeviceName(const ByteArray& endpoint_info);
// Build the endpoint info from the device name.
std::string BuildEndpointInfo(const std::string& device_name);
} // namespace nearby::connections::advertisements } // namespace nearby::connections::advertisements
#endif // THIRD_PARTY_NEARBY_CONNECTIONS_IMPLEMENTATION_MEDIUMS_ADVERTISEMENTS_ADVERTISEMENT_UTIL_H_ #endif // THIRD_PARTY_NEARBY_CONNECTIONS_IMPLEMENTATION_MEDIUMS_ADVERTISEMENTS_ADVERTISEMENT_UTIL_H_
@@ -20,6 +20,7 @@
#include <string> #include <string>
#include "connections/implementation/mediums/advertisements/data_element.h" #include "connections/implementation/mediums/advertisements/data_element.h"
#include "connections/implementation/mediums/utils.h"
#include "internal/crypto_cros/hkdf.h" #include "internal/crypto_cros/hkdf.h"
#include "internal/platform/byte_array.h" #include "internal/platform/byte_array.h"
#include "internal/platform/crypto.h" #include "internal/platform/crypto.h"
@@ -47,10 +48,7 @@ DctAdvertisement::DctAdvertisement(const std::string& service_id,
const std::string& device_name, uint16_t psm, const std::string& device_name, uint16_t psm,
uint8_t dedup) { uint8_t dedup) {
psm_ = psm; psm_ = psm;
service_id_hash_ = nearby::crypto::HkdfSha256( service_id_hash_ = ComputeServiceIdHash(service_id);
/*secret=*/service_id, /*salt= */ kServiceIdHashSalt,
/*info=*/kServiceIdHashInfo,
/*derived_key_size=*/kServiceIdHashSize);
if (device_name.size() > kMaxDeviceNameSize) { if (device_name.size() > kMaxDeviceNameSize) {
is_device_name_truncated_ = true; is_device_name_truncated_ = true;
// Truncate the device name and make sure it is a valid UTF-8 string. // Truncate the device name and make sure it is a valid UTF-8 string.
@@ -164,6 +162,19 @@ std::optional<std::string> DctAdvertisement::GenerateEndpointId(
return endpoint_id; return endpoint_id;
} }
std::string DctAdvertisement::GenerateDeviceToken(
const std::string& device_name) {
return std::string(Utils::Sha256Hash(device_name, 2));
}
std::string DctAdvertisement::ComputeServiceIdHash(
const std::string& service_id) {
return nearby::crypto::HkdfSha256(
/*secret=*/service_id, /*salt= */ kServiceIdHashSalt,
/*info=*/kServiceIdHashInfo,
/*derived_key_size=*/kServiceIdHashSize);
}
std::string DctAdvertisement::ToData() const { std::string DctAdvertisement::ToData() const {
StreamWriter writer; StreamWriter writer;
@@ -35,15 +35,25 @@ class DctAdvertisement {
static std::optional<std::string> GenerateEndpointId( static std::optional<std::string> GenerateEndpointId(
uint8_t dedup, const std::string& device_name); uint8_t dedup, const std::string& device_name);
static std::string GenerateDeviceToken(const std::string& device_name);
static std::string ComputeServiceIdHash(const std::string& service_id);
std::string ToData() const; std::string ToData() const;
uint8_t GetVersion() const { return version_; } uint8_t GetVersion() const { return version_; }
uint16_t GetDedup() const { return dedup_; }
uint16_t GetPsm() const { return psm_; } uint16_t GetPsm() const { return psm_; }
std::string GetServiceIdHash() const { return service_id_hash_; } std::string GetServiceIdHash() const { return service_id_hash_; }
bool IsDeviceNameTruncated() const { return is_device_name_truncated_; } bool IsDeviceNameTruncated() const { return is_device_name_truncated_; }
std::string GetDeviceName() const { return device_name_; } std::string GetDeviceName() const { return device_name_; }
std::optional<std::string> GetEndpointId() const {
return GenerateEndpointId(dedup_, device_name_);
}
std::string GetDeviceToken() const {
return GenerateDeviceToken(device_name_);
}
private: private:
DctAdvertisement() = default; DctAdvertisement() = default;
DctAdvertisement(const std::string& service_id, DctAdvertisement(const std::string& service_id,
+89 -42
View File
@@ -40,6 +40,7 @@
#include "connections/implementation/mediums/ble_v2/discovered_peripheral_tracker.h" #include "connections/implementation/mediums/ble_v2/discovered_peripheral_tracker.h"
#include "connections/implementation/mediums/bluetooth_radio.h" #include "connections/implementation/mediums/bluetooth_radio.h"
#include "connections/implementation/mediums/utils.h" #include "connections/implementation/mediums/utils.h"
#include "connections/implementation/pcp.h"
#include "connections/power_level.h" #include "connections/power_level.h"
#include "internal/flags/nearby_flags.h" #include "internal/flags/nearby_flags.h"
#include "internal/platform/ble_v2.h" #include "internal/platform/ble_v2.h"
@@ -372,14 +373,15 @@ bool BleV2::StopLegacyAdvertising(const std::string& input_service_id) {
return status.ok(); return status.ok();
} }
void BleV2::AddAlternateUuidForService( void BleV2::AddAlternateUuidForService(uint16_t uuid,
uint16_t uuid, const std::string& service_id) { const std::string& service_id) {
MutexLock lock(&mutex_); MutexLock lock(&mutex_);
medium_.AddAlternateUuidForService(uuid, service_id); medium_.AddAlternateUuidForService(uuid, service_id);
} }
ErrorOr<bool> BleV2::StartScanning(const std::string& service_id, ErrorOr<bool> BleV2::StartScanning(const std::string& service_id, Pcp pcp,
PowerLevel power_level, PowerLevel power_level,
bool include_dct_advertisement,
DiscoveredPeripheralCallback callback) { DiscoveredPeripheralCallback callback) {
MutexLock lock(&mutex_); MutexLock lock(&mutex_);
@@ -406,7 +408,7 @@ ErrorOr<bool> BleV2::StartScanning(const std::string& service_id,
// Start to track the advertisement found for specific `service_id`. // Start to track the advertisement found for specific `service_id`.
discovered_peripheral_tracker_.StartTracking( discovered_peripheral_tracker_.StartTracking(
service_id, std::move(callback), service_id, include_dct_advertisement, pcp, std::move(callback),
mediums::bleutils::kCopresenceServiceUuid); mediums::bleutils::kCopresenceServiceUuid);
if (FeatureFlags::GetInstance().GetFlags().enable_ble_v2_async_scanning) { if (FeatureFlags::GetInstance().GetFlags().enable_ble_v2_async_scanning) {
@@ -425,44 +427,89 @@ ErrorOr<bool> BleV2::StartScanning(const std::string& service_id,
scanned_service_ids_.insert(service_id); scanned_service_ids_.insert(service_id);
// TODO(b/213835576): We should re-start scanning once the power level is // TODO(b/213835576): We should re-start scanning once the power level is
// changed. // changed.
if (!medium_.StartScanning( if (include_dct_advertisement) {
mediums::bleutils::kCopresenceServiceUuid, std::vector<Uuid> service_uuids = {
PowerLevelToTxPowerLevel(power_level), mediums::bleutils::kCopresenceServiceUuid,
{ mediums::bleutils::kDctServiceUuid};
.advertisement_found_cb = if (!medium_.StartMultipleServicesScanning(
[this](BleV2Peripheral peripheral, service_uuids, PowerLevelToTxPowerLevel(power_level),
BleAdvertisementData advertisement_data) { {
RunOnBleThread([this, peripheral = std::move(peripheral), .advertisement_found_cb =
advertisement_data]() { [this](BleV2Peripheral peripheral,
MutexLock lock(&mutex_); BleAdvertisementData advertisement_data) {
discovered_peripheral_tracker_ RunOnBleThread([this, peripheral = std::move(peripheral),
.ProcessFoundBleAdvertisement( advertisement_data]() {
std::move(peripheral), advertisement_data, MutexLock lock(&mutex_);
[this](BleV2Peripheral peripheral, int num_slots, discovered_peripheral_tracker_
int psm, .ProcessFoundBleAdvertisement(
const std::vector<std::string>& std::move(peripheral), advertisement_data,
interesting_service_ids, [this](BleV2Peripheral peripheral,
mediums::AdvertisementReadResult& int num_slots, int psm,
advertisement_read_result) { const std::vector<std::string>&
// Th`mutex_` is already held here. Use interesting_service_ids,
// `AssumeHeld` tell the thread mediums::AdvertisementReadResult&
// annotation static analysis that advertisement_read_result) {
// `mutex_` is already exclusively // Th`mutex_` is already held here. Use
// locked. // `AssumeHeld` tell the thread
AssumeHeld(mutex_); // annotation static analysis that
ProcessFetchGattAdvertisementsRequest( // `mutex_` is already exclusively
std::move(peripheral), num_slots, psm, // locked.
interesting_service_ids, AssumeHeld(mutex_);
advertisement_read_result); ProcessFetchGattAdvertisementsRequest(
}); std::move(peripheral), num_slots, psm,
}); interesting_service_ids,
}, advertisement_read_result);
})) { });
LOG(INFO) << "Failed to start scan of BLE services."; });
discovered_peripheral_tracker_.StopTracking(service_id); },
// Erase the service id that is just added. })) {
scanned_service_ids_.erase(service_id); LOG(INFO) << "Failed to start scan of multiple BLE services.";
return {Error(OperationResultCode::CONNECTIVITY_BLE_SCAN_FAILURE)}; discovered_peripheral_tracker_.StopTracking(service_id);
// Erase the service id that is just added.
scanned_service_ids_.erase(service_id);
return {Error(OperationResultCode::CONNECTIVITY_BLE_SCAN_FAILURE)};
}
} else {
if (!medium_.StartScanning(
mediums::bleutils::kCopresenceServiceUuid,
PowerLevelToTxPowerLevel(power_level),
{
.advertisement_found_cb =
[this](BleV2Peripheral peripheral,
BleAdvertisementData advertisement_data) {
RunOnBleThread([this, peripheral = std::move(peripheral),
advertisement_data]() {
MutexLock lock(&mutex_);
discovered_peripheral_tracker_
.ProcessFoundBleAdvertisement(
std::move(peripheral), advertisement_data,
[this](BleV2Peripheral peripheral,
int num_slots, int psm,
const std::vector<std::string>&
interesting_service_ids,
mediums::AdvertisementReadResult&
advertisement_read_result) {
// Th`mutex_` is already held here. Use
// `AssumeHeld` tell the thread
// annotation static analysis that
// `mutex_` is already exclusively
// locked.
AssumeHeld(mutex_);
ProcessFetchGattAdvertisementsRequest(
std::move(peripheral), num_slots, psm,
interesting_service_ids,
advertisement_read_result);
});
});
},
})) {
LOG(INFO) << "Failed to start scan of BLE services.";
discovered_peripheral_tracker_.StopTracking(service_id);
// Erase the service id that is just added.
scanned_service_ids_.erase(service_id);
return {Error(OperationResultCode::CONNECTIVITY_BLE_SCAN_FAILURE)};
}
} }
absl::Duration peripheral_lost_timeout = absl::Duration peripheral_lost_timeout =
+6 -1
View File
@@ -33,6 +33,7 @@
#include "connections/implementation/mediums/ble_v2/discovered_peripheral_tracker.h" #include "connections/implementation/mediums/ble_v2/discovered_peripheral_tracker.h"
#include "connections/implementation/mediums/ble_v2/instant_on_lost_manager.h" #include "connections/implementation/mediums/ble_v2/instant_on_lost_manager.h"
#include "connections/implementation/mediums/bluetooth_radio.h" #include "connections/implementation/mediums/bluetooth_radio.h"
#include "connections/implementation/pcp.h"
#include "connections/power_level.h" #include "connections/power_level.h"
#include "internal/platform/ble_v2.h" #include "internal/platform/ble_v2.h"
#include "internal/platform/bluetooth_adapter.h" #include "internal/platform/bluetooth_adapter.h"
@@ -124,11 +125,15 @@ class BleV2 final {
// Returns true, if the scanning is successfully enabled, false otherwise. // Returns true, if the scanning is successfully enabled, false otherwise.
// //
// service_id - The service ID to track. // service_id - The service ID to track.
// pcp - The PCP to use for the discovery.
// power_level - The power level to use for the discovery. // power_level - The power level to use for the discovery.
// include_dct_advertisement - Whether to include the dct advertisement in
// the discovery. it is false by default.
// discovered_peripheral_callback - The callback to invoke for discovery // discovered_peripheral_callback - The callback to invoke for discovery
// events. // events.
ErrorOr<bool> StartScanning(const std::string& service_id, ErrorOr<bool> StartScanning(const std::string& service_id, Pcp pcp,
PowerLevel power_level, PowerLevel power_level,
bool include_dct_advertisement,
DiscoveredPeripheralCallback callback) DiscoveredPeripheralCallback callback)
ABSL_LOCKS_EXCLUDED(mutex_); ABSL_LOCKS_EXCLUDED(mutex_);
@@ -17,6 +17,7 @@
#include <algorithm> #include <algorithm>
#include <iterator> #include <iterator>
#include <memory> #include <memory>
#include <optional>
#include <string> #include <string>
#include <utility> #include <utility>
#include <vector> #include <vector>
@@ -25,7 +26,10 @@
#include "absl/status/statusor.h" #include "absl/status/statusor.h"
#include "absl/strings/escaping.h" #include "absl/strings/escaping.h"
#include "absl/time/time.h" #include "absl/time/time.h"
#include "connections/implementation/ble_advertisement.h"
#include "connections/implementation/flags/nearby_connections_feature_flags.h" #include "connections/implementation/flags/nearby_connections_feature_flags.h"
#include "connections/implementation/mediums/advertisements/advertisement_util.h"
#include "connections/implementation/mediums/advertisements/dct_advertisement.h"
#include "connections/implementation/mediums/ble_v2/advertisement_read_result.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.h"
#include "connections/implementation/mediums/ble_v2/ble_advertisement_header.h" #include "connections/implementation/mediums/ble_v2/ble_advertisement_header.h"
@@ -34,6 +38,8 @@
#include "connections/implementation/mediums/ble_v2/discovered_peripheral_callback.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/ble_v2/instant_on_lost_advertisement.h"
#include "connections/implementation/mediums/lost_entity_tracker.h" #include "connections/implementation/mediums/lost_entity_tracker.h"
#include "connections/implementation/pcp.h"
#include "connections/implementation/webrtc_state.h"
#include "internal/flags/nearby_flags.h" #include "internal/flags/nearby_flags.h"
#include "internal/platform/ble_v2.h" #include "internal/platform/ble_v2.h"
#include "internal/platform/byte_array.h" #include "internal/platform/byte_array.h"
@@ -78,7 +84,7 @@ DiscoveredPeripheralTracker::~DiscoveredPeripheralTracker() {
} }
void DiscoveredPeripheralTracker::StartTracking( void DiscoveredPeripheralTracker::StartTracking(
const std::string& service_id, const std::string& service_id, bool include_dct_advertisement, Pcp pcp,
DiscoveredPeripheralCallback discovered_peripheral_callback, DiscoveredPeripheralCallback discovered_peripheral_callback,
const Uuid& fast_advertisement_service_uuid) { const Uuid& fast_advertisement_service_uuid) {
MutexLock lock(&mutex_); MutexLock lock(&mutex_);
@@ -88,11 +94,20 @@ void DiscoveredPeripheralTracker::StartTracking(
std::move(discovered_peripheral_callback), std::move(discovered_peripheral_callback),
.lost_entity_tracker = .lost_entity_tracker =
std::make_unique<LostEntityTracker<BleAdvertisement>>(), std::make_unique<LostEntityTracker<BleAdvertisement>>(),
.fast_advertisement_service_uuid = fast_advertisement_service_uuid}; .fast_advertisement_service_uuid = fast_advertisement_service_uuid,
.include_dct_advertisement = include_dct_advertisement,
.pcp = pcp};
// Replace if key exists. // Replace if key exists.
service_id_infos_.insert_or_assign(service_id, std::move(service_id_info)); service_id_infos_.insert_or_assign(service_id, std::move(service_id_info));
// Add service id hash to service id map for dct advertisement.
if (include_dct_advertisement) {
dct_service_id_hash_to_service_id_map_.insert_or_assign(
advertisements::ble::DctAdvertisement::ComputeServiceIdHash(service_id),
service_id);
}
// Clear all of the GATT read results. With this cleared, we will now attempt // Clear all of the GATT read results. With this cleared, we will now attempt
// to reconnect to every peripheral we see, giving us a chance to search for // to reconnect to every peripheral we see, giving us a chance to search for
// the new service we're now tracking. // the new service we're now tracking.
@@ -106,6 +121,8 @@ void DiscoveredPeripheralTracker::StartTracking(
void DiscoveredPeripheralTracker::StopTracking(const std::string& service_id) { void DiscoveredPeripheralTracker::StopTracking(const std::string& service_id) {
MutexLock lock(&mutex_); MutexLock lock(&mutex_);
dct_service_id_hash_to_service_id_map_.erase(
advertisements::ble::DctAdvertisement::ComputeServiceIdHash(service_id));
service_id_infos_.erase(service_id); service_id_infos_.erase(service_id);
} }
@@ -147,6 +164,17 @@ void DiscoveredPeripheralTracker::ProcessFoundBleAdvertisement(
} }
return; return;
} }
if (advertisement_data.service_data.contains(bleutils::kDctServiceUuid)) {
std::optional<BleAdvertisementData> dct_advertisement_data =
HandleDctAdvertisement(advertisement_data);
if (!dct_advertisement_data.has_value()) {
return;
}
advertisement_data = std::move(*dct_advertisement_data);
}
HandleAdvertisement(peripheral, advertisement_data); HandleAdvertisement(peripheral, advertisement_data);
HandleAdvertisementHeader(peripheral, advertisement_data, HandleAdvertisementHeader(peripheral, advertisement_data,
std::move(advertisement_fetcher)); std::move(advertisement_fetcher));
@@ -594,6 +622,64 @@ bool DiscoveredPeripheralTracker::IsDummyAdvertisementHeader(
ByteArray(bloom_filter); ByteArray(bloom_filter);
} }
std::optional<BleAdvertisementData>
DiscoveredPeripheralTracker::HandleDctAdvertisement(
const BleAdvertisementData& advertisement_data) {
// This is DCT advertisement. Build a new advertisement data base on DCT
// advertisement.
std::optional<advertisements::ble::DctAdvertisement> dct_advertisement =
advertisements::ble::DctAdvertisement::Parse(std::string(
advertisement_data.service_data.at(bleutils::kDctServiceUuid)));
if (!dct_advertisement.has_value()) {
LOG(WARNING) << "Failed to parse DCT advertisement.";
return std::nullopt;
}
std::optional<std::string> endpoint_id = dct_advertisement->GetEndpointId();
if (!endpoint_id.has_value()) {
LOG(WARNING) << "Failed to generate endpoint id.";
return std::nullopt;
}
const auto& it = dct_service_id_hash_to_service_id_map_.find(
dct_advertisement->GetServiceIdHash());
if (it == dct_service_id_hash_to_service_id_map_.end()) {
LOG(WARNING) << "Failed to find service id hash in the map.";
return std::nullopt;
}
std::string& service_id = it->second;
const auto& service_id_info_it = service_id_infos_.find(service_id);
if (service_id_info_it == service_id_infos_.end()) {
LOG(WARNING) << "Failed to find service id in the map.";
return std::nullopt;
}
ServiceIdInfo& service_id_info = service_id_info_it->second;
ByteArray service_id_hash = bleutils::GenerateServiceIdHash(service_id);
// Build the new BLE advertisement data.
std::string endpoint_info =
advertisements::BuildEndpointInfo(dct_advertisement->GetDeviceName());
connections::BleAdvertisement connections_advertisement(
connections::BleAdvertisement::Version::kV1, service_id_info.pcp,
/*service_id_hash=*/service_id_hash, *endpoint_id,
ByteArray(endpoint_info), /*bluetooth_mac_address=*/"",
/*uwb_address=*/ByteArray(),
/*web_rtc_state=*/WebRtcState::kUnconnectable);
BleAdvertisement medium_advertisement = {
mediums::BleAdvertisement::Version::kV2,
mediums::BleAdvertisement::SocketVersion::kV2,
/*service_id_hash=*/service_id_hash,
ByteArray(connections_advertisement),
ByteArray(dct_advertisement->GetDeviceToken()),
dct_advertisement->GetPsm()};
BleAdvertisementData new_advertisement_data{};
new_advertisement_data.service_data.insert(
{service_id_info.fast_advertisement_service_uuid,
medium_advertisement.ByteArrayWithExtraField()});
return new_advertisement_data;
}
void DiscoveredPeripheralTracker::HandleAdvertisementHeader( void DiscoveredPeripheralTracker::HandleAdvertisementHeader(
BleV2Peripheral peripheral, BleV2Peripheral peripheral,
const nearby::api::ble_v2::BleAdvertisementData& advertisement_data, const nearby::api::ble_v2::BleAdvertisementData& advertisement_data,
@@ -17,6 +17,7 @@
#include <array> #include <array>
#include <memory> #include <memory>
#include <optional>
#include <string> #include <string>
#include <vector> #include <vector>
@@ -31,6 +32,7 @@
#include "connections/implementation/mediums/ble_v2/ble_advertisement_header.h" #include "connections/implementation/mediums/ble_v2/ble_advertisement_header.h"
#include "connections/implementation/mediums/ble_v2/discovered_peripheral_callback.h" #include "connections/implementation/mediums/ble_v2/discovered_peripheral_callback.h"
#include "connections/implementation/mediums/lost_entity_tracker.h" #include "connections/implementation/mediums/lost_entity_tracker.h"
#include "connections/implementation/pcp.h"
#include "internal/platform/ble_v2.h" #include "internal/platform/ble_v2.h"
#include "internal/platform/byte_array.h" #include "internal/platform/byte_array.h"
#include "internal/platform/implementation/ble_v2.h" #include "internal/platform/implementation/ble_v2.h"
@@ -80,7 +82,7 @@ class DiscoveredPeripheralTracker {
// that `fast_advertisement_service_uuid` will be ignored for regular // that `fast_advertisement_service_uuid` will be ignored for regular
// advertisement. // advertisement.
void StartTracking( void StartTracking(
const std::string& service_id, const std::string& service_id, bool include_dct_advertisement, Pcp pcp,
DiscoveredPeripheralCallback discovered_peripheral_callback, DiscoveredPeripheralCallback discovered_peripheral_callback,
const Uuid& fast_advertisement_service_uuid) ABSL_LOCKS_EXCLUDED(mutex_); const Uuid& fast_advertisement_service_uuid) ABSL_LOCKS_EXCLUDED(mutex_);
@@ -120,6 +122,11 @@ class DiscoveredPeripheralTracker {
// Used to check for fast advertisements delivered through BLE advertisement // Used to check for fast advertisements delivered through BLE advertisement
// service data, under the given UUID. // service data, under the given UUID.
Uuid fast_advertisement_service_uuid; Uuid fast_advertisement_service_uuid;
// Used to check for dct advertisements delivered through BLE advertisement
// service data.
bool include_dct_advertisement;
Pcp pcp;
}; };
// A container to hold the related informations for a GATT advertisement. // A container to hold the related informations for a GATT advertisement.
@@ -206,6 +213,10 @@ class DiscoveredPeripheralTracker {
bool IsDummyAdvertisementHeader( bool IsDummyAdvertisementHeader(
const BleAdvertisementHeader& advertisement_header); const BleAdvertisementHeader& advertisement_header);
std::optional<api::ble_v2::BleAdvertisementData> HandleDctAdvertisement(
const api::ble_v2::BleAdvertisementData& advertisement_data)
ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
// Handles the advertisement header for regular advertisement. // Handles the advertisement header for regular advertisement.
void HandleAdvertisementHeader( void HandleAdvertisementHeader(
BleV2Peripheral peripheral, BleV2Peripheral peripheral,
@@ -230,7 +241,7 @@ class DiscoveredPeripheralTracker {
const BleAdvertisementHeader& advertisement_header) const BleAdvertisementHeader& advertisement_header)
ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_); ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
// Fetches advertsiement from BLE medium if advertisement header is read in // Fetches advertisement from BLE medium if advertisement header is read in
// AdvertisementData. // AdvertisementData.
// //
// advertisement_fetcher : a fetcher passed from BLE medium to read the // advertisement_fetcher : a fetcher passed from BLE medium to read the
@@ -285,6 +296,9 @@ class DiscoveredPeripheralTracker {
absl::flat_hash_map<std::string, ServiceIdInfo> service_id_infos_ absl::flat_hash_map<std::string, ServiceIdInfo> service_id_infos_
ABSL_GUARDED_BY(mutex_); ABSL_GUARDED_BY(mutex_);
absl::flat_hash_map<std::string, std::string>
dct_service_id_hash_to_service_id_map_ ABSL_GUARDED_BY(mutex_);
// ------------ ADVERTISEMENT HEADER MAPS ------------ // ------------ ADVERTISEMENT HEADER MAPS ------------
// Maps advertisement headers to AdvertisementReadResult. Tells us when to // Maps advertisement headers to AdvertisementReadResult. Tells us when to
// retry reading a GATT advertisement. If no entry exists for a particular // retry reading a GATT advertisement. If no entry exists for a particular
@@ -15,7 +15,6 @@
#include "connections/implementation/mediums/ble_v2/discovered_peripheral_tracker.h" #include "connections/implementation/mediums/ble_v2/discovered_peripheral_tracker.h"
#include <atomic> #include <atomic>
#include <list>
#include <memory> #include <memory>
#include <string> #include <string>
#include <vector> #include <vector>
@@ -28,6 +27,7 @@
#include "absl/time/clock.h" #include "absl/time/clock.h"
#include "absl/time/time.h" #include "absl/time/time.h"
#include "connections/implementation/flags/nearby_connections_feature_flags.h" #include "connections/implementation/flags/nearby_connections_feature_flags.h"
#include "connections/implementation/mediums/advertisements/dct_advertisement.h"
#include "connections/implementation/mediums/ble_v2/advertisement_read_result.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.h"
#include "connections/implementation/mediums/ble_v2/ble_advertisement_header.h" #include "connections/implementation/mediums/ble_v2/ble_advertisement_header.h"
@@ -36,6 +36,7 @@
#include "connections/implementation/mediums/ble_v2/discovered_peripheral_callback.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/ble_v2/instant_on_lost_advertisement.h"
#include "connections/implementation/mediums/utils.h" #include "connections/implementation/mediums/utils.h"
#include "connections/implementation/pcp.h"
#include "internal/flags/nearby_flags.h" #include "internal/flags/nearby_flags.h"
#include "internal/platform/ble_v2.h" #include "internal/platform/ble_v2.h"
#include "internal/platform/bluetooth_adapter.h" #include "internal/platform/bluetooth_adapter.h"
@@ -61,6 +62,7 @@ constexpr absl::string_view kServiceIdB = "B";
constexpr absl::string_view kData = "\x04\x02\x00"; constexpr absl::string_view kData = "\x04\x02\x00";
constexpr absl::string_view kData2 = "\x07\x00\x07"; constexpr absl::string_view kData2 = "\x07\x00\x07";
constexpr absl::string_view kDeviceToken = "\x04\x20"; constexpr absl::string_view kDeviceToken = "\x04\x20";
constexpr absl::string_view kDeviceName = "device";
ByteArray CreateFastBleAdvertisement(const ByteArray& data, ByteArray CreateFastBleAdvertisement(const ByteArray& data,
const ByteArray& device_token) { const ByteArray& device_token) {
@@ -91,6 +93,13 @@ ByteArray CreateLegacyBleAdvertisement(const std::string& service_id,
data, device_token, BleAdvertisementHeader::kDefaultPsmValue)); data, device_token, BleAdvertisementHeader::kDefaultPsmValue));
} }
ByteArray CreateDctAdvertisement(const std::string& service_id,
const std::string& device_name) {
auto dct_advertisement = advertisements::ble::DctAdvertisement::Create(
service_id, device_name, 0xf100, 0x01);
return ByteArray(dct_advertisement->ToData());
}
BleAdvertisementHeader CreateFastBleAdvertisementHeader( BleAdvertisementHeader CreateFastBleAdvertisementHeader(
const ByteArray& advertisement_bytes) { const ByteArray& advertisement_bytes) {
BloomFilter bloom_filter( BloomFilter bloom_filter(
@@ -300,7 +309,7 @@ TEST_P(DiscoveredPeripheralTrackerTest,
CountDownLatch fetch_latch(1); CountDownLatch fetch_latch(1);
discovered_peripheral_tracker_.StartTracking( discovered_peripheral_tracker_.StartTracking(
std::string(kServiceIdA), std::string(kServiceIdA), false, Pcp::kP2pPointToPoint,
{ {
.peripheral_discovered_cb = .peripheral_discovered_cb =
[&found_latch](BleV2Peripheral peripheral, [&found_latch](BleV2Peripheral peripheral,
@@ -329,6 +338,40 @@ TEST_P(DiscoveredPeripheralTrackerTest,
EXPECT_EQ(GetFetchAdvertisementCallbackCount(), 0); EXPECT_EQ(GetFetchAdvertisementCallbackCount(), 0);
} }
TEST_P(DiscoveredPeripheralTrackerTest,
DctAdvertisementPeripheralDiscovered) {
ByteArray dct_advertisement_bytes = CreateDctAdvertisement(
std::string(kServiceIdA), std::string(kDeviceName));
CountDownLatch found_latch(1);
CountDownLatch fetch_latch(1);
discovered_peripheral_tracker_.StartTracking(
std::string(kServiceIdA), true, Pcp::kP2pPointToPoint,
{
.peripheral_discovered_cb =
[&found_latch](BleV2Peripheral peripheral,
const std::string& service_id,
const ByteArray& advertisement_bytes,
bool fast_advertisement) {
EXPECT_FALSE(fast_advertisement);
found_latch.CountDown();
},
},
Uuid(kFastAdvertisementServiceUuid));
api::ble_v2::BleAdvertisementData advertisement_data{};
advertisement_data.service_data.insert(
{bleutils::kDctServiceUuid, dct_advertisement_bytes});
FindAdvertisement(advertisement_data, {}, fetch_latch);
// We should receive a client callback of a peripheral discovery without a
// GATT read.
fetch_latch.Await(kWaitDuration);
EXPECT_TRUE(found_latch.Await(kWaitDuration).result());
EXPECT_EQ(GetFetchAdvertisementCallbackCount(), 0);
}
TEST_P(DiscoveredPeripheralTrackerTest, TEST_P(DiscoveredPeripheralTrackerTest,
ReportFoundLegacyDeviceWhenFoundBleAdvertisementPeripheralDiscovered) { ReportFoundLegacyDeviceWhenFoundBleAdvertisementPeripheralDiscovered) {
DisableBluetoothScanning(); DisableBluetoothScanning();
@@ -343,7 +386,7 @@ TEST_P(DiscoveredPeripheralTrackerTest,
CountDownLatch fetch_latch(1); CountDownLatch fetch_latch(1);
discovered_peripheral_tracker_.StartTracking( discovered_peripheral_tracker_.StartTracking(
std::string(kServiceIdA), std::string(kServiceIdA), false, Pcp::kP2pPointToPoint,
{.peripheral_discovered_cb = {.peripheral_discovered_cb =
[&found_latch]( [&found_latch](
BleV2Peripheral peripheral, const std::string& service_id, BleV2Peripheral peripheral, const std::string& service_id,
@@ -381,7 +424,7 @@ TEST_P(DiscoveredPeripheralTrackerTest,
// 1st tracking. // 1st tracking.
discovered_peripheral_tracker_.StartTracking( discovered_peripheral_tracker_.StartTracking(
std::string(kServiceIdA), std::string(kServiceIdA), false, Pcp::kP2pPointToPoint,
{ {
.peripheral_discovered_cb = .peripheral_discovered_cb =
[&callback_times, &found_latch]( [&callback_times, &found_latch](
@@ -406,7 +449,7 @@ TEST_P(DiscoveredPeripheralTrackerTest,
// 2nd tracking. // 2nd tracking.
discovered_peripheral_tracker_.StartTracking( discovered_peripheral_tracker_.StartTracking(
std::string(kServiceIdA), std::string(kServiceIdA), false, Pcp::kP2pPointToPoint,
{ {
.peripheral_discovered_cb = .peripheral_discovered_cb =
[&callback_times, &found_latch]( [&callback_times, &found_latch](
@@ -425,7 +468,7 @@ TEST_P(DiscoveredPeripheralTrackerTest,
// 3rd tracking. // 3rd tracking.
discovered_peripheral_tracker_.StartTracking( discovered_peripheral_tracker_.StartTracking(
std::string(kServiceIdA), std::string(kServiceIdA), false, Pcp::kP2pPointToPoint,
{ {
.peripheral_discovered_cb = .peripheral_discovered_cb =
[&callback_times, &found_latch]( [&callback_times, &found_latch](
@@ -452,7 +495,7 @@ TEST_P(DiscoveredPeripheralTrackerTest,
std::atomic<int> callback_times = 0; std::atomic<int> callback_times = 0;
discovered_peripheral_tracker_.StartTracking( discovered_peripheral_tracker_.StartTracking(
std::string(kServiceIdA), std::string(kServiceIdA), false, Pcp::kP2pPointToPoint,
{ {
.peripheral_discovered_cb = .peripheral_discovered_cb =
[&callback_times, &found_latch]( [&callback_times, &found_latch](
@@ -497,7 +540,7 @@ TEST_P(DiscoveredPeripheralTrackerTest,
// Start tracking a service ID and then process a discovery containing a valid // Start tracking a service ID and then process a discovery containing a valid
// fast advertisement, but under a different service UUID. // fast advertisement, but under a different service UUID.
discovered_peripheral_tracker_.StartTracking( discovered_peripheral_tracker_.StartTracking(
std::string(kServiceIdA), std::string(kServiceIdA), false, Pcp::kP2pPointToPoint,
{ {
.peripheral_discovered_cb = .peripheral_discovered_cb =
[&found_latch](BleV2Peripheral peripheral, [&found_latch](BleV2Peripheral peripheral,
@@ -540,7 +583,7 @@ TEST_P(DiscoveredPeripheralTrackerTest,
CountDownLatch fetch_latch(1); CountDownLatch fetch_latch(1);
discovered_peripheral_tracker_.StartTracking( discovered_peripheral_tracker_.StartTracking(
std::string(kServiceIdA), std::string(kServiceIdA), false, Pcp::kP2pPointToPoint,
{ {
.peripheral_discovered_cb = .peripheral_discovered_cb =
[&found_latch_a](BleV2Peripheral peripheral, [&found_latch_a](BleV2Peripheral peripheral,
@@ -554,7 +597,7 @@ TEST_P(DiscoveredPeripheralTrackerTest,
}, },
Uuid(kFastAdvertisementServiceUuid)); Uuid(kFastAdvertisementServiceUuid));
discovered_peripheral_tracker_.StartTracking( discovered_peripheral_tracker_.StartTracking(
std::string(kServiceIdB), std::string(kServiceIdB), false, Pcp::kP2pPointToPoint,
{ {
.peripheral_discovered_cb = .peripheral_discovered_cb =
[&found_latch_b](BleV2Peripheral peripheral, [&found_latch_b](BleV2Peripheral peripheral,
@@ -599,7 +642,7 @@ TEST_P(DiscoveredPeripheralTrackerTest,
CountDownLatch fetch_latch(1); CountDownLatch fetch_latch(1);
discovered_peripheral_tracker_.StartTracking( discovered_peripheral_tracker_.StartTracking(
std::string(kServiceIdA), std::string(kServiceIdA), false, Pcp::kP2pPointToPoint,
{ {
.peripheral_discovered_cb = .peripheral_discovered_cb =
[&found_latch](BleV2Peripheral peripheral, [&found_latch](BleV2Peripheral peripheral,
@@ -639,7 +682,7 @@ TEST_P(DiscoveredPeripheralTrackerTest,
CountDownLatch fetch_latch(1); CountDownLatch fetch_latch(1);
discovered_peripheral_tracker_.StartTracking( discovered_peripheral_tracker_.StartTracking(
std::string(kServiceIdA), std::string(kServiceIdA), false, Pcp::kP2pPointToPoint,
{ {
.peripheral_discovered_cb = .peripheral_discovered_cb =
[&found_latch]( [&found_latch](
@@ -680,7 +723,7 @@ TEST_P(DiscoveredPeripheralTrackerTest,
std::atomic<int> callback_times = 0; std::atomic<int> callback_times = 0;
discovered_peripheral_tracker_.StartTracking( discovered_peripheral_tracker_.StartTracking(
std::string(kServiceIdA), std::string(kServiceIdA), false, Pcp::kP2pPointToPoint,
{ {
.peripheral_discovered_cb = .peripheral_discovered_cb =
[&callback_times, &found_latch]( [&callback_times, &found_latch](
@@ -726,7 +769,7 @@ TEST_P(DiscoveredPeripheralTrackerTest,
std::atomic<int> callback_times = 0; std::atomic<int> callback_times = 0;
discovered_peripheral_tracker_.StartTracking( discovered_peripheral_tracker_.StartTracking(
std::string(kServiceIdA), std::string(kServiceIdA), false, Pcp::kP2pPointToPoint,
{ {
.peripheral_discovered_cb = .peripheral_discovered_cb =
[&callback_times, &found_latch]( [&callback_times, &found_latch](
@@ -774,7 +817,7 @@ TEST_P(DiscoveredPeripheralTrackerTest,
CountDownLatch fetch_latch(1); CountDownLatch fetch_latch(1);
discovered_peripheral_tracker_.StartTracking( discovered_peripheral_tracker_.StartTracking(
std::string(kServiceIdA), std::string(kServiceIdA), false, Pcp::kP2pPointToPoint,
{ {
.peripheral_discovered_cb = .peripheral_discovered_cb =
[&found_latch]( [&found_latch](
@@ -811,7 +854,7 @@ TEST_P(DiscoveredPeripheralTrackerTest,
std::atomic<int> lost_callback_times = 0; std::atomic<int> lost_callback_times = 0;
discovered_peripheral_tracker_.StartTracking( discovered_peripheral_tracker_.StartTracking(
std::string(kServiceIdA), std::string(kServiceIdA), false, Pcp::kP2pPointToPoint,
{ {
.peripheral_discovered_cb = .peripheral_discovered_cb =
[&found_latch](BleV2Peripheral peripheral, [&found_latch](BleV2Peripheral peripheral,
@@ -874,7 +917,7 @@ TEST_P(DiscoveredPeripheralTrackerTest,
CountDownLatch fetch_latch(1); CountDownLatch fetch_latch(1);
discovered_peripheral_tracker_.StartTracking( discovered_peripheral_tracker_.StartTracking(
std::string(kServiceIdA), std::string(kServiceIdA), false, Pcp::kP2pPointToPoint,
{ {
.peripheral_discovered_cb = .peripheral_discovered_cb =
[&found_latch](BleV2Peripheral peripheral, [&found_latch](BleV2Peripheral peripheral,
@@ -931,7 +974,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, LostPeripheralForAdvertisementLost) {
CountDownLatch fetch_latch(1); CountDownLatch fetch_latch(1);
discovered_peripheral_tracker_.StartTracking( discovered_peripheral_tracker_.StartTracking(
std::string(kServiceIdA), std::string(kServiceIdA), false, Pcp::kP2pPointToPoint,
{ {
.peripheral_discovered_cb = .peripheral_discovered_cb =
[&found_latch](BleV2Peripheral peripheral, [&found_latch](BleV2Peripheral peripheral,
@@ -990,7 +1033,7 @@ TEST_P(DiscoveredPeripheralTrackerTest,
CountDownLatch fetch_latch(1); CountDownLatch fetch_latch(1);
discovered_peripheral_tracker_.StartTracking( discovered_peripheral_tracker_.StartTracking(
std::string(kServiceIdA), std::string(kServiceIdA), false, Pcp::kP2pPointToPoint,
{ {
.peripheral_discovered_cb = .peripheral_discovered_cb =
[&found_latch_a](BleV2Peripheral peripheral, [&found_latch_a](BleV2Peripheral peripheral,
@@ -1009,7 +1052,7 @@ TEST_P(DiscoveredPeripheralTrackerTest,
}, },
Uuid(kFastAdvertisementServiceUuid)); Uuid(kFastAdvertisementServiceUuid));
discovered_peripheral_tracker_.StartTracking( discovered_peripheral_tracker_.StartTracking(
std::string(kServiceIdB), std::string(kServiceIdB), false, Pcp::kP2pPointToPoint,
{ {
.peripheral_discovered_cb = .peripheral_discovered_cb =
[&found_latch_b](BleV2Peripheral peripheral, [&found_latch_b](BleV2Peripheral peripheral,
@@ -1071,7 +1114,7 @@ TEST_P(DiscoveredPeripheralTrackerTest,
CountDownLatch fetch_latch(1); CountDownLatch fetch_latch(1);
discovered_peripheral_tracker_.StartTracking( discovered_peripheral_tracker_.StartTracking(
std::string(kServiceIdA), std::string(kServiceIdA), false, Pcp::kP2pPointToPoint,
{ {
.peripheral_discovered_cb = .peripheral_discovered_cb =
[&found_latch](BleV2Peripheral peripheral, [&found_latch](BleV2Peripheral peripheral,
@@ -1125,7 +1168,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, LostPeripheralForInstantOnLost) {
CountDownLatch fetch_latch(1); CountDownLatch fetch_latch(1);
discovered_peripheral_tracker_.StartTracking( discovered_peripheral_tracker_.StartTracking(
std::string(kServiceIdA), std::string(kServiceIdA), false, Pcp::kP2pPointToPoint,
{ {
.peripheral_discovered_cb = .peripheral_discovered_cb =
[&found_latch](BleV2Peripheral peripheral, [&found_latch](BleV2Peripheral peripheral,
@@ -1190,7 +1233,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, InstantLostPeripheralForInstantOnLost) {
CountDownLatch fetch_latch(1); CountDownLatch fetch_latch(1);
discovered_peripheral_tracker_.StartTracking( discovered_peripheral_tracker_.StartTracking(
std::string(kServiceIdA), std::string(kServiceIdA), false, Pcp::kP2pPointToPoint,
{ {
.peripheral_discovered_cb = .peripheral_discovered_cb =
[&found_latch](BleV2Peripheral peripheral, [&found_latch](BleV2Peripheral peripheral,
@@ -1256,7 +1299,7 @@ TEST_P(DiscoveredPeripheralTrackerTest,
MockDiscoveredPeripheralCallback mock_callback; MockDiscoveredPeripheralCallback mock_callback;
discovered_peripheral_tracker_.StartTracking( discovered_peripheral_tracker_.StartTracking(
std::string(kServiceIdA), std::string(kServiceIdA), false, Pcp::kP2pPointToPoint,
{ {
.peripheral_discovered_cb = .peripheral_discovered_cb =
[&mock_callback](BleV2Peripheral peripheral, [&mock_callback](BleV2Peripheral peripheral,
@@ -1334,7 +1377,7 @@ TEST_P(DiscoveredPeripheralTrackerTest,
CountDownLatch fetch_latch(1); CountDownLatch fetch_latch(1);
discovered_peripheral_tracker_.StartTracking( discovered_peripheral_tracker_.StartTracking(
std::string(kServiceIdA), std::string(kServiceIdA), false, Pcp::kP2pPointToPoint,
{ {
.peripheral_discovered_cb = .peripheral_discovered_cb =
[&found_latch](BleV2Peripheral peripheral, [&found_latch](BleV2Peripheral peripheral,
@@ -1394,7 +1437,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, HandleDummyAdvertisement) {
CountDownLatch legacy_device_found_latch(1); CountDownLatch legacy_device_found_latch(1);
discovered_peripheral_tracker_.StartTracking( discovered_peripheral_tracker_.StartTracking(
std::string(kServiceIdA), std::string(kServiceIdA), false, Pcp::kP2pPointToPoint,
{ {
.peripheral_discovered_cb = .peripheral_discovered_cb =
[](BleV2Peripheral peripheral, const std::string& service_id, [](BleV2Peripheral peripheral, const std::string& service_id,
@@ -1430,7 +1473,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, SkipDummyAdvertisement) {
CountDownLatch legacy_device_found_latch(1); CountDownLatch legacy_device_found_latch(1);
discovered_peripheral_tracker_.StartTracking( discovered_peripheral_tracker_.StartTracking(
std::string(kServiceIdA), std::string(kServiceIdA), false, Pcp::kP2pPointToPoint,
{ {
.peripheral_discovered_cb = .peripheral_discovered_cb =
[](BleV2Peripheral peripheral, const std::string& service_id, [](BleV2Peripheral peripheral, const std::string& service_id,
@@ -1463,7 +1506,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, FetchGattAdvertisementInThread) {
CountDownLatch fetch_latch(1); CountDownLatch fetch_latch(1);
discovered_peripheral_tracker_.StartTracking( discovered_peripheral_tracker_.StartTracking(
std::string(kServiceIdA), std::string(kServiceIdA), false, Pcp::kP2pPointToPoint,
{ {
.peripheral_discovered_cb = .peripheral_discovered_cb =
[&found_latch](BleV2Peripheral peripheral, [&found_latch](BleV2Peripheral peripheral,
@@ -1505,7 +1548,7 @@ TEST_P(DiscoveredPeripheralTrackerTest,
CountDownLatch fetch_latch(1); CountDownLatch fetch_latch(1);
discovered_peripheral_tracker_.StartTracking( discovered_peripheral_tracker_.StartTracking(
std::string(kServiceIdA), std::string(kServiceIdA), false, Pcp::kP2pPointToPoint,
{ {
.peripheral_discovered_cb = .peripheral_discovered_cb =
[&found_latch](BleV2Peripheral peripheral, [&found_latch](BleV2Peripheral peripheral,
@@ -1554,7 +1597,7 @@ TEST_P(DiscoveredPeripheralTrackerTest,
CountDownLatch fetch_latch(2); CountDownLatch fetch_latch(2);
discovered_peripheral_tracker_.StartTracking( discovered_peripheral_tracker_.StartTracking(
std::string(kServiceIdA), std::string(kServiceIdA), false, Pcp::kP2pPointToPoint,
{ {
.peripheral_discovered_cb = .peripheral_discovered_cb =
[&found_latch](BleV2Peripheral peripheral, [&found_latch](BleV2Peripheral peripheral,
@@ -24,6 +24,7 @@
#include "connections/implementation/flags/nearby_connections_feature_flags.h" #include "connections/implementation/flags/nearby_connections_feature_flags.h"
#include "connections/implementation/mediums/ble_v2/discovered_peripheral_callback.h" #include "connections/implementation/mediums/ble_v2/discovered_peripheral_callback.h"
#include "connections/implementation/mediums/bluetooth_radio.h" #include "connections/implementation/mediums/bluetooth_radio.h"
#include "connections/implementation/pcp.h"
#include "connections/power_level.h" #include "connections/power_level.h"
#include "internal/flags/nearby_flags.h" #include "internal/flags/nearby_flags.h"
#include "internal/platform/ble_v2.h" #include "internal/platform/ble_v2.h"
@@ -102,7 +103,8 @@ TEST_P(BleV2Test, CanConnect) {
BleV2Peripheral discovered_peripheral; BleV2Peripheral discovered_peripheral;
ble_client.StartScanning( ble_client.StartScanning(
service_id, PowerLevel::kHighPower, service_id, Pcp::kP2pPointToPoint, PowerLevel::kHighPower,
/*include_dct_advertisement=*/false,
mediums::DiscoveredPeripheralCallback{ mediums::DiscoveredPeripheralCallback{
.peripheral_discovered_cb = .peripheral_discovered_cb =
[&discovered_latch, &discovered_peripheral]( [&discovered_latch, &discovered_peripheral](
@@ -162,7 +164,8 @@ TEST_P(BleV2Test, CanCancelConnect) {
BleV2Peripheral discovered_peripheral; BleV2Peripheral discovered_peripheral;
ble_client.StartScanning( ble_client.StartScanning(
service_id, PowerLevel::kHighPower, service_id, Pcp::kP2pPointToPoint, PowerLevel::kHighPower,
/*include_dct_advertisement=*/false,
mediums::DiscoveredPeripheralCallback{ mediums::DiscoveredPeripheralCallback{
.peripheral_discovered_cb = .peripheral_discovered_cb =
[&discovered_latch, &discovered_peripheral]( [&discovered_latch, &discovered_peripheral](
@@ -234,7 +237,8 @@ TEST_F(BleV2Test, CanStartFastAdvertising) {
CountDownLatch found_latch(1); CountDownLatch found_latch(1);
ble_b.StartScanning( ble_b.StartScanning(
std::string(kServiceIDA), PowerLevel::kHighPower, std::string(kServiceIDA), Pcp::kP2pPointToPoint, PowerLevel::kHighPower,
/*include_dct_advertisement=*/false,
mediums::DiscoveredPeripheralCallback{ mediums::DiscoveredPeripheralCallback{
.peripheral_discovered_cb = .peripheral_discovered_cb =
[&found_latch](BleV2Peripheral peripheral, [&found_latch](BleV2Peripheral peripheral,
@@ -270,7 +274,8 @@ TEST_F(BleV2Test, CanStartFastScanning) {
BleV2::AdvertisingType::kFast, advertisement_bytes); BleV2::AdvertisingType::kFast, advertisement_bytes);
EXPECT_TRUE(ble_a.StartScanning( EXPECT_TRUE(ble_a.StartScanning(
std::string(kServiceIDA), PowerLevel::kHighPower, std::string(kServiceIDA), Pcp::kP2pPointToPoint, PowerLevel::kHighPower,
/*include_dct_advertisement=*/false,
mediums::DiscoveredPeripheralCallback{ mediums::DiscoveredPeripheralCallback{
.peripheral_discovered_cb = .peripheral_discovered_cb =
[&found_latch](BleV2Peripheral peripheral, [&found_latch](BleV2Peripheral peripheral,
@@ -300,7 +305,8 @@ TEST_F(BleV2Test, CanStartAdvertising) {
CountDownLatch found_latch(1); CountDownLatch found_latch(1);
ble_b.StartScanning( ble_b.StartScanning(
std::string(kServiceIDA), PowerLevel::kHighPower, std::string(kServiceIDA), Pcp::kP2pPointToPoint, PowerLevel::kHighPower,
/*include_dct_advertisement=*/false,
mediums::DiscoveredPeripheralCallback{ mediums::DiscoveredPeripheralCallback{
.peripheral_discovered_cb = .peripheral_discovered_cb =
[&found_latch](BleV2Peripheral peripheral, [&found_latch](BleV2Peripheral peripheral,
@@ -336,7 +342,8 @@ TEST_F(BleV2Test, CanStartScanning) {
BleV2::AdvertisingType::kRegular, advertisement_bytes); BleV2::AdvertisingType::kRegular, advertisement_bytes);
EXPECT_TRUE(ble_a.StartScanning( EXPECT_TRUE(ble_a.StartScanning(
std::string(kServiceIDA), PowerLevel::kHighPower, std::string(kServiceIDA), Pcp::kP2pPointToPoint, PowerLevel::kHighPower,
/*include_dct_advertisement=*/false,
mediums::DiscoveredPeripheralCallback{ mediums::DiscoveredPeripheralCallback{
.peripheral_discovered_cb = .peripheral_discovered_cb =
[&found_latch](BleV2Peripheral peripheral, [&found_latch](BleV2Peripheral peripheral,
@@ -360,16 +367,19 @@ TEST_F(BleV2Test, CanStartStopMultipleScanningWithDifferentServiceIds) {
BleV2 ble(radio); BleV2 ble(radio);
radio.Enable(); radio.Enable();
EXPECT_TRUE(ble.StartScanning(std::string(kServiceIDA), EXPECT_TRUE(ble.StartScanning(std::string(kServiceIDA), Pcp::kP2pPointToPoint,
PowerLevel::kHighPower, PowerLevel::kHighPower,
/*include_dct_advertisement=*/false,
mediums::DiscoveredPeripheralCallback{})); mediums::DiscoveredPeripheralCallback{}));
EXPECT_TRUE(ble.StartScanning(std::string(kServiceIDB), EXPECT_TRUE(ble.StartScanning(std::string(kServiceIDB), Pcp::kP2pPointToPoint,
PowerLevel::kHighPower, PowerLevel::kHighPower,
/*include_dct_advertisement=*/false,
mediums::DiscoveredPeripheralCallback{})); mediums::DiscoveredPeripheralCallback{}));
EXPECT_TRUE(ble.StopScanning(std::string(kServiceIDA))); EXPECT_TRUE(ble.StopScanning(std::string(kServiceIDA)));
EXPECT_TRUE(ble.StartScanning(std::string(kServiceIDA), EXPECT_TRUE(ble.StartScanning(std::string(kServiceIDA), Pcp::kP2pPointToPoint,
PowerLevel::kHighPower, PowerLevel::kHighPower,
/*include_dct_advertisement=*/false,
mediums::DiscoveredPeripheralCallback{})); mediums::DiscoveredPeripheralCallback{}));
EXPECT_TRUE(ble.StopScanning(std::string(kServiceIDA))); EXPECT_TRUE(ble.StopScanning(std::string(kServiceIDA)));
EXPECT_TRUE(ble.StopScanning(std::string(kServiceIDB))); EXPECT_TRUE(ble.StopScanning(std::string(kServiceIDB)));
@@ -397,10 +407,12 @@ TEST_F(BleV2Test, DestructWorksForStartAdvertisingAndScanningWithoutStop) {
// Device B starts scanning with service IDA and IDB // Device B starts scanning with service IDA and IDB
EXPECT_TRUE(ble_b.StartScanning(std::string(kServiceIDA), EXPECT_TRUE(ble_b.StartScanning(std::string(kServiceIDA),
PowerLevel::kHighPower, Pcp::kP2pPointToPoint, PowerLevel::kHighPower,
/*include_dct_advertisement=*/false,
mediums::DiscoveredPeripheralCallback{})); mediums::DiscoveredPeripheralCallback{}));
EXPECT_TRUE(ble_b.StartScanning(std::string(kServiceIDB), EXPECT_TRUE(ble_b.StartScanning(std::string(kServiceIDB),
PowerLevel::kHighPower, Pcp::kP2pPointToPoint, PowerLevel::kHighPower,
/*include_dct_advertisement=*/false,
mediums::DiscoveredPeripheralCallback{})); mediums::DiscoveredPeripheralCallback{}));
env_.Stop(); env_.Stop();
} }
@@ -421,7 +433,8 @@ TEST_F(BleV2Test, StartFastScanningDiscoverAndLostPeripheral) {
BleV2::AdvertisingType::kFast, advertisement_bytes); BleV2::AdvertisingType::kFast, advertisement_bytes);
ble_a.StartScanning( ble_a.StartScanning(
std::string(kServiceIDA), PowerLevel::kHighPower, std::string(kServiceIDA), Pcp::kP2pPointToPoint, PowerLevel::kHighPower,
/*include_dct_advertisement=*/false,
mediums::DiscoveredPeripheralCallback{ mediums::DiscoveredPeripheralCallback{
.peripheral_discovered_cb = .peripheral_discovered_cb =
[&found_latch](BleV2Peripheral peripheral, [&found_latch](BleV2Peripheral peripheral,
@@ -469,7 +482,8 @@ TEST_F(BleV2Test,
BleV2::AdvertisingType::kFast, advertisement_bytes); BleV2::AdvertisingType::kFast, advertisement_bytes);
ble_a.StartScanning( ble_a.StartScanning(
std::string(kServiceIDA), PowerLevel::kHighPower, std::string(kServiceIDA), Pcp::kP2pPointToPoint, PowerLevel::kHighPower,
/*include_dct_advertisement=*/false,
mediums::DiscoveredPeripheralCallback{ mediums::DiscoveredPeripheralCallback{
.peripheral_discovered_cb = .peripheral_discovered_cb =
[&found_latch](BleV2Peripheral peripheral, [&found_latch](BleV2Peripheral peripheral,
@@ -514,7 +528,8 @@ TEST_F(BleV2Test, StartScanningDiscoverAndLostPeripheral) {
BleV2::AdvertisingType::kRegular, advertisement_bytes); BleV2::AdvertisingType::kRegular, advertisement_bytes);
ble_a.StartScanning( ble_a.StartScanning(
std::string(kServiceIDA), PowerLevel::kHighPower, std::string(kServiceIDA), Pcp::kP2pPointToPoint, PowerLevel::kHighPower,
/*include_dct_advertisement=*/false,
mediums::DiscoveredPeripheralCallback{ mediums::DiscoveredPeripheralCallback{
.peripheral_discovered_cb = .peripheral_discovered_cb =
[&found_latch](BleV2Peripheral peripheral, [&found_latch](BleV2Peripheral peripheral,
@@ -561,7 +576,8 @@ TEST_F(BleV2Test, StartScanningDiscoverButNoPeripheralLostAfterStopScanning) {
BleV2::AdvertisingType::kRegular, advertisement_bytes); BleV2::AdvertisingType::kRegular, advertisement_bytes);
ble_a.StartScanning( ble_a.StartScanning(
std::string(kServiceIDA), PowerLevel::kHighPower, std::string(kServiceIDA), Pcp::kP2pPointToPoint, PowerLevel::kHighPower,
/*include_dct_advertisement=*/false,
mediums::DiscoveredPeripheralCallback{ mediums::DiscoveredPeripheralCallback{
.peripheral_discovered_cb = .peripheral_discovered_cb =
[&found_latch](BleV2Peripheral peripheral, [&found_latch](BleV2Peripheral peripheral,
@@ -693,7 +709,8 @@ TEST_F(BleV2Test, HandleLegacyAdvertising) {
EXPECT_FALSE(ble_a.IsAdvertisingForLegacyDevice(std::string(kServiceIDA))); EXPECT_FALSE(ble_a.IsAdvertisingForLegacyDevice(std::string(kServiceIDA)));
std::string legacy_service_id("NearbySharing"); std::string legacy_service_id("NearbySharing");
EXPECT_TRUE(ble_a.StartScanning( EXPECT_TRUE(ble_a.StartScanning(
legacy_service_id, PowerLevel::kHighPower, legacy_service_id, Pcp::kP2pPointToPoint, PowerLevel::kHighPower,
/*include_dct_advertisement=*/false,
mediums::DiscoveredPeripheralCallback{ mediums::DiscoveredPeripheralCallback{
.peripheral_discovered_cb = .peripheral_discovered_cb =
[](BleV2Peripheral peripheral, const std::string& service_id, [](BleV2Peripheral peripheral, const std::string& service_id,
@@ -729,7 +746,8 @@ TEST_F(BleV2Test, CanStartAsyncScanning) {
BleV2::AdvertisingType::kRegular, advertisement_bytes); BleV2::AdvertisingType::kRegular, advertisement_bytes);
EXPECT_TRUE(ble_a.StartScanning( EXPECT_TRUE(ble_a.StartScanning(
std::string(kServiceIDA), PowerLevel::kHighPower, std::string(kServiceIDA), Pcp::kP2pPointToPoint, PowerLevel::kHighPower,
/*include_dct_advertisement=*/false,
mediums::DiscoveredPeripheralCallback{ mediums::DiscoveredPeripheralCallback{
.peripheral_discovered_cb = .peripheral_discovered_cb =
[&found_latch](BleV2Peripheral peripheral, [&found_latch](BleV2Peripheral peripheral,
@@ -765,7 +783,8 @@ TEST_F(BleV2Test, StartAsyncScanningWithPlatformErrors) {
// Disable radio a to simulate platform error. // Disable radio a to simulate platform error.
radio_a.Disable(); radio_a.Disable();
EXPECT_FALSE(ble_a.StartScanning( EXPECT_FALSE(ble_a.StartScanning(
std::string(kServiceIDA), PowerLevel::kHighPower, std::string(kServiceIDA), Pcp::kP2pPointToPoint, PowerLevel::kHighPower,
/*include_dct_advertisement=*/false,
mediums::DiscoveredPeripheralCallback{ mediums::DiscoveredPeripheralCallback{
.peripheral_discovered_cb = .peripheral_discovered_cb =
[&found_latch](BleV2Peripheral peripheral, [&found_latch](BleV2Peripheral peripheral,
@@ -779,7 +798,8 @@ TEST_F(BleV2Test, StartAsyncScanningWithPlatformErrors) {
radio_a.Enable(); radio_a.Enable();
EXPECT_TRUE(ble_a.StartScanning( EXPECT_TRUE(ble_a.StartScanning(
std::string(kServiceIDA), PowerLevel::kHighPower, std::string(kServiceIDA), Pcp::kP2pPointToPoint, PowerLevel::kHighPower,
/*include_dct_advertisement=*/false,
mediums::DiscoveredPeripheralCallback{ mediums::DiscoveredPeripheralCallback{
.peripheral_discovered_cb = .peripheral_discovered_cb =
[&found_latch](BleV2Peripheral peripheral, [&found_latch](BleV2Peripheral peripheral,
@@ -818,7 +838,8 @@ TEST_F(BleV2Test, StartAsyncScanningDiscoverAndLostPeripheral) {
BleV2::AdvertisingType::kRegular, advertisement_bytes); BleV2::AdvertisingType::kRegular, advertisement_bytes);
EXPECT_TRUE(ble_a.StartScanning( EXPECT_TRUE(ble_a.StartScanning(
std::string(kServiceIDA), PowerLevel::kHighPower, std::string(kServiceIDA), Pcp::kP2pPointToPoint, PowerLevel::kHighPower,
/*include_dct_advertisement=*/false,
mediums::DiscoveredPeripheralCallback{ mediums::DiscoveredPeripheralCallback{
.peripheral_discovered_cb = .peripheral_discovered_cb =
[&found_latch](BleV2Peripheral peripheral, [&found_latch](BleV2Peripheral peripheral,
@@ -866,7 +887,8 @@ TEST_F(BleV2Test,
BleV2::AdvertisingType::kRegular, advertisement_bytes); BleV2::AdvertisingType::kRegular, advertisement_bytes);
EXPECT_TRUE(ble_a.StartScanning( EXPECT_TRUE(ble_a.StartScanning(
std::string(kServiceIDA), PowerLevel::kHighPower, std::string(kServiceIDA), Pcp::kP2pPointToPoint, PowerLevel::kHighPower,
/*include_dct_advertisement=*/false,
mediums::DiscoveredPeripheralCallback{ mediums::DiscoveredPeripheralCallback{
.peripheral_discovered_cb = .peripheral_discovered_cb =
[&found_latch](BleV2Peripheral peripheral, [&found_latch](BleV2Peripheral peripheral,
@@ -920,7 +942,8 @@ TEST_F(BleV2Test, CanStartStopMultipleAsyncScanningWithDifferentServiceIds) {
BleV2::AdvertisingType::kRegular, advertisement_bytes_b); BleV2::AdvertisingType::kRegular, advertisement_bytes_b);
ble_scanner.StartScanning( ble_scanner.StartScanning(
std::string(kServiceIDA), PowerLevel::kHighPower, std::string(kServiceIDA), Pcp::kP2pPointToPoint, PowerLevel::kHighPower,
/*include_dct_advertisement=*/false,
mediums::DiscoveredPeripheralCallback{ mediums::DiscoveredPeripheralCallback{
.peripheral_discovered_cb = .peripheral_discovered_cb =
[&found_latch_a](BleV2Peripheral peripheral, [&found_latch_a](BleV2Peripheral peripheral,
@@ -934,7 +957,8 @@ TEST_F(BleV2Test, CanStartStopMultipleAsyncScanningWithDifferentServiceIds) {
}); });
ble_scanner.StartScanning( ble_scanner.StartScanning(
std::string(kServiceIDB), PowerLevel::kHighPower, std::string(kServiceIDB), Pcp::kP2pPointToPoint, PowerLevel::kHighPower,
/*include_dct_advertisement=*/false,
mediums::DiscoveredPeripheralCallback{ mediums::DiscoveredPeripheralCallback{
.peripheral_discovered_cb = .peripheral_discovered_cb =
[&found_latch_b](BleV2Peripheral peripheral, [&found_latch_b](BleV2Peripheral peripheral,
@@ -981,7 +1005,8 @@ TEST_F(BleV2Test, StartMultipleAsyncScanningDiscoverAndLostPeripheral) {
BleV2::AdvertisingType::kRegular, advertisement_bytes_b); BleV2::AdvertisingType::kRegular, advertisement_bytes_b);
ble_scanner.StartScanning( ble_scanner.StartScanning(
std::string(kServiceIDA), PowerLevel::kHighPower, std::string(kServiceIDA), Pcp::kP2pPointToPoint, PowerLevel::kHighPower,
/*include_dct_advertisement=*/false,
mediums::DiscoveredPeripheralCallback{ mediums::DiscoveredPeripheralCallback{
.peripheral_discovered_cb = .peripheral_discovered_cb =
[&found_latch_a](BleV2Peripheral peripheral, [&found_latch_a](BleV2Peripheral peripheral,
@@ -1004,7 +1029,8 @@ TEST_F(BleV2Test, StartMultipleAsyncScanningDiscoverAndLostPeripheral) {
}); });
ble_scanner.StartScanning( ble_scanner.StartScanning(
std::string(kServiceIDB), PowerLevel::kHighPower, std::string(kServiceIDB), Pcp::kP2pPointToPoint, PowerLevel::kHighPower,
/*include_dct_advertisement=*/false,
mediums::DiscoveredPeripheralCallback{ mediums::DiscoveredPeripheralCallback{
.peripheral_discovered_cb = .peripheral_discovered_cb =
[&found_latch_b](BleV2Peripheral peripheral, [&found_latch_b](BleV2Peripheral peripheral,
@@ -2624,7 +2624,7 @@ ErrorOr<Medium> P2pClusterPcpHandler::StartBleV2Scanning(
*discovery_options.ble_options.alternate_uuid, service_id); *discovery_options.ble_options.alternate_uuid, service_id);
} }
ErrorOr<bool> ble_v2_result = ble_v2_medium_.StartScanning( ErrorOr<bool> ble_v2_result = ble_v2_medium_.StartScanning(
service_id, power_level, service_id, GetPcp(), power_level, client->IsDctEnabled(),
{ {
.peripheral_discovered_cb = absl::bind_front( .peripheral_discovered_cb = absl::bind_front(
&P2pClusterPcpHandler::BleV2PeripheralDiscoveredHandler, this, &P2pClusterPcpHandler::BleV2PeripheralDiscoveredHandler, this,