feat(wpa_supplicant): refactor wpa_supplicant

This commit is contained in:
Zhang Jun Hao
2020-08-18 10:25:17 +08:00
parent 38b91692da
commit e716bb876d
36 changed files with 3764 additions and 1630 deletions

View File

@ -15,6 +15,10 @@
#ifndef _ESP_WIFI_INTERNAL_H
#define _ESP_WIFI_INTERNAL_H
#include "esp_wifi_types.h"
#include "esp_event.h"
#include "esp_wifi.h"
#ifdef __cplusplus
extern "C" {
#endif
@ -58,6 +62,44 @@ typedef enum {
#define WIFI_LOG_SUBMODULE_FRAG (1<<14)
#define WIFI_LOG_SUBMODULE_WPA2 (1<<15)
/**
* @brief Initialize Wi-Fi Driver
* Alloc resource for WiFi driver, such as WiFi control structure, RX/TX buffer,
* WiFi NVS structure among others.
*
* For the most part, you need not call this function directly. It gets called
* from esp_wifi_init().
*
* This function may be called, if you only need to initialize the Wi-Fi driver
* without having to use the network stack on top.
*
* @param config provide WiFi init configuration
*
* @return
* - ESP_OK: succeed
* - ESP_ERR_NO_MEM: out of memory
* - others: refer to error code esp_err.h
*/
esp_err_t esp_wifi_init_internal(const wifi_init_config_t *config);
/**
* @brief Deinitialize Wi-Fi Driver
* Free resource for WiFi driver, such as WiFi control structure, RX/TX buffer,
* WiFi NVS structure among others.
*
* For the most part, you need not call this function directly. It gets called
* from esp_wifi_deinit().
*
* This function may be called, if you call esp_wifi_init_internal to initialize
* WiFi driver.
*
* @return
* - ESP_OK: succeed
* - others: refer to error code esp_err.h
*/
esp_err_t esp_wifi_deinit_internal(void);
/**
* @brief Set WIFI received TCP/IP data cache ram type
*

View File

@ -89,12 +89,16 @@ typedef enum {
WIFI_REASON_802_1X_AUTH_FAILED = 23,
WIFI_REASON_CIPHER_SUITE_REJECTED = 24,
WIFI_REASON_INVALID_PMKID = 53,
WIFI_REASON_BEACON_TIMEOUT = 200,
WIFI_REASON_NO_AP_FOUND = 201,
WIFI_REASON_AUTH_FAIL = 202,
WIFI_REASON_ASSOC_FAIL = 203,
WIFI_REASON_HANDSHAKE_TIMEOUT = 204,
WIFI_REASON_BASIC_RATE_NOT_SUPPORT = 205,
WIFI_REASON_CONNECTION_FAIL = 205,
WIFI_REASON_AP_TSF_RESET = 206,
WIFI_REASON_BASIC_RATE_NOT_SUPPORT = 207,
} wifi_err_reason_t;
typedef enum {
@ -517,6 +521,10 @@ typedef struct {
wifi_auth_mode_t new_mode; /**< the new auth mode of AP */
} wifi_event_sta_authmode_change_t;
#define MAX_SSID_LEN 32
#define MAX_PASSPHRASE_LEN 64
#define MAX_WPS_AP_CRED 3
/** Argument structure for WIFI_EVENT_STA_WPS_ER_PIN event */
typedef struct {
uint8_t pin_code[8]; /**< PIN code of station in enrollee mode */

View File

@ -60,6 +60,16 @@ void vPortETSIntrUnlock(void);
#define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5]
#define MACSTR "%02x:%02x:%02x:%02x:%02x:%02x"
typedef uint32_t ETSSignal;
typedef uint32_t ETSParam;
typedef struct ETSEventTag ETSEvent; /**< Event transmit/receive in ets*/
struct ETSEventTag {
ETSSignal sig; /**< Event signal, in same task, different Event with different signal*/
ETSParam par; /**< Event parameter, sometimes without usage, then will be set as 0*/
};
/**
* @brief Delay function, maximum value: 65535 us.
*

View File

@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include <stdio.h>
#include <string.h>
#include "sdkconfig.h"
#include "esp_libc.h"
#include "esp_system.h"
@ -19,6 +21,8 @@
#include "esp_log.h"
#include "esp_private/wifi.h"
#include "phy.h"
#include "esp_supplicant/esp_wpa.h"
#include "esp_aio.h"
#define TAG "wifi_init"
@ -30,8 +34,10 @@ const bool _g_esp_wifi_connect_open_router_when_pwd_is_set = true;
const bool _g_esp_wifi_connect_open_router_when_pwd_is_set = false;
#endif
#define EP_OFFSET 36
int ieee80211_output_pbuf(esp_aio_t *aio);
esp_err_t mac_init(void);
esp_err_t esp_wifi_init_internal(const wifi_init_config_t *config);
ESP_EVENT_DEFINE_BASE(WIFI_EVENT);
@ -109,6 +115,19 @@ static void esp_wifi_set_debug_log()
#endif /* CONFIG_ESP8266_WIFI_DEBUG_LOG_ENABLE*/
}
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);
return err;
}
return err;
}
/**
* @brief Init WiFi
* Alloc resource for WiFi driver, such as WiFi control structure, RX/TX buffer,
@ -137,6 +156,16 @@ esp_err_t esp_wifi_init(const wifi_init_config_t *config)
esp_err_t result = esp_wifi_init_internal(config);
if (result == ESP_OK) {
esp_wifi_set_debug_log();
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;
}
}
result = tcpip_adapter_set_default_wifi_handlers();
@ -169,3 +198,42 @@ bool IRAM_ATTR esp_wifi_try_rate_from_high(void) {
#endif
return false;
}
int wifi_internal_send_cb(esp_aio_t* aio)
{
char* pb = (char*)aio->arg;
if (pb) {
os_free(pb);
pb = NULL;
}
return ESP_OK;
}
int esp_wifi_internal_tx(wifi_interface_t wifi_if, void *buffer, uint16_t len)
{
esp_aio_t aio;
esp_err_t ret = ESP_OK;
uint8_t *dram_buffer = (uint8_t *)heap_caps_malloc(len + EP_OFFSET, MALLOC_CAP_8BIT);
if (!dram_buffer) {
return ESP_ERR_NO_MEM;
}
dram_buffer = ((uint8_t *)dram_buffer + EP_OFFSET);
memcpy(dram_buffer, buffer, len);
aio.arg = (char *)dram_buffer - EP_OFFSET;
aio.cb = wifi_internal_send_cb;
aio.fd = wifi_if;
aio.pbuf = (char *)dram_buffer;
aio.len = len;
aio.ret = 0;
ret = ieee80211_output_pbuf(&aio);
if(ret != 0) {
os_free(aio.arg);
}
return ret;
}