• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 #include "esp_wifi.h"
15 #include "esp_netif.h"
16 #include "esp_log.h"
17 #include "esp_private/wifi.h"
18 #include "esp_wifi_netif.h"
19 
20 //
21 //  Purpose of this module is provide object oriented abstraction to wifi interfaces
22 //  in order to integrate wifi as esp-netif driver
23 //
24 
25 /**
26  * @brief WiFi netif driver structure
27  */
28 typedef struct wifi_netif_driver {
29     esp_netif_driver_base_t base;
30     wifi_interface_t wifi_if;
31 }* wifi_netif_driver_t;
32 
33 static const char* TAG = "wifi_netif";
34 
35 /**
36  * @brief Local storage for netif handles and callbacks for specific wifi interfaces
37  */
38 static esp_netif_receive_t s_wifi_rxcbs[MAX_WIFI_IFS] = { NULL };
39 static esp_netif_t *s_wifi_netifs[MAX_WIFI_IFS] = { NULL };
40 
41 /**
42  * @brief WiFi netif driver IO functions, a thin glue layer
43  *         to the original wifi interface API
44  */
wifi_sta_receive(void * buffer,uint16_t len,void * eb)45 static esp_err_t wifi_sta_receive(void *buffer, uint16_t len, void *eb)
46 {
47     return s_wifi_rxcbs[WIFI_IF_STA](s_wifi_netifs[WIFI_IF_STA], buffer, len, eb);
48 }
49 
wifi_ap_receive(void * buffer,uint16_t len,void * eb)50 static esp_err_t wifi_ap_receive(void *buffer, uint16_t len, void *eb)
51 {
52     return s_wifi_rxcbs[WIFI_IF_AP](s_wifi_netifs[WIFI_IF_AP], buffer, len, eb);
53 }
54 
wifi_free(void * h,void * buffer)55 static void wifi_free(void *h, void* buffer)
56 {
57     esp_wifi_internal_free_rx_buffer(buffer);
58 }
59 
wifi_transmit(void * h,void * buffer,size_t len)60 static esp_err_t wifi_transmit(void *h, void *buffer, size_t len)
61 {
62     wifi_netif_driver_t driver = h;
63     return esp_wifi_internal_tx(driver->wifi_if, buffer, len);
64 }
65 
wifi_transmit_wrap(void * h,void * buffer,size_t len,void * netstack_buf)66 static esp_err_t wifi_transmit_wrap(void *h, void *buffer, size_t len, void *netstack_buf)
67 {
68     wifi_netif_driver_t driver = h;
69 #if (CONFIG_ESP32_SPIRAM_SUPPORT || CONFIG_ESP32S2_SPIRAM_SUPPORT || CONFIG_ESP32S3_SPIRAM_SUPPORT)
70     return esp_wifi_internal_tx_by_ref(driver->wifi_if, buffer, len, netstack_buf);
71 #else
72     return esp_wifi_internal_tx(driver->wifi_if, buffer, len);
73 #endif
74 }
75 
wifi_driver_start(esp_netif_t * esp_netif,void * args)76 static esp_err_t wifi_driver_start(esp_netif_t * esp_netif, void * args)
77 {
78     wifi_netif_driver_t driver = args;
79     driver->base.netif = esp_netif;
80     esp_netif_driver_ifconfig_t driver_ifconfig = {
81             .handle =  driver,
82             .transmit = wifi_transmit,
83             .transmit_wrap= wifi_transmit_wrap,
84             .driver_free_rx_buffer = wifi_free
85     };
86 
87     return esp_netif_set_driver_config(esp_netif, &driver_ifconfig);
88 }
89 
esp_wifi_destroy_if_driver(wifi_netif_driver_t h)90 void esp_wifi_destroy_if_driver(wifi_netif_driver_t h)
91 {
92     free(h);
93 }
94 
esp_wifi_create_if_driver(wifi_interface_t wifi_if)95 wifi_netif_driver_t esp_wifi_create_if_driver(wifi_interface_t wifi_if)
96 {
97     wifi_netif_driver_t driver = calloc(1, sizeof(struct wifi_netif_driver));
98     if (driver == NULL) {
99         ESP_LOGE(TAG, "No memory to create a wifi interface handle");
100         return NULL;
101     }
102     driver->wifi_if = wifi_if;
103     driver->base.post_attach = wifi_driver_start;
104     return driver;
105 }
106 
esp_wifi_get_if_mac(wifi_netif_driver_t ifx,uint8_t mac[6])107 esp_err_t esp_wifi_get_if_mac(wifi_netif_driver_t ifx, uint8_t mac[6])
108 {
109     wifi_interface_t wifi_interface = ifx->wifi_if;
110 
111     return esp_wifi_get_mac(wifi_interface, mac);
112 }
113 
esp_wifi_is_if_ready_when_started(wifi_netif_driver_t ifx)114 bool esp_wifi_is_if_ready_when_started(wifi_netif_driver_t ifx)
115 {
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 }
119 
esp_wifi_register_if_rxcb(wifi_netif_driver_t ifx,esp_netif_receive_t fn,void * arg)120 esp_err_t esp_wifi_register_if_rxcb(wifi_netif_driver_t ifx, esp_netif_receive_t fn, void * arg)
121 {
122     if (ifx->base.netif != arg) {
123         ESP_LOGE(TAG, "Invalid argument: supplied netif=%p does not equal to interface netif=%p", arg, ifx->base.netif);
124         return ESP_ERR_INVALID_ARG;
125     }
126     wifi_interface_t wifi_interface = ifx->wifi_if;
127     s_wifi_rxcbs[wifi_interface] = fn;
128     wifi_rxcb_t rxcb = NULL;
129     esp_err_t ret;
130 
131     switch (wifi_interface)
132     {
133     case WIFI_IF_STA:
134         rxcb = wifi_sta_receive;
135         break;
136 
137     case WIFI_IF_AP:
138         rxcb = wifi_ap_receive;
139         break;
140 
141     default:
142         break;
143     }
144 
145     if (rxcb == NULL) {
146         ESP_LOGE(TAG, "Unknown wifi interface id if=%d", wifi_interface);
147         return ESP_ERR_NOT_SUPPORTED;
148     }
149 
150     s_wifi_netifs[wifi_interface] = ifx->base.netif;
151     if ((ret = esp_wifi_internal_reg_rxcb(wifi_interface,  rxcb)) != ESP_OK) {
152         ESP_LOGE(TAG, "esp_wifi_internal_reg_rxcb for if=%d failed with %d", wifi_interface, ret);
153         return ESP_ERR_INVALID_STATE;
154     }
155     return ESP_OK;
156 }
157