From 1210969d04e64fc9efc1b60a21ce5f3f2044a5ce Mon Sep 17 00:00:00 2001 From: Francis Tsui Date: Sun, 26 Oct 2025 15:15:07 -0700 Subject: [PATCH] Cleanup use of HotspotCredentials. PiperOrigin-RevId: 824262890 --- .../apple/Tests/GNCWifiHotspotMediumTest.mm | 2 +- .../platform/implementation/apple/wifi_hotspot.h | 2 +- .../platform/implementation/apple/wifi_hotspot.mm | 6 +++--- .../platform/implementation/g3/wifi_hotspot.cc | 13 ++++++------- .../platform/implementation/g3/wifi_hotspot.h | 3 ++- internal/platform/implementation/wifi_hotspot.h | 4 ++-- .../implementation/windows/wifi_hotspot.h | 3 ++- .../implementation/windows/wifi_hotspot_medium.cc | 15 ++++++++------- .../implementation/windows/wifi_hotspot_native.cc | 13 +++++-------- .../implementation/windows/wifi_hotspot_native.h | 9 +++------ .../implementation/windows/wifi_hotspot_test.cc | 2 +- internal/platform/medium_environment.cc | 4 ++-- internal/platform/medium_environment.h | 6 +++--- internal/platform/wifi_hotspot.h | 3 +-- 14 files changed, 40 insertions(+), 45 deletions(-) diff --git a/internal/platform/implementation/apple/Tests/GNCWifiHotspotMediumTest.mm b/internal/platform/implementation/apple/Tests/GNCWifiHotspotMediumTest.mm index 5f326594..40cd82eb 100644 --- a/internal/platform/implementation/apple/Tests/GNCWifiHotspotMediumTest.mm +++ b/internal/platform/implementation/apple/Tests/GNCWifiHotspotMediumTest.mm @@ -72,7 +72,7 @@ const char kIPAddress[] = "192.168.1.2"; hotspotCredentials.SetSSID("TestSSID"); hotspotCredentials.SetPassword("TestPassword"); - XCTAssertFalse(_hotspotMedium->ConnectWifiHotspot(&hotspotCredentials)); + XCTAssertFalse(_hotspotMedium->ConnectWifiHotspot(hotspotCredentials)); } - (void)testListenForService { diff --git a/internal/platform/implementation/apple/wifi_hotspot.h b/internal/platform/implementation/apple/wifi_hotspot.h index 9e1c0bd0..5678b377 100644 --- a/internal/platform/implementation/apple/wifi_hotspot.h +++ b/internal/platform/implementation/apple/wifi_hotspot.h @@ -103,7 +103,7 @@ class WifiHotspotMedium : public api::WifiHotspotMedium { * * @param hotspot_credentials_ The credentials of the Wifi Hotspot to connect to. */ - bool ConnectWifiHotspot(HotspotCredentials* hotspot_credentials_) override; + bool ConnectWifiHotspot(const HotspotCredentials& hotspot_credentials_) override; /** * @brief Disconnects from the currently connected Wifi Hotspot. diff --git a/internal/platform/implementation/apple/wifi_hotspot.mm b/internal/platform/implementation/apple/wifi_hotspot.mm index 86ec5e43..b4611aaa 100644 --- a/internal/platform/implementation/apple/wifi_hotspot.mm +++ b/internal/platform/implementation/apple/wifi_hotspot.mm @@ -110,9 +110,9 @@ WifiHotspotMedium::WifiHotspotMedium(GNCHotspotMedium* hotspot_medium) : medium_ WifiHotspotMedium::~WifiHotspotMedium() { DisconnectWifiHotspot(); } -bool WifiHotspotMedium::ConnectWifiHotspot(HotspotCredentials* hotspot_credentials_) { - NSString* ssid = [[NSString alloc] initWithUTF8String:hotspot_credentials_->GetSSID().c_str()]; - std::string hotspot_password = hotspot_credentials_->GetPassword(); +bool WifiHotspotMedium::ConnectWifiHotspot(const HotspotCredentials& hotspot_credentials_) { + NSString* ssid = [[NSString alloc] initWithUTF8String:hotspot_credentials_.GetSSID().c_str()]; + std::string hotspot_password = hotspot_credentials_.GetPassword(); NSString* password = [[NSString alloc] initWithUTF8String:hotspot_password.c_str()]; GNCLoggerInfo(@"ConnectWifiHotspot SSID: %@ Password: %s", ssid, diff --git a/internal/platform/implementation/g3/wifi_hotspot.cc b/internal/platform/implementation/g3/wifi_hotspot.cc index e8793501..fb03770d 100644 --- a/internal/platform/implementation/g3/wifi_hotspot.cc +++ b/internal/platform/implementation/g3/wifi_hotspot.cc @@ -14,7 +14,6 @@ #include "internal/platform/implementation/g3/wifi_hotspot.h" -#include #include #include #include @@ -158,23 +157,23 @@ bool WifiHotspotMedium::StopWifiHotspot() { } bool WifiHotspotMedium::ConnectWifiHotspot( - HotspotCredentials* hotspot_credentials) { + const HotspotCredentials& hotspot_credentials) { absl::MutexLock lock(mutex_); - LOG(INFO) << "G3 ConnectWifiHotspot: ssid=" << hotspot_credentials->GetSSID() - << ", password:" << hotspot_credentials->GetPassword(); + LOG(INFO) << "G3 ConnectWifiHotspot: ssid=" << hotspot_credentials.GetSSID() + << ", password:" << hotspot_credentials.GetPassword(); auto& env = MediumEnvironment::Instance(); auto* remote_medium = static_cast( - env.GetWifiHotspotMedium(hotspot_credentials->GetSSID(), {})); + env.GetWifiHotspotMedium(hotspot_credentials.GetSSID(), "")); if (!remote_medium) { - env.UpdateWifiHotspotMediumForStartOrConnect(*this, hotspot_credentials, + env.UpdateWifiHotspotMediumForStartOrConnect(*this, &hotspot_credentials, /*is_ap=*/false, /*enabled=*/false); return false; } - env.UpdateWifiHotspotMediumForStartOrConnect(*this, hotspot_credentials, + env.UpdateWifiHotspotMediumForStartOrConnect(*this, &hotspot_credentials, /*is_ap=*/false, /*enabled=*/true); return true; diff --git a/internal/platform/implementation/g3/wifi_hotspot.h b/internal/platform/implementation/g3/wifi_hotspot.h index a3b3e69f..1b69d1a6 100644 --- a/internal/platform/implementation/g3/wifi_hotspot.h +++ b/internal/platform/implementation/g3/wifi_hotspot.h @@ -158,7 +158,8 @@ class WifiHotspotMedium : public api::WifiHotspotMedium { // Advertiser stop the current WiFi Hotspot bool StopWifiHotspot() override; // Discoverer connects to the Hotspot - bool ConnectWifiHotspot(HotspotCredentials* hotspot_credentials) override; + bool ConnectWifiHotspot( + const HotspotCredentials& hotspot_credentials) override; // Discoverer disconnects from the Hotspot bool DisconnectWifiHotspot() override; diff --git a/internal/platform/implementation/wifi_hotspot.h b/internal/platform/implementation/wifi_hotspot.h index 44f99b38..83ff5255 100644 --- a/internal/platform/implementation/wifi_hotspot.h +++ b/internal/platform/implementation/wifi_hotspot.h @@ -19,7 +19,6 @@ #include "internal/platform/cancellation_flag.h" #include "internal/platform/input_stream.h" -#include "internal/platform/listeners.h" #include "internal/platform/output_stream.h" #include "internal/platform/wifi_credential.h" @@ -101,7 +100,8 @@ class WifiHotspotMedium { virtual bool StopWifiHotspot() = 0; // Client device connect to a softAP with specified credential. - virtual bool ConnectWifiHotspot(HotspotCredentials* hotspot_credentials) = 0; + virtual bool ConnectWifiHotspot( + const HotspotCredentials& hotspot_credentials) = 0; virtual bool DisconnectWifiHotspot() = 0; // Returns the port range as a pair of min and max port. diff --git a/internal/platform/implementation/windows/wifi_hotspot.h b/internal/platform/implementation/windows/wifi_hotspot.h index 67fe6c2f..7fedc3d8 100644 --- a/internal/platform/implementation/windows/wifi_hotspot.h +++ b/internal/platform/implementation/windows/wifi_hotspot.h @@ -217,7 +217,8 @@ class WifiHotspotMedium : public api::WifiHotspotMedium { // Advertiser stop the current WiFi Hotspot bool StopWifiHotspot() override; // Discoverer connects to the Hotspot - bool ConnectWifiHotspot(HotspotCredentials* hotspot_credentials) override; + bool ConnectWifiHotspot( + const HotspotCredentials& hotspot_credentials) override; // Discoverer disconnects from the Hotspot bool DisconnectWifiHotspot() override; diff --git a/internal/platform/implementation/windows/wifi_hotspot_medium.cc b/internal/platform/implementation/windows/wifi_hotspot_medium.cc index b1c061de..422d3e57 100644 --- a/internal/platform/implementation/windows/wifi_hotspot_medium.cc +++ b/internal/platform/implementation/windows/wifi_hotspot_medium.cc @@ -383,9 +383,11 @@ fire_and_forget WifiHotspotMedium::OnConnectionRequested( } bool WifiHotspotMedium::ConnectWifiHotspot( - HotspotCredentials* hotspot_credentials) { + const HotspotCredentials& hotspot_credentials) { absl::MutexLock lock(mutex_); + std::string ssid = hotspot_credentials.GetSSID(); + std::string password = hotspot_credentials.GetPassword(); try { if (IsConnected()) { LOG(WARNING) << "Already connected to Hotspot, disconnect first."; @@ -398,7 +400,7 @@ bool WifiHotspotMedium::ConnectWifiHotspot( platform::config_package_nearby::nearby_platform_feature:: kEnableIntelPieSdk)) { auto channel = WifiUtils::ConvertFrequencyMhzToChannel( - hotspot_credentials->GetFrequency()); + hotspot_credentials.GetFrequency()); WifiIntel& intel_wifi{WifiIntel::GetInstance()}; intel_wifi_started = intel_wifi.Start(); if (intel_wifi_started) { @@ -409,9 +411,8 @@ bool WifiHotspotMedium::ConnectWifiHotspot( if (NearbyFlags::GetInstance().GetBoolFlag( platform::config_package_nearby::nearby_platform_feature:: kEnableWifiHotspotNativeScan)) { - if (!wifi_hotspot_native_.Scan(hotspot_credentials->GetSSID())) { - LOG(INFO) << "Hotspot " << hotspot_credentials->GetSSID() - << " is not found"; + if (!wifi_hotspot_native_.Scan(ssid)) { + LOG(INFO) << "Hotspot " << ssid << " is not found"; if (intel_wifi_started) { WifiIntel& intel_wifi{WifiIntel::GetInstance()}; @@ -423,7 +424,7 @@ bool WifiHotspotMedium::ConnectWifiHotspot( } bool connected = - wifi_hotspot_native_.ConnectToWifiNetwork(hotspot_credentials); + wifi_hotspot_native_.ConnectToWifiNetwork(ssid, password); if (intel_wifi_started) { WifiIntel& intel_wifi{WifiIntel::GetInstance()}; @@ -486,7 +487,7 @@ bool WifiHotspotMedium::ConnectWifiHotspot( return false; } medium_status_ |= kMediumStatusConnected; - LOG(INFO) << "Connected to hotspot: " << hotspot_credentials->GetSSID(); + LOG(INFO) << "Connected to hotspot: " << ssid; return true; } catch (std::exception exception) { diff --git a/internal/platform/implementation/windows/wifi_hotspot_native.cc b/internal/platform/implementation/windows/wifi_hotspot_native.cc index 52c8c103..ca039b8f 100644 --- a/internal/platform/implementation/windows/wifi_hotspot_native.cc +++ b/internal/platform/implementation/windows/wifi_hotspot_native.cc @@ -25,7 +25,6 @@ #include #include #include -#include #include #include @@ -39,7 +38,6 @@ #include "internal/platform/implementation/windows/network_info.h" #include "internal/platform/implementation/windows/string_utils.h" #include "internal/platform/logging.h" -#include "internal/platform/wifi_credential.h" namespace nearby::windows { @@ -113,8 +111,8 @@ WifiHotspotNative::~WifiHotspotNative() { VLOG(1) << "WifiHotspotNative destroyed successfully."; } -bool WifiHotspotNative::ConnectToWifiNetwork( - HotspotCredentials* hotspot_credentials) { +bool WifiHotspotNative::ConnectToWifiNetwork(absl::string_view ssid, + absl::string_view password) { GUID interface_guid = GetInterfaceGuid(); if (interface_guid == GUID_NULL) { LOG(ERROR) << __func__ << ": No available WLAN Interface to use."; @@ -122,7 +120,7 @@ bool WifiHotspotNative::ConnectToWifiNetwork( } { absl::MutexLock lock(mutex_); - if (!SetWlanProfile(interface_guid, hotspot_credentials)) { + if (!SetWlanProfile(interface_guid, ssid, password)) { LOG(ERROR) << "Failed to set WLAN profile."; return false; } @@ -428,9 +426,8 @@ bool WifiHotspotNative::UnregisterWlanNotificationCallback() { } bool WifiHotspotNative::SetWlanProfile( - GUID interface_guid, HotspotCredentials* hotspot_credentials) { - std::wstring profile = BuildWlanProfile(hotspot_credentials->GetSSID(), - hotspot_credentials->GetPassword()); + GUID interface_guid, absl::string_view ssid, absl::string_view password) { + std::wstring profile = BuildWlanProfile(ssid, password); DWORD reason = 0; DWORD result = WlanSetProfile( /*hClientHandle=*/wifi_, /*pInterfaceGuid=*/&interface_guid, diff --git a/internal/platform/implementation/windows/wifi_hotspot_native.h b/internal/platform/implementation/windows/wifi_hotspot_native.h index fe0d8cad..fbf43048 100644 --- a/internal/platform/implementation/windows/wifi_hotspot_native.h +++ b/internal/platform/implementation/windows/wifi_hotspot_native.h @@ -23,9 +23,7 @@ // clang-format on #include -#include #include -#include #include "absl/base/nullability.h" #include "absl/base/thread_annotations.h" @@ -33,7 +31,6 @@ #include "absl/synchronization/mutex.h" #include "internal/platform/count_down_latch.h" #include "internal/platform/implementation/windows/network_info.h" -#include "internal/platform/wifi_credential.h" namespace nearby::windows { @@ -41,7 +38,7 @@ class WifiHotspotNative { public: WifiHotspotNative(); ~WifiHotspotNative(); - bool ConnectToWifiNetwork(HotspotCredentials* hotspot_credentials) + bool ConnectToWifiNetwork(absl::string_view ssid, absl::string_view password) ABSL_LOCKS_EXCLUDED(mutex_); bool DisconnectWifiNetwork() ABSL_LOCKS_EXCLUDED(mutex_); @@ -80,8 +77,8 @@ class WifiHotspotNative { WlanNotificationContext* absl_nonnull context) ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_); bool UnregisterWlanNotificationCallback(); - bool SetWlanProfile(GUID interface_guid, - HotspotCredentials* hotspot_credentials) + bool SetWlanProfile(GUID interface_guid, absl::string_view ssid, + absl::string_view password) ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_); bool RemoveCreatedWlanProfile(GUID interface_guid) ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_); diff --git a/internal/platform/implementation/windows/wifi_hotspot_test.cc b/internal/platform/implementation/windows/wifi_hotspot_test.cc index 73a20cee..1446f655 100644 --- a/internal/platform/implementation/windows/wifi_hotspot_test.cc +++ b/internal/platform/implementation/windows/wifi_hotspot_test.cc @@ -121,7 +121,7 @@ TEST(WifiHotspotMedium, DISABLED_ConnectWifiHotspot) { kEnableIntelPieSdk, true); - EXPECT_TRUE(hotspot_medium.ConnectWifiHotspot(&hotspot_credentials)); + EXPECT_TRUE(hotspot_medium.ConnectWifiHotspot(hotspot_credentials)); absl::SleepFor(absl::Seconds(1)); while (true) { LOG(INFO) << "Enter \"s\" to stop test:"; diff --git a/internal/platform/medium_environment.cc b/internal/platform/medium_environment.cc index 98ad833a..0137efef 100644 --- a/internal/platform/medium_environment.cc +++ b/internal/platform/medium_environment.cc @@ -1056,8 +1056,8 @@ api::WifiHotspotMedium* MediumEnvironment::GetWifiHotspotMedium( } void MediumEnvironment::UpdateWifiHotspotMediumForStartOrConnect( - api::WifiHotspotMedium& medium, HotspotCredentials* hotspot_credentials, - bool is_ap, bool enabled) { + api::WifiHotspotMedium& medium, + const HotspotCredentials* hotspot_credentials, bool is_ap, bool enabled) { if (!enabled_) return; CountDownLatch latch(1); diff --git a/internal/platform/medium_environment.h b/internal/platform/medium_environment.h index d32a42be..0833e1f5 100644 --- a/internal/platform/medium_environment.h +++ b/internal/platform/medium_environment.h @@ -325,8 +325,8 @@ class MediumEnvironment { // Updates credential and Medium role(AP or STA) to indicate the current // medium is exposing Start Hotspot event. void UpdateWifiHotspotMediumForStartOrConnect( - api::WifiHotspotMedium& medium, HotspotCredentials* hotspot_credentials, - bool is_ap, bool enabled); + api::WifiHotspotMedium& medium, + const HotspotCredentials* hotspot_credentials, bool is_ap, bool enabled); // Removes medium-related info. This should correspond to device stopped or // disconnected. @@ -435,7 +435,7 @@ class MediumEnvironment { bool is_ap = true; // Set "true" when SoftAP is started or STA is connected bool is_active = false; - HotspotCredentials* hotspot_credentials; + const HotspotCredentials* hotspot_credentials; }; struct BluetoothPairingContext { diff --git a/internal/platform/wifi_hotspot.h b/internal/platform/wifi_hotspot.h index 7e2ef020..c0b0ed44 100644 --- a/internal/platform/wifi_hotspot.h +++ b/internal/platform/wifi_hotspot.h @@ -23,7 +23,6 @@ #include "absl/base/thread_annotations.h" #include "absl/strings/string_view.h" -#include "absl/types/optional.h" #include "internal/platform/cancellation_flag.h" #include "internal/platform/exception.h" #include "internal/platform/implementation/platform.h" @@ -187,7 +186,7 @@ class WifiHotspotMedium { bool ConnectWifiHotspot(const HotspotCredentials& hotspot_credentials) { MutexLock lock(&mutex_); hotspot_credentials_ = hotspot_credentials; - return impl_->ConnectWifiHotspot(&hotspot_credentials_); + return impl_->ConnectWifiHotspot(hotspot_credentials); } bool DisconnectWifiHotspot() { return impl_->DisconnectWifiHotspot(); }