Merge branch 'release' into github to roll forward to cl/343785060.

This commit is contained in:
hai007
2020-11-23 23:19:38 -08:00
27 changed files with 556 additions and 756 deletions
+25 -21
View File
@@ -34,14 +34,13 @@ bool WifiLan::IsAvailable() const {
bool WifiLan::IsAvailableLocked() const { return medium_.IsValid(); }
bool WifiLan::StartAdvertising(const std::string& service_id,
const std::string& service_info_name,
const std::string& endpoint_info_name) {
const NsdServiceInfo& nsd_service_info) {
MutexLock lock(&mutex_);
if (service_info_name.empty()) {
NEARBY_LOG(
INFO,
"Refusing to turn on WifiLan advertising. Empty service info name.");
if (!nsd_service_info.IsValid()) {
NEARBY_LOGS(INFO)
<< "Refusing to turn on WifiLan advertising. nsd_service_info is not "
"valid.";
return false;
}
@@ -51,16 +50,19 @@ bool WifiLan::StartAdvertising(const std::string& service_id,
return false;
}
if (!medium_.StartAdvertising(service_id, service_info_name,
endpoint_info_name)) {
NEARBY_LOG(
INFO, "Failed to turn on WifiLan advertising with service info name=%s",
service_info_name.c_str());
if (!medium_.StartAdvertising(service_id, nsd_service_info)) {
NEARBY_LOGS(INFO)
<< "Failed to turn on WifiLan advertising with wifi_lan_service="
<< &nsd_service_info
<< ", service_info_name=" << nsd_service_info.GetServiceInfoName()
<< ", service_id=" << service_id;
return false;
}
NEARBY_LOGS(INFO) << "Turned on WifiLan advertising with service info name="
<< service_info_name << ", service id=" << service_id;
NEARBY_LOGS(INFO) << "Turned on WifiLan advertising with wifi_lan_service="
<< &nsd_service_info << ", service_info_name="
<< nsd_service_info.GetServiceInfoName()
<< ", service_id=" << service_id;
advertising_info_.Add(service_id);
return true;
}
@@ -73,7 +75,7 @@ bool WifiLan::StopAdvertising(const std::string& service_id) {
return false;
}
NEARBY_LOG(INFO, "Turned off WifiLan advertising with service id=%s",
NEARBY_LOG(INFO, "Turned off WifiLan advertising with service_id=%s",
service_id.c_str());
bool ret = medium_.StopAdvertising(service_id);
// Reset our bundle of advertising state to mark that we're no longer
@@ -98,7 +100,7 @@ bool WifiLan::StartDiscovery(const std::string& service_id,
if (service_id.empty()) {
NEARBY_LOG(INFO,
"Refusing to start WifiLan discovering with empty service id.");
"Refusing to start WifiLan discovering with empty service_id.");
return false;
}
@@ -122,7 +124,7 @@ bool WifiLan::StartDiscovery(const std::string& service_id,
return false;
}
NEARBY_LOG(INFO, "Turned on WifiLan discovering with service id=%s",
NEARBY_LOG(INFO, "Turned on WifiLan discovering with service_id=%s",
service_id.c_str());
// Mark the fact that we're currently performing a WifiLan discovering.
discovering_info_.Add(service_id);
@@ -139,7 +141,7 @@ bool WifiLan::StopDiscovery(const std::string& service_id) {
return false;
}
NEARBY_LOG(INFO, "Turned off WifiLan discovering with service id=%s",
NEARBY_LOG(INFO, "Turned off WifiLan discovering with service_id=%s",
service_id.c_str());
bool ret = medium_.StopDiscovery(service_id);
discovering_info_.Clear();
@@ -163,7 +165,7 @@ bool WifiLan::StartAcceptingConnections(const std::string& service_id,
if (service_id.empty()) {
NEARBY_LOG(INFO,
"Refusing to start accepting WifiLan connections with empty "
"service id.");
"service_id.");
return false;
}
@@ -223,8 +225,10 @@ bool WifiLan::IsAcceptingConnectionsLocked(const std::string& service_id) {
WifiLanSocket WifiLan::Connect(WifiLanService& wifi_lan_service,
const std::string& service_id) {
MutexLock lock(&mutex_);
NEARBY_LOG(INFO, "WifiLan::Connect: service=%p, service_info_name=%s",
&wifi_lan_service, wifi_lan_service.GetServiceName().c_str());
NEARBY_LOGS(INFO) << "WifiLan::Connect: wifi_lan_service="
<< &wifi_lan_service << ", service_info_name="
<< wifi_lan_service.GetServiceInfo().GetServiceInfoName()
<< ", service_id=" << service_id;
// Socket to return. To allow for NRVO to work, it has to be a single object.
WifiLanSocket socket;
@@ -254,7 +258,7 @@ WifiLanSocket WifiLan::Connect(WifiLanService& wifi_lan_service,
WifiLanService WifiLan::GetRemoteWifiLanService(const std::string& ip_address,
int port) {
MutexLock lock(&mutex_);
return medium_.FindRemoteService(ip_address, port);
return medium_.GetRemoteService(ip_address, port);
}
std::pair<std::string, int> WifiLan::GetServiceAddress(
+3 -4
View File
@@ -37,12 +37,11 @@ class WifiLan {
// Returns true, if WifiLan communications are supported by a platform.
bool IsAvailable() const ABSL_LOCKS_EXCLUDED(mutex_);
// Sets custom service info name, endpoint info name and then enables WifiLan
// advertising.
// Sets custom service info name, endpoint info name in NsdServiceInfo and
// then enables WifiLan advertising.
// Returns true, if name is successfully set, and false otherwise.
bool StartAdvertising(const std::string& service_id,
const std::string& service_info_name,
const std::string& endpoint_info_name)
const NsdServiceInfo& nsd_service_info)
ABSL_LOCKS_EXCLUDED(mutex_);
// Disables WifiLan advertising, and restores service info name to
+16 -6
View File
@@ -34,6 +34,7 @@ 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 kEndpointInfoKey{"n"};
class WifiLanTest : public ::testing::Test {
protected:
@@ -73,8 +74,11 @@ TEST_F(WifiLanTest, CanStartAdvertising) {
},
});
EXPECT_TRUE(wifi_lan_a.StartAdvertising(service_id, service_info_name,
endpoint_info_name));
NsdServiceInfo nsd_service_info;
nsd_service_info.SetServiceInfoName(service_info_name);
nsd_service_info.SetTxtRecord(std::string(kEndpointInfoKey),
endpoint_info_name);
EXPECT_TRUE(wifi_lan_a.StartAdvertising(service_id, nsd_service_info));
EXPECT_TRUE(found_latch.Await(kWaitDuration).result());
EXPECT_TRUE(wifi_lan_a.StopAdvertising(service_id));
EXPECT_TRUE(wifi_lan_b.StopDiscovery(service_id));
@@ -91,8 +95,11 @@ TEST_F(WifiLanTest, CanStartDiscovery) {
CountDownLatch accept_latch(1);
CountDownLatch lost_latch(1);
wifi_lan_b.StartAdvertising(service_id, service_info_name,
endpoint_info_name);
NsdServiceInfo nsd_service_info;
nsd_service_info.SetServiceInfoName(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, {
@@ -124,8 +131,11 @@ TEST_F(WifiLanTest, CanStartAcceptingConnectionsAndConnect) {
CountDownLatch found_latch(1);
CountDownLatch accept_latch(1);
wifi_lan_a.StartAdvertising(service_id, service_info_name,
endpoint_info_name);
NsdServiceInfo nsd_service_info;
nsd_service_info.SetServiceInfoName(service_info_name);
nsd_service_info.SetTxtRecord(std::string(kEndpointInfoKey),
endpoint_info_name);
wifi_lan_a.StartAdvertising(service_id, nsd_service_info);
wifi_lan_a.StartAcceptingConnections(
service_id,
{
+31 -35
View File
@@ -23,6 +23,7 @@
#include "core/internal/mediums/webrtc/webrtc_socket_wrapper.h"
#include "core/internal/webrtc_endpoint_channel.h"
#include "core/internal/wifi_lan_endpoint_channel.h"
#include "platform/base/nsd_service_info.h"
#include "platform/base/types.h"
#include "platform/public/crypto.h"
#include "proto/connections_enums.pb.h"
@@ -470,9 +471,9 @@ bool P2pClusterPcpHandler::IsRecognizedWifiLanEndpoint(
}
void P2pClusterPcpHandler::WifiLanServiceDiscoveredHandler(
ClientProxy* client, WifiLanService& service,
ClientProxy* client, WifiLanService& wifi_lan_service,
const std::string& service_id) {
RunOnPcpHandlerThread([this, client, service_id, &service]() {
RunOnPcpHandlerThread([this, client, service_id, &wifi_lan_service]() {
// Make sure we are still discovering before proceeding.
if (!client->IsDiscovering()) {
NEARBY_LOG(
@@ -483,10 +484,8 @@ void P2pClusterPcpHandler::WifiLanServiceDiscoveredHandler(
return;
}
// Parse the WifiLan service name.
WifiLanServiceInfo service_info(service.GetServiceName(),
service.GetTxtRecord(std::string{
WifiLanServiceInfo::kKeyEndpointInfo}));
// Parse the WifiLanServiceInfo.
WifiLanServiceInfo service_info(wifi_lan_service.GetServiceInfo());
// Make sure the WifiLan service name points to a valid
// endpoint we're discovering.
@@ -496,7 +495,7 @@ void P2pClusterPcpHandler::WifiLanServiceDiscoveredHandler(
NEARBY_LOG(
INFO,
"Invoking BasePcpHandler::OnEndpointFound() for WifiLan "
"service=%s; id=%s; name=%s",
"service_id=%s; endpoint_id=%s; endpoint_info=%s",
service_id.c_str(), service_info.GetEndpointId().c_str(),
absl::BytesToHexString(service_info.GetEndpointInfo().data()).c_str());
OnEndpointFound(client, std::make_shared<WifiLanEndpoint>(WifiLanEndpoint{
@@ -507,22 +506,19 @@ void P2pClusterPcpHandler::WifiLanServiceDiscoveredHandler(
proto::connections::Medium::WIFI_LAN,
service_info.GetWebRtcState(),
},
service,
wifi_lan_service,
}));
});
}
void P2pClusterPcpHandler::WifiLanServiceLostHandler(
ClientProxy* client, WifiLanService& service,
ClientProxy* client, WifiLanService& wifi_lan_service,
const std::string& service_id) {
std::string service_info_name = service.GetServiceName();
std::string endpoint_info_name =
service.GetTxtRecord(std::string{WifiLanServiceInfo::kKeyEndpointInfo});
NEARBY_LOG(
INFO, "WifiLan: [LOST, SCHED] service_info_name=%s, endpoint_info_name=%",
service_info_name.c_str(), endpoint_info_name.c_str());
RunOnPcpHandlerThread([this, client, service_id, service_info_name,
endpoint_info_name]() {
NsdServiceInfo nsd_service_info = wifi_lan_service.GetServiceInfo();
NEARBY_LOG(INFO,
"WifiLan: [LOST, SCHED] wifi_lan_service=%p, service_info_name=%s",
&wifi_lan_service, nsd_service_info.GetServiceInfoName().c_str());
RunOnPcpHandlerThread([this, client, service_id, nsd_service_info]() {
// Make sure we are still discovering before proceeding.
if (!client->IsDiscovering()) {
NEARBY_LOG(
@@ -533,8 +529,8 @@ void P2pClusterPcpHandler::WifiLanServiceLostHandler(
return;
}
// Parse the WifiLan service name.
WifiLanServiceInfo service_info(service_info_name, endpoint_info_name);
// Parse the WifiLanServiceInfo.
WifiLanServiceInfo service_info(nsd_service_info);
// Make sure the WifiLan service name points to a valid
// endpoint we're discovering.
@@ -1057,7 +1053,9 @@ proto::connections::Medium P2pClusterPcpHandler::StartWifiLanAdvertising(
RunOnPcpHandlerThread([this, client, local_endpoint_info,
socket = std::move(socket)]() mutable {
std::string remote_service_info_name =
socket.GetRemoteWifiLanService().GetServiceName();
socket.GetRemoteWifiLanService()
.GetServiceInfo()
.GetServiceInfoName();
auto channel = absl::make_unique<WifiLanEndpointChannel>(
remote_service_info_name, socket);
ByteArray remote_service_info{remote_service_info_name};
@@ -1088,32 +1086,30 @@ proto::connections::Medium P2pClusterPcpHandler::StartWifiLanAdvertising(
local_endpoint_info,
ByteArray{},
web_rtc_state};
std::string service_info_name(service_info);
if (service_info_name.empty()) {
NEARBY_LOG(INFO,
"P2pClusterPcpHandler::StartWifiLanAdvertising: generate "
"WifiLanServiceInfo failed");
NsdServiceInfo nsd_service_info{service_info};
if (!nsd_service_info.IsValid()) {
NEARBY_LOGS(INFO)
<< "P2pClusterPcpHandler::StartWifiLanAdvertising: generate "
"NsdServiceInfo failed";
wifi_lan_medium_.StopAcceptingConnections(service_id);
return proto::connections::UNKNOWN_MEDIUM;
} else {
NEARBY_LOG(INFO,
"P2pClusterPcpHandler::StartWifiLanAdvertising: generate "
"WifiLanServiceInfo succeeded; service_info_name=%s",
service_info_name.c_str());
NEARBY_LOGS(INFO)
<< "P2pClusterPcpHandler::StartWifiLanAdvertising: generate "
"NsdServiceInfo succeeded; service_info_name="
<< nsd_service_info.GetServiceInfoName();
}
auto local_endpoint_info_name = service_info.GetEndpointInfoName();
NEARBY_LOG(
INFO,
"P2pClusterPcpHandler::StartWifiLanAdvertising: service=%s: come up",
service_id.c_str());
if (!wifi_lan_medium_.StartAdvertising(service_id, service_info_name,
local_endpoint_info_name)) {
if (!wifi_lan_medium_.StartAdvertising(service_id, nsd_service_info)) {
NEARBY_LOG(INFO,
"P2pClusterPcpHandler::StartWifiLanAdvertising: failed to "
"start advertising, service_info_name=%s",
service_info_name.c_str());
nsd_service_info.GetServiceInfoName().c_str());
wifi_lan_medium_.StopAcceptingConnections(service_id);
return proto::connections::UNKNOWN_MEDIUM;
}
@@ -1137,10 +1133,10 @@ proto::connections::Medium P2pClusterPcpHandler::StartWifiLanDiscovery(
BasePcpHandler::ConnectImplResult P2pClusterPcpHandler::WifiLanConnectImpl(
ClientProxy* client, WifiLanEndpoint* endpoint) {
WifiLanService& service = endpoint->wifi_lan_service;
WifiLanService& wifi_lan_service = endpoint->wifi_lan_service;
WifiLanSocket wifi_lan_socket =
wifi_lan_medium_.Connect(service, endpoint->service_id);
wifi_lan_medium_.Connect(wifi_lan_service, endpoint->service_id);
if (!wifi_lan_socket.IsValid()) {
return BasePcpHandler::ConnectImplResult{
.status = {Status::kWifiLanError},
@@ -48,6 +48,7 @@ const std::size_t kMaxEndpointInfoLength = 131u;
ServiceControllerRouter::~ServiceControllerRouter() {
NEARBY_LOG(INFO, "ServiceControllerRouter going down.");
service_controller_.reset();
// And make sure that cleanup is the last thing we do.
serializer_.Shutdown();
}
@@ -404,8 +405,8 @@ void ServiceControllerRouter::ReleaseServiceControllerForClient(
ClientProxy* client) {
clients_.erase(client);
// service_controller_ won't be released here. Instead, in desctructor.
if (clients_.empty()) {
service_controller_.reset();
current_strategy_ = Strategy{};
}
}
+22 -17
View File
@@ -63,18 +63,22 @@ WifiLanServiceInfo::WifiLanServiceInfo(Version version, Pcp pcp,
web_rtc_state_ = web_rtc_state;
}
WifiLanServiceInfo::WifiLanServiceInfo(absl::string_view service_info_name,
absl::string_view endpoint_info_name) {
ByteArray service_info_bytes = Base64Utils::Decode(service_info_name);
endpoint_info_ = Base64Utils::Decode(endpoint_info_name);
if (endpoint_info_.size() > kMaxEndpointInfoLength) {
NEARBY_LOG(INFO,
"Cannot deserialize EndpointInfo: expecting endpoint info "
"max %d raw bytes, got %" PRIu64,
kMaxEndpointInfoLength, endpoint_info_.size());
return;
WifiLanServiceInfo::WifiLanServiceInfo(const NsdServiceInfo& nsd_service_info) {
auto txt_endpoint_info_name =
nsd_service_info.GetTxtRecord(std::string(kKeyEndpointInfo));
if (!txt_endpoint_info_name.empty()) {
endpoint_info_ = Base64Utils::Decode(txt_endpoint_info_name);
if (endpoint_info_.size() > kMaxEndpointInfoLength) {
NEARBY_LOG(INFO,
"Cannot deserialize EndpointInfo: expecting endpoint info "
"max %d raw bytes, got %" PRIu64,
kMaxEndpointInfoLength, endpoint_info_.size());
return;
}
}
auto service_info_name = nsd_service_info.GetServiceInfoName();
ByteArray service_info_bytes = Base64Utils::Decode(service_info_name);
if (service_info_bytes.Empty()) {
NEARBY_LOG(
INFO,
@@ -153,9 +157,9 @@ WifiLanServiceInfo::WifiLanServiceInfo(absl::string_view service_info_name,
}
}
WifiLanServiceInfo::operator std::string() const {
WifiLanServiceInfo::operator NsdServiceInfo() const {
if (!IsValid()) {
return "";
return {};
}
// The upper 3 bits are the Version.
@@ -188,11 +192,12 @@ WifiLanServiceInfo::operator std::string() const {
absl::StrAppend(&out, std::string(1, field_byte));
}
return Base64Utils::Encode(ByteArray{std::move(out)});
}
std::string WifiLanServiceInfo::GetEndpointInfoName() const {
return Base64Utils::Encode(endpoint_info_);
NsdServiceInfo nsd_service_info;
nsd_service_info.SetServiceInfoName(
Base64Utils::Encode(ByteArray{std::move(out)}));
nsd_service_info.SetTxtRecord(std::string(kKeyEndpointInfo),
Base64Utils::Encode(endpoint_info_));
return nsd_service_info;
}
} // namespace connections
+4 -11
View File
@@ -20,6 +20,7 @@
#include "core/internal/base_pcp_handler.h"
#include "core/internal/pcp.h"
#include "platform/base/byte_array.h"
#include "platform/base/nsd_service_info.h"
#include "absl/strings/string_view.h"
namespace location {
@@ -50,23 +51,15 @@ class WifiLanServiceInfo {
const ByteArray& uwb_address,
WebRtcState web_rtc_state);
// Constructs WifiLanService through packed string of WifiLanServiceInfo and
// EndpointInfo.
//
// service_info_name - A packed string of |WifiLanServiceInfo|. It does
// not include endpoint_info which should be stored
// in next param bleow.
// endpoint_info_name - The endpoint info packed string.
WifiLanServiceInfo(absl::string_view service_info_name,
absl::string_view endpoint_info_name);
// Constructs WifiLanServiceInfo through NsdServiceInfo.
explicit WifiLanServiceInfo(const NsdServiceInfo& nsd_service_info);
WifiLanServiceInfo(const WifiLanServiceInfo&) = default;
WifiLanServiceInfo& operator=(const WifiLanServiceInfo&) = default;
WifiLanServiceInfo(WifiLanServiceInfo&&) = default;
WifiLanServiceInfo& operator=(WifiLanServiceInfo&&) = default;
~WifiLanServiceInfo() = default;
explicit operator std::string() const;
std::string GetEndpointInfoName() const;
explicit operator NsdServiceInfo() const;
bool IsValid() const { return !endpoint_id_.empty(); }
Version GetVersion() const { return version_; }
+11 -11
View File
@@ -18,6 +18,7 @@
#include <memory>
#include "platform/base/base64_utils.h"
#include "platform/base/nsd_service_info.h"
#include "gtest/gtest.h"
namespace location {
@@ -63,11 +64,9 @@ TEST(WifiLanServiceInfoTest, ConstructionFromSerializedStringWorks) {
endpoint_info,
ByteArray{},
kWebRtcState};
std::string wifi_lan_service_info_string{org_wifi_lan_service_info};
auto endpoint_info_name = org_wifi_lan_service_info.GetEndpointInfoName();
NsdServiceInfo nsd_service_info{org_wifi_lan_service_info};
WifiLanServiceInfo wifi_lan_service_info{wifi_lan_service_info_string,
endpoint_info_name};
WifiLanServiceInfo wifi_lan_service_info{nsd_service_info};
EXPECT_TRUE(wifi_lan_service_info.IsValid());
EXPECT_EQ(kPcp, wifi_lan_service_info.GetPcp());
@@ -174,14 +173,15 @@ TEST(WifiLanServiceInfoTest, ConstructionFailsWithLongServiceIdHash) {
EXPECT_FALSE(wifi_lan_service_info.IsValid());
}
TEST(WifiLanServiceInfoTest, ConstructionFailsWithShortStringLength) {
char wifi_lan_service_info_string[] = {'X', '\0'};
ByteArray endpoint_info{std::string(kEndPointName)};
TEST(WifiLanServiceInfoTest, ConstructionFailsWithShortServiceNameLength) {
char wifi_lan_service_info_name[] = {'X', '\0'};
ByteArray wifi_lan_service_info_bytes{wifi_lan_service_info_name};
ByteArray wifi_lan_service_info_bytes{wifi_lan_service_info_string};
WifiLanServiceInfo wifi_lan_service_info{
Base64Utils::Encode(wifi_lan_service_info_bytes),
Base64Utils::Encode(endpoint_info)};
NsdServiceInfo nsd_service_info;
nsd_service_info.SetServiceInfoName(
Base64Utils::Encode(wifi_lan_service_info_bytes));
WifiLanServiceInfo wifi_lan_service_info{nsd_service_info};
EXPECT_FALSE(wifi_lan_service_info.IsValid());
}