1 /*
2 * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include "esp_netif.h"
8 #include "esp_private/wifi.h"
9 #include "esp_log.h"
10 #include "esp_netif_net_stack.h"
11
12 #if CONFIG_ESP_NETIF_TCPIP_ADAPTER_COMPATIBLE_LAYER
13
14 #if CONFIG_ETH_ENABLED
15 #include "esp_eth.h"
16 #endif
17 #include "tcpip_adapter_types.h"
18 #include "esp_wifi_default.h"
19
20 //
21 // Accessing some internal default interfaces and esp-netifs
22 //
23 extern void _esp_wifi_set_default_ap_netif(esp_netif_t* esp_netif);
24 extern void _esp_wifi_set_default_sta_netif(esp_netif_t* esp_netif);
25 extern esp_err_t _esp_wifi_set_default_wifi_handlers(void);
26 extern esp_err_t _esp_wifi_clear_default_wifi_handlers(void);
27 extern esp_err_t esp_netif_up(esp_netif_t *esp_netif);
28 extern esp_err_t esp_netif_down(esp_netif_t *esp_netif);
29
30 //
31 // Purpose of this module is to provide backward compatible version of esp-netif
32 // with legacy tcpip_adapter interface
33 //
34
35 static const char* TAG = "tcpip_adapter_compat";
36
37 static esp_netif_t *s_esp_netifs[TCPIP_ADAPTER_IF_MAX] = { NULL };
38 static const char* s_netif_keyif[TCPIP_ADAPTER_IF_MAX] = {
39 "WIFI_STA_DEF",
40 "WIFI_AP_DEF",
41 "ETH_DEF",
42 };
43
44 static bool s_tcpip_adapter_compat = false;
45
46 #ifdef CONFIG_ESP_WIFI_SOFTAP_SUPPORT
wifi_create_and_start_ap(void * esp_netif,esp_event_base_t base,int32_t event_id,void * data)47 static void wifi_create_and_start_ap(void *esp_netif, esp_event_base_t base, int32_t event_id, void *data)
48 {
49 if (s_esp_netifs[TCPIP_ADAPTER_IF_AP] == NULL) {
50 esp_netif_config_t cfg = ESP_NETIF_DEFAULT_WIFI_AP();
51 esp_netif_t *ap_netif = esp_netif_new(&cfg);
52
53 esp_netif_attach_wifi_ap(ap_netif);
54 esp_wifi_set_default_wifi_ap_handlers();
55 s_esp_netifs[TCPIP_ADAPTER_IF_AP] = ap_netif;
56 }
57 }
58 #endif
59
wifi_create_and_start_sta(void * esp_netif,esp_event_base_t base,int32_t event_id,void * data)60 static void wifi_create_and_start_sta(void *esp_netif, esp_event_base_t base, int32_t event_id, void *data)
61 {
62 if (s_esp_netifs[TCPIP_ADAPTER_IF_STA] == NULL) {
63 esp_netif_config_t cfg = ESP_NETIF_DEFAULT_WIFI_STA();
64 esp_netif_t *sta_netif = esp_netif_new(&cfg);
65
66 esp_netif_attach_wifi_station(sta_netif);
67 esp_wifi_set_default_wifi_sta_handlers();
68 s_esp_netifs[TCPIP_ADAPTER_IF_STA] = sta_netif;
69 }
70 }
71
netif_from_if(tcpip_adapter_if_t interface)72 static inline esp_netif_t * netif_from_if(tcpip_adapter_if_t interface)
73 {
74 if (interface < TCPIP_ADAPTER_IF_MAX) {
75 if (s_esp_netifs[interface] == NULL) {
76 s_esp_netifs[interface] = esp_netif_get_handle_from_ifkey(s_netif_keyif[interface]);
77 if (s_esp_netifs[interface] == NULL && s_tcpip_adapter_compat) {
78 // if not found in compat mode -> create it
79 if (interface == TCPIP_ADAPTER_IF_STA) {
80 wifi_create_and_start_sta(NULL, 0, 0, NULL);
81 s_esp_netifs[interface] = esp_netif_get_handle_from_ifkey("WIFI_STA_DEF");
82 }
83 #ifdef CONFIG_ESP_WIFI_SOFTAP_SUPPORT
84 else if (interface == TCPIP_ADAPTER_IF_AP) {
85 wifi_create_and_start_ap(NULL, 0, 0, NULL);
86 s_esp_netifs[interface] = esp_netif_get_handle_from_ifkey("WIFI_AP_DEF");
87 }
88 #endif
89 }
90 }
91 return s_esp_netifs[interface];
92 }
93 return NULL;
94 }
95
tcpip_adapter_init(void)96 void tcpip_adapter_init(void)
97 {
98 s_tcpip_adapter_compat = true;
99 esp_err_t err;
100 if (ESP_OK != (err = esp_netif_init())) {
101 ESP_LOGE(TAG, "ESP-NETIF initialization failed with %d in tcpip_adapter compatibility mode", err);
102 }
103 }
104
105 #if CONFIG_ETH_ENABLED
106
tcpip_adapter_clear_default_eth_handlers(void)107 esp_err_t tcpip_adapter_clear_default_eth_handlers(void)
108 {
109 return ESP_OK;
110 }
111
tcpip_adapter_set_default_eth_handlers(void)112 esp_err_t tcpip_adapter_set_default_eth_handlers(void)
113 {
114 if (s_tcpip_adapter_compat) {
115 esp_netif_config_t cfg = ESP_NETIF_DEFAULT_ETH();
116 esp_netif_t *eth_netif = esp_netif_new(&cfg);
117
118 s_esp_netifs[TCPIP_ADAPTER_IF_ETH] = eth_netif;
119 }
120 return ESP_OK;
121
122 }
123
tcpip_adapter_compat_start_eth(void * eth_driver)124 esp_err_t tcpip_adapter_compat_start_eth(void *eth_driver)
125 {
126 if (s_tcpip_adapter_compat) {
127 esp_netif_t *esp_netif = netif_from_if(TCPIP_ADAPTER_IF_ETH);
128 if (esp_netif) {
129 esp_netif_attach(esp_netif, esp_eth_new_netif_glue(eth_driver));
130 }
131 }
132 return ESP_OK;
133 }
134
135 #endif
136
tcpip_adapter_eth_input(void * buffer,uint16_t len,void * eb)137 esp_err_t tcpip_adapter_eth_input(void *buffer, uint16_t len, void *eb)
138 {
139 return esp_netif_receive(netif_from_if(TCPIP_ADAPTER_IF_ETH), buffer, len, eb);
140 }
141
tcpip_adapter_sta_input(void * buffer,uint16_t len,void * eb)142 esp_err_t tcpip_adapter_sta_input(void *buffer, uint16_t len, void *eb)
143 {
144 return esp_netif_receive(netif_from_if(TCPIP_ADAPTER_IF_STA), buffer, len, eb);
145 }
146
147 #ifdef CONFIG_ESP_WIFI_SOFTAP_SUPPORT
tcpip_adapter_ap_input(void * buffer,uint16_t len,void * eb)148 esp_err_t tcpip_adapter_ap_input(void *buffer, uint16_t len, void *eb)
149 {
150 return esp_netif_receive(netif_from_if(TCPIP_ADAPTER_IF_AP), buffer, len, eb);
151 }
152 #endif
153
tcpip_adapter_set_default_wifi_handlers(void)154 esp_err_t tcpip_adapter_set_default_wifi_handlers(void)
155 {
156 #if CONFIG_ESP_NETIF_TCPIP_ADAPTER_COMPATIBLE_LAYER
157 if (s_tcpip_adapter_compat) {
158 // create instances and register default handlers only on start event
159 esp_err_t err = esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_START, wifi_create_and_start_sta, NULL);
160 if (err != ESP_OK) {
161 return err;
162 }
163 #ifdef CONFIG_ESP_WIFI_SOFTAP_SUPPORT
164 err = esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_AP_START, wifi_create_and_start_ap, NULL);
165 if (err != ESP_OK) {
166 return err;
167 }
168 #endif
169 _esp_wifi_set_default_wifi_handlers();
170 }
171 return ESP_OK;
172 #else
173 ESP_LOGE(TAG, "%s: tcpip adapter compatibility layer is disabled", __func__);
174 return ESP_ERR_INVALID_STATE;
175 #endif
176 }
177
tcpip_adapter_clear_default_wifi_handlers(void)178 esp_err_t tcpip_adapter_clear_default_wifi_handlers(void)
179 {
180 if (s_tcpip_adapter_compat) {
181 // Clear default handlers only if tcpip-adapter mode used
182 return _esp_wifi_clear_default_wifi_handlers();
183 }
184 // No action if tcpip-adapter compatibility enabled, but interfaces created/configured with esp-netif
185 return ESP_OK;
186 }
187
tcpip_adapter_if_from_esp_netif(esp_netif_t * esp_netif)188 tcpip_adapter_if_t tcpip_adapter_if_from_esp_netif(esp_netif_t *esp_netif)
189 {
190 for (int i=0; i<TCPIP_ADAPTER_IF_MAX; ++i) {
191 if (esp_netif == s_esp_netifs[i])
192 return i;
193 }
194 return TCPIP_ADAPTER_IF_MAX;
195 }
196
tcpip_adapter_get_ip_info(tcpip_adapter_if_t tcpip_if,tcpip_adapter_ip_info_t * ip_info)197 esp_err_t tcpip_adapter_get_ip_info(tcpip_adapter_if_t tcpip_if, tcpip_adapter_ip_info_t *ip_info)
198 {
199 return esp_netif_get_ip_info(netif_from_if(tcpip_if), (esp_netif_ip_info_t *)ip_info);
200 }
201
202 #if CONFIG_LWIP_IPV6
tcpip_adapter_get_ip6_linklocal(tcpip_adapter_if_t tcpip_if,ip6_addr_t * if_ip6)203 esp_err_t tcpip_adapter_get_ip6_linklocal(tcpip_adapter_if_t tcpip_if, ip6_addr_t *if_ip6)
204 {
205 return esp_netif_get_ip6_linklocal(netif_from_if(tcpip_if), (esp_ip6_addr_t*)if_ip6);
206 }
207
tcpip_adapter_get_ip6_global(tcpip_adapter_if_t tcpip_if,ip6_addr_t * if_ip6)208 esp_err_t tcpip_adapter_get_ip6_global(tcpip_adapter_if_t tcpip_if, ip6_addr_t *if_ip6)
209 {
210 return esp_netif_get_ip6_global(netif_from_if(tcpip_if), (esp_ip6_addr_t*)if_ip6);
211 }
212 #endif
213
tcpip_adapter_dhcpc_get_status(tcpip_adapter_if_t tcpip_if,tcpip_adapter_dhcp_status_t * status)214 esp_err_t tcpip_adapter_dhcpc_get_status(tcpip_adapter_if_t tcpip_if, tcpip_adapter_dhcp_status_t *status)
215 {
216 return esp_netif_dhcpc_get_status(netif_from_if(tcpip_if), status);
217 }
218
tcpip_adapter_is_netif_up(tcpip_adapter_if_t tcpip_if)219 bool tcpip_adapter_is_netif_up(tcpip_adapter_if_t tcpip_if)
220 {
221 return esp_netif_is_netif_up(netif_from_if(tcpip_if));
222 }
223
tcpip_adapter_get_netif(tcpip_adapter_if_t tcpip_if,void ** netif)224 esp_err_t tcpip_adapter_get_netif(tcpip_adapter_if_t tcpip_if, void ** netif)
225 {
226 esp_netif_t *esp_netif = netif_from_if(tcpip_if);
227 if (esp_netif) {
228 void* net_stack_netif = esp_netif_get_netif_impl(esp_netif);
229 *netif = net_stack_netif;
230 return ESP_OK;
231 }
232 return ESP_ERR_INVALID_ARG;
233 }
234 #if CONFIG_LWIP_IPV6
tcpip_adapter_create_ip6_linklocal(tcpip_adapter_if_t tcpip_if)235 esp_err_t tcpip_adapter_create_ip6_linklocal(tcpip_adapter_if_t tcpip_if)
236 {
237 return esp_netif_create_ip6_linklocal(netif_from_if(tcpip_if));
238 }
239 #endif
tcpip_adapter_dhcps_stop(tcpip_adapter_if_t tcpip_if)240 esp_err_t tcpip_adapter_dhcps_stop(tcpip_adapter_if_t tcpip_if)
241 {
242 return esp_netif_dhcps_stop(netif_from_if(tcpip_if));
243 }
244
tcpip_adapter_dhcpc_stop(tcpip_adapter_if_t tcpip_if)245 esp_err_t tcpip_adapter_dhcpc_stop(tcpip_adapter_if_t tcpip_if)
246 {
247 return esp_netif_dhcpc_stop(netif_from_if(tcpip_if));
248 }
249
tcpip_adapter_dhcps_start(tcpip_adapter_if_t tcpip_if)250 esp_err_t tcpip_adapter_dhcps_start(tcpip_adapter_if_t tcpip_if)
251 {
252 return esp_netif_dhcps_start(netif_from_if(tcpip_if));
253 }
254
tcpip_adapter_dhcpc_start(tcpip_adapter_if_t tcpip_if)255 esp_err_t tcpip_adapter_dhcpc_start(tcpip_adapter_if_t tcpip_if)
256 {
257 return esp_netif_dhcpc_start(netif_from_if(tcpip_if));
258 }
tcpip_adapter_dhcps_get_status(tcpip_adapter_if_t tcpip_if,tcpip_adapter_dhcp_status_t * status)259 esp_err_t tcpip_adapter_dhcps_get_status(tcpip_adapter_if_t tcpip_if, tcpip_adapter_dhcp_status_t *status)
260 {
261 return esp_netif_dhcps_get_status(netif_from_if(tcpip_if), status);
262 }
263
tcpip_adapter_dhcps_option(tcpip_adapter_dhcp_option_mode_t opt_op,tcpip_adapter_dhcp_option_id_t opt_id,void * opt_val,uint32_t opt_len)264 esp_err_t tcpip_adapter_dhcps_option(tcpip_adapter_dhcp_option_mode_t opt_op, tcpip_adapter_dhcp_option_id_t opt_id, void *opt_val, uint32_t opt_len)
265 {
266 // Note: legacy mode supports dhcps only for default wifi AP
267 return esp_netif_dhcps_option(esp_netif_get_handle_from_ifkey("WIFI_AP_DEF"), opt_op, opt_id, opt_val, opt_len);
268 }
269
tcpip_adapter_dhcpc_option(tcpip_adapter_dhcp_option_mode_t opt_op,tcpip_adapter_dhcp_option_id_t opt_id,void * opt_val,uint32_t opt_len)270 esp_err_t tcpip_adapter_dhcpc_option(tcpip_adapter_dhcp_option_mode_t opt_op, tcpip_adapter_dhcp_option_id_t opt_id, void *opt_val, uint32_t opt_len)
271 {
272 return esp_netif_dhcpc_option(netif_from_if(TCPIP_ADAPTER_IF_STA), opt_op, opt_id, opt_val, opt_len);
273 }
274
tcpip_adapter_set_ip_info(tcpip_adapter_if_t tcpip_if,const tcpip_adapter_ip_info_t * ip_info)275 esp_err_t tcpip_adapter_set_ip_info(tcpip_adapter_if_t tcpip_if, const tcpip_adapter_ip_info_t *ip_info)
276 {
277 return esp_netif_set_ip_info(netif_from_if(tcpip_if), (esp_netif_ip_info_t *)ip_info);
278 }
279
tcpip_adapter_get_dns_info(tcpip_adapter_if_t tcpip_if,tcpip_adapter_dns_type_t type,tcpip_adapter_dns_info_t * dns)280 esp_err_t tcpip_adapter_get_dns_info(tcpip_adapter_if_t tcpip_if, tcpip_adapter_dns_type_t type, tcpip_adapter_dns_info_t *dns)
281 {
282 return esp_netif_get_dns_info(netif_from_if(tcpip_if), type, dns);
283 }
284
tcpip_adapter_set_dns_info(tcpip_adapter_if_t tcpip_if,tcpip_adapter_dns_type_t type,tcpip_adapter_dns_info_t * dns)285 esp_err_t tcpip_adapter_set_dns_info(tcpip_adapter_if_t tcpip_if, tcpip_adapter_dns_type_t type, tcpip_adapter_dns_info_t *dns)
286 {
287 return esp_netif_set_dns_info(netif_from_if(tcpip_if), type, dns);
288 }
289
tcpip_adapter_get_netif_index(tcpip_adapter_if_t tcpip_if)290 int tcpip_adapter_get_netif_index(tcpip_adapter_if_t tcpip_if)
291 {
292 return esp_netif_get_netif_impl_index(netif_from_if(tcpip_if));
293 }
294
tcpip_adapter_get_sta_list(const wifi_sta_list_t * wifi_sta_list,tcpip_adapter_sta_list_t * tcpip_sta_list)295 esp_err_t tcpip_adapter_get_sta_list(const wifi_sta_list_t *wifi_sta_list, tcpip_adapter_sta_list_t *tcpip_sta_list)
296 {
297 return esp_netif_get_sta_list(wifi_sta_list, tcpip_sta_list);
298 }
299
tcpip_adapter_compat_start_netif(esp_netif_t * netif,uint8_t * mac,tcpip_adapter_ip_info_t * ip_info)300 static esp_err_t tcpip_adapter_compat_start_netif(esp_netif_t *netif, uint8_t *mac, tcpip_adapter_ip_info_t *ip_info)
301 {
302 if (netif == NULL || mac == NULL || ip_info == NULL) {
303 return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS;
304 }
305 esp_netif_set_mac(netif, mac);
306 esp_netif_set_ip_info(netif, (esp_netif_ip_info_t *)ip_info);
307 esp_netif_action_start(netif, NULL, 0, NULL);
308 return ESP_OK;
309 }
310
tcpip_adapter_eth_start(uint8_t * mac,tcpip_adapter_ip_info_t * ip_info,void * args)311 esp_err_t tcpip_adapter_eth_start(uint8_t *mac, tcpip_adapter_ip_info_t *ip_info, void *args)
312 {
313 return tcpip_adapter_compat_start_netif(netif_from_if(TCPIP_ADAPTER_IF_ETH),
314 mac, ip_info);
315 }
316
tcpip_adapter_sta_start(uint8_t * mac,tcpip_adapter_ip_info_t * ip_info)317 esp_err_t tcpip_adapter_sta_start(uint8_t *mac, tcpip_adapter_ip_info_t *ip_info)
318 {
319 return tcpip_adapter_compat_start_netif(netif_from_if(TCPIP_ADAPTER_IF_STA),
320 mac, ip_info);
321 }
322
323 #ifdef CONFIG_ESP_WIFI_SOFTAP_SUPPORT
tcpip_adapter_ap_start(uint8_t * mac,tcpip_adapter_ip_info_t * ip_info)324 esp_err_t tcpip_adapter_ap_start(uint8_t *mac, tcpip_adapter_ip_info_t *ip_info)
325 {
326 return tcpip_adapter_compat_start_netif(netif_from_if(TCPIP_ADAPTER_IF_AP),
327 mac, ip_info);
328 }
329 #endif
330
tcpip_adapter_stop(tcpip_adapter_if_t tcpip_if)331 esp_err_t tcpip_adapter_stop(tcpip_adapter_if_t tcpip_if)
332 {
333 esp_netif_t *netif = netif_from_if(tcpip_if);
334 if (netif == NULL) {
335 return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS;
336 }
337 esp_netif_action_stop(netif_from_if(tcpip_if), NULL, 0, NULL);
338 return ESP_OK;
339 }
340
tcpip_adapter_up(tcpip_adapter_if_t tcpip_if)341 esp_err_t tcpip_adapter_up(tcpip_adapter_if_t tcpip_if)
342 {
343 return esp_netif_up(netif_from_if(tcpip_if));
344 }
345
tcpip_adapter_down(tcpip_adapter_if_t tcpip_if)346 esp_err_t tcpip_adapter_down(tcpip_adapter_if_t tcpip_if)
347 {
348 return esp_netif_down(netif_from_if(tcpip_if));
349 }
350
tcpip_adapter_get_old_ip_info(tcpip_adapter_if_t tcpip_if,tcpip_adapter_ip_info_t * ip_info)351 esp_err_t tcpip_adapter_get_old_ip_info(tcpip_adapter_if_t tcpip_if, tcpip_adapter_ip_info_t *ip_info)
352 {
353 return esp_netif_get_old_ip_info(netif_from_if(tcpip_if), (esp_netif_ip_info_t *)ip_info);
354 }
355
tcpip_adapter_set_old_ip_info(tcpip_adapter_if_t tcpip_if,const tcpip_adapter_ip_info_t * ip_info)356 esp_err_t tcpip_adapter_set_old_ip_info(tcpip_adapter_if_t tcpip_if, const tcpip_adapter_ip_info_t *ip_info)
357 {
358 return esp_netif_set_old_ip_info(netif_from_if(tcpip_if), (esp_netif_ip_info_t *)ip_info);
359 }
360
tcpip_adapter_get_esp_if(void * dev)361 esp_interface_t tcpip_adapter_get_esp_if(void *dev)
362 {
363 esp_netif_t *netif = esp_netif_get_handle_from_netif_impl(dev);
364 for (int i=0; i< TCPIP_ADAPTER_IF_MAX; ++i) {
365 if (s_esp_netifs[i] == netif) {
366 return i;
367 }
368 }
369 return ESP_IF_MAX;
370 }
371
tcpip_adapter_set_hostname(tcpip_adapter_if_t tcpip_if,const char * hostname)372 esp_err_t tcpip_adapter_set_hostname(tcpip_adapter_if_t tcpip_if, const char *hostname)
373 {
374 return esp_netif_set_hostname(netif_from_if(tcpip_if), hostname);
375 }
376
tcpip_adapter_get_hostname(tcpip_adapter_if_t tcpip_if,const char ** hostname)377 esp_err_t tcpip_adapter_get_hostname(tcpip_adapter_if_t tcpip_if, const char **hostname)
378 {
379 return esp_netif_get_hostname(netif_from_if(tcpip_if), hostname);
380 }
381
382 #else
383
tcpip_adapter_compat_start_eth(void * eth_driver)384 esp_err_t tcpip_adapter_compat_start_eth(void* eth_driver)
385 {
386 return ESP_OK;
387 }
388
389 #endif
390