2021-04-07 03:04:51 -04:00
|
|
|
/*
|
2023-02-13 23:36:40 -05:00
|
|
|
* SPDX-FileCopyrightText: 2019-2023 Espressif Systems (Shanghai) CO LTD
|
2021-04-07 03:04:51 -04:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2019-09-15 13:49:45 -04:00
|
|
|
#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
|
|
|
|
*/
|
2022-05-11 10:01:48 -04:00
|
|
|
struct wifi_netif_driver {
|
2019-09-15 13:49:45 -04:00
|
|
|
esp_netif_driver_base_t base;
|
|
|
|
wifi_interface_t wifi_if;
|
2022-05-11 10:01:48 -04:00
|
|
|
};
|
2019-09-15 13:49:45 -04:00
|
|
|
|
|
|
|
static const char* TAG = "wifi_netif";
|
|
|
|
|
2022-05-11 10:01:48 -04:00
|
|
|
/**
|
|
|
|
* @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);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_ESP_WIFI_SOFTAP_SUPPORT
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-02-13 23:36:40 -05:00
|
|
|
#ifdef CONFIG_ESP_WIFI_NAN_ENABLE
|
|
|
|
static esp_err_t wifi_nan_receive(void *buffer, uint16_t len, void *eb)
|
|
|
|
{
|
|
|
|
return s_wifi_rxcbs[WIFI_IF_NAN](s_wifi_netifs[WIFI_IF_NAN], buffer, len, eb);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-09-15 13:49:45 -04:00
|
|
|
static void wifi_free(void *h, void* buffer)
|
|
|
|
{
|
2022-02-16 02:57:56 -05:00
|
|
|
if (buffer) {
|
|
|
|
esp_wifi_internal_free_rx_buffer(buffer);
|
|
|
|
}
|
2019-09-15 13:49:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-02-13 01:26:48 -05:00
|
|
|
static esp_err_t wifi_transmit_wrap(void *h, void *buffer, size_t len, void *netstack_buf)
|
|
|
|
{
|
|
|
|
wifi_netif_driver_t driver = h;
|
2022-05-10 00:27:36 -04:00
|
|
|
#if CONFIG_SPIRAM
|
2020-02-13 01:26:48 -05:00
|
|
|
return esp_wifi_internal_tx_by_ref(driver->wifi_if, buffer, len, netstack_buf);
|
|
|
|
#else
|
|
|
|
return esp_wifi_internal_tx(driver->wifi_if, buffer, len);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-09-15 13:49:45 -04:00
|
|
|
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,
|
2020-02-13 01:26:48 -05:00
|
|
|
.transmit_wrap= wifi_transmit_wrap,
|
2019-09-15 13:49:45 -04:00
|
|
|
.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)
|
|
|
|
{
|
2021-09-30 07:57:53 -04:00
|
|
|
if (h) {
|
|
|
|
esp_wifi_internal_reg_rxcb(h->wifi_if, NULL); // ignore the potential error
|
|
|
|
// as the wifi might have been already uninitialized
|
2022-05-11 10:01:48 -04:00
|
|
|
s_wifi_netifs[h->wifi_if] = NULL;
|
2021-09-30 07:57:53 -04:00
|
|
|
}
|
2019-09-15 13:49:45 -04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-10-22 03:43:20 -04:00
|
|
|
bool esp_wifi_is_if_ready_when_started(wifi_netif_driver_t ifx)
|
2019-09-15 13:49:45 -04:00
|
|
|
{
|
2021-04-07 03:04:51 -04:00
|
|
|
#ifdef CONFIG_ESP_WIFI_SOFTAP_SUPPORT
|
2019-09-15 13:49:45 -04:00
|
|
|
// 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);
|
2021-04-07 03:04:51 -04:00
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
2019-09-15 13:49:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2022-05-11 10:01:48 -04:00
|
|
|
s_wifi_rxcbs[wifi_interface] = fn;
|
2019-09-15 13:49:45 -04:00
|
|
|
wifi_rxcb_t rxcb = NULL;
|
|
|
|
esp_err_t ret;
|
|
|
|
|
|
|
|
switch (wifi_interface)
|
|
|
|
{
|
2021-04-07 03:04:51 -04:00
|
|
|
|
2019-09-15 13:49:45 -04:00
|
|
|
case WIFI_IF_STA:
|
2022-05-11 10:01:48 -04:00
|
|
|
rxcb = wifi_sta_receive;
|
2019-09-15 13:49:45 -04:00
|
|
|
break;
|
2020-11-10 02:40:01 -05:00
|
|
|
|
2021-04-07 03:04:51 -04:00
|
|
|
#ifdef CONFIG_ESP_WIFI_SOFTAP_SUPPORT
|
2019-09-15 13:49:45 -04:00
|
|
|
case WIFI_IF_AP:
|
2022-05-11 10:01:48 -04:00
|
|
|
rxcb = wifi_ap_receive;
|
2019-09-15 13:49:45 -04:00
|
|
|
break;
|
2021-04-07 03:04:51 -04:00
|
|
|
#endif
|
2019-09-15 13:49:45 -04:00
|
|
|
|
2023-02-13 23:36:40 -05:00
|
|
|
#ifdef CONFIG_ESP_WIFI_NAN_ENABLE
|
|
|
|
case WIFI_IF_NAN:
|
|
|
|
rxcb = wifi_nan_receive;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
2019-09-15 13:49:45 -04:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rxcb == NULL) {
|
|
|
|
ESP_LOGE(TAG, "Unknown wifi interface id if=%d", wifi_interface);
|
|
|
|
return ESP_ERR_NOT_SUPPORTED;
|
|
|
|
}
|
|
|
|
|
2022-07-05 23:33:08 -04:00
|
|
|
/* Interface must be set before registering Wi-Fi RX callback */
|
2022-05-11 10:01:48 -04:00
|
|
|
s_wifi_netifs[wifi_interface] = ifx->base.netif;
|
2019-09-15 13:49:45 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
return ESP_OK;
|
|
|
|
}
|