1 /*
2  * SPDX-FileCopyrightText: 2019-2021 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #include "sdkconfig.h"
7 #include "esp_wifi.h"
8 #include "esp_netif.h"
9 #include "esp_log.h"
10 #include "esp_private/wifi.h"
11 #include "esp_wifi_netif.h"
12 #include <string.h>
13 
14 //
15 //  Purpose of this module is to provide basic wifi initialization setup for
16 //  default station and AP and to register default handles for these interfaces
17 //
18 static const char* TAG = "wifi_init_default";
19 
20 static esp_netif_t *s_wifi_netifs[MAX_WIFI_IFS] = { NULL };
21 static bool wifi_default_handlers_set = false;
22 
23 static esp_err_t disconnect_and_destroy(esp_netif_t* esp_netif);
24 
25 //
26 // Default event handlers
27 //
28 
29 /**
30  * @brief Wifi start action when station or AP get started
31  */
wifi_start(void * esp_netif,esp_event_base_t base,int32_t event_id,void * data)32 static void wifi_start(void *esp_netif, esp_event_base_t base, int32_t event_id, void *data)
33 {
34     uint8_t mac[6];
35     esp_err_t ret;
36 
37     ESP_LOGD(TAG, "%s esp-netif:%p event-id%d", __func__, esp_netif, event_id);
38 
39     wifi_netif_driver_t driver = esp_netif_get_io_driver(esp_netif);
40 
41     if ((ret = esp_wifi_get_if_mac(driver, mac)) != ESP_OK) {
42         ESP_LOGE(TAG, "esp_wifi_get_mac failed with %d", ret);
43         return;
44     }
45     ESP_LOGD(TAG, "WIFI mac address: %x %x %x %x %x %x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
46 
47     if (esp_wifi_is_if_ready_when_started(driver)) {
48         if ((ret = esp_wifi_register_if_rxcb(driver,  esp_netif_receive, esp_netif)) != ESP_OK) {
49             ESP_LOGE(TAG, "esp_wifi_register_if_rxcb for if=%p failed with %d", driver, ret);
50             return;
51         }
52     }
53 
54     if ((ret = esp_wifi_internal_reg_netstack_buf_cb(esp_netif_netstack_buf_ref, esp_netif_netstack_buf_free)) != ESP_OK) {
55         ESP_LOGE(TAG, "netstack cb reg failed with %d", ret);
56         return;
57     }
58     esp_netif_set_mac(esp_netif, mac);
59     esp_netif_action_start(esp_netif, base, event_id, data);
60 }
61 
62 /**
63  * @brief Wifi default handlers for specific events for station and APs
64  */
65 
wifi_default_action_sta_start(void * arg,esp_event_base_t base,int32_t event_id,void * data)66 static void wifi_default_action_sta_start(void *arg, esp_event_base_t base, int32_t event_id, void *data)
67 {
68     if (s_wifi_netifs[WIFI_IF_STA] != NULL) {
69         wifi_start(s_wifi_netifs[WIFI_IF_STA], base, event_id, data);
70     }
71 }
72 
wifi_default_action_sta_stop(void * arg,esp_event_base_t base,int32_t event_id,void * data)73 static void wifi_default_action_sta_stop(void *arg, esp_event_base_t base, int32_t event_id, void *data)
74 {
75     if (s_wifi_netifs[WIFI_IF_STA] != NULL) {
76         esp_netif_action_stop(s_wifi_netifs[WIFI_IF_STA], base, event_id, data);
77     }
78 }
79 
wifi_default_action_sta_connected(void * arg,esp_event_base_t base,int32_t event_id,void * data)80 static void wifi_default_action_sta_connected(void *arg, esp_event_base_t base, int32_t event_id, void *data)
81 {
82     if (s_wifi_netifs[WIFI_IF_STA] != NULL) {
83         esp_err_t ret;
84         esp_netif_t *esp_netif = s_wifi_netifs[WIFI_IF_STA];
85         wifi_netif_driver_t driver = esp_netif_get_io_driver(esp_netif);
86 
87         if (!esp_wifi_is_if_ready_when_started(driver)) {
88             // if interface not ready when started, rxcb to be registered on connection
89             if ((ret = esp_wifi_register_if_rxcb(driver,  esp_netif_receive, esp_netif)) != ESP_OK) {
90                 ESP_LOGE(TAG, "esp_wifi_register_if_rxcb for if=%p failed with %d", driver, ret);
91                 return;
92             }
93         }
94 
95         esp_netif_action_connected(s_wifi_netifs[WIFI_IF_STA], base, event_id, data);
96     }
97 }
98 
wifi_default_action_sta_disconnected(void * arg,esp_event_base_t base,int32_t event_id,void * data)99 static void wifi_default_action_sta_disconnected(void *arg, esp_event_base_t base, int32_t event_id, void *data)
100 {
101     if (s_wifi_netifs[WIFI_IF_STA] != NULL) {
102         esp_netif_action_disconnected(s_wifi_netifs[WIFI_IF_STA], base, event_id, data);
103     }
104 }
105 
106 #ifdef CONFIG_ESP_WIFI_SOFTAP_SUPPORT
wifi_default_action_ap_start(void * arg,esp_event_base_t base,int32_t event_id,void * data)107 static void wifi_default_action_ap_start(void *arg, esp_event_base_t base, int32_t event_id, void *data)
108 {
109     if (s_wifi_netifs[WIFI_IF_AP] != NULL) {
110         wifi_start(s_wifi_netifs[WIFI_IF_AP], base, event_id, data);
111     }
112 }
113 
wifi_default_action_ap_stop(void * arg,esp_event_base_t base,int32_t event_id,void * data)114 static void wifi_default_action_ap_stop(void *arg, esp_event_base_t base, int32_t event_id, void *data)
115 {
116     if (s_wifi_netifs[WIFI_IF_AP] != NULL) {
117         esp_netif_action_stop(s_wifi_netifs[WIFI_IF_AP], base, event_id, data);
118     }
119 }
120 #endif
121 
wifi_default_action_sta_got_ip(void * arg,esp_event_base_t base,int32_t event_id,void * data)122 static void wifi_default_action_sta_got_ip(void *arg, esp_event_base_t base, int32_t event_id, void *data)
123 {
124     if (s_wifi_netifs[WIFI_IF_STA] != NULL) {
125         ESP_LOGD(TAG, "Got IP wifi default handler entered");
126         int ret = esp_wifi_internal_set_sta_ip();
127         if (ret != ESP_OK) {
128             ESP_LOGI(TAG, "esp_wifi_internal_set_sta_ip failed with %d", ret);
129         }
130         esp_netif_action_got_ip(s_wifi_netifs[WIFI_IF_STA], base, event_id, data);
131     }
132 }
133 
134 /**
135  * @brief Clear default handlers
136  */
_esp_wifi_clear_default_wifi_handlers(void)137 esp_err_t _esp_wifi_clear_default_wifi_handlers(void)
138 {
139     esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_START, wifi_default_action_sta_start);
140     esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_STOP, wifi_default_action_sta_stop);
141     esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, wifi_default_action_sta_connected);
142     esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, wifi_default_action_sta_disconnected);
143 #ifdef CONFIG_ESP_WIFI_SOFTAP_SUPPORT
144     esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_AP_START, wifi_default_action_ap_start);
145     esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_AP_STOP, wifi_default_action_ap_stop);
146 #endif
147     esp_event_handler_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, wifi_default_action_sta_got_ip);
148     esp_unregister_shutdown_handler((shutdown_handler_t)esp_wifi_stop);
149     wifi_default_handlers_set = false;
150     return ESP_OK;
151 }
152 
153 /**
154  * @brief Set default handlers
155  */
_esp_wifi_set_default_wifi_handlers(void)156 esp_err_t _esp_wifi_set_default_wifi_handlers(void)
157 {
158     if (wifi_default_handlers_set) {
159         return ESP_OK;
160     }
161     esp_err_t err;
162 
163     err = esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_START, wifi_default_action_sta_start, NULL);
164     if (err != ESP_OK) {
165         goto fail;
166     }
167 
168     err = esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_STOP, wifi_default_action_sta_stop, NULL);
169     if (err != ESP_OK) {
170         goto fail;
171     }
172 
173     err = esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, wifi_default_action_sta_connected, NULL);
174     if (err != ESP_OK) {
175         goto fail;
176     }
177 
178     err = esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, wifi_default_action_sta_disconnected, NULL);
179     if (err != ESP_OK) {
180         goto fail;
181     }
182 
183 #ifdef CONFIG_ESP_WIFI_SOFTAP_SUPPORT
184     err = esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_AP_START, wifi_default_action_ap_start, NULL);
185     if (err != ESP_OK) {
186         goto fail;
187     }
188 
189     err = esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_AP_STOP, wifi_default_action_ap_stop, NULL);
190     if (err != ESP_OK) {
191         goto fail;
192     }
193 #endif
194 
195     err = esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, wifi_default_action_sta_got_ip, NULL);
196     if (err != ESP_OK) {
197         goto fail;
198     }
199 
200     err = esp_register_shutdown_handler((shutdown_handler_t)esp_wifi_stop);
201     if (err != ESP_OK && err != ESP_ERR_INVALID_STATE) {
202         goto fail;
203     }
204     wifi_default_handlers_set = true;
205     return ESP_OK;
206 
207 fail:
208     _esp_wifi_clear_default_wifi_handlers();
209     return err;
210 }
211 
212 /**
213  * @brief Set default handlers for station (official API)
214  */
esp_wifi_set_default_wifi_sta_handlers(void)215 esp_err_t esp_wifi_set_default_wifi_sta_handlers(void)
216 {
217     return _esp_wifi_set_default_wifi_handlers();
218 }
219 
220 /**
221  * @brief Set default handlers for AP (official API)
222  */
esp_wifi_set_default_wifi_ap_handlers(void)223 esp_err_t esp_wifi_set_default_wifi_ap_handlers(void)
224 {
225     return _esp_wifi_set_default_wifi_handlers();
226 }
227 
228 /**
229  * @brief Clear default handlers and destroy appropriate objects (official API)
230  */
esp_wifi_clear_default_wifi_driver_and_handlers(void * esp_netif)231 esp_err_t esp_wifi_clear_default_wifi_driver_and_handlers(void *esp_netif)
232 {
233     int i;
234     for (i = 0; i< MAX_WIFI_IFS; ++i) {
235         // clear internal static pointers to netifs
236         if (s_wifi_netifs[i] == esp_netif) {
237             s_wifi_netifs[i] = NULL;
238         }
239         // check if all netifs are cleared to delete default handlers
240         if (s_wifi_netifs[i] != NULL) {
241             break;
242         }
243     }
244 
245     if (i == MAX_WIFI_IFS) { // if all wifi default netifs are null
246         ESP_LOGD(TAG, "Clearing wifi default handlers");
247         _esp_wifi_clear_default_wifi_handlers();
248     }
249     return disconnect_and_destroy(esp_netif);
250 }
251 
252 
253 //
254 // Object manipulation
255 //
256 
257 /**
258  * @brief Create and destroy objects
259  */
disconnect_and_destroy(esp_netif_t * esp_netif)260 static esp_err_t disconnect_and_destroy(esp_netif_t* esp_netif)
261 {
262     wifi_netif_driver_t driver = esp_netif_get_io_driver(esp_netif);
263     esp_netif_driver_ifconfig_t driver_ifconfig = { };
264     esp_err_t  ret = esp_netif_set_driver_config(esp_netif, &driver_ifconfig);
265     esp_wifi_destroy_if_driver(driver);
266     return ret;
267 }
268 
create_and_attach(wifi_interface_t wifi_if,esp_netif_t * esp_netif)269 static esp_err_t create_and_attach(wifi_interface_t wifi_if, esp_netif_t* esp_netif)
270 {
271     wifi_netif_driver_t driver = esp_wifi_create_if_driver(wifi_if);
272     if (driver == NULL) {
273         ESP_LOGE(TAG, "Failed to create wifi interface handle");
274         return ESP_FAIL;
275     }
276     return esp_netif_attach(esp_netif, driver);
277 }
278 
esp_netif_attach_wifi(esp_netif_t * esp_netif,wifi_interface_t wifi_if)279 static inline esp_err_t esp_netif_attach_wifi(esp_netif_t *esp_netif, wifi_interface_t wifi_if)
280 {
281     if (esp_netif == NULL || (wifi_if != WIFI_IF_STA
282 #ifdef CONFIG_ESP_WIFI_SOFTAP_SUPPORT
283     && wifi_if != WIFI_IF_AP
284 #endif
285     )) {
286         return ESP_ERR_INVALID_ARG;
287     }
288     s_wifi_netifs[wifi_if] = esp_netif;
289     return create_and_attach(wifi_if, esp_netif);
290 }
291 
esp_netif_attach_wifi_station(esp_netif_t * esp_netif)292 esp_err_t esp_netif_attach_wifi_station(esp_netif_t *esp_netif)
293 {
294     return esp_netif_attach_wifi(esp_netif, WIFI_IF_STA);
295 }
296 
297 #ifdef CONFIG_ESP_WIFI_SOFTAP_SUPPORT
esp_netif_attach_wifi_ap(esp_netif_t * esp_netif)298 esp_err_t esp_netif_attach_wifi_ap(esp_netif_t *esp_netif)
299 {
300     return esp_netif_attach_wifi(esp_netif, WIFI_IF_AP);
301 }
302 #endif
303 
304 
305 //
306 // Default WiFi creation from user code
307 //
308 
309 #ifdef CONFIG_ESP_WIFI_SOFTAP_SUPPORT
310 /**
311  * @brief User init default AP (official API)
312  */
esp_netif_create_default_wifi_ap(void)313 esp_netif_t* esp_netif_create_default_wifi_ap(void)
314 {
315     esp_netif_config_t cfg = ESP_NETIF_DEFAULT_WIFI_AP();
316     esp_netif_t *netif = esp_netif_new(&cfg);
317     assert(netif);
318     esp_netif_attach_wifi_ap(netif);
319     esp_wifi_set_default_wifi_ap_handlers();
320     return netif;
321 }
322 #endif
323 
324 /**
325  * @brief User init default station (official API)
326  */
esp_netif_create_default_wifi_sta(void)327 esp_netif_t* esp_netif_create_default_wifi_sta(void)
328 {
329     esp_netif_config_t cfg = ESP_NETIF_DEFAULT_WIFI_STA();
330     esp_netif_t *netif = esp_netif_new(&cfg);
331     assert(netif);
332     esp_netif_attach_wifi_station(netif);
333     esp_wifi_set_default_wifi_sta_handlers();
334     return netif;
335 }
336 
337 /**
338  * @brief User init default wifi esp_netif object (official API)
339  */
esp_netif_destroy_default_wifi(void * esp_netif)340 void esp_netif_destroy_default_wifi(void *esp_netif)
341 {
342     if (esp_netif) {
343         esp_wifi_clear_default_wifi_driver_and_handlers(esp_netif);
344     }
345     esp_netif_destroy(esp_netif);
346 }
347 
348 /**
349  * @brief User init custom wifi interface
350  */
esp_netif_create_wifi(wifi_interface_t wifi_if,esp_netif_inherent_config_t * esp_netif_config)351 esp_netif_t* esp_netif_create_wifi(wifi_interface_t wifi_if, esp_netif_inherent_config_t *esp_netif_config)
352 {
353     esp_netif_config_t cfg = {
354         .base = esp_netif_config
355     };
356     if (wifi_if == WIFI_IF_STA) {
357         cfg.stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_STA;
358     } else
359 #ifdef CONFIG_ESP_WIFI_SOFTAP_SUPPORT
360     if (wifi_if == WIFI_IF_AP) {
361         cfg.stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_AP;
362     } else
363 #endif
364     {
365         return NULL;
366     }
367 
368     esp_netif_t *netif = esp_netif_new(&cfg);
369     assert(netif);
370     esp_netif_attach_wifi(netif, wifi_if);
371     return netif;
372 }
373 
374 #ifdef CONFIG_ESP_WIFI_SOFTAP_SUPPORT
375 /**
376  * @brief Creates mesh network interfaces based on default STA and AP,
377  * but without DHCP, this is to be enabled separately only on root node
378  */
esp_netif_create_default_wifi_mesh_netifs(esp_netif_t ** p_netif_sta,esp_netif_t ** p_netif_ap)379 esp_err_t esp_netif_create_default_wifi_mesh_netifs(esp_netif_t **p_netif_sta, esp_netif_t **p_netif_ap)
380 {
381     // Create "almost" default AP, with un-flagged DHCP server
382     esp_netif_inherent_config_t netif_cfg;
383     memcpy(&netif_cfg, ESP_NETIF_BASE_DEFAULT_WIFI_AP, sizeof(netif_cfg));
384     netif_cfg.flags &= ~ESP_NETIF_DHCP_SERVER;
385     esp_netif_config_t cfg_ap = {
386             .base = &netif_cfg,
387             .stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_AP,
388     };
389     esp_netif_t *netif_ap = esp_netif_new(&cfg_ap);
390     assert(netif_ap);
391     ESP_ERROR_CHECK(esp_netif_attach_wifi_ap(netif_ap));
392     ESP_ERROR_CHECK(esp_wifi_set_default_wifi_ap_handlers());
393 
394     // ...and stop DHCP server to be compatible with former tcpip_adapter (to keep the ESP_NETIF_DHCP_STOPPED state)
395     ESP_ERROR_CHECK(esp_netif_dhcps_stop(netif_ap));
396 
397     // Create "almost" default station, but with un-flagged DHCP client
398     memcpy(&netif_cfg, ESP_NETIF_BASE_DEFAULT_WIFI_STA, sizeof(netif_cfg));
399     netif_cfg.flags &= ~ESP_NETIF_DHCP_CLIENT;
400     esp_netif_config_t cfg_sta = {
401             .base = &netif_cfg,
402             .stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_STA,
403     };
404     esp_netif_t *netif_sta = esp_netif_new(&cfg_sta);
405     assert(netif_sta);
406     ESP_ERROR_CHECK(esp_netif_attach_wifi_station(netif_sta));
407     ESP_ERROR_CHECK(esp_wifi_set_default_wifi_sta_handlers());
408 
409     // ...and stop DHCP client (to be started separately if the station were promoted to root)
410     ESP_ERROR_CHECK(esp_netif_dhcpc_stop(netif_sta));
411 
412     if (p_netif_sta) {
413         *p_netif_sta = netif_sta;
414     }
415     if (p_netif_ap) {
416         *p_netif_ap = netif_ap;
417     }
418     return ESP_OK;
419 }
420 #endif // CONFIG_ESP_WIFI_SOFTAP_SUPPORT
421