Pass over MediumMetadata to client_proxy for incoming connection

PiperOrigin-RevId: 489597576
This commit is contained in:
hai007
2022-11-18 17:41:42 -08:00
committed by Copybara-Service
parent 49d47d8e16
commit 5066b189f9
9 changed files with 241 additions and 50 deletions
+15 -13
View File
@@ -14,6 +14,7 @@
#ifndef CORE_CONNECTION_OPTIONS_H_
#define CORE_CONNECTION_OPTIONS_H_
#include <string>
#include <vector>
#include "connections/options_base.h"
#include "internal/platform/byte_array.h"
@@ -23,6 +24,19 @@ namespace location {
namespace nearby {
namespace connections {
struct ConnectionInfo {
std::string local_endpoint_id;
ByteArray local_endpoint_info;
std::int32_t nonce;
bool supports_5_ghz = false;
std::string bssid;
std::int32_t ap_frequency = -1;
std::string ip_address;
std::vector<proto::connections::Medium> supported_mediums;
std::int32_t keep_alive_interval_millis;
std::int32_t keep_alive_timeout_millis;
};
// Connection Options: used for both Advertising and Discovery.
// All fields are mutable, to make the type copy-assignable.
struct ConnectionOptions : public OptionsBase {
@@ -40,19 +54,7 @@ struct ConnectionOptions : public OptionsBase {
int keep_alive_timeout_millis = 0;
std::vector<Medium> GetMediums() const;
};
struct ConnectionInfo {
std::string local_endpoint_id;
ByteArray local_endpoint_info;
std::int32_t nonce;
bool supports_5_ghz = false;
std::string bssid;
std::int32_t ap_frequency = -1;
std::string ip_address;
std::vector<proto::connections::Medium> supported_mediums;
std::int32_t keep_alive_interval_millis;
std::int32_t keep_alive_timeout_millis;
ConnectionInfo connection_info;
};
} // namespace connections
+18 -21
View File
@@ -20,14 +20,17 @@
#include <cstdlib>
#include <limits>
#include <memory>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#include "securegcm/d2d_connection_context_v1.h"
#include "securegcm/ukey2_handshake.h"
#include "absl/container/flat_hash_set.h"
#include "absl/strings/escaping.h"
#include "absl/types/span.h"
#include "connections/connection_options.h"
#include "connections/implementation/mediums/utils.h"
#include "connections/implementation/offline_frames.h"
#include "internal/platform/base64_utils.h"
@@ -381,29 +384,9 @@ void BasePcpHandler::OnEncryptionSuccessRunnable(
// Set ourselves up so that we receive all acceptance/rejection messages
endpoint_manager_->RegisterFrameProcessor(V1Frame::CONNECTION_RESPONSE, this);
ConnectionOptions connection_options;
connection_options.strategy = connection_info.connection_options.strategy;
ConnectionOptions connection_options = connection_info.connection_options;
connection_options.allowed =
ComputeIntersectionOfSupportedMediums(connection_info);
connection_options.auto_upgrade_bandwidth =
connection_info.connection_options.auto_upgrade_bandwidth;
connection_options.enforce_topology_constraints =
connection_info.connection_options.enforce_topology_constraints;
connection_options.low_power = connection_info.connection_options.low_power;
connection_options.enable_bluetooth_listening =
connection_info.connection_options.enable_bluetooth_listening;
connection_options.enable_webrtc_listening =
connection_info.connection_options.enable_webrtc_listening;
connection_options.is_out_of_band_connection =
connection_info.connection_options.is_out_of_band_connection;
connection_options.remote_bluetooth_mac_address =
connection_info.connection_options.remote_bluetooth_mac_address;
connection_options.fast_advertisement_service_uuid =
connection_info.connection_options.fast_advertisement_service_uuid;
connection_options.keep_alive_interval_millis =
connection_info.connection_options.keep_alive_interval_millis;
connection_options.keep_alive_timeout_millis =
connection_info.connection_options.keep_alive_timeout_millis;
// Now we register our endpoint so that we can listen for both sides to
// accept.
@@ -1194,6 +1177,20 @@ Exception BasePcpHandler::OnIncomingConnection(
FeatureFlags::GetInstance().GetFlags().keep_alive_timeout_millis;
}
const MediumMetadata& medium_metadata = connection_request.medium_metadata();
ConnectionInfo& connection_info = connection_options.connection_info;
connection_info.supports_5_ghz = medium_metadata.supports_5_ghz();
connection_info.bssid = medium_metadata.bssid();
connection_info.ap_frequency = medium_metadata.ap_frequency();
connection_info.ip_address = medium_metadata.ip_address();
NEARBY_LOGS(INFO) << connection_request.endpoint_id()
<< "'s WIFI information: is_supports_5_ghz="
<< connection_info.supports_5_ghz
<< "; bssid=" << connection_info.bssid
<< "; ap_frequency=" << connection_info.ap_frequency
<< "Mhz; ip_address in bytes format="
<< connection_info.ip_address;
// We've successfully connected to the device, and are now about to jump on to
// the EncryptionRunner thread to start running our encryption protocol. We'll
// mark ourselves as pending in case we get another call to RequestConnection
+41 -1
View File
@@ -22,12 +22,12 @@
#include <string>
#include <utility>
#include "internal/analytics/event_logger.h"
#include "absl/container/flat_hash_map.h"
#include "absl/container/flat_hash_set.h"
#include "absl/strings/escaping.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/str_format.h"
#include "internal/analytics/event_logger.h"
#include "internal/platform/error_code_recorder.h"
#include "internal/platform/feature_flags.h"
#include "internal/platform/logging.h"
@@ -387,6 +387,46 @@ BooleanMediumSelector ClientProxy::GetUpgradeMediums(
return {};
}
bool ClientProxy::Is5GHzSupported(const std::string& endpoint_id) const {
MutexLock lock(&mutex_);
const Connection* item = LookupConnection(endpoint_id);
if (item != nullptr) {
return item->connection_options.connection_info.supports_5_ghz;
}
return false;
}
std::string ClientProxy::GetBssid(const std::string& endpoint_id) const {
MutexLock lock(&mutex_);
const Connection* item = LookupConnection(endpoint_id);
if (item != nullptr) {
return item->connection_options.connection_info.bssid;
}
return {};
}
std::int32_t ClientProxy::GetApFrequency(const std::string& endpoint_id) const {
MutexLock lock(&mutex_);
const Connection* item = LookupConnection(endpoint_id);
if (item != nullptr) {
return item->connection_options.connection_info.ap_frequency;
}
return -1;
}
std::string ClientProxy::GetIPAddress(const std::string& endpoint_id) const {
MutexLock lock(&mutex_);
const Connection* item = LookupConnection(endpoint_id);
if (item != nullptr) {
return item->connection_options.connection_info.ip_address;
}
return {};
}
bool ClientProxy::IsConnectedToEndpoint(const std::string& endpoint_id) const {
return ConnectionStatusMatches(endpoint_id, Connection::kConnected);
}
@@ -125,6 +125,14 @@ class ClientProxy final {
// Returns all mediums eligible for upgrade.
BooleanMediumSelector GetUpgradeMediums(const std::string& endpoint_id) const;
// Returns if this endpoint support 5G for WIFI.
bool Is5GHzSupported(const std::string& endpoint_id) const;
// Returns BSSID for this endpoint.
std::string GetBssid(const std::string& endpoint_id) const;
// Returns WIFI Frequency for this endpoint.
std::int32_t GetApFrequency(const std::string& endpoint_id) const;
// Returns IP Address in 4 bytes format for this endpoint.
std::string GetIPAddress(const std::string& endpoint_id) const;
// Returns true if it's safe to send payloads to this endpoint.
bool IsConnectedToEndpoint(const std::string& endpoint_id) const;
// Returns all endpoints that can safely be sent payloads.
@@ -264,24 +264,46 @@ TEST_P(P2pClusterPcpHandlerTest, CanConnect) {
EXPECT_TRUE(discover_latch.Await(absl::Milliseconds(1000)).result());
EXPECT_EQ(endpoint_name_a, std::string{discovered.endpoint_info});
const std::string kBssid = "34:36:3B:C7:8C:71";
const std::int32_t kFreq = 5200;
constexpr char kIp4Bytes[] = {(char)192, (char)168, (char)1, (char)37};
const std::string kIpAddr4Bytes(kIp4Bytes);
connection_options_.connection_info.supports_5_ghz = true;
connection_options_.connection_info.bssid = kBssid;
connection_options_.connection_info.ap_frequency = kFreq;
connection_options_.connection_info.ip_address = kIpAddr4Bytes;
client_b_.AddCancellationFlag(discovered.endpoint_id);
handler_b.RequestConnection(
&client_b_, discovered.endpoint_id,
{
.endpoint_info = discovered.endpoint_info,
.listener =
{
.initiated_cb =
[&connect_latch](const std::string& endpoint_id,
const ConnectionResponseInfo& info) {
NEARBY_LOG(INFO,
"RequestConnection: initiated_cb called");
connect_latch.CountDown();
},
},
},
{.endpoint_info = discovered.endpoint_info,
.listener =
{
.initiated_cb =
[&connect_latch](const std::string& endpoint_id,
const ConnectionResponseInfo& info) {
NEARBY_LOG(INFO, "RequestConnection: initiated_cb called");
connect_latch.CountDown();
},
}},
connection_options_);
std::string client_b_local_endpoint = client_b_.GetLocalEndpointId();
EXPECT_TRUE(connect_latch.Await(absl::Milliseconds(1000)).result());
EXPECT_TRUE(client_b_.Is5GHzSupported(discovered.endpoint_id));
EXPECT_EQ(client_b_.GetBssid(discovered.endpoint_id), kBssid);
EXPECT_EQ(client_b_.GetApFrequency(discovered.endpoint_id), kFreq);
EXPECT_EQ(client_b_.GetIPAddress(discovered.endpoint_id), kIpAddr4Bytes);
EXPECT_EQ(client_a_.Is5GHzSupported(client_b_local_endpoint),
mediums_b.GetWifi().GetCapability().supports_5_ghz);
EXPECT_EQ(client_a_.GetBssid(client_b_local_endpoint),
mediums_b.GetWifi().GetInformation().bssid);
EXPECT_EQ(client_a_.GetApFrequency(client_b_local_endpoint),
mediums_b.GetWifi().GetInformation().ap_frequency);
EXPECT_EQ(client_a_.GetIPAddress(client_b_local_endpoint),
mediums_b.GetWifi().GetInformation().ip_address_4_bytes);
bwu_a.Shutdown();
bwu_b.Shutdown();
env_.Stop();
@@ -67,6 +67,7 @@ cc_library(
"bluetooth_adapter.h",
"bluetooth_classic.h",
"credential_storage_impl.h",
"wifi.h",
"wifi_direct.h",
"wifi_hotspot.h",
"wifi_lan.h",
@@ -51,6 +51,7 @@
#include "internal/platform/implementation/g3/mutex.h"
#include "internal/platform/implementation/g3/scheduled_executor.h"
#include "internal/platform/implementation/g3/single_thread_executor.h"
#include "internal/platform/implementation/g3/wifi.h"
#include "internal/platform/implementation/g3/wifi_direct.h"
#include "internal/platform/implementation/g3/wifi_hotspot.h"
#include "internal/platform/implementation/g3/wifi_lan.h"
@@ -172,7 +173,7 @@ ImplementationPlatform::CreateServerSyncMedium() {
}
std::unique_ptr<WifiMedium> ImplementationPlatform::CreateWifiMedium() {
return std::unique_ptr<WifiMedium>();
return std::make_unique<g3::WifiMedium>();
}
std::unique_ptr<WifiLanMedium> ImplementationPlatform::CreateWifiLanMedium() {
+120
View File
@@ -0,0 +1,120 @@
// Copyright 2022 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 PLATFORM_IMPL_G3_WIFI_H_
#define PLATFORM_IMPL_G3_WIFI_H_
#include <string>
#include <vector>
#include "absl/synchronization/mutex.h"
#include "internal/platform/implementation/wifi.h"
#include "internal/platform/logging.h"
#include "internal/platform/medium_environment.h"
namespace location {
namespace nearby {
namespace g3 {
class WifiMedium;
// Container of operations that can be performed over the Wifi medium.
class WifiMedium : public api::WifiMedium {
public:
WifiMedium() {
auto& env = MediumEnvironment::Instance();
const std::string ip_addr_4bytes = env.GetFakeIPAddress();
std::string ip_addr_dot_decimal;
if (!ip_addr_4bytes.empty()) {
for (auto byte : ip_addr_4bytes) {
if (!ip_addr_dot_decimal.empty())
absl::StrAppend(&ip_addr_dot_decimal, ".");
absl::StrAppend(&ip_addr_dot_decimal, absl::StrFormat("%d", byte));
}
}
wifi_capability_ = {true, false, true};
wifi_information_ = {true, "nearby_test_ap", "34:36:3B:C7:8C:77",
5230, ip_addr_dot_decimal, ip_addr_4bytes};
}
~WifiMedium() override = default;
WifiMedium(const WifiMedium&) = delete;
WifiMedium(WifiMedium&&) = delete;
WifiMedium& operator=(const WifiMedium&) = delete;
WifiMedium& operator=(WifiMedium&&) = delete;
// If the WiFi Adaptor supports to start a Wifi interface.
bool IsInterfaceValid() const override { return true; }
api::WifiCapability& GetCapability() override {
absl::MutexLock lock(&mutex_);
return wifi_capability_;
}
api::WifiInformation& GetInformation() override {
absl::MutexLock lock(&mutex_);
return wifi_information_;
}
std::string GetIpAddress() override {
absl::MutexLock lock(&mutex_);
return wifi_information_.ip_address_dot_decimal;
}
class ScanResultCallback : public api::WifiMedium::ScanResultCallback {
public:
// TODO(b/184975123): replace with real implementation.
~ScanResultCallback() override = default;
// TODO(b/184975123): replace with real implementation.
void OnScanResults(
const std::vector<api::WifiScanResult>& scan_results) override {}
};
// Does not take ownership of the passed-in scan_result_callback -- destroying
// that is up to the caller.
// TODO(b/184975123): replace with real implementation.
bool Scan(const api::WifiMedium::ScanResultCallback& scan_result_callback)
override {
return false;
}
// If 'password' is an empty string, none has been provided. Returns
// WifiConnectionStatus::CONNECTED on success, or the appropriate failure code
// otherwise.
// TODO(b/184975123): replace with real implementation.
api::WifiConnectionStatus ConnectToNetwork(
absl::string_view ssid, absl::string_view password,
api::WifiAuthType auth_type) override {
return api::WifiConnectionStatus::kUnknown;
}
// Blocks until it's certain of there being a connection to the internet, or
// returns false if it fails to do so.
//
// How this method wants to verify said connection is totally up to it (so it
// can feel free to ping whatever server, download whatever resource, etc.
// that it needs to gain confidence that the internet is reachable hereon in).
// TODO(b/184975123): replace with real implementation.
bool VerifyInternetConnectivity() override { return false; }
private:
absl::Mutex mutex_;
api::WifiCapability wifi_capability_ ABSL_GUARDED_BY(mutex_);
api::WifiInformation wifi_information_ ABSL_GUARDED_BY(mutex_);
};
} // namespace g3
} // namespace nearby
} // namespace location
#endif // PLATFORM_IMPL_G3_WIFI_H_
+1 -1
View File
@@ -28,7 +28,7 @@ TEST(WifiMediumTest, ConstructorDestructorWorks) {
if (wifi_a.IsValid() && wifi_b.IsValid()) {
// Make sure we can create 2 distinct mediums.
EXPECT_NE(&wifi_a.GetImpl(), &wifi_a.GetImpl());
EXPECT_NE(&wifi_a.GetImpl(), &wifi_b.GetImpl());
}
// TODO(b/233324423): Add test coverage for wifi.h
}