Roll forward to cl/337147929

Signed-off-by: hai007 <hais@google.com>
This commit is contained in:
hai007
2020-10-14 13:08:04 -07:00
11 changed files with 149 additions and 98 deletions
+51 -25
View File
@@ -324,26 +324,18 @@ Status BasePcpHandler::RequestConnection(ClientProxy* client,
return;
}
if (discovery_options_.allowed.web_rtc) {
auto webrtc_endpoint = std::make_shared<WebRtcEndpoint>(
DiscoveredEndpoint{endpoint->endpoint_id, endpoint->endpoint_info,
endpoint->service_id,
proto::connections::Medium::WEB_RTC},
CreatePeerIdFromAdvertisement(endpoint->service_id,
endpoint->endpoint_id,
endpoint->endpoint_info));
OnEndpointFound(client, webrtc_endpoint);
}
auto remote_bluetooth_mac_address =
BluetoothUtils::ToString(options.remote_bluetooth_mac_address);
if (!remote_bluetooth_mac_address.empty()) {
if (AddRemoteBluetoothMacAddressEndpoint(endpoint_id,
if (AppendRemoteBluetoothMacAddressEndpoint(endpoint_id,
remote_bluetooth_mac_address))
NEARBY_LOGS(INFO) << "Appended remote Bluetooth MAC Address endpoint "
<< "[" << remote_bluetooth_mac_address << "]";
}
if (AppendWebRTCEndpoint(endpoint_id))
NEARBY_LOGS(INFO) << "Appended Web RTC endpoint.";
auto discovered_endpoints = GetDiscoveredEndpoints(endpoint_id);
std::unique_ptr<EndpointChannel> channel;
ConnectImplResult connect_impl_result;
@@ -1002,23 +994,20 @@ proto::connections::Medium BasePcpHandler::ChooseBestUpgradeMedium(
return proto::connections::Medium::UNKNOWN_MEDIUM;
}
bool BasePcpHandler::AddRemoteBluetoothMacAddressEndpoint(
std::string endpoint_id, std::string remote_bluetooth_mac_address) {
bool BasePcpHandler::AppendRemoteBluetoothMacAddressEndpoint(
const std::string& endpoint_id,
const std::string& remote_bluetooth_mac_address) {
if (!discovery_options_.allowed.bluetooth) {
return false;
}
auto endpoints = GetDiscoveredEndpoints(endpoint_id);
if (endpoints.empty()) {
NEARBY_LOGS(INFO) << "Cannot append remote Bluetooth MAC Address endpoint, "
"because endpointId "
<< endpoint_id << " has not been discovered "
<< "[" << remote_bluetooth_mac_address << "]";
auto it = discovered_endpoints_.equal_range(endpoint_id);
if (it.first == it.second) {
return false;
}
for (auto endpoint : endpoints) {
if (endpoint->medium == proto::connections::Medium::BLUETOOTH) {
auto endpoint = it.first->second.get();
for (auto item = it.first; item != it.second; item++) {
if (item->second->medium == proto::connections::Medium::BLUETOOTH) {
NEARBY_LOGS(INFO)
<< "Cannot append remote Bluetooth MAC Address endpoint, because the "
"endpoint has already been found over Bluetooth "
@@ -1042,9 +1031,10 @@ bool BasePcpHandler::AddRemoteBluetoothMacAddressEndpoint(
std::make_shared<BluetoothEndpoint>(BluetoothEndpoint{
{
endpoint_id,
endpoints[0]->endpoint_info,
endpoints[0]->service_id,
endpoint->endpoint_info,
endpoint->service_id,
proto::connections::Medium::BLUETOOTH,
WebRtcState::kUnconnectable
},
remote_bluetooth_device,
});
@@ -1053,6 +1043,42 @@ bool BasePcpHandler::AddRemoteBluetoothMacAddressEndpoint(
return true;
}
bool BasePcpHandler::AppendWebRTCEndpoint(const std::string& endpoint_id) {
if (!discovery_options_.allowed.web_rtc) {
return false;
}
bool should_connect_web_rtc = false;
auto it = discovered_endpoints_.equal_range(endpoint_id);
if (it.first == it.second) return false;
auto endpoint = it.first->second.get();
for (auto item = it.first; item != it.second; item++) {
if (item->second->web_rtc_state != WebRtcState::kUnconnectable) {
should_connect_web_rtc = true;
break;
}
}
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),
});
discovered_endpoints_.emplace(endpoint_id, std::move(webrtc_endpoint));
return true;
}
void BasePcpHandler::EvaluateConnectionResult(ClientProxy* client,
const std::string& endpoint_id,
bool can_close_immediately) {
+13 -5
View File
@@ -191,17 +191,20 @@ class BasePcpHandler : public PcpHandler,
struct DiscoveredEndpoint {
DiscoveredEndpoint(std::string endpoint_id, ByteArray endpoint_info,
std::string service_id,
proto::connections::Medium medium)
proto::connections::Medium medium,
WebRtcState web_rtc_state)
: endpoint_id(std::move(endpoint_id)),
endpoint_info(std::move(endpoint_info)),
service_id(std::move(service_id)),
medium(medium) {}
medium(medium),
web_rtc_state(web_rtc_state) {}
virtual ~DiscoveredEndpoint() = default;
std::string endpoint_id;
ByteArray endpoint_info;
std::string service_id;
proto::connections::Medium medium;
WebRtcState web_rtc_state;
};
struct BluetoothEndpoint : public DiscoveredEndpoint {
@@ -420,10 +423,15 @@ class BasePcpHandler : public PcpHandler,
const std::vector<proto::connections::Medium>& supported_mediums);
// Returns true if the bluetooth endpoint based on remote bluetooth mac
// address is created and added into discovered_endpoints_ with key
// address is created and appended into discovered_endpoints_ with key
// endpoint_id.
bool AddRemoteBluetoothMacAddressEndpoint(
std::string endpoint_id, std::string remote_bluetooth_mac_address);
bool AppendRemoteBluetoothMacAddressEndpoint(
const std::string& endpoint_id,
const std::string& remote_bluetooth_mac_address);
// Returns true if the webrtc endpoint is created and appended into
// discovered_endpoints_ with key endpoint_id.
bool AppendWebRTCEndpoint(const std::string& endpoint_id);
void ProcessPreConnectionInitiationFailure(const std::string& endpoint_id,
EndpointChannel* channel,
@@ -338,6 +338,7 @@ class BasePcpHandlerTest
info.endpoint_info,
"service",
discovered_medium,
WebRtcState::kUndefined,
},
MockContext{flag},
}));
+27 -24
View File
@@ -174,32 +174,35 @@ BleAdvertisement::BleAdvertisement(bool fast_advertisement,
BluetoothUtils::ToString(bluetooth_mac_address_bytes);
}
// The next 1 byte is supposed to be the length of the uwb_address.
std::uint32_t expected_uwb_address_length = base_input_stream.ReadUint8();
// If the length of uwb_address is not zero, then retrieve it.
if (expected_uwb_address_length != 0) {
uwb_address_ = base_input_stream.ReadBytes(expected_uwb_address_length);
if (uwb_address_.Empty() ||
uwb_address_.size() != expected_uwb_address_length) {
NEARBY_LOG(INFO,
"Cannot deserialize BleAdvertisement: "
"expected uwbAddress size to be %d bytes, got %" PRIu64,
expected_uwb_address_length, uwb_address_.size());
// The next 1 byte is supposed to be the length of the uwb_address. If the
// next byte is not available then it should be a fast advertisement and skip
// it for remaining bytes.
if (base_input_stream.IsAvailable(1)) {
std::uint32_t expected_uwb_address_length = base_input_stream.ReadUint8();
// If the length of uwb_address is not zero, then retrieve it.
if (expected_uwb_address_length != 0) {
uwb_address_ = base_input_stream.ReadBytes(expected_uwb_address_length);
if (uwb_address_.Empty() ||
uwb_address_.size() != expected_uwb_address_length) {
NEARBY_LOG(INFO,
"Cannot deserialize BleAdvertisement: "
"expected uwbAddress size to be %d bytes, got %" PRIu64,
expected_uwb_address_length, uwb_address_.size());
// Clear enpoint_id for validity.
endpoint_id_.clear();
return;
// Clear enpoint_id for validity.
endpoint_id_.clear();
return;
}
}
}
// The next 1 byte is extra field.
web_rtc_state_ = WebRtcState::kUndefined;
if (!fast_advertisement_) {
if (base_input_stream.IsAvailable(kExtraFieldLength)) {
auto extra_field = static_cast<char>(base_input_stream.ReadUint8());
web_rtc_state_ = (extra_field & kWebRtcConnectableFlagBitmask) == 1
? WebRtcState::kConnectable
: WebRtcState::kUnconnectable;
// The next 1 byte is extra field.
if (!fast_advertisement_) {
if (base_input_stream.IsAvailable(kExtraFieldLength)) {
auto extra_field = static_cast<char>(base_input_stream.ReadUint8());
web_rtc_state_ = (extra_field & kWebRtcConnectableFlagBitmask) == 1
? WebRtcState::kConnectable
: WebRtcState::kUnconnectable;
}
}
}
@@ -247,7 +250,7 @@ BleAdvertisement::operator ByteArray() const {
if (!uwb_address_.Empty()) {
absl::StrAppend(&out, std::string(1, uwb_address_.size()));
absl::StrAppend(&out, std::string(uwb_address_));
} else {
} else if (!fast_advertisement_) {
// Write UWB address with length 0 to be able to read the next field when
// decode.
absl::StrAppend(&out, std::string(1, uwb_address_.size()));
+1 -1
View File
@@ -76,7 +76,7 @@ class BwuHandler {
struct BwuNotifications {
std::function<void(ClientProxy* client,
IncomingSocketConnection* connection)>
std::unique_ptr<IncomingSocketConnection> connection)>
incoming_connection_cb;
};
};
+3 -3
View File
@@ -276,9 +276,9 @@ void BwuManager::OnBwuNegotiationFrame(ClientProxy* client,
void BwuManager::OnIncomingConnection(
ClientProxy* client,
BwuHandler::IncomingSocketConnection* mutable_connection) {
auto connection = std::make_shared<BwuHandler::IncomingSocketConnection>(
std::move(*mutable_connection));
std::unique_ptr<BwuHandler::IncomingSocketConnection> mutable_connection) {
std::shared_ptr<BwuHandler::IncomingSocketConnection> connection(
mutable_connection.release());
RunOnBwuManagerThread([this, client, connection]() {
EndpointChannel* channel = connection->channel.get();
if (channel == nullptr) {
+4 -2
View File
@@ -15,6 +15,7 @@
#ifndef CORE_V2_INTERNAL_BWU_MANAGER_H_
#define CORE_V2_INTERNAL_BWU_MANAGER_H_
#include <memory>
#include <string>
#include <vector>
@@ -121,8 +122,9 @@ class BwuManager : public EndpointManager::FrameProcessor {
// Common functionality to take an incoming connection and go through the
// upgrade process. This is a callback, invoked by concrete handlers, once
// connection is available.
void OnIncomingConnection(ClientProxy* client,
BwuHandler::IncomingSocketConnection* connection);
void OnIncomingConnection(
ClientProxy* client,
std::unique_ptr<BwuHandler::IncomingSocketConnection> mutable_connection);
void RunUpgradeProtocol(ClientProxy* client, const std::string& endpoint_id,
std::unique_ptr<EndpointChannel> new_channel);
+37 -30
View File
@@ -91,19 +91,7 @@ BasePcpHandler::StartOperationResult P2pClusterPcpHandler::StartAdvertisingImpl(
const ConnectionOptions& options) {
std::vector<proto::connections::Medium> mediums_started_successfully;
if (options.allowed.wifi_lan) {
const ByteArray wifi_lan_hash =
GenerateHash(service_id, WifiLanServiceInfo::kServiceIdHashLength);
proto::connections::Medium wifi_lan_medium =
StartWifiLanAdvertising(client, service_id, wifi_lan_hash,
local_endpoint_id, local_endpoint_info);
if (wifi_lan_medium != proto::connections::UNKNOWN_MEDIUM) {
NEARBY_LOG(INFO,
"P2pClusterPcpHandler::StartAdvertisingImpl: WifiLan added");
mediums_started_successfully.push_back(wifi_lan_medium);
}
}
WebRtcState web_rtc_state{WebRtcState::kUnconnectable};
if (options.allowed.web_rtc) {
proto::connections::Medium webrtc_medium =
StartListeningForWebRtcConnections(
@@ -112,15 +100,29 @@ BasePcpHandler::StartOperationResult P2pClusterPcpHandler::StartAdvertisingImpl(
NEARBY_LOG(INFO,
"P2pClusterPcpHandler::StartAdvertisingImpl: WebRtc added");
mediums_started_successfully.push_back(webrtc_medium);
web_rtc_state = WebRtcState::kConnectable;
}
}
if (options.allowed.wifi_lan) {
const ByteArray wifi_lan_hash =
GenerateHash(service_id, WifiLanServiceInfo::kServiceIdHashLength);
proto::connections::Medium wifi_lan_medium = StartWifiLanAdvertising(
client, service_id, wifi_lan_hash, local_endpoint_id,
local_endpoint_info, web_rtc_state);
if (wifi_lan_medium != proto::connections::UNKNOWN_MEDIUM) {
NEARBY_LOG(INFO,
"P2pClusterPcpHandler::StartAdvertisingImpl: WifiLan added");
mediums_started_successfully.push_back(wifi_lan_medium);
}
}
if (options.allowed.bluetooth) {
const ByteArray bluetooth_hash =
GenerateHash(service_id, BluetoothDeviceName::kServiceIdHashLength);
proto::connections::Medium bluetooth_medium =
StartBluetoothAdvertising(client, service_id, bluetooth_hash,
local_endpoint_id, local_endpoint_info);
proto::connections::Medium bluetooth_medium = StartBluetoothAdvertising(
client, service_id, bluetooth_hash, local_endpoint_id,
local_endpoint_info, web_rtc_state);
if (bluetooth_medium != proto::connections::UNKNOWN_MEDIUM) {
NEARBY_LOG(INFO, "P2pClusterPcpHandler::StartAdvertisingImpl: BT added");
mediums_started_successfully.push_back(bluetooth_medium);
@@ -128,8 +130,9 @@ BasePcpHandler::StartOperationResult P2pClusterPcpHandler::StartAdvertisingImpl(
}
if (options.allowed.ble) {
proto::connections::Medium ble_medium = StartBleAdvertising(
client, service_id, local_endpoint_id, local_endpoint_info, options);
proto::connections::Medium ble_medium =
StartBleAdvertising(client, service_id, local_endpoint_id,
local_endpoint_info, options, web_rtc_state);
if (ble_medium != proto::connections::UNKNOWN_MEDIUM) {
NEARBY_LOG(INFO, "P2pClusterPcpHandler::StartAdvertisingImpl: Ble added");
mediums_started_successfully.push_back(ble_medium);
@@ -236,6 +239,7 @@ void P2pClusterPcpHandler::BluetoothDeviceDiscoveredHandler(
device_name.GetEndpointInfo(),
service_id,
proto::connections::Medium::BLUETOOTH,
device_name.GetWebRtcState()
},
device,
}));
@@ -275,6 +279,7 @@ void P2pClusterPcpHandler::BluetoothDeviceLostHandler(
device_name.GetEndpointInfo(),
service_id,
proto::connections::Medium::BLUETOOTH,
WebRtcState::kUndefined
});
});
}
@@ -367,6 +372,7 @@ void P2pClusterPcpHandler::BlePeripheralDiscoveredHandler(
advertisement.GetEndpointInfo(),
service_id,
proto::connections::Medium::BLE,
advertisement.GetWebRtcState()
},
peripheral,
}));
@@ -396,6 +402,7 @@ void P2pClusterPcpHandler::BlePeripheralDiscoveredHandler(
advertisement.GetEndpointInfo(),
service_id,
proto::connections::Medium::BLUETOOTH,
advertisement.GetWebRtcState(),
},
remote_bluetooth_device,
}));
@@ -435,6 +442,7 @@ void P2pClusterPcpHandler::BlePeripheralLostHandler(
ble_endpoint_state.endpoint_info,
service_id,
proto::connections::Medium::BLE,
WebRtcState::kUndefined,
});
}
});
@@ -509,6 +517,7 @@ void P2pClusterPcpHandler::WifiLanServiceDiscoveredHandler(
service_info.GetEndpointInfo(),
service_id,
proto::connections::Medium::WIFI_LAN,
service_info.GetWebRtcState(),
},
service,
}));
@@ -550,6 +559,7 @@ void P2pClusterPcpHandler::WifiLanServiceLostHandler(
service_info.GetEndpointInfo(),
service_id,
proto::connections::Medium::WIFI_LAN,
WebRtcState::kUndefined,
});
});
}
@@ -680,7 +690,7 @@ BasePcpHandler::ConnectImplResult P2pClusterPcpHandler::ConnectImpl(
proto::connections::Medium P2pClusterPcpHandler::StartBluetoothAdvertising(
ClientProxy* client, const std::string& service_id,
const ByteArray& service_id_hash, const std::string& local_endpoint_id,
const ByteArray& local_endpoint_info) {
const ByteArray& local_endpoint_info, WebRtcState web_rtc_state) {
// Start listening for connections before advertising in case a connection
// request comes in very quickly.
NEARBY_LOG(
@@ -732,10 +742,9 @@ proto::connections::Medium P2pClusterPcpHandler::StartBluetoothAdvertising(
absl::BytesToHexString(local_endpoint_info.data()).c_str());
// Generate a BluetoothDeviceName with which to become Bluetooth discoverable.
// TODO(b/169550050): Implement UWBAddress.
// TODO(b/169303359): Implement WebRtcState.
std::string device_name(BluetoothDeviceName(
kBluetoothDeviceNameVersion, GetPcp(), local_endpoint_id, service_id_hash,
local_endpoint_info, ByteArray{}, WebRtcState::kUnconnectable));
local_endpoint_info, ByteArray{}, web_rtc_state));
if (device_name.empty()) {
NEARBY_LOG(INFO,
"P2pClusterPcpHandler::StartBluetoothAdvertising: generate "
@@ -811,7 +820,7 @@ BasePcpHandler::ConnectImplResult P2pClusterPcpHandler::BluetoothConnectImpl(
proto::connections::Medium P2pClusterPcpHandler::StartBleAdvertising(
ClientProxy* client, const std::string& service_id,
const std::string& local_endpoint_id, const ByteArray& local_endpoint_info,
const ConnectionOptions& options) {
const ConnectionOptions& options, WebRtcState web_rtc_state) {
bool fast_advertisement = !options.fast_advertisement_service_uuid.empty();
PowerLevel power_level =
options.low_power ? PowerLevel::kLowPower : PowerLevel::kHighPower;
@@ -919,11 +928,10 @@ proto::connections::Medium P2pClusterPcpHandler::StartBleAdvertising(
ShouldAdvertiseBluetoothMacOverBle(power_level))
bluetooth_mac_address = bluetooth_medium_.GetMacAddress();
// TODO(b/169303359): Implement WebRtcState.
advertisement_bytes = ByteArray(BleAdvertisement(
kBleAdvertisementVersion, GetPcp(), service_id_hash, local_endpoint_id,
local_endpoint_info, bluetooth_mac_address, ByteArray{},
WebRtcState::kUnconnectable));
advertisement_bytes = ByteArray(
BleAdvertisement(kBleAdvertisementVersion, GetPcp(), service_id_hash,
local_endpoint_id, local_endpoint_info,
bluetooth_mac_address, ByteArray{}, web_rtc_state));
}
if (advertisement_bytes.Empty()) {
NEARBY_LOG(INFO,
@@ -993,7 +1001,7 @@ BasePcpHandler::ConnectImplResult P2pClusterPcpHandler::BleConnectImpl(
proto::connections::Medium P2pClusterPcpHandler::StartWifiLanAdvertising(
ClientProxy* client, const std::string& service_id,
const ByteArray& service_id_hash, const std::string& local_endpoint_id,
const ByteArray& local_endpoint_info) {
const ByteArray& local_endpoint_info, WebRtcState web_rtc_state) {
// Start listening for connections before advertising in case a connection
// request comes in very quickly.
NEARBY_LOG(INFO,
@@ -1045,10 +1053,9 @@ proto::connections::Medium P2pClusterPcpHandler::StartWifiLanAdvertising(
absl::BytesToHexString(local_endpoint_info.data()).c_str());
// Generate a WifiLanServiceInfo with which to become WifiLan discoverable.
// TODO(b/169550050): Implement UWBAddress.
// TODO(b/169303359): Implement WebRtcState.
std::string service_info_name(WifiLanServiceInfo(
kWifiLanServiceInfoVersion, GetPcp(), local_endpoint_id, service_id_hash,
local_endpoint_info, ByteArray{}, WebRtcState::kUnconnectable));
local_endpoint_info, ByteArray{}, web_rtc_state));
if (service_info_name.empty()) {
NEARBY_LOG(INFO,
"P2pClusterPcpHandler::StartWifiLanAdvertising: generate "
@@ -150,7 +150,7 @@ class P2pClusterPcpHandler : public BasePcpHandler {
proto::connections::Medium StartBluetoothAdvertising(
ClientProxy* client, const std::string& service_id,
const ByteArray& service_id_hash, const std::string& local_endpoint_id,
const ByteArray& local_endpoint_info);
const ByteArray& local_endpoint_info, WebRtcState web_rtc_state);
proto::connections::Medium StartBluetoothDiscovery(
BluetoothDiscoveredDeviceCallback callback, ClientProxy* client,
const std::string& service_id);
@@ -172,7 +172,8 @@ class P2pClusterPcpHandler : public BasePcpHandler {
proto::connections::Medium StartBleAdvertising(
ClientProxy* client, const std::string& service_id,
const std::string& local_endpoint_id,
const ByteArray& local_endpoint_info, const ConnectionOptions& options);
const ByteArray& local_endpoint_info, const ConnectionOptions& options,
WebRtcState web_rtc_state);
proto::connections::Medium StartBleScanning(
BleDiscoveredPeripheralCallback callback, ClientProxy* client,
const std::string& service_id,
@@ -192,7 +193,7 @@ class P2pClusterPcpHandler : public BasePcpHandler {
proto::connections::Medium StartWifiLanAdvertising(
ClientProxy* client, const std::string& service_id,
const ByteArray& service_id_hash, const std::string& local_endpoint_id,
const ByteArray& local_endpoint_info);
const ByteArray& local_endpoint_info, WebRtcState web_rtc_state);
proto::connections::Medium StartWifiLanDiscovery(
WifiLanDiscoveredServiceCallback callback, ClientProxy* client,
const std::string& service_id);
+6 -4
View File
@@ -53,11 +53,13 @@ void WebrtcBwuHandler::OnIncomingWebrtcConnection(
mediums::WebRtcSocketWrapper socket) {
std::string service_id = Utils::UnwrapUpgradeServiceId(upgrade_service_id);
auto channel = std::make_unique<WebRtcEndpointChannel>(service_id, socket);
IncomingSocketConnection connection{
std::make_unique<WebrtcIncomingSocket>(service_id, socket),
std::move(channel)};
auto webrtc_socket =
std::make_unique<WebrtcIncomingSocket>(service_id, socket);
std::unique_ptr<IncomingSocketConnection> connection(
new IncomingSocketConnection{std::move(webrtc_socket),
std::move(channel)});
bwu_notifications_.incoming_connection_cb(client, &connection);
bwu_notifications_.incoming_connection_cb(client, std::move(connection));
}
// Called by BWU initiator. BT Medium is set up, and BWU request is prepared,