nearbyconnections : Implement WifiLanV2 Advertising functions for /medium, /public(wrapper), /g3.

PiperOrigin-RevId: 405547661
This commit is contained in:
edwinwu
2021-10-25 19:45:53 -07:00
committed by Copybara-Service
parent d0da545834
commit 32836a8357
12 changed files with 570 additions and 32 deletions
+50
View File
@@ -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);
}
+23
View File
@@ -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();
};
+62 -2
View File
@@ -134,15 +134,62 @@ Exception WifiLanServerSocketV2::Close() {
Exception WifiLanServerSocketV2::DoClose() { return {Exception::kSuccess}; }
WifiLanMediumV2::WifiLanMediumV2() {}
WifiLanMediumV2::WifiLanMediumV2() {
auto& env = MediumEnvironment::Instance();
env.RegisterWifiLanMediumV2(*this);
}
WifiLanMediumV2::~WifiLanMediumV2() {}
WifiLanMediumV2::~WifiLanMediumV2() {
auto& env = MediumEnvironment::Instance();
env.UnregisterWifiLanMediumV2(*this);
}
bool WifiLanMediumV2::StartAdvertising(const NsdServiceInfo& nsd_service_info) {
std::string service_type = nsd_service_info.GetServiceType();
NEARBY_LOGS(INFO) << "G3 WifiLan StartAdvertising: nsd_service_info="
<< &nsd_service_info
<< ", service_name=" << nsd_service_info.GetServiceName()
<< ", service_type=" << service_type;
{
absl::MutexLock lock(&mutex_);
if (advertising_info_.Existed(service_type)) {
NEARBY_LOGS(INFO)
<< "G3 WifiLan StartAdvertising: Can't start advertising because "
"service_type="
<< service_type << ", has started already.";
return false;
}
}
auto& env = MediumEnvironment::Instance();
env.UpdateWifiLanMediumV2ForAdvertising(*this, nsd_service_info,
/*enabled=*/true);
{
absl::MutexLock lock(&mutex_);
advertising_info_.Add(service_type);
}
return true;
}
bool WifiLanMediumV2::StopAdvertising(const NsdServiceInfo& nsd_service_info) {
std::string service_type = nsd_service_info.GetServiceType();
NEARBY_LOGS(INFO) << "G3 WifiLan StopAdvertising: nsd_service_info="
<< &nsd_service_info
<< ", service_name=" << nsd_service_info.GetServiceName()
<< ", service_type=" << service_type;
{
absl::MutexLock lock(&mutex_);
if (!advertising_info_.Existed(service_type)) {
NEARBY_LOGS(INFO)
<< "G3 WifiLan StopAdvertising: Can't stop advertising because "
"we never started advertising for service_type="
<< service_type;
return false;
}
advertising_info_.Remove(service_type);
}
auto& env = MediumEnvironment::Instance();
env.UpdateWifiLanMediumV2ForAdvertising(*this, nsd_service_info,
/*enabled=*/false);
return true;
}
@@ -171,6 +218,19 @@ std::unique_ptr<api::WifiLanServerSocketV2> WifiLanMediumV2::ListenForService(
return {};
}
std::pair<std::string, int> WifiLanMediumV2::GetFakeCredentials() const {
std::string ip_address;
ip_address.resize(4);
uint32_t raw_ip_addr = Prng().NextUint32();
uint16_t port = Prng().NextUint32();
ip_address[0] = static_cast<char>(raw_ip_addr >> 24);
ip_address[1] = static_cast<char>(raw_ip_addr >> 16);
ip_address[2] = static_cast<char>(raw_ip_addr >> 8);
ip_address[3] = static_cast<char>(raw_ip_addr >> 0);
return std::make_pair(ip_address, port);
}
} // namespace g3
} // namespace nearby
} // namespace location
+13 -13
View File
@@ -208,26 +208,26 @@ class WifiLanMediumV2 : public api::WifiLanMediumV2 {
int port = 0) override;
private:
static constexpr int kMaxConcurrentAcceptLoops = 5;
struct AdvertisingInfo {
bool Empty() const { return service_id.empty(); }
void Clear() { service_id.clear(); }
bool Empty() const { return service_types.empty(); }
void Clear() { service_types.clear(); }
void Add(const std::string& service_type) {
service_types.insert(service_type);
}
void Remove(const std::string& service_type) {
service_types.erase(service_type);
}
bool Existed(const std::string& service_type) const {
return service_types.contains(service_type);
}
std::string service_id;
absl::flat_hash_set<std::string> service_types;
};
struct DiscoveringInfo {
bool Empty() const { return service_id.empty(); }
void Clear() { service_id.clear(); }
std::string service_id;
};
void SetWifiLanService(const NsdServiceInfo& nsd_service_info);
std::pair<std::string, int> GetFakeCredentials() const;
absl::Mutex mutex_;
AdvertisingInfo advertising_info_ ABSL_GUARDED_BY(mutex_);
};
} // namespace g3
+1
View File
@@ -150,6 +150,7 @@ cc_test(
"scheduled_executor_test.cc",
"single_thread_executor_test.cc",
"wifi_lan_test.cc",
"wifi_lan_test_v2.cc",
],
copts = ["-DCORE_ADAPTER_DLL"],
shard_count = 16,
+110
View File
@@ -0,0 +1,110 @@
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <memory>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "absl/strings/string_view.h"
#include "platform/base/medium_environment.h"
#include "platform/public/count_down_latch.h"
#include "platform/public/logging.h"
#include "platform/public/wifi_lan_v2.h"
namespace location {
namespace nearby {
namespace {
using FeatureFlags = FeatureFlags::Flags;
constexpr FeatureFlags kTestCases[] = {
FeatureFlags{
.enable_cancellation_flag = true,
},
FeatureFlags{
.enable_cancellation_flag = false,
},
};
constexpr absl::string_view kServiceType{"_service.tcp_"};
constexpr absl::string_view kServiceInfoName{"Simulated service info name"};
constexpr absl::string_view kEndpointName{"Simulated endpoint name"};
constexpr absl::string_view kEndpointInfoKey{"n"};
class WifiLanMediumV2Test : public ::testing::TestWithParam<FeatureFlags> {
protected:
WifiLanMediumV2Test() { env_.Stop(); }
MediumEnvironment& env_{MediumEnvironment::Instance()};
};
TEST_F(WifiLanMediumV2Test, ConstructorDestructorWorks) {
env_.Start();
WifiLanMediumV2 wifi_lan_a;
WifiLanMediumV2 wifi_lan_b;
// Make sure we can create functional mediums.
ASSERT_TRUE(wifi_lan_a.IsValid());
ASSERT_TRUE(wifi_lan_b.IsValid());
// Make sure we can create 2 distinct mediums.
EXPECT_NE(&wifi_lan_a.GetImpl(), &wifi_lan_b.GetImpl());
env_.Stop();
}
TEST_F(WifiLanMediumV2Test, CanStartAdvertising) {
env_.Start();
WifiLanMediumV2 wifi_lan_a;
std::string service_type(kServiceType);
std::string service_info_name{kServiceInfoName};
std::string endpoint_info_name{kEndpointName};
NsdServiceInfo nsd_service_info;
nsd_service_info.SetServiceName(service_info_name);
nsd_service_info.SetTxtRecord(std::string(kEndpointInfoKey),
endpoint_info_name);
nsd_service_info.SetServiceType(service_type);
wifi_lan_a.StartAdvertising(nsd_service_info);
EXPECT_TRUE(wifi_lan_a.StopAdvertising(nsd_service_info));
env_.Stop();
}
TEST_F(WifiLanMediumV2Test, CanStartMultipleAdvertising) {
env_.Start();
WifiLanMediumV2 wifi_lan_a;
std::string service_type(kServiceType);
std::string service_tye_1("_service_1.tcp_");
std::string service_info_name{kServiceInfoName};
std::string endpoint_info_name{kEndpointName};
NsdServiceInfo nsd_service_info_1;
nsd_service_info_1.SetServiceName(service_info_name);
nsd_service_info_1.SetTxtRecord(std::string(kEndpointInfoKey),
endpoint_info_name);
nsd_service_info_1.SetServiceType(service_type);
NsdServiceInfo nsd_service_info_2 = nsd_service_info_1;
nsd_service_info_2.SetServiceType(service_tye_1);
EXPECT_TRUE(wifi_lan_a.StartAdvertising(nsd_service_info_1));
EXPECT_TRUE(wifi_lan_a.StartAdvertising(nsd_service_info_2));
EXPECT_TRUE(wifi_lan_a.StopAdvertising(nsd_service_info_1));
EXPECT_TRUE(wifi_lan_a.StopAdvertising(nsd_service_info_2));
env_.Stop();
}
} // namespace
} // namespace nearby
} // namespace location
+2 -2
View File
@@ -20,11 +20,11 @@ namespace location {
namespace nearby {
bool WifiLanMediumV2::StartAdvertising(const NsdServiceInfo& nsd_service_info) {
return false;
return impl_->StartAdvertising(nsd_service_info);
}
bool WifiLanMediumV2::StopAdvertising(const NsdServiceInfo& nsd_service_info) {
return false;
return impl_->StopAdvertising(nsd_service_info);
}
bool WifiLanMediumV2::StartDiscovery(const std::string& service_type,