mirror of
https://github.com/espressif/esp-idf
synced 2025-03-10 01:29:21 -04:00
esp_netif: extract wifi_netif module as an abstraction to wifi universal interface defined by if handle and callback
This commit is contained in:
parent
359f6b3a21
commit
20add7da60
@ -176,7 +176,6 @@ struct esp_netif_driver_ifconfig {
|
|||||||
void (*driver_free_rx_buffer)(void *h, void* buffer);
|
void (*driver_free_rx_buffer)(void *h, void* buffer);
|
||||||
};
|
};
|
||||||
|
|
||||||
//typedef struct esp_netif_net_stack_ifconfig esp_netif_net_stack_ifconfig_t;
|
|
||||||
typedef struct esp_netif_driver_ifconfig esp_netif_driver_ifconfig_t;
|
typedef struct esp_netif_driver_ifconfig esp_netif_driver_ifconfig_t;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -194,4 +193,9 @@ struct esp_netif_config {
|
|||||||
const esp_netif_netstack_config_t *stack;
|
const esp_netif_netstack_config_t *stack;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief ESP-NETIF Receive function type
|
||||||
|
*/
|
||||||
|
typedef esp_err_t (*esp_netif_receive_t)(esp_netif_t *esp_netif, void *buffer, size_t len, void *eb);
|
||||||
|
|
||||||
#endif // _ESP_NETIF_TYPES_H_
|
#endif // _ESP_NETIF_TYPES_H_
|
@ -16,6 +16,7 @@ idf_component_register(SRCS "src/coexist.c"
|
|||||||
"src/smartconfig_ack.c"
|
"src/smartconfig_ack.c"
|
||||||
"src/wifi_init.c"
|
"src/wifi_init.c"
|
||||||
"src/wifi_default.c"
|
"src/wifi_default.c"
|
||||||
|
"src/wifi_netif.c"
|
||||||
INCLUDE_DIRS "include" "${idf_target}/include"
|
INCLUDE_DIRS "include" "${idf_target}/include"
|
||||||
PRIV_REQUIRES wpa_supplicant nvs_flash esp_netif
|
PRIV_REQUIRES wpa_supplicant nvs_flash esp_netif
|
||||||
LDFRAGMENTS "${ldfragments}")
|
LDFRAGMENTS "${ldfragments}")
|
||||||
|
@ -16,26 +16,42 @@
|
|||||||
#define _ESP_WIFI_DEFAULT_H
|
#define _ESP_WIFI_DEFAULT_H
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Sets default wifi event handlers for STA interface
|
* @brief Attaches wifi station interface to supplied netif
|
||||||
*
|
*
|
||||||
* @param esp_netif instance of corresponding if object
|
* @param esp_netif instance to attach the wifi station to
|
||||||
*
|
*
|
||||||
* @return
|
* @return
|
||||||
* - ESP_OK on success, error returned from esp_event_handler_register if failed
|
* - ESP_OK on success
|
||||||
|
* - ESP_FAIL if attach failed
|
||||||
*/
|
*/
|
||||||
esp_err_t esp_wifi_set_default_wifi_sta_handlers(void *esp_netif);
|
esp_err_t esp_netif_attach_wifi_station(esp_netif_t *esp_netif);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Attaches wifi soft AP interface to supplied netif
|
||||||
|
*
|
||||||
|
* @param esp_netif instance to attach the wifi AP to
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
* - ESP_OK on success
|
||||||
|
* - ESP_FAIL if attach failed
|
||||||
|
*/
|
||||||
|
esp_err_t esp_netif_attach_wifi_ap(esp_netif_t *esp_netif);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Sets default wifi event handlers for STA interface
|
* @brief Sets default wifi event handlers for STA interface
|
||||||
*
|
*
|
||||||
* @param esp_netif instance of corresponding if object
|
* @return
|
||||||
|
* - ESP_OK on success, error returned from esp_event_handler_register if failed
|
||||||
|
*/
|
||||||
|
esp_err_t esp_wifi_set_default_wifi_sta_handlers(void);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Sets default wifi event handlers for STA interface
|
||||||
*
|
*
|
||||||
* @return
|
* @return
|
||||||
* - ESP_OK on success, error returned from esp_event_handler_register if failed
|
* - ESP_OK on success, error returned from esp_event_handler_register if failed
|
||||||
*/
|
*/
|
||||||
esp_err_t esp_wifi_set_default_wifi_driver_and_handlers(wifi_interface_t wifi_if, void *esp_netif);
|
esp_err_t esp_wifi_set_default_wifi_ap_handlers(void);
|
||||||
|
|
||||||
esp_err_t esp_wifi_set_default_wifi_ap_handlers(void *esp_netif);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Clears default wifi event handlers for supplied network interface
|
* @brief Clears default wifi event handlers for supplied network interface
|
||||||
|
84
components/esp_wifi/include/esp_wifi_netif.h
Normal file
84
components/esp_wifi/include/esp_wifi_netif.h
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
// Copyright 2019 Espressif Systems (Shanghai) PTE LTD
|
||||||
|
//
|
||||||
|
// 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
|
||||||
|
|
||||||
|
// 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.
|
||||||
|
|
||||||
|
#ifndef _ESP_WIFI_NETIF_H
|
||||||
|
#define _ESP_WIFI_NETIF_H
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Number of WiFi interfaces used by wifi-netif abstraction
|
||||||
|
*/
|
||||||
|
#define MAX_WIFI_IFS (2)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Forward declaration of WiFi interface handle
|
||||||
|
*/
|
||||||
|
typedef struct wifi_netif_driver* wifi_netif_driver_t;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Creates wifi driver instance to be used with esp-netif
|
||||||
|
*
|
||||||
|
* @param wifi_if wifi interface type (station, softAP)
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
* - pointer to wifi interface handle on success
|
||||||
|
* - NULL otherwise
|
||||||
|
*/
|
||||||
|
wifi_netif_driver_t esp_wifi_create_if_driver(wifi_interface_t wifi_if);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Destroys wifi driver instance
|
||||||
|
*
|
||||||
|
* @param h pointer to wifi interface handle
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
void esp_wifi_destroy_if_driver(wifi_netif_driver_t h);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Return mac of specified wifi driver instance
|
||||||
|
*
|
||||||
|
* @param[in] ifx pointer to wifi interface handle
|
||||||
|
* @param[out] mac output mac address
|
||||||
|
*
|
||||||
|
* @return ESP_OK on success
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
esp_err_t esp_wifi_get_if_mac(wifi_netif_driver_t ifx, uint8_t mac[6]);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Return true if the supplied interface instance is ready after start.
|
||||||
|
* Typically used when registering on receive callback, which ought to be
|
||||||
|
* installed as soon as AP started, but once STA gets connected.
|
||||||
|
*
|
||||||
|
* @param[in] ifx pointer to wifi interface handle
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
* - true if ready after intertace started (typically Access Point type)
|
||||||
|
* - false if ready once intertace connected (typically for Station type)
|
||||||
|
*/
|
||||||
|
bool esp_wifi_is_if_ready_when_stared(wifi_netif_driver_t ifx);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Register interface receive callback function with argument
|
||||||
|
*
|
||||||
|
* @param[in] ifx pointer to wifi interface handle
|
||||||
|
* @param[in] fn funtion to be registered (typically esp_netif_receive)
|
||||||
|
* @param[in] arg argument to be supplied to registered function (typically esp_netif ptr)
|
||||||
|
*
|
||||||
|
* @return ESP_OK on success
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
esp_err_t esp_wifi_register_if_rxcb(wifi_netif_driver_t ifx, esp_netif_receive_t fn, void * arg);
|
||||||
|
|
||||||
|
|
||||||
|
#endif //_ESP_WIFI_NETIF_H
|
@ -15,66 +15,44 @@
|
|||||||
#include "esp_netif.h"
|
#include "esp_netif.h"
|
||||||
#include "esp_log.h"
|
#include "esp_log.h"
|
||||||
#include "esp_private/wifi.h"
|
#include "esp_private/wifi.h"
|
||||||
|
#include "esp_wifi_netif.h"
|
||||||
|
|
||||||
//
|
//
|
||||||
// Purpose of this module is to provide basic wifi initialization setup for
|
// Purpose of this module is to provide basic wifi initialization setup for
|
||||||
// station and AP and their conversion to esp-netif objects
|
// default station and AP and to register default handles for these interfaces
|
||||||
// Also this module holds esp-netif handles for AP and STA
|
|
||||||
//
|
//
|
||||||
|
|
||||||
typedef struct wifi_netif_driver {
|
|
||||||
esp_netif_driver_base_t base;
|
|
||||||
wifi_interface_t wifi_if;
|
|
||||||
}* wifi_netif_driver_t;
|
|
||||||
|
|
||||||
static const char* TAG = "wifi_init_default";
|
static const char* TAG = "wifi_init_default";
|
||||||
|
|
||||||
#define MAX_WIFI_IFS (2)
|
|
||||||
|
|
||||||
static esp_netif_t *s_wifi_netifs[MAX_WIFI_IFS] = { NULL };
|
static esp_netif_t *s_wifi_netifs[MAX_WIFI_IFS] = { NULL };
|
||||||
static bool wifi_default_handlers_set = false;
|
static bool wifi_default_handlers_set = false;
|
||||||
|
|
||||||
static esp_err_t wifi_sta_receive(void *buffer, uint16_t len, void *eb)
|
static esp_err_t disconnect_and_destroy(esp_netif_t* esp_netif);
|
||||||
{
|
|
||||||
return esp_netif_receive(s_wifi_netifs[WIFI_IF_STA], buffer, len, eb);
|
|
||||||
}
|
|
||||||
|
|
||||||
static esp_err_t wifi_ap_receive(void *buffer, uint16_t len, void *eb)
|
//
|
||||||
{
|
// Default event handlers
|
||||||
return esp_netif_receive(s_wifi_netifs[WIFI_IF_AP], buffer, len, eb);
|
//
|
||||||
}
|
|
||||||
|
|
||||||
void wifi_free(void *h, void* buffer)
|
/**
|
||||||
|
* @brief Wifi start action when station or AP get started
|
||||||
|
*/
|
||||||
|
static void wifi_start(void *esp_netif, esp_event_base_t base, int32_t event_id, void *data)
|
||||||
{
|
{
|
||||||
esp_wifi_internal_free_rx_buffer(buffer);
|
|
||||||
}
|
|
||||||
|
|
||||||
esp_err_t wifi_transmit(void *h, void *buffer, size_t len)
|
|
||||||
{
|
|
||||||
wifi_netif_driver_t driver = h;
|
|
||||||
return esp_wifi_internal_tx(driver->wifi_if, buffer, len);
|
|
||||||
}
|
|
||||||
|
|
||||||
void wifi_start(void *esp_netif, esp_event_base_t base, int32_t event_id, void *data)
|
|
||||||
{
|
|
||||||
ESP_LOGD(TAG, "%s esp-netif:%p event-id%d", __func__, esp_netif, event_id);
|
|
||||||
|
|
||||||
uint8_t mac[6];
|
uint8_t mac[6];
|
||||||
esp_err_t ret;
|
esp_err_t ret;
|
||||||
|
|
||||||
wifi_netif_driver_t driver = esp_netif_get_io_driver(esp_netif);
|
ESP_LOGD(TAG, "%s esp-netif:%p event-id%d", __func__, esp_netif, event_id);
|
||||||
wifi_interface_t wifi_interface = driver->wifi_if;
|
|
||||||
|
|
||||||
if ((ret = esp_wifi_get_mac(wifi_interface, mac)) != ESP_OK) {
|
wifi_netif_driver_t driver = esp_netif_get_io_driver(esp_netif);
|
||||||
|
|
||||||
|
if ((ret = esp_wifi_get_if_mac(driver, mac)) != ESP_OK) {
|
||||||
ESP_LOGE(TAG, "esp_wifi_get_mac failed with %d", ret);
|
ESP_LOGE(TAG, "esp_wifi_get_mac failed with %d", ret);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
ESP_LOGD(TAG, "WIFI mac address: %x %x %x %x %x %x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
|
ESP_LOGD(TAG, "WIFI mac address: %x %x %x %x %x %x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
|
||||||
|
|
||||||
if (wifi_interface == ESP_IF_WIFI_AP) {
|
if (esp_wifi_is_if_ready_when_stared(driver)) {
|
||||||
// By default register wifi rxcb on start for AP only, station gets it registered on connect event
|
if ((ret = esp_wifi_register_if_rxcb(driver, esp_netif_receive, esp_netif)) != ESP_OK) {
|
||||||
if ((ret = esp_wifi_internal_reg_rxcb(wifi_interface, wifi_ap_receive)) != ESP_OK) {
|
ESP_LOGE(TAG, "esp_wifi_register_if_rxcb for if=%p failed with %d", driver, ret);
|
||||||
ESP_LOGE(TAG, "esp_wifi_internal_reg_rxcb for if=%d failed with %d", wifi_interface, ret);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -83,6 +61,9 @@ void wifi_start(void *esp_netif, esp_event_base_t base, int32_t event_id, void *
|
|||||||
esp_netif_action_start(esp_netif, base, event_id, data);
|
esp_netif_action_start(esp_netif, base, event_id, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Wifi default handlers for specific events for station and APs
|
||||||
|
*/
|
||||||
|
|
||||||
static void wifi_default_action_sta_start(void *arg, esp_event_base_t base, int32_t event_id, void *data)
|
static void wifi_default_action_sta_start(void *arg, esp_event_base_t base, int32_t event_id, void *data)
|
||||||
{
|
{
|
||||||
@ -102,11 +83,16 @@ static void wifi_default_action_sta_connected(void *arg, esp_event_base_t base,
|
|||||||
{
|
{
|
||||||
if (s_wifi_netifs[WIFI_IF_STA] != NULL) {
|
if (s_wifi_netifs[WIFI_IF_STA] != NULL) {
|
||||||
esp_err_t ret;
|
esp_err_t ret;
|
||||||
// By default register wifi rxcb once the STA gets connected
|
esp_netif_t *esp_netif = s_wifi_netifs[WIFI_IF_STA];
|
||||||
if ((ret = esp_wifi_internal_reg_rxcb(ESP_IF_WIFI_STA, wifi_sta_receive)) != ESP_OK) {
|
wifi_netif_driver_t driver = esp_netif_get_io_driver(esp_netif);
|
||||||
ESP_LOGE(TAG, "esp_wifi_internal_reg_rxcb for if=%d failed with %d", ESP_IF_WIFI_STA, ret);
|
|
||||||
|
if (!esp_wifi_is_if_ready_when_stared(driver)) {
|
||||||
|
// if interface not ready when started, rxcb to be registered on connection
|
||||||
|
if ((ret = esp_wifi_register_if_rxcb(driver, esp_netif_receive, esp_netif)) != ESP_OK) {
|
||||||
|
ESP_LOGE(TAG, "esp_wifi_register_if_rxcb for if=%p failed with %d", driver, ret);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
esp_netif_action_connected(s_wifi_netifs[WIFI_IF_STA], base, event_id, data);
|
esp_netif_action_connected(s_wifi_netifs[WIFI_IF_STA], base, event_id, data);
|
||||||
}
|
}
|
||||||
@ -145,7 +131,10 @@ static void wifi_default_action_sta_got_ip(void *arg, esp_event_base_t base, int
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t esp_wifi_clear_default_wifi_handlers(void)
|
/**
|
||||||
|
* @brief Clear default handlers
|
||||||
|
*/
|
||||||
|
esp_err_t _esp_wifi_clear_default_wifi_handlers(void)
|
||||||
{
|
{
|
||||||
esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_START, wifi_default_action_sta_start);
|
esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_START, wifi_default_action_sta_start);
|
||||||
esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_STOP, wifi_default_action_sta_stop);
|
esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_STOP, wifi_default_action_sta_stop);
|
||||||
@ -159,42 +148,9 @@ esp_err_t esp_wifi_clear_default_wifi_handlers(void)
|
|||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
static esp_err_t wifi_driver_start(esp_netif_t * esp_netif, void * args)
|
/**
|
||||||
{
|
* @brief Set default handlers
|
||||||
wifi_netif_driver_t driver = args;
|
*/
|
||||||
driver->base.netif = esp_netif;
|
|
||||||
esp_netif_driver_ifconfig_t driver_ifconfig = {
|
|
||||||
.handle = driver,
|
|
||||||
.transmit = wifi_transmit,
|
|
||||||
.driver_free_rx_buffer = wifi_free
|
|
||||||
};
|
|
||||||
|
|
||||||
return esp_netif_set_driver_config(esp_netif, &driver_ifconfig);
|
|
||||||
}
|
|
||||||
|
|
||||||
static esp_err_t disconnect_and_destroy(esp_netif_t* esp_netif)
|
|
||||||
{
|
|
||||||
wifi_netif_driver_t driver = esp_netif_get_io_driver(esp_netif);
|
|
||||||
driver->base.netif = NULL;
|
|
||||||
esp_netif_driver_ifconfig_t driver_ifconfig = { };
|
|
||||||
esp_err_t ret = esp_netif_set_driver_config(esp_netif, &driver_ifconfig);
|
|
||||||
free(driver);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
static esp_err_t create_and_attach(wifi_interface_t wifi_if, esp_netif_t* esp_netif)
|
|
||||||
{
|
|
||||||
wifi_netif_driver_t driver = calloc(1, sizeof(struct wifi_netif_driver));
|
|
||||||
if (driver == NULL) {
|
|
||||||
ESP_LOGE(TAG, "No memory to create a wifi driver");
|
|
||||||
return ESP_ERR_NO_MEM;
|
|
||||||
}
|
|
||||||
driver->wifi_if = wifi_if;
|
|
||||||
driver->base.post_attach = wifi_driver_start;
|
|
||||||
esp_netif_attach(esp_netif, driver);
|
|
||||||
return ESP_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
esp_err_t _esp_wifi_set_default_wifi_handlers(void)
|
esp_err_t _esp_wifi_set_default_wifi_handlers(void)
|
||||||
{
|
{
|
||||||
if (wifi_default_handlers_set) {
|
if (wifi_default_handlers_set) {
|
||||||
@ -244,19 +200,29 @@ esp_err_t _esp_wifi_set_default_wifi_handlers(void)
|
|||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
|
|
||||||
fail:
|
fail:
|
||||||
esp_wifi_clear_default_wifi_handlers();
|
_esp_wifi_clear_default_wifi_handlers();
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t esp_wifi_set_default_wifi_driver_and_handlers(wifi_interface_t wifi_if, void *esp_netif)
|
/**
|
||||||
|
* @brief Set default handlers for station (official API)
|
||||||
|
*/
|
||||||
|
esp_err_t esp_wifi_set_default_wifi_sta_handlers(void)
|
||||||
{
|
{
|
||||||
assert(esp_netif);
|
|
||||||
assert(wifi_if < MAX_WIFI_IFS);
|
|
||||||
s_wifi_netifs[wifi_if] = esp_netif;
|
|
||||||
ESP_ERROR_CHECK(create_and_attach(wifi_if, esp_netif));
|
|
||||||
return _esp_wifi_set_default_wifi_handlers();
|
return _esp_wifi_set_default_wifi_handlers();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Set default handlers for AP (official API)
|
||||||
|
*/
|
||||||
|
esp_err_t esp_wifi_set_default_wifi_ap_handlers(void)
|
||||||
|
{
|
||||||
|
return _esp_wifi_set_default_wifi_handlers();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Clear default handlers and destroy appropriate objects (official API)
|
||||||
|
*/
|
||||||
esp_err_t esp_wifi_clear_default_wifi_driver_and_handlers(void *esp_netif)
|
esp_err_t esp_wifi_clear_default_wifi_driver_and_handlers(void *esp_netif)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
@ -273,25 +239,83 @@ esp_err_t esp_wifi_clear_default_wifi_driver_and_handlers(void *esp_netif)
|
|||||||
|
|
||||||
if (i == MAX_WIFI_IFS) { // if all wifi default netifs are null
|
if (i == MAX_WIFI_IFS) { // if all wifi default netifs are null
|
||||||
ESP_LOGD(TAG, "Clearing wifi default handlers");
|
ESP_LOGD(TAG, "Clearing wifi default handlers");
|
||||||
esp_wifi_clear_default_wifi_handlers();
|
_esp_wifi_clear_default_wifi_handlers();
|
||||||
}
|
}
|
||||||
return disconnect_and_destroy(esp_netif);
|
return disconnect_and_destroy(esp_netif);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//
|
||||||
|
// Object manipulation
|
||||||
|
//
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Create and destroy objects
|
||||||
|
*/
|
||||||
|
static esp_err_t disconnect_and_destroy(esp_netif_t* esp_netif)
|
||||||
|
{
|
||||||
|
wifi_netif_driver_t driver = esp_netif_get_io_driver(esp_netif);
|
||||||
|
esp_netif_driver_ifconfig_t driver_ifconfig = { };
|
||||||
|
esp_err_t ret = esp_netif_set_driver_config(esp_netif, &driver_ifconfig);
|
||||||
|
esp_wifi_destroy_if_driver(driver);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
static esp_err_t create_and_attach(wifi_interface_t wifi_if, esp_netif_t* esp_netif)
|
||||||
|
{
|
||||||
|
wifi_netif_driver_t driver = esp_wifi_create_if_driver(wifi_if);
|
||||||
|
if (driver == NULL) {
|
||||||
|
ESP_LOGE(TAG, "Failed to create wifi interface handle");
|
||||||
|
return ESP_FAIL;
|
||||||
|
}
|
||||||
|
return esp_netif_attach(esp_netif, driver);
|
||||||
|
}
|
||||||
|
|
||||||
|
esp_err_t esp_netif_attach_wifi_station(esp_netif_t *esp_netif)
|
||||||
|
{
|
||||||
|
if (esp_netif == NULL) {
|
||||||
|
return ESP_ERR_INVALID_ARG;
|
||||||
|
}
|
||||||
|
s_wifi_netifs[WIFI_IF_STA] = esp_netif;
|
||||||
|
return create_and_attach(WIFI_IF_STA, esp_netif);
|
||||||
|
}
|
||||||
|
|
||||||
|
esp_err_t esp_netif_attach_wifi_ap(esp_netif_t *esp_netif)
|
||||||
|
{
|
||||||
|
if (esp_netif == NULL) {
|
||||||
|
return ESP_ERR_INVALID_ARG;
|
||||||
|
}
|
||||||
|
s_wifi_netifs[WIFI_IF_AP] = esp_netif;
|
||||||
|
return create_and_attach(WIFI_IF_AP, esp_netif);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//
|
||||||
|
// Default WiFi creation from user code
|
||||||
|
//
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief User init default AP (official API)
|
||||||
|
*/
|
||||||
esp_netif_t* esp_netif_create_default_wifi_ap(void)
|
esp_netif_t* esp_netif_create_default_wifi_ap(void)
|
||||||
{
|
{
|
||||||
esp_netif_config_t cfg = ESP_NETIF_DEFAULT_WIFI_AP();
|
esp_netif_config_t cfg = ESP_NETIF_DEFAULT_WIFI_AP();
|
||||||
esp_netif_t *netif = esp_netif_new(&cfg);
|
esp_netif_t *netif = esp_netif_new(&cfg);
|
||||||
assert(netif);
|
assert(netif);
|
||||||
esp_wifi_set_default_wifi_driver_and_handlers(ESP_IF_WIFI_AP, netif);
|
esp_netif_attach_wifi_ap(netif);
|
||||||
|
esp_wifi_set_default_wifi_ap_handlers();
|
||||||
return netif;
|
return netif;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief User init default station (official API)
|
||||||
|
*/
|
||||||
esp_netif_t* esp_netif_create_default_wifi_sta(void)
|
esp_netif_t* esp_netif_create_default_wifi_sta(void)
|
||||||
{
|
{
|
||||||
esp_netif_config_t cfg = ESP_NETIF_DEFAULT_WIFI_STA();
|
esp_netif_config_t cfg = ESP_NETIF_DEFAULT_WIFI_STA();
|
||||||
esp_netif_t *netif = esp_netif_new(&cfg);
|
esp_netif_t *netif = esp_netif_new(&cfg);
|
||||||
assert(netif);
|
assert(netif);
|
||||||
esp_wifi_set_default_wifi_driver_and_handlers(ESP_IF_WIFI_STA, netif);
|
esp_netif_attach_wifi_station(netif);
|
||||||
|
esp_wifi_set_default_wifi_sta_handlers();
|
||||||
return netif;
|
return netif;
|
||||||
}
|
}
|
146
components/esp_wifi/src/wifi_netif.c
Normal file
146
components/esp_wifi/src/wifi_netif.c
Normal file
@ -0,0 +1,146 @@
|
|||||||
|
// Copyright 2018 Espressif Systems (Shanghai) PTE LTD
|
||||||
|
//
|
||||||
|
// 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
|
||||||
|
//
|
||||||
|
// 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_wifi.h"
|
||||||
|
#include "esp_netif.h"
|
||||||
|
#include "esp_log.h"
|
||||||
|
#include "esp_private/wifi.h"
|
||||||
|
#include "esp_wifi_netif.h"
|
||||||
|
|
||||||
|
//
|
||||||
|
// Purpose of this module is provide object oriented abstraction to wifi interfaces
|
||||||
|
// in order to integrate wifi as esp-netif driver
|
||||||
|
//
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief WiFi netif driver structure
|
||||||
|
*/
|
||||||
|
typedef struct wifi_netif_driver {
|
||||||
|
esp_netif_driver_base_t base;
|
||||||
|
wifi_interface_t wifi_if;
|
||||||
|
}* wifi_netif_driver_t;
|
||||||
|
|
||||||
|
static const char* TAG = "wifi_netif";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Local storage for netif handles and callbacks for specific wifi interfaces
|
||||||
|
*/
|
||||||
|
static esp_netif_receive_t s_wifi_rxcbs[MAX_WIFI_IFS] = { NULL };
|
||||||
|
static esp_netif_t *s_wifi_netifs[MAX_WIFI_IFS] = { NULL };
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief WiFi netif driver IO functions, a thin glue layer
|
||||||
|
* to the original wifi interface API
|
||||||
|
*/
|
||||||
|
static esp_err_t wifi_sta_receive(void *buffer, uint16_t len, void *eb)
|
||||||
|
{
|
||||||
|
return s_wifi_rxcbs[WIFI_IF_STA](s_wifi_netifs[WIFI_IF_STA], buffer, len, eb);
|
||||||
|
}
|
||||||
|
|
||||||
|
static esp_err_t wifi_ap_receive(void *buffer, uint16_t len, void *eb)
|
||||||
|
{
|
||||||
|
return s_wifi_rxcbs[WIFI_IF_AP](s_wifi_netifs[WIFI_IF_AP], buffer, len, eb);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void wifi_free(void *h, void* buffer)
|
||||||
|
{
|
||||||
|
esp_wifi_internal_free_rx_buffer(buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
static esp_err_t wifi_transmit(void *h, void *buffer, size_t len)
|
||||||
|
{
|
||||||
|
wifi_netif_driver_t driver = h;
|
||||||
|
return esp_wifi_internal_tx(driver->wifi_if, buffer, len);
|
||||||
|
}
|
||||||
|
|
||||||
|
static esp_err_t wifi_driver_start(esp_netif_t * esp_netif, void * args)
|
||||||
|
{
|
||||||
|
wifi_netif_driver_t driver = args;
|
||||||
|
driver->base.netif = esp_netif;
|
||||||
|
esp_netif_driver_ifconfig_t driver_ifconfig = {
|
||||||
|
.handle = driver,
|
||||||
|
.transmit = wifi_transmit,
|
||||||
|
.driver_free_rx_buffer = wifi_free
|
||||||
|
};
|
||||||
|
|
||||||
|
return esp_netif_set_driver_config(esp_netif, &driver_ifconfig);
|
||||||
|
}
|
||||||
|
|
||||||
|
void esp_wifi_destroy_if_driver(wifi_netif_driver_t h)
|
||||||
|
{
|
||||||
|
free(h);
|
||||||
|
}
|
||||||
|
|
||||||
|
wifi_netif_driver_t esp_wifi_create_if_driver(wifi_interface_t wifi_if)
|
||||||
|
{
|
||||||
|
wifi_netif_driver_t driver = calloc(1, sizeof(struct wifi_netif_driver));
|
||||||
|
if (driver == NULL) {
|
||||||
|
ESP_LOGE(TAG, "No memory to create a wifi interface handle");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
driver->wifi_if = wifi_if;
|
||||||
|
driver->base.post_attach = wifi_driver_start;
|
||||||
|
return driver;
|
||||||
|
}
|
||||||
|
|
||||||
|
esp_err_t esp_wifi_get_if_mac(wifi_netif_driver_t ifx, uint8_t mac[6])
|
||||||
|
{
|
||||||
|
wifi_interface_t wifi_interface = ifx->wifi_if;
|
||||||
|
|
||||||
|
return esp_wifi_get_mac(wifi_interface, mac);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool esp_wifi_is_if_ready_when_stared(wifi_netif_driver_t ifx)
|
||||||
|
{
|
||||||
|
// WiFi rxcb to be register wifi rxcb on start for AP only, station gets it registered on connect event
|
||||||
|
return (ifx->wifi_if == WIFI_IF_AP);
|
||||||
|
}
|
||||||
|
|
||||||
|
esp_err_t esp_wifi_register_if_rxcb(wifi_netif_driver_t ifx, esp_netif_receive_t fn, void * arg)
|
||||||
|
{
|
||||||
|
if (ifx->base.netif != arg) {
|
||||||
|
ESP_LOGE(TAG, "Invalid argument: supplied netif=%p does not equal to interface netif=%p", arg, ifx->base.netif);
|
||||||
|
return ESP_ERR_INVALID_ARG;
|
||||||
|
}
|
||||||
|
wifi_interface_t wifi_interface = ifx->wifi_if;
|
||||||
|
s_wifi_rxcbs[wifi_interface] = fn;
|
||||||
|
wifi_rxcb_t rxcb = NULL;
|
||||||
|
esp_err_t ret;
|
||||||
|
|
||||||
|
switch (wifi_interface)
|
||||||
|
{
|
||||||
|
case WIFI_IF_STA:
|
||||||
|
rxcb = wifi_sta_receive;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case WIFI_IF_AP:
|
||||||
|
rxcb = wifi_ap_receive;
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (rxcb == NULL) {
|
||||||
|
ESP_LOGE(TAG, "Unknown wifi interface id if=%d", wifi_interface);
|
||||||
|
return ESP_ERR_NOT_SUPPORTED;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((ret = esp_wifi_internal_reg_rxcb(wifi_interface, rxcb)) != ESP_OK) {
|
||||||
|
ESP_LOGE(TAG, "esp_wifi_internal_reg_rxcb for if=%d failed with %d", wifi_interface, ret);
|
||||||
|
return ESP_ERR_INVALID_STATE;
|
||||||
|
}
|
||||||
|
|
||||||
|
s_wifi_netifs[wifi_interface] = ifx->base.netif;
|
||||||
|
return ESP_OK;
|
||||||
|
}
|
@ -26,8 +26,7 @@
|
|||||||
extern void _esp_wifi_set_default_ap_netif(esp_netif_t* esp_netif);
|
extern void _esp_wifi_set_default_ap_netif(esp_netif_t* esp_netif);
|
||||||
extern void _esp_wifi_set_default_sta_netif(esp_netif_t* esp_netif);
|
extern void _esp_wifi_set_default_sta_netif(esp_netif_t* esp_netif);
|
||||||
extern esp_err_t _esp_wifi_set_default_wifi_handlers(void);
|
extern esp_err_t _esp_wifi_set_default_wifi_handlers(void);
|
||||||
extern esp_err_t esp_eth_set_default_handlers(void *esp_netif);
|
extern esp_err_t _esp_wifi_clear_default_wifi_handlers(void);
|
||||||
extern esp_err_t esp_wifi_clear_default_wifi_handlers(void);
|
|
||||||
|
|
||||||
//
|
//
|
||||||
// Purpose of this module is to provide backward compatible version of esp-netif
|
// Purpose of this module is to provide backward compatible version of esp-netif
|
||||||
@ -45,15 +44,14 @@ static const char* s_netif_keyif[TCPIP_ADAPTER_IF_MAX] = {
|
|||||||
|
|
||||||
static bool s_tcpip_adapter_compat = false;
|
static bool s_tcpip_adapter_compat = false;
|
||||||
|
|
||||||
void wifi_start(void *esp_netif, esp_event_base_t base, int32_t event_id, void *data);
|
|
||||||
|
|
||||||
static void wifi_create_and_start_ap(void *esp_netif, esp_event_base_t base, int32_t event_id, void *data)
|
static void wifi_create_and_start_ap(void *esp_netif, esp_event_base_t base, int32_t event_id, void *data)
|
||||||
{
|
{
|
||||||
if (s_esp_netifs[TCPIP_ADAPTER_IF_AP] == NULL) {
|
if (s_esp_netifs[TCPIP_ADAPTER_IF_AP] == NULL) {
|
||||||
esp_netif_config_t cfg = ESP_NETIF_DEFAULT_WIFI_AP();
|
esp_netif_config_t cfg = ESP_NETIF_DEFAULT_WIFI_AP();
|
||||||
esp_netif_t *ap_netif = esp_netif_new(&cfg);
|
esp_netif_t *ap_netif = esp_netif_new(&cfg);
|
||||||
|
|
||||||
esp_wifi_set_default_wifi_driver_and_handlers(ESP_IF_WIFI_AP, ap_netif);
|
esp_netif_attach_wifi_ap(ap_netif);
|
||||||
|
esp_wifi_set_default_wifi_sta_handlers();
|
||||||
s_esp_netifs[TCPIP_ADAPTER_IF_AP] = ap_netif;
|
s_esp_netifs[TCPIP_ADAPTER_IF_AP] = ap_netif;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -64,7 +62,8 @@ static void wifi_create_and_start_sta(void *esp_netif, esp_event_base_t base, in
|
|||||||
esp_netif_config_t cfg = ESP_NETIF_DEFAULT_WIFI_STA();
|
esp_netif_config_t cfg = ESP_NETIF_DEFAULT_WIFI_STA();
|
||||||
esp_netif_t *sta_netif = esp_netif_new(&cfg);
|
esp_netif_t *sta_netif = esp_netif_new(&cfg);
|
||||||
|
|
||||||
esp_wifi_set_default_wifi_driver_and_handlers(ESP_IF_WIFI_STA, sta_netif);
|
esp_netif_attach_wifi_station(sta_netif);
|
||||||
|
esp_wifi_set_default_wifi_sta_handlers();
|
||||||
s_esp_netifs[TCPIP_ADAPTER_IF_STA] = sta_netif;
|
s_esp_netifs[TCPIP_ADAPTER_IF_STA] = sta_netif;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -168,7 +167,7 @@ esp_err_t tcpip_adapter_set_default_wifi_handlers(void)
|
|||||||
|
|
||||||
esp_err_t tcpip_adapter_clear_default_wifi_handlers(void)
|
esp_err_t tcpip_adapter_clear_default_wifi_handlers(void)
|
||||||
{
|
{
|
||||||
return esp_wifi_clear_default_wifi_handlers();
|
return _esp_wifi_clear_default_wifi_handlers();
|
||||||
}
|
}
|
||||||
|
|
||||||
tcpip_adapter_if_t tcpip_adapter_if_from_esp_netif(esp_netif_t *esp_netif)
|
tcpip_adapter_if_t tcpip_adapter_if_from_esp_netif(esp_netif_t *esp_netif)
|
||||||
|
@ -137,7 +137,8 @@ static void start(void)
|
|||||||
|
|
||||||
assert(netif);
|
assert(netif);
|
||||||
|
|
||||||
esp_wifi_set_default_wifi_driver_and_handlers(ESP_IF_WIFI_STA, netif);
|
esp_netif_attach_wifi_station(netif);
|
||||||
|
esp_wifi_set_default_wifi_sta_handlers();
|
||||||
|
|
||||||
s_example_esp_netif = netif;
|
s_example_esp_netif = netif;
|
||||||
|
|
||||||
|
@ -8,7 +8,6 @@
|
|||||||
*/
|
*/
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "esp_wifi.h"
|
#include "esp_wifi.h"
|
||||||
#include "esp_wifi_default.h"
|
|
||||||
#include "esp_system.h"
|
#include "esp_system.h"
|
||||||
#include "esp_event.h"
|
#include "esp_event.h"
|
||||||
#include "esp_log.h"
|
#include "esp_log.h"
|
||||||
@ -388,8 +387,8 @@ static esp_err_t create_wifi_netifs(void)
|
|||||||
};
|
};
|
||||||
esp_netif_t *netif_ap = esp_netif_new(&cfg_ap);
|
esp_netif_t *netif_ap = esp_netif_new(&cfg_ap);
|
||||||
assert(netif_ap);
|
assert(netif_ap);
|
||||||
ESP_ERROR_CHECK(esp_wifi_set_default_wifi_driver_and_handlers(ESP_IF_WIFI_AP, netif_ap));
|
ESP_ERROR_CHECK(esp_netif_attach_wifi_ap(netif_ap));
|
||||||
|
ESP_ERROR_CHECK(esp_wifi_set_default_wifi_sta_handlers());
|
||||||
|
|
||||||
memcpy(&netif_cfg, ESP_NETIF_BASE_DEFAULT_WIFI_STA, sizeof(netif_cfg));
|
memcpy(&netif_cfg, ESP_NETIF_BASE_DEFAULT_WIFI_STA, sizeof(netif_cfg));
|
||||||
netif_cfg.flags &= ~ESP_NETIF_DHCPC;
|
netif_cfg.flags &= ~ESP_NETIF_DHCPC;
|
||||||
@ -400,7 +399,8 @@ static esp_err_t create_wifi_netifs(void)
|
|||||||
|
|
||||||
netif_sta = esp_netif_new(&cfg_sta);
|
netif_sta = esp_netif_new(&cfg_sta);
|
||||||
assert(netif_sta);
|
assert(netif_sta);
|
||||||
ESP_ERROR_CHECK(esp_wifi_set_default_wifi_driver_and_handlers(ESP_IF_WIFI_STA, netif_sta));
|
ESP_ERROR_CHECK(esp_netif_attach_wifi_station(netif_sta));
|
||||||
|
ESP_ERROR_CHECK(esp_wifi_set_default_wifi_sta_handlers());
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -8,7 +8,6 @@
|
|||||||
*/
|
*/
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "esp_wifi.h"
|
#include "esp_wifi.h"
|
||||||
#include "esp_wifi_default.h"
|
|
||||||
#include "esp_system.h"
|
#include "esp_system.h"
|
||||||
#include "esp_event.h"
|
#include "esp_event.h"
|
||||||
#include "esp_log.h"
|
#include "esp_log.h"
|
||||||
@ -312,8 +311,8 @@ static esp_err_t create_wifi_netifs(void)
|
|||||||
};
|
};
|
||||||
esp_netif_t *netif_ap = esp_netif_new(&cfg_ap);
|
esp_netif_t *netif_ap = esp_netif_new(&cfg_ap);
|
||||||
assert(netif_ap);
|
assert(netif_ap);
|
||||||
ESP_ERROR_CHECK(esp_wifi_set_default_wifi_driver_and_handlers(ESP_IF_WIFI_AP, netif_ap));
|
ESP_ERROR_CHECK(esp_netif_attach_wifi_ap(netif_ap));
|
||||||
|
ESP_ERROR_CHECK(esp_wifi_set_default_wifi_sta_handlers());
|
||||||
|
|
||||||
memcpy(&netif_cfg, ESP_NETIF_BASE_DEFAULT_WIFI_STA, sizeof(netif_cfg));
|
memcpy(&netif_cfg, ESP_NETIF_BASE_DEFAULT_WIFI_STA, sizeof(netif_cfg));
|
||||||
netif_cfg.flags &= ~ESP_NETIF_DHCPC;
|
netif_cfg.flags &= ~ESP_NETIF_DHCPC;
|
||||||
@ -324,7 +323,8 @@ static esp_err_t create_wifi_netifs(void)
|
|||||||
|
|
||||||
netif_sta = esp_netif_new(&cfg_sta);
|
netif_sta = esp_netif_new(&cfg_sta);
|
||||||
assert(netif_sta);
|
assert(netif_sta);
|
||||||
ESP_ERROR_CHECK(esp_wifi_set_default_wifi_driver_and_handlers(ESP_IF_WIFI_STA, netif_sta));
|
ESP_ERROR_CHECK(esp_netif_attach_wifi_station(netif_sta));
|
||||||
|
ESP_ERROR_CHECK(esp_wifi_set_default_wifi_sta_handlers());
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user