mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-14 22:56:12 -04:00
nearbyconnections : Replace WifiLanV2 to WifiLan.
PiperOrigin-RevId: 406033177
This commit is contained in:
committed by
Copybara-Service
parent
94ef1b532b
commit
acf516189b
@@ -43,9 +43,7 @@ cc_library(
|
||||
"webrtc_bwu_handler.cc",
|
||||
"webrtc_endpoint_channel.cc",
|
||||
"wifi_lan_bwu_handler.cc",
|
||||
"wifi_lan_bwu_handler_v2.cc",
|
||||
"wifi_lan_endpoint_channel.cc",
|
||||
"wifi_lan_endpoint_channel_v2.cc",
|
||||
"wifi_lan_service_info.cc",
|
||||
],
|
||||
hdrs = [
|
||||
@@ -82,9 +80,7 @@ cc_library(
|
||||
"webrtc_bwu_handler.h",
|
||||
"webrtc_endpoint_channel.h",
|
||||
"wifi_lan_bwu_handler.h",
|
||||
"wifi_lan_bwu_handler_v2.h",
|
||||
"wifi_lan_endpoint_channel.h",
|
||||
"wifi_lan_endpoint_channel_v2.h",
|
||||
"wifi_lan_service_info.h",
|
||||
],
|
||||
compatible_with = ["//buildenv/target:non_prod"],
|
||||
|
||||
@@ -199,16 +199,8 @@ class BasePcpHandler : public PcpHandler,
|
||||
};
|
||||
|
||||
struct WifiLanEndpoint : public DiscoveredEndpoint {
|
||||
WifiLanEndpoint(DiscoveredEndpoint endpoint, WifiLanService service)
|
||||
: DiscoveredEndpoint(std::move(endpoint)),
|
||||
wifi_lan_service(std::move(service)) {}
|
||||
|
||||
WifiLanService wifi_lan_service;
|
||||
};
|
||||
|
||||
struct WifiLanV2Endpoint : public DiscoveredEndpoint {
|
||||
WifiLanV2Endpoint(DiscoveredEndpoint endpoint,
|
||||
const NsdServiceInfo& service_info)
|
||||
WifiLanEndpoint(DiscoveredEndpoint endpoint,
|
||||
const NsdServiceInfo& service_info)
|
||||
: DiscoveredEndpoint(std::move(endpoint)), service_info(service_info) {}
|
||||
|
||||
NsdServiceInfo service_info;
|
||||
|
||||
@@ -24,7 +24,6 @@ cc_library(
|
||||
"uuid.cc",
|
||||
"webrtc.cc",
|
||||
"wifi_lan.cc",
|
||||
"wifi_lan_v2.cc",
|
||||
],
|
||||
hdrs = [
|
||||
"ble.h",
|
||||
@@ -36,7 +35,6 @@ cc_library(
|
||||
"uuid.h",
|
||||
"webrtc.h",
|
||||
"wifi_lan.h",
|
||||
"wifi_lan_v2.h",
|
||||
],
|
||||
compatible_with = ["//buildenv/target:non_prod"],
|
||||
visibility = [
|
||||
@@ -104,7 +102,6 @@ cc_test(
|
||||
"lost_entity_tracker_test.cc",
|
||||
"uuid_test.cc",
|
||||
"wifi_lan_test.cc",
|
||||
"wifi_lan_test_v2.cc",
|
||||
],
|
||||
shard_count = 16,
|
||||
deps = [
|
||||
|
||||
@@ -26,8 +26,6 @@ Ble& Mediums::GetBle() { return ble_; }
|
||||
|
||||
WifiLan& Mediums::GetWifiLan() { return wifi_lan_; }
|
||||
|
||||
WifiLanV2& Mediums::GetWifiLanV2() { return wifi_lan_v2_; }
|
||||
|
||||
mediums::WebRtc& Mediums::GetWebRtc() { return webrtc_; }
|
||||
|
||||
} // namespace connections
|
||||
|
||||
@@ -20,7 +20,6 @@
|
||||
#include "core/internal/mediums/bluetooth_radio.h"
|
||||
#include "core/internal/mediums/webrtc.h"
|
||||
#include "core/internal/mediums/wifi_lan.h"
|
||||
#include "core/internal/mediums/wifi_lan_v2.h"
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
@@ -44,9 +43,6 @@ class Mediums {
|
||||
// Returns a handle to the Wifi-Lan medium.
|
||||
WifiLan& GetWifiLan();
|
||||
|
||||
// Returns a handle to the Wifi-Lan medium.
|
||||
WifiLanV2& GetWifiLanV2();
|
||||
|
||||
// Returns a handle to the WebRtc medium.
|
||||
mediums::WebRtc& GetWebRtc();
|
||||
|
||||
@@ -63,7 +59,6 @@ class Mediums {
|
||||
BluetoothClassic bluetooth_classic_{bluetooth_radio_};
|
||||
Ble ble_{bluetooth_radio_};
|
||||
WifiLan wifi_lan_;
|
||||
WifiLanV2 wifi_lan_v2_;
|
||||
mediums::WebRtc webrtc_;
|
||||
};
|
||||
|
||||
|
||||
@@ -27,6 +27,24 @@ namespace location {
|
||||
namespace nearby {
|
||||
namespace connections {
|
||||
|
||||
WifiLan::~WifiLan() {
|
||||
// Destructor is not taking locks, but methods it is calling are.
|
||||
while (!discovering_info_.service_ids.empty()) {
|
||||
StopDiscovery(*discovering_info_.service_ids.begin());
|
||||
}
|
||||
while (!server_sockets_.empty()) {
|
||||
StopAcceptingConnections(server_sockets_.begin()->first);
|
||||
}
|
||||
while (!advertising_info_.nsd_service_infos.empty()) {
|
||||
StopAdvertising(advertising_info_.nsd_service_infos.begin()->first);
|
||||
}
|
||||
|
||||
// All the AcceptLoopRunnable objects in here should already have gotten an
|
||||
// opportunity to shut themselves down cleanly in the calls to
|
||||
// StopAcceptingConnections() above.
|
||||
accept_loops_runner_.Shutdown();
|
||||
}
|
||||
|
||||
bool WifiLan::IsAvailable() const {
|
||||
MutexLock lock(&mutex_);
|
||||
|
||||
@@ -39,6 +57,12 @@ bool WifiLan::StartAdvertising(const std::string& service_id,
|
||||
NsdServiceInfo& nsd_service_info) {
|
||||
MutexLock lock(&mutex_);
|
||||
|
||||
if (!IsAvailableLocked()) {
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "Can't turn on WifiLan advertising. WifiLan is not available.";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!nsd_service_info.IsValid()) {
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "Refusing to turn on WifiLan advertising. nsd_service_info is not "
|
||||
@@ -52,27 +76,36 @@ bool WifiLan::StartAdvertising(const std::string& service_id,
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!IsAvailableLocked()) {
|
||||
NEARBY_LOG(INFO,
|
||||
"Can't turn on WifiLan advertising. WifiLan is not available.");
|
||||
if (!IsAcceptingConnectionsLocked(service_id)) {
|
||||
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
|
||||
<< ". Should accept connections before advertising.";
|
||||
return false;
|
||||
}
|
||||
|
||||
nsd_service_info.SetServiceType(GenerateServiceType(service_id));
|
||||
if (!medium_.StartAdvertising(service_id, nsd_service_info)) {
|
||||
const auto& it = server_sockets_.find(service_id);
|
||||
if (it != server_sockets_.end()) {
|
||||
nsd_service_info.SetIPAddress(it->second.GetIPAddress());
|
||||
nsd_service_info.SetPort(it->second.GetPort());
|
||||
}
|
||||
if (!medium_.StartAdvertising(nsd_service_info)) {
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "Failed to turn on WifiLan advertising with wifi_lan_service="
|
||||
<< "Failed to turn on WifiLan advertising with nsd_service_info="
|
||||
<< &nsd_service_info
|
||||
<< ", service_info_name=" << nsd_service_info.GetServiceName()
|
||||
<< ", service_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.GetServiceName()
|
||||
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);
|
||||
advertising_info_.Add(service_id, std::move(nsd_service_info));
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -80,15 +113,17 @@ bool WifiLan::StopAdvertising(const std::string& service_id) {
|
||||
MutexLock lock(&mutex_);
|
||||
|
||||
if (!IsAdvertisingLocked(service_id)) {
|
||||
NEARBY_LOG(INFO, "Can't turn off WifiLan advertising; it is already off");
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "Can't turn off WifiLan advertising; it is already off";
|
||||
return false;
|
||||
}
|
||||
|
||||
NEARBY_LOG(INFO, "Turned off WifiLan advertising with service_id=%s",
|
||||
service_id.c_str());
|
||||
bool ret = medium_.StopAdvertising(service_id);
|
||||
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.
|
||||
// advertising for specific service_id.
|
||||
advertising_info_.Remove(service_id);
|
||||
return ret;
|
||||
}
|
||||
@@ -108,33 +143,33 @@ bool WifiLan::StartDiscovery(const std::string& service_id,
|
||||
MutexLock lock(&mutex_);
|
||||
|
||||
if (service_id.empty()) {
|
||||
NEARBY_LOG(INFO,
|
||||
"Refusing to start WifiLan discovering with empty service_id.");
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "Refusing to start WifiLan discovering with empty service_id.";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!IsAvailableLocked()) {
|
||||
NEARBY_LOG(
|
||||
INFO,
|
||||
"Can't discover WifiLan services because WifiLan isn't available.");
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "Can't discover WifiLan services because WifiLan isn't available.";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (IsDiscoveringLocked(service_id)) {
|
||||
NEARBY_LOG(
|
||||
INFO,
|
||||
"Refusing to start discovery of WifiLan services because another "
|
||||
"discovery is already in-progress.");
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "Refusing to start discovery of WifiLan services because another "
|
||||
"discovery is already in-progress.";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!medium_.StartDiscovery(service_id, callback)) {
|
||||
NEARBY_LOG(INFO, "Failed to start discovery of WifiLan services.");
|
||||
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_LOG(INFO, "Turned on WifiLan discovering with service_id=%s",
|
||||
service_id.c_str());
|
||||
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;
|
||||
@@ -144,22 +179,22 @@ bool WifiLan::StopDiscovery(const std::string& service_id) {
|
||||
MutexLock lock(&mutex_);
|
||||
|
||||
if (!IsDiscoveringLocked(service_id)) {
|
||||
NEARBY_LOG(INFO,
|
||||
"Can't turn off WifiLan discovering because we never started "
|
||||
"discovering.");
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "Can't turn off WifiLan discovering because we never started "
|
||||
"discovering.";
|
||||
return false;
|
||||
}
|
||||
|
||||
NEARBY_LOG(INFO, "Turned off WifiLan discovering with service_id=%s",
|
||||
service_id.c_str());
|
||||
bool ret = medium_.StopDiscovery(service_id);
|
||||
discovering_info_.Clear();
|
||||
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 WifiLan::IsDiscovering(const std::string& service_id) {
|
||||
MutexLock lock(&mutex_);
|
||||
|
||||
return IsDiscoveringLocked(service_id);
|
||||
}
|
||||
|
||||
@@ -172,87 +207,129 @@ bool WifiLan::StartAcceptingConnections(const std::string& service_id,
|
||||
MutexLock lock(&mutex_);
|
||||
|
||||
if (service_id.empty()) {
|
||||
NEARBY_LOG(INFO,
|
||||
"Refusing to start accepting WifiLan connections with empty "
|
||||
"service_id.");
|
||||
NEARBY_LOGS(INFO) << "Refusing to start accepting WifiLan connections; "
|
||||
"service_id is empty.";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!IsAvailableLocked()) {
|
||||
NEARBY_LOG(INFO,
|
||||
"Can't start accepting WifiLan connections for %s because "
|
||||
"WifiLan isn't available.",
|
||||
service_id.c_str());
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "Can't start accepting WifiLan connections [service_id="
|
||||
<< service_id << "]; WifiLan not available.";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (IsAcceptingConnectionsLocked(service_id)) {
|
||||
NEARBY_LOG(INFO,
|
||||
"Refusing to start accepting WifiLan connections for %s because "
|
||||
"another WifiLan service socket is already in-progress.",
|
||||
service_id.c_str());
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "Refusing to start accepting WifiLan connections [service="
|
||||
<< service_id
|
||||
<< "]; WifiLan server is already in-progress with the same name.";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!medium_.StartAcceptingConnections(service_id, callback)) {
|
||||
NEARBY_LOG(INFO, "Failed to accept connections callback for %s.",
|
||||
service_id.c_str());
|
||||
// We can generate an exact port here on server socket; now we just assign 0
|
||||
// to let platform medium decide it.
|
||||
int port = 0;
|
||||
WifiLanServerSocket server_socket = medium_.ListenForService(port);
|
||||
if (!server_socket.IsValid()) {
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "Failed to start accepting WifiLan connections for service_id="
|
||||
<< service_id;
|
||||
return false;
|
||||
}
|
||||
|
||||
accepting_connections_info_.Add(service_id);
|
||||
// Mark the fact that there's an in-progress WifiLan server accepting
|
||||
// connections.
|
||||
auto owned_server_socket =
|
||||
server_sockets_.insert({service_id, std::move(server_socket)})
|
||||
.first->second;
|
||||
|
||||
// Start the accept loop on a dedicated thread - this stays alive and
|
||||
// listening for new incoming connections until StopAcceptingConnections() is
|
||||
// invoked.
|
||||
accept_loops_runner_.Execute(
|
||||
"wifi-lan-accept",
|
||||
[callback = std::move(callback),
|
||||
server_socket = std::move(owned_server_socket), service_id]() mutable {
|
||||
while (true) {
|
||||
WifiLanSocket client_socket = server_socket.Accept();
|
||||
if (!client_socket.IsValid()) {
|
||||
server_socket.Close();
|
||||
break;
|
||||
}
|
||||
callback.accepted_cb(std::move(client_socket));
|
||||
}
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WifiLan::StopAcceptingConnections(const std::string& service_id) {
|
||||
MutexLock lock(&mutex_);
|
||||
|
||||
if (!IsAcceptingConnectionsLocked(service_id)) {
|
||||
NEARBY_LOG(INFO,
|
||||
"Can't stop accepting WifiLan connections because it was never "
|
||||
"started.");
|
||||
if (service_id.empty()) {
|
||||
NEARBY_LOGS(INFO) << "Unable to stop accepting WifiLan connections because "
|
||||
"the service_id is empty.";
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ret = medium_.StopAcceptingConnections(service_id);
|
||||
// Reset our bundle of accepting connections state to mark that we're no
|
||||
// longer accepting connections.
|
||||
accepting_connections_info_.Remove(service_id);
|
||||
return ret;
|
||||
const auto& it = server_sockets_.find(service_id);
|
||||
if (it == server_sockets_.end()) {
|
||||
NEARBY_LOGS(INFO) << "Can't stop accepting WifiLan connections for "
|
||||
<< service_id << " because it was never started.";
|
||||
return false;
|
||||
}
|
||||
|
||||
// Closing the WifiLanServerSocket will kick off the suicide of the thread
|
||||
// in accept_loops_thread_pool_ that blocks on WifiLanServerSocket.accept().
|
||||
// That may take some time to complete, but there's no particular reason to
|
||||
// wait around for it.
|
||||
auto item = server_sockets_.extract(it);
|
||||
|
||||
// Store a handle to the WifiLanServerSocket, so we can use it after
|
||||
// removing the entry from server_sockets_; making it scoped
|
||||
// is a bonus that takes care of deallocation before we leave this method.
|
||||
WifiLanServerSocket& listening_socket = item.mapped();
|
||||
|
||||
// Regardless of whether or not we fail to close the existing
|
||||
// WifiLanServerSocket, remove it from server_sockets_ so that it
|
||||
// frees up this service for another round.
|
||||
|
||||
// Finally, close the WifiLanServerSocket.
|
||||
if (!listening_socket.Close().Ok()) {
|
||||
NEARBY_LOGS(INFO) << "Failed to close WifiLan server socket for service_id="
|
||||
<< service_id;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WifiLan::IsAcceptingConnections(const std::string& service_id) {
|
||||
MutexLock lock(&mutex_);
|
||||
|
||||
return IsAcceptingConnectionsLocked(service_id);
|
||||
}
|
||||
|
||||
bool WifiLan::IsAcceptingConnectionsLocked(const std::string& service_id) {
|
||||
return accepting_connections_info_.Existed(service_id);
|
||||
return server_sockets_.find(service_id) != server_sockets_.end();
|
||||
}
|
||||
|
||||
WifiLanSocket WifiLan::Connect(WifiLanService& wifi_lan_service,
|
||||
const std::string& service_id,
|
||||
WifiLanSocket WifiLan::Connect(const std::string& service_id,
|
||||
const NsdServiceInfo& service_info,
|
||||
CancellationFlag* cancellation_flag) {
|
||||
MutexLock lock(&mutex_);
|
||||
NEARBY_LOGS(INFO) << "WifiLan::Connect: wifi_lan_service="
|
||||
<< &wifi_lan_service << ", service_info_name="
|
||||
<< 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;
|
||||
|
||||
if (service_id.empty()) {
|
||||
NEARBY_LOG(INFO,
|
||||
"Refusing to create WifiLan socket with empty service_id.");
|
||||
NEARBY_LOGS(INFO) << "Refusing to create client WifiLan socket because "
|
||||
"service_id is empty.";
|
||||
return socket;
|
||||
}
|
||||
|
||||
if (!IsAvailableLocked()) {
|
||||
NEARBY_LOG(INFO,
|
||||
"Can't create client WifiLan socket [service_id=%s]; WifiLan "
|
||||
"isn't available.",
|
||||
service_id.c_str());
|
||||
NEARBY_LOGS(INFO) << "Can't create client WifiLan socket [service_id="
|
||||
<< service_id << "]; WifiLan isn't available.";
|
||||
return socket;
|
||||
}
|
||||
|
||||
@@ -261,25 +338,57 @@ WifiLanSocket WifiLan::Connect(WifiLanService& wifi_lan_service,
|
||||
return socket;
|
||||
}
|
||||
|
||||
socket = medium_.Connect(wifi_lan_service, service_id, cancellation_flag);
|
||||
socket = medium_.ConnectToService(service_info, cancellation_flag);
|
||||
if (!socket.IsValid()) {
|
||||
NEARBY_LOG(INFO, "Failed to Connect via WifiLan [service_id=%s]",
|
||||
service_id.c_str());
|
||||
NEARBY_LOGS(INFO) << "Failed to Connect via WifiLan [service_id="
|
||||
<< service_id << "]";
|
||||
}
|
||||
|
||||
return socket;
|
||||
}
|
||||
|
||||
WifiLanService WifiLan::GetRemoteWifiLanService(const std::string& ip_address,
|
||||
int port) {
|
||||
WifiLanSocket WifiLan::Connect(const std::string& service_id,
|
||||
const std::string& ip_address, int port,
|
||||
CancellationFlag* cancellation_flag) {
|
||||
MutexLock lock(&mutex_);
|
||||
return medium_.GetRemoteService(ip_address, port);
|
||||
// Socket to return. To allow for NRVO to work, it has to be a single object.
|
||||
WifiLanSocket socket;
|
||||
|
||||
if (service_id.empty()) {
|
||||
NEARBY_LOGS(INFO) << "Refusing to create client WifiLan socket because "
|
||||
"service_id is empty.";
|
||||
return socket;
|
||||
}
|
||||
|
||||
if (!IsAvailableLocked()) {
|
||||
NEARBY_LOGS(INFO) << "Can't create client WifiLan socket [service_id="
|
||||
<< service_id << "]; WifiLan isn't available.";
|
||||
return socket;
|
||||
}
|
||||
|
||||
if (cancellation_flag->Cancelled()) {
|
||||
NEARBY_LOGS(INFO) << "Can't create client WifiLan socket due to cancel.";
|
||||
return socket;
|
||||
}
|
||||
|
||||
socket = medium_.ConnectToService(ip_address, port, cancellation_flag);
|
||||
if (!socket.IsValid()) {
|
||||
NEARBY_LOGS(INFO) << "Failed to Connect via WifiLan [service_id="
|
||||
<< service_id << "]";
|
||||
}
|
||||
|
||||
return socket;
|
||||
}
|
||||
|
||||
std::pair<std::string, int> WifiLan::GetCredentials(
|
||||
const std::string& service_id) {
|
||||
MutexLock lock(&mutex_);
|
||||
return medium_.GetCredentials(service_id);
|
||||
const auto& it = server_sockets_.find(service_id);
|
||||
if (it == server_sockets_.end()) {
|
||||
return std::pair<std::string, int>();
|
||||
}
|
||||
return std::pair<std::string, int>(it->second.GetIPAddress(),
|
||||
it->second.GetPort());
|
||||
}
|
||||
|
||||
std::string WifiLan::GenerateServiceType(const std::string& service_id) {
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
#include "absl/container/flat_hash_set.h"
|
||||
#include "platform/base/byte_array.h"
|
||||
#include "platform/base/cancellation_flag.h"
|
||||
#include "platform/base/nsd_service_info.h"
|
||||
#include "platform/public/multi_thread_executor.h"
|
||||
#include "platform/public/mutex.h"
|
||||
#include "platform/public/wifi_lan.h"
|
||||
@@ -33,33 +34,42 @@ namespace connections {
|
||||
class WifiLan {
|
||||
public:
|
||||
using DiscoveredServiceCallback = WifiLanMedium::DiscoveredServiceCallback;
|
||||
using AcceptedConnectionCallback = WifiLanMedium::AcceptedConnectionCallback;
|
||||
|
||||
// Callback that is invoked when a new connection is accepted.
|
||||
struct AcceptedConnectionCallback {
|
||||
std::function<void(WifiLanSocket socket)> accepted_cb =
|
||||
DefaultCallback<WifiLanSocket>();
|
||||
};
|
||||
|
||||
WifiLan() = default;
|
||||
~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 in NsdServiceInfo and
|
||||
// then enables WifiLan advertising.
|
||||
// Returns true, if name is successfully set, and false otherwise.
|
||||
// Returns true, if NsdServiceInfo is successfully set, and false otherwise.
|
||||
bool StartAdvertising(const std::string& service_id,
|
||||
NsdServiceInfo& nsd_service_info)
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
// Disables WifiLan advertising, and restores service info name to
|
||||
// what they were before the call to StartAdvertising().
|
||||
// Disables WifiLan advertising.
|
||||
// Returns false if no successful call StartAdvertising() was previously
|
||||
// made, otherwise returns true.
|
||||
bool StopAdvertising(const std::string& service_id)
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
bool IsAdvertising(const std::string& service_id) ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
// Enables WifiLan discovery mode. Will report any discoverable services in
|
||||
// range through a callback. Returns true, if discovery mode was enabled,
|
||||
// false otherwise.
|
||||
// Enables WifiLan discovery. Will report any discoverable services
|
||||
// through a callback.
|
||||
// Returns true, if discovery was enabled, false otherwise.
|
||||
bool StartDiscovery(const std::string& service_id,
|
||||
DiscoveredServiceCallback callback)
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
// Disables WifiLan discovery mode.
|
||||
// Disables WifiLan discovery.
|
||||
bool StopDiscovery(const std::string& service_id) ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
bool IsDiscovering(const std::string& service_id) ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
@@ -81,36 +91,55 @@ class WifiLan {
|
||||
// another service with StartAcceptingConnections() using the same service_id.
|
||||
// Blocks until connection is established, or server-side is terminated.
|
||||
// Returns socket instance. On success, WifiLanSocket.IsValid() return true.
|
||||
WifiLanSocket Connect(WifiLanService& wifi_lan_service,
|
||||
const std::string& service_id,
|
||||
WifiLanSocket Connect(const std::string& service_id,
|
||||
const NsdServiceInfo& service_info,
|
||||
CancellationFlag* cancellation_flag)
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
WifiLanService GetRemoteWifiLanService(const std::string& ip_address,
|
||||
int port) ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
// Establishes connection to WifiLan service by ip address and port for
|
||||
// bandwidth upgradation.
|
||||
// Returns socket instance. On success, WifiLanSocket.IsValid() return true.
|
||||
WifiLanSocket Connect(const std::string& service_id,
|
||||
const std::string& ip_address, int port,
|
||||
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_);
|
||||
|
||||
private:
|
||||
struct AdvertisingInfo {
|
||||
bool Empty() const { return service_ids.empty(); }
|
||||
void Clear() { service_ids.clear(); }
|
||||
void Add(const std::string& service_id) { service_ids.emplace(service_id); }
|
||||
bool Empty() const { return nsd_service_infos.empty(); }
|
||||
void Clear() { nsd_service_infos.clear(); }
|
||||
void Add(const std::string& service_id,
|
||||
const NsdServiceInfo& nsd_service_info) {
|
||||
nsd_service_infos.insert({service_id, nsd_service_info});
|
||||
}
|
||||
void Remove(const std::string& service_id) {
|
||||
service_ids.erase(service_id);
|
||||
nsd_service_infos.erase(service_id);
|
||||
}
|
||||
bool Existed(const std::string& service_id) const {
|
||||
return service_ids.contains(service_id);
|
||||
return nsd_service_infos.contains(service_id);
|
||||
}
|
||||
NsdServiceInfo* GetServiceInfo(const std::string& service_id) {
|
||||
const auto& it = nsd_service_infos.find(service_id);
|
||||
if (it == nsd_service_infos.end()) {
|
||||
return nullptr;
|
||||
}
|
||||
return &it->second;
|
||||
}
|
||||
|
||||
absl::flat_hash_set<std::string> service_ids;
|
||||
absl::flat_hash_map<std::string, NsdServiceInfo> nsd_service_infos;
|
||||
};
|
||||
|
||||
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);
|
||||
}
|
||||
@@ -121,19 +150,7 @@ class WifiLan {
|
||||
absl::flat_hash_set<std::string> service_ids;
|
||||
};
|
||||
|
||||
struct AcceptingConnectionsInfo {
|
||||
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 Remove(const std::string& service_id) {
|
||||
service_ids.erase(service_id);
|
||||
}
|
||||
bool Existed(const std::string& service_id) const {
|
||||
return service_ids.contains(service_id);
|
||||
}
|
||||
|
||||
absl::flat_hash_set<std::string> service_ids;
|
||||
};
|
||||
static constexpr int kMaxConcurrentAcceptLoops = 5;
|
||||
|
||||
// Same as IsAvailable(), but must be called with mutex_ held.
|
||||
bool IsAvailableLocked() const ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
|
||||
@@ -157,7 +174,17 @@ class WifiLan {
|
||||
WifiLanMedium medium_ ABSL_GUARDED_BY(mutex_);
|
||||
AdvertisingInfo advertising_info_ ABSL_GUARDED_BY(mutex_);
|
||||
DiscoveringInfo discovering_info_ ABSL_GUARDED_BY(mutex_);
|
||||
AcceptingConnectionsInfo accepting_connections_info_ ABSL_GUARDED_BY(mutex_);
|
||||
|
||||
// A thread pool dedicated to running all the accept loops from
|
||||
// StartAcceptingConnections().
|
||||
MultiThreadExecutor accept_loops_runner_{kMaxConcurrentAcceptLoops};
|
||||
|
||||
// A map of service_id -> ServerSocket. If map is non-empty, we
|
||||
// are currently listening for incoming connections.
|
||||
// WifiLanServerSocket instances are used from accept_loops_runner_,
|
||||
// and thus require pointer stability.
|
||||
absl::flat_hash_map<std::string, WifiLanServerSocket> server_sockets_
|
||||
ABSL_GUARDED_BY(mutex_);
|
||||
};
|
||||
|
||||
} // namespace connections
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
#include "gtest/gtest.h"
|
||||
#include "absl/strings/string_view.h"
|
||||
#include "platform/base/medium_environment.h"
|
||||
#include "platform/base/nsd_service_info.h"
|
||||
#include "platform/public/count_down_latch.h"
|
||||
#include "platform/public/logging.h"
|
||||
#include "platform/public/wifi_lan.h"
|
||||
@@ -42,9 +43,8 @@ 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 WifiLanTest : public ::testing::TestWithParam<FeatureFlags> {
|
||||
@@ -56,54 +56,59 @@ class WifiLanTest : public ::testing::TestWithParam<FeatureFlags> {
|
||||
MediumEnvironment& env_{MediumEnvironment::Instance()};
|
||||
};
|
||||
|
||||
TEST_P(WifiLanTest, CanStartAcceptingConnectionsAndConnect) {
|
||||
TEST_P(WifiLanTest, CanConnect) {
|
||||
FeatureFlags feature_flags = GetParam();
|
||||
env_.SetFeatureFlags(feature_flags);
|
||||
env_.Start();
|
||||
WifiLan wifi_lan_a;
|
||||
WifiLan wifi_lan_b;
|
||||
WifiLan wifi_lan_client;
|
||||
WifiLan wifi_lan_server;
|
||||
std::string service_id(kServiceID);
|
||||
std::string service_info_name{kServiceInfoName};
|
||||
std::string endpoint_info_name{kEndpointName};
|
||||
CountDownLatch found_latch(1);
|
||||
std::string service_info_name(kServiceInfoName);
|
||||
std::string endpoint_info_name(kEndpointName);
|
||||
CountDownLatch discovered_latch(1);
|
||||
CountDownLatch accept_latch(1);
|
||||
|
||||
WifiLanSocket socket_for_server;
|
||||
EXPECT_TRUE(wifi_lan_server.StartAcceptingConnections(
|
||||
service_id,
|
||||
{
|
||||
.accepted_cb =
|
||||
[&socket_for_server, &accept_latch](WifiLanSocket socket) {
|
||||
socket_for_server = std::move(socket);
|
||||
accept_latch.CountDown();
|
||||
},
|
||||
}));
|
||||
|
||||
NsdServiceInfo nsd_service_info;
|
||||
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);
|
||||
wifi_lan_a.StartAcceptingConnections(
|
||||
service_id,
|
||||
{
|
||||
.accepted_cb = [&accept_latch](
|
||||
WifiLanSocket socket,
|
||||
absl::string_view) { accept_latch.CountDown(); },
|
||||
});
|
||||
WifiLanService discovered_service;
|
||||
wifi_lan_b.StartDiscovery(
|
||||
wifi_lan_server.StartAdvertising(service_id, nsd_service_info);
|
||||
|
||||
NsdServiceInfo discovered_service_info;
|
||||
wifi_lan_client.StartDiscovery(
|
||||
service_id,
|
||||
{
|
||||
.service_discovered_cb =
|
||||
[&found_latch, &discovered_service](
|
||||
WifiLanService& service, absl::string_view service_id) {
|
||||
discovered_service = service;
|
||||
NEARBY_LOG(INFO, "Discovered service=%p [impl=%p]", &service,
|
||||
&service.GetImpl());
|
||||
found_latch.CountDown();
|
||||
[&discovered_latch, &discovered_service_info](
|
||||
NsdServiceInfo service_info, const std::string& service_id) {
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "Discovered service_info=" << &service_info;
|
||||
discovered_service_info = service_info;
|
||||
discovered_latch.CountDown();
|
||||
},
|
||||
});
|
||||
discovered_latch.Await(kWaitDuration).result();
|
||||
ASSERT_TRUE(discovered_service_info.IsValid());
|
||||
|
||||
EXPECT_TRUE(found_latch.Await(kWaitDuration).result());
|
||||
ASSERT_TRUE(discovered_service.IsValid());
|
||||
CancellationFlag flag;
|
||||
WifiLanSocket socket =
|
||||
wifi_lan_b.Connect(discovered_service, service_id, &flag);
|
||||
WifiLanSocket socket_for_client =
|
||||
wifi_lan_client.Connect(service_id, discovered_service_info, &flag);
|
||||
EXPECT_TRUE(accept_latch.Await(kWaitDuration).result());
|
||||
EXPECT_TRUE(socket.IsValid());
|
||||
wifi_lan_b.StopDiscovery(service_id);
|
||||
wifi_lan_a.StopAcceptingConnections(service_id);
|
||||
wifi_lan_a.StopAdvertising(service_id);
|
||||
EXPECT_TRUE(wifi_lan_server.StopAcceptingConnections(service_id));
|
||||
EXPECT_TRUE(wifi_lan_server.StopAdvertising(service_id));
|
||||
EXPECT_TRUE(socket_for_server.IsValid());
|
||||
EXPECT_TRUE(socket_for_client.IsValid());
|
||||
env_.Stop();
|
||||
}
|
||||
|
||||
@@ -111,56 +116,64 @@ TEST_P(WifiLanTest, CanCancelConnect) {
|
||||
FeatureFlags feature_flags = GetParam();
|
||||
env_.SetFeatureFlags(feature_flags);
|
||||
env_.Start();
|
||||
WifiLan wifi_lan_a;
|
||||
WifiLan wifi_lan_b;
|
||||
WifiLan wifi_lan_client;
|
||||
WifiLan wifi_lan_server;
|
||||
std::string service_id(kServiceID);
|
||||
std::string service_info_name{kServiceInfoName};
|
||||
std::string endpoint_info_name{kEndpointName};
|
||||
CountDownLatch found_latch(1);
|
||||
std::string service_info_name(kServiceInfoName);
|
||||
std::string endpoint_info_name(kEndpointName);
|
||||
CountDownLatch discovered_latch(1);
|
||||
CountDownLatch accept_latch(1);
|
||||
|
||||
WifiLanSocket socket_for_server;
|
||||
EXPECT_TRUE(wifi_lan_server.StartAcceptingConnections(
|
||||
service_id,
|
||||
{
|
||||
.accepted_cb =
|
||||
[&socket_for_server, &accept_latch](WifiLanSocket socket) {
|
||||
socket_for_server = std::move(socket);
|
||||
accept_latch.CountDown();
|
||||
},
|
||||
}));
|
||||
|
||||
NsdServiceInfo nsd_service_info;
|
||||
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);
|
||||
wifi_lan_a.StartAcceptingConnections(
|
||||
service_id,
|
||||
{
|
||||
.accepted_cb = [&accept_latch](
|
||||
WifiLanSocket socket,
|
||||
absl::string_view) { accept_latch.CountDown(); },
|
||||
});
|
||||
WifiLanService discovered_service;
|
||||
wifi_lan_b.StartDiscovery(
|
||||
wifi_lan_server.StartAdvertising(service_id, nsd_service_info);
|
||||
|
||||
NsdServiceInfo discovered_service_info;
|
||||
wifi_lan_client.StartDiscovery(
|
||||
service_id,
|
||||
{
|
||||
.service_discovered_cb =
|
||||
[&found_latch, &discovered_service](
|
||||
WifiLanService& service, absl::string_view service_id) {
|
||||
discovered_service = service;
|
||||
NEARBY_LOG(INFO, "Discovered service=%p [impl=%p]", &service,
|
||||
&service.GetImpl());
|
||||
found_latch.CountDown();
|
||||
[&discovered_latch, &discovered_service_info](
|
||||
NsdServiceInfo service_info, const std::string& service_id) {
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "Discovered service_info=" << &service_info;
|
||||
discovered_service_info = service_info;
|
||||
discovered_latch.CountDown();
|
||||
},
|
||||
});
|
||||
EXPECT_TRUE(discovered_latch.Await(kWaitDuration).result());
|
||||
ASSERT_TRUE(discovered_service_info.IsValid());
|
||||
|
||||
EXPECT_TRUE(found_latch.Await(kWaitDuration).result());
|
||||
ASSERT_TRUE(discovered_service.IsValid());
|
||||
CancellationFlag flag(true);
|
||||
WifiLanSocket socket =
|
||||
wifi_lan_b.Connect(discovered_service, service_id, &flag);
|
||||
WifiLanSocket socket_for_client =
|
||||
wifi_lan_client.Connect(service_id, discovered_service_info, &flag);
|
||||
// If FeatureFlag is disabled, Cancelled is false as no-op.
|
||||
if (!feature_flags.enable_cancellation_flag) {
|
||||
EXPECT_TRUE(accept_latch.Await(kWaitDuration).result());
|
||||
EXPECT_TRUE(socket.IsValid());
|
||||
EXPECT_TRUE(wifi_lan_server.StopAcceptingConnections(service_id));
|
||||
EXPECT_TRUE(wifi_lan_server.StopAdvertising(service_id));
|
||||
EXPECT_TRUE(socket_for_server.IsValid());
|
||||
EXPECT_TRUE(socket_for_client.IsValid());
|
||||
} else {
|
||||
EXPECT_FALSE(accept_latch.Await(kWaitDuration).result());
|
||||
EXPECT_FALSE(socket.IsValid());
|
||||
EXPECT_TRUE(wifi_lan_server.StopAcceptingConnections(service_id));
|
||||
EXPECT_TRUE(wifi_lan_server.StopAdvertising(service_id));
|
||||
EXPECT_FALSE(socket_for_server.IsValid());
|
||||
EXPECT_FALSE(socket_for_client.IsValid());
|
||||
}
|
||||
wifi_lan_b.StopDiscovery(service_id);
|
||||
wifi_lan_a.StopAcceptingConnections(service_id);
|
||||
wifi_lan_a.StopAdvertising(service_id);
|
||||
env_.Stop();
|
||||
}
|
||||
|
||||
@@ -181,42 +194,127 @@ TEST_F(WifiLanTest, CanConstructValidObject) {
|
||||
TEST_F(WifiLanTest, CanStartAdvertising) {
|
||||
env_.Start();
|
||||
WifiLan wifi_lan_a;
|
||||
WifiLan wifi_lan_b;
|
||||
std::string service_id(kServiceID);
|
||||
std::string service_info_name{kServiceInfoName};
|
||||
std::string endpoint_info_name{kEndpointName};
|
||||
CountDownLatch found_latch(1);
|
||||
std::string service_info_name(kServiceInfoName);
|
||||
std::string endpoint_info_name(kEndpointName);
|
||||
|
||||
wifi_lan_b.StartDiscovery(
|
||||
service_id, DiscoveredServiceCallback{
|
||||
.service_discovered_cb =
|
||||
[&found_latch](WifiLanService& service,
|
||||
absl::string_view service_id) {
|
||||
found_latch.CountDown();
|
||||
},
|
||||
});
|
||||
EXPECT_TRUE(wifi_lan_a.StartAcceptingConnections(service_id, {}));
|
||||
|
||||
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(found_latch.Await(kWaitDuration).result());
|
||||
EXPECT_TRUE(wifi_lan_a.StopAdvertising(service_id));
|
||||
EXPECT_TRUE(wifi_lan_b.StopDiscovery(service_id));
|
||||
env_.Stop();
|
||||
}
|
||||
|
||||
TEST_F(WifiLanTest, CanStartMultipleAdvertising) {
|
||||
env_.Start();
|
||||
WifiLan wifi_lan_a;
|
||||
std::string service_id_1(kServiceID);
|
||||
std::string service_id_2("com.google.location.nearby.apps.test_1");
|
||||
std::string service_info_name_1(kServiceInfoName);
|
||||
std::string service_info_name_2("ServiceInfoName_1");
|
||||
std::string endpoint_info_name(kEndpointName);
|
||||
|
||||
EXPECT_TRUE(wifi_lan_a.StartAcceptingConnections(service_id_1, {}));
|
||||
EXPECT_TRUE(wifi_lan_a.StartAcceptingConnections(service_id_2, {}));
|
||||
|
||||
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));
|
||||
EXPECT_TRUE(wifi_lan_a.StopAcceptingConnections(service_id_1));
|
||||
EXPECT_TRUE(wifi_lan_a.StopAcceptingConnections(service_id_2));
|
||||
env_.Stop();
|
||||
}
|
||||
|
||||
TEST_F(WifiLanTest, CanStartDiscovery) {
|
||||
env_.Start();
|
||||
WifiLan 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(WifiLanTest, CanStartMultipleDiscovery) {
|
||||
env_.Start();
|
||||
WifiLan 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(WifiLanTest, CanAdvertiseThatOtherMediumDiscover) {
|
||||
env_.Start();
|
||||
WifiLan wifi_lan_a;
|
||||
WifiLan wifi_lan_b;
|
||||
std::string service_id(kServiceID);
|
||||
std::string service_info_name{kServiceInfoName};
|
||||
std::string endpoint_info_name{kEndpointName};
|
||||
CountDownLatch accept_latch(1);
|
||||
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();
|
||||
},
|
||||
});
|
||||
|
||||
EXPECT_TRUE(wifi_lan_a.StartAcceptingConnections(service_id, {}));
|
||||
|
||||
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(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(WifiLanTest, CanDiscoverThatOtherMediumAdvertise) {
|
||||
env_.Start();
|
||||
WifiLan wifi_lan_a;
|
||||
WifiLan 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);
|
||||
|
||||
EXPECT_TRUE(wifi_lan_b.StartAcceptingConnections(service_id, {}));
|
||||
|
||||
NsdServiceInfo nsd_service_info;
|
||||
nsd_service_info.SetServiceName(service_info_name);
|
||||
nsd_service_info.SetTxtRecord(std::string(kEndpointInfoKey),
|
||||
@@ -224,20 +322,20 @@ TEST_F(WifiLanTest, CanStartDiscovery) {
|
||||
wifi_lan_b.StartAdvertising(service_id, nsd_service_info);
|
||||
|
||||
EXPECT_TRUE(wifi_lan_a.StartDiscovery(
|
||||
service_id, {
|
||||
service_id, DiscoveredServiceCallback{
|
||||
.service_discovered_cb =
|
||||
[&accept_latch](WifiLanService& service,
|
||||
const std::string& service_id) {
|
||||
accept_latch.CountDown();
|
||||
[&discovered_latch](NsdServiceInfo service_info,
|
||||
const std::string& service_id) {
|
||||
discovered_latch.CountDown();
|
||||
},
|
||||
.service_lost_cb =
|
||||
[&lost_latch](WifiLanService& service,
|
||||
[&lost_latch](NsdServiceInfo service_info,
|
||||
const std::string& service_id) {
|
||||
lost_latch.CountDown();
|
||||
},
|
||||
}));
|
||||
EXPECT_TRUE(accept_latch.Await(kWaitDuration).result());
|
||||
wifi_lan_b.StopAdvertising(service_id);
|
||||
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();
|
||||
|
||||
@@ -1,344 +0,0 @@
|
||||
// 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/base/nsd_service_info.h"
|
||||
#include "platform/public/count_down_latch.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::Duration kWaitDuration = absl::Milliseconds(1000);
|
||||
constexpr absl::string_view kServiceID{"com.google.location.nearby.apps.test"};
|
||||
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()};
|
||||
};
|
||||
|
||||
TEST_P(WifiLanV2Test, CanConnect) {
|
||||
FeatureFlags feature_flags = GetParam();
|
||||
env_.SetFeatureFlags(feature_flags);
|
||||
env_.Start();
|
||||
WifiLanV2 wifi_lan_client;
|
||||
WifiLanV2 wifi_lan_server;
|
||||
std::string service_id(kServiceID);
|
||||
std::string service_info_name(kServiceInfoName);
|
||||
std::string endpoint_info_name(kEndpointName);
|
||||
CountDownLatch discovered_latch(1);
|
||||
CountDownLatch accept_latch(1);
|
||||
|
||||
WifiLanSocketV2 socket_for_server;
|
||||
EXPECT_TRUE(wifi_lan_server.StartAcceptingConnections(
|
||||
service_id,
|
||||
{
|
||||
.accepted_cb =
|
||||
[&socket_for_server, &accept_latch](WifiLanSocketV2 socket) {
|
||||
socket_for_server = std::move(socket);
|
||||
accept_latch.CountDown();
|
||||
},
|
||||
}));
|
||||
|
||||
NsdServiceInfo nsd_service_info;
|
||||
nsd_service_info.SetServiceName(service_info_name);
|
||||
nsd_service_info.SetTxtRecord(std::string(kEndpointInfoKey),
|
||||
endpoint_info_name);
|
||||
wifi_lan_server.StartAdvertising(service_id, nsd_service_info);
|
||||
|
||||
NsdServiceInfo discovered_service_info;
|
||||
wifi_lan_client.StartDiscovery(
|
||||
service_id,
|
||||
{
|
||||
.service_discovered_cb =
|
||||
[&discovered_latch, &discovered_service_info](
|
||||
NsdServiceInfo service_info, const std::string& service_id) {
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "Discovered service_info=" << &service_info;
|
||||
discovered_service_info = service_info;
|
||||
discovered_latch.CountDown();
|
||||
},
|
||||
});
|
||||
discovered_latch.Await(kWaitDuration).result();
|
||||
ASSERT_TRUE(discovered_service_info.IsValid());
|
||||
|
||||
CancellationFlag flag;
|
||||
WifiLanSocketV2 socket_for_client =
|
||||
wifi_lan_client.Connect(service_id, discovered_service_info, &flag);
|
||||
EXPECT_TRUE(accept_latch.Await(kWaitDuration).result());
|
||||
EXPECT_TRUE(wifi_lan_server.StopAcceptingConnections(service_id));
|
||||
EXPECT_TRUE(wifi_lan_server.StopAdvertising(service_id));
|
||||
EXPECT_TRUE(socket_for_server.IsValid());
|
||||
EXPECT_TRUE(socket_for_client.IsValid());
|
||||
env_.Stop();
|
||||
}
|
||||
|
||||
TEST_P(WifiLanV2Test, CanCancelConnect) {
|
||||
FeatureFlags feature_flags = GetParam();
|
||||
env_.SetFeatureFlags(feature_flags);
|
||||
env_.Start();
|
||||
WifiLanV2 wifi_lan_client;
|
||||
WifiLanV2 wifi_lan_server;
|
||||
std::string service_id(kServiceID);
|
||||
std::string service_info_name(kServiceInfoName);
|
||||
std::string endpoint_info_name(kEndpointName);
|
||||
CountDownLatch discovered_latch(1);
|
||||
CountDownLatch accept_latch(1);
|
||||
|
||||
WifiLanSocketV2 socket_for_server;
|
||||
EXPECT_TRUE(wifi_lan_server.StartAcceptingConnections(
|
||||
service_id,
|
||||
{
|
||||
.accepted_cb =
|
||||
[&socket_for_server, &accept_latch](WifiLanSocketV2 socket) {
|
||||
socket_for_server = std::move(socket);
|
||||
accept_latch.CountDown();
|
||||
},
|
||||
}));
|
||||
|
||||
NsdServiceInfo nsd_service_info;
|
||||
nsd_service_info.SetServiceName(service_info_name);
|
||||
nsd_service_info.SetTxtRecord(std::string(kEndpointInfoKey),
|
||||
endpoint_info_name);
|
||||
wifi_lan_server.StartAdvertising(service_id, nsd_service_info);
|
||||
|
||||
NsdServiceInfo discovered_service_info;
|
||||
wifi_lan_client.StartDiscovery(
|
||||
service_id,
|
||||
{
|
||||
.service_discovered_cb =
|
||||
[&discovered_latch, &discovered_service_info](
|
||||
NsdServiceInfo service_info, const std::string& service_id) {
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "Discovered service_info=" << &service_info;
|
||||
discovered_service_info = service_info;
|
||||
discovered_latch.CountDown();
|
||||
},
|
||||
});
|
||||
EXPECT_TRUE(discovered_latch.Await(kWaitDuration).result());
|
||||
ASSERT_TRUE(discovered_service_info.IsValid());
|
||||
|
||||
CancellationFlag flag(true);
|
||||
WifiLanSocketV2 socket_for_client =
|
||||
wifi_lan_client.Connect(service_id, discovered_service_info, &flag);
|
||||
// If FeatureFlag is disabled, Cancelled is false as no-op.
|
||||
if (!feature_flags.enable_cancellation_flag) {
|
||||
EXPECT_TRUE(accept_latch.Await(kWaitDuration).result());
|
||||
EXPECT_TRUE(wifi_lan_server.StopAcceptingConnections(service_id));
|
||||
EXPECT_TRUE(wifi_lan_server.StopAdvertising(service_id));
|
||||
EXPECT_TRUE(socket_for_server.IsValid());
|
||||
EXPECT_TRUE(socket_for_client.IsValid());
|
||||
} else {
|
||||
EXPECT_FALSE(accept_latch.Await(kWaitDuration).result());
|
||||
EXPECT_TRUE(wifi_lan_server.StopAcceptingConnections(service_id));
|
||||
EXPECT_TRUE(wifi_lan_server.StopAdvertising(service_id));
|
||||
EXPECT_FALSE(socket_for_server.IsValid());
|
||||
EXPECT_FALSE(socket_for_client.IsValid());
|
||||
}
|
||||
env_.Stop();
|
||||
}
|
||||
|
||||
INSTANTIATE_TEST_SUITE_P(ParametrisedWifiLanTest, WifiLanV2Test,
|
||||
::testing::ValuesIn(kTestCases));
|
||||
|
||||
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);
|
||||
|
||||
EXPECT_TRUE(wifi_lan_a.StartAcceptingConnections(service_id, {}));
|
||||
|
||||
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_1(kServiceID);
|
||||
std::string service_id_2("com.google.location.nearby.apps.test_1");
|
||||
std::string service_info_name_1(kServiceInfoName);
|
||||
std::string service_info_name_2("ServiceInfoName_1");
|
||||
std::string endpoint_info_name(kEndpointName);
|
||||
|
||||
EXPECT_TRUE(wifi_lan_a.StartAcceptingConnections(service_id_1, {}));
|
||||
EXPECT_TRUE(wifi_lan_a.StartAcceptingConnections(service_id_2, {}));
|
||||
|
||||
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();
|
||||
},
|
||||
});
|
||||
|
||||
EXPECT_TRUE(wifi_lan_a.StartAcceptingConnections(service_id, {}));
|
||||
|
||||
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(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);
|
||||
|
||||
EXPECT_TRUE(wifi_lan_b.StartAcceptingConnections(service_id, {}));
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace connections
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
@@ -1,409 +0,0 @@
|
||||
// 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 "core/internal/mediums/wifi_lan_v2.h"
|
||||
|
||||
#include <memory>
|
||||
#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"
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
namespace connections {
|
||||
|
||||
WifiLanV2::~WifiLanV2() {
|
||||
// Destructor is not taking locks, but methods it is calling are.
|
||||
while (!discovering_info_.service_ids.empty()) {
|
||||
StopDiscovery(*discovering_info_.service_ids.begin());
|
||||
}
|
||||
while (!server_sockets_.empty()) {
|
||||
StopAcceptingConnections(server_sockets_.begin()->first);
|
||||
}
|
||||
while (!advertising_info_.nsd_service_infos.empty()) {
|
||||
StopAdvertising(advertising_info_.nsd_service_infos.begin()->first);
|
||||
}
|
||||
|
||||
// All the AcceptLoopRunnable objects in here should already have gotten an
|
||||
// opportunity to shut themselves down cleanly in the calls to
|
||||
// StopAcceptingConnections() above.
|
||||
accept_loops_runner_.Shutdown();
|
||||
}
|
||||
|
||||
bool WifiLanV2::IsAvailable() const {
|
||||
MutexLock lock(&mutex_);
|
||||
|
||||
return IsAvailableLocked();
|
||||
}
|
||||
|
||||
bool WifiLanV2::IsAvailableLocked() const { return medium_.IsValid(); }
|
||||
|
||||
bool WifiLanV2::StartAdvertising(const std::string& service_id,
|
||||
NsdServiceInfo& nsd_service_info) {
|
||||
MutexLock lock(&mutex_);
|
||||
|
||||
if (!IsAvailableLocked()) {
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "Can't turn on WifiLan advertising. WifiLan is not available.";
|
||||
return false;
|
||||
}
|
||||
|
||||
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 (!IsAcceptingConnectionsLocked(service_id)) {
|
||||
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
|
||||
<< ". Should accept connections before advertising.";
|
||||
return false;
|
||||
}
|
||||
|
||||
nsd_service_info.SetServiceType(GenerateServiceType(service_id));
|
||||
const auto& it = server_sockets_.find(service_id);
|
||||
if (it != server_sockets_.end()) {
|
||||
nsd_service_info.SetIPAddress(it->second.GetIPAddress());
|
||||
nsd_service_info.SetPort(it->second.GetPort());
|
||||
}
|
||||
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) {
|
||||
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);
|
||||
}
|
||||
|
||||
bool WifiLanV2::IsAdvertisingLocked(const std::string& service_id) {
|
||||
return advertising_info_.Existed(service_id);
|
||||
}
|
||||
|
||||
bool WifiLanV2::StartDiscovery(const std::string& service_id,
|
||||
DiscoveredServiceCallback callback) {
|
||||
MutexLock lock(&mutex_);
|
||||
|
||||
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_);
|
||||
|
||||
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) {
|
||||
MutexLock lock(&mutex_);
|
||||
return IsDiscoveringLocked(service_id);
|
||||
}
|
||||
|
||||
bool WifiLanV2::IsDiscoveringLocked(const std::string& service_id) {
|
||||
return discovering_info_.Existed(service_id);
|
||||
}
|
||||
|
||||
bool WifiLanV2::StartAcceptingConnections(const std::string& service_id,
|
||||
AcceptedConnectionCallback callback) {
|
||||
MutexLock lock(&mutex_);
|
||||
|
||||
if (service_id.empty()) {
|
||||
NEARBY_LOGS(INFO) << "Refusing to start accepting WifiLan connections; "
|
||||
"service_id is empty.";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!IsAvailableLocked()) {
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "Can't start accepting WifiLan connections [service_id="
|
||||
<< service_id << "]; WifiLan not available.";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (IsAcceptingConnectionsLocked(service_id)) {
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "Refusing to start accepting WifiLan connections [service="
|
||||
<< service_id
|
||||
<< "]; WifiLan server is already in-progress with the same name.";
|
||||
return false;
|
||||
}
|
||||
|
||||
// We can generate an exact port here on server socket; now we just assign 0
|
||||
// to let platform medium decide it.
|
||||
int port = 0;
|
||||
WifiLanServerSocketV2 server_socket = medium_.ListenForService(port);
|
||||
if (!server_socket.IsValid()) {
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "Failed to start accepting WifiLan connections for service_id="
|
||||
<< service_id;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Mark the fact that there's an in-progress WifiLan server accepting
|
||||
// connections.
|
||||
auto owned_server_socket =
|
||||
server_sockets_.insert({service_id, std::move(server_socket)})
|
||||
.first->second;
|
||||
|
||||
// Start the accept loop on a dedicated thread - this stays alive and
|
||||
// listening for new incoming connections until StopAcceptingConnections() is
|
||||
// invoked.
|
||||
accept_loops_runner_.Execute(
|
||||
"wifi-lan-accept",
|
||||
[callback = std::move(callback),
|
||||
server_socket = std::move(owned_server_socket), service_id]() mutable {
|
||||
while (true) {
|
||||
WifiLanSocketV2 client_socket = server_socket.Accept();
|
||||
if (!client_socket.IsValid()) {
|
||||
server_socket.Close();
|
||||
break;
|
||||
}
|
||||
callback.accepted_cb(std::move(client_socket));
|
||||
}
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WifiLanV2::StopAcceptingConnections(const std::string& service_id) {
|
||||
MutexLock lock(&mutex_);
|
||||
|
||||
if (service_id.empty()) {
|
||||
NEARBY_LOGS(INFO) << "Unable to stop accepting WifiLan connections because "
|
||||
"the service_id is empty.";
|
||||
return false;
|
||||
}
|
||||
|
||||
const auto& it = server_sockets_.find(service_id);
|
||||
if (it == server_sockets_.end()) {
|
||||
NEARBY_LOGS(INFO) << "Can't stop accepting WifiLan connections for "
|
||||
<< service_id << " because it was never started.";
|
||||
return false;
|
||||
}
|
||||
|
||||
// Closing the WifiLanServerSocket will kick off the suicide of the thread
|
||||
// in accept_loops_thread_pool_ that blocks on WifiLanServerSocket.accept().
|
||||
// That may take some time to complete, but there's no particular reason to
|
||||
// wait around for it.
|
||||
auto item = server_sockets_.extract(it);
|
||||
|
||||
// Store a handle to the WifiLanServerSocket, so we can use it after
|
||||
// removing the entry from server_sockets_; making it scoped
|
||||
// is a bonus that takes care of deallocation before we leave this method.
|
||||
WifiLanServerSocketV2& listening_socket = item.mapped();
|
||||
|
||||
// Regardless of whether or not we fail to close the existing
|
||||
// WifiLanServerSocket, remove it from server_sockets_ so that it
|
||||
// frees up this service for another round.
|
||||
|
||||
// Finally, close the WifiLanServerSocket.
|
||||
if (!listening_socket.Close().Ok()) {
|
||||
NEARBY_LOGS(INFO) << "Failed to close WifiLan server socket for service_id="
|
||||
<< service_id;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WifiLanV2::IsAcceptingConnections(const std::string& service_id) {
|
||||
MutexLock lock(&mutex_);
|
||||
return IsAcceptingConnectionsLocked(service_id);
|
||||
}
|
||||
|
||||
bool WifiLanV2::IsAcceptingConnectionsLocked(const std::string& service_id) {
|
||||
return server_sockets_.find(service_id) != server_sockets_.end();
|
||||
}
|
||||
|
||||
WifiLanSocketV2 WifiLanV2::Connect(const std::string& service_id,
|
||||
const NsdServiceInfo& service_info,
|
||||
CancellationFlag* cancellation_flag) {
|
||||
MutexLock lock(&mutex_);
|
||||
// Socket to return. To allow for NRVO to work, it has to be a single object.
|
||||
WifiLanSocketV2 socket;
|
||||
|
||||
if (service_id.empty()) {
|
||||
NEARBY_LOGS(INFO) << "Refusing to create client WifiLan socket because "
|
||||
"service_id is empty.";
|
||||
return socket;
|
||||
}
|
||||
|
||||
if (!IsAvailableLocked()) {
|
||||
NEARBY_LOGS(INFO) << "Can't create client WifiLan socket [service_id="
|
||||
<< service_id << "]; WifiLan isn't available.";
|
||||
return socket;
|
||||
}
|
||||
|
||||
if (cancellation_flag->Cancelled()) {
|
||||
NEARBY_LOGS(INFO) << "Can't create client WifiLan socket due to cancel.";
|
||||
return socket;
|
||||
}
|
||||
|
||||
socket = medium_.ConnectToService(service_info, cancellation_flag);
|
||||
if (!socket.IsValid()) {
|
||||
NEARBY_LOGS(INFO) << "Failed to Connect via WifiLan [service_id="
|
||||
<< service_id << "]";
|
||||
}
|
||||
|
||||
return socket;
|
||||
}
|
||||
|
||||
WifiLanSocketV2 WifiLanV2::Connect(const std::string& service_id,
|
||||
const std::string& ip_address, int port,
|
||||
CancellationFlag* cancellation_flag) {
|
||||
MutexLock lock(&mutex_);
|
||||
// Socket to return. To allow for NRVO to work, it has to be a single object.
|
||||
WifiLanSocketV2 socket;
|
||||
|
||||
if (service_id.empty()) {
|
||||
NEARBY_LOGS(INFO) << "Refusing to create client WifiLan socket because "
|
||||
"service_id is empty.";
|
||||
return socket;
|
||||
}
|
||||
|
||||
if (!IsAvailableLocked()) {
|
||||
NEARBY_LOGS(INFO) << "Can't create client WifiLan socket [service_id="
|
||||
<< service_id << "]; WifiLan isn't available.";
|
||||
return socket;
|
||||
}
|
||||
|
||||
if (cancellation_flag->Cancelled()) {
|
||||
NEARBY_LOGS(INFO) << "Can't create client WifiLan socket due to cancel.";
|
||||
return socket;
|
||||
}
|
||||
|
||||
socket = medium_.ConnectToService(ip_address, port, cancellation_flag);
|
||||
if (!socket.IsValid()) {
|
||||
NEARBY_LOGS(INFO) << "Failed to Connect via WifiLan [service_id="
|
||||
<< service_id << "]";
|
||||
}
|
||||
|
||||
return socket;
|
||||
}
|
||||
|
||||
std::pair<std::string, int> WifiLanV2::GetCredentials(
|
||||
const std::string& service_id) {
|
||||
MutexLock lock(&mutex_);
|
||||
const auto& it = server_sockets_.find(service_id);
|
||||
if (it == server_sockets_.end()) {
|
||||
return std::pair<std::string, int>();
|
||||
}
|
||||
return std::pair<std::string, int>(it->second.GetIPAddress(),
|
||||
it->second.GetPort());
|
||||
}
|
||||
|
||||
std::string WifiLanV2::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
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
@@ -1,194 +0,0 @@
|
||||
// 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.
|
||||
|
||||
#ifndef CORE_INTERNAL_MEDIUMS_WIFI_LAN_V2_H_
|
||||
#define CORE_INTERNAL_MEDIUMS_WIFI_LAN_V2_H_
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
|
||||
#include "absl/container/flat_hash_map.h"
|
||||
#include "absl/container/flat_hash_set.h"
|
||||
#include "platform/base/byte_array.h"
|
||||
#include "platform/base/cancellation_flag.h"
|
||||
#include "platform/base/nsd_service_info.h"
|
||||
#include "platform/public/multi_thread_executor.h"
|
||||
#include "platform/public/mutex.h"
|
||||
#include "platform/public/wifi_lan_v2.h"
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
namespace connections {
|
||||
|
||||
class WifiLanV2 {
|
||||
public:
|
||||
using DiscoveredServiceCallback = WifiLanMediumV2::DiscoveredServiceCallback;
|
||||
|
||||
// Callback that is invoked when a new connection is accepted.
|
||||
struct AcceptedConnectionCallback {
|
||||
std::function<void(WifiLanSocketV2 socket)> accepted_cb =
|
||||
DefaultCallback<WifiLanSocketV2>();
|
||||
};
|
||||
|
||||
WifiLanV2() = default;
|
||||
~WifiLanV2();
|
||||
|
||||
// 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 in NsdServiceInfo and
|
||||
// then enables WifiLan advertising.
|
||||
// Returns true, if NsdServiceInfo is successfully set, and false otherwise.
|
||||
bool StartAdvertising(const std::string& service_id,
|
||||
NsdServiceInfo& nsd_service_info)
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
// Disables WifiLan advertising.
|
||||
// Returns false if no successful call StartAdvertising() was previously
|
||||
// made, otherwise returns true.
|
||||
bool StopAdvertising(const std::string& service_id)
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
bool IsAdvertising(const std::string& service_id) ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
// Enables WifiLan discovery. Will report any discoverable services
|
||||
// through a callback.
|
||||
// Returns true, if discovery was enabled, false otherwise.
|
||||
bool StartDiscovery(const std::string& service_id,
|
||||
DiscoveredServiceCallback callback)
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
// Disables WifiLan discovery.
|
||||
bool StopDiscovery(const std::string& service_id) ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
bool IsDiscovering(const std::string& service_id) ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
// Starts a worker thread, creates a WifiLan socket, associates it with a
|
||||
// service id.
|
||||
bool StartAcceptingConnections(const std::string& service_id,
|
||||
AcceptedConnectionCallback callback)
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
// Closes socket corresponding to a service id.
|
||||
bool StopAcceptingConnections(const std::string& service_id)
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
bool IsAcceptingConnections(const std::string& service_id)
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
// Establishes connection to WifiLan service that was might be started on
|
||||
// another service with StartAcceptingConnections() using the same service_id.
|
||||
// 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,
|
||||
const NsdServiceInfo& service_info,
|
||||
CancellationFlag* cancellation_flag)
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
// Establishes connection to WifiLan service by ip address and port for
|
||||
// bandwidth upgradation.
|
||||
// Returns socket instance. On success, WifiLanSocket.IsValid() return true.
|
||||
WifiLanSocketV2 Connect(const std::string& service_id,
|
||||
const std::string& ip_address, int port,
|
||||
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_);
|
||||
|
||||
private:
|
||||
struct AdvertisingInfo {
|
||||
bool Empty() const { return nsd_service_infos.empty(); }
|
||||
void Clear() { nsd_service_infos.clear(); }
|
||||
void Add(const std::string& service_id,
|
||||
const NsdServiceInfo& nsd_service_info) {
|
||||
nsd_service_infos.insert({service_id, nsd_service_info});
|
||||
}
|
||||
void Remove(const std::string& service_id) {
|
||||
nsd_service_infos.erase(service_id);
|
||||
}
|
||||
bool Existed(const std::string& service_id) const {
|
||||
return nsd_service_infos.contains(service_id);
|
||||
}
|
||||
NsdServiceInfo* GetServiceInfo(const std::string& service_id) {
|
||||
const auto& it = nsd_service_infos.find(service_id);
|
||||
if (it == nsd_service_infos.end()) {
|
||||
return nullptr;
|
||||
}
|
||||
return &it->second;
|
||||
}
|
||||
|
||||
absl::flat_hash_map<std::string, NsdServiceInfo> nsd_service_infos;
|
||||
};
|
||||
|
||||
struct DiscoveringInfo {
|
||||
bool Empty() const { return service_ids.empty(); }
|
||||
void Clear() { service_ids.clear(); }
|
||||
void Add(const std::string& service_id) { service_ids.insert(service_id); }
|
||||
void Remove(const std::string& service_id) {
|
||||
service_ids.erase(service_id);
|
||||
}
|
||||
bool Existed(const std::string& service_id) const {
|
||||
return service_ids.contains(service_id);
|
||||
}
|
||||
|
||||
absl::flat_hash_set<std::string> service_ids;
|
||||
};
|
||||
|
||||
static constexpr int kMaxConcurrentAcceptLoops = 5;
|
||||
|
||||
// Same as IsAvailable(), but must be called with mutex_ held.
|
||||
bool IsAvailableLocked() const ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
|
||||
|
||||
// Same as IsAdvertising(), but must be called with mutex_ held.
|
||||
bool IsAdvertisingLocked(const std::string& service_id)
|
||||
ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
|
||||
|
||||
// Same as IsDiscovering(), but must be called with mutex_ held.
|
||||
bool IsDiscoveringLocked(const std::string& service_id)
|
||||
ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
|
||||
|
||||
// Same as IsAcceptingConnections(), but must be called with mutex_ held.
|
||||
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_;
|
||||
WifiLanMediumV2 medium_ ABSL_GUARDED_BY(mutex_);
|
||||
AdvertisingInfo advertising_info_ ABSL_GUARDED_BY(mutex_);
|
||||
DiscoveringInfo discovering_info_ ABSL_GUARDED_BY(mutex_);
|
||||
|
||||
// A thread pool dedicated to running all the accept loops from
|
||||
// StartAcceptingConnections().
|
||||
MultiThreadExecutor accept_loops_runner_{kMaxConcurrentAcceptLoops};
|
||||
|
||||
// A map of service_id -> ServerSocket. If map is non-empty, we
|
||||
// are currently listening for incoming connections.
|
||||
// WifiLanServerSocket instances are used from accept_loops_runner_,
|
||||
// and thus require pointer stability.
|
||||
absl::flat_hash_map<std::string, WifiLanServerSocketV2> server_sockets_
|
||||
ABSL_GUARDED_BY(mutex_);
|
||||
};
|
||||
|
||||
} // namespace connections
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
|
||||
#endif // CORE_INTERNAL_MEDIUMS_WIFI_LAN_H_
|
||||
@@ -25,7 +25,6 @@
|
||||
#include "core/internal/mediums/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"
|
||||
@@ -60,7 +59,6 @@ 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) {}
|
||||
|
||||
@@ -70,9 +68,6 @@ 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::MDNS);
|
||||
}
|
||||
if (wifi_lan_medium_.IsAvailable()) {
|
||||
mediums.push_back(proto::connections::WIFI_LAN);
|
||||
}
|
||||
@@ -100,17 +95,6 @@ BasePcpHandler::StartOperationResult P2pClusterPcpHandler::StartAdvertisingImpl(
|
||||
|
||||
WebRtcState web_rtc_state{WebRtcState::kUnconnectable};
|
||||
|
||||
if (options.allowed.wifi_lan_v2) {
|
||||
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,
|
||||
@@ -184,10 +168,6 @@ 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};
|
||||
}
|
||||
|
||||
@@ -555,126 +535,6 @@ void P2pClusterPcpHandler::BlePeripheralLostHandler(
|
||||
}
|
||||
|
||||
bool P2pClusterPcpHandler::IsRecognizedWifiLanEndpoint(
|
||||
const std::string& service_id,
|
||||
const WifiLanServiceInfo& service_info) const {
|
||||
if (!service_info.IsValid()) {
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "WifiLanServiceInfo doesn't conform to the format, discarding.";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (service_info.GetPcp() != GetPcp()) {
|
||||
NEARBY_LOGS(INFO) << "WifiLanServiceInfo doesn't match on Pcp; expected "
|
||||
<< PcpToStrategy(GetPcp()).GetName() << ", found "
|
||||
<< PcpToStrategy(service_info.GetPcp()).GetName();
|
||||
return false;
|
||||
}
|
||||
|
||||
ByteArray expected_service_id_hash =
|
||||
GenerateHash(service_id, WifiLanServiceInfo::kServiceIdHashLength);
|
||||
|
||||
if (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(service_info.GetServiceIdHash().data());
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void P2pClusterPcpHandler::WifiLanServiceDiscoveredHandler(
|
||||
ClientProxy* client, WifiLanService& wifi_lan_service,
|
||||
const std::string& service_id) {
|
||||
RunOnPcpHandlerThread(
|
||||
"p2p-wifi-service-discovered",
|
||||
[this, client, service_id, &wifi_lan_service]()
|
||||
RUN_ON_PCP_HANDLER_THREAD() {
|
||||
// Make sure we are still discovering before proceeding.
|
||||
if (!client->IsDiscovering()) {
|
||||
NEARBY_LOGS(WARNING)
|
||||
<< "Skipping discovery of NsdServiceInfo "
|
||||
<< wifi_lan_service.GetServiceInfo().GetServiceName()
|
||||
<< " because we are no longer discovering.";
|
||||
return;
|
||||
}
|
||||
|
||||
// Parse the WifiLanServiceInfo.
|
||||
WifiLanServiceInfo service_info(wifi_lan_service.GetServiceInfo());
|
||||
|
||||
// Make sure the WifiLan service name points to a valid
|
||||
// endpoint we're discovering.
|
||||
if (!IsRecognizedWifiLanEndpoint(service_id, service_info)) return;
|
||||
|
||||
// Report the discovered endpoint to the client.
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "Found NsdServiceInfo "
|
||||
<< wifi_lan_service.GetServiceInfo().GetServiceName()
|
||||
<< " (with endpoint_id=" << service_info.GetEndpointId()
|
||||
<< "and endpoint_info="
|
||||
<< absl::BytesToHexString(service_info.GetEndpointInfo().data())
|
||||
<< ").";
|
||||
OnEndpointFound(client,
|
||||
std::make_shared<WifiLanEndpoint>(WifiLanEndpoint{
|
||||
{
|
||||
service_info.GetEndpointId(),
|
||||
service_info.GetEndpointInfo(),
|
||||
service_id,
|
||||
proto::connections::Medium::WIFI_LAN,
|
||||
service_info.GetWebRtcState(),
|
||||
},
|
||||
wifi_lan_service,
|
||||
}));
|
||||
});
|
||||
}
|
||||
|
||||
void P2pClusterPcpHandler::WifiLanServiceLostHandler(
|
||||
ClientProxy* client, WifiLanService& wifi_lan_service,
|
||||
const std::string& service_id) {
|
||||
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.GetServiceName().c_str());
|
||||
RunOnPcpHandlerThread(
|
||||
"p2p-wifi-service-lost",
|
||||
[this, client, service_id, nsd_service_info]()
|
||||
RUN_ON_PCP_HANDLER_THREAD() {
|
||||
// Make sure we are still discovering before proceeding.
|
||||
if (!client->IsDiscovering()) {
|
||||
NEARBY_LOGS(WARNING) << "Ignoring lost NsdServiceInfo "
|
||||
<< nsd_service_info.GetServiceName()
|
||||
<< " because we are no longer "
|
||||
"discovering.";
|
||||
return;
|
||||
}
|
||||
|
||||
// Parse the WifiLanServiceInfo.
|
||||
WifiLanServiceInfo service_info(nsd_service_info);
|
||||
|
||||
// Make sure the WifiLan service name points to a valid
|
||||
// endpoint we're discovering.
|
||||
if (!IsRecognizedWifiLanEndpoint(service_id, service_info)) return;
|
||||
|
||||
// Report the lost endpoint to the client.
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "Lost NsdServiceInfo " << nsd_service_info.GetServiceName()
|
||||
<< " (with endpoint_id=" << service_info.GetEndpointId()
|
||||
<< " and endpoint_info="
|
||||
<< absl::BytesToHexString(service_info.GetEndpointInfo().data())
|
||||
<< ").";
|
||||
OnEndpointLost(client, DiscoveredEndpoint{
|
||||
service_info.GetEndpointId(),
|
||||
service_info.GetEndpointInfo(),
|
||||
service_id,
|
||||
proto::connections::Medium::WIFI_LAN,
|
||||
WebRtcState::kUndefined,
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
bool P2pClusterPcpHandler::IsRecognizedWifiLanV2Endpoint(
|
||||
const std::string& service_id,
|
||||
const WifiLanServiceInfo& wifi_lan_service_info) const {
|
||||
if (!wifi_lan_service_info.IsValid()) {
|
||||
@@ -707,7 +567,7 @@ bool P2pClusterPcpHandler::IsRecognizedWifiLanV2Endpoint(
|
||||
return true;
|
||||
}
|
||||
|
||||
void P2pClusterPcpHandler::WifiLanV2ServiceDiscoveredHandler(
|
||||
void P2pClusterPcpHandler::WifiLanServiceDiscoveredHandler(
|
||||
ClientProxy* client, NsdServiceInfo service_info,
|
||||
const std::string& service_id) {
|
||||
RunOnPcpHandlerThread(
|
||||
@@ -725,7 +585,7 @@ void P2pClusterPcpHandler::WifiLanV2ServiceDiscoveredHandler(
|
||||
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)) {
|
||||
if (!IsRecognizedWifiLanEndpoint(service_id, wifi_lan_service_info)) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -739,12 +599,12 @@ void P2pClusterPcpHandler::WifiLanV2ServiceDiscoveredHandler(
|
||||
wifi_lan_service_info.GetEndpointInfo().data())
|
||||
<< ").";
|
||||
OnEndpointFound(client,
|
||||
std::make_shared<WifiLanV2Endpoint>(WifiLanV2Endpoint{
|
||||
std::make_shared<WifiLanEndpoint>(WifiLanEndpoint{
|
||||
{
|
||||
wifi_lan_service_info.GetEndpointId(),
|
||||
wifi_lan_service_info.GetEndpointInfo(),
|
||||
service_id,
|
||||
proto::connections::Medium::MDNS,
|
||||
proto::connections::Medium::WIFI_LAN,
|
||||
wifi_lan_service_info.GetWebRtcState(),
|
||||
},
|
||||
service_info,
|
||||
@@ -752,7 +612,7 @@ void P2pClusterPcpHandler::WifiLanV2ServiceDiscoveredHandler(
|
||||
});
|
||||
}
|
||||
|
||||
void P2pClusterPcpHandler::WifiLanV2ServiceLostHandler(
|
||||
void P2pClusterPcpHandler::WifiLanServiceLostHandler(
|
||||
ClientProxy* client, NsdServiceInfo service_info,
|
||||
const std::string& service_id) {
|
||||
NEARBY_LOGS(INFO) << "WifiLan: [LOST, SCHED] service_info=" << &service_info
|
||||
@@ -774,7 +634,7 @@ void P2pClusterPcpHandler::WifiLanV2ServiceLostHandler(
|
||||
|
||||
// Make sure the WifiLan service name points to a valid
|
||||
// endpoint we're discovering.
|
||||
if (!IsRecognizedWifiLanV2Endpoint(service_id, wifi_lan_service_info))
|
||||
if (!IsRecognizedWifiLanEndpoint(service_id, wifi_lan_service_info))
|
||||
return;
|
||||
|
||||
// Report the lost endpoint to the client.
|
||||
@@ -808,24 +668,6 @@ BasePcpHandler::StartOperationResult P2pClusterPcpHandler::StartDiscoveryImpl(
|
||||
|
||||
std::vector<proto::connections::Medium> mediums_started_successfully;
|
||||
|
||||
if (options.allowed.wifi_lan_v2) {
|
||||
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(
|
||||
{
|
||||
@@ -899,7 +741,6 @@ BasePcpHandler::StartOperationResult P2pClusterPcpHandler::StartDiscoveryImpl(
|
||||
|
||||
Status P2pClusterPcpHandler::StopDiscoveryImpl(ClientProxy* client) {
|
||||
wifi_lan_medium_.StopDiscovery(client->GetDiscoveryServiceId());
|
||||
wifi_lan_medium_v2_.StopDiscovery(client->GetDiscoveryServiceId());
|
||||
if (client->GetClientId() == bluetooth_classic_discoverer_client_id_) {
|
||||
bluetooth_medium_.StopDiscovery();
|
||||
bluetooth_classic_discoverer_client_id_ = 0;
|
||||
@@ -969,13 +810,6 @@ BasePcpHandler::ConnectImplResult P2pClusterPcpHandler::ConnectImpl(
|
||||
}
|
||||
break;
|
||||
}
|
||||
case proto::connections::Medium::MDNS: {
|
||||
auto* wifi_lan_endpoint = down_cast<WifiLanV2Endpoint*>(endpoint);
|
||||
if (wifi_lan_endpoint) {
|
||||
return WifiLanV2ConnectImpl(client, wifi_lan_endpoint);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case proto::connections::Medium::WEB_RTC: {
|
||||
break;
|
||||
}
|
||||
@@ -1376,14 +1210,13 @@ proto::connections::Medium P2pClusterPcpHandler::StartWifiLanAdvertising(
|
||||
WebRtcState web_rtc_state) {
|
||||
// Start listening for connections before advertising in case a connection
|
||||
// request comes in very quickly.
|
||||
NEARBY_LOG(INFO,
|
||||
"P2pClusterPcpHandler::StartWifiLanAdvertising: service=%s: start",
|
||||
service_id.c_str());
|
||||
NEARBY_LOGS(INFO) << "P2pClusterPcpHandler::StartWifiLanAdvertising: service="
|
||||
<< service_id << ": start";
|
||||
if (!wifi_lan_medium_.IsAcceptingConnections(service_id)) {
|
||||
if (!wifi_lan_medium_.StartAcceptingConnections(
|
||||
service_id, {.accepted_cb = [this, client, local_endpoint_info](
|
||||
WifiLanSocket socket,
|
||||
const std::string& service_id) {
|
||||
service_id,
|
||||
{.accepted_cb = [this, client, local_endpoint_info,
|
||||
local_endpoint_id](WifiLanSocket socket) {
|
||||
if (!socket.IsValid()) {
|
||||
NEARBY_LOGS(WARNING)
|
||||
<< "Invalid socket in accept callback("
|
||||
@@ -1393,22 +1226,18 @@ proto::connections::Medium P2pClusterPcpHandler::StartWifiLanAdvertising(
|
||||
}
|
||||
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 =
|
||||
socket.GetRemoteWifiLanService()
|
||||
.GetServiceInfo()
|
||||
.GetServiceName();
|
||||
auto channel =
|
||||
absl::make_unique<WifiLanEndpointChannel>(
|
||||
remote_service_info_name, socket);
|
||||
ByteArray remote_service_info{remote_service_info_name};
|
||||
[this, client, local_endpoint_id, local_endpoint_info,
|
||||
socket = std::move(
|
||||
socket)]() RUN_ON_PCP_HANDLER_THREAD() mutable {
|
||||
std::string remote_service_name = local_endpoint_id;
|
||||
auto channel = absl::make_unique<WifiLanEndpointChannel>(
|
||||
remote_service_name, socket);
|
||||
ByteArray remote_service_name_byte{remote_service_name};
|
||||
|
||||
OnIncomingConnection(
|
||||
client, remote_service_info, std::move(channel),
|
||||
proto::connections::Medium::WIFI_LAN);
|
||||
});
|
||||
OnIncomingConnection(client, remote_service_name_byte,
|
||||
std::move(channel),
|
||||
proto::connections::Medium::WIFI_LAN);
|
||||
});
|
||||
}})) {
|
||||
NEARBY_LOGS(WARNING)
|
||||
<< "In StartWifiLanAdvertising("
|
||||
@@ -1419,12 +1248,12 @@ proto::connections::Medium P2pClusterPcpHandler::StartWifiLanAdvertising(
|
||||
<< 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;
|
||||
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.
|
||||
@@ -1438,7 +1267,7 @@ proto::connections::Medium P2pClusterPcpHandler::StartWifiLanAdvertising(
|
||||
local_endpoint_info,
|
||||
ByteArray{},
|
||||
web_rtc_state};
|
||||
NsdServiceInfo nsd_service_info{service_info};
|
||||
NsdServiceInfo nsd_service_info(service_info);
|
||||
if (!nsd_service_info.IsValid()) {
|
||||
NEARBY_LOGS(WARNING) << "In StartWifiLanAdvertising("
|
||||
<< absl::BytesToHexString(local_endpoint_info.data())
|
||||
@@ -1499,165 +1328,10 @@ proto::connections::Medium P2pClusterPcpHandler::StartWifiLanDiscovery(
|
||||
|
||||
BasePcpHandler::ConnectImplResult P2pClusterPcpHandler::WifiLanConnectImpl(
|
||||
ClientProxy* client, WifiLanEndpoint* endpoint) {
|
||||
NEARBY_LOGS(VERBOSE) << "Client " << client->GetClientId()
|
||||
<< " is attempting to connect to endpoint(id="
|
||||
<< endpoint->endpoint_id << ") over WifiLan.";
|
||||
WifiLanService& wifi_lan_service = endpoint->wifi_lan_service;
|
||||
|
||||
WifiLanSocket wifi_lan_socket = wifi_lan_medium_.Connect(
|
||||
wifi_lan_service, endpoint->service_id,
|
||||
client->GetCancellationFlag(endpoint->endpoint_id));
|
||||
if (!wifi_lan_socket.IsValid()) {
|
||||
NEARBY_LOGS(ERROR)
|
||||
<< "In WifiLanConnectImpl(), failed to connect to service "
|
||||
<< wifi_lan_service.GetServiceInfo().GetServiceName()
|
||||
<< " for endpoint(id=" << endpoint->endpoint_id << ").";
|
||||
return BasePcpHandler::ConnectImplResult{
|
||||
.status = {Status::kWifiLanError},
|
||||
};
|
||||
}
|
||||
|
||||
auto channel = absl::make_unique<WifiLanEndpointChannel>(
|
||||
endpoint->endpoint_id, wifi_lan_socket);
|
||||
NEARBY_LOGS(VERBOSE) << "Client " << client->GetClientId()
|
||||
<< " created WifiLan endpoint channel to endpoint(id="
|
||||
<< endpoint->endpoint_id << ").";
|
||||
return BasePcpHandler::ConnectImplResult{
|
||||
.medium = proto::connections::Medium::WIFI_LAN,
|
||||
.status = {Status::kSuccess},
|
||||
.endpoint_channel = std::move(channel),
|
||||
};
|
||||
}
|
||||
|
||||
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,
|
||||
local_endpoint_id](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_id, local_endpoint_info,
|
||||
socket = std::move(socket)]()
|
||||
RUN_ON_PCP_HANDLER_THREAD() mutable {
|
||||
std::string remote_service_name = local_endpoint_id;
|
||||
auto channel =
|
||||
absl::make_unique<WifiLanEndpointChannelV2>(
|
||||
remote_service_name, socket);
|
||||
ByteArray remote_service_name_byte{remote_service_name};
|
||||
|
||||
OnIncomingConnection(client, remote_service_name_byte,
|
||||
std::move(channel),
|
||||
proto::connections::Medium::MDNS);
|
||||
});
|
||||
}})) {
|
||||
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::MDNS;
|
||||
}
|
||||
|
||||
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::MDNS;
|
||||
} 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;
|
||||
}
|
||||
}
|
||||
|
||||
BasePcpHandler::ConnectImplResult P2pClusterPcpHandler::WifiLanV2ConnectImpl(
|
||||
ClientProxy* client, WifiLanV2Endpoint* endpoint) {
|
||||
NEARBY_LOGS(INFO) << "Client " << client->GetClientId()
|
||||
<< " is attempting to connect to endpoint(id="
|
||||
<< endpoint->endpoint_id << ") over WifiLan.";
|
||||
WifiLanSocketV2 socket = wifi_lan_medium_v2_.Connect(
|
||||
WifiLanSocket socket = wifi_lan_medium_.Connect(
|
||||
endpoint->service_id, endpoint->service_info,
|
||||
client->GetCancellationFlag(endpoint->endpoint_id));
|
||||
NEARBY_LOGS(ERROR) << "In WifiLanConnectImpl(), connect to service "
|
||||
@@ -1673,13 +1347,13 @@ BasePcpHandler::ConnectImplResult P2pClusterPcpHandler::WifiLanV2ConnectImpl(
|
||||
};
|
||||
}
|
||||
|
||||
auto channel = absl::make_unique<WifiLanEndpointChannelV2>(
|
||||
endpoint->endpoint_id, socket);
|
||||
auto channel =
|
||||
absl::make_unique<WifiLanEndpointChannel>(endpoint->endpoint_id, socket);
|
||||
NEARBY_LOGS(INFO) << "Client " << client->GetClientId()
|
||||
<< " created WifiLan endpoint channel to endpoint(id="
|
||||
<< endpoint->endpoint_id << ").";
|
||||
return BasePcpHandler::ConnectImplResult{
|
||||
.medium = proto::connections::Medium::MDNS,
|
||||
.medium = proto::connections::Medium::WIFI_LAN,
|
||||
.status = {Status::kSuccess},
|
||||
.endpoint_channel = std::move(channel),
|
||||
};
|
||||
|
||||
@@ -108,8 +108,6 @@ 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;
|
||||
@@ -173,11 +171,12 @@ class P2pClusterPcpHandler : public BasePcpHandler {
|
||||
// WifiLan
|
||||
bool IsRecognizedWifiLanEndpoint(
|
||||
const std::string& service_id,
|
||||
const WifiLanServiceInfo& service_info) const;
|
||||
const WifiLanServiceInfo& wifi_lan_service_info) const;
|
||||
void WifiLanServiceDiscoveredHandler(ClientProxy* client,
|
||||
WifiLanService& service,
|
||||
NsdServiceInfo service_info,
|
||||
const std::string& service_id);
|
||||
void WifiLanServiceLostHandler(ClientProxy* client, WifiLanService& service,
|
||||
void WifiLanServiceLostHandler(ClientProxy* client,
|
||||
NsdServiceInfo service_info,
|
||||
const std::string& service_id);
|
||||
proto::connections::Medium StartWifiLanAdvertising(
|
||||
ClientProxy* client, const std::string& service_id,
|
||||
@@ -189,31 +188,10 @@ class P2pClusterPcpHandler : public BasePcpHandler {
|
||||
BasePcpHandler::ConnectImplResult WifiLanConnectImpl(
|
||||
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);
|
||||
BasePcpHandler::ConnectImplResult WifiLanV2ConnectImpl(
|
||||
ClientProxy* client, WifiLanV2Endpoint* endpoint);
|
||||
|
||||
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};
|
||||
|
||||
@@ -46,30 +46,6 @@ ByteArray WifiLanBwuHandler::InitializeUpgradedMediumForEndpoint(
|
||||
// stop the advertising yet.
|
||||
std::string upgrade_service_id = Utils::WrapUpgradeServiceId(service_id);
|
||||
|
||||
if (!wifi_lan_medium_.IsAcceptingConnections(upgrade_service_id)) {
|
||||
if (!wifi_lan_medium_.StartAcceptingConnections(
|
||||
upgrade_service_id,
|
||||
{
|
||||
.accepted_cb = absl::bind_front(
|
||||
&WifiLanBwuHandler::OnIncomingWifiLanConnection, this,
|
||||
client),
|
||||
})) {
|
||||
NEARBY_LOG(ERROR,
|
||||
"WifiLanBwuHandler couldn't initiate the WifiLan upgrade for "
|
||||
"endpoint %s because it failed to start listening for "
|
||||
"incoming WifiLan connections.",
|
||||
endpoint_id.c_str());
|
||||
return {};
|
||||
}
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "WifiLanBwuHandler successfully started listening for incoming "
|
||||
"WifiLan connections while upgrading endpoint "
|
||||
<< endpoint_id;
|
||||
}
|
||||
|
||||
// cache service ID to revert
|
||||
active_service_ids_.emplace(upgrade_service_id);
|
||||
|
||||
auto credential = wifi_lan_medium_.GetCredentials(upgrade_service_id);
|
||||
auto ip_address = credential.first;
|
||||
auto port = credential.second;
|
||||
@@ -82,6 +58,31 @@ ByteArray WifiLanBwuHandler::InitializeUpgradedMediumForEndpoint(
|
||||
return {};
|
||||
}
|
||||
|
||||
if (!wifi_lan_medium_.IsAcceptingConnections(upgrade_service_id)) {
|
||||
if (!wifi_lan_medium_.StartAcceptingConnections(
|
||||
upgrade_service_id,
|
||||
{
|
||||
.accepted_cb = absl::bind_front(
|
||||
&WifiLanBwuHandler::OnIncomingWifiLanConnection, this,
|
||||
client, service_id),
|
||||
})) {
|
||||
NEARBY_LOGS(ERROR)
|
||||
<< "WifiLanBwuHandler couldn't initiate the WifiLan upgrade for "
|
||||
"endpoint "
|
||||
<< endpoint_id
|
||||
<< " because it failed to start listening for "
|
||||
"incoming WifiLan connections.";
|
||||
return {};
|
||||
}
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "WifiLanBwuHandler successfully started listening for incoming "
|
||||
"WifiLan connections while upgrading endpoint "
|
||||
<< endpoint_id;
|
||||
}
|
||||
|
||||
// cache service ID to revert
|
||||
active_service_ids_.insert(upgrade_service_id);
|
||||
|
||||
return parser::ForBwuWifiLanPathAvailable(ip_address, port);
|
||||
}
|
||||
|
||||
@@ -103,34 +104,45 @@ WifiLanBwuHandler::CreateUpgradedEndpointChannel(
|
||||
if (!upgrade_path_info.has_wifi_lan_socket()) {
|
||||
return nullptr;
|
||||
}
|
||||
const UpgradePathInfo::WifiLanSocket& wifi_lan_socket =
|
||||
const UpgradePathInfo::WifiLanSocket& upgrade_path_info_socket =
|
||||
upgrade_path_info.wifi_lan_socket();
|
||||
if (!wifi_lan_socket.has_ip_address() || !wifi_lan_socket.has_wifi_port()) {
|
||||
if (!upgrade_path_info_socket.has_ip_address() ||
|
||||
!upgrade_path_info_socket.has_wifi_port()) {
|
||||
NEARBY_LOG(ERROR, "WifiLanBwuHandler failed to parse UpgradePathInfo.");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
const std::string& ip_address = wifi_lan_socket.ip_address();
|
||||
std::int32_t port = wifi_lan_socket.wifi_port();
|
||||
const std::string& ip_address = upgrade_path_info_socket.ip_address();
|
||||
std::int32_t port = upgrade_path_info_socket.wifi_port();
|
||||
|
||||
NEARBY_LOGS(VERBOSE) << "WifiLanBwuHandler is attempting to connect to "
|
||||
"available WifiLan service ("
|
||||
<< ip_address << ":" << port << ") for endpoint "
|
||||
<< endpoint_id;
|
||||
|
||||
WifiLanService wifi_lan_service =
|
||||
wifi_lan_medium_.GetRemoteWifiLanService(ip_address, port);
|
||||
if (!wifi_lan_service.IsValid()) {
|
||||
return nullptr;
|
||||
}
|
||||
WifiLanSocket socket = wifi_lan_medium_.Connect(
|
||||
wifi_lan_service, service_id, client->GetCancellationFlag(endpoint_id));
|
||||
service_id, ip_address, port, client->GetCancellationFlag(endpoint_id));
|
||||
if (!socket.IsValid()) {
|
||||
NEARBY_LOGS(ERROR)
|
||||
<< "WifiLanBwuHandler failed to connect to the WifiLan service ("
|
||||
<< ip_address << ":" << port << ") for endpoint " << endpoint_id;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
NEARBY_LOGS(VERBOSE)
|
||||
<< "WifiLanBwuHandler successfully connected to WifiLan service ("
|
||||
<< ip_address << ":" << port << ") while upgrading endpoint "
|
||||
<< endpoint_id;
|
||||
|
||||
// Create a new WifiLanEndpointChannel.
|
||||
auto channel = std::make_unique<WifiLanEndpointChannel>(service_id, socket);
|
||||
auto channel = absl::make_unique<WifiLanEndpointChannel>(service_id, socket);
|
||||
if (channel == nullptr) {
|
||||
NEARBY_LOGS(ERROR) << "WifiLanBwuHandler failed to create WifiLan endpoint "
|
||||
"channel to the WifiLan service ("
|
||||
<< ip_address << ":" << port << ") for endpoint "
|
||||
<< endpoint_id;
|
||||
socket.Close();
|
||||
NEARBY_LOG(ERROR,
|
||||
"WifiLanBwuHandler failed to create new EndpointChannel for "
|
||||
"outgoing socket %p, aborting upgrade.",
|
||||
&socket.GetImpl());
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return channel;
|
||||
@@ -138,16 +150,14 @@ WifiLanBwuHandler::CreateUpgradedEndpointChannel(
|
||||
|
||||
// Accept Connection Callback.
|
||||
void WifiLanBwuHandler::OnIncomingWifiLanConnection(
|
||||
ClientProxy* client, WifiLanSocket socket,
|
||||
const std::string& upgrade_service_id) {
|
||||
std::string service_id = Utils::UnwrapUpgradeServiceId(upgrade_service_id);
|
||||
auto channel = std::make_unique<WifiLanEndpointChannel>(service_id, socket);
|
||||
auto wifi_lan_socket =
|
||||
std::make_unique<WifiLanIncomingSocket>(service_id, socket);
|
||||
ClientProxy* client, const std::string& service_id, WifiLanSocket socket) {
|
||||
auto channel = absl::make_unique<WifiLanEndpointChannel>(service_id, socket);
|
||||
std::unique_ptr<IncomingSocketConnection> connection(
|
||||
new IncomingSocketConnection{std::move(wifi_lan_socket),
|
||||
std::move(channel)});
|
||||
|
||||
new IncomingSocketConnection{
|
||||
.socket =
|
||||
absl::make_unique<WifiLanIncomingSocket>(service_id, socket),
|
||||
.channel = std::move(channel),
|
||||
});
|
||||
bwu_notifications_.incoming_connection_cb(client, std::move(connection));
|
||||
}
|
||||
|
||||
|
||||
@@ -49,8 +49,9 @@ class WifiLanBwuHandler : public BaseBwuHandler {
|
||||
void OnEndpointDisconnect(ClientProxy* client,
|
||||
const std::string& endpoint_id) override {}
|
||||
|
||||
void OnIncomingWifiLanConnection(ClientProxy* client, WifiLanSocket socket,
|
||||
const std::string& upgrade_service_id);
|
||||
void OnIncomingWifiLanConnection(ClientProxy* client,
|
||||
const std::string& service_id,
|
||||
WifiLanSocket socket);
|
||||
|
||||
class WifiLanIncomingSocket : public BwuHandler::IncomingSocket {
|
||||
public:
|
||||
|
||||
@@ -1,169 +0,0 @@
|
||||
// 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 "core/internal/wifi_lan_bwu_handler_v2.h"
|
||||
|
||||
#include <locale>
|
||||
#include <string>
|
||||
|
||||
#include "absl/functional/bind_front.h"
|
||||
#include "core/internal/client_proxy.h"
|
||||
#include "core/internal/mediums/utils.h"
|
||||
#include "core/internal/offline_frames.h"
|
||||
#include "core/internal/wifi_lan_endpoint_channel_v2.h"
|
||||
#include "platform/public/wifi_lan_v2.h"
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
namespace connections {
|
||||
|
||||
WifiLanV2BwuHandler::WifiLanV2BwuHandler(
|
||||
Mediums& mediums, EndpointChannelManager& channel_manager,
|
||||
BwuNotifications notifications)
|
||||
: BaseBwuHandler(channel_manager, std::move(notifications)),
|
||||
mediums_(mediums) {}
|
||||
|
||||
// Called by BWU initiator. Set up WifiLan upgraded medium for this endpoint,
|
||||
// and returns a upgrade path info (ip address, port) for remote party to
|
||||
// perform discovery.
|
||||
ByteArray WifiLanV2BwuHandler::InitializeUpgradedMediumForEndpoint(
|
||||
ClientProxy* client, const std::string& service_id,
|
||||
const std::string& endpoint_id) {
|
||||
// Use wrapped service ID to avoid have the same ID with the one for
|
||||
// startAdvertising. Otherwise, the listening request would be ignored because
|
||||
// the medium already start accepting the connection because the client not
|
||||
// stop the advertising yet.
|
||||
std::string upgrade_service_id = Utils::WrapUpgradeServiceId(service_id);
|
||||
|
||||
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 "
|
||||
"endpoint "
|
||||
<< endpoint_id
|
||||
<< " because the wifi_lan ip address were unable to be obtained.";
|
||||
return {};
|
||||
}
|
||||
|
||||
if (!wifi_lan_medium_.IsAcceptingConnections(upgrade_service_id)) {
|
||||
if (!wifi_lan_medium_.StartAcceptingConnections(
|
||||
upgrade_service_id,
|
||||
{
|
||||
.accepted_cb = absl::bind_front(
|
||||
&WifiLanV2BwuHandler::OnIncomingWifiLanConnection, this,
|
||||
client, service_id),
|
||||
})) {
|
||||
NEARBY_LOGS(ERROR)
|
||||
<< "WifiLanBwuHandler couldn't initiate the WifiLan upgrade for "
|
||||
"endpoint "
|
||||
<< endpoint_id
|
||||
<< " because it failed to start listening for "
|
||||
"incoming WifiLan connections.";
|
||||
return {};
|
||||
}
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "WifiLanBwuHandler successfully started listening for incoming "
|
||||
"WifiLan connections while upgrading endpoint "
|
||||
<< endpoint_id;
|
||||
}
|
||||
|
||||
// cache service ID to revert
|
||||
active_service_ids_.insert(upgrade_service_id);
|
||||
|
||||
return parser::ForBwuWifiLanPathAvailable(ip_address, port);
|
||||
}
|
||||
|
||||
void WifiLanV2BwuHandler::Revert() {
|
||||
for (const std::string& service_id : active_service_ids_) {
|
||||
wifi_lan_medium_.StopAcceptingConnections(service_id);
|
||||
}
|
||||
active_service_ids_.clear();
|
||||
|
||||
NEARBY_LOG(INFO, "WifiLanBwuHandler successfully reverted all states.");
|
||||
}
|
||||
|
||||
// Called by BWU target. Retrieves a new medium info from incoming message,
|
||||
// and establishes connection over WifiLan using this info.
|
||||
std::unique_ptr<EndpointChannel>
|
||||
WifiLanV2BwuHandler::CreateUpgradedEndpointChannel(
|
||||
ClientProxy* client, const std::string& service_id,
|
||||
const std::string& endpoint_id, const UpgradePathInfo& upgrade_path_info) {
|
||||
if (!upgrade_path_info.has_wifi_lan_socket()) {
|
||||
return nullptr;
|
||||
}
|
||||
const UpgradePathInfo::WifiLanSocket& upgrade_path_info_socket =
|
||||
upgrade_path_info.wifi_lan_socket();
|
||||
if (!upgrade_path_info_socket.has_ip_address() ||
|
||||
!upgrade_path_info_socket.has_wifi_port()) {
|
||||
NEARBY_LOG(ERROR, "WifiLanBwuHandler failed to parse UpgradePathInfo.");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
const std::string& ip_address = upgrade_path_info_socket.ip_address();
|
||||
std::int32_t port = upgrade_path_info_socket.wifi_port();
|
||||
|
||||
NEARBY_LOGS(VERBOSE) << "WifiLanBwuHandler is attempting to connect to "
|
||||
"available WifiLan service ("
|
||||
<< ip_address << ":" << port << ") for endpoint "
|
||||
<< endpoint_id;
|
||||
|
||||
WifiLanSocketV2 socket = wifi_lan_medium_.Connect(
|
||||
service_id, ip_address, port, client->GetCancellationFlag(endpoint_id));
|
||||
if (!socket.IsValid()) {
|
||||
NEARBY_LOGS(ERROR)
|
||||
<< "WifiLanBwuHandler failed to connect to the WifiLan service ("
|
||||
<< ip_address << ":" << port << ") for endpoint " << endpoint_id;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
NEARBY_LOGS(VERBOSE)
|
||||
<< "WifiLanBwuHandler successfully connected to WifiLan service ("
|
||||
<< ip_address << ":" << port << ") while upgrading endpoint "
|
||||
<< endpoint_id;
|
||||
|
||||
// Create a new WifiLanEndpointChannel.
|
||||
auto channel =
|
||||
absl::make_unique<WifiLanEndpointChannelV2>(service_id, socket);
|
||||
if (channel == nullptr) {
|
||||
NEARBY_LOGS(ERROR) << "WifiLanBwuHandler failed to create WifiLan endpoint "
|
||||
"channel to the WifiLan service ("
|
||||
<< ip_address << ":" << port << ") for endpoint "
|
||||
<< endpoint_id;
|
||||
socket.Close();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return channel;
|
||||
}
|
||||
|
||||
// Accept Connection Callback.
|
||||
void WifiLanV2BwuHandler::OnIncomingWifiLanConnection(
|
||||
ClientProxy* client, const std::string& service_id,
|
||||
WifiLanSocketV2 socket) {
|
||||
auto channel =
|
||||
absl::make_unique<WifiLanEndpointChannelV2>(service_id, socket);
|
||||
std::unique_ptr<IncomingSocketConnection> connection(
|
||||
new IncomingSocketConnection{
|
||||
.socket =
|
||||
absl::make_unique<WifiLanV2IncomingSocket>(service_id, socket),
|
||||
.channel = std::move(channel),
|
||||
});
|
||||
bwu_notifications_.incoming_connection_cb(client, std::move(connection));
|
||||
}
|
||||
|
||||
} // namespace connections
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
@@ -1,80 +0,0 @@
|
||||
// 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.
|
||||
|
||||
#ifndef CORE_INTERNAL_WIFI_LAN_BWU_HANDLER_H_
|
||||
#define CORE_INTERNAL_WIFI_LAN_BWU_HANDLER_H_
|
||||
|
||||
#include "core/internal/base_bwu_handler.h"
|
||||
#include "core/internal/client_proxy.h"
|
||||
#include "core/internal/endpoint_channel_manager.h"
|
||||
#include "core/internal/mediums/mediums.h"
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
namespace connections {
|
||||
|
||||
// Defines the set of methods that need to be implemented to handle the
|
||||
// per-Medium-specific operations needed to upgrade an EndpointChannel.
|
||||
class WifiLanV2BwuHandler : public BaseBwuHandler {
|
||||
public:
|
||||
WifiLanV2BwuHandler(Mediums& mediums, EndpointChannelManager& channel_manager,
|
||||
BwuNotifications notifications);
|
||||
~WifiLanV2BwuHandler() override = default;
|
||||
|
||||
private:
|
||||
ByteArray InitializeUpgradedMediumForEndpoint(
|
||||
ClientProxy* client, const std::string& service_id,
|
||||
const std::string& endpoint_id) override;
|
||||
|
||||
void Revert() override;
|
||||
|
||||
std::unique_ptr<EndpointChannel> CreateUpgradedEndpointChannel(
|
||||
ClientProxy* client, const std::string& service_id,
|
||||
const std::string& endpoint_id,
|
||||
const UpgradePathInfo& upgrade_path_info) override;
|
||||
|
||||
Medium GetUpgradeMedium() const override { return Medium::MDNS; }
|
||||
|
||||
void OnEndpointDisconnect(ClientProxy* client,
|
||||
const std::string& endpoint_id) override {}
|
||||
|
||||
void OnIncomingWifiLanConnection(ClientProxy* client,
|
||||
const std::string& service_id,
|
||||
WifiLanSocketV2 socket);
|
||||
|
||||
class WifiLanV2IncomingSocket : public BwuHandler::IncomingSocket {
|
||||
public:
|
||||
explicit WifiLanV2IncomingSocket(const std::string& name,
|
||||
WifiLanSocketV2 socket)
|
||||
: name_(name), socket_(socket) {}
|
||||
~WifiLanV2IncomingSocket() override = default;
|
||||
|
||||
std::string ToString() override { return name_; }
|
||||
void Close() override { socket_.Close(); }
|
||||
|
||||
private:
|
||||
std::string name_;
|
||||
WifiLanSocketV2 socket_;
|
||||
};
|
||||
|
||||
Mediums& mediums_;
|
||||
WifiLanV2& wifi_lan_medium_{mediums_.GetWifiLanV2()};
|
||||
absl::flat_hash_set<std::string> active_service_ids_;
|
||||
};
|
||||
|
||||
} // namespace connections
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
|
||||
#endif // CORE_INTERNAL_WIFI_LAN_BWU_HANDLER_H_
|
||||
@@ -23,34 +23,18 @@ namespace location {
|
||||
namespace nearby {
|
||||
namespace connections {
|
||||
|
||||
namespace {
|
||||
|
||||
OutputStream* GetOutputStreamOrNull(WifiLanSocket& socket) {
|
||||
if (socket.GetRemoteWifiLanService().IsValid())
|
||||
return &socket.GetOutputStream();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
InputStream* GetInputStreamOrNull(WifiLanSocket& socket) {
|
||||
if (socket.GetRemoteWifiLanService().IsValid())
|
||||
return &socket.GetInputStream();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
WifiLanEndpointChannel::WifiLanEndpointChannel(const std::string& channel_name,
|
||||
WifiLanSocket socket)
|
||||
: BaseEndpointChannel(channel_name, GetInputStreamOrNull(socket),
|
||||
GetOutputStreamOrNull(socket)),
|
||||
wifi_lan_socket_(std::move(socket)) {}
|
||||
: BaseEndpointChannel(channel_name, &socket.GetInputStream(),
|
||||
&socket.GetOutputStream()),
|
||||
socket_(std::move(socket)) {}
|
||||
|
||||
proto::connections::Medium WifiLanEndpointChannel::GetMedium() const {
|
||||
return proto::connections::Medium::WIFI_LAN;
|
||||
}
|
||||
|
||||
void WifiLanEndpointChannel::CloseImpl() {
|
||||
auto status = wifi_lan_socket_.Close();
|
||||
auto status = socket_.Close();
|
||||
if (!status.Ok()) {
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "Failed to close underlying socket for WifiLanEndpointChannel "
|
||||
|
||||
@@ -32,7 +32,7 @@ class WifiLanEndpointChannel final : public BaseEndpointChannel {
|
||||
private:
|
||||
void CloseImpl() override;
|
||||
|
||||
WifiLanSocket wifi_lan_socket_;
|
||||
WifiLanSocket socket_;
|
||||
};
|
||||
|
||||
} // namespace connections
|
||||
|
||||
@@ -1,47 +0,0 @@
|
||||
// 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 "core/internal/wifi_lan_endpoint_channel_v2.h"
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "platform/public/logging.h"
|
||||
#include "platform/public/wifi_lan_v2.h"
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
namespace connections {
|
||||
|
||||
WifiLanEndpointChannelV2::WifiLanEndpointChannelV2(
|
||||
const std::string& channel_name, WifiLanSocketV2 socket)
|
||||
: BaseEndpointChannel(channel_name, &socket.GetInputStream(),
|
||||
&socket.GetOutputStream()),
|
||||
socket_(std::move(socket)) {}
|
||||
|
||||
proto::connections::Medium WifiLanEndpointChannelV2::GetMedium() const {
|
||||
return proto::connections::Medium::WIFI_LAN;
|
||||
}
|
||||
|
||||
void WifiLanEndpointChannelV2::CloseImpl() {
|
||||
auto status = socket_.Close();
|
||||
if (!status.Ok()) {
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "Failed to close underlying socket for WifiLanEndpointChannel "
|
||||
<< GetName() << " : exception = " << status.value;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace connections
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
@@ -1,43 +0,0 @@
|
||||
// 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.
|
||||
|
||||
#ifndef CORE_INTERNAL_WIFI_LAN_ENDPOINT_CHANNEL_V2_H_
|
||||
#define CORE_INTERNAL_WIFI_LAN_ENDPOINT_CHANNEL_V2_H_
|
||||
|
||||
#include "core/internal/base_endpoint_channel.h"
|
||||
#include "platform/public/wifi_lan_v2.h"
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
namespace connections {
|
||||
|
||||
class WifiLanEndpointChannelV2 final : public BaseEndpointChannel {
|
||||
public:
|
||||
// Creates both outgoing and incoming WifiLan channels.
|
||||
WifiLanEndpointChannelV2(const std::string& channel_name,
|
||||
WifiLanSocketV2 socket);
|
||||
|
||||
proto::connections::Medium GetMedium() const override;
|
||||
|
||||
private:
|
||||
void CloseImpl() override;
|
||||
|
||||
WifiLanSocketV2 socket_;
|
||||
};
|
||||
|
||||
} // namespace connections
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
|
||||
#endif // CORE_INTERNAL_WIFI_LAN_ENDPOINT_CHANNEL_V2_H_
|
||||
+2
-7
@@ -32,19 +32,18 @@ struct MediumSelector {
|
||||
T ble;
|
||||
T web_rtc;
|
||||
T wifi_lan;
|
||||
T wifi_lan_v2;
|
||||
|
||||
constexpr MediumSelector() = default;
|
||||
constexpr MediumSelector(const MediumSelector&) = default;
|
||||
constexpr MediumSelector& operator=(const MediumSelector&) = default;
|
||||
constexpr bool Any(T value) const {
|
||||
return bluetooth == value || ble == value || web_rtc == value ||
|
||||
wifi_lan == value || wifi_lan_v2;
|
||||
wifi_lan == value;
|
||||
}
|
||||
|
||||
constexpr bool All(T value) const {
|
||||
return bluetooth == value && ble == value && web_rtc == value &&
|
||||
wifi_lan == value && wifi_lan_v2 == value;
|
||||
wifi_lan == value;
|
||||
}
|
||||
|
||||
constexpr int Count(T value) const {
|
||||
@@ -52,7 +51,6 @@ struct MediumSelector {
|
||||
if (bluetooth == value) count++;
|
||||
if (ble == value) count++;
|
||||
if (wifi_lan == value) count++;
|
||||
if (wifi_lan_v2 == value) count++;
|
||||
if (web_rtc == value) count++;
|
||||
return count;
|
||||
}
|
||||
@@ -62,14 +60,12 @@ struct MediumSelector {
|
||||
ble = value;
|
||||
web_rtc = value;
|
||||
wifi_lan = value;
|
||||
wifi_lan_v2 = value;
|
||||
return *this;
|
||||
}
|
||||
|
||||
std::vector<Medium> GetMediums(T value) const {
|
||||
std::vector<Medium> mediums;
|
||||
// Mediums are sorted in order of decreasing preference.
|
||||
if (wifi_lan_v2 == value) mediums.push_back(Medium::MDNS);
|
||||
if (wifi_lan == value) mediums.push_back(Medium::WIFI_LAN);
|
||||
if (web_rtc == value) mediums.push_back(Medium::WEB_RTC);
|
||||
if (bluetooth == value) mediums.push_back(Medium::BLUETOOTH);
|
||||
@@ -127,7 +123,6 @@ struct DLL_API ConnectionOptions {
|
||||
uint32_t* mediumsSize);
|
||||
};
|
||||
|
||||
|
||||
// Metadata injected to facilitate out-of-band connections. The medium field is
|
||||
// required, and the other fields are only specified for a specific medium.
|
||||
// Currently, Bluetooth is the only supported medium for out-of-band
|
||||
|
||||
Reference in New Issue
Block a user