This commit is contained in:
@@ -31,6 +31,8 @@ static int s_retry_count = 0;
|
||||
static char s_sta_ip[20] = "No Wi-Fi";
|
||||
static char s_active_ssid[WIFI_SSID_MAX_LEN + 1] = "Disconnected";
|
||||
static SemaphoreHandle_t s_wifi_mux = NULL;
|
||||
static SemaphoreHandle_t s_net_lock = NULL;
|
||||
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* NVS Storage Helpers */
|
||||
@@ -203,6 +205,9 @@ static void wifi_event_handler(void *arg, esp_event_base_t base, int32_t id, voi
|
||||
if (s_wifi_group) {
|
||||
xEventGroupSetBits(s_wifi_group, WIFI_CONNECTED_BIT);
|
||||
}
|
||||
/* Enable modem power saving when idle on station network */
|
||||
esp_wifi_set_ps(WIFI_PS_MIN_MODEM);
|
||||
ESP_LOGI(TAG, "Wi-Fi modem sleep enabled (WIFI_PS_MIN_MODEM: ~30-40 mA saved)");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -361,11 +366,13 @@ esp_err_t wifi_manager_init(void)
|
||||
}
|
||||
|
||||
s_wifi_mux = xSemaphoreCreateMutex();
|
||||
s_net_lock = xSemaphoreCreateMutex();
|
||||
s_wifi_group = xEventGroupCreate();
|
||||
if (s_wifi_group == NULL) {
|
||||
if (s_wifi_group == NULL || s_net_lock == NULL) {
|
||||
return ESP_ERR_NO_MEM;
|
||||
}
|
||||
|
||||
|
||||
ESP_ERROR_CHECK(esp_netif_init());
|
||||
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
||||
esp_netif_t *sta_netif = esp_netif_create_default_wifi_sta();
|
||||
@@ -467,4 +474,45 @@ esp_err_t wifi_manager_start(void)
|
||||
}
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static bool s_power_save_enabled = true;
|
||||
|
||||
esp_err_t wifi_manager_acquire_net_lock(TickType_t timeout_ticks)
|
||||
{
|
||||
if (!s_net_lock) {
|
||||
s_net_lock = xSemaphoreCreateMutex();
|
||||
}
|
||||
if (!s_net_lock) {
|
||||
return ESP_ERR_NO_MEM;
|
||||
}
|
||||
BaseType_t taken = xSemaphoreTake(s_net_lock, timeout_ticks);
|
||||
if (taken == pdTRUE) {
|
||||
/* Full RF performance for active TLS data transfers / OTA */
|
||||
esp_wifi_set_ps(WIFI_PS_NONE);
|
||||
return ESP_OK;
|
||||
}
|
||||
return ESP_ERR_TIMEOUT;
|
||||
}
|
||||
|
||||
void wifi_manager_release_net_lock(void)
|
||||
{
|
||||
if (s_net_lock) {
|
||||
/* Revert to low-power modem sleep when idle */
|
||||
if (s_power_save_enabled && wifi_manager_connected()) {
|
||||
esp_wifi_set_ps(WIFI_PS_MIN_MODEM);
|
||||
}
|
||||
xSemaphoreGive(s_net_lock);
|
||||
}
|
||||
}
|
||||
|
||||
esp_err_t wifi_manager_set_power_save(bool enable)
|
||||
{
|
||||
s_power_save_enabled = enable;
|
||||
wifi_ps_type_t ps_type = enable ? WIFI_PS_MIN_MODEM : WIFI_PS_NONE;
|
||||
esp_err_t err = esp_wifi_set_ps(ps_type);
|
||||
ESP_LOGI(TAG, "Wi-Fi power save %s (%s)",
|
||||
enable ? "enabled" : "disabled",
|
||||
enable ? "WIFI_PS_MIN_MODEM" : "WIFI_PS_NONE");
|
||||
return err;
|
||||
}
|
||||
Reference in New Issue
Block a user