mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-14 22:56:12 -04:00
nearbyconnections : Implement WifiLanV2 Advertising functions for /medium, /public(wrapper), /g3.
PiperOrigin-RevId: 405547661
This commit is contained in:
committed by
Copybara-Service
parent
d0da545834
commit
32836a8357
@@ -26,8 +26,10 @@
|
||||
#include "platform/api/bluetooth_adapter.h"
|
||||
#include "platform/api/bluetooth_classic.h"
|
||||
#include "platform/api/wifi_lan.h"
|
||||
#include "platform/api/wifi_lan_v2.h"
|
||||
#include "platform/base/feature_flags.h"
|
||||
#include "platform/base/logging.h"
|
||||
#include "platform/base/nsd_service_info.h"
|
||||
#include "platform/public/count_down_latch.h"
|
||||
|
||||
namespace location {
|
||||
@@ -65,6 +67,7 @@ void MediumEnvironment::Reset() {
|
||||
webrtc_signaling_message_callback_.clear();
|
||||
webrtc_signaling_complete_callback_.clear();
|
||||
wifi_lan_mediums_.clear();
|
||||
wifi_lan_mediums_v2_.clear();
|
||||
use_valid_peer_connection_ = true;
|
||||
peer_connection_latency_ = absl::ZeroDuration();
|
||||
});
|
||||
@@ -705,6 +708,53 @@ api::WifiLanService* MediumEnvironment::GetWifiLanService(
|
||||
return remote_wifi_lan_service;
|
||||
}
|
||||
|
||||
void MediumEnvironment::RegisterWifiLanMediumV2(api::WifiLanMediumV2& medium) {
|
||||
if (!enabled_) return;
|
||||
RunOnMediumEnvironmentThread([this, &medium]() {
|
||||
wifi_lan_mediums_v2_.insert({&medium, WifiLanMediumV2Context{}});
|
||||
NEARBY_LOG(INFO, "Registered: medium=%p", &medium);
|
||||
});
|
||||
}
|
||||
|
||||
void MediumEnvironment::UpdateWifiLanMediumV2ForAdvertising(
|
||||
api::WifiLanMediumV2& medium, const NsdServiceInfo& service_info,
|
||||
bool enabled) {
|
||||
if (!enabled_) return;
|
||||
RunOnMediumEnvironmentThread([this, &medium, service_info = service_info,
|
||||
enabled]() {
|
||||
std::string service_type = service_info.GetServiceType();
|
||||
NEARBY_LOGS(INFO) << "Update WifiLan medium for advertising: this=" << this
|
||||
<< "; medium=" << &medium
|
||||
<< "; service_name=" << service_info.GetServiceName()
|
||||
<< "; service_type=" << service_type
|
||||
<< ", enabled=" << enabled;
|
||||
for (auto& medium_info : wifi_lan_mediums_v2_) {
|
||||
auto& local_medium = medium_info.first;
|
||||
auto& info = medium_info.second;
|
||||
// Do not send notification to the same medium but update
|
||||
// service info map.
|
||||
if (local_medium == &medium) {
|
||||
if (enabled) {
|
||||
info.advertising_services.insert({service_type, service_info});
|
||||
} else {
|
||||
info.advertising_services.erase(service_type);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void MediumEnvironment::UnregisterWifiLanMediumV2(
|
||||
api::WifiLanMediumV2& medium) {
|
||||
if (!enabled_) return;
|
||||
RunOnMediumEnvironmentThread([this, &medium]() {
|
||||
auto item = wifi_lan_mediums_v2_.extract(&medium);
|
||||
if (item.empty()) return;
|
||||
NEARBY_LOG(INFO, "Unregistered WifiLan medium");
|
||||
});
|
||||
}
|
||||
|
||||
void MediumEnvironment::SetFeatureFlags(const FeatureFlags::Flags& flags) {
|
||||
const_cast<FeatureFlags&>(FeatureFlags::GetInstance()).SetFlags(flags);
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
#define PLATFORM_BASE_MEDIUM_ENVIRONMENT_H_
|
||||
|
||||
#include <atomic>
|
||||
#include <memory>
|
||||
|
||||
#include "absl/container/flat_hash_map.h"
|
||||
#include "absl/strings/string_view.h"
|
||||
@@ -243,6 +244,20 @@ class MediumEnvironment {
|
||||
api::WifiLanService* GetWifiLanService(const std::string& ip_address,
|
||||
int port);
|
||||
|
||||
// Adds medium-related info to allow for discovery/advertising to work.
|
||||
// This provides acccess to this medium from other mediums, when protocol
|
||||
// expects they should communicate.
|
||||
void RegisterWifiLanMediumV2(api::WifiLanMediumV2& medium);
|
||||
|
||||
// Updates advertising info to indicate the current medium is exposing
|
||||
// advertising event.
|
||||
void UpdateWifiLanMediumV2ForAdvertising(
|
||||
api::WifiLanMediumV2& medium, const NsdServiceInfo& nsd_service_info,
|
||||
bool enabled);
|
||||
|
||||
// Removes medium-related info. This should correspond to device power off.
|
||||
void UnregisterWifiLanMediumV2(api::WifiLanMediumV2& medium);
|
||||
|
||||
void SetFeatureFlags(const FeatureFlags::Flags& flags);
|
||||
|
||||
private:
|
||||
@@ -272,6 +287,11 @@ class MediumEnvironment {
|
||||
absl::flat_hash_map<std::string, WifiLanServiceIdContext> services;
|
||||
};
|
||||
|
||||
struct WifiLanMediumV2Context {
|
||||
// advertising service type vs NsdServiceInfo map.
|
||||
absl::flat_hash_map<std::string, NsdServiceInfo> advertising_services;
|
||||
};
|
||||
|
||||
// This is a singleton object, for which destructor will never be called.
|
||||
// Constructor will be invoked once from Instance() static method.
|
||||
// Object is create in-place (with a placement new) to guarantee that
|
||||
@@ -323,6 +343,9 @@ class MediumEnvironment {
|
||||
absl::flat_hash_map<api::WifiLanMedium*, WifiLanMediumContext>
|
||||
wifi_lan_mediums_;
|
||||
|
||||
absl::flat_hash_map<api::WifiLanMediumV2*, WifiLanMediumV2Context>
|
||||
wifi_lan_mediums_v2_;
|
||||
|
||||
bool use_valid_peer_connection_ = true;
|
||||
absl::Duration peer_connection_latency_ = absl::ZeroDuration();
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user