mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 07:36:10 -04:00
nearbyconnections : Implement WifiLanV2 Discovery functions for /medium, /public(wrapper), /g3.
PiperOrigin-RevId: 405782006
This commit is contained in:
committed by
Copybara-Service
parent
ef57b09fc3
commit
83480d6473
@@ -19,6 +19,7 @@
|
||||
#include "absl/strings/string_view.h"
|
||||
#include "core/internal/mediums/wifi_lan_v2.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"
|
||||
|
||||
@@ -38,14 +39,16 @@ constexpr FeatureFlags kTestCases[] = {
|
||||
},
|
||||
};
|
||||
|
||||
constexpr absl::Duration kWaitDuration = absl::Milliseconds(1000);
|
||||
constexpr absl::string_view kServiceID{"com.google.location.nearby.apps.test"};
|
||||
constexpr absl::string_view kServiceInfoName{
|
||||
"Simulated WifiLan service encrypted string #1"};
|
||||
constexpr absl::string_view kEndpointName{"Simulated endpoint name"};
|
||||
constexpr absl::string_view kServiceInfoName{"ServiceInfoName"};
|
||||
constexpr absl::string_view kEndpointName{"EndpointName"};
|
||||
constexpr absl::string_view kEndpointInfoKey{"n"};
|
||||
|
||||
class WifiLanV2Test : public ::testing::TestWithParam<FeatureFlags> {
|
||||
protected:
|
||||
using DiscoveredServiceCallback = WifiLanMediumV2::DiscoveredServiceCallback;
|
||||
|
||||
WifiLanV2Test() { env_.Stop(); }
|
||||
|
||||
MediumEnvironment& env_{MediumEnvironment::Instance()};
|
||||
@@ -66,8 +69,8 @@ TEST_F(WifiLanV2Test, CanStartAdvertising) {
|
||||
env_.Start();
|
||||
WifiLanV2 wifi_lan_a;
|
||||
std::string service_id(kServiceID);
|
||||
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);
|
||||
@@ -81,20 +84,123 @@ TEST_F(WifiLanV2Test, CanStartAdvertising) {
|
||||
TEST_F(WifiLanV2Test, CanStartMultipleAdvertising) {
|
||||
env_.Start();
|
||||
WifiLanV2 wifi_lan_a;
|
||||
std::string service_id(kServiceID);
|
||||
std::string service_id_1(kServiceID);
|
||||
std::string service_id_2("com.google.location.nearby.apps.test_1");
|
||||
std::string service_info_name{kServiceInfoName};
|
||||
std::string endpoint_info_name{kEndpointName};
|
||||
std::string service_info_name_1(kServiceInfoName);
|
||||
std::string service_info_name_2("ServiceInfoName_1");
|
||||
std::string endpoint_info_name(kEndpointName);
|
||||
|
||||
NsdServiceInfo nsd_service_info_1;
|
||||
nsd_service_info_1.SetServiceName(service_info_name_1);
|
||||
nsd_service_info_1.SetTxtRecord(std::string(kEndpointInfoKey),
|
||||
endpoint_info_name);
|
||||
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);
|
||||
EXPECT_TRUE(wifi_lan_a.StartAdvertising(service_id_1, nsd_service_info_1));
|
||||
EXPECT_TRUE(wifi_lan_a.StartAdvertising(service_id_2, nsd_service_info_2));
|
||||
EXPECT_TRUE(wifi_lan_a.StopAdvertising(service_id_1));
|
||||
EXPECT_TRUE(wifi_lan_a.StopAdvertising(service_id_2));
|
||||
env_.Stop();
|
||||
}
|
||||
|
||||
TEST_F(WifiLanV2Test, CanStartDiscovery) {
|
||||
env_.Start();
|
||||
WifiLanV2 wifi_lan_a;
|
||||
std::string service_id(kServiceID);
|
||||
|
||||
EXPECT_TRUE(
|
||||
wifi_lan_a.StartDiscovery(service_id, DiscoveredServiceCallback{}));
|
||||
EXPECT_TRUE(wifi_lan_a.StopDiscovery(service_id));
|
||||
env_.Stop();
|
||||
}
|
||||
|
||||
TEST_F(WifiLanV2Test, CanStartMultipleDiscovery) {
|
||||
env_.Start();
|
||||
WifiLanV2 wifi_lan_a;
|
||||
std::string service_id_1(kServiceID);
|
||||
std::string service_id_2("com.google.location.nearby.apps.test_1");
|
||||
|
||||
EXPECT_TRUE(
|
||||
wifi_lan_a.StartDiscovery(service_id_1, DiscoveredServiceCallback{}));
|
||||
|
||||
EXPECT_TRUE(
|
||||
wifi_lan_a.StartDiscovery(service_id_2, DiscoveredServiceCallback{}));
|
||||
EXPECT_TRUE(wifi_lan_a.StopDiscovery(service_id_1));
|
||||
EXPECT_TRUE(wifi_lan_a.StopDiscovery(service_id_2));
|
||||
env_.Stop();
|
||||
}
|
||||
|
||||
TEST_F(WifiLanV2Test, CanAdvertiseThatOtherMediumDiscover) {
|
||||
env_.Start();
|
||||
WifiLanV2 wifi_lan_a;
|
||||
WifiLanV2 wifi_lan_b;
|
||||
std::string service_id(kServiceID);
|
||||
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, DiscoveredServiceCallback{
|
||||
.service_discovered_cb =
|
||||
[&discovered_latch](NsdServiceInfo service_info,
|
||||
const std::string& service_id) {
|
||||
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);
|
||||
EXPECT_TRUE(wifi_lan_a.StartAdvertising(service_id_1, nsd_service_info));
|
||||
EXPECT_TRUE(wifi_lan_a.StartAdvertising(service_id_2, nsd_service_info));
|
||||
EXPECT_TRUE(wifi_lan_a.StopAdvertising(service_id_1));
|
||||
EXPECT_TRUE(wifi_lan_a.StopAdvertising(service_id_2));
|
||||
EXPECT_TRUE(wifi_lan_a.StartAdvertising(service_id, nsd_service_info));
|
||||
EXPECT_TRUE(discovered_latch.Await(kWaitDuration).result());
|
||||
EXPECT_TRUE(wifi_lan_a.StopAdvertising(service_id));
|
||||
EXPECT_TRUE(lost_latch.Await(kWaitDuration).result());
|
||||
EXPECT_TRUE(wifi_lan_b.StopDiscovery(service_id));
|
||||
env_.Stop();
|
||||
}
|
||||
|
||||
TEST_F(WifiLanV2Test, CanDiscoverThatOtherMediumAdvertise) {
|
||||
env_.Start();
|
||||
WifiLanV2 wifi_lan_a;
|
||||
WifiLanV2 wifi_lan_b;
|
||||
std::string service_id(kServiceID);
|
||||
std::string service_info_name(kServiceInfoName);
|
||||
std::string endpoint_info_name(kEndpointName);
|
||||
CountDownLatch discovered_latch(1);
|
||||
CountDownLatch lost_latch(1);
|
||||
|
||||
NsdServiceInfo nsd_service_info;
|
||||
nsd_service_info.SetServiceName(service_info_name);
|
||||
nsd_service_info.SetTxtRecord(std::string(kEndpointInfoKey),
|
||||
endpoint_info_name);
|
||||
wifi_lan_b.StartAdvertising(service_id, nsd_service_info);
|
||||
|
||||
EXPECT_TRUE(wifi_lan_a.StartDiscovery(
|
||||
service_id, DiscoveredServiceCallback{
|
||||
.service_discovered_cb =
|
||||
[&discovered_latch](NsdServiceInfo service_info,
|
||||
const std::string& service_id) {
|
||||
discovered_latch.CountDown();
|
||||
},
|
||||
.service_lost_cb =
|
||||
[&lost_latch](NsdServiceInfo service_info,
|
||||
const std::string& service_id) {
|
||||
lost_latch.CountDown();
|
||||
},
|
||||
}));
|
||||
EXPECT_TRUE(discovered_latch.Await(kWaitDuration).result());
|
||||
EXPECT_TRUE(wifi_lan_b.StopAdvertising(service_id));
|
||||
EXPECT_TRUE(lost_latch.Await(kWaitDuration).result());
|
||||
EXPECT_TRUE(wifi_lan_a.StopDiscovery(service_id));
|
||||
env_.Stop();
|
||||
}
|
||||
|
||||
|
||||
@@ -115,12 +115,56 @@ bool WifiLanV2::IsAdvertisingLocked(const std::string& service_id) {
|
||||
bool WifiLanV2::StartDiscovery(const std::string& service_id,
|
||||
DiscoveredServiceCallback callback) {
|
||||
MutexLock lock(&mutex_);
|
||||
return false;
|
||||
|
||||
if (service_id.empty()) {
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "Refusing to start WifiLan discovering with empty service_id.";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!IsAvailableLocked()) {
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "Can't discover WifiLan services because WifiLan isn't available.";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (IsDiscoveringLocked(service_id)) {
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "Refusing to start discovery of WifiLan services because another "
|
||||
"discovery is already in-progress.";
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string service_type = GenerateServiceType(service_id);
|
||||
bool ret = medium_.StartDiscovery(service_id, service_type, callback);
|
||||
if (!ret) {
|
||||
NEARBY_LOGS(INFO) << "Failed to start discovery of WifiLan services.";
|
||||
return false;
|
||||
}
|
||||
|
||||
NEARBY_LOGS(INFO) << "Turned on WifiLan discovering with service_id="
|
||||
<< service_id;
|
||||
// Mark the fact that we're currently performing a WifiLan discovering.
|
||||
discovering_info_.Add(service_id);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WifiLanV2::StopDiscovery(const std::string& service_id) {
|
||||
MutexLock lock(&mutex_);
|
||||
return false;
|
||||
|
||||
if (!IsDiscoveringLocked(service_id)) {
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "Can't turn off WifiLan discovering because we never started "
|
||||
"discovering.";
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string service_type = GenerateServiceType(service_id);
|
||||
NEARBY_LOGS(INFO) << "Turned off WifiLan discovering with service_id="
|
||||
<< service_id << ", service_type=" << service_type;
|
||||
bool ret = medium_.StopDiscovery(service_type);
|
||||
discovering_info_.Remove(service_id);
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool WifiLanV2::IsDiscovering(const std::string& service_id) {
|
||||
@@ -153,7 +197,7 @@ bool WifiLanV2::IsAcceptingConnectionsLocked(const std::string& service_id) {
|
||||
}
|
||||
|
||||
WifiLanSocketV2 WifiLanV2::Connect(const std::string& service_id,
|
||||
NsdServiceInfo& service_info,
|
||||
const NsdServiceInfo& service_info,
|
||||
CancellationFlag* cancellation_flag) {
|
||||
MutexLock lock(&mutex_);
|
||||
|
||||
|
||||
@@ -92,7 +92,7 @@ class WifiLanV2 {
|
||||
// Blocks until connection is established, or server-side is terminated.
|
||||
// Returns socket instance. On success, WifiLanSocket.IsValid() return true.
|
||||
WifiLanSocketV2 Connect(const std::string& service_id,
|
||||
NsdServiceInfo& service_info,
|
||||
const NsdServiceInfo& service_info,
|
||||
CancellationFlag* cancellation_flag)
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
@@ -104,6 +104,10 @@ class WifiLanV2 {
|
||||
CancellationFlag* cancellation_flag)
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
// Gets ip address + port for remote services on the network to identify and
|
||||
// connect to this service.
|
||||
//
|
||||
// Credential is for the currently-hosted Wifi ServerSocket (if any).
|
||||
std::pair<std::string, int> GetCredentials(const std::string& service_id)
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
@@ -135,7 +139,7 @@ class WifiLanV2 {
|
||||
struct DiscoveringInfo {
|
||||
bool Empty() const { return service_ids.empty(); }
|
||||
void Clear() { service_ids.clear(); }
|
||||
void Add(const std::string& service_id) { service_ids.emplace(service_id); }
|
||||
void Add(const std::string& service_id) { service_ids.insert(service_id); }
|
||||
void Remove(const std::string& service_id) {
|
||||
service_ids.erase(service_id);
|
||||
}
|
||||
@@ -187,4 +191,4 @@ class WifiLanV2 {
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
|
||||
#endif // CORE_INTERNAL_MEDIUMS_WIFI_LAN_V2_H_
|
||||
#endif // CORE_INTERNAL_MEDIUMS_WIFI_LAN_H_
|
||||
|
||||
Reference in New Issue
Block a user