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
+1
View File
@@ -95,6 +95,7 @@ cc_test(
"lost_entity_tracker_test.cc",
"uuid_test.cc",
"wifi_lan_test.cc",
"wifi_lan_test_v2.cc",
],
shard_count = 16,
deps = [
@@ -0,0 +1,104 @@
// 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 <string>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "absl/strings/string_view.h"
#include "core/internal/mediums/wifi_lan_v2.h"
#include "platform/base/medium_environment.h"
#include "platform/public/logging.h"
#include "platform/public/wifi_lan_v2.h"
namespace location {
namespace nearby {
namespace connections {
namespace {
using FeatureFlags = FeatureFlags::Flags;
constexpr FeatureFlags kTestCases[] = {
FeatureFlags{
.enable_cancellation_flag = true,
},
FeatureFlags{
.enable_cancellation_flag = false,
},
};
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 WifiLanV2Test : public ::testing::TestWithParam<FeatureFlags> {
protected:
WifiLanV2Test() { env_.Stop(); }
MediumEnvironment& env_{MediumEnvironment::Instance()};
};
TEST_F(WifiLanV2Test, CanConstructValidObject) {
env_.Start();
WifiLanV2 wifi_lan_a;
WifiLanV2 wifi_lan_b;
std::string service_id(kServiceID);
EXPECT_TRUE(wifi_lan_a.IsAvailable());
EXPECT_TRUE(wifi_lan_b.IsAvailable());
env_.Stop();
}
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};
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, nsd_service_info));
EXPECT_TRUE(wifi_lan_a.StopAdvertising(service_id));
env_.Stop();
}
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};
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));
env_.Stop();
}
} // namespace
} // namespace connections
} // namespace nearby
} // namespace location
+57 -2
View File
@@ -36,6 +36,7 @@ WifiLanV2::~WifiLanV2() {
bool WifiLanV2::IsAvailable() const {
MutexLock lock(&mutex_);
return IsAvailableLocked();
}
@@ -43,13 +44,67 @@ bool WifiLanV2::IsAvailableLocked() const { return medium_.IsValid(); }
bool WifiLanV2::StartAdvertising(const std::string& service_id,
NsdServiceInfo& nsd_service_info) {
return false;
MutexLock lock(&mutex_);
if (!nsd_service_info.IsValid()) {
NEARBY_LOGS(INFO)
<< "Refusing to turn on WifiLan advertising. nsd_service_info is not "
"valid.";
return false;
}
if (IsAdvertisingLocked(service_id)) {
NEARBY_LOGS(INFO)
<< "Failed to WifiLan advertise because we're already advertising.";
return false;
}
if (!IsAvailableLocked()) {
NEARBY_LOGS(INFO)
<< "Can't turn on WifiLan advertising. WifiLan is not available.";
return false;
}
nsd_service_info.SetServiceType(GenerateServiceType(service_id));
if (!medium_.StartAdvertising(nsd_service_info)) {
NEARBY_LOGS(INFO)
<< "Failed to turn on WifiLan advertising with nsd_service_info="
<< &nsd_service_info
<< ", service_name=" << nsd_service_info.GetServiceName()
<< ", service_id=" << service_id;
return false;
}
NEARBY_LOGS(INFO) << "Turned on WifiLan advertising with nsd_service_info="
<< &nsd_service_info
<< ", service_name=" << nsd_service_info.GetServiceName()
<< ", service_id=" << service_id;
advertising_info_.Add(service_id, std::move(nsd_service_info));
return true;
}
bool WifiLanV2::StopAdvertising(const std::string& service_id) { return false; }
bool WifiLanV2::StopAdvertising(const std::string& service_id) {
MutexLock lock(&mutex_);
if (!IsAdvertisingLocked(service_id)) {
NEARBY_LOGS(INFO)
<< "Can't turn off WifiLan advertising; it is already off";
return false;
}
NEARBY_LOGS(INFO) << "Turned off WifiLan advertising with service_id="
<< service_id;
bool ret =
medium_.StopAdvertising(*advertising_info_.GetServiceInfo(service_id));
// Reset our bundle of advertising state to mark that we're no longer
// advertising for specific service_id.
advertising_info_.Remove(service_id);
return ret;
}
bool WifiLanV2::IsAdvertising(const std::string& service_id) {
MutexLock lock(&mutex_);
return IsAdvertisingLocked(service_id);
}
+139 -12
View File
@@ -25,6 +25,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 "core/internal/wifi_lan_endpoint_channel_v2.h"
#include "platform/base/nsd_service_info.h"
#include "platform/base/types.h"
#include "platform/public/crypto.h"
@@ -59,6 +60,7 @@ P2pClusterPcpHandler::P2pClusterPcpHandler(
bluetooth_medium_(mediums->GetBluetoothClassic()),
ble_medium_(mediums->GetBle()),
wifi_lan_medium_(mediums->GetWifiLan()),
wifi_lan_medium_v2_(mediums->GetWifiLanV2()),
webrtc_medium_(mediums->GetWebRtc()),
injected_bluetooth_device_store_(injected_bluetooth_device_store) {}
@@ -68,6 +70,9 @@ P2pClusterPcpHandler::P2pClusterPcpHandler(
std::vector<proto::connections::Medium>
P2pClusterPcpHandler::GetConnectionMediumsByPriority() {
std::vector<proto::connections::Medium> mediums;
if (wifi_lan_medium_v2_.IsAvailable()) {
mediums.push_back(proto::connections::WIFI_LAN);
}
if (wifi_lan_medium_.IsAvailable()) {
mediums.push_back(proto::connections::WIFI_LAN);
}
@@ -96,11 +101,20 @@ BasePcpHandler::StartOperationResult P2pClusterPcpHandler::StartAdvertisingImpl(
WebRtcState web_rtc_state{WebRtcState::kUnconnectable};
if (options.allowed.wifi_lan) {
const ByteArray wifi_lan_hash =
GenerateHash(service_id, WifiLanServiceInfo::kServiceIdHashLength);
proto::connections::Medium wifi_lan_medium = StartWifiLanAdvertising(
client, service_id, wifi_lan_hash, local_endpoint_id,
local_endpoint_info, web_rtc_state);
proto::connections::Medium wifi_lan_medium =
StartWifiLanV2Advertising(client, service_id, local_endpoint_id,
local_endpoint_info, web_rtc_state);
if (wifi_lan_medium != proto::connections::UNKNOWN_MEDIUM) {
NEARBY_LOGS(INFO)
<< "P2pClusterPcpHandler::StartAdvertisingImpl: WifiLan added";
mediums_started_successfully.push_back(wifi_lan_medium);
}
}
if (options.allowed.wifi_lan) {
proto::connections::Medium wifi_lan_medium =
StartWifiLanAdvertising(client, service_id, local_endpoint_id,
local_endpoint_info, web_rtc_state);
if (wifi_lan_medium != proto::connections::UNKNOWN_MEDIUM) {
NEARBY_LOG(INFO,
"P2pClusterPcpHandler::StartAdvertisingImpl: WifiLan added");
@@ -170,6 +184,10 @@ Status P2pClusterPcpHandler::StopAdvertisingImpl(ClientProxy* client) {
wifi_lan_medium_.StopAdvertising(client->GetAdvertisingServiceId());
wifi_lan_medium_.StopAcceptingConnections(client->GetAdvertisingServiceId());
wifi_lan_medium_v2_.StopAdvertising(client->GetAdvertisingServiceId());
wifi_lan_medium_v2_.StopAcceptingConnections(
client->GetAdvertisingServiceId());
return {Status::kSuccess};
}
@@ -903,8 +921,8 @@ proto::connections::Medium P2pClusterPcpHandler::StartBluetoothAdvertising(
NEARBY_LOGS(INFO) << "In StartBluetoothAdvertising("
<< absl::BytesToHexString(local_endpoint_info.data())
<< "), client=" << client->GetClientId()
<< " generated BluetoothDeviceName %s with service_id="
<< service_id;
<< " generated BluetoothDeviceName " << device_name
<< " with service_id=" << service_id;
// Become Bluetooth discoverable.
if (!bluetooth_medium_.TurnOnDiscoverability(device_name)) {
@@ -1206,8 +1224,8 @@ BasePcpHandler::ConnectImplResult P2pClusterPcpHandler::BleConnectImpl(
proto::connections::Medium P2pClusterPcpHandler::StartWifiLanAdvertising(
ClientProxy* client, const std::string& service_id,
const ByteArray& service_id_hash, const std::string& local_endpoint_id,
const ByteArray& local_endpoint_info, WebRtcState web_rtc_state) {
const std::string& local_endpoint_id, const ByteArray& local_endpoint_info,
WebRtcState web_rtc_state) {
// Start listening for connections before advertising in case a connection
// request comes in very quickly.
NEARBY_LOG(INFO,
@@ -1254,13 +1272,17 @@ proto::connections::Medium P2pClusterPcpHandler::StartWifiLanAdvertising(
return proto::connections::UNKNOWN_MEDIUM;
}
NEARBY_LOGS(INFO)
<< "In StartWifiLanAdvertising(%s), client=" << client->GetClientId()
<< "In StartWifiLanAdvertising("
<< absl::BytesToHexString(local_endpoint_info.data())
<< "), client=" << client->GetClientId()
<< " started listening for incoming WifiLan connections to service_id="
<< service_id;
}
// Generate a WifiLanServiceInfo with which to become WifiLan discoverable.
// TODO(b/169550050): Implement UWBAddress.
const ByteArray service_id_hash =
GenerateHash(service_id, WifiLanServiceInfo::kServiceIdHashLength);
WifiLanServiceInfo service_info{kWifiLanServiceInfoVersion,
GetPcp(),
local_endpoint_id,
@@ -1285,8 +1307,10 @@ proto::connections::Medium P2pClusterPcpHandler::StartWifiLanAdvertising(
wifi_lan_medium_.StopAcceptingConnections(service_id);
return proto::connections::UNKNOWN_MEDIUM;
}
NEARBY_LOGS(INFO) << "In StartWifiLanAdvertising(%s), client="
<< client->GetClientId() << " generated WifiLanServiceInfo "
NEARBY_LOGS(INFO) << "In StartWifiLanAdvertising("
<< absl::BytesToHexString(local_endpoint_info.data())
<< "), client=" << client->GetClientId()
<< " generated WifiLanServiceInfo "
<< nsd_service_info.GetServiceName()
<< " with service_id=" << service_id;
@@ -1357,6 +1381,109 @@ BasePcpHandler::ConnectImplResult P2pClusterPcpHandler::WifiLanConnectImpl(
};
}
proto::connections::Medium P2pClusterPcpHandler::StartWifiLanV2Advertising(
ClientProxy* client, const std::string& service_id,
const std::string& local_endpoint_id, const ByteArray& local_endpoint_info,
WebRtcState web_rtc_state) {
// Start listening for connections before advertising in case a connection
// request comes in very quickly.
NEARBY_LOGS(INFO) << "P2pClusterPcpHandler::StartWifiLanAdvertising: service="
<< service_id << ": start";
if (!wifi_lan_medium_v2_.IsAcceptingConnections(service_id)) {
if (!wifi_lan_medium_v2_.StartAcceptingConnections(
service_id, {.accepted_cb = [this, client, local_endpoint_info](
WifiLanSocketV2 socket) {
if (!socket.IsValid()) {
NEARBY_LOGS(WARNING)
<< "Invalid socket in accept callback("
<< absl::BytesToHexString(local_endpoint_info.data())
<< "), client=" << client->GetClientId();
return;
}
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};
OnIncomingConnection(client, remote_service_info,
std::move(channel),
proto::connections::Medium::WIFI_LAN);
});
}})) {
NEARBY_LOGS(WARNING)
<< "In StartWifiLanAdvertising("
<< absl::BytesToHexString(local_endpoint_info.data())
<< "), client=" << client->GetClientId()
<< " failed to start listening for incoming WifiLan connections "
"to service_id="
<< service_id;
return proto::connections::UNKNOWN_MEDIUM;
}
NEARBY_LOGS(INFO) << "In StartWifiLanAdvertising("
<< absl::BytesToHexString(local_endpoint_info.data())
<< "), client=" << client->GetClientId()
<< " started listening for incoming WifiLan connections "
"to service_id = "
<< service_id;
}
// Generate a WifiLanServiceInfo with which to become WifiLan discoverable.
// TODO(b/169550050): Implement UWBAddress.
const ByteArray service_id_hash =
GenerateHash(service_id, WifiLanServiceInfo::kServiceIdHashLength);
WifiLanServiceInfo service_info{kWifiLanServiceInfoVersion,
GetPcp(),
local_endpoint_id,
service_id_hash,
local_endpoint_info,
ByteArray{},
web_rtc_state};
NsdServiceInfo nsd_service_info(service_info);
if (!nsd_service_info.IsValid()) {
NEARBY_LOGS(WARNING) << "In StartWifiLanAdvertising("
<< absl::BytesToHexString(local_endpoint_info.data())
<< "), client=" << client->GetClientId()
<< " failed to generate WifiLanServiceInfo {version="
<< static_cast<int>(kWifiLanServiceInfoVersion)
<< ", pcp=" << PcpToStrategy(GetPcp()).GetName()
<< ", endpoint_id=" << local_endpoint_id
<< ", service_id_hash="
<< absl::BytesToHexString(service_id_hash.data())
<< ", endpoint_info="
<< absl::BytesToHexString(local_endpoint_info.data())
<< "}.";
wifi_lan_medium_v2_.StopAcceptingConnections(service_id);
return proto::connections::UNKNOWN_MEDIUM;
}
NEARBY_LOGS(INFO) << "In StartWifiLanAdvertising("
<< absl::BytesToHexString(local_endpoint_info.data())
<< "), client=" << client->GetClientId()
<< " generated WifiLanServiceInfo "
<< nsd_service_info.GetServiceName()
<< " with service_id=" << service_id;
if (!wifi_lan_medium_v2_.StartAdvertising(service_id, nsd_service_info)) {
NEARBY_LOGS(INFO) << "In StartWifiLanAdvertising("
<< absl::BytesToHexString(local_endpoint_info.data())
<< "), client=" << client->GetClientId()
<< " couldn't advertise with WifiLanServiceInfo "
<< nsd_service_info.GetServiceName();
wifi_lan_medium_v2_.StopAcceptingConnections(service_id);
return proto::connections::UNKNOWN_MEDIUM;
}
NEARBY_LOGS(INFO) << "In StartWifiLanAdvertising("
<< absl::BytesToHexString(local_endpoint_info.data())
<< "), client=" << client->GetClientId()
<< " advertised with WifiLanServiceInfo "
<< nsd_service_info.GetServiceName();
return proto::connections::WIFI_LAN;
}
} // namespace connections
} // namespace nearby
} // namespace location
+8 -1
View File
@@ -180,7 +180,7 @@ class P2pClusterPcpHandler : public BasePcpHandler {
const std::string& service_id);
proto::connections::Medium StartWifiLanAdvertising(
ClientProxy* client, const std::string& service_id,
const ByteArray& service_id_hash, const std::string& local_endpoint_id,
const std::string& local_endpoint_id,
const ByteArray& local_endpoint_info, WebRtcState web_rtc_state);
proto::connections::Medium StartWifiLanDiscovery(
WifiLanDiscoveredServiceCallback callback, ClientProxy* client,
@@ -188,10 +188,17 @@ class P2pClusterPcpHandler : public BasePcpHandler {
BasePcpHandler::ConnectImplResult WifiLanConnectImpl(
ClientProxy* client, WifiLanEndpoint* endpoint);
// WifiLanV2
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);
BluetoothRadio& bluetooth_radio_;
BluetoothClassic& bluetooth_medium_;
Ble& ble_medium_;
WifiLan& wifi_lan_medium_;
WifiLanV2& wifi_lan_medium_v2_;
mediums::WebRtc& webrtc_medium_;
InjectedBluetoothDeviceStore& injected_bluetooth_device_store_;
std::int64_t bluetooth_classic_discoverer_client_id_{0};