Merge branch 'google3' to roll forward up to cl/351408510.

This commit is contained in:
hai007
2021-01-12 11:20:06 -08:00
11 changed files with 171 additions and 32 deletions
+33 -25
View File
@@ -156,8 +156,7 @@ void BasePcpHandler::InjectEndpoint(
ClientProxy* client, const std::string& service_id,
const OutOfBandConnectionMetadata& metadata) {
CountDownLatch latch(1);
RunOnPcpHandlerThread([this, client, service_id, metadata,
&latch]() {
RunOnPcpHandlerThread([this, client, service_id, metadata, &latch]() {
InjectEndpointImpl(client, service_id, metadata);
latch.CountDown();
});
@@ -344,7 +343,7 @@ Status BasePcpHandler::RequestConnection(ClientProxy* client,
BluetoothUtils::ToString(options.remote_bluetooth_mac_address);
if (!remote_bluetooth_mac_address.empty()) {
if (AppendRemoteBluetoothMacAddressEndpoint(endpoint_id,
remote_bluetooth_mac_address))
remote_bluetooth_mac_address))
NEARBY_LOGS(INFO) << "Appended remote Bluetooth MAC Address endpoint "
<< "[" << remote_bluetooth_mac_address << "]";
}
@@ -357,6 +356,7 @@ Status BasePcpHandler::RequestConnection(ClientProxy* client,
ConnectImplResult connect_impl_result;
for (auto connect_endpoint : discovered_endpoints) {
if (!MediumSupported(connect_endpoint->medium, options)) continue;
connect_impl_result = ConnectImpl(client, connect_endpoint);
if (connect_impl_result.status.Ok()) {
channel = std::move(connect_impl_result.endpoint_channel);
@@ -380,7 +380,7 @@ Status BasePcpHandler::RequestConnection(ClientProxy* client,
// endpoint about ourselves.
Exception write_exception = WriteConnectionRequestFrame(
channel.get(), client->GetLocalEndpointId(), info.endpoint_info, nonce,
GetConnectionMediumsByPriority());
GetSupportedConnectionMediumsByPriority(options));
if (!write_exception.Ok()) {
NEARBY_LOG(INFO, "Failed to send connection request: id=%s",
endpoint_id.c_str());
@@ -430,6 +430,27 @@ Status BasePcpHandler::RequestConnection(ClientProxy* client,
return status;
}
bool BasePcpHandler::MediumSupported(const proto::connections::Medium& medium,
const ConnectionOptions& options) const {
for (auto supported_medium :
options.allowed.GetMediums(/* is supported = */true)) {
if (medium == supported_medium) return true;
}
return false;
}
std::vector<proto::connections::Medium>
BasePcpHandler::GetSupportedConnectionMediumsByPriority(
const ConnectionOptions& options) {
std::vector<proto::connections::Medium> supported_mediums_by_priority;
for (auto medium_by_priority : GetConnectionMediumsByPriority()) {
if (MediumSupported(medium_by_priority, options)) {
supported_mediums_by_priority.push_back(medium_by_priority);
}
}
return supported_mediums_by_priority;
}
// Get any single discovered endpoint for a given endpoint_id.
BasePcpHandler::DiscoveredEndpoint* BasePcpHandler::GetDiscoveredEndpoint(
const std::string& endpoint_id) {
@@ -1052,13 +1073,8 @@ bool BasePcpHandler::AppendRemoteBluetoothMacAddressEndpoint(
auto bluetooth_endpoint =
std::make_shared<BluetoothEndpoint>(BluetoothEndpoint{
{
endpoint_id,
endpoint->endpoint_info,
endpoint->service_id,
proto::connections::Medium::BLUETOOTH,
WebRtcState::kUnconnectable
},
{endpoint_id, endpoint->endpoint_info, endpoint->service_id,
proto::connections::Medium::BLUETOOTH, WebRtcState::kUnconnectable},
remote_bluetooth_device,
});
@@ -1083,20 +1099,12 @@ bool BasePcpHandler::AppendWebRTCEndpoint(const std::string& endpoint_id) {
}
if (!should_connect_web_rtc) return false;
auto webrtc_endpoint =
std::make_shared<WebRtcEndpoint>(WebRtcEndpoint{
{
endpoint_id,
endpoint->endpoint_info,
endpoint->service_id,
proto::connections::Medium::WEB_RTC,
WebRtcState::kConnectable
},
CreatePeerIdFromAdvertisement(
endpoint->service_id,
endpoint->endpoint_id,
endpoint->endpoint_info),
});
auto webrtc_endpoint = std::make_shared<WebRtcEndpoint>(WebRtcEndpoint{
{endpoint_id, endpoint->endpoint_info, endpoint->service_id,
proto::connections::Medium::WEB_RTC, WebRtcState::kConnectable},
CreatePeerIdFromAdvertisement(endpoint->service_id, endpoint->endpoint_id,
endpoint->endpoint_info),
});
discovered_endpoints_.emplace(endpoint_id, std::move(webrtc_endpoint));
return true;
+7 -4
View File
@@ -132,8 +132,7 @@ class BasePcpHandler : public PcpHandler,
// otherwise does nothing.
void StopDiscovery(ClientProxy* client) override;
void InjectEndpoint(ClientProxy* client,
const std::string& service_id,
void InjectEndpoint(ClientProxy* client, const std::string& service_id,
const OutOfBandConnectionMetadata& metadata) override;
// Requests a newly discovered remote endpoint it to form a connection.
@@ -294,8 +293,7 @@ class BasePcpHandler : public PcpHandler,
// @PcpHandlerThread
virtual Status InjectEndpointImpl(
ClientProxy* client,
const std::string& service_id,
ClientProxy* client, const std::string& service_id,
const OutOfBandConnectionMetadata& metadata) = 0;
// @PcpHandlerThread
@@ -480,6 +478,11 @@ class BasePcpHandler : public PcpHandler,
void WaitForLatch(const std::string& method_name, CountDownLatch* latch);
Status WaitForResult(const std::string& method_name, std::int64_t client_id,
Future<Status>* future);
bool MediumSupported(const proto::connections::Medium& medium,
const ConnectionOptions& options) const;
std::vector<proto::connections::Medium>
GetSupportedConnectionMediumsByPriority(
const ConnectionOptions& options);
AtomicReference<Medium> bwu_medium_{Medium::UNKNOWN_MEDIUM};
ScheduledExecutor alarm_executor_;
+70
View File
@@ -301,6 +301,13 @@ void BwuManager::OnIncomingConnection(
return;
}
if (!WriteClientIntroductionAckFrame(channel)) {
// This was never a fully EstablishedConnection, no need to provide a
// closure reason.
channel->Close();
return;
}
const std::string& endpoint_id = introduction.endpoint_id();
auto item = in_progress_upgrades_.extract(endpoint_id);
if (item.empty()) return;
@@ -424,6 +431,22 @@ BwuManager::ProcessBwuPathAvailableEventInternal(
return {};
}
if (upgrade_path_info.supports_client_introduction_ack()) {
if (!ReadClientIntroductionAckFrame(channel.get())) {
// This was never a fully EstablishedConnection, no need to provide a
// closure reason.
channel->Close();
NEARBY_LOG(
ERROR,
"Failed to read BWU_NEGOTIATION.CLIENT_INTRODUCTION_ACK OfflineFrame "
"to newly-created EndpointChannel %s, aborting upgrade.",
channel->GetName().c_str());
return {};
}
}
NEARBY_LOG(
INFO,
"Successfully wrote BWU_NEGOTIATION.CLIENT_INTRODUCTION OfflineFrame to "
@@ -473,19 +496,66 @@ void BwuManager::RunUpgradeFailedProtocol(
bool BwuManager::ReadClientIntroductionFrame(EndpointChannel* channel,
ClientIntroduction& introduction) {
CancelableAlarm timeout_alarm(
"BwuManager::ReadClientIntroductionFrame",
[channel]() {
NEARBY_LOG(
ERROR,
"In BandwidthUpgradeManager, failed to read the "
"ClientIntroductionFrame after %d seconds. Timing out and closing "
"EndpointChannel %s.",
kReadClientIntroductionFrameTimeout, channel->GetType().c_str());
channel->Close();
},
kReadClientIntroductionFrameTimeout, &alarm_executor_);
auto data = channel->Read();
timeout_alarm.Cancel();
if (!data.ok()) return false;
auto transfer(parser::FromBytes(data.result()));
if (!transfer.ok()) return false;
OfflineFrame frame = transfer.result();
if (!frame.has_v1() || !frame.v1().has_bandwidth_upgrade_negotiation())
return false;
if (frame.v1().bandwidth_upgrade_negotiation().event_type() !=
BandwidthUpgradeNegotiationFrame::CLIENT_INTRODUCTION)
return false;
const auto& frame_intro =
frame.v1().bandwidth_upgrade_negotiation().client_introduction();
introduction = frame_intro;
return true;
}
bool BwuManager::ReadClientIntroductionAckFrame(EndpointChannel* channel) {
CancelableAlarm timeout_alarm(
"BwuManager::ReadClientIntroductionAckFrame",
[channel]() {
NEARBY_LOG(ERROR,
"In BandwidthUpgradeManager, failed to read the "
"ClientIntroductionAckFrame after %d seconds. Timing out "
"and closing EndpointChannel %s.",
kReadClientIntroductionFrameTimeout,
channel->GetType().c_str());
channel->Close();
},
kReadClientIntroductionFrameTimeout, &alarm_executor_);
auto data = channel->Read();
timeout_alarm.Cancel();
if (!data.ok()) return false;
auto transfer(parser::FromBytes(data.result()));
if (!transfer.ok()) return false;
OfflineFrame frame = transfer.result();
if (!frame.has_v1() || !frame.v1().has_bandwidth_upgrade_negotiation())
return false;
if (frame.v1().bandwidth_upgrade_negotiation().event_type() !=
BandwidthUpgradeNegotiationFrame::CLIENT_INTRODUCTION_ACK)
return false;
return true;
}
bool BwuManager::WriteClientIntroductionAckFrame(EndpointChannel* channel) {
return channel->Write(parser::ForBwuIntroductionAck()).Ok();
}
void BwuManager::ProcessLastWriteToPriorChannelEvent(
ClientProxy* client, const std::string& endpoint_id) {
// By this point in the upgrade protocol, there is the guarantee that both
+4
View File
@@ -99,6 +99,8 @@ class BwuManager : public EndpointManager::FrameProcessor {
void Shutdown();
private:
static constexpr absl::Duration kReadClientIntroductionFrameTimeout =
absl::Seconds(5);
BwuHandler* SetCurrentBwuHandler(Medium medium);
void InitBwuHandlers();
void RunOnBwuManagerThread(std::function<void()> runnable);
@@ -143,6 +145,8 @@ class BwuManager : public EndpointManager::FrameProcessor {
const std::string& endpoint_id);
bool ReadClientIntroductionFrame(EndpointChannel* endpoint_channel,
ClientIntroduction& introduction);
bool ReadClientIntroductionAckFrame(EndpointChannel* endpoint_channel);
bool WriteClientIntroductionAckFrame(EndpointChannel* endpoint_channel);
void ProcessEndpointDisconnection(ClientProxy* client,
const std::string& endpoint_id,
CountDownLatch* barrier);
@@ -346,6 +346,18 @@ bool BluetoothClassic::StopAcceptingConnections(
BluetoothSocket BluetoothClassic::Connect(BluetoothDevice& bluetooth_device,
const std::string& service_name) {
for (int attempts_count = 0; attempts_count < kConnectAttemptsLimit;
attempts_count++) {
auto wrapper_result = AttemptToConnect(bluetooth_device, service_name);
if (wrapper_result.IsValid()) {
return wrapper_result;
}
}
return BluetoothSocket();
}
BluetoothSocket BluetoothClassic::AttemptToConnect(
BluetoothDevice& bluetooth_device, const std::string& service_name) {
MutexLock lock(&mutex_);
NEARBY_LOG(INFO, "BluetoothClassic::Connect: device=%p", &bluetooth_device);
// Socket to return. To allow for NRVO to work, it has to be a single object.
+12 -2
View File
@@ -105,8 +105,8 @@ class BluetoothClassic {
return adapter_.IsValid();
}
// Establishes connection to BT service that was might be started on another
// device with StartAcceptingConnections() using the same service_name.
// Establishes connection to BT service with internal retry for maximum
// attempts of kConnectAttemptsLimit.
// Blocks until connection is established, or server-side is terminated.
// Returns socket instance. On success, BluetoothSocket.IsValid() return true.
// Called by client.
@@ -126,6 +126,8 @@ class BluetoothClassic {
static constexpr int kMaxConcurrentAcceptLoops = 5;
static constexpr int kConnectAttemptsLimit = 3;
// Constructs UUID object from arbitrary string, using MD5 hash, and then
// converts UUID to a readable UUID string and returns it.
static std::string GenerateUuidFromString(const std::string& data);
@@ -160,6 +162,14 @@ class BluetoothClassic {
// Returns true if device is currently in discovery mode.
bool IsDiscovering() const ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
// Establishes connection to BT service that was might be started on another
// device with StartAcceptingConnections() using the same service_name.
// Blocks until connection is established, or server-side is terminated.
// Returns socket instance. On success, BluetoothSocket.IsValid() return true.
// Called by client.
BluetoothSocket AttemptToConnect(BluetoothDevice& bluetooth_device,
const std::string& service_name);
mutable Mutex mutex_;
BluetoothRadio& radio_ ABSL_GUARDED_BY(mutex_);
BluetoothAdapter& adapter_ ABSL_GUARDED_BY(mutex_){
+19
View File
@@ -160,6 +160,7 @@ ByteArray ForBwuWifiHotspotPathAvailable(const std::string& ssid,
BandwidthUpgradeNegotiationFrame::UPGRADE_PATH_AVAILABLE);
auto* upgrade_path_info = sub_frame->mutable_upgrade_path_info();
upgrade_path_info->set_medium(UpgradePathInfo::WIFI_HOTSPOT);
upgrade_path_info->set_supports_client_introduction_ack(true);
upgrade_path_info->set_supports_disabling_encryption(
supports_disabling_encryption);
auto* wifi_hotspot_credentials =
@@ -184,6 +185,7 @@ ByteArray ForBwuWifiLanPathAvailable(const std::string& ip_address,
BandwidthUpgradeNegotiationFrame::UPGRADE_PATH_AVAILABLE);
auto* upgrade_path_info = sub_frame->mutable_upgrade_path_info();
upgrade_path_info->set_medium(UpgradePathInfo::WIFI_LAN);
upgrade_path_info->set_supports_client_introduction_ack(true);
auto* wifi_lan_socket = upgrade_path_info->mutable_wifi_lan_socket();
wifi_lan_socket->set_ip_address(ip_address);
wifi_lan_socket->set_wifi_port(port);
@@ -205,6 +207,7 @@ ByteArray ForBwuWifiAwarePathAvailable(const std::string& service_id,
BandwidthUpgradeNegotiationFrame::UPGRADE_PATH_AVAILABLE);
auto* upgrade_path_info = sub_frame->mutable_upgrade_path_info();
upgrade_path_info->set_medium(UpgradePathInfo::WIFI_AWARE);
upgrade_path_info->set_supports_client_introduction_ack(true);
upgrade_path_info->set_supports_disabling_encryption(
supports_disabling_encryption);
auto* wifi_aware_credentials =
@@ -231,6 +234,7 @@ ByteArray ForBwuWifiDirectPathAvailable(const std::string& ssid,
BandwidthUpgradeNegotiationFrame::UPGRADE_PATH_AVAILABLE);
auto* upgrade_path_info = sub_frame->mutable_upgrade_path_info();
upgrade_path_info->set_medium(UpgradePathInfo::WIFI_DIRECT);
upgrade_path_info->set_supports_client_introduction_ack(true);
upgrade_path_info->set_supports_disabling_encryption(
supports_disabling_encryption);
auto* wifi_direct_credentials =
@@ -255,6 +259,7 @@ ByteArray ForBwuBluetoothPathAvailable(const std::string& service_id,
BandwidthUpgradeNegotiationFrame::UPGRADE_PATH_AVAILABLE);
auto* upgrade_path_info = sub_frame->mutable_upgrade_path_info();
upgrade_path_info->set_medium(UpgradePathInfo::BLUETOOTH);
upgrade_path_info->set_supports_client_introduction_ack(true);
auto* bluetooth_credentials =
upgrade_path_info->mutable_bluetooth_credentials();
bluetooth_credentials->set_mac_address(mac_address);
@@ -275,6 +280,7 @@ ByteArray ForBwuWebrtcPathAvailable(const std::string& peer_id,
BandwidthUpgradeNegotiationFrame::UPGRADE_PATH_AVAILABLE);
auto* upgrade_path_info = sub_frame->mutable_upgrade_path_info();
upgrade_path_info->set_medium(UpgradePathInfo::WEB_RTC);
upgrade_path_info->set_supports_client_introduction_ack(true);
auto* webrtc_credentials = upgrade_path_info->mutable_web_rtc_credentials();
webrtc_credentials->set_peer_id(peer_id);
auto* local_location_hint = webrtc_credentials->mutable_location_hint();
@@ -324,6 +330,19 @@ ByteArray ForBwuIntroduction(const std::string& endpoint_id) {
return ToBytes(std::move(frame));
}
ByteArray ForBwuIntroductionAck() {
OfflineFrame frame;
frame.set_version(OfflineFrame::V1);
auto* v1_frame = frame.mutable_v1();
v1_frame->set_type(V1Frame::BANDWIDTH_UPGRADE_NEGOTIATION);
auto* sub_frame = v1_frame->mutable_bandwidth_upgrade_negotiation();
sub_frame->set_event_type(
BandwidthUpgradeNegotiationFrame::CLIENT_INTRODUCTION_ACK);
return ToBytes(std::move(frame));
}
ByteArray ForBwuFailure(const UpgradePathInfo& info) {
OfflineFrame frame;
+1
View File
@@ -60,6 +60,7 @@ ByteArray ForControlPayloadTransfer(
// Builds Bandwidth Upgrade [BWU] messages.
ByteArray ForBwuIntroduction(const std::string& endpoint_id);
ByteArray ForBwuIntroductionAck();
ByteArray ForBwuWifiHotspotPathAvailable(const std::string& ssid,
const std::string& password,
std::int32_t port,
+5
View File
@@ -203,6 +203,7 @@ TEST(OfflineFramesTest, CanGenerateBwuWifiHotspotPathAvailable) {
gateway: "0.0.0.0"
>
supports_disabling_encryption: false
supports_client_introduction_ack: true
>
>
>)pb";
@@ -225,6 +226,7 @@ TEST(OfflineFramesTest, CanGenerateBwuWifiLanPathAvailable) {
upgrade_path_info: <
medium: WIFI_LAN
wifi_lan_socket: < ip_address: "\x01\x02\x03\x04" wifi_port: 1234 >
supports_client_introduction_ack: true
>
>
>)pb";
@@ -251,6 +253,7 @@ TEST(OfflineFramesTest, CanGenerateBwuWifiAwarePathAvailable) {
password: "password"
>
supports_disabling_encryption: false
supports_client_introduction_ack: true
>
>
>)pb";
@@ -279,6 +282,7 @@ TEST(OfflineFramesTest, CanGenerateBwuWifiDirectPathAvailable) {
frequency: 1000
>
supports_disabling_encryption: false
supports_client_introduction_ack: true
>
>
>)pb";
@@ -304,6 +308,7 @@ TEST(OfflineFramesTest, CanGenerateBwuBluetoothPathAvailable) {
service_name: "service"
mac_address: "\x11\x22\x33\x44\x55\x66"
>
supports_client_introduction_ack: true
>
>
>)pb";