Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions components/wifi-manager/network_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,12 @@ bool network_is_interface_connected(esp_netif_t * interface);
*/
#define DEFAULT_STA_ONLY 1

/** @brief Defines if wifi power save shall be enabled.
* Value: WIFI_PS_NONE for full power (wifi modem always on)
* Value: WIFI_PS_MODEM for power save (wifi modem sleep periodically)
/** @brief Defines the default wifi power save mode.
* Value: WIFI_PS_NONE (0) for full power (wifi modem always on)
* Value: WIFI_PS_MIN_MODEM (1) for minimum power save (wifi modem sleeps periodically)
* Value: WIFI_PS_MAX_MODEM (2) for maximum power save
* Note: Power save is only effective when in STA only mode
* Can be overridden via NVS key "wifi_ps" (0, 1, or 2)
*/
#define DEFAULT_STA_POWER_SAVE WIFI_PS_MIN_MODEM

Expand Down
18 changes: 16 additions & 2 deletions components/wifi-manager/network_wifi.c
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,17 @@ esp_netif_t* network_wifi_get_interface() {
esp_netif_t* network_wifi_get_ap_interface() {
return wifi_ap_netif;
}
static wifi_ps_type_t network_wifi_get_ps_mode() {
uint16_t ps_mode = DEFAULT_STA_POWER_SAVE;
config_get_uint16t_from_str("wifi_ps", &ps_mode, DEFAULT_STA_POWER_SAVE);
Comment on lines +478 to +479
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The ps_mode variable is initialized, but its value is immediately overwritten by config_get_uint16t_from_str in the next line, as this function handles setting a default value if the NVS key is not found. The initial assignment is redundant and can be removed to make the code slightly cleaner.

    uint16_t ps_mode;
    config_get_uint16t_from_str("wifi_ps", &ps_mode, DEFAULT_STA_POWER_SAVE);

if (ps_mode > WIFI_PS_MAX_MODEM) {
ESP_LOGW(TAG, "Invalid wifi_ps value %d, defaulting to %d", ps_mode, DEFAULT_STA_POWER_SAVE);
ps_mode = DEFAULT_STA_POWER_SAVE;
}
ESP_LOGI(TAG, "WiFi power save mode: %d (0=none, 1=min_modem, 2=max_modem)", ps_mode);
return (wifi_ps_type_t)ps_mode;
}

esp_err_t network_wifi_set_sta_mode() {
if (!wifi_netif) {
ESP_LOGE(TAG, "Wifi not initialized. Cannot set sta mode");
Expand All @@ -488,6 +499,8 @@ esp_err_t network_wifi_set_sta_mode() {
err = esp_wifi_start();
if (err != ESP_OK) {
ESP_LOGE(TAG, "Error starting wifi: %s", esp_err_to_name(err));
} else {
ESP_ERROR_CHECK_WITHOUT_ABORT(esp_wifi_set_ps(network_wifi_get_ps_mode()));
}
}
return err;
Expand Down Expand Up @@ -951,9 +964,10 @@ esp_netif_t* network_wifi_config_ap() {
}

msg = "Setting wifi power save";
ESP_LOGD(TAG, "%s (%d)", msg, DEFAULT_STA_POWER_SAVE);
wifi_ps_type_t ps_mode = network_wifi_get_ps_mode();
ESP_LOGD(TAG, "%s (%d)", msg, ps_mode);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The network_wifi_get_ps_mode() function, called on the previous line, already logs the selected power save mode at the INFO level. This ESP_LOGD call is therefore redundant and can be removed to avoid duplicate log entries.


if ((err = esp_wifi_set_ps(DEFAULT_STA_POWER_SAVE)) != ESP_OK) /* stop AP DHCP server */
if ((err = esp_wifi_set_ps(ps_mode)) != ESP_OK)
{
ESP_LOGE(TAG, "%s failed. Error %s", msg, esp_err_to_name(err));
return wifi_ap_netif;
Expand Down