1 /*
2 * SPDX-FileCopyrightText: 2019-2021 Espressif Systems (Shanghai) CO LTD
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6 #include "esp_wifi.h"
7 #include "esp_netif.h"
8 #include "esp_log.h"
9 #include "esp_private/wifi.h"
10 #include "esp_wifi_netif.h"
11
12 //
13 // Purpose of this module is provide object oriented abstraction to wifi interfaces
14 // in order to integrate wifi as esp-netif driver
15 //
16
17 /**
18 * @brief WiFi netif driver structure
19 */
20 typedef struct wifi_netif_driver {
21 esp_netif_driver_base_t base;
22 wifi_interface_t wifi_if;
23 }* wifi_netif_driver_t;
24
25 static const char* TAG = "wifi_netif";
26
27 /**
28 * @brief Local storage for netif handles and callbacks for specific wifi interfaces
29 */
30 static esp_netif_receive_t s_wifi_rxcbs[MAX_WIFI_IFS] = { NULL };
31 static esp_netif_t *s_wifi_netifs[MAX_WIFI_IFS] = { NULL };
32
33 /**
34 * @brief WiFi netif driver IO functions, a thin glue layer
35 * to the original wifi interface API
36 */
wifi_sta_receive(void * buffer,uint16_t len,void * eb)37 static esp_err_t wifi_sta_receive(void *buffer, uint16_t len, void *eb)
38 {
39 return s_wifi_rxcbs[WIFI_IF_STA](s_wifi_netifs[WIFI_IF_STA], buffer, len, eb);
40 }
41
42 #ifdef CONFIG_ESP_WIFI_SOFTAP_SUPPORT
wifi_ap_receive(void * buffer,uint16_t len,void * eb)43 static esp_err_t wifi_ap_receive(void *buffer, uint16_t len, void *eb)
44 {
45 return s_wifi_rxcbs[WIFI_IF_AP](s_wifi_netifs[WIFI_IF_AP], buffer, len, eb);
46 }
47 #endif
48
wifi_free(void * h,void * buffer)49 static void wifi_free(void *h, void* buffer)
50 {
51 esp_wifi_internal_free_rx_buffer(buffer);
52 }
53
wifi_transmit(void * h,void * buffer,size_t len)54 static esp_err_t wifi_transmit(void *h, void *buffer, size_t len)
55 {
56 wifi_netif_driver_t driver = h;
57 return esp_wifi_internal_tx(driver->wifi_if, buffer, len);
58 }
59
wifi_transmit_wrap(void * h,void * buffer,size_t len,void * netstack_buf)60 static esp_err_t wifi_transmit_wrap(void *h, void *buffer, size_t len, void *netstack_buf)
61 {
62 wifi_netif_driver_t driver = h;
63 #if (CONFIG_ESP32_SPIRAM_SUPPORT || CONFIG_ESP32S2_SPIRAM_SUPPORT || CONFIG_ESP32S3_SPIRAM_SUPPORT)
64 return esp_wifi_internal_tx_by_ref(driver->wifi_if, buffer, len, netstack_buf);
65 #else
66 return esp_wifi_internal_tx(driver->wifi_if, buffer, len);
67 #endif
68 }
69
wifi_driver_start(esp_netif_t * esp_netif,void * args)70 static esp_err_t wifi_driver_start(esp_netif_t * esp_netif, void * args)
71 {
72 wifi_netif_driver_t driver = args;
73 driver->base.netif = esp_netif;
74 esp_netif_driver_ifconfig_t driver_ifconfig = {
75 .handle = driver,
76 .transmit = wifi_transmit,
77 .transmit_wrap= wifi_transmit_wrap,
78 .driver_free_rx_buffer = wifi_free
79 };
80
81 return esp_netif_set_driver_config(esp_netif, &driver_ifconfig);
82 }
83
esp_wifi_destroy_if_driver(wifi_netif_driver_t h)84 void esp_wifi_destroy_if_driver(wifi_netif_driver_t h)
85 {
86 if (h) {
87 esp_wifi_internal_reg_rxcb(h->wifi_if, NULL); // ignore the potential error
88 // as the wifi might have been already uninitialized
89 s_wifi_netifs[h->wifi_if] = NULL;
90 }
91 free(h);
92 }
93
esp_wifi_create_if_driver(wifi_interface_t wifi_if)94 wifi_netif_driver_t esp_wifi_create_if_driver(wifi_interface_t wifi_if)
95 {
96 wifi_netif_driver_t driver = calloc(1, sizeof(struct wifi_netif_driver));
97 if (driver == NULL) {
98 ESP_LOGE(TAG, "No memory to create a wifi interface handle");
99 return NULL;
100 }
101 driver->wifi_if = wifi_if;
102 driver->base.post_attach = wifi_driver_start;
103 return driver;
104 }
105
esp_wifi_get_if_mac(wifi_netif_driver_t ifx,uint8_t mac[6])106 esp_err_t esp_wifi_get_if_mac(wifi_netif_driver_t ifx, uint8_t mac[6])
107 {
108 wifi_interface_t wifi_interface = ifx->wifi_if;
109
110 return esp_wifi_get_mac(wifi_interface, mac);
111 }
112
esp_wifi_is_if_ready_when_started(wifi_netif_driver_t ifx)113 bool esp_wifi_is_if_ready_when_started(wifi_netif_driver_t ifx)
114 {
115 #ifdef CONFIG_ESP_WIFI_SOFTAP_SUPPORT
116 // WiFi rxcb to be register wifi rxcb on start for AP only, station gets it registered on connect event
117 return (ifx->wifi_if == WIFI_IF_AP);
118 #else
119 return false;
120 #endif
121 }
122
esp_wifi_register_if_rxcb(wifi_netif_driver_t ifx,esp_netif_receive_t fn,void * arg)123 esp_err_t esp_wifi_register_if_rxcb(wifi_netif_driver_t ifx, esp_netif_receive_t fn, void * arg)
124 {
125 if (ifx->base.netif != arg) {
126 ESP_LOGE(TAG, "Invalid argument: supplied netif=%p does not equal to interface netif=%p", arg, ifx->base.netif);
127 return ESP_ERR_INVALID_ARG;
128 }
129 wifi_interface_t wifi_interface = ifx->wifi_if;
130 s_wifi_rxcbs[wifi_interface] = fn;
131 wifi_rxcb_t rxcb = NULL;
132 esp_err_t ret;
133
134 switch (wifi_interface)
135 {
136
137 case WIFI_IF_STA:
138 rxcb = wifi_sta_receive;
139 break;
140
141 #ifdef CONFIG_ESP_WIFI_SOFTAP_SUPPORT
142 case WIFI_IF_AP:
143 rxcb = wifi_ap_receive;
144 break;
145 #endif
146
147 default:
148 break;
149 }
150
151 if (rxcb == NULL) {
152 ESP_LOGE(TAG, "Unknown wifi interface id if=%d", wifi_interface);
153 return ESP_ERR_NOT_SUPPORTED;
154 }
155
156 s_wifi_netifs[wifi_interface] = ifx->base.netif;
157 if ((ret = esp_wifi_internal_reg_rxcb(wifi_interface, rxcb)) != ESP_OK) {
158 ESP_LOGE(TAG, "esp_wifi_internal_reg_rxcb for if=%d failed with %d", wifi_interface, ret);
159 return ESP_ERR_INVALID_STATE;
160 }
161 return ESP_OK;
162 }
163