nearbyconnections : Adds accessor of ServiceType for NsdServiceInfo.

PiperOrigin-RevId: 404003948
This commit is contained in:
edwinwu
2021-10-18 10:55:45 -07:00
committed by Copybara-Service
parent b6ea14f741
commit 296274bc48
19 changed files with 350 additions and 294 deletions
+1
View File
@@ -47,6 +47,7 @@ cc_library(
"//absl/functional:bind_front",
"//absl/numeric:int128",
"//absl/strings",
"//absl/strings:str_format",
"//absl/time",
"//core:core_types",
"//core/internal/mediums/ble_v2",
+22 -6
View File
@@ -18,6 +18,8 @@
#include <string>
#include <utility>
#include "absl/strings/str_format.h"
#include "core/internal/mediums/utils.h"
#include "platform/public/logging.h"
#include "platform/public/mutex_lock.h"
@@ -34,7 +36,7 @@ bool WifiLan::IsAvailable() const {
bool WifiLan::IsAvailableLocked() const { return medium_.IsValid(); }
bool WifiLan::StartAdvertising(const std::string& service_id,
const NsdServiceInfo& nsd_service_info) {
NsdServiceInfo& nsd_service_info) {
MutexLock lock(&mutex_);
if (!nsd_service_info.IsValid()) {
@@ -56,18 +58,19 @@ bool WifiLan::StartAdvertising(const std::string& service_id,
return false;
}
nsd_service_info.SetServiceType(GenerateServiceType(service_id));
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_info_name=" << nsd_service_info.GetServiceName()
<< ", service_id=" << service_id;
return false;
}
NEARBY_LOGS(INFO) << "Turned on WifiLan advertising with wifi_lan_service="
<< &nsd_service_info << ", service_info_name="
<< nsd_service_info.GetServiceInfoName()
<< nsd_service_info.GetServiceName()
<< ", service_id=" << service_id;
advertising_info_.Add(service_id);
return true;
@@ -234,7 +237,7 @@ WifiLanSocket WifiLan::Connect(WifiLanService& wifi_lan_service,
MutexLock lock(&mutex_);
NEARBY_LOGS(INFO) << "WifiLan::Connect: wifi_lan_service="
<< &wifi_lan_service << ", service_info_name="
<< wifi_lan_service.GetServiceInfo().GetServiceInfoName()
<< wifi_lan_service.GetServiceInfo().GetServiceName()
<< ", service_id=" << service_id;
// Socket to return. To allow for NRVO to work, it has to be a single object.
WifiLanSocket socket;
@@ -273,10 +276,23 @@ WifiLanService WifiLan::GetRemoteWifiLanService(const std::string& ip_address,
return medium_.GetRemoteService(ip_address, port);
}
std::pair<std::string, int> WifiLan::GetServiceAddress(
std::pair<std::string, int> WifiLan::GetCredentials(
const std::string& service_id) {
MutexLock lock(&mutex_);
return medium_.GetServiceAddress(service_id);
return medium_.GetCredentials(service_id);
}
std::string WifiLan::GenerateServiceType(const std::string& service_id) {
std::string service_id_hash_string;
const ByteArray service_id_hash = Utils::Sha256Hash(
service_id, NsdServiceInfo::kTypeFromServiceIdHashLength);
for (auto byte : std::string(service_id_hash)) {
absl::StrAppend(&service_id_hash_string, absl::StrFormat("%02X", byte));
}
return absl::StrFormat(NsdServiceInfo::kNsdTypeFormat,
service_id_hash_string);
}
} // namespace connections
+5 -2
View File
@@ -42,7 +42,7 @@ class WifiLan {
// then enables WifiLan advertising.
// Returns true, if name is successfully set, and false otherwise.
bool StartAdvertising(const std::string& service_id,
const NsdServiceInfo& nsd_service_info)
NsdServiceInfo& nsd_service_info)
ABSL_LOCKS_EXCLUDED(mutex_);
// Disables WifiLan advertising, and restores service info name to
@@ -89,7 +89,7 @@ class WifiLan {
WifiLanService GetRemoteWifiLanService(const std::string& ip_address,
int port) ABSL_LOCKS_EXCLUDED(mutex_);
std::pair<std::string, int> GetServiceAddress(const std::string& service_id)
std::pair<std::string, int> GetCredentials(const std::string& service_id)
ABSL_LOCKS_EXCLUDED(mutex_);
private:
@@ -150,6 +150,9 @@ class WifiLan {
bool IsAcceptingConnectionsLocked(const std::string& service_id)
ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
// Generates mDNS type.
std::string GenerateServiceType(const std::string& service_id);
mutable Mutex mutex_;
WifiLanMedium medium_ ABSL_GUARDED_BY(mutex_);
AdvertisingInfo advertising_info_ ABSL_GUARDED_BY(mutex_);
+4 -4
View File
@@ -69,7 +69,7 @@ TEST_P(WifiLanTest, CanStartAcceptingConnectionsAndConnect) {
CountDownLatch accept_latch(1);
NsdServiceInfo nsd_service_info;
nsd_service_info.SetServiceInfoName(service_info_name);
nsd_service_info.SetServiceName(service_info_name);
nsd_service_info.SetTxtRecord(std::string(kEndpointInfoKey),
endpoint_info_name);
wifi_lan_a.StartAdvertising(service_id, nsd_service_info);
@@ -120,7 +120,7 @@ TEST_P(WifiLanTest, CanCancelConnect) {
CountDownLatch accept_latch(1);
NsdServiceInfo nsd_service_info;
nsd_service_info.SetServiceInfoName(service_info_name);
nsd_service_info.SetServiceName(service_info_name);
nsd_service_info.SetTxtRecord(std::string(kEndpointInfoKey),
endpoint_info_name);
wifi_lan_a.StartAdvertising(service_id, nsd_service_info);
@@ -197,7 +197,7 @@ TEST_F(WifiLanTest, CanStartAdvertising) {
});
NsdServiceInfo nsd_service_info;
nsd_service_info.SetServiceInfoName(service_info_name);
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));
@@ -218,7 +218,7 @@ TEST_F(WifiLanTest, CanStartDiscovery) {
CountDownLatch lost_latch(1);
NsdServiceInfo nsd_service_info;
nsd_service_info.SetServiceInfoName(service_info_name);
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);
+10 -11
View File
@@ -578,7 +578,7 @@ void P2pClusterPcpHandler::WifiLanServiceDiscoveredHandler(
if (!client->IsDiscovering()) {
NEARBY_LOGS(WARNING)
<< "Skipping discovery of NsdServiceInfo "
<< wifi_lan_service.GetServiceInfo().GetServiceInfoName()
<< wifi_lan_service.GetServiceInfo().GetServiceName()
<< " because we are no longer discovering.";
return;
}
@@ -593,7 +593,7 @@ void P2pClusterPcpHandler::WifiLanServiceDiscoveredHandler(
// Report the discovered endpoint to the client.
NEARBY_LOGS(INFO)
<< "Found NsdServiceInfo "
<< wifi_lan_service.GetServiceInfo().GetServiceInfoName()
<< wifi_lan_service.GetServiceInfo().GetServiceName()
<< " (with endpoint_id=" << service_info.GetEndpointId()
<< "and endpoint_info="
<< absl::BytesToHexString(service_info.GetEndpointInfo().data())
@@ -618,7 +618,7 @@ void P2pClusterPcpHandler::WifiLanServiceLostHandler(
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());
&wifi_lan_service, nsd_service_info.GetServiceName().c_str());
RunOnPcpHandlerThread(
"p2p-wifi-service-lost",
[this, client, service_id, nsd_service_info]()
@@ -626,7 +626,7 @@ void P2pClusterPcpHandler::WifiLanServiceLostHandler(
// Make sure we are still discovering before proceeding.
if (!client->IsDiscovering()) {
NEARBY_LOGS(WARNING) << "Ignoring lost NsdServiceInfo "
<< nsd_service_info.GetServiceInfoName()
<< nsd_service_info.GetServiceName()
<< " because we are no longer "
"discovering.";
return;
@@ -641,8 +641,7 @@ void P2pClusterPcpHandler::WifiLanServiceLostHandler(
// Report the lost endpoint to the client.
NEARBY_LOGS(INFO)
<< "Lost NsdServiceInfo "
<< nsd_service_info.GetServiceInfoName()
<< "Lost NsdServiceInfo " << nsd_service_info.GetServiceName()
<< " (with endpoint_id=" << service_info.GetEndpointId()
<< " and endpoint_info="
<< absl::BytesToHexString(service_info.GetEndpointInfo().data())
@@ -1234,7 +1233,7 @@ proto::connections::Medium P2pClusterPcpHandler::StartWifiLanAdvertising(
std::string remote_service_info_name =
socket.GetRemoteWifiLanService()
.GetServiceInfo()
.GetServiceInfoName();
.GetServiceName();
auto channel =
absl::make_unique<WifiLanEndpointChannel>(
remote_service_info_name, socket);
@@ -1288,7 +1287,7 @@ proto::connections::Medium P2pClusterPcpHandler::StartWifiLanAdvertising(
}
NEARBY_LOGS(INFO) << "In StartWifiLanAdvertising(%s), client="
<< client->GetClientId() << " generated WifiLanServiceInfo "
<< nsd_service_info.GetServiceInfoName()
<< nsd_service_info.GetServiceName()
<< " with service_id=" << service_id;
if (!wifi_lan_medium_.StartAdvertising(service_id, nsd_service_info)) {
@@ -1296,7 +1295,7 @@ proto::connections::Medium P2pClusterPcpHandler::StartWifiLanAdvertising(
<< absl::BytesToHexString(local_endpoint_info.data())
<< "), client=" << client->GetClientId()
<< " couldn't advertise with WifiLanServiceInfo "
<< nsd_service_info.GetServiceInfoName();
<< nsd_service_info.GetServiceName();
wifi_lan_medium_.StopAcceptingConnections(service_id);
return proto::connections::UNKNOWN_MEDIUM;
}
@@ -1304,7 +1303,7 @@ proto::connections::Medium P2pClusterPcpHandler::StartWifiLanAdvertising(
<< absl::BytesToHexString(local_endpoint_info.data())
<< "), client=" << client->GetClientId()
<< " advertised with WifiLanServiceInfo "
<< nsd_service_info.GetServiceInfoName();
<< nsd_service_info.GetServiceName();
return proto::connections::WIFI_LAN;
}
@@ -1339,7 +1338,7 @@ BasePcpHandler::ConnectImplResult P2pClusterPcpHandler::WifiLanConnectImpl(
if (!wifi_lan_socket.IsValid()) {
NEARBY_LOGS(ERROR)
<< "In WifiLanConnectImpl(), failed to connect to service "
<< wifi_lan_service.GetServiceInfo().GetServiceInfoName()
<< wifi_lan_service.GetServiceInfo().GetServiceName()
<< " for endpoint(id=" << endpoint->endpoint_id << ").";
return BasePcpHandler::ConnectImplResult{
.status = {Status::kWifiLanError},
+3 -3
View File
@@ -70,9 +70,9 @@ ByteArray WifiLanBwuHandler::InitializeUpgradedMediumForEndpoint(
// cache service ID to revert
active_service_ids_.emplace(upgrade_service_id);
auto service_address = wifi_lan_medium_.GetServiceAddress(upgrade_service_id);
auto ip_address = service_address.first;
auto port = service_address.second;
auto credential = wifi_lan_medium_.GetCredentials(upgrade_service_id);
auto ip_address = credential.first;
auto port = credential.second;
if (ip_address.empty()) {
NEARBY_LOGS(INFO)
<< "WifiLanBwuHandler couldn't initiate the wifi_lan upgrade for "
+2 -2
View File
@@ -77,7 +77,7 @@ WifiLanServiceInfo::WifiLanServiceInfo(const NsdServiceInfo& nsd_service_info) {
}
}
auto service_info_name = nsd_service_info.GetServiceInfoName();
auto service_info_name = nsd_service_info.GetServiceName();
ByteArray service_info_bytes = Base64Utils::Decode(service_info_name);
if (service_info_bytes.Empty()) {
NEARBY_LOG(
@@ -193,7 +193,7 @@ WifiLanServiceInfo::operator NsdServiceInfo() const {
}
NsdServiceInfo nsd_service_info;
nsd_service_info.SetServiceInfoName(
nsd_service_info.SetServiceName(
Base64Utils::Encode(ByteArray{std::move(out)}));
nsd_service_info.SetTxtRecord(std::string(kKeyEndpointInfo),
Base64Utils::Encode(endpoint_info_));
@@ -146,7 +146,7 @@ TEST(WifiLanServiceInfoTest, ConstructionFailsWithShortServiceNameLength) {
ByteArray wifi_lan_service_info_bytes{wifi_lan_service_info_name};
NsdServiceInfo nsd_service_info;
nsd_service_info.SetServiceInfoName(
nsd_service_info.SetServiceName(
Base64Utils::Encode(wifi_lan_service_info_bytes));
WifiLanServiceInfo wifi_lan_service_info{nsd_service_info};