Add advertising logic for DCT

PiperOrigin-RevId: 733752366
This commit is contained in:
Guogang Li
2025-03-05 08:54:56 -08:00
committed by Copybara-Service
parent 18f52de400
commit 553f5dbf01
8 changed files with 185 additions and 34 deletions
+68 -17
View File
@@ -28,9 +28,9 @@
#include "absl/container/flat_hash_map.h"
#include "absl/container/flat_hash_set.h"
#include "absl/functional/any_invocable.h"
#include "absl/random/random.h"
#include "absl/strings/escaping.h"
#include "absl/strings/string_view.h"
#include "absl/time/time.h"
#include "absl/types/span.h"
#include "connections/advertising_options.h"
#include "connections/connection_options.h"
@@ -39,6 +39,7 @@
#include "connections/implementation/analytics/analytics_recorder.h"
#include "connections/implementation/analytics/discovery_metadata_params.h"
#include "connections/implementation/flags/nearby_connections_feature_flags.h"
#include "connections/implementation/mediums/advertisements/dct_advertisement.h"
#include "connections/listeners.h"
#include "connections/medium_selector.h"
#include "connections/payload.h"
@@ -107,6 +108,9 @@ ClientProxy::ClientProxy(::nearby::analytics::EventLogger* event_logger)
NEARBY_LOGS(INFO) << "[safe-to-disconnect]: Local enabled: "
<< supports_safe_to_disconnect_
<< "; Version: " << local_safe_to_disconnect_version_;
// Generate a 7 bits dedup value.
absl::BitGen bitgen;
dct_dedup_ = absl::Uniform(bitgen, 0, 1 << 7);
}
ClientProxy::~ClientProxy() { Reset(); }
@@ -115,24 +119,29 @@ std::int64_t ClientProxy::GetClientId() const { return client_id_; }
std::string ClientProxy::GetLocalEndpointId() {
MutexLock lock(&mutex_);
if (!local_endpoint_id_.empty()) {
NEARBY_LOGS(INFO) << __func__
<< ": Reusing cached endpoint id: " << local_endpoint_id_;
if (IsDctEnabled() && GetEndpointIdForDct().has_value()) {
NEARBY_LOGS(INFO) << "DCT is using genereted endpoint id.";
return GetEndpointIdForDct().value();
} else {
if (!local_endpoint_id_.empty()) {
NEARBY_LOGS(INFO) << __func__ << ": Reusing cached endpoint id: "
<< local_endpoint_id_;
return local_endpoint_id_;
}
if (external_device_provider_ == nullptr) {
local_endpoint_id_ = GenerateLocalEndpointId();
NEARBY_LOGS(INFO) << __func__ << ": Locally generating endpoint id: "
<< local_endpoint_id_;
} else {
local_endpoint_id_ =
external_device_provider_->GetLocalDevice()->GetEndpointId();
NEARBY_LOGS(INFO)
<< __func__
<< ": From external device provider, populating endpoint id: "
<< local_endpoint_id_;
}
return local_endpoint_id_;
}
if (external_device_provider_ == nullptr) {
local_endpoint_id_ = GenerateLocalEndpointId();
NEARBY_LOGS(INFO) << __func__ << ": Locally generating endpoint id: "
<< local_endpoint_id_;
} else {
local_endpoint_id_ =
external_device_provider_->GetLocalDevice()->GetEndpointId();
NEARBY_LOGS(INFO)
<< __func__
<< ": From external device provider, populating endpoint id: "
<< local_endpoint_id_;
}
return local_endpoint_id_;
}
const NearbyDevice* ClientProxy::GetLocalDevice() {
@@ -1301,6 +1310,48 @@ void ClientProxy::SetWebRtcNonCellular(bool webrtc_non_cellular) {
webrtc_non_cellular_ = webrtc_non_cellular;
}
bool ClientProxy::IsDctEnabled() const {
if (api::ImplementationPlatform::GetCurrentOS() != api::OSName::kApple) {
return false;
}
#if defined(NC_IOS_SDK)
return true;
#else
return false;
#endif
}
uint8_t ClientProxy::GetDctDedup() const { return dct_dedup_; }
void ClientProxy::UpdateDctDeviceName(absl::string_view device_name) {
if (!dct_device_name_.empty() && dct_device_name_ != device_name) {
// Need to update dedup value if device name is changed.
absl::BitGen bitgen;
dct_dedup_ = absl::Uniform(bitgen, 0, 1 << 7);
}
dct_device_name_ = device_name;
// The DCT endpoint ID should be derived from device name and dedup value.
std::optional<std::string> dct_endpoint_id =
advertisements::ble::DctAdvertisement::GenerateEndpointId(
dct_dedup_, dct_device_name_);
if (dct_endpoint_id.has_value()) {
dct_endpoint_id_ = *dct_endpoint_id;
} else {
dct_endpoint_id_.clear();
}
}
std::optional<std::string> ClientProxy::GetEndpointIdForDct() const {
if (dct_endpoint_id_.empty()) {
return std::nullopt;
}
return dct_endpoint_id_;
}
std::string ClientProxy::ToString(PayloadProgressInfo::Status status) const {
switch (status) {
case PayloadProgressInfo::Status::kSuccess: