Skip to content

Commit

Permalink
ESP32: Implement ICD related methods for ESP32 Wi-Fi platform (#27319)
Browse files Browse the repository at this point in the history
* ESP32: Implement ICD related methods for ESP32 Wi-Fi platform

* Restyled by clang-format

---------

Co-authored-by: Restyled.io <commits@restyled.io>
  • Loading branch information
2 people authored and pull[bot] committed Aug 25, 2023
1 parent d634855 commit 1574866
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 1 deletion.
33 changes: 33 additions & 0 deletions config/esp32/components/chip/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions src/platform/ESP32/CHIPDevicePlatformConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions src/platform/ESP32/ConnectivityManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
32 changes: 31 additions & 1 deletion src/platform/ESP32/ConnectivityManagerImpl_WiFi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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

Expand Down
7 changes: 7 additions & 0 deletions src/platform/ESP32/ESP32Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions src/platform/ESP32/NetworkCommissioningDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint8_t>(sizeof(wifiConfig.sta.ssid))));
memcpy(wifiConfig.sta.password, key, std::min(keyLen, static_cast<uint8_t>(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);
Expand Down

0 comments on commit 1574866

Please sign in to comment.