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 #include <string.h>
20
21 //
22 // Purpose of this module is to provide basic wifi initialization setup for
23 // default station and AP and to register default handles for these interfaces
24 //
25 static const char* TAG = "wifi_init_default";
26
27 static esp_netif_t *s_wifi_netifs[MAX_WIFI_IFS] = { NULL };
28 static bool wifi_default_handlers_set = false;
29
30 static esp_err_t disconnect_and_destroy(esp_netif_t* esp_netif);
31
32 //
33 // Default event handlers
34 //
35
36 /**
37 * @brief Wifi start action when station or AP get started
38 */
wifi_start(void * esp_netif,esp_event_base_t base,int32_t event_id,void * data)39 static void wifi_start(void *esp_netif, esp_event_base_t base, int32_t event_id, void *data)
40 {
41 uint8_t mac[6];
42 esp_err_t ret;
43
44 ESP_LOGD(TAG, "%s esp-netif:%p event-id%d", __func__, esp_netif, event_id);
45
46 wifi_netif_driver_t driver = esp_netif_get_io_driver(esp_netif);
47
48 if ((ret = esp_wifi_get_if_mac(driver, mac)) != ESP_OK) {
49 ESP_LOGE(TAG, "esp_wifi_get_mac failed with %d", ret);
50 return;
51 }
52 ESP_LOGD(TAG, "WIFI mac address: %x %x %x %x %x %x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
53
54 if (esp_wifi_is_if_ready_when_started(driver)) {
55 if ((ret = esp_wifi_register_if_rxcb(driver, esp_netif_receive, esp_netif)) != ESP_OK) {
56 ESP_LOGE(TAG, "esp_wifi_register_if_rxcb for if=%p failed with %d", driver, ret);
57 return;
58 }
59 }
60
61 if ((ret = esp_wifi_internal_reg_netstack_buf_cb(esp_netif_netstack_buf_ref, esp_netif_netstack_buf_free)) != ESP_OK) {
62 ESP_LOGE(TAG, "netstack cb reg failed with %d", ret);
63 return;
64 }
65 esp_netif_set_mac(esp_netif, mac);
66 esp_netif_action_start(esp_netif, base, event_id, data);
67 }
68
69 /**
70 * @brief Wifi default handlers for specific events for station and APs
71 */
72
wifi_default_action_sta_start(void * arg,esp_event_base_t base,int32_t event_id,void * data)73 static void wifi_default_action_sta_start(void *arg, esp_event_base_t base, int32_t event_id, void *data)
74 {
75 if (s_wifi_netifs[WIFI_IF_STA] != NULL) {
76 wifi_start(s_wifi_netifs[WIFI_IF_STA], base, event_id, data);
77 }
78 }
79
wifi_default_action_sta_stop(void * arg,esp_event_base_t base,int32_t event_id,void * data)80 static void wifi_default_action_sta_stop(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_netif_action_stop(s_wifi_netifs[WIFI_IF_STA], base, event_id, data);
84 }
85 }
86
wifi_default_action_sta_connected(void * arg,esp_event_base_t base,int32_t event_id,void * data)87 static void wifi_default_action_sta_connected(void *arg, esp_event_base_t base, int32_t event_id, void *data)
88 {
89 if (s_wifi_netifs[WIFI_IF_STA] != NULL) {
90 esp_err_t ret;
91 esp_netif_t *esp_netif = s_wifi_netifs[WIFI_IF_STA];
92 wifi_netif_driver_t driver = esp_netif_get_io_driver(esp_netif);
93
94 if (!esp_wifi_is_if_ready_when_started(driver)) {
95 // if interface not ready when started, rxcb to be registered on connection
96 if ((ret = esp_wifi_register_if_rxcb(driver, esp_netif_receive, esp_netif)) != ESP_OK) {
97 ESP_LOGE(TAG, "esp_wifi_register_if_rxcb for if=%p failed with %d", driver, ret);
98 return;
99 }
100 }
101
102 esp_netif_action_connected(s_wifi_netifs[WIFI_IF_STA], base, event_id, data);
103 }
104 }
105
wifi_default_action_sta_disconnected(void * arg,esp_event_base_t base,int32_t event_id,void * data)106 static void wifi_default_action_sta_disconnected(void *arg, esp_event_base_t base, int32_t event_id, void *data)
107 {
108 if (s_wifi_netifs[WIFI_IF_STA] != NULL) {
109 esp_netif_action_disconnected(s_wifi_netifs[WIFI_IF_STA], base, event_id, data);
110 }
111 }
112
wifi_default_action_ap_start(void * arg,esp_event_base_t base,int32_t event_id,void * data)113 static void wifi_default_action_ap_start(void *arg, esp_event_base_t base, int32_t event_id, void *data)
114 {
115 if (s_wifi_netifs[WIFI_IF_AP] != NULL) {
116 wifi_start(s_wifi_netifs[WIFI_IF_AP], base, event_id, data);
117 }
118 }
119
wifi_default_action_ap_stop(void * arg,esp_event_base_t base,int32_t event_id,void * data)120 static void wifi_default_action_ap_stop(void *arg, esp_event_base_t base, int32_t event_id, void *data)
121 {
122 if (s_wifi_netifs[WIFI_IF_AP] != NULL) {
123 esp_netif_action_stop(s_wifi_netifs[WIFI_IF_AP], base, event_id, data);
124 }
125 }
126
wifi_default_action_sta_got_ip(void * arg,esp_event_base_t base,int32_t event_id,void * data)127 static void wifi_default_action_sta_got_ip(void *arg, esp_event_base_t base, int32_t event_id, void *data)
128 {
129 if (s_wifi_netifs[WIFI_IF_STA] != NULL) {
130 ESP_LOGD(TAG, "Got IP wifi default handler entered");
131 int ret = esp_wifi_internal_set_sta_ip();
132 if (ret != ESP_OK) {
133 ESP_LOGI(TAG, "esp_wifi_internal_set_sta_ip failed with %d", ret);
134 }
135 esp_netif_action_got_ip(s_wifi_netifs[WIFI_IF_STA], base, event_id, data);
136 }
137 }
138
139 /**
140 * @brief Clear default handlers
141 */
_esp_wifi_clear_default_wifi_handlers(void)142 esp_err_t _esp_wifi_clear_default_wifi_handlers(void)
143 {
144 esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_START, wifi_default_action_sta_start);
145 esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_STOP, wifi_default_action_sta_stop);
146 esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, wifi_default_action_sta_connected);
147 esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, wifi_default_action_sta_disconnected);
148 esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_AP_START, wifi_default_action_ap_start);
149 esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_AP_STOP, wifi_default_action_ap_stop);
150 esp_event_handler_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, wifi_default_action_sta_got_ip);
151 esp_unregister_shutdown_handler((shutdown_handler_t)esp_wifi_stop);
152 wifi_default_handlers_set = false;
153 return ESP_OK;
154 }
155
156 /**
157 * @brief Set default handlers
158 */
_esp_wifi_set_default_wifi_handlers(void)159 esp_err_t _esp_wifi_set_default_wifi_handlers(void)
160 {
161 if (wifi_default_handlers_set) {
162 return ESP_OK;
163 }
164 esp_err_t err;
165 err = esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_START, wifi_default_action_sta_start, NULL);
166 if (err != ESP_OK) {
167 goto fail;
168 }
169
170 err = esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_STOP, wifi_default_action_sta_stop, NULL);
171 if (err != ESP_OK) {
172 goto fail;
173 }
174
175 err = esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, wifi_default_action_sta_connected, NULL);
176 if (err != ESP_OK) {
177 goto fail;
178 }
179
180 err = esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, wifi_default_action_sta_disconnected, NULL);
181 if (err != ESP_OK) {
182 goto fail;
183 }
184
185 err = esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_AP_START, wifi_default_action_ap_start, NULL);
186 if (err != ESP_OK) {
187 goto fail;
188 }
189
190 err = esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_AP_STOP, wifi_default_action_ap_stop, NULL);
191 if (err != ESP_OK) {
192 goto fail;
193 }
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 && wifi_if != WIFI_IF_AP)) {
282 return ESP_ERR_INVALID_ARG;
283 }
284 s_wifi_netifs[wifi_if] = esp_netif;
285 return create_and_attach(wifi_if, esp_netif);
286 }
287
esp_netif_attach_wifi_station(esp_netif_t * esp_netif)288 esp_err_t esp_netif_attach_wifi_station(esp_netif_t *esp_netif)
289 {
290 return esp_netif_attach_wifi(esp_netif, WIFI_IF_STA);
291 }
292
esp_netif_attach_wifi_ap(esp_netif_t * esp_netif)293 esp_err_t esp_netif_attach_wifi_ap(esp_netif_t *esp_netif)
294 {
295 return esp_netif_attach_wifi(esp_netif, WIFI_IF_AP);
296 }
297
298
299 //
300 // Default WiFi creation from user code
301 //
302
303 /**
304 * @brief User init default AP (official API)
305 */
esp_netif_create_default_wifi_ap(void)306 esp_netif_t* esp_netif_create_default_wifi_ap(void)
307 {
308 esp_netif_config_t cfg = ESP_NETIF_DEFAULT_WIFI_AP();
309 esp_netif_t *netif = esp_netif_new(&cfg);
310 assert(netif);
311 esp_netif_attach_wifi_ap(netif);
312 esp_wifi_set_default_wifi_ap_handlers();
313 return netif;
314 }
315
316 /**
317 * @brief User init default station (official API)
318 */
esp_netif_create_default_wifi_sta(void)319 esp_netif_t* esp_netif_create_default_wifi_sta(void)
320 {
321 esp_netif_config_t cfg = ESP_NETIF_DEFAULT_WIFI_STA();
322 esp_netif_t *netif = esp_netif_new(&cfg);
323 assert(netif);
324 esp_netif_attach_wifi_station(netif);
325 esp_wifi_set_default_wifi_sta_handlers();
326 return netif;
327 }
328
329 /**
330 * @brief User init custom wifi interface
331 */
esp_netif_create_wifi(wifi_interface_t wifi_if,esp_netif_inherent_config_t * esp_netif_config)332 esp_netif_t* esp_netif_create_wifi(wifi_interface_t wifi_if, esp_netif_inherent_config_t *esp_netif_config)
333 {
334 esp_netif_config_t cfg = {
335 .base = esp_netif_config
336 };
337 if (wifi_if == WIFI_IF_STA) {
338 cfg.stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_STA;
339 } else if (wifi_if == WIFI_IF_AP) {
340 cfg.stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_AP;
341 } else {
342 return NULL;
343 }
344
345 esp_netif_t *netif = esp_netif_new(&cfg);
346 assert(netif);
347 esp_netif_attach_wifi(netif, wifi_if);
348 return netif;
349 }
350
351 /**
352 * @brief Creates mesh network interfaces based on default STA and AP,
353 * but without DHCP, this is to be enabled separately only on root node
354 */
esp_netif_create_default_wifi_mesh_netifs(esp_netif_t ** p_netif_sta,esp_netif_t ** p_netif_ap)355 esp_err_t esp_netif_create_default_wifi_mesh_netifs(esp_netif_t **p_netif_sta, esp_netif_t **p_netif_ap)
356 {
357 // Create "almost" default AP, with un-flagged DHCP server
358 esp_netif_inherent_config_t netif_cfg;
359 memcpy(&netif_cfg, ESP_NETIF_BASE_DEFAULT_WIFI_AP, sizeof(netif_cfg));
360 netif_cfg.flags &= ~ESP_NETIF_DHCP_SERVER;
361 esp_netif_config_t cfg_ap = {
362 .base = &netif_cfg,
363 .stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_AP,
364 };
365 esp_netif_t *netif_ap = esp_netif_new(&cfg_ap);
366 assert(netif_ap);
367 ESP_ERROR_CHECK(esp_netif_attach_wifi_ap(netif_ap));
368 ESP_ERROR_CHECK(esp_wifi_set_default_wifi_ap_handlers());
369
370 // ...and stop DHCP server to be compatible with former tcpip_adapter (to keep the ESP_NETIF_DHCP_STOPPED state)
371 ESP_ERROR_CHECK(esp_netif_dhcps_stop(netif_ap));
372
373 // Create "almost" default station, but with un-flagged DHCP client
374 memcpy(&netif_cfg, ESP_NETIF_BASE_DEFAULT_WIFI_STA, sizeof(netif_cfg));
375 netif_cfg.flags &= ~ESP_NETIF_DHCP_CLIENT;
376 esp_netif_config_t cfg_sta = {
377 .base = &netif_cfg,
378 .stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_STA,
379 };
380 esp_netif_t *netif_sta = esp_netif_new(&cfg_sta);
381 assert(netif_sta);
382 ESP_ERROR_CHECK(esp_netif_attach_wifi_station(netif_sta));
383 ESP_ERROR_CHECK(esp_wifi_set_default_wifi_sta_handlers());
384
385 // ...and stop DHCP client (to be started separately if the station were promoted to root)
386 ESP_ERROR_CHECK(esp_netif_dhcpc_stop(netif_sta));
387
388 if (p_netif_sta) {
389 *p_netif_sta = netif_sta;
390 }
391 if (p_netif_ap) {
392 *p_netif_ap = netif_ap;
393 }
394 return ESP_OK;
395 }
396