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