1 /*
2  * SPDX-FileCopyrightText: 2019-2023 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 struct wifi_netif_driver {
21     esp_netif_driver_base_t base;
22     wifi_interface_t wifi_if;
23 };
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 
49 #ifdef CONFIG_ESP_WIFI_NAN_ENABLE
wifi_nan_receive(void * buffer,uint16_t len,void * eb)50 static esp_err_t wifi_nan_receive(void *buffer, uint16_t len, void *eb)
51 {
52     return s_wifi_rxcbs[WIFI_IF_NAN](s_wifi_netifs[WIFI_IF_NAN], buffer, len, eb);
53 }
54 #endif
55 
wifi_free(void * h,void * buffer)56 static void wifi_free(void *h, void* buffer)
57 {
58     if (buffer) {
59         esp_wifi_internal_free_rx_buffer(buffer);
60     }
61 }
62 
wifi_transmit(void * h,void * buffer,size_t len)63 static esp_err_t wifi_transmit(void *h, void *buffer, size_t len)
64 {
65     wifi_netif_driver_t driver = h;
66     return esp_wifi_internal_tx(driver->wifi_if, buffer, len);
67 }
68 
wifi_transmit_wrap(void * h,void * buffer,size_t len,void * netstack_buf)69 static esp_err_t wifi_transmit_wrap(void *h, void *buffer, size_t len, void *netstack_buf)
70 {
71     wifi_netif_driver_t driver = h;
72 #if CONFIG_SPIRAM
73     return esp_wifi_internal_tx_by_ref(driver->wifi_if, buffer, len, netstack_buf);
74 #else
75     return esp_wifi_internal_tx(driver->wifi_if, buffer, len);
76 #endif
77 }
78 
wifi_driver_start(esp_netif_t * esp_netif,void * args)79 static esp_err_t wifi_driver_start(esp_netif_t * esp_netif, void * args)
80 {
81     wifi_netif_driver_t driver = args;
82     driver->base.netif = esp_netif;
83     esp_netif_driver_ifconfig_t driver_ifconfig = {
84             .handle =  driver,
85             .transmit = wifi_transmit,
86             .transmit_wrap= wifi_transmit_wrap,
87             .driver_free_rx_buffer = wifi_free
88     };
89 
90     return esp_netif_set_driver_config(esp_netif, &driver_ifconfig);
91 }
92 
esp_wifi_destroy_if_driver(wifi_netif_driver_t h)93 void esp_wifi_destroy_if_driver(wifi_netif_driver_t h)
94 {
95     if (h) {
96         esp_wifi_internal_reg_rxcb(h->wifi_if, NULL);  // ignore the potential error
97                                                        // as the wifi might have been already uninitialized
98         s_wifi_netifs[h->wifi_if] = NULL;
99     }
100     free(h);
101 }
102 
esp_wifi_create_if_driver(wifi_interface_t wifi_if)103 wifi_netif_driver_t esp_wifi_create_if_driver(wifi_interface_t wifi_if)
104 {
105     wifi_netif_driver_t driver = calloc(1, sizeof(struct wifi_netif_driver));
106     if (driver == NULL) {
107         ESP_LOGE(TAG, "No memory to create a wifi interface handle");
108         return NULL;
109     }
110     driver->wifi_if = wifi_if;
111     driver->base.post_attach = wifi_driver_start;
112     return driver;
113 }
114 
esp_wifi_get_if_mac(wifi_netif_driver_t ifx,uint8_t mac[6])115 esp_err_t esp_wifi_get_if_mac(wifi_netif_driver_t ifx, uint8_t mac[6])
116 {
117     wifi_interface_t wifi_interface = ifx->wifi_if;
118 
119     return esp_wifi_get_mac(wifi_interface, mac);
120 }
121 
esp_wifi_is_if_ready_when_started(wifi_netif_driver_t ifx)122 bool esp_wifi_is_if_ready_when_started(wifi_netif_driver_t ifx)
123 {
124 #ifdef CONFIG_ESP_WIFI_SOFTAP_SUPPORT
125     // WiFi rxcb to be register wifi rxcb on start for AP only, station gets it registered on connect event
126     return (ifx->wifi_if == WIFI_IF_AP);
127 #else
128     return false;
129 #endif
130 }
131 
esp_wifi_register_if_rxcb(wifi_netif_driver_t ifx,esp_netif_receive_t fn,void * arg)132 esp_err_t esp_wifi_register_if_rxcb(wifi_netif_driver_t ifx, esp_netif_receive_t fn, void * arg)
133 {
134     if (ifx->base.netif != arg) {
135         ESP_LOGE(TAG, "Invalid argument: supplied netif=%p does not equal to interface netif=%p", arg, ifx->base.netif);
136         return ESP_ERR_INVALID_ARG;
137     }
138     wifi_interface_t wifi_interface = ifx->wifi_if;
139     s_wifi_rxcbs[wifi_interface] = fn;
140     wifi_rxcb_t rxcb = NULL;
141     esp_err_t ret;
142 
143     switch (wifi_interface)
144     {
145 
146     case WIFI_IF_STA:
147         rxcb = wifi_sta_receive;
148         break;
149 
150 #ifdef CONFIG_ESP_WIFI_SOFTAP_SUPPORT
151     case WIFI_IF_AP:
152         rxcb = wifi_ap_receive;
153         break;
154 #endif
155 
156 #ifdef CONFIG_ESP_WIFI_NAN_ENABLE
157     case WIFI_IF_NAN:
158         rxcb = wifi_nan_receive;
159         break;
160 #endif
161 
162     default:
163         break;
164     }
165 
166     if (rxcb == NULL) {
167         ESP_LOGE(TAG, "Unknown wifi interface id if=%d", wifi_interface);
168         return ESP_ERR_NOT_SUPPORTED;
169     }
170 
171     /* Interface must be set before registering Wi-Fi RX callback */
172     s_wifi_netifs[wifi_interface] = ifx->base.netif;
173     if ((ret = esp_wifi_internal_reg_rxcb(wifi_interface,  rxcb)) != ESP_OK) {
174         ESP_LOGE(TAG, "esp_wifi_internal_reg_rxcb for if=%d failed with %d", wifi_interface, ret);
175         return ESP_ERR_INVALID_STATE;
176     }
177     return ESP_OK;
178 }
179