mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-14 22:56:12 -04:00
Merge branch 'master' into release
Change-Id: Ieb7f2b392864096e9cd783ab89f32442bc57abe7
This commit is contained in:
@@ -155,7 +155,6 @@ cc_test(
|
||||
"//testing/base/public:gunit",
|
||||
"//testing/base/public:gunit_main",
|
||||
"//absl/container:flat_hash_set",
|
||||
"//absl/functional:bind_front",
|
||||
"//absl/strings",
|
||||
"//absl/synchronization",
|
||||
"//absl/time",
|
||||
|
||||
@@ -302,6 +302,7 @@ Status BasePcpHandler::RequestConnection(ClientProxy* client,
|
||||
return;
|
||||
}
|
||||
|
||||
std::vector<DiscoveredEndpoint*> endpoints;
|
||||
auto endpoint = GetDiscoveredEndpoint(endpoint_id);
|
||||
if (endpoint == nullptr) {
|
||||
NEARBY_LOG(INFO, "Discovered endpoint not found: id=%s",
|
||||
@@ -310,9 +311,31 @@ Status BasePcpHandler::RequestConnection(ClientProxy* client,
|
||||
return;
|
||||
}
|
||||
|
||||
auto connect_impl_result = ConnectImpl(client, endpoint);
|
||||
std::unique_ptr<EndpointChannel> channel =
|
||||
std::move(connect_impl_result.endpoint_channel);
|
||||
auto webrtc_endpoint = absl::make_unique<WebRtcEndpoint>(
|
||||
DiscoveredEndpoint{endpoint->endpoint_id, endpoint->endpoint_name,
|
||||
endpoint->service_id,
|
||||
proto::connections::Medium::WEB_RTC},
|
||||
CreatePeerIdFromAdvertisement(endpoint->service_id,
|
||||
endpoint->endpoint_id,
|
||||
endpoint->endpoint_name));
|
||||
endpoints.push_back(endpoint);
|
||||
endpoints.push_back(webrtc_endpoint.get());
|
||||
|
||||
std::sort(endpoints.begin(), endpoints.end(),
|
||||
[this](DiscoveredEndpoint* a, DiscoveredEndpoint* b) -> bool {
|
||||
return IsPreferred(*a, *b);
|
||||
});
|
||||
|
||||
std::unique_ptr<EndpointChannel> channel;
|
||||
ConnectImplResult connect_impl_result;
|
||||
|
||||
for (auto connect_endpoint : endpoints) {
|
||||
connect_impl_result = ConnectImpl(client, connect_endpoint);
|
||||
if (connect_impl_result.status.Ok()) {
|
||||
channel = std::move(connect_impl_result.endpoint_channel);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (channel == nullptr) {
|
||||
NEARBY_LOG(INFO, "Endpoint channel not available: id=%s",
|
||||
@@ -1068,6 +1091,13 @@ void BasePcpHandler::PendingConnectionInfo::LocalEndpointRejectedConnection(
|
||||
client->LocalEndpointRejectedConnection(endpoint_id);
|
||||
}
|
||||
|
||||
mediums::PeerId BasePcpHandler::CreatePeerIdFromAdvertisement(
|
||||
const std::string& service_id, const std::string& endpoint_id,
|
||||
const std::string& endpoint_name) {
|
||||
std::string seed = absl::StrCat(service_id, endpoint_id, endpoint_name);
|
||||
return mediums::PeerId::FromSeed(ByteArray(std::move(seed)));
|
||||
}
|
||||
|
||||
} // namespace connections
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
|
||||
@@ -24,6 +24,7 @@
|
||||
#include "core_v2/internal/encryption_runner.h"
|
||||
#include "core_v2/internal/endpoint_channel_manager.h"
|
||||
#include "core_v2/internal/endpoint_manager.h"
|
||||
#include "core_v2/internal/mediums/webrtc.h"
|
||||
#include "core_v2/internal/pcp.h"
|
||||
#include "core_v2/internal/pcp_handler.h"
|
||||
#include "core_v2/listeners.h"
|
||||
@@ -195,6 +196,14 @@ class BasePcpHandler : public PcpHandler,
|
||||
proto::connections::Medium medium;
|
||||
};
|
||||
|
||||
struct WebRtcEndpoint : public DiscoveredEndpoint {
|
||||
WebRtcEndpoint(DiscoveredEndpoint endpoint, mediums::PeerId peer_id)
|
||||
: DiscoveredEndpoint(std::move(endpoint)),
|
||||
peer_id(std::move(peer_id)) {}
|
||||
|
||||
mediums::PeerId peer_id;
|
||||
};
|
||||
|
||||
struct ConnectImplResult {
|
||||
proto::connections::Medium medium =
|
||||
proto::connections::Medium::UNKNOWN_MEDIUM;
|
||||
@@ -249,6 +258,10 @@ class BasePcpHandler : public PcpHandler,
|
||||
GetConnectionMediumsByPriority() = 0;
|
||||
virtual proto::connections::Medium GetDefaultUpgradeMedium() = 0;
|
||||
|
||||
mediums::PeerId CreatePeerIdFromAdvertisement(const string& service_id,
|
||||
const string& endpoint_id,
|
||||
const string& endpoint_name);
|
||||
|
||||
EndpointManager* endpoint_manager_;
|
||||
EndpointChannelManager* channel_manager_;
|
||||
|
||||
|
||||
@@ -107,11 +107,15 @@ class MockPcpHandler : public BasePcpHandler {
|
||||
MOCK_METHOD(Status, StopDiscoveryImpl, (ClientProxy * client), (override));
|
||||
MOCK_METHOD(ConnectImplResult, ConnectImpl,
|
||||
(ClientProxy * client, DiscoveredEndpoint* endpoint), (override));
|
||||
MOCK_METHOD(std::vector<proto::connections::Medium>,
|
||||
GetConnectionMediumsByPriority, (), (override));
|
||||
MOCK_METHOD(proto::connections::Medium, GetDefaultUpgradeMedium, (),
|
||||
(override));
|
||||
|
||||
std::vector<proto::connections::Medium> GetConnectionMediumsByPriority()
|
||||
override {
|
||||
return {proto::connections::Medium::BLE,
|
||||
proto::connections::Medium::WEB_RTC};
|
||||
}
|
||||
|
||||
// Mock adapters for protected non-virtual methods of a base class.
|
||||
void OnEndpointFound(ClientProxy* client,
|
||||
std::shared_ptr<DiscoveredEndpoint> endpoint) {
|
||||
|
||||
@@ -241,7 +241,7 @@ EndpointManager::~EndpointManager() {
|
||||
NEARBY_LOG(INFO, "EndpointManager is down");
|
||||
}
|
||||
|
||||
const EndpointManager::FrameProcessor::Handle
|
||||
EndpointManager::FrameProcessor::Handle
|
||||
EndpointManager::RegisterFrameProcessor(
|
||||
V1Frame::FrameType frame_type, EndpointManager::FrameProcessor* processor) {
|
||||
const FrameProcessor::Handle handle = processor;
|
||||
|
||||
@@ -95,7 +95,7 @@ class EndpointManager {
|
||||
// FrameProcessor* instances are of dynamic duration and survive all sessions.
|
||||
// returns unique handle to be used for unregistering.
|
||||
// Blocks until registration is complete.
|
||||
const FrameProcessor::Handle RegisterFrameProcessor(
|
||||
FrameProcessor::Handle RegisterFrameProcessor(
|
||||
V1Frame::FrameType frame_type, FrameProcessor* processor);
|
||||
void UnregisterFrameProcessor(V1Frame::FrameType frame_type,
|
||||
const void* handle, bool sync = false);
|
||||
|
||||
@@ -63,6 +63,7 @@ cc_test(
|
||||
deps = [
|
||||
":webrtc",
|
||||
"//platform_v2/base",
|
||||
"//platform_v2/base:test_util",
|
||||
"//platform_v2/impl/g3", # buildcleaner: keep
|
||||
"//platform_v2/public:comm",
|
||||
"//platform_v2/public:types",
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
|
||||
#include "core_v2/internal/mediums/webrtc/session_description_wrapper.h"
|
||||
#include "platform_v2/base/byte_array.h"
|
||||
#include "platform_v2/base/medium_environment.h"
|
||||
#include "platform_v2/public/webrtc.h"
|
||||
#include "gmock/gmock.h"
|
||||
#include "gtest/gtest.h"
|
||||
@@ -34,6 +35,14 @@ namespace connections {
|
||||
namespace mediums {
|
||||
namespace {
|
||||
|
||||
class ConnectionFlowTest : public ::testing::Test {
|
||||
protected:
|
||||
ConnectionFlowTest() {
|
||||
MediumEnvironment::Instance().Stop();
|
||||
MediumEnvironment::Instance().Start({.webrtc_enabled = true});
|
||||
}
|
||||
};
|
||||
|
||||
std::unique_ptr<webrtc::IceCandidateInterface> CopyCandidate(
|
||||
const webrtc::IceCandidateInterface* candidate) {
|
||||
return webrtc::CreateIceCandidate(candidate->sdp_mid(),
|
||||
@@ -43,7 +52,7 @@ std::unique_ptr<webrtc::IceCandidateInterface> CopyCandidate(
|
||||
|
||||
// TODO(bfranz) - Add test that deterministically sends answerer_ice_candidates
|
||||
// before answer is sent.
|
||||
TEST(ConnectionFlowTest, SuccessfulOfferAnswerFlow) {
|
||||
TEST_F(ConnectionFlowTest, SuccessfulOfferAnswerFlow) {
|
||||
WebRtcMedium webrtc_medium_offerer, webrtc_medium_answerer;
|
||||
|
||||
Future<ByteArray> message_received_future;
|
||||
@@ -109,7 +118,7 @@ TEST(ConnectionFlowTest, SuccessfulOfferAnswerFlow) {
|
||||
EXPECT_EQ(received_message.result(), ByteArray{message});
|
||||
}
|
||||
|
||||
TEST(ConnectionFlowTest, CreateAnswerBeforeOfferReceived) {
|
||||
TEST_F(ConnectionFlowTest, CreateAnswerBeforeOfferReceived) {
|
||||
WebRtcMedium webrtc_medium;
|
||||
|
||||
std::unique_ptr<ConnectionFlow> answerer = ConnectionFlow::Create(
|
||||
@@ -120,7 +129,7 @@ TEST(ConnectionFlowTest, CreateAnswerBeforeOfferReceived) {
|
||||
EXPECT_FALSE(answer.IsValid());
|
||||
}
|
||||
|
||||
TEST(ConnectionFlowTest, SetAnswerBeforeOffer) {
|
||||
TEST_F(ConnectionFlowTest, SetAnswerBeforeOffer) {
|
||||
WebRtcMedium webrtc_medium_offerer, webrtc_medium_answerer;
|
||||
|
||||
std::unique_ptr<ConnectionFlow> offerer =
|
||||
@@ -142,7 +151,7 @@ TEST(ConnectionFlowTest, SetAnswerBeforeOffer) {
|
||||
EXPECT_FALSE(offerer->OnAnswerReceived(answer));
|
||||
}
|
||||
|
||||
TEST(ConnectionFlowTest, CannotCreateOfferAfterClose) {
|
||||
TEST_F(ConnectionFlowTest, CannotCreateOfferAfterClose) {
|
||||
WebRtcMedium webrtc_medium;
|
||||
|
||||
std::unique_ptr<ConnectionFlow> offerer = ConnectionFlow::Create(
|
||||
@@ -154,7 +163,7 @@ TEST(ConnectionFlowTest, CannotCreateOfferAfterClose) {
|
||||
EXPECT_FALSE(offerer->CreateOffer().IsValid());
|
||||
}
|
||||
|
||||
TEST(ConnectionFlowTest, CannotSetSessionDescriptionAfterClose) {
|
||||
TEST_F(ConnectionFlowTest, CannotSetSessionDescriptionAfterClose) {
|
||||
WebRtcMedium webrtc_medium;
|
||||
|
||||
std::unique_ptr<ConnectionFlow> offerer = ConnectionFlow::Create(
|
||||
@@ -169,7 +178,7 @@ TEST(ConnectionFlowTest, CannotSetSessionDescriptionAfterClose) {
|
||||
EXPECT_FALSE(offerer->SetLocalSessionDescription(offer));
|
||||
}
|
||||
|
||||
TEST(ConnectionFlowTest, CannotReceiveOfferAfterClose) {
|
||||
TEST_F(ConnectionFlowTest, CannotReceiveOfferAfterClose) {
|
||||
WebRtcMedium webrtc_medium_offerer, webrtc_medium_answerer;
|
||||
|
||||
std::unique_ptr<ConnectionFlow> offerer =
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
#include "core_v2/internal/mediums/webrtc/webrtc_socket_wrapper.h"
|
||||
#include "platform_v2/base/listeners.h"
|
||||
#include "platform_v2/base/medium_environment.h"
|
||||
#include "platform_v2/public/mutex_lock.h"
|
||||
#include "gmock/gmock.h"
|
||||
#include "gtest/gtest.h"
|
||||
@@ -27,8 +28,16 @@ namespace mediums {
|
||||
|
||||
namespace {
|
||||
|
||||
class WebRtcTest : public ::testing::Test {
|
||||
protected:
|
||||
WebRtcTest() {
|
||||
MediumEnvironment::Instance().Stop();
|
||||
MediumEnvironment::Instance().Start({.webrtc_enabled = true});
|
||||
}
|
||||
};
|
||||
|
||||
// Basic test to check that device is accepting connections when initialized.
|
||||
TEST(WebRtcTest, NotAcceptingConnections) {
|
||||
TEST_F(WebRtcTest, NotAcceptingConnections) {
|
||||
WebRtc webrtc;
|
||||
ASSERT_TRUE(webrtc.IsAvailable());
|
||||
EXPECT_FALSE(webrtc.IsAcceptingConnections());
|
||||
@@ -36,7 +45,7 @@ TEST(WebRtcTest, NotAcceptingConnections) {
|
||||
|
||||
// Tests the flow when the device tries to accept connections twice. In this
|
||||
// case, only the first call is successful and subsequent calls fail.
|
||||
TEST(WebRtcTest, StartAcceptingConnectionTwice) {
|
||||
TEST_F(WebRtcTest, StartAcceptingConnectionTwice) {
|
||||
using MockAcceptedCallback =
|
||||
testing::MockFunction<void(WebRtcSocketWrapper socket)>;
|
||||
testing::StrictMock<MockAcceptedCallback> mock_accepted_callback_;
|
||||
@@ -54,7 +63,7 @@ TEST(WebRtcTest, StartAcceptingConnectionTwice) {
|
||||
|
||||
// Tests the flow when the device tries to connect but the data channel times
|
||||
// out.
|
||||
TEST(WebRtcTest, Connect_DataChannelTimeOut) {
|
||||
TEST_F(WebRtcTest, Connect_DataChannelTimeOut) {
|
||||
WebRtc webrtc;
|
||||
PeerId peer_id("peer_id");
|
||||
|
||||
@@ -68,7 +77,7 @@ TEST(WebRtcTest, Connect_DataChannelTimeOut) {
|
||||
|
||||
// Tests the flow when the device calls Connect() after calling
|
||||
// StartAcceptingConnections() without StopAcceptingConnections().
|
||||
TEST(WebRtcTest, StartAcceptingConnection_ThenConnect) {
|
||||
TEST_F(WebRtcTest, StartAcceptingConnection_ThenConnect) {
|
||||
using MockAcceptedCallback =
|
||||
testing::MockFunction<void(WebRtcSocketWrapper socket)>;
|
||||
testing::StrictMock<MockAcceptedCallback> mock_accepted_callback_;
|
||||
@@ -88,7 +97,7 @@ TEST(WebRtcTest, StartAcceptingConnection_ThenConnect) {
|
||||
|
||||
// Tests the flow when the device calls StartAcceptingConnections but the medium
|
||||
// is closed before a peer device can connect to it.
|
||||
TEST(WebRtcTest, StartAndStopAcceptingConnections) {
|
||||
TEST_F(WebRtcTest, StartAndStopAcceptingConnections) {
|
||||
using MockAcceptedCallback =
|
||||
testing::MockFunction<void(WebRtcSocketWrapper socket)>;
|
||||
testing::StrictMock<MockAcceptedCallback> mock_accepted_callback_;
|
||||
@@ -105,7 +114,7 @@ TEST(WebRtcTest, StartAndStopAcceptingConnections) {
|
||||
|
||||
// Tests the flow when the device tries to connect to two different peers
|
||||
// without disconnecting in between.
|
||||
TEST(WebRtcTest, ConnectTwice) {
|
||||
TEST_F(WebRtcTest, ConnectTwice) {
|
||||
WebRtc receiver, sender, device_c;
|
||||
WebRtcSocketWrapper receiver_socket, sender_socket;
|
||||
const PeerId self_id("self_id"), other_id("other_id");
|
||||
@@ -149,7 +158,7 @@ TEST(WebRtcTest, ConnectTwice) {
|
||||
|
||||
// Tests the flow when the two devices exchange SDP messages and connect to each
|
||||
// other but disconnect before being able to send/receive the actual data.
|
||||
TEST(WebRtcTest, ConnectBothDevicesAndAbort) {
|
||||
TEST_F(WebRtcTest, ConnectBothDevicesAndAbort) {
|
||||
WebRtc receiver, sender;
|
||||
WebRtcSocketWrapper receiver_socket, sender_socket;
|
||||
const PeerId self_id("self_id");
|
||||
@@ -175,7 +184,7 @@ TEST(WebRtcTest, ConnectBothDevicesAndAbort) {
|
||||
|
||||
// Tests the flow when the two devices exchange SDP messages and connect to each
|
||||
// other and the actual data is exchanged successfully between the devices.
|
||||
TEST(WebRtcTest, ConnectBothDevicesAndSendData) {
|
||||
TEST_F(WebRtcTest, ConnectBothDevicesAndSendData) {
|
||||
WebRtc receiver, sender;
|
||||
WebRtcSocketWrapper receiver_socket, sender_socket;
|
||||
const PeerId self_id("self_id");
|
||||
@@ -207,7 +216,7 @@ TEST(WebRtcTest, ConnectBothDevicesAndSendData) {
|
||||
|
||||
// Tests the flow when the two devices exchange SDP messages and connect to each
|
||||
// other but the signaling channel is closed before sending the data.
|
||||
TEST(WebRtcTest, ConnectBothDevices_ShutdownSignaling_SendData) {
|
||||
TEST_F(WebRtcTest, ConnectBothDevices_ShutdownSignaling_SendData) {
|
||||
WebRtc receiver, sender;
|
||||
WebRtcSocketWrapper receiver_socket, sender_socket;
|
||||
const PeerId self_id("self_id");
|
||||
|
||||
@@ -34,10 +34,10 @@ bool WifiLan::IsAvailable() const {
|
||||
bool WifiLan::IsAvailableLocked() const { return medium_.IsValid(); }
|
||||
|
||||
bool WifiLan::StartAdvertising(const std::string& service_id,
|
||||
const std::string& wifi_lan_service_info_name) {
|
||||
const std::string& service_info_name) {
|
||||
MutexLock lock(&mutex_);
|
||||
|
||||
if (wifi_lan_service_info_name.empty()) {
|
||||
if (service_info_name.empty()) {
|
||||
NEARBY_LOG(
|
||||
INFO,
|
||||
"Refusing to turn on WifiLan advertising. Empty service info name.");
|
||||
@@ -50,45 +50,45 @@ bool WifiLan::StartAdvertising(const std::string& service_id,
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!medium_.StartAdvertising(service_id, wifi_lan_service_info_name)) {
|
||||
if (!medium_.StartAdvertising(service_id, service_info_name)) {
|
||||
NEARBY_LOG(
|
||||
INFO, "Failed to turn on WifiLan advertising with service info name=%s",
|
||||
wifi_lan_service_info_name.c_str());
|
||||
service_info_name.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
NEARBY_LOGS(INFO) << "Turned on WifiLan advertising with service info name="
|
||||
<< wifi_lan_service_info_name
|
||||
<< service_info_name
|
||||
<< ", service id=" << service_id;
|
||||
advertising_info_.service_id = service_id;
|
||||
advertising_info_.Add(service_id);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WifiLan::StopAdvertising(const std::string& service_id) {
|
||||
MutexLock lock(&mutex_);
|
||||
|
||||
if (!IsAdvertisingLocked()) {
|
||||
if (!IsAdvertisingLocked(service_id)) {
|
||||
NEARBY_LOG(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(advertising_info_.service_id);
|
||||
bool ret = medium_.StopAdvertising(service_id);
|
||||
// Reset our bundle of advertising state to mark that we're no longer
|
||||
// advertising.
|
||||
advertising_info_.Clear();
|
||||
advertising_info_.Remove(service_id);
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool WifiLan::IsAdvertising() {
|
||||
bool WifiLan::IsAdvertising(const std::string& service_id) {
|
||||
MutexLock lock(&mutex_);
|
||||
|
||||
return IsAdvertisingLocked();
|
||||
return IsAdvertisingLocked(service_id);
|
||||
}
|
||||
|
||||
bool WifiLan::IsAdvertisingLocked() {
|
||||
return !advertising_info_.Empty();
|
||||
bool WifiLan::IsAdvertisingLocked(const std::string& service_id) {
|
||||
return advertising_info_.Existed(service_id);
|
||||
}
|
||||
|
||||
bool WifiLan::StartDiscovery(const std::string& service_id,
|
||||
@@ -124,7 +124,7 @@ bool WifiLan::StartDiscovery(const std::string& service_id,
|
||||
NEARBY_LOG(INFO, "Turned on WifiLan discovering with service id=%s",
|
||||
service_id.c_str());
|
||||
// Mark the fact that we're currently performing a WifiLan discovering.
|
||||
discovering_info_.service_id = service_id;
|
||||
discovering_info_.Add(service_id);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -152,7 +152,7 @@ bool WifiLan::IsDiscovering(const std::string& service_id) {
|
||||
}
|
||||
|
||||
bool WifiLan::IsDiscoveringLocked(const std::string& service_id) {
|
||||
return !discovering_info_.Empty();
|
||||
return discovering_info_.Existed(service_id);
|
||||
}
|
||||
|
||||
bool WifiLan::StartAcceptingConnections(const std::string& service_id,
|
||||
@@ -188,7 +188,7 @@ bool WifiLan::StartAcceptingConnections(const std::string& service_id,
|
||||
return false;
|
||||
}
|
||||
|
||||
accepting_connections_info_.service_id = service_id;
|
||||
accepting_connections_info_.Add(service_id);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -202,11 +202,10 @@ bool WifiLan::StopAcceptingConnections(const std::string& service_id) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ret =
|
||||
medium_.StopAcceptingConnections(accepting_connections_info_.service_id);
|
||||
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_.Clear();
|
||||
accepting_connections_info_.Remove(service_id);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -217,7 +216,7 @@ bool WifiLan::IsAcceptingConnections(const std::string& service_id) {
|
||||
}
|
||||
|
||||
bool WifiLan::IsAcceptingConnectionsLocked(const std::string& service_id) {
|
||||
return !accepting_connections_info_.Empty();
|
||||
return accepting_connections_info_.Existed(service_id);
|
||||
}
|
||||
|
||||
WifiLanSocket WifiLan::Connect(WifiLanService& wifi_lan_service,
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
#include "platform_v2/public/mutex.h"
|
||||
#include "platform_v2/public/wifi_lan.h"
|
||||
#include "absl/container/flat_hash_map.h"
|
||||
#include "absl/container/flat_hash_set.h"
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
@@ -39,7 +40,7 @@ class WifiLan {
|
||||
// Sets custom service info name, and then enables WifiLan advertising.
|
||||
// Returns true, if name is successfully set, and false otherwise.
|
||||
bool StartAdvertising(const std::string& service_id,
|
||||
const std::string& wifi_lan_service_info_name)
|
||||
const std::string& service_info_name)
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
// Disables WifiLan advertising, and restores service info name to
|
||||
@@ -47,7 +48,7 @@ class WifiLan {
|
||||
bool StopAdvertising(const std::string& service_id)
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
bool IsAdvertising() 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,
|
||||
@@ -84,31 +85,53 @@ class WifiLan {
|
||||
|
||||
private:
|
||||
struct AdvertisingInfo {
|
||||
bool Empty() const { return service_id.empty(); }
|
||||
void Clear() { service_id.clear(); }
|
||||
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);
|
||||
}
|
||||
|
||||
std::string service_id;
|
||||
absl::flat_hash_set<std::string> service_ids;
|
||||
};
|
||||
|
||||
struct DiscoveringInfo {
|
||||
bool Empty() const { return service_id.empty(); }
|
||||
void Clear() { service_id.clear(); }
|
||||
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);
|
||||
}
|
||||
|
||||
std::string service_id;
|
||||
absl::flat_hash_set<std::string> service_ids;
|
||||
};
|
||||
|
||||
struct AcceptingConnectionsInfo {
|
||||
bool Empty() const { return service_id.empty(); }
|
||||
void Clear() { service_id.clear(); }
|
||||
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);
|
||||
}
|
||||
|
||||
std::string service_id;
|
||||
absl::flat_hash_set<std::string> service_ids;
|
||||
};
|
||||
|
||||
// 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() ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
|
||||
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)
|
||||
|
||||
@@ -247,32 +247,33 @@ P2pClusterPcpHandler::MakeBluetoothDeviceLostHandler(
|
||||
}
|
||||
|
||||
bool P2pClusterPcpHandler::IsRecognizedWifiLanEndpoint(
|
||||
const std::string& name_string, const std::string& service_id,
|
||||
const WifiLanServiceInfo& name) const {
|
||||
if (!name.IsValid()) {
|
||||
const std::string& service_id,
|
||||
const WifiLanServiceInfo& service_info) const {
|
||||
if (!service_info.IsValid()) {
|
||||
NEARBY_LOG(
|
||||
INFO,
|
||||
"P2pClusterPcpHandler::IsRecognizedWifiLanEndpoint: name is invalid");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (name.GetPcp() != GetPcp()) {
|
||||
if (service_info.GetPcp() != GetPcp()) {
|
||||
NEARBY_LOG(INFO,
|
||||
"P2pClusterPcpHandler::IsRecognizedWifiLanEndpoint: Pcp is "
|
||||
"not matched; name.Pcp=%d, Pcp=%d",
|
||||
name.GetPcp(), GetPcp());
|
||||
service_info.GetPcp(), GetPcp());
|
||||
return false;
|
||||
}
|
||||
|
||||
ByteArray expected_service_id_hash =
|
||||
GenerateHash(service_id, BluetoothDeviceName::kServiceIdHashLength);
|
||||
|
||||
if (name.GetServiceIdHash() != expected_service_id_hash) {
|
||||
if (service_info.GetServiceIdHash() != expected_service_id_hash) {
|
||||
NEARBY_LOG(INFO,
|
||||
"P2pClusterPcpHandler::IsRecognizedWifiLanEndpoint: service "
|
||||
"id hash is "
|
||||
"not matched; name.service_id_hash=%s, expected=%s",
|
||||
name.GetServiceIdHash().data(), expected_service_id_hash.data());
|
||||
service_info.GetServiceIdHash().data(),
|
||||
expected_service_id_hash.data());
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -296,25 +297,23 @@ P2pClusterPcpHandler::MakeWifiLanServiceDiscoveredHandler(
|
||||
}
|
||||
|
||||
// Parse the WifiLan service name.
|
||||
const std::string& service_name_string = service.GetName();
|
||||
WifiLanServiceInfo service_name(service_name_string);
|
||||
const std::string& service_info_name = service.GetName();
|
||||
WifiLanServiceInfo service_info(service_info_name);
|
||||
|
||||
// Make sure the WifiLan service name points to a valid
|
||||
// endpoint we're discovering.
|
||||
if (!IsRecognizedWifiLanEndpoint(service_name_string, service_id,
|
||||
service_name))
|
||||
return;
|
||||
if (!IsRecognizedWifiLanEndpoint(service_id, service_info)) return;
|
||||
|
||||
// Report the discovered endpoint to the client.
|
||||
NEARBY_LOG(INFO,
|
||||
"Invoking BasePcpHandler::OnEndpointFound() for WifiLan "
|
||||
"service=%s; id=%s; name=%s",
|
||||
service_id.c_str(), service_name.GetEndpointId().c_str(),
|
||||
service_name.GetEndpointName().c_str());
|
||||
service_id.c_str(), service_info.GetEndpointId().c_str(),
|
||||
service_info.GetEndpointName().c_str());
|
||||
OnEndpointFound(client, std::make_shared<WifiLanEndpoint>(WifiLanEndpoint{
|
||||
{
|
||||
service_name.GetEndpointId(),
|
||||
service_name.GetEndpointName(),
|
||||
service_info.GetEndpointId(),
|
||||
service_info.GetEndpointName(),
|
||||
service_id,
|
||||
proto::connections::Medium::WIFI_LAN,
|
||||
},
|
||||
@@ -341,14 +340,12 @@ P2pClusterPcpHandler::MakeWifiLanServiceLostHandler(
|
||||
}
|
||||
|
||||
// Parse the WifiLan service name.
|
||||
const std::string& service_name_string = service.GetName();
|
||||
WifiLanServiceInfo service_name(service_name_string);
|
||||
const std::string& service_info_name = service.GetName();
|
||||
WifiLanServiceInfo service_info(service_info_name);
|
||||
|
||||
// Make sure the WifiLan service name points to a valid
|
||||
// endpoint we're discovering.
|
||||
if (!IsRecognizedWifiLanEndpoint(service_name_string, service_id,
|
||||
service_name))
|
||||
return;
|
||||
if (!IsRecognizedWifiLanEndpoint(service_id, service_info)) return;
|
||||
|
||||
// Report the discovered endpoint to the client.
|
||||
NEARBY_LOG(
|
||||
@@ -358,8 +355,8 @@ P2pClusterPcpHandler::MakeWifiLanServiceLostHandler(
|
||||
client, service_id.c_str());
|
||||
OnEndpointLost(client, WifiLanEndpoint{
|
||||
{
|
||||
service_name.GetEndpointId(),
|
||||
service_name.GetEndpointName(),
|
||||
service_info.GetEndpointId(),
|
||||
service_info.GetEndpointName(),
|
||||
service_id,
|
||||
proto::connections::Medium::WIFI_LAN,
|
||||
},
|
||||
@@ -611,11 +608,11 @@ proto::connections::Medium P2pClusterPcpHandler::StartWifiLanAdvertising(
|
||||
}
|
||||
RunOnPcpHandlerThread([this, client, local_endpoint_name,
|
||||
socket = std::move(socket)]() mutable {
|
||||
std::string remote_service_name =
|
||||
std::string remote_service_info_name =
|
||||
socket.GetRemoteWifiLanService().GetName();
|
||||
auto channel = absl::make_unique<WifiLanEndpointChannel>(
|
||||
remote_service_name, socket);
|
||||
OnIncomingConnection(client, remote_service_name,
|
||||
remote_service_info_name, socket);
|
||||
OnIncomingConnection(client, remote_service_info_name,
|
||||
std::move(channel),
|
||||
proto::connections::Medium::WIFI_LAN);
|
||||
});
|
||||
@@ -632,10 +629,10 @@ proto::connections::Medium P2pClusterPcpHandler::StartWifiLanAdvertising(
|
||||
service_id.c_str(), local_endpoint_id.c_str(),
|
||||
std::string(service_id_hash).c_str(), local_endpoint_name.c_str());
|
||||
// Generate a WifiLanServiceInfo with which to become WifiLan discoverable.
|
||||
std::string service_name(WifiLanServiceInfo(
|
||||
std::string service_info_name(WifiLanServiceInfo(
|
||||
WifiLanServiceInfo::Version::kV1, GetPcp(), local_endpoint_id,
|
||||
service_id_hash, local_endpoint_name));
|
||||
if (service_name.empty()) {
|
||||
if (service_info_name.empty()) {
|
||||
NEARBY_LOG(INFO,
|
||||
"P2pClusterPcpHandler::StartWifiLanAdvertising: generate "
|
||||
"WifiLanServiceInfo failed");
|
||||
@@ -644,8 +641,8 @@ proto::connections::Medium P2pClusterPcpHandler::StartWifiLanAdvertising(
|
||||
} else {
|
||||
NEARBY_LOG(INFO,
|
||||
"P2pClusterPcpHandler::StartWifiLanAdvertising: generate "
|
||||
"WifiLanServiceInfo succeeded; service_name=%s",
|
||||
service_name.c_str());
|
||||
"WifiLanServiceInfo succeeded; service_info_name=%s",
|
||||
service_info_name.c_str());
|
||||
}
|
||||
|
||||
NEARBY_LOG(
|
||||
@@ -653,11 +650,11 @@ proto::connections::Medium P2pClusterPcpHandler::StartWifiLanAdvertising(
|
||||
"P2pClusterPcpHandler::StartWifiLanAdvertising: service=%s: come up",
|
||||
service_id.c_str());
|
||||
|
||||
if (!wifi_lan_medium_.StartAdvertising(service_id, service_name)) {
|
||||
if (!wifi_lan_medium_.StartAdvertising(service_id, service_info_name)) {
|
||||
NEARBY_LOG(INFO,
|
||||
"P2pClusterPcpHandler::StartWifiLanAdvertising: failed to "
|
||||
"start advertising, service_name=%s",
|
||||
service_name.c_str());
|
||||
"start advertising, service_info_name=%s",
|
||||
service_info_name.c_str());
|
||||
wifi_lan_medium_.StopAcceptingConnections(service_id);
|
||||
return proto::connections::UNKNOWN_MEDIUM;
|
||||
}
|
||||
@@ -762,13 +759,6 @@ BasePcpHandler::ConnectImplResult P2pClusterPcpHandler::WebRtcConnectImpl(
|
||||
.endpoint_channel = std::move(channel)};
|
||||
}
|
||||
|
||||
mediums::PeerId P2pClusterPcpHandler::CreatePeerIdFromAdvertisement(
|
||||
const std::string& service_id, const std::string& endpoint_id,
|
||||
const std::string& endpoint_name) {
|
||||
std::string seed = absl::StrCat(service_id, endpoint_id, endpoint_name);
|
||||
return mediums::PeerId::FromSeed(ByteArray(seed));
|
||||
}
|
||||
|
||||
} // namespace connections
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
|
||||
@@ -97,12 +97,6 @@ class P2pClusterPcpHandler : public BasePcpHandler {
|
||||
wifi_lan_service(std::move(service)) {}
|
||||
WifiLanService wifi_lan_service;
|
||||
};
|
||||
struct WebRtcEndpoint : public BasePcpHandler::DiscoveredEndpoint {
|
||||
WebRtcEndpoint(DiscoveredEndpoint endpoint, mediums::PeerId peer_id)
|
||||
: DiscoveredEndpoint(std::move(endpoint)),
|
||||
peer_id(std::move(peer_id)) {}
|
||||
mediums::PeerId peer_id;
|
||||
};
|
||||
|
||||
using BluetoothDiscoveredDeviceCallback =
|
||||
BluetoothClassic::DiscoveredDeviceCallback;
|
||||
@@ -115,7 +109,7 @@ class P2pClusterPcpHandler : public BasePcpHandler {
|
||||
|
||||
static ByteArray GenerateHash(const std::string& source, size_t size);
|
||||
|
||||
// Bluetooth.
|
||||
// Bluetooth
|
||||
bool IsRecognizedBluetoothEndpoint(const std::string& name_string,
|
||||
const std::string& service_id,
|
||||
const BluetoothDeviceName& name) const;
|
||||
@@ -133,10 +127,10 @@ class P2pClusterPcpHandler : public BasePcpHandler {
|
||||
BasePcpHandler::ConnectImplResult BluetoothConnectImpl(
|
||||
ClientProxy* client, BluetoothEndpoint* endpoint);
|
||||
|
||||
// WifiLan.
|
||||
bool IsRecognizedWifiLanEndpoint(const std::string& name_string,
|
||||
const std::string& service_id,
|
||||
const WifiLanServiceInfo& name) const;
|
||||
// WifiLan
|
||||
bool IsRecognizedWifiLanEndpoint(
|
||||
const std::string& service_id,
|
||||
const WifiLanServiceInfo& service_info) const;
|
||||
std::function<void(WifiLanService&, const std::string&)>
|
||||
MakeWifiLanServiceDiscoveredHandler(ClientProxy* client,
|
||||
const std::string& service_id);
|
||||
@@ -160,9 +154,6 @@ class P2pClusterPcpHandler : public BasePcpHandler {
|
||||
const std::string& local_endpoint_name);
|
||||
BasePcpHandler::ConnectImplResult WebRtcConnectImpl(
|
||||
ClientProxy* client, WebRtcEndpoint* webrtc_endpoint);
|
||||
mediums::PeerId CreatePeerIdFromAdvertisement(const string& service_id,
|
||||
const string& endpoint_id,
|
||||
const string& endpoint_name);
|
||||
|
||||
BluetoothRadio& bluetooth_radio_;
|
||||
BluetoothClassic& bluetooth_medium_;
|
||||
|
||||
@@ -27,7 +27,7 @@ class WifiLanEndpointChannel final : public BaseEndpointChannel {
|
||||
public:
|
||||
// Creates both outgoing and incoming WifiLan channels.
|
||||
WifiLanEndpointChannel(const std::string& channel_name,
|
||||
WifiLanSocket bluetooth_socket);
|
||||
WifiLanSocket socket);
|
||||
|
||||
proto::connections::Medium GetMedium() const override;
|
||||
|
||||
|
||||
@@ -21,10 +21,22 @@ namespace location {
|
||||
namespace nearby {
|
||||
namespace connections {
|
||||
|
||||
// Generic type: allows definition of a feature T for every Medium.
|
||||
template <typename T>
|
||||
struct MediumSelector {
|
||||
T bluetooth;
|
||||
T web_rtc;
|
||||
T wifi_lan;
|
||||
};
|
||||
|
||||
// Feature On/Off switch for mediums.
|
||||
using BooleanMediumSelector = MediumSelector<bool>;
|
||||
|
||||
// Connection Options: used for both Advertising and Discovery.
|
||||
// All fields are mutable, to make the type copy-assignable.
|
||||
struct ConnectionOptions {
|
||||
Strategy strategy;
|
||||
BooleanMediumSelector allowed;
|
||||
bool auto_upgrade_bandwidth;
|
||||
bool enforce_topology_constraints;
|
||||
// Verify if ConnectionOptions is in a not-initialized (Empty) state.
|
||||
|
||||
Reference in New Issue
Block a user