From bd91caad3887f9357d311751cc9a870a5505f89a Mon Sep 17 00:00:00 2001 From: Lasan Mahaliyana Date: Thu, 12 Mar 2026 01:07:20 +0530 Subject: [PATCH] wifi hotspot now starts on 40/80mhz by defaults and fallbacks to auto channel width selection --- .../implementation/linux/wifi_hotspot.cc | 106 +++++++++++------- 1 file changed, 66 insertions(+), 40 deletions(-) diff --git a/internal/platform/implementation/linux/wifi_hotspot.cc b/internal/platform/implementation/linux/wifi_hotspot.cc index e8d21a47..67248750 100644 --- a/internal/platform/implementation/linux/wifi_hotspot.cc +++ b/internal/platform/implementation/linux/wifi_hotspot.cc @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -37,7 +38,10 @@ namespace { // Prefer non-DFS channels that are broadly available in the 00 (world) regulatory // domain for AP mode. constexpr int kPreferred24GhzChannel = 6; -constexpr int kPreferred5GhzChannel = 40; +constexpr int kPreferred5GhzChannel = 36; +// NM_SETTING_WIRELESS_CHANNEL_WIDTH_* (nm-settings: 40mhz=40, 80mhz=80). +constexpr int32_t k40MhzChannelWidth = 40; +constexpr int32_t k80MhzChannelWidth = 80; } // namespace @@ -179,46 +183,68 @@ bool NetworkManagerWifiHotspotMedium::StartWifiHotspot( << "channel " << selected_channel; } - std::map> - connection_settings{ - { - "connection", - {{"uuid", *connection_id}, - {"id", "Google Nearby Hotspot"}, - {"type", "802-11-wireless"}, - {"zone", "Public"}}, - }, - {"802-11-wireless", - {{"assigned-mac-address", "random"}, - {"ap-isolation", networkmanager::constants::kNMTernaryFalse}, - {"mode", "ap"}, - {"band", selected_band}, - {"channel", selected_channel}, - {"ssid", std::vector(ssid.begin(), ssid.end())}, - {"security", "802-11-wireless-security"}}}, - {"802-11-wireless-security", - {{"pmf", - networkmanager::constants::setting::kWirelessSecurityPMFDisable}, - {"key-mgmt", "wpa-psk"}, - {"psk", password}}}, - {"ipv4", {{"method", "shared"}}}, - {"ipv6", - { - {"addr-gen-mode", networkmanager::constants::setting:: - kIP6ConfigAddrGenModeStablePrivacy}, - {"method", "shared"}, - }}}; + const int32_t fallback_channel_width = + selected_band == "a" ? k80MhzChannelWidth : k40MhzChannelWidth; + std::unique_ptr active_conn; - try { - auto [path, active_path, result] = - network_manager_->AddAndActivateConnection2( - connection_settings, wireless_device_->getObjectPath(), "/", - {{"persist", "volatile"}, {"bind-activation", "dbus-client"}}); - active_conn = std::make_unique( - system_bus_, active_path); - } catch (const sdbus::Error &e) { - DBUS_LOG_METHOD_CALL_ERROR(network_manager_, "AddAndActivateConnection2", - e); + for (bool include_channel_width : {true, false}) { + std::map> + connection_settings{ + { + "connection", + {{"uuid", *connection_id}, + {"id", "Google Nearby Hotspot"}, + {"type", "802-11-wireless"}, + {"zone", "Public"}}, + }, + {"802-11-wireless", + {{"assigned-mac-address", "random"}, + {"ap-isolation", networkmanager::constants::kNMTernaryFalse}, + {"mode", "ap"}, + {"band", selected_band}, + {"channel", selected_channel}, + {"ssid", std::vector(ssid.begin(), ssid.end())}, + {"security", "802-11-wireless-security"}}}, + {"802-11-wireless-security", + {{"pmf", + networkmanager::constants::setting::kWirelessSecurityPMFDisable}, + {"key-mgmt", "wpa-psk"}, + {"psk", password}}}, + {"ipv4", {{"method", "shared"}}}, + {"ipv6", + { + {"addr-gen-mode", networkmanager::constants::setting:: + kIP6ConfigAddrGenModeStablePrivacy}, + {"method", "shared"}, + }}}; + if (include_channel_width) { + connection_settings["802-11-wireless"]["channel-width"] = + fallback_channel_width; + } + try { + auto [path, active_path, result] = + network_manager_->AddAndActivateConnection2( + connection_settings, wireless_device_->getObjectPath(), "/", + {{"persist", "volatile"}, {"bind-activation", "dbus-client"}}); + active_conn = std::make_unique( + system_bus_, active_path); + break; + } catch (const sdbus::Error &e) { + if (include_channel_width) { + LOG(WARNING) << __func__ + << ": Failed to set channel-width=" + << fallback_channel_width << "mhz: " + << e.getName() << ": " << e.getMessage() + << ". Retrying without channel-width."; + continue; + } + DBUS_LOG_METHOD_CALL_ERROR(network_manager_, "AddAndActivateConnection2", + e); + return false; + } + } + + if (active_conn == nullptr) { return false; }