Skip to content

Commit

Permalink
add flag disable_wifi_hotspot_for_selected_devices and use it to disa…
Browse files Browse the repository at this point in the history
…ble wifi hotspot for HP Realtek devices.

PiperOrigin-RevId: 713396925
  • Loading branch information
suetfei authored and copybara-github committed Jan 15, 2025
1 parent 07db16d commit 0249f74
Show file tree
Hide file tree
Showing 16 changed files with 332 additions and 61 deletions.
7 changes: 6 additions & 1 deletion connections/implementation/offline_service_controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ namespace connections {
class OfflineServiceController : public ServiceController {
public:
OfflineServiceController() = default;
explicit OfflineServiceController(const BwuManager::Config& bwu_config)
: bwu_config_(bwu_config) {};
~OfflineServiceController() override;

Status StartAdvertising(ClientProxy* client, const std::string& service_id,
Expand Down Expand Up @@ -102,6 +104,8 @@ class OfflineServiceController : public ServiceController {

void ShutdownBwuManagerExecutors() override;

BwuManager::Config GetBwuConfig() const { return bwu_config_; }

private:
// Note that the order of declaration of these is crucial, because we depend
// on the destructors running (strictly) in the reverse order; a deviation
Expand All @@ -111,8 +115,9 @@ class OfflineServiceController : public ServiceController {
EndpointChannelManager channel_manager_;
EndpointManager endpoint_manager_{&channel_manager_};
PayloadManager payload_manager_{endpoint_manager_};
BwuManager::Config bwu_config_;
BwuManager bwu_manager_{
mediums_, endpoint_manager_, channel_manager_, {}, {}};
mediums_, endpoint_manager_, channel_manager_, {}, bwu_config_};
InjectedBluetoothDeviceStore injected_bluetooth_device_store_;
PcpManager pcp_manager_{mediums_, channel_manager_, endpoint_manager_,
bwu_manager_, injected_bluetooth_device_store_};
Expand Down
40 changes: 39 additions & 1 deletion connections/implementation/service_controller_router.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,20 @@
#include "connections/implementation/service_controller_router.h"

#include <cstddef>
#include <functional>
#include <memory>
#include <string>
#include <utility>

#include "absl/strings/string_view.h"
#include "connections/advertising_options.h"
#include "connections/discovery_options.h"
#include "connections/implementation/bwu_manager.h"
#include "connections/implementation/client_proxy.h"
#include "connections/implementation/flags/nearby_connections_feature_flags.h"
#include "connections/implementation/offline_service_controller.h"
#include "connections/listeners.h"
#include "connections/medium_selector.h"
#include "connections/params.h"
#include "connections/payload.h"
#include "connections/v3/bandwidth_info.h"
Expand Down Expand Up @@ -89,6 +94,17 @@ ServiceControllerRouter::ServiceControllerRouter() {
NEARBY_LOGS(INFO) << "ServiceControllerRouter going up.";
}

ServiceControllerRouter::ServiceControllerRouter(
bool enable_wifi_hotspot_for_hp_realtek_devices,
std::function<bool()> if_hp_realtek_device)
: enable_wifi_hotspot_for_hp_realtek_devices_(
enable_wifi_hotspot_for_hp_realtek_devices),
if_hp_realtek_device_(std::move(if_hp_realtek_device)) {
LOG(INFO) << "ServiceControllerRouter going up, "
"enable_wifi_hotspot_for_hp_realtek_devices="
<< enable_wifi_hotspot_for_hp_realtek_devices_;
}

// Constructor called by the CrOS platform implementation to override the
// kEnableBleV2 flag.
ServiceControllerRouter::ServiceControllerRouter(bool enable_ble_v2)
Expand Down Expand Up @@ -713,7 +729,29 @@ void ServiceControllerRouter::SetServiceControllerForTesting(

ServiceController* ServiceControllerRouter::GetServiceController() {
if (!service_controller_) {
service_controller_ = std::make_unique<OfflineServiceController>();
LOG(INFO) << __func__ << ": enable_wifi_hotspot_for_hp_realtek_devices_ = "
<< enable_wifi_hotspot_for_hp_realtek_devices_
<< " if_hp_realtek_device_ = " << if_hp_realtek_device_();
// Temporarily fix to get around wifi hotspot issues for HP Aero with
// Realtek.
// See b/380191431 for more details.
if (!enable_wifi_hotspot_for_hp_realtek_devices_ &&
if_hp_realtek_device_()) {
LOG(INFO) << __func__
<< ": it is HP Realtek device, set BwuManager::Config.";
BwuManager::Config bwu_config;
bwu_config.allow_upgrade_to = {/*bluetooth=*/true,
/*ble=*/true,
/*web_rtc_no_cellular=*/true,
/*web_rtc=*/true,
/*wifi_lan=*/true,
/*wifi_hotspot=*/false,
/*wifi_direct=*/true};
service_controller_ =
std::make_unique<OfflineServiceController>(bwu_config);
} else {
service_controller_ = std::make_unique<OfflineServiceController>();
}
}
return service_controller_.get();
}
Expand Down
9 changes: 6 additions & 3 deletions connections/implementation/service_controller_router.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
#ifndef CORE_INTERNAL_SERVICE_CONTROLLER_ROUTER_H_
#define CORE_INTERNAL_SERVICE_CONTROLLER_ROUTER_H_

#include <functional>
#include <memory>
#include <string>
#include <vector>

#include "absl/container/flat_hash_set.h"
#include "absl/strings/string_view.h"
#include "absl/types/span.h"
#include "connections/implementation/client_proxy.h"
Expand Down Expand Up @@ -59,6 +59,8 @@ class ServiceControllerRouter {
public:
ServiceControllerRouter();
explicit ServiceControllerRouter(bool enable_ble_v2);
ServiceControllerRouter(bool enable_wifi_hotspot_for_hp_realtek_devices,
std::function<bool()> if_hp_realtek_device);
virtual ~ServiceControllerRouter();
// Not copyable or movable
ServiceControllerRouter(const ServiceControllerRouter&) = delete;
Expand Down Expand Up @@ -173,11 +175,12 @@ class ServiceControllerRouter {

void SetServiceControllerForTesting(
std::unique_ptr<ServiceController> service_controller);

private:
// Lazily create ServiceController.
ServiceController* GetServiceController();

private:
bool enable_wifi_hotspot_for_hp_realtek_devices_ = false;
std::function<bool()> if_hp_realtek_device_;
void RouteToServiceController(const std::string& name, Runnable runnable);
void FinishClientSession(ClientProxy* client);

Expand Down
58 changes: 57 additions & 1 deletion connections/implementation/service_controller_router_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,18 @@
#include "connections/implementation/client_proxy.h"
#include "connections/implementation/flags/nearby_connections_feature_flags.h"
#include "connections/implementation/mock_service_controller.h"
#include "connections/implementation/offline_service_controller.h"
#include "connections/listeners.h"
#include "connections/medium_selector.h"
#include "connections/params.h"
#include "connections/v3/bandwidth_info.h"
#include "connections/v3/connection_listening_options.h"
#include "connections/v3/connection_result.h"
#include "connections/v3/connections_device.h"
#include "connections/v3/listening_result.h"
#include "connections/v3/params.h"
#include "internal/interop/authentication_status.h"
#include "internal/flags/nearby_flags.h"
#include "internal/interop/authentication_status.h"
#include "internal/platform/byte_array.h"
#include "internal/platform/condition_variable.h"
#include "internal/platform/count_down_latch.h"
Expand Down Expand Up @@ -1331,6 +1333,60 @@ TEST_F(ServiceControllerRouterTest,
/*expecting_call=*/false);
}

TEST(ServiceControllerRouterCheckHpRealtekDeviceTest,
disableWifiHotspotForHpRealtekDevices_isHPRealtekDevice_checkBwuConfig) {
ServiceControllerRouter router(
false /* enable_wifi_hotspot_for_hp_realtek_devices */,
[]() { return true; });
auto service_controller = router.GetServiceController();
EXPECT_NE(service_controller, nullptr);
auto bwu_config = static_cast<OfflineServiceController*>(service_controller)
->GetBwuConfig();

EXPECT_THAT(bwu_config.allow_upgrade_to,
testing::FieldsAre(/*bluetooth=*/true, /*ble=*/true,
/*web_rtc_no_cellular=*/true,
/*web_rtc=*/true, /*wifi_lan=*/true,
/*wifi_hotspot=*/false, /*wifi_direct=*/true));
}

TEST(
ServiceControllerRouterCheckHpRealtekDeviceTest,
disableWifiHotspotForHpRealtekDevices_notHPRealtekDevice_defaultBwuConfig) {
ServiceControllerRouter router(
false /* enable_wifi_hotspot_for_hp_realtek_devices */,
[]() { return false; });
auto service_controller = router.GetServiceController();
EXPECT_NE(service_controller, nullptr);
auto bwu_config = static_cast<OfflineServiceController*>(service_controller)
->GetBwuConfig();

EXPECT_THAT(
bwu_config.allow_upgrade_to,
testing::FieldsAre(/*bluetooth=*/false, /*ble=*/false,
/*web_rtc_no_cellular=*/false,
/*web_rtc=*/false, /*wifi_lan=*/false,
/*wifi_hotspot=*/false, /*wifi_direct=*/false));
}

TEST(ServiceControllerRouterCheckHpRealtekDeviceTest,
EnableWifiHotspotForHpRealtekDevices_defaultBwuConfig) {
ServiceControllerRouter router(
true /* enable_wifi_hotspot_for_hp_realtek_devices */,
[]() { return true; });
auto service_controller = router.GetServiceController();
EXPECT_NE(service_controller, nullptr);
auto bwu_config = static_cast<OfflineServiceController*>(service_controller)
->GetBwuConfig();

EXPECT_THAT(
bwu_config.allow_upgrade_to,
testing::FieldsAre(/*bluetooth=*/false, /*ble=*/false,
/*web_rtc_no_cellular=*/false,
/*web_rtc=*/false, /*wifi_lan=*/false,
/*wifi_hotspot=*/false, /*wifi_direct=*/false));
}

} // namespace
} // namespace connections
} // namespace nearby
4 changes: 4 additions & 0 deletions sharing/flags/generated/nearby_sharing_feature_flags.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ constexpr auto kDeleteUnexpectedReceivedFileFix =
// The default time in milliseconds a cached entry can be in LOST state.
constexpr auto kDiscoveryCacheLostExpiryMs =
flags::Flag<int64_t>(kConfigPackage, "45658774", 500);
// When true, enable wifi hotspot medium for selected devices.
constexpr auto kEnableWifiHotspotForHpRealtekDevices =
flags::Flag<bool>(kConfigPackage, "45673628", false);
// When true, honor 3P client_id & client_secret in the gRPC request
constexpr auto kHonor3PClientIdAndSecret =
flags::Flag<bool>(kConfigPackage, "45665616", true);
Expand Down Expand Up @@ -124,6 +127,7 @@ inline absl::btree_map<int, const flags::Flag<bool>&> GetBoolFlags() {
{45667328, kCallNearbyIdentityApi},
{45664277, kDedupInUnregisterShareTarget},
{45657036, kDeleteUnexpectedReceivedFileFix},
{45673628, kEnableWifiHotspotForHpRealtekDevices},
{45665616, kHonor3PClientIdAndSecret},
{45417647, kEnableQrCodeUi},
{45410558, kShowAdminModeWarning},
Expand Down
7 changes: 5 additions & 2 deletions sharing/internal/public/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@ cc_library(
],
visibility = ["//visibility:public"],
deps = [
"//internal/network:url",
"//internal/platform:types",
"//sharing/internal/api:platform",
"@com_google_absl//absl/status",
"@com_google_absl//absl/strings",
],
)
Expand All @@ -44,12 +42,16 @@ cc_library(
deps = [
":logging",
":types",
"//internal/flags:nearby_flags",
"//internal/network:url",
"//internal/platform:types",
"//sharing/flags/generated:generated_flags",
"//sharing/internal/api:platform",
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/status",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/synchronization",
],
)

Expand All @@ -66,6 +68,7 @@ cc_test(
":types",
"//internal/platform/implementation/g3", # fixdeps: keep
"//sharing/internal/api:mock_sharing_platform",
"//sharing/internal/api:platform",
"@com_github_protobuf_matchers//protobuf-matchers",
"@com_google_googletest//:gtest_main",
],
Expand Down
2 changes: 2 additions & 0 deletions sharing/internal/public/connectivity_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ class ConnectivityManager {
std::function<void(ConnectionType, bool)>) = 0;
virtual void UnregisterConnectionListener(
absl::string_view listener_name) = 0;
// Is the device a HP device with Realtek wireless module.
virtual bool IsHPRealtekDevice() = 0;
};

} // namespace nearby
Expand Down
39 changes: 38 additions & 1 deletion sharing/internal/public/connectivity_manager_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,19 @@

#include "sharing/internal/public/connectivity_manager_impl.h"

#include <algorithm>
#include <functional>
#include <memory>
#include <optional>
#include <string>
#include <utility>

#include "absl/container/flat_hash_map.h"
#include "absl/strings/match.h"
#include "absl/strings/string_view.h"
#include "absl/synchronization/mutex.h"
#include "internal/flags/nearby_flags.h"
#include "sharing/flags/generated/nearby_sharing_feature_flags.h"
#include "sharing/internal/api/network_monitor.h"
#include "sharing/internal/api/sharing_platform.h"
#include "sharing/internal/public/connectivity_manager.h"
Expand Down Expand Up @@ -55,7 +61,8 @@ std::string GetConnectionTypeString(ConnectionType connection_type) {

} // namespace

ConnectivityManagerImpl::ConnectivityManagerImpl(SharingPlatform& platform) {
ConnectivityManagerImpl::ConnectivityManagerImpl(SharingPlatform& platform)
: platform_(platform) {
network_monitor_ = platform.CreateNetworkMonitor(
[this](api::NetworkMonitor::ConnectionType connection_type,
bool is_lan_connected) {
Expand All @@ -73,6 +80,36 @@ bool ConnectivityManagerImpl::IsLanConnected() {
return network_monitor_->IsLanConnected();
}

bool ConnectivityManagerImpl::IsHPRealtekDevice() {
// This function should not be called if kEnableWifiHotspotForHpRealtekDevices
// is true.
if (NearbyFlags::GetInstance().GetBoolFlag(
sharing::config_package_nearby::nearby_sharing_feature::
kEnableWifiHotspotForHpRealtekDevices)) {
return false;
}

absl::MutexLock lock(&mutex_);

// Lazy initialization since CreateSystemInfo can take a long time.
if (!is_hp_realtek_device_.has_value()) {
auto system_info = platform_.CreateSystemInfo();
const auto& driver_infos = system_info->GetNetworkDriverInfos();
auto it = std::find_if(driver_infos.begin(), driver_infos.end(),
[](auto drive_info) {
return absl::StrContainsIgnoreCase(
drive_info.manufacturer, "Realtek");
});
if (it != driver_infos.end() &&
absl::EqualsIgnoreCase(system_info->GetComputerManufacturer(), "HP")) {
is_hp_realtek_device_ = std::make_optional(true);
} else {
is_hp_realtek_device_ = std::make_optional(false);
}
}
return is_hp_realtek_device_.value();
}

ConnectionType ConnectivityManagerImpl::GetConnectionType() {
return static_cast<ConnectionType>(network_monitor_->GetCurrentConnection());
}
Expand Down
10 changes: 9 additions & 1 deletion sharing/internal/public/connectivity_manager_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@

#include <functional>
#include <memory>
#include <optional>
#include <string>

#include "absl/base/thread_annotations.h"
#include "absl/container/flat_hash_map.h"
#include "absl/strings/string_view.h"
#include "absl/synchronization/mutex.h"
#include "sharing/internal/api/network_monitor.h"
#include "sharing/internal/api/sharing_platform.h"
#include "sharing/internal/api/system_info.h"
#include "sharing/internal/public/connectivity_manager.h"

namespace nearby {
Expand All @@ -34,7 +38,7 @@ class ConnectivityManagerImpl : public ConnectivityManager {
nearby::sharing::api::SharingPlatform& platform);

bool IsLanConnected() override;

bool IsHPRealtekDevice() override;
ConnectionType GetConnectionType() override;

void RegisterConnectionListener(
Expand All @@ -48,6 +52,10 @@ class ConnectivityManagerImpl : public ConnectivityManager {
absl::flat_hash_map<std::string, std::function<void(ConnectionType, bool)>>
listeners_;
std::unique_ptr<api::NetworkMonitor> network_monitor_;
nearby::sharing::api::SharingPlatform& platform_;
mutable absl::Mutex mutex_;

std::optional<bool> is_hp_realtek_device_ ABSL_GUARDED_BY(mutex_);
};

} // namespace nearby
Expand Down
Loading

0 comments on commit 0249f74

Please sign in to comment.