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

PiperOrigin-RevId: 405782006
This commit is contained in:
edwinwu
2021-10-26 18:27:36 -07:00
committed by Copybara-Service
parent ef57b09fc3
commit 83480d6473
15 changed files with 786 additions and 85 deletions
+126 -12
View File
@@ -37,6 +37,8 @@ constexpr FeatureFlags kTestCases[] = {
},
};
constexpr absl::Duration kWaitDuration = absl::Milliseconds(1000);
constexpr absl::string_view kServiceId{"service_id"};
constexpr absl::string_view kServiceType{"_service.tcp_"};
constexpr absl::string_view kServiceInfoName{"Simulated service info name"};
constexpr absl::string_view kEndpointName{"Simulated endpoint name"};
@@ -44,6 +46,8 @@ constexpr absl::string_view kEndpointInfoKey{"n"};
class WifiLanMediumV2Test : public ::testing::TestWithParam<FeatureFlags> {
protected:
using DiscoveredServiceCallback = WifiLanMediumV2::DiscoveredServiceCallback;
WifiLanMediumV2Test() { env_.Stop(); }
MediumEnvironment& env_{MediumEnvironment::Instance()};
@@ -67,16 +71,15 @@ 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};
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.StartAdvertising(nsd_service_info));
EXPECT_TRUE(wifi_lan_a.StopAdvertising(nsd_service_info));
env_.Stop();
}
@@ -84,19 +87,23 @@ TEST_F(WifiLanMediumV2Test, CanStartAdvertising) {
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};
std::string service_type_1(kServiceType);
std::string service_type_2("_service_1.tcp_");
std::string service_info_name_1(kServiceInfoName);
std::string service_info_name_2(kServiceInfoName);
std::string endpoint_info_name(kEndpointName);
NsdServiceInfo nsd_service_info_1;
nsd_service_info_1.SetServiceName(service_info_name);
nsd_service_info_1.SetServiceName(service_info_name_1);
nsd_service_info_1.SetTxtRecord(std::string(kEndpointInfoKey),
endpoint_info_name);
nsd_service_info_1.SetServiceType(service_type);
nsd_service_info_1.SetServiceType(service_type_1);
NsdServiceInfo nsd_service_info_2 = nsd_service_info_1;
nsd_service_info_2.SetServiceType(service_tye_1);
NsdServiceInfo nsd_service_info_2;
nsd_service_info_2.SetServiceName(service_info_name_2);
nsd_service_info_2.SetTxtRecord(std::string(kEndpointInfoKey),
endpoint_info_name);
nsd_service_info_2.SetServiceType(service_type_2);
EXPECT_TRUE(wifi_lan_a.StartAdvertising(nsd_service_info_1));
EXPECT_TRUE(wifi_lan_a.StartAdvertising(nsd_service_info_2));
@@ -105,6 +112,113 @@ TEST_F(WifiLanMediumV2Test, CanStartMultipleAdvertising) {
env_.Stop();
}
TEST_F(WifiLanMediumV2Test, CanStartDiscovery) {
env_.Start();
WifiLanMediumV2 wifi_lan_a;
std::string service_id(kServiceId);
std::string service_type(kServiceType);
EXPECT_TRUE(wifi_lan_a.StartDiscovery(service_id, service_type,
DiscoveredServiceCallback{}));
EXPECT_TRUE(wifi_lan_a.StopDiscovery(service_type));
env_.Stop();
}
TEST_F(WifiLanMediumV2Test, CanStartMultipleDiscovery) {
env_.Start();
WifiLanMediumV2 wifi_lan_a;
std::string service_id_1(kServiceId);
std::string service_id_2("service_id_2");
std::string service_type_1(kServiceType);
std::string service_type_2("_service_1.tcp_");
EXPECT_TRUE(wifi_lan_a.StartDiscovery(service_id_1, service_type_1,
DiscoveredServiceCallback{}));
EXPECT_TRUE(wifi_lan_a.StartDiscovery(service_id_2, service_type_2,
DiscoveredServiceCallback{}));
EXPECT_TRUE(wifi_lan_a.StopDiscovery(service_type_1));
EXPECT_TRUE(wifi_lan_a.StopDiscovery(service_type_2));
env_.Stop();
}
TEST_F(WifiLanMediumV2Test, CanAdvertiseThatOtherMediumDiscover) {
env_.Start();
WifiLanMediumV2 wifi_lan_a;
WifiLanMediumV2 wifi_lan_b;
std::string service_id(kServiceId);
std::string service_type(kServiceType);
std::string service_info_name(kServiceInfoName);
std::string endpoint_info_name(kEndpointName);
CountDownLatch discovered_latch(1);
CountDownLatch lost_latch(1);
wifi_lan_b.StartDiscovery(
service_id, service_type,
DiscoveredServiceCallback{
.service_discovered_cb =
[&discovered_latch](NsdServiceInfo service_info,
const std::string& service_type) {
discovered_latch.CountDown();
},
.service_lost_cb =
[&lost_latch](NsdServiceInfo service_info,
const std::string& service_id) {
lost_latch.CountDown();
},
});
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);
EXPECT_TRUE(wifi_lan_a.StartAdvertising(nsd_service_info));
EXPECT_TRUE(discovered_latch.Await(kWaitDuration).result());
EXPECT_TRUE(wifi_lan_a.StopAdvertising(nsd_service_info));
EXPECT_TRUE(lost_latch.Await(kWaitDuration).result());
EXPECT_TRUE(wifi_lan_b.StopDiscovery(service_type));
env_.Stop();
}
TEST_F(WifiLanMediumV2Test, CanDiscoverThatOtherMediumAdvertise) {
env_.Start();
WifiLanMediumV2 wifi_lan_a;
WifiLanMediumV2 wifi_lan_b;
std::string service_id(kServiceId);
std::string service_type(kServiceType);
std::string service_info_name(kServiceInfoName);
std::string endpoint_info_name(kEndpointName);
CountDownLatch discovered_latch(1);
CountDownLatch lost_latch(1);
wifi_lan_a.StartDiscovery(
service_id, service_type,
DiscoveredServiceCallback{
.service_discovered_cb =
[&discovered_latch](NsdServiceInfo service_info,
const std::string& service_type) {
discovered_latch.CountDown();
},
.service_lost_cb =
[&lost_latch](NsdServiceInfo service_info,
const std::string& service_type) {
lost_latch.CountDown();
},
});
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);
EXPECT_TRUE(wifi_lan_b.StartAdvertising(nsd_service_info));
EXPECT_TRUE(discovered_latch.Await(kWaitDuration).result());
EXPECT_TRUE(wifi_lan_b.StopAdvertising(nsd_service_info));
EXPECT_TRUE(lost_latch.Await(kWaitDuration).result());
EXPECT_TRUE(wifi_lan_a.StopDiscovery(service_type));
env_.Stop();
}
} // namespace
} // namespace nearby
} // namespace location
+91 -3
View File
@@ -27,13 +27,101 @@ bool WifiLanMediumV2::StopAdvertising(const NsdServiceInfo& nsd_service_info) {
return impl_->StopAdvertising(nsd_service_info);
}
bool WifiLanMediumV2::StartDiscovery(const std::string& service_type,
bool WifiLanMediumV2::StartDiscovery(const std::string& service_id,
const std::string& service_type,
DiscoveredServiceCallback callback) {
return false;
{
MutexLock lock(&mutex_);
if (discovery_callbacks_.contains(service_type)) {
NEARBY_LOGS(INFO) << "WifiLan Discovery already start with service_type="
<< service_type << "; impl=" << &GetImpl();
return false;
}
}
api::WifiLanMediumV2::DiscoveredServiceCallback api_callback = {
.service_discovered_cb =
[this](NsdServiceInfo service_info) {
MutexLock lock(&mutex_);
std::string service_type = service_info.GetServiceType();
auto pair = discovery_services_.insert(service_type);
if (!pair.second) {
NEARBY_LOGS(INFO)
<< "Discovering (again) service_info=" << &service_info
<< ", service_type=" << service_type
<< ", service_name=" << service_info.GetServiceName();
return;
}
NEARBY_LOGS(INFO)
<< "Adding service_info=" << &service_info
<< ", service_type=" << service_type
<< ", service_name=" << service_info.GetServiceName();
// Callback service found.
const auto& it = discovery_callbacks_.find(service_type);
if (it != discovery_callbacks_.end()) {
std::string service_id = it->second->service_id;
DiscoveredServiceCallback medium_callback =
it->second->medium_callback;
medium_callback.service_discovered_cb(service_info, service_id);
} else {
NEARBY_LOGS(ERROR)
<< "There is no callback found for service_type="
<< service_type;
}
},
.service_lost_cb =
[this](NsdServiceInfo service_info) {
MutexLock lock(&mutex_);
std::string service_type = service_info.GetServiceType();
auto item = discovery_services_.extract(service_type);
if (item.empty()) return;
NEARBY_LOGS(INFO)
<< "Removing service_info=" << &service_info
<< ", service_type=" << service_type
<< ", service_info_name=" << service_info.GetServiceName();
// Callback service lost.
const auto& it = discovery_callbacks_.find(service_type);
if (it != discovery_callbacks_.end()) {
std::string service_id = it->second->service_id;
DiscoveredServiceCallback medium_callback =
it->second->medium_callback;
medium_callback.service_lost_cb(service_info, service_id);
}
},
};
{
// Insert callback to the map first no matter it succeeds or not.
MutexLock lock(&mutex_);
auto pair = discovery_callbacks_.insert(
{service_type, absl::make_unique<DiscoveryCallbackInfo>()});
auto& context = *pair.first->second;
context.medium_callback = std::move(callback);
context.service_id = service_id;
}
bool success = impl_->StartDiscovery(service_type, std::move(api_callback));
if (!success) {
// If failed, then revert back the insertion.
MutexLock lock(&mutex_);
discovery_callbacks_.erase(service_type);
}
NEARBY_LOGS(INFO) << "WifiLan Discovery started for service_type="
<< service_type << ", impl=" << &GetImpl()
<< ", success=" << success;
return success;
}
bool WifiLanMediumV2::StopDiscovery(const std::string& service_type) {
return false;
MutexLock lock(&mutex_);
if (!discovery_callbacks_.contains(service_type)) {
return false;
}
discovery_callbacks_.erase(service_type);
if (discovery_services_.contains(service_type)) {
discovery_services_.erase(service_type);
}
NEARBY_LOGS(INFO) << "WifiLan Discovery disabled for service_type="
<< service_type << ", impl=" << &GetImpl();
return impl_->StopDiscovery(service_type);
}
WifiLanSocketV2 WifiLanMediumV2::ConnectToService(
+6 -9
View File
@@ -126,15 +126,9 @@ class WifiLanMediumV2 {
public:
using Platform = api::ImplementationPlatform;
// WifiLanService is a proxy object created as a result of WifiLan discovery.
// Its lifetime spans between calls to service_discovered_cb and
// service_lost_cb.
// It is safe to use WifiLanService in service_discovered_cb() callback
// and at any time afterwards, until service_lost_cb() is called.
// It is not safe to use WifiLanService after returning from
// service_lost_cb() callback.
struct DiscoveredServiceCallback {
std::function<void(NsdServiceInfo, const std::string& service_type)>
std::function<void(NsdServiceInfo service_info,
const std::string& service_type)>
service_discovered_cb =
DefaultCallback<NsdServiceInfo, const std::string&>();
std::function<void(NsdServiceInfo service_info,
@@ -143,6 +137,7 @@ class WifiLanMediumV2 {
};
struct DiscoveryCallbackInfo {
std::string service_id;
DiscoveredServiceCallback medium_callback;
};
@@ -169,7 +164,8 @@ class WifiLanMediumV2 {
bool StopAdvertising(const NsdServiceInfo& nsd_service_info);
// Returns true once the WifiLan discovery has been initiated.
bool StartDiscovery(const std::string& service_type,
bool StartDiscovery(const std::string& service_id,
const std::string& service_type,
DiscoveredServiceCallback callback);
// Returns true once service_type is associated to existing callback. If the
@@ -201,6 +197,7 @@ class WifiLanMediumV2 {
std::unique_ptr<api::WifiLanMediumV2> impl_;
absl::flat_hash_map<std::string, std::unique_ptr<DiscoveryCallbackInfo>>
discovery_callbacks_ ABSL_GUARDED_BY(mutex_);
absl::flat_hash_set<std::string> discovery_services_ ABSL_GUARDED_BY(mutex_);
};
} // namespace nearby