diff --git a/config/esp32/components/chip/Kconfig b/config/esp32/components/chip/Kconfig index bff112fd594046..7f6694a615bf6d 100644 --- a/config/esp32/components/chip/Kconfig +++ b/config/esp32/components/chip/Kconfig @@ -390,6 +390,39 @@ menu "CHIP Device Layer" The amount of time (in milliseconds) to wait for Internet connectivity to be established on the device's WiFi station interface during a Network Provisioning TestConnectivity operation. + choice WIFI_POWER_SAVE_MODE + prompt "WiFi power-saving mode" + default WIFI_POWER_SAVE_MIN + depends on ENABLE_WIFI_STATION && !ENABLE_WIFI_AP + help + The Modem-sleep mode which refers to the legacy power-saving mode in the IEEE 802.11 protocol. + + config WIFI_POWER_SAVE_MIN + bool "Minimal power-saving mode" + help + In minimum power-saving mode, station wakes up every DTIM to receive beacon. + + config WIFI_POWER_SAVE_MAX + bool "Maximum power-saving mode" + help + In maximum power-saving mode, station wakes up in every listen interval to receive beacon. + Listen interval can be configured by calling API 'esp_wifi_set_config()' before connecting + to AP. + + config WIFI_POWER_SAVE_NONE + bool "No power-saving" + help + No power save + + endchoice + + config WIFI_PS_LISTEN_INTERVAL + int "Listen interval for maximum power-saving mode" + depends on WIFI_POWER_SAVE_MAX + default 3 + help + Interval for station to listen to beacon from AP. The unit of listen interval is one beacon interval. + endmenu menu "WiFi AP Options" diff --git a/src/platform/ESP32/CHIPDevicePlatformConfig.h b/src/platform/ESP32/CHIPDevicePlatformConfig.h index 5bee6b221919c2..96b910785cf620 100644 --- a/src/platform/ESP32/CHIPDevicePlatformConfig.h +++ b/src/platform/ESP32/CHIPDevicePlatformConfig.h @@ -72,6 +72,8 @@ #define CHIP_DEVICE_CONFIG_ENABLE_WIFI CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP | CHIP_DEVICE_CONFIG_ENABLE_WIFI_STATION #endif // CONFIG_IDF_TARGET_ESP32H2 +#define CHIP_DEVICE_CONFIG_ENABLE_SED CONFIG_WIFI_POWER_SAVE_MIN || CONFIG_WIFI_POWER_SAVE_MAX + #define CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE CONFIG_ENABLE_CHIPOBLE #define CHIP_DEVICE_CONFIG_ENABLE_EXTENDED_DISCOVERY CONFIG_ENABLE_EXTENDED_DISCOVERY #define CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONABLE_DEVICE_TYPE CONFIG_ENABLE_COMMISSIONABLE_DEVICE_TYPE diff --git a/src/platform/ESP32/ConnectivityManagerImpl.h b/src/platform/ESP32/ConnectivityManagerImpl.h index 35658206b76c02..bc12092c506391 100644 --- a/src/platform/ESP32/ConnectivityManagerImpl.h +++ b/src/platform/ESP32/ConnectivityManagerImpl.h @@ -119,6 +119,11 @@ class ConnectivityManagerImpl final : public ConnectivityManager, void _OnWiFiScanDone(); void _OnWiFiStationProvisionChange(); +#if CHIP_DEVICE_CONFIG_ENABLE_SED + CHIP_ERROR _GetSEDIntervalsConfig(ConnectivityManager::SEDIntervalsConfig & intervalsConfig); + CHIP_ERROR _SetSEDIntervalsConfig(const ConnectivityManager::SEDIntervalsConfig & intervalsConfig); + CHIP_ERROR _RequestSEDActiveMode(bool onOff, bool delayIdle = false); +#endif // ===== Private members reserved for use by this class only. System::Clock::Timestamp mLastStationConnectFailTime; diff --git a/src/platform/ESP32/ConnectivityManagerImpl_WiFi.cpp b/src/platform/ESP32/ConnectivityManagerImpl_WiFi.cpp index 98d87bff717be8..e79ba6e5830aaf 100644 --- a/src/platform/ESP32/ConnectivityManagerImpl_WiFi.cpp +++ b/src/platform/ESP32/ConnectivityManagerImpl_WiFi.cpp @@ -423,7 +423,10 @@ CHIP_ERROR ConnectivityManagerImpl::InitWiFi() std::min(sizeof(wifiConfig.sta.password), strlen(CONFIG_DEFAULT_WIFI_PASSWORD))); wifiConfig.sta.scan_method = WIFI_ALL_CHANNEL_SCAN; wifiConfig.sta.sort_method = WIFI_CONNECT_AP_BY_SIGNAL; - esp_err_t err = esp_wifi_set_config(WIFI_IF_STA, &wifiConfig); +#if CONFIG_WIFI_POWER_SAVE_MAX + wifiConfig.sta.listen_interval = CONFIG_WIFI_PS_LISTEN_INTERVAL; +#endif + esp_err_t err = esp_wifi_set_config(WIFI_IF_STA, &wifiConfig); if (err != ESP_OK) { ChipLogError(DeviceLayer, "esp_wifi_set_config() failed: %s", esp_err_to_name(err)); @@ -1109,6 +1112,33 @@ void ConnectivityManagerImpl::OnStationIPv6AddressAvailable(const ip_event_got_i #endif } +#if CHIP_DEVICE_CONFIG_ENABLE_SED +static constexpr uint32_t kBeaconIntervalMs = 100; +static constexpr uint32_t kDefaultDTIMInterval = 3; // this is determined by the AP, use a constant value for it. + +CHIP_ERROR ConnectivityManagerImpl::_GetSEDIntervalsConfig(ConnectivityManager::SEDIntervalsConfig & sedIntervalsConfig) +{ + sedIntervalsConfig.ActiveIntervalMS = chip::System::Clock::Milliseconds32(kBeaconIntervalMs); +#if CONFIG_WIFI_POWER_SAVE_MIN + sedIntervalsConfig.IdleIntervalMS = chip::System::Clock::Milliseconds32(kDefaultDTIMInterval * kBeaconIntervalMs); +#elif CONFIG_WIFI_POWER_SAVE_MAX + sedIntervalsConfig.IdleIntervalMS = chip::System::Clock::Milliseconds32(CONFIG_WIFI_PS_LISTEN_INTERVAL * kBeaconIntervalMs); +#endif + return CHIP_NO_ERROR; +} + +CHIP_ERROR ConnectivityManagerImpl::_SetSEDIntervalsConfig(const ConnectivityManager::SEDIntervalsConfig & intervalsConfig) +{ + return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE; +} + +CHIP_ERROR ConnectivityManagerImpl::_RequestSEDActiveMode(bool onOff, bool delayIdle) +{ + return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE; +} + +#endif // CHIP_DEVICE_CONFIG_ENABLE_SED + } // namespace DeviceLayer } // namespace chip diff --git a/src/platform/ESP32/ESP32Utils.cpp b/src/platform/ESP32/ESP32Utils.cpp index 4a03d08282dcda..c5000929ab08dd 100644 --- a/src/platform/ESP32/ESP32Utils.cpp +++ b/src/platform/ESP32/ESP32Utils.cpp @@ -117,6 +117,13 @@ CHIP_ERROR ESP32Utils::StartWiFiLayer(void) ChipLogError(DeviceLayer, "esp_wifi_start() failed: %s", esp_err_to_name(err)); return ESP32Utils::MapError(err); } +#if CONFIC_WIFI_POWER_SAVE_MIN + esp_wifi_set_ps(WIFI_PS_MIN_MODEM); +#elif CONFIC_WIFI_POWER_SAVE_MAX + esp_wifi_set_ps(WIFI_PS_MAX_MODEM); +#elif CONFIG_WIFI_POWER_SAVE_NONE + esp_wifi_set_ps(WIFI_PS_NONE); +#endif } return CHIP_NO_ERROR; diff --git a/src/platform/ESP32/NetworkCommissioningDriver.cpp b/src/platform/ESP32/NetworkCommissioningDriver.cpp index 7e080f5246256e..a99f20a57a0885 100644 --- a/src/platform/ESP32/NetworkCommissioningDriver.cpp +++ b/src/platform/ESP32/NetworkCommissioningDriver.cpp @@ -189,6 +189,9 @@ CHIP_ERROR ESPWiFiDriver::ConnectWiFiNetwork(const char * ssid, uint8_t ssidLen, memset(&wifiConfig, 0, sizeof(wifiConfig)); memcpy(wifiConfig.sta.ssid, ssid, std::min(ssidLen, static_cast(sizeof(wifiConfig.sta.ssid)))); memcpy(wifiConfig.sta.password, key, std::min(keyLen, static_cast(sizeof(wifiConfig.sta.password)))); +#if CONFIG_WIFI_POWER_SAVE_MAX + wifiConfig.sta..listen_interval = CONFIG_WIFI_PS_LISTEN_INTERVAL; +#endif // Configure the ESP WiFi interface. esp_err_t err = esp_wifi_set_config(WIFI_IF_STA, &wifiConfig);