Cleanup use of HotspotCredentials.

PiperOrigin-RevId: 824262890
This commit is contained in:
Francis Tsui
2025-10-26 15:17:16 -07:00
committed by Copybara-Service
parent be3e658d75
commit 1210969d04
14 changed files with 40 additions and 45 deletions
@@ -72,7 +72,7 @@ const char kIPAddress[] = "192.168.1.2";
hotspotCredentials.SetSSID("TestSSID"); hotspotCredentials.SetSSID("TestSSID");
hotspotCredentials.SetPassword("TestPassword"); hotspotCredentials.SetPassword("TestPassword");
XCTAssertFalse(_hotspotMedium->ConnectWifiHotspot(&hotspotCredentials)); XCTAssertFalse(_hotspotMedium->ConnectWifiHotspot(hotspotCredentials));
} }
- (void)testListenForService { - (void)testListenForService {
@@ -103,7 +103,7 @@ class WifiHotspotMedium : public api::WifiHotspotMedium {
* *
* @param hotspot_credentials_ The credentials of the Wifi Hotspot to connect to. * @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. * @brief Disconnects from the currently connected Wifi Hotspot.
@@ -110,9 +110,9 @@ WifiHotspotMedium::WifiHotspotMedium(GNCHotspotMedium* hotspot_medium) : medium_
WifiHotspotMedium::~WifiHotspotMedium() { DisconnectWifiHotspot(); } WifiHotspotMedium::~WifiHotspotMedium() { DisconnectWifiHotspot(); }
bool WifiHotspotMedium::ConnectWifiHotspot(HotspotCredentials* hotspot_credentials_) { bool WifiHotspotMedium::ConnectWifiHotspot(const HotspotCredentials& hotspot_credentials_) {
NSString* ssid = [[NSString alloc] initWithUTF8String:hotspot_credentials_->GetSSID().c_str()]; NSString* ssid = [[NSString alloc] initWithUTF8String:hotspot_credentials_.GetSSID().c_str()];
std::string hotspot_password = hotspot_credentials_->GetPassword(); std::string hotspot_password = hotspot_credentials_.GetPassword();
NSString* password = [[NSString alloc] initWithUTF8String:hotspot_password.c_str()]; NSString* password = [[NSString alloc] initWithUTF8String:hotspot_password.c_str()];
GNCLoggerInfo(@"ConnectWifiHotspot SSID: %@ Password: %s", ssid, GNCLoggerInfo(@"ConnectWifiHotspot SSID: %@ Password: %s", ssid,
@@ -14,7 +14,6 @@
#include "internal/platform/implementation/g3/wifi_hotspot.h" #include "internal/platform/implementation/g3/wifi_hotspot.h"
#include <iostream>
#include <memory> #include <memory>
#include <string> #include <string>
#include <utility> #include <utility>
@@ -158,23 +157,23 @@ bool WifiHotspotMedium::StopWifiHotspot() {
} }
bool WifiHotspotMedium::ConnectWifiHotspot( bool WifiHotspotMedium::ConnectWifiHotspot(
HotspotCredentials* hotspot_credentials) { const HotspotCredentials& hotspot_credentials) {
absl::MutexLock lock(mutex_); absl::MutexLock lock(mutex_);
LOG(INFO) << "G3 ConnectWifiHotspot: ssid=" << hotspot_credentials->GetSSID() LOG(INFO) << "G3 ConnectWifiHotspot: ssid=" << hotspot_credentials.GetSSID()
<< ", password:" << hotspot_credentials->GetPassword(); << ", password:" << hotspot_credentials.GetPassword();
auto& env = MediumEnvironment::Instance(); auto& env = MediumEnvironment::Instance();
auto* remote_medium = static_cast<WifiHotspotMedium*>( auto* remote_medium = static_cast<WifiHotspotMedium*>(
env.GetWifiHotspotMedium(hotspot_credentials->GetSSID(), {})); env.GetWifiHotspotMedium(hotspot_credentials.GetSSID(), ""));
if (!remote_medium) { if (!remote_medium) {
env.UpdateWifiHotspotMediumForStartOrConnect(*this, hotspot_credentials, env.UpdateWifiHotspotMediumForStartOrConnect(*this, &hotspot_credentials,
/*is_ap=*/false, /*is_ap=*/false,
/*enabled=*/false); /*enabled=*/false);
return false; return false;
} }
env.UpdateWifiHotspotMediumForStartOrConnect(*this, hotspot_credentials, env.UpdateWifiHotspotMediumForStartOrConnect(*this, &hotspot_credentials,
/*is_ap=*/false, /*is_ap=*/false,
/*enabled=*/true); /*enabled=*/true);
return true; return true;
@@ -158,7 +158,8 @@ class WifiHotspotMedium : public api::WifiHotspotMedium {
// Advertiser stop the current WiFi Hotspot // Advertiser stop the current WiFi Hotspot
bool StopWifiHotspot() override; bool StopWifiHotspot() override;
// Discoverer connects to the Hotspot // Discoverer connects to the Hotspot
bool ConnectWifiHotspot(HotspotCredentials* hotspot_credentials) override; bool ConnectWifiHotspot(
const HotspotCredentials& hotspot_credentials) override;
// Discoverer disconnects from the Hotspot // Discoverer disconnects from the Hotspot
bool DisconnectWifiHotspot() override; bool DisconnectWifiHotspot() override;
@@ -19,7 +19,6 @@
#include "internal/platform/cancellation_flag.h" #include "internal/platform/cancellation_flag.h"
#include "internal/platform/input_stream.h" #include "internal/platform/input_stream.h"
#include "internal/platform/listeners.h"
#include "internal/platform/output_stream.h" #include "internal/platform/output_stream.h"
#include "internal/platform/wifi_credential.h" #include "internal/platform/wifi_credential.h"
@@ -101,7 +100,8 @@ class WifiHotspotMedium {
virtual bool StopWifiHotspot() = 0; virtual bool StopWifiHotspot() = 0;
// Client device connect to a softAP with specified credential. // 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; virtual bool DisconnectWifiHotspot() = 0;
// Returns the port range as a pair of min and max port. // Returns the port range as a pair of min and max port.
@@ -217,7 +217,8 @@ class WifiHotspotMedium : public api::WifiHotspotMedium {
// Advertiser stop the current WiFi Hotspot // Advertiser stop the current WiFi Hotspot
bool StopWifiHotspot() override; bool StopWifiHotspot() override;
// Discoverer connects to the Hotspot // Discoverer connects to the Hotspot
bool ConnectWifiHotspot(HotspotCredentials* hotspot_credentials) override; bool ConnectWifiHotspot(
const HotspotCredentials& hotspot_credentials) override;
// Discoverer disconnects from the Hotspot // Discoverer disconnects from the Hotspot
bool DisconnectWifiHotspot() override; bool DisconnectWifiHotspot() override;
@@ -383,9 +383,11 @@ fire_and_forget WifiHotspotMedium::OnConnectionRequested(
} }
bool WifiHotspotMedium::ConnectWifiHotspot( bool WifiHotspotMedium::ConnectWifiHotspot(
HotspotCredentials* hotspot_credentials) { const HotspotCredentials& hotspot_credentials) {
absl::MutexLock lock(mutex_); absl::MutexLock lock(mutex_);
std::string ssid = hotspot_credentials.GetSSID();
std::string password = hotspot_credentials.GetPassword();
try { try {
if (IsConnected()) { if (IsConnected()) {
LOG(WARNING) << "Already connected to Hotspot, disconnect first."; LOG(WARNING) << "Already connected to Hotspot, disconnect first.";
@@ -398,7 +400,7 @@ bool WifiHotspotMedium::ConnectWifiHotspot(
platform::config_package_nearby::nearby_platform_feature:: platform::config_package_nearby::nearby_platform_feature::
kEnableIntelPieSdk)) { kEnableIntelPieSdk)) {
auto channel = WifiUtils::ConvertFrequencyMhzToChannel( auto channel = WifiUtils::ConvertFrequencyMhzToChannel(
hotspot_credentials->GetFrequency()); hotspot_credentials.GetFrequency());
WifiIntel& intel_wifi{WifiIntel::GetInstance()}; WifiIntel& intel_wifi{WifiIntel::GetInstance()};
intel_wifi_started = intel_wifi.Start(); intel_wifi_started = intel_wifi.Start();
if (intel_wifi_started) { if (intel_wifi_started) {
@@ -409,9 +411,8 @@ bool WifiHotspotMedium::ConnectWifiHotspot(
if (NearbyFlags::GetInstance().GetBoolFlag( if (NearbyFlags::GetInstance().GetBoolFlag(
platform::config_package_nearby::nearby_platform_feature:: platform::config_package_nearby::nearby_platform_feature::
kEnableWifiHotspotNativeScan)) { kEnableWifiHotspotNativeScan)) {
if (!wifi_hotspot_native_.Scan(hotspot_credentials->GetSSID())) { if (!wifi_hotspot_native_.Scan(ssid)) {
LOG(INFO) << "Hotspot " << hotspot_credentials->GetSSID() LOG(INFO) << "Hotspot " << ssid << " is not found";
<< " is not found";
if (intel_wifi_started) { if (intel_wifi_started) {
WifiIntel& intel_wifi{WifiIntel::GetInstance()}; WifiIntel& intel_wifi{WifiIntel::GetInstance()};
@@ -423,7 +424,7 @@ bool WifiHotspotMedium::ConnectWifiHotspot(
} }
bool connected = bool connected =
wifi_hotspot_native_.ConnectToWifiNetwork(hotspot_credentials); wifi_hotspot_native_.ConnectToWifiNetwork(ssid, password);
if (intel_wifi_started) { if (intel_wifi_started) {
WifiIntel& intel_wifi{WifiIntel::GetInstance()}; WifiIntel& intel_wifi{WifiIntel::GetInstance()};
@@ -486,7 +487,7 @@ bool WifiHotspotMedium::ConnectWifiHotspot(
return false; return false;
} }
medium_status_ |= kMediumStatusConnected; medium_status_ |= kMediumStatusConnected;
LOG(INFO) << "Connected to hotspot: " << hotspot_credentials->GetSSID(); LOG(INFO) << "Connected to hotspot: " << ssid;
return true; return true;
} catch (std::exception exception) { } catch (std::exception exception) {
@@ -25,7 +25,6 @@
#include <cstring> #include <cstring>
#include <cwchar> #include <cwchar>
#include <memory> #include <memory>
#include <optional>
#include <string> #include <string>
#include <utility> #include <utility>
@@ -39,7 +38,6 @@
#include "internal/platform/implementation/windows/network_info.h" #include "internal/platform/implementation/windows/network_info.h"
#include "internal/platform/implementation/windows/string_utils.h" #include "internal/platform/implementation/windows/string_utils.h"
#include "internal/platform/logging.h" #include "internal/platform/logging.h"
#include "internal/platform/wifi_credential.h"
namespace nearby::windows { namespace nearby::windows {
@@ -113,8 +111,8 @@ WifiHotspotNative::~WifiHotspotNative() {
VLOG(1) << "WifiHotspotNative destroyed successfully."; VLOG(1) << "WifiHotspotNative destroyed successfully.";
} }
bool WifiHotspotNative::ConnectToWifiNetwork( bool WifiHotspotNative::ConnectToWifiNetwork(absl::string_view ssid,
HotspotCredentials* hotspot_credentials) { absl::string_view password) {
GUID interface_guid = GetInterfaceGuid(); GUID interface_guid = GetInterfaceGuid();
if (interface_guid == GUID_NULL) { if (interface_guid == GUID_NULL) {
LOG(ERROR) << __func__ << ": No available WLAN Interface to use."; LOG(ERROR) << __func__ << ": No available WLAN Interface to use.";
@@ -122,7 +120,7 @@ bool WifiHotspotNative::ConnectToWifiNetwork(
} }
{ {
absl::MutexLock lock(mutex_); absl::MutexLock lock(mutex_);
if (!SetWlanProfile(interface_guid, hotspot_credentials)) { if (!SetWlanProfile(interface_guid, ssid, password)) {
LOG(ERROR) << "Failed to set WLAN profile."; LOG(ERROR) << "Failed to set WLAN profile.";
return false; return false;
} }
@@ -428,9 +426,8 @@ bool WifiHotspotNative::UnregisterWlanNotificationCallback() {
} }
bool WifiHotspotNative::SetWlanProfile( bool WifiHotspotNative::SetWlanProfile(
GUID interface_guid, HotspotCredentials* hotspot_credentials) { GUID interface_guid, absl::string_view ssid, absl::string_view password) {
std::wstring profile = BuildWlanProfile(hotspot_credentials->GetSSID(), std::wstring profile = BuildWlanProfile(ssid, password);
hotspot_credentials->GetPassword());
DWORD reason = 0; DWORD reason = 0;
DWORD result = WlanSetProfile( DWORD result = WlanSetProfile(
/*hClientHandle=*/wifi_, /*pInterfaceGuid=*/&interface_guid, /*hClientHandle=*/wifi_, /*pInterfaceGuid=*/&interface_guid,
@@ -23,9 +23,7 @@
// clang-format on // clang-format on
#include <memory> #include <memory>
#include <optional>
#include <string> #include <string>
#include <vector>
#include "absl/base/nullability.h" #include "absl/base/nullability.h"
#include "absl/base/thread_annotations.h" #include "absl/base/thread_annotations.h"
@@ -33,7 +31,6 @@
#include "absl/synchronization/mutex.h" #include "absl/synchronization/mutex.h"
#include "internal/platform/count_down_latch.h" #include "internal/platform/count_down_latch.h"
#include "internal/platform/implementation/windows/network_info.h" #include "internal/platform/implementation/windows/network_info.h"
#include "internal/platform/wifi_credential.h"
namespace nearby::windows { namespace nearby::windows {
@@ -41,7 +38,7 @@ class WifiHotspotNative {
public: public:
WifiHotspotNative(); WifiHotspotNative();
~WifiHotspotNative(); ~WifiHotspotNative();
bool ConnectToWifiNetwork(HotspotCredentials* hotspot_credentials) bool ConnectToWifiNetwork(absl::string_view ssid, absl::string_view password)
ABSL_LOCKS_EXCLUDED(mutex_); ABSL_LOCKS_EXCLUDED(mutex_);
bool DisconnectWifiNetwork() ABSL_LOCKS_EXCLUDED(mutex_); bool DisconnectWifiNetwork() ABSL_LOCKS_EXCLUDED(mutex_);
@@ -80,8 +77,8 @@ class WifiHotspotNative {
WlanNotificationContext* absl_nonnull context) WlanNotificationContext* absl_nonnull context)
ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_); ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
bool UnregisterWlanNotificationCallback(); bool UnregisterWlanNotificationCallback();
bool SetWlanProfile(GUID interface_guid, bool SetWlanProfile(GUID interface_guid, absl::string_view ssid,
HotspotCredentials* hotspot_credentials) absl::string_view password)
ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_); ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
bool RemoveCreatedWlanProfile(GUID interface_guid) bool RemoveCreatedWlanProfile(GUID interface_guid)
ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_); ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
@@ -121,7 +121,7 @@ TEST(WifiHotspotMedium, DISABLED_ConnectWifiHotspot) {
kEnableIntelPieSdk, kEnableIntelPieSdk,
true); true);
EXPECT_TRUE(hotspot_medium.ConnectWifiHotspot(&hotspot_credentials)); EXPECT_TRUE(hotspot_medium.ConnectWifiHotspot(hotspot_credentials));
absl::SleepFor(absl::Seconds(1)); absl::SleepFor(absl::Seconds(1));
while (true) { while (true) {
LOG(INFO) << "Enter \"s\" to stop test:"; LOG(INFO) << "Enter \"s\" to stop test:";
+2 -2
View File
@@ -1056,8 +1056,8 @@ api::WifiHotspotMedium* MediumEnvironment::GetWifiHotspotMedium(
} }
void MediumEnvironment::UpdateWifiHotspotMediumForStartOrConnect( void MediumEnvironment::UpdateWifiHotspotMediumForStartOrConnect(
api::WifiHotspotMedium& medium, HotspotCredentials* hotspot_credentials, api::WifiHotspotMedium& medium,
bool is_ap, bool enabled) { const HotspotCredentials* hotspot_credentials, bool is_ap, bool enabled) {
if (!enabled_) return; if (!enabled_) return;
CountDownLatch latch(1); CountDownLatch latch(1);
+3 -3
View File
@@ -325,8 +325,8 @@ class MediumEnvironment {
// Updates credential and Medium role(AP or STA) to indicate the current // Updates credential and Medium role(AP or STA) to indicate the current
// medium is exposing Start Hotspot event. // medium is exposing Start Hotspot event.
void UpdateWifiHotspotMediumForStartOrConnect( void UpdateWifiHotspotMediumForStartOrConnect(
api::WifiHotspotMedium& medium, HotspotCredentials* hotspot_credentials, api::WifiHotspotMedium& medium,
bool is_ap, bool enabled); const HotspotCredentials* hotspot_credentials, bool is_ap, bool enabled);
// Removes medium-related info. This should correspond to device stopped or // Removes medium-related info. This should correspond to device stopped or
// disconnected. // disconnected.
@@ -435,7 +435,7 @@ class MediumEnvironment {
bool is_ap = true; bool is_ap = true;
// Set "true" when SoftAP is started or STA is connected // Set "true" when SoftAP is started or STA is connected
bool is_active = false; bool is_active = false;
HotspotCredentials* hotspot_credentials; const HotspotCredentials* hotspot_credentials;
}; };
struct BluetoothPairingContext { struct BluetoothPairingContext {
+1 -2
View File
@@ -23,7 +23,6 @@
#include "absl/base/thread_annotations.h" #include "absl/base/thread_annotations.h"
#include "absl/strings/string_view.h" #include "absl/strings/string_view.h"
#include "absl/types/optional.h"
#include "internal/platform/cancellation_flag.h" #include "internal/platform/cancellation_flag.h"
#include "internal/platform/exception.h" #include "internal/platform/exception.h"
#include "internal/platform/implementation/platform.h" #include "internal/platform/implementation/platform.h"
@@ -187,7 +186,7 @@ class WifiHotspotMedium {
bool ConnectWifiHotspot(const HotspotCredentials& hotspot_credentials) { bool ConnectWifiHotspot(const HotspotCredentials& hotspot_credentials) {
MutexLock lock(&mutex_); MutexLock lock(&mutex_);
hotspot_credentials_ = hotspot_credentials; hotspot_credentials_ = hotspot_credentials;
return impl_->ConnectWifiHotspot(&hotspot_credentials_); return impl_->ConnectWifiHotspot(hotspot_credentials);
} }
bool DisconnectWifiHotspot() { return impl_->DisconnectWifiHotspot(); } bool DisconnectWifiHotspot() { return impl_->DisconnectWifiHotspot(); }