2017-09-22 15:34:52 +00:00
|
|
|
// Copyright 2015-2017 Espressif Systems (Shanghai) PTE LTD
|
2017-08-17 04:11:34 +00:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2017-09-22 15:34:52 +00:00
|
|
|
//
|
2017-08-17 04:11:34 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
#include <esp_event.h>
|
|
|
|
#include <esp_wifi.h>
|
2017-09-22 15:34:52 +00:00
|
|
|
#include "esp_log.h"
|
2019-03-22 06:21:15 +00:00
|
|
|
#include "esp_private/wifi.h"
|
2017-09-22 15:34:52 +00:00
|
|
|
#include "esp_pm.h"
|
|
|
|
#include "soc/rtc.h"
|
2018-08-13 08:37:56 +00:00
|
|
|
#include "esp_wpa.h"
|
2019-06-28 14:47:34 +00:00
|
|
|
#include "esp_netif.h"
|
|
|
|
#include "tcpip_adapter_compatible/tcpip_adapter_compat.h"
|
2018-02-27 10:22:20 +00:00
|
|
|
|
2019-07-19 07:32:05 +00:00
|
|
|
#if (CONFIG_ESP32_WIFI_RX_BA_WIN > CONFIG_ESP32_WIFI_DYNAMIC_RX_BUFFER_NUM)
|
|
|
|
#error "WiFi configuration check: WARNING, WIFI_RX_BA_WIN should not be larger than WIFI_DYNAMIC_RX_BUFFER_NUM!"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if (CONFIG_ESP32_WIFI_RX_BA_WIN > (CONFIG_ESP32_WIFI_STATIC_RX_BUFFER_NUM << 1))
|
|
|
|
#error "WiFi configuration check: WARNING, WIFI_RX_BA_WIN should not be larger than double of the WIFI_STATIC_RX_BUFFER_NUM!"
|
|
|
|
#endif
|
2018-02-27 10:22:20 +00:00
|
|
|
|
2018-08-23 09:50:16 +00:00
|
|
|
ESP_EVENT_DEFINE_BASE(WIFI_EVENT);
|
2017-09-22 15:34:52 +00:00
|
|
|
|
|
|
|
#ifdef CONFIG_PM_ENABLE
|
|
|
|
static esp_pm_lock_handle_t s_wifi_modem_sleep_lock;
|
|
|
|
#endif
|
2017-08-17 04:11:34 +00:00
|
|
|
|
2019-06-17 03:50:37 +00:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
2019-02-20 13:01:27 +00:00
|
|
|
/* Callback function to update WiFi MAC time */
|
|
|
|
wifi_mac_time_update_cb_t s_wifi_mac_time_update_cb = NULL;
|
2019-06-17 03:50:37 +00:00
|
|
|
#endif
|
2019-02-20 13:01:27 +00:00
|
|
|
|
2018-11-06 03:09:32 +00:00
|
|
|
static const char* TAG = "wifi_init";
|
|
|
|
|
2019-07-16 09:33:30 +00:00
|
|
|
static void __attribute__((constructor)) s_set_default_wifi_log_level(void)
|
2018-05-02 05:11:21 +00:00
|
|
|
{
|
|
|
|
/* WiFi libraries aren't compiled to know CONFIG_LOG_DEFAULT_LEVEL,
|
|
|
|
so set it at runtime startup. Done here not in esp_wifi_init() to allow
|
|
|
|
the user to set the level again before esp_wifi_init() is called.
|
|
|
|
*/
|
|
|
|
esp_log_level_set("wifi", CONFIG_LOG_DEFAULT_LEVEL);
|
2019-01-08 14:14:36 +00:00
|
|
|
esp_log_level_set("mesh", CONFIG_LOG_DEFAULT_LEVEL);
|
2018-05-02 05:11:21 +00:00
|
|
|
}
|
|
|
|
|
2019-07-16 09:33:30 +00:00
|
|
|
static void esp_wifi_set_debug_log(void)
|
2018-10-29 09:12:08 +00:00
|
|
|
{
|
|
|
|
/* set WiFi log level and module */
|
|
|
|
#if CONFIG_ESP32_WIFI_DEBUG_LOG_ENABLE
|
|
|
|
uint32_t g_wifi_log_level = WIFI_LOG_INFO;
|
|
|
|
uint32_t g_wifi_log_module = 0;
|
|
|
|
uint32_t g_wifi_log_submodule = 0;
|
|
|
|
#if CONFIG_ESP32_WIFI_DEBUG_LOG_DEBUG
|
|
|
|
g_wifi_log_level = WIFI_LOG_DEBUG;
|
|
|
|
#endif
|
|
|
|
#if CONFIG_ESP32_WIFI_DEBUG_LOG_VERBOSE
|
|
|
|
g_wifi_log_level = WIFI_LOG_VERBOSE;
|
|
|
|
#endif
|
|
|
|
#if CONFIG_ESP32_WIFI_DEBUG_LOG_MODULE_ALL
|
|
|
|
g_wifi_log_module = WIFI_LOG_MODULE_ALL;
|
|
|
|
#endif
|
|
|
|
#if CONFIG_ESP32_WIFI_DEBUG_LOG_MODULE_WIFI
|
|
|
|
g_wifi_log_module = WIFI_LOG_MODULE_WIFI;
|
|
|
|
#endif
|
|
|
|
#if CONFIG_ESP32_WIFI_DEBUG_LOG_MODULE_COEX
|
|
|
|
g_wifi_log_module = WIFI_LOG_MODULE_COEX;
|
|
|
|
#endif
|
|
|
|
#if CONFIG_ESP32_WIFI_DEBUG_LOG_MODULE_MESH
|
|
|
|
g_wifi_log_module = WIFI_LOG_MODULE_MESH;
|
|
|
|
#endif
|
|
|
|
#if CONFIG_ESP32_WIFI_DEBUG_LOG_SUBMODULE_ALL
|
|
|
|
g_wifi_log_submodule |= WIFI_LOG_SUBMODULE_ALL;
|
|
|
|
#endif
|
|
|
|
#if CONFIG_ESP32_WIFI_DEBUG_LOG_SUBMODULE_INIT
|
|
|
|
g_wifi_log_submodule |= WIFI_LOG_SUBMODULE_INIT;
|
|
|
|
#endif
|
|
|
|
#if CONFIG_ESP32_WIFI_DEBUG_LOG_SUBMODULE_IOCTL
|
|
|
|
g_wifi_log_submodule |= WIFI_LOG_SUBMODULE_IOCTL;
|
|
|
|
#endif
|
|
|
|
#if CONFIG_ESP32_WIFI_DEBUG_LOG_SUBMODULE_CONN
|
|
|
|
g_wifi_log_submodule |= WIFI_LOG_SUBMODULE_CONN;
|
|
|
|
#endif
|
|
|
|
#if CONFIG_ESP32_WIFI_DEBUG_LOG_SUBMODULE_SCAN
|
|
|
|
g_wifi_log_submodule |= WIFI_LOG_SUBMODULE_SCAN;
|
|
|
|
#endif
|
|
|
|
esp_wifi_internal_set_log_level(g_wifi_log_level);
|
|
|
|
esp_wifi_internal_set_log_mod(g_wifi_log_module, g_wifi_log_submodule, true);
|
|
|
|
|
|
|
|
#endif /* CONFIG_ESP32_WIFI_DEBUG_LOG_ENABLE*/
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-10-24 09:11:30 +00:00
|
|
|
esp_err_t esp_wifi_deinit(void)
|
|
|
|
{
|
|
|
|
esp_err_t err = ESP_OK;
|
|
|
|
|
|
|
|
esp_supplicant_deinit();
|
|
|
|
err = esp_wifi_deinit_internal();
|
|
|
|
if (err != ESP_OK) {
|
|
|
|
ESP_LOGE(TAG, "Failed to deinit Wi-Fi driver (0x%x)", err);
|
|
|
|
}
|
|
|
|
|
|
|
|
tcpip_adapter_clear_default_wifi_handlers();
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2017-10-13 01:47:19 +00:00
|
|
|
esp_err_t esp_wifi_init(const wifi_init_config_t *config)
|
2017-08-17 04:11:34 +00:00
|
|
|
{
|
2017-09-22 15:34:52 +00:00
|
|
|
#ifdef CONFIG_PM_ENABLE
|
|
|
|
if (s_wifi_modem_sleep_lock == NULL) {
|
|
|
|
esp_err_t err = esp_pm_lock_create(ESP_PM_APB_FREQ_MAX, 0, "wifi",
|
|
|
|
&s_wifi_modem_sleep_lock);
|
|
|
|
if (err != ESP_OK) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2019-06-28 14:47:34 +00:00
|
|
|
#if CONFIG_ESP_NETIF_USE_TCPIP_ADAPTER_COMPATIBLE_LAYER
|
2018-11-06 03:09:32 +00:00
|
|
|
esp_err_t err = tcpip_adapter_set_default_wifi_handlers();
|
|
|
|
if (err != ESP_OK) {
|
|
|
|
ESP_LOGW(TAG, "Failed to set default Wi-Fi event handlers (0x%x)", err);
|
|
|
|
}
|
2019-06-28 14:47:34 +00:00
|
|
|
#endif
|
2018-10-29 09:12:08 +00:00
|
|
|
esp_err_t result = esp_wifi_init_internal(config);
|
2019-02-20 13:01:27 +00:00
|
|
|
if (result == ESP_OK) {
|
|
|
|
esp_wifi_set_debug_log();
|
2019-06-17 03:50:37 +00:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
2019-02-20 13:01:27 +00:00
|
|
|
s_wifi_mac_time_update_cb = esp_wifi_internal_update_mac_time;
|
2019-06-17 03:50:37 +00:00
|
|
|
#endif
|
2018-08-13 08:37:56 +00:00
|
|
|
|
|
|
|
result = esp_supplicant_init();
|
|
|
|
if (result != ESP_OK) {
|
|
|
|
ESP_LOGE(TAG, "Failed to init supplicant (0x%x)", result);
|
|
|
|
esp_err_t deinit_ret = esp_wifi_deinit();
|
|
|
|
if (deinit_ret != ESP_OK) {
|
|
|
|
ESP_LOGE(TAG, "Failed to deinit Wi-Fi (0x%x)", deinit_ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2019-02-20 13:01:27 +00:00
|
|
|
}
|
2018-10-29 09:12:08 +00:00
|
|
|
|
|
|
|
return result;
|
2017-09-22 15:34:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_PM_ENABLE
|
|
|
|
void wifi_apb80m_request(void)
|
|
|
|
{
|
|
|
|
assert(s_wifi_modem_sleep_lock);
|
|
|
|
esp_pm_lock_acquire(s_wifi_modem_sleep_lock);
|
|
|
|
if (rtc_clk_apb_freq_get() != APB_CLK_FREQ) {
|
|
|
|
ESP_LOGE(__func__, "WiFi needs 80MHz APB frequency to work, but got %dHz", rtc_clk_apb_freq_get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void wifi_apb80m_release(void)
|
|
|
|
{
|
|
|
|
assert(s_wifi_modem_sleep_lock);
|
|
|
|
esp_pm_lock_release(s_wifi_modem_sleep_lock);
|
2017-08-17 04:11:34 +00:00
|
|
|
}
|
2017-09-22 15:34:52 +00:00
|
|
|
#endif //CONFIG_PM_ENABLE
|