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
+8
View File
@@ -206,6 +206,14 @@ class BasePcpHandler : public PcpHandler,
WifiLanService wifi_lan_service;
};
struct WifiLanV2Endpoint : public DiscoveredEndpoint {
WifiLanV2Endpoint(DiscoveredEndpoint endpoint,
const NsdServiceInfo& service_info)
: DiscoveredEndpoint(std::move(endpoint)), service_info(service_info) {}
NsdServiceInfo service_info;
};
struct WebRtcEndpoint : public DiscoveredEndpoint {
WebRtcEndpoint(DiscoveredEndpoint endpoint, mediums::PeerId peer_id)
: DiscoveredEndpoint(std::move(endpoint)),
+118 -12
View File
@@ -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();
}
+47 -3
View File
@@ -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_);
+7 -3
View File
@@ -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_
@@ -38,8 +38,8 @@ constexpr absl::string_view kServiceId = "service-id";
constexpr absl::string_view kDeviceA = "device-a";
constexpr absl::string_view kDeviceB = "device-b";
constexpr absl::string_view kMessage = "message";
constexpr absl::Duration kProgressTimeout = absl::Milliseconds(1000);
constexpr absl::Duration kDefaultTimeout = absl::Milliseconds(1000);
constexpr absl::Duration kProgressTimeout = absl::Milliseconds(1500);
constexpr absl::Duration kDefaultTimeout = absl::Milliseconds(1500);
constexpr absl::Duration kDisconnectTimeout = absl::Milliseconds(15000);
constexpr BooleanMediumSelector kTestCases[] = {
+169 -10
View File
@@ -674,6 +674,128 @@ void P2pClusterPcpHandler::WifiLanServiceLostHandler(
});
}
bool P2pClusterPcpHandler::IsRecognizedWifiLanV2Endpoint(
const std::string& service_id,
const WifiLanServiceInfo& wifi_lan_service_info) const {
if (!wifi_lan_service_info.IsValid()) {
NEARBY_LOGS(INFO)
<< "WifiLanServiceInfo doesn't conform to the format, discarding.";
return false;
}
if (wifi_lan_service_info.GetPcp() != GetPcp()) {
NEARBY_LOGS(INFO)
<< "WifiLanServiceInfo doesn't match on Pcp; expected "
<< PcpToStrategy(GetPcp()).GetName() << ", found "
<< PcpToStrategy(wifi_lan_service_info.GetPcp()).GetName();
return false;
}
ByteArray expected_service_id_hash =
GenerateHash(service_id, WifiLanServiceInfo::kServiceIdHashLength);
if (wifi_lan_service_info.GetServiceIdHash() != expected_service_id_hash) {
NEARBY_LOGS(INFO)
<< "WifiLanServiceInfo doesn't match on expected service_id_hash; "
"expected "
<< absl::BytesToHexString(expected_service_id_hash.data()) << ", found "
<< absl::BytesToHexString(
wifi_lan_service_info.GetServiceIdHash().data());
return false;
}
return true;
}
void P2pClusterPcpHandler::WifiLanV2ServiceDiscoveredHandler(
ClientProxy* client, NsdServiceInfo service_info,
const std::string& service_id) {
RunOnPcpHandlerThread(
"p2p-wifi-service-discovered",
[this, client, service_id, service_info]() RUN_ON_PCP_HANDLER_THREAD() {
// Make sure we are still discovering before proceeding.
if (!client->IsDiscovering()) {
NEARBY_LOGS(WARNING) << "Skipping discovery of NsdServiceInfo "
<< service_info.GetServiceName()
<< " because we are no longer discovering.";
return;
}
// Parse the WifiLanServiceInfo.
WifiLanServiceInfo wifi_lan_service_info(service_info);
// Make sure the WifiLan service name points to a valid
// endpoint we're discovering.
if (!IsRecognizedWifiLanV2Endpoint(service_id, wifi_lan_service_info)) {
return;
}
// Report the discovered endpoint to the client.
NEARBY_LOGS(INFO) << "Found NsdServiceInfo "
<< service_info.GetServiceName()
<< " (with endpoint_id="
<< wifi_lan_service_info.GetEndpointId()
<< "and endpoint_info="
<< absl::BytesToHexString(
wifi_lan_service_info.GetEndpointInfo().data())
<< ").";
OnEndpointFound(client,
std::make_shared<WifiLanV2Endpoint>(WifiLanV2Endpoint{
{
wifi_lan_service_info.GetEndpointId(),
wifi_lan_service_info.GetEndpointInfo(),
service_id,
proto::connections::Medium::WIFI_LAN,
wifi_lan_service_info.GetWebRtcState(),
},
service_info,
}));
});
}
void P2pClusterPcpHandler::WifiLanV2ServiceLostHandler(
ClientProxy* client, NsdServiceInfo service_info,
const std::string& service_id) {
NEARBY_LOGS(INFO) << "WifiLan: [LOST, SCHED] service_info=" << &service_info
<< ", service_name=" << service_info.GetServiceName();
RunOnPcpHandlerThread(
"p2p-wifi-service-lost",
[this, client, service_id, service_info]() RUN_ON_PCP_HANDLER_THREAD() {
// Make sure we are still discovering before proceeding.
if (!client->IsDiscovering()) {
NEARBY_LOGS(WARNING) << "Ignoring lost NsdServiceInfo "
<< service_info.GetServiceName()
<< " because we are no longer "
"discovering.";
return;
}
// Parse the WifiLanServiceInfo.
WifiLanServiceInfo wifi_lan_service_info(service_info);
// Make sure the WifiLan service name points to a valid
// endpoint we're discovering.
if (!IsRecognizedWifiLanV2Endpoint(service_id, wifi_lan_service_info))
return;
// Report the lost endpoint to the client.
NEARBY_LOGS(INFO) << "Lost NsdServiceInfo "
<< service_info.GetServiceName()
<< " (with endpoint_id="
<< wifi_lan_service_info.GetEndpointId()
<< " and endpoint_info="
<< absl::BytesToHexString(
wifi_lan_service_info.GetEndpointInfo().data())
<< ").";
OnEndpointLost(client, DiscoveredEndpoint{
wifi_lan_service_info.GetEndpointId(),
wifi_lan_service_info.GetEndpointInfo(),
service_id,
proto::connections::Medium::WIFI_LAN,
WebRtcState::kUndefined,
});
});
}
BasePcpHandler::StartOperationResult P2pClusterPcpHandler::StartDiscoveryImpl(
ClientProxy* client, const std::string& service_id,
const ConnectionOptions& options) {
@@ -686,6 +808,24 @@ BasePcpHandler::StartOperationResult P2pClusterPcpHandler::StartDiscoveryImpl(
std::vector<proto::connections::Medium> mediums_started_successfully;
if (options.allowed.wifi_lan) {
proto::connections::Medium wifi_lan_medium = StartWifiLanV2Discovery(
{
.service_discovered_cb = absl::bind_front(
&P2pClusterPcpHandler::WifiLanV2ServiceDiscoveredHandler, this,
client),
.service_lost_cb = absl::bind_front(
&P2pClusterPcpHandler::WifiLanV2ServiceLostHandler, this,
client),
},
client, service_id);
if (wifi_lan_medium != proto::connections::UNKNOWN_MEDIUM) {
NEARBY_LOGS(INFO)
<< "P2pClusterPcpHandler::StartDiscoveryImpl: WifiLan added";
mediums_started_successfully.push_back(wifi_lan_medium);
}
}
if (options.allowed.wifi_lan) {
proto::connections::Medium wifi_lan_medium = StartWifiLanDiscovery(
{
@@ -1403,17 +1543,18 @@ proto::connections::Medium P2pClusterPcpHandler::StartWifiLanV2Advertising(
RunOnPcpHandlerThread(
"p2p-wifi-on-incoming-connection",
[this, client, local_endpoint_info,
socket = std::move(
socket)]() RUN_ON_PCP_HANDLER_THREAD() mutable {
std::string remote_service_info_name;
auto channel = absl::make_unique<WifiLanEndpointChannelV2>(
remote_service_info_name, socket);
ByteArray remote_service_info{remote_service_info_name};
socket = std::move(socket)]()
RUN_ON_PCP_HANDLER_THREAD() mutable {
std::string remote_service_info_name;
auto channel =
absl::make_unique<WifiLanEndpointChannelV2>(
remote_service_info_name, socket);
ByteArray remote_service_info{remote_service_info_name};
OnIncomingConnection(client, remote_service_info,
std::move(channel),
proto::connections::Medium::WIFI_LAN);
});
OnIncomingConnection(
client, remote_service_info, std::move(channel),
proto::connections::Medium::WIFI_LAN);
});
}})) {
NEARBY_LOGS(WARNING)
<< "In StartWifiLanAdvertising("
@@ -1484,6 +1625,24 @@ proto::connections::Medium P2pClusterPcpHandler::StartWifiLanV2Advertising(
return proto::connections::WIFI_LAN;
}
proto::connections::Medium P2pClusterPcpHandler::StartWifiLanV2Discovery(
WifiLanV2DiscoveredServiceCallback callback, ClientProxy* client,
const std::string& service_id) {
if (wifi_lan_medium_v2_.StartDiscovery(service_id, std::move(callback))) {
NEARBY_LOGS(INFO) << "In StartWifiLanDiscovery(), client="
<< client->GetClientId()
<< " started scanning for Wifi devices for service_id="
<< service_id;
return proto::connections::WIFI_LAN;
} else {
NEARBY_LOGS(INFO) << "In StartWifiLanDiscovery(), client="
<< client->GetClientId()
<< " couldn't start scanning on Wifi for service_id="
<< service_id;
return proto::connections::UNKNOWN_MEDIUM;
}
}
} // namespace connections
} // namespace nearby
} // namespace location
@@ -109,6 +109,8 @@ class P2pClusterPcpHandler : public BasePcpHandler {
BluetoothClassic::DiscoveredDeviceCallback;
using BleDiscoveredPeripheralCallback = Ble::DiscoveredPeripheralCallback;
using WifiLanDiscoveredServiceCallback = WifiLan::DiscoveredServiceCallback;
using WifiLanV2DiscoveredServiceCallback =
WifiLanV2::DiscoveredServiceCallback;
static constexpr BluetoothDeviceName::Version kBluetoothDeviceNameVersion =
BluetoothDeviceName::Version::kV1;
@@ -189,10 +191,22 @@ class P2pClusterPcpHandler : public BasePcpHandler {
ClientProxy* client, WifiLanEndpoint* endpoint);
// WifiLanV2
bool IsRecognizedWifiLanV2Endpoint(
const std::string& service_id,
const WifiLanServiceInfo& wifi_lan_service_info) const;
void WifiLanV2ServiceDiscoveredHandler(ClientProxy* client,
NsdServiceInfo service_info,
const std::string& service_id);
void WifiLanV2ServiceLostHandler(ClientProxy* client,
NsdServiceInfo service_info,
const std::string& service_id);
proto::connections::Medium StartWifiLanV2Advertising(
ClientProxy* client, const std::string& service_id,
const std::string& local_endpoint_id,
const ByteArray& local_endpoint_info, WebRtcState web_rtc_state);
proto::connections::Medium StartWifiLanV2Discovery(
WifiLanV2DiscoveredServiceCallback callback, ClientProxy* client,
const std::string& service_id);
BluetoothRadio& bluetooth_radio_;
BluetoothClassic& bluetooth_medium_;