diff --git a/connections/connection_options.h b/connections/connection_options.h index 7132aed4..8d09e755 100644 --- a/connections/connection_options.h +++ b/connections/connection_options.h @@ -37,6 +37,8 @@ struct ConnectionInfo { std::int32_t keep_alive_interval_millis; std::int32_t keep_alive_timeout_millis; std::optional medium_role; + std::vector + supported_wifi_direct_auth_types; }; // Connection Options: used for both Advertising and Discovery. diff --git a/connections/implementation/base_pcp_handler.cc b/connections/implementation/base_pcp_handler.cc index ba1a6247..00a1e2b9 100644 --- a/connections/implementation/base_pcp_handler.cc +++ b/connections/implementation/base_pcp_handler.cc @@ -29,6 +29,7 @@ #include "absl/container/flat_hash_set.h" #include "absl/strings/escaping.h" #include "absl/strings/str_cat.h" +#include "absl/strings/str_join.h" #include "absl/strings/string_view.h" #include "absl/time/time.h" #include "absl/types/span.h" @@ -111,6 +112,7 @@ using ::location::nearby::connections::OsInfo; using ::location::nearby::connections::PresenceDevice; using ::location::nearby::connections::V1Frame; using ::location::nearby::proto::connections::OperationResultCode; +using ::location::nearby::proto::connections::WifiDirectAuthType; using ::securegcm::UKey2Handshake; BasePcpHandler::BasePcpHandler(Mediums* mediums, @@ -435,6 +437,52 @@ BooleanMediumSelector BasePcpHandler::ComputeIntersectionOfSupportedMediums( continue; } } + if (my_medium == + location::nearby::proto::connections::Medium::WIFI_DIRECT) { + auto remote_supported_wifi_direct_auth_types = + pending_connection_info.connection_options.connection_info + .supported_wifi_direct_auth_types; + LOG(INFO) << "Remote supported WifiDirect auth types: " + << absl::StrJoin( + remote_supported_wifi_direct_auth_types, ", ", + [](std::string* out, int auth_type) { + absl::StrAppend( + out, + WifiDirectAuthType_Name( + static_cast(auth_type))); + }); + auto local_supported_wifi_direct_auth_types = + mediums_->GetWifiDirect().GetSupportedWifiDirectAuthTypes(); + LOG(INFO) << "Local supported WifiDirect auth types: " + << absl::StrJoin( + local_supported_wifi_direct_auth_types, ", ", + [](std::string* out, int auth_type) { + absl::StrAppend( + out, + WifiDirectAuthType_Name( + static_cast(auth_type))); + }); + bool found_common_auth_type = false; + for (const auto& auth_type : local_supported_wifi_direct_auth_types) { + if (auth_type == WifiDirectAuthType::WIFI_DIRECT_TYPE_UNKNOWN) { + continue; + } + if (std::find(remote_supported_wifi_direct_auth_types.begin(), + remote_supported_wifi_direct_auth_types.end(), + auth_type) != + remote_supported_wifi_direct_auth_types.end()) { + LOG(INFO) << "Found common WifiDirect auth type: " + << WifiDirectAuthType_Name(auth_type); + mediums_->GetWifiDirect().SetPreferredWifiDirectAuthType(auth_type); + found_common_auth_type = true; + break; + } + } + if (!found_common_auth_type) { + LOG(INFO) << "No common WifiDirect auth type found, skip WifiDirect."; + continue; + } + } intersection.emplace(my_medium); } @@ -839,6 +887,17 @@ ConnectionInfo BasePcpHandler::FillConnectionInfo( } connection_info.supported_mediums = GetSupportedConnectionMediumsByPriority(connection_options); + if (NearbyFlags::GetInstance().GetBoolFlag( + config_package_nearby::nearby_connections_feature:: + kEnableWifiDirect)) { + connection_info.supported_wifi_direct_auth_types = + mediums_->GetWifiDirect().GetSupportedWifiDirectAuthTypes(); + VLOG(1) << "Set SupportedWifiDirectAuthTypes for WIFI_DIRECT: " + << absl::StrJoin(connection_info.supported_wifi_direct_auth_types, + ","); + } else { + connection_info.supported_wifi_direct_auth_types = {}; + } if (!NearbyFlags::GetInstance().GetBoolFlag( config_package_nearby::nearby_connections_feature:: @@ -2056,6 +2115,20 @@ Exception BasePcpHandler::OnIncomingConnection( << absl::BytesToHexString(connection_info.ip_address) << "; has no mediumRole"; } + connection_info.supported_wifi_direct_auth_types = + parser::MediumMetadataWFDAuthTypesToWFDAuthTypes(medium_metadata); + if (!connection_info.supported_wifi_direct_auth_types.empty()) { + LOG(INFO) << connection_request.endpoint_id() + << "'s supported WifiDirect auth types: " + << absl::StrJoin( + connection_info.supported_wifi_direct_auth_types, ", ", + [](std::string* out, int auth_type) { + absl::StrAppend( + out, + WifiDirectAuthType_Name( + static_cast(auth_type))); + }); + } // 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 diff --git a/connections/implementation/mediums/wifi_direct.cc b/connections/implementation/mediums/wifi_direct.cc index 874f9031..75f7b35d 100644 --- a/connections/implementation/mediums/wifi_direct.cc +++ b/connections/implementation/mediums/wifi_direct.cc @@ -16,6 +16,8 @@ #include #include +#include +#include #include "absl/strings/string_view.h" #include "internal/platform/cancellation_flag.h" @@ -31,6 +33,13 @@ namespace { using ::location::nearby::proto::connections::OperationResultCode; } // namespace +WifiDirect::WifiDirect() : is_go_started_(false), is_connected_to_go_(false) { + supported_wifi_direct_auth_types_ = medium_.GetSupportedWifiDirectAuthTypes(); + if (!supported_wifi_direct_auth_types_.empty()) { + preferred_wifi_direct_auth_type_ = + supported_wifi_direct_auth_types_.front(); + } +} WifiDirect::~WifiDirect() { while (!server_sockets_.empty()) { StopAcceptingConnections(server_sockets_.begin()->first); @@ -288,5 +297,15 @@ ErrorOr WifiDirect::Connect( return socket; } +bool WifiDirect::SetPreferredWifiDirectAuthType(WifiDirectAuthType auth_type) { + if (std::find(supported_wifi_direct_auth_types_.begin(), + supported_wifi_direct_auth_types_.end(), + auth_type) == supported_wifi_direct_auth_types_.end()) { + return false; + } + preferred_wifi_direct_auth_type_ = auth_type; + return true; +} + } // namespace connections } // namespace nearby diff --git a/connections/implementation/mediums/wifi_direct.h b/connections/implementation/mediums/wifi_direct.h index df2849b6..e85d2e64 100644 --- a/connections/implementation/mediums/wifi_direct.h +++ b/connections/implementation/mediums/wifi_direct.h @@ -16,6 +16,7 @@ #define CORE_INTERNAL_MEDIUMS_WIFI_DIRECT_H_ #include +#include #include "absl/base/thread_annotations.h" #include "absl/container/flat_hash_map.h" @@ -36,8 +37,10 @@ class WifiDirect { // Callback that is invoked when a new connection is accepted. using AcceptedConnectionCallback = absl::AnyInvocable; + using WifiDirectAuthType = + ::location::nearby::proto::connections::WifiDirectAuthType; - WifiDirect() : is_go_started_(false), is_connected_to_go_(false) {} + WifiDirect(); ~WifiDirect(); // Not copyable or movable WifiDirect(const WifiDirect&) = delete; @@ -94,6 +97,19 @@ class WifiDirect { WifiDirectCredentials* GetCredentials(absl::string_view service_id) ABSL_LOCKS_EXCLUDED(mutex_); + // Returns the supported WifiDirect auth types. + std::vector GetSupportedWifiDirectAuthTypes() { + return supported_wifi_direct_auth_types_; + } + + // Returns the preferred WifiDirect auth type. + WifiDirectAuthType GetPreferredWifiDirectAuthType() { + return preferred_wifi_direct_auth_type_; + } + + // Sets the preferred WifiDirect auth type. + bool SetPreferredWifiDirectAuthType(WifiDirectAuthType auth_type); + private: mutable Mutex mutex_; static constexpr int kMaxConcurrentAcceptLoops = 5; @@ -118,6 +134,10 @@ class WifiDirect { // used from accept_loops_runner_, and thus require pointer stability. absl::flat_hash_map server_sockets_ ABSL_GUARDED_BY(mutex_); + // The supported WifiDirect auth types. + std::vector supported_wifi_direct_auth_types_; + WifiDirectAuthType preferred_wifi_direct_auth_type_ = + WifiDirectAuthType::WIFI_DIRECT_TYPE_UNKNOWN; }; } // namespace connections } // namespace nearby diff --git a/connections/implementation/mediums/wifi_direct_test.cc b/connections/implementation/mediums/wifi_direct_test.cc index 930f3df1..358df01a 100644 --- a/connections/implementation/mediums/wifi_direct_test.cc +++ b/connections/implementation/mediums/wifi_direct_test.cc @@ -190,6 +190,40 @@ TEST_F(WifiDirectTest, CanStartGOTheOtherFailConnect) { EXPECT_TRUE(wifi_direct_a.StopWifiDirect()); } +TEST_F(WifiDirectTest, GetSupportedWifiDirectAuthTypes) { + WifiDirect wifi_direct; + auto supported_types = wifi_direct.GetSupportedWifiDirectAuthTypes(); + EXPECT_EQ(supported_types.size(), 1); + EXPECT_EQ(supported_types[0], + WifiDirect::WifiDirectAuthType::WIFI_DIRECT_WITH_PIN); +} + +TEST_F(WifiDirectTest, GetPreferredWifiDirectAuthType_Default) { + WifiDirect wifi_direct; + // Default should be the first supported type, which is WIFI_DIRECT_WITH_PIN + EXPECT_EQ(wifi_direct.GetPreferredWifiDirectAuthType(), + WifiDirect::WifiDirectAuthType::WIFI_DIRECT_WITH_PIN); +} + +TEST_F(WifiDirectTest, SetPreferredWifiDirectAuthType_Supported) { + WifiDirect wifi_direct; + // Attempt to set the preferred type to the already default/supported type. + EXPECT_TRUE(wifi_direct.SetPreferredWifiDirectAuthType( + WifiDirect::WifiDirectAuthType::WIFI_DIRECT_WITH_PIN)); + EXPECT_EQ(wifi_direct.GetPreferredWifiDirectAuthType(), + WifiDirect::WifiDirectAuthType::WIFI_DIRECT_WITH_PIN); +} + +TEST_F(WifiDirectTest, SetPreferredWifiDirectAuthType_Unsupported) { + WifiDirect wifi_direct; + // Attempt to set an unsupported type. + EXPECT_FALSE(wifi_direct.SetPreferredWifiDirectAuthType( + WifiDirect::WifiDirectAuthType::WIFI_DIRECT_WITH_PASSWORD)); + // Preferred type should remain the default. + EXPECT_EQ(wifi_direct.GetPreferredWifiDirectAuthType(), + WifiDirect::WifiDirectAuthType::WIFI_DIRECT_WITH_PIN); +} + } // namespace } // namespace connections } // namespace nearby diff --git a/connections/implementation/offline_frames.cc b/connections/implementation/offline_frames.cc index 1168a5cc..31611d69 100644 --- a/connections/implementation/offline_frames.cc +++ b/connections/implementation/offline_frames.cc @@ -86,7 +86,7 @@ V1Frame::FrameType GetFrameType(const OfflineFrame& frame) { ByteArray ForConnectionRequestConnections( const location::nearby::connections::ConnectionsDevice& proto_connections_device, - const ConnectionInfo& conection_info) { + const ConnectionInfo& connection_info) { OfflineFrame frame; frame.set_version(OfflineFrame::V1); @@ -97,42 +97,49 @@ ByteArray ForConnectionRequestConnections( connection_request->mutable_connections_device()->MergeFrom( proto_connections_device); } - if (!conection_info.local_endpoint_id.empty()) { - connection_request->set_endpoint_id(conection_info.local_endpoint_id); + if (!connection_info.local_endpoint_id.empty()) { + connection_request->set_endpoint_id(connection_info.local_endpoint_id); } - if (!conection_info.local_endpoint_info.Empty()) { + if (!connection_info.local_endpoint_info.Empty()) { connection_request->set_endpoint_name( - conection_info.local_endpoint_info.string_data()); + connection_info.local_endpoint_info.string_data()); connection_request->set_endpoint_info( - conection_info.local_endpoint_info.string_data()); + connection_info.local_endpoint_info.string_data()); } - connection_request->set_nonce(conection_info.nonce); + connection_request->set_nonce(connection_info.nonce); auto* medium_metadata = connection_request->mutable_medium_metadata(); - medium_metadata->set_supports_5_ghz(conection_info.supports_5_ghz); - if (!conection_info.bssid.empty()) - medium_metadata->set_bssid(conection_info.bssid); - medium_metadata->set_ap_frequency(conection_info.ap_frequency); - if (!conection_info.ip_address.empty()) - medium_metadata->set_ip_address(conection_info.ip_address); + medium_metadata->set_supports_5_ghz(connection_info.supports_5_ghz); + if (!connection_info.bssid.empty()) + medium_metadata->set_bssid(connection_info.bssid); + medium_metadata->set_ap_frequency(connection_info.ap_frequency); + if (!connection_info.ip_address.empty()) + medium_metadata->set_ip_address(connection_info.ip_address); if (NearbyFlags::GetInstance().GetBoolFlag( config_package_nearby::nearby_connections_feature:: kEnableDynamicRoleSwitch) && - conection_info.medium_role.has_value()) { + connection_info.medium_role.has_value()) { medium_metadata->mutable_medium_role()->MergeFrom( - conection_info.medium_role.value()); + connection_info.medium_role.value()); } - if (!conection_info.supported_mediums.empty()) { - for (const auto& medium : conection_info.supported_mediums) { + if (!connection_info.supported_wifi_direct_auth_types.empty()) { + for (const auto& auth_type : + connection_info.supported_wifi_direct_auth_types) { + medium_metadata->add_supported_wifi_direct_auth_types( + WFDAuthTypeToMediumMetadataWFDAuthType(auth_type)); + } + } + if (!connection_info.supported_mediums.empty()) { + for (const auto& medium : connection_info.supported_mediums) { connection_request->add_mediums(MediumToConnectionRequestMedium(medium)); } } - if (conection_info.keep_alive_interval_millis > 0) { + if (connection_info.keep_alive_interval_millis > 0) { connection_request->set_keep_alive_interval_millis( - conection_info.keep_alive_interval_millis); + connection_info.keep_alive_interval_millis); } - if (conection_info.keep_alive_timeout_millis > 0) { + if (connection_info.keep_alive_timeout_millis > 0) { connection_request->set_keep_alive_timeout_millis( - conection_info.keep_alive_timeout_millis); + connection_info.keep_alive_timeout_millis); } return ToBytes(std::move(frame)); @@ -364,14 +371,11 @@ ByteArray ForBwuWifiAwarePathAvailable(const std::string& service_id, return ToBytes(std::move(frame)); } -ByteArray ForBwuWifiDirectPathAvailable(const std::string& ssid, - const std::string& password, - std::int32_t port, - std::int32_t frequency, - bool supports_disabling_encryption, - const std::string& gateway, - const std::string& service_name, - const std::string& pin) { +ByteArray ForBwuWifiDirectPathAvailable( + const std::string& ssid, const std::string& password, std::int32_t port, + std::int32_t frequency, bool supports_disabling_encryption, + const std::string& gateway, const std::string& service_name, + const std::string& pin) { OfflineFrame frame; frame.set_version(OfflineFrame::V1); @@ -735,6 +739,44 @@ std::vector ConnectionRequestMediumsToMediums( return result; } +MediumMetadata::WifiDirectAuthType WFDAuthTypeToMediumMetadataWFDAuthType( + WifiDirectAuthType wifi_direct_auth_type) { + switch (wifi_direct_auth_type) { + case WifiDirectAuthType::WIFI_DIRECT_WITH_PASSWORD: + return MediumMetadata::WIFI_DIRECT_WITH_PASSWORD; + case WifiDirectAuthType::WIFI_DIRECT_WITH_PIN: + return MediumMetadata::WIFI_DIRECT_WITH_PIN; + default: + return MediumMetadata::WIFI_DIRECT_TYPE_UNKNOWN; + } +} + +WifiDirectAuthType MediumMetadataWFDAuthTypeToWFDAuthType( + MediumMetadata::WifiDirectAuthType wifi_direct_auth_type) { + switch (wifi_direct_auth_type) { + case MediumMetadata::WIFI_DIRECT_WITH_PASSWORD: + return WifiDirectAuthType::WIFI_DIRECT_WITH_PASSWORD; + case MediumMetadata::WIFI_DIRECT_WITH_PIN: + return WifiDirectAuthType::WIFI_DIRECT_WITH_PIN; + default: + return WifiDirectAuthType::WIFI_DIRECT_TYPE_UNKNOWN; + } +} + +std::vector MediumMetadataWFDAuthTypesToWFDAuthTypes( + const MediumMetadata& medium_metadata) { + std::vector result; + for (const auto& int_wifi_direct_auth_type : + medium_metadata.supported_wifi_direct_auth_types()) { + // The int_wifi_direct_auth_type is guaranteed to be a valid + // MediumMetadata::WifiDirectAuthType by the proto spec. + result.push_back(MediumMetadataWFDAuthTypeToWFDAuthType( + static_cast( + int_wifi_direct_auth_type))); + } + return result; +} + } // namespace parser } // namespace connections } // namespace nearby diff --git a/connections/implementation/offline_frames.h b/connections/implementation/offline_frames.h index bbf15dbe..0114d83f 100644 --- a/connections/implementation/offline_frames.h +++ b/connections/implementation/offline_frames.h @@ -32,6 +32,9 @@ namespace parser { using UpgradePathInfo = ::location::nearby::connections:: BandwidthUpgradeNegotiationFrame::UpgradePathInfo; +using MediumMetadata = ::location::nearby::connections::MediumMetadata; +using WifiDirectAuthType = + ::location::nearby::proto::connections::WifiDirectAuthType; // Serialize/Deserialize Nearby Connections Protocol messages. @@ -125,7 +128,12 @@ Medium ConnectionRequestMediumToMedium( std::vector ConnectionRequestMediumsToMediums( const location::nearby::connections::ConnectionRequestFrame& connection_request_frame); - +MediumMetadata::WifiDirectAuthType WFDAuthTypeToMediumMetadataWFDAuthType( + WifiDirectAuthType wifi_direct_auth_type); +WifiDirectAuthType MediumMetadataWFDAuthTypeToWFDAuthType( + MediumMetadata::WifiDirectAuthType wifi_direct_auth_type); +std::vector MediumMetadataWFDAuthTypesToWFDAuthTypes( + const MediumMetadata& medium_metadata); } // namespace parser } // namespace connections } // namespace nearby diff --git a/connections/implementation/offline_frames_test.cc b/connections/implementation/offline_frames_test.cc index 9ec2377e..4f039d41 100644 --- a/connections/implementation/offline_frames_test.cc +++ b/connections/implementation/offline_frames_test.cc @@ -42,8 +42,12 @@ using ::location::nearby::connections::OsInfo; using ::location::nearby::connections::PayloadTransferFrame; using ::location::nearby::connections::V1Frame; using Medium = ::location::nearby::proto::connections::Medium; +using WifiDirectAuthType = + ::location::nearby::proto::connections::WifiDirectAuthType; +using MediumMetadata = ::location::nearby::connections::MediumMetadata; using ::location::nearby::connections::MediumRole; using ::protobuf_matchers::EqualsProto; +using ::testing::Pointwise; constexpr absl::string_view kEndpointId{"ABC"}; constexpr absl::string_view kEndpointName{"XYZ"}; @@ -280,6 +284,76 @@ TEST(OfflineFramesTest, CanGeneratePresenceConnectionRequest) { EXPECT_THAT(message, EqualsProto(kExpected)); } +TEST(OfflineFramesTest, + ForConnectionRequestConnectionsPopulatesWifiDirectAuthTypes) { + constexpr absl::string_view kExpected = + R"pb( + version: V1 + v1: < + type: CONNECTION_REQUEST + connection_request: < + endpoint_id: "ABC" + endpoint_name: "XYZ" + endpoint_info: "XYZ" + nonce: 1234 + medium_metadata: < + supports_5_ghz: true + bssid: "FF:FF:FF:FF:FF:FF" + ip_address: "8xqT" + ap_frequency: 2412 + supported_wifi_direct_auth_types: WIFI_DIRECT_WITH_PIN + supported_wifi_direct_auth_types: WIFI_DIRECT_WITH_PASSWORD + > + mediums: MDNS + mediums: BLUETOOTH + mediums: WIFI_HOTSPOT + mediums: BLE + mediums: WIFI_LAN + mediums: WIFI_AWARE + mediums: NFC + mediums: WIFI_DIRECT + mediums: WEB_RTC + mediums: USB + mediums: AWDL + keep_alive_interval_millis: 1000 + keep_alive_timeout_millis: 5000 + connections_device { + endpoint_id: "ABC" + endpoint_type: CONNECTIONS_ENDPOINT + endpoint_info: "XYZ" + } + > + >)pb"; + + ConnectionInfo connection_info{std::string(kEndpointId), + ByteArray{std::string(kEndpointName)}, + kNonce, + kSupports5ghz, + std::string(kBssid), + kApFrequency, + std::string(kIp4Bytes), + std::vector>( + kMediums.begin(), kMediums.end()), + kKeepAliveIntervalMillis, + kKeepAliveTimeoutMillis}; + connection_info.supported_wifi_direct_auth_types = { + WifiDirectAuthType::WIFI_DIRECT_WITH_PIN, + WifiDirectAuthType::WIFI_DIRECT_WITH_PASSWORD}; + + location::nearby::connections::ConnectionsDevice connections_device; + connections_device.set_endpoint_id("ABC"); + connections_device.set_endpoint_type( + location::nearby::connections::CONNECTIONS_ENDPOINT); + connections_device.set_endpoint_info("XYZ"); + + ByteArray bytes = + ForConnectionRequestConnections(connections_device, connection_info); + auto response = FromBytes(bytes); + ASSERT_TRUE(response.ok()); + OfflineFrame message = response.result(); + EXPECT_THAT(message, EqualsProto(kExpected)); +} + TEST(OfflineFramesTest, CanGenerateConnectionResponse) { constexpr absl::string_view kExpected = R"pb( @@ -730,6 +804,49 @@ TEST(OfflineFramesTest, CanGenerateBwuPathRequest) { EXPECT_THAT(message, EqualsProto(kExpected)); } +TEST(OfflineFramesTest, WFDAuthTypeToMediumMetadataWFDAuthType) { + EXPECT_EQ(WFDAuthTypeToMediumMetadataWFDAuthType( + WifiDirectAuthType::WIFI_DIRECT_WITH_PASSWORD), + MediumMetadata::WIFI_DIRECT_WITH_PASSWORD); + EXPECT_EQ(WFDAuthTypeToMediumMetadataWFDAuthType( + WifiDirectAuthType::WIFI_DIRECT_WITH_PIN), + MediumMetadata::WIFI_DIRECT_WITH_PIN); + EXPECT_EQ(WFDAuthTypeToMediumMetadataWFDAuthType( + WifiDirectAuthType::WIFI_DIRECT_TYPE_UNKNOWN), + MediumMetadata::WIFI_DIRECT_TYPE_UNKNOWN); +} + +TEST(OfflineFramesTest, MediumMetadataWFDAuthTypeToWFDAuthType) { + EXPECT_EQ(MediumMetadataWFDAuthTypeToWFDAuthType( + MediumMetadata::WIFI_DIRECT_WITH_PASSWORD), + WifiDirectAuthType::WIFI_DIRECT_WITH_PASSWORD); + EXPECT_EQ(MediumMetadataWFDAuthTypeToWFDAuthType( + MediumMetadata::WIFI_DIRECT_WITH_PIN), + WifiDirectAuthType::WIFI_DIRECT_WITH_PIN); + EXPECT_EQ(MediumMetadataWFDAuthTypeToWFDAuthType( + MediumMetadata::WIFI_DIRECT_TYPE_UNKNOWN), + WifiDirectAuthType::WIFI_DIRECT_TYPE_UNKNOWN); +} + +TEST(OfflineFramesTest, MediumMetadataWFDAuthTypesToWFDAuthTypes) { + MediumMetadata medium_metadata; + medium_metadata.add_supported_wifi_direct_auth_types( + MediumMetadata::WIFI_DIRECT_WITH_PASSWORD); + medium_metadata.add_supported_wifi_direct_auth_types( + MediumMetadata::WIFI_DIRECT_WITH_PIN); + + std::vector expected = { + WifiDirectAuthType::WIFI_DIRECT_WITH_PASSWORD, + WifiDirectAuthType::WIFI_DIRECT_WITH_PIN}; + + EXPECT_THAT(MediumMetadataWFDAuthTypesToWFDAuthTypes(medium_metadata), + Pointwise(testing::Eq(), expected)); + + MediumMetadata empty_medium_metadata; + EXPECT_TRUE( + MediumMetadataWFDAuthTypesToWFDAuthTypes(empty_medium_metadata).empty()); +} + } // namespace } // namespace parser } // namespace connections diff --git a/internal/platform/implementation/g3/wifi_direct.h b/internal/platform/implementation/g3/wifi_direct.h index f53d1b7e..fe9ec31d 100644 --- a/internal/platform/implementation/g3/wifi_direct.h +++ b/internal/platform/implementation/g3/wifi_direct.h @@ -182,6 +182,12 @@ class WifiDirectMedium : public api::WifiDirectMedium { return std::nullopt; } + // Returns the supported WifiDirect auth types. + std::vector GetSupportedWifiDirectAuthTypes() + const override { + return {WifiDirectAuthType::WIFI_DIRECT_WITH_PIN}; + } + private: absl::Mutex mutex_; diff --git a/internal/platform/implementation/wifi_direct.h b/internal/platform/implementation/wifi_direct.h index 0d267b29..adfcf55f 100644 --- a/internal/platform/implementation/wifi_direct.h +++ b/internal/platform/implementation/wifi_direct.h @@ -19,6 +19,7 @@ #include #include #include +#include #include "absl/strings/string_view.h" #include "absl/types/optional.h" @@ -81,6 +82,9 @@ class WifiDirectServerSocket { // Container of operations that can be performed over the WifiLan medium. class WifiDirectMedium { public: + using WifiDirectAuthType = + ::location::nearby::proto::connections::WifiDirectAuthType; + virtual ~WifiDirectMedium() = default; // If the WiFi Adaptor supports to start a WifiDirect interface. @@ -119,6 +123,10 @@ class WifiDirectMedium { // Returns the port range as a pair of min and max port. virtual absl::optional> GetDynamicPortRange() = 0; + + // Returns the supported WifiDirect auth types. + virtual std::vector GetSupportedWifiDirectAuthTypes() + const = 0; }; } // namespace api diff --git a/internal/platform/implementation/windows/wifi_direct.h b/internal/platform/implementation/windows/wifi_direct.h index 154c13e1..d2e71daf 100644 --- a/internal/platform/implementation/windows/wifi_direct.h +++ b/internal/platform/implementation/windows/wifi_direct.h @@ -25,6 +25,7 @@ #include #include #include +#include // Nearby connections headers #include "absl/base/nullability.h" @@ -210,6 +211,10 @@ class WifiDirectMedium : public api::WifiDirectMedium { return absl::nullopt; } + // Returns the supported WifiDirect auth types. + std::vector GetSupportedWifiDirectAuthTypes() + const override; + private: enum Value : char { kMediumStatusIdle = 0, diff --git a/internal/platform/implementation/windows/wifi_direct_medium.cc b/internal/platform/implementation/windows/wifi_direct_medium.cc index c6ed7775..c09c81f0 100644 --- a/internal/platform/implementation/windows/wifi_direct_medium.cc +++ b/internal/platform/implementation/windows/wifi_direct_medium.cc @@ -18,6 +18,7 @@ #include #include #include +#include #include "absl/strings/str_format.h" #include "absl/strings/string_view.h" @@ -737,5 +738,11 @@ bool WifiDirectMedium::DisconnectWifiDirect() { return false; } +std::vector +WifiDirectMedium::GetSupportedWifiDirectAuthTypes() const { + // Windows only supports WifiDirect with Service Discovery, which uses a PIN. + return {WifiDirectAuthType::WIFI_DIRECT_WITH_PIN}; +} + } // namespace windows } // namespace nearby diff --git a/internal/platform/wifi_direct.h b/internal/platform/wifi_direct.h index 33b6900f..136e4725 100644 --- a/internal/platform/wifi_direct.h +++ b/internal/platform/wifi_direct.h @@ -19,6 +19,7 @@ #include #include #include +#include #include "absl/base/thread_annotations.h" #include "absl/strings/string_view.h" @@ -152,6 +153,8 @@ class WifiDirectServerSocket final { class WifiDirectMedium { public: using Platform = api::ImplementationPlatform; + using WifiDirectAuthType = + ::location::nearby::proto::connections::WifiDirectAuthType; WifiDirectMedium() : impl_(Platform::CreateWifiDirectMedium()) {} ~WifiDirectMedium() = default; @@ -198,6 +201,17 @@ class WifiDirectMedium { return *impl_; } + // Returns the supported WifiDirect auth types. + std::vector GetSupportedWifiDirectAuthTypes() + const { + // NOTE: This assumes that supported_wifi_direct_auth_types_ is populated + // during the constructor or at some other initialization phase. + if (impl_ == nullptr) { + return {}; + } + return impl_->GetSupportedWifiDirectAuthTypes(); + } + private: Mutex mutex_; std::unique_ptr impl_; diff --git a/internal/platform/wifi_direct_test.cc b/internal/platform/wifi_direct_test.cc index f8ecb636..2139b0af 100644 --- a/internal/platform/wifi_direct_test.cc +++ b/internal/platform/wifi_direct_test.cc @@ -282,5 +282,15 @@ TEST_F(WifiDirectMediumTest, CanStartDirectGOThatOtherFailConnect) { EXPECT_TRUE(wifi_direct_a.StopWifiDirect()); } +TEST_F(WifiDirectMediumTest, GetSupportedWifiDirectAuthTypes) { + WifiDirectMedium wifi_direct_a; + // g3 only supports WifiDirect with auth type of PIN. + auto supported_types = wifi_direct_a.GetSupportedWifiDirectAuthTypes(); + EXPECT_EQ(supported_types.size(), 1); + EXPECT_EQ(supported_types[0], + location::nearby::proto::connections:: + WifiDirectAuthType::WIFI_DIRECT_WITH_PIN); +} + } // namespace } // namespace nearby