esp32_wifi_driver.c
Go to the documentation of this file.
1 /**
2  * @file esp32_wifi_driver.c
3  * @brief ESP32 Wi-Fi controller
4  *
5  * @section License
6  *
7  * Copyright (C) 2010-2018 Oryx Embedded SARL. All rights reserved.
8  *
9  * This file is part of CycloneTCP Open.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24  *
25  * @author Oryx Embedded SARL (www.oryx-embedded.com)
26  * @version 1.9.0
27  **/
28 
29 //Switch to the appropriate trace level
30 #define TRACE_LEVEL NIC_TRACE_LEVEL
31 
32 //Dependencies
33 #include "esp_wifi.h"
34 #include "esp_wifi_internal.h"
35 #include "core/net.h"
37 #include "debug.h"
38 
39 //System event handlers
40 extern system_event_handler_t default_event_handlers[SYSTEM_EVENT_MAX];
41 
42 //Forward declaration of functions
43 esp_err_t esp32WifiStaStartEvent(system_event_t *event);
44 esp_err_t esp32WifiStaStopEvent(system_event_t *event);
45 esp_err_t esp32WifiStaConnectedEvent(system_event_t *event);
46 esp_err_t esp32WifiStaDisconnectedEvent(system_event_t *event);
47 esp_err_t esp32WifiStaGotIpEvent(system_event_t *event);
48 esp_err_t esp32WifiStaLostIpEvent(system_event_t *event);
49 esp_err_t esp32WifiApStartEvent(system_event_t *event);
50 esp_err_t esp32WifiApStopEvent(system_event_t *event);
51 esp_err_t esp32WifiStaRxCallback(void *buffer, uint16_t length, void *eb);
52 esp_err_t esp32WifiApRxCallback(void *buffer, uint16_t length, void *eb);
53 
54 //Underlying network interface
55 static NetInterface *esp32WifiStaInterface = NULL;
56 static NetInterface *esp32WifiApInterface = NULL;
57 
58 
59 /**
60  * @brief ESP32 Wi-Fi driver (STA mode)
61  **/
62 
64 {
66  ETH_MTU,
74  NULL,
75  NULL,
76  NULL,
77  TRUE,
78  TRUE,
79  TRUE,
80  TRUE
81 };
82 
83 
84 /**
85  * @brief ESP32 Wi-Fi driver (AP mode)
86  **/
87 
89 {
91  ETH_MTU,
99  NULL,
100  NULL,
101  NULL,
102  TRUE,
103  TRUE,
104  TRUE,
105  TRUE
106 };
107 
108 
109 /**
110  * @brief ESP32_WIFI initialization
111  * @param[in] interface Underlying network interface
112  * @return Error code
113  **/
114 
116 {
117  esp_err_t ret;
118 
119  //Initialize status code
120  ret = ESP_OK;
121 
122  //STA or AP mode?
123  if(interface->nicDriver == &esp32WifiStaDriver)
124  {
125  //Debug message
126  TRACE_INFO("Initializing ESP32 Wi-Fi (STA mode)...\r\n");
127  }
128  else
129  {
130  //Debug message
131  TRACE_INFO("Initializing ESP32 Wi-Fi (AP mode)...\r\n");
132  }
133 
134  //Initialization sequence is performed once at startup
135  if(esp32WifiStaInterface == NULL && esp32WifiApInterface == NULL)
136  {
137  //Set default configuration
138  wifi_init_config_t config = WIFI_INIT_CONFIG_DEFAULT();
139 
140  //Initialize Wi-Fi driver
141  ret = esp_wifi_init(&config);
142 
143  //Check status code
144  if(ret == ESP_OK)
145  {
146  //Register event handlers
147  default_event_handlers[SYSTEM_EVENT_STA_START] = esp32WifiStaStartEvent;
148  default_event_handlers[SYSTEM_EVENT_STA_STOP] = esp32WifiStaStopEvent;
149  default_event_handlers[SYSTEM_EVENT_STA_CONNECTED] = esp32WifiStaConnectedEvent;
150  default_event_handlers[SYSTEM_EVENT_STA_DISCONNECTED] = esp32WifiStaDisconnectedEvent;
151  default_event_handlers[SYSTEM_EVENT_STA_GOT_IP] = esp32WifiStaGotIpEvent;
152  default_event_handlers[SYSTEM_EVENT_STA_LOST_IP] = esp32WifiStaLostIpEvent;
153  default_event_handlers[SYSTEM_EVENT_AP_START] = esp32WifiApStartEvent;
154  default_event_handlers[SYSTEM_EVENT_AP_STOP] = esp32WifiApStopEvent;
155 
156  //Register shutdown handler
157  ret = esp_register_shutdown_handler((shutdown_handler_t) esp_wifi_stop);
158  }
159  }
160 
161  //Check status code
162  if(ret == ESP_OK)
163  {
164  //STA or AP mode?
165  if(interface->nicDriver == &esp32WifiStaDriver)
166  {
167  //Save underlying network interface (STA mode)
168  esp32WifiStaInterface = interface;
169 
170  //Optionally set the MAC address
171  if(macCompAddr(&interface->macAddr, &MAC_UNSPECIFIED_ADDR))
172  {
173  //Use the factory preprogrammed station address
174  ret = esp_wifi_get_mac(ESP_IF_WIFI_STA, interface->macAddr.b);
175 
176  //Check status code
177  if(ret == ESP_OK)
178  {
179  //Generate the 64-bit interface identifier
180  macAddrToEui64(&interface->macAddr, &interface->eui64);
181  }
182  }
183  else
184  {
185  //Override the factory preprogrammed address
186  ret = esp_wifi_set_mac(ESP_IF_WIFI_STA, interface->macAddr.b);
187  }
188  }
189  else
190  {
191  //Save underlying network interface (AP mode)
192  esp32WifiApInterface = interface;
193 
194  //Optionally set the MAC address
195  if(macCompAddr(&interface->macAddr, &MAC_UNSPECIFIED_ADDR))
196  {
197  //Use the factory preprogrammed station address
198  ret = esp_wifi_get_mac(ESP_IF_WIFI_AP, interface->macAddr.b);
199 
200  //Check status code
201  if(ret == ESP_OK)
202  {
203  //Generate the 64-bit interface identifier
204  macAddrToEui64(&interface->macAddr, &interface->eui64);
205  }
206  }
207  else
208  {
209  //Override the factory preprogrammed address
210  ret = esp_wifi_set_mac(ESP_IF_WIFI_AP, interface->macAddr.b);
211  }
212  }
213  }
214 
215  //ESP32 Wi-Fi is now ready to send
216  osSetEvent(&interface->nicTxEvent);
217 
218  //Return status code
219  if(ret == ESP_OK)
220  return NO_ERROR;
221  else
222  return ERROR_FAILURE;
223 }
224 
225 
226 /**
227  * @brief ESP32 Wi-Fi timer handler
228  *
229  * This routine is periodically called by the TCP/IP stack to
230  * handle periodic operations such as polling the link state
231  *
232  * @param[in] interface Underlying network interface
233  **/
234 
235 void esp32WifiTick(NetInterface *interface)
236 {
237 }
238 
239 
240 /**
241  * @brief Enable interrupts
242  * @param[in] interface Underlying network interface
243  **/
244 
246 {
247 }
248 
249 
250 /**
251  * @brief Disable interrupts
252  * @param[in] interface Underlying network interface
253  **/
254 
256 {
257 }
258 
259 
260 /**
261  * @brief ESP32 Wi-Fi event handler
262  * @param[in] interface Underlying network interface
263  **/
264 
266 {
267 }
268 
269 
270 /**
271  * @brief Send a packet
272  * @param[in] interface Underlying network interface
273  * @param[in] buffer Multi-part buffer containing the data to send
274  * @param[in] offset Offset to the first data byte
275  * @return Error code
276  **/
277 
279  const NetBuffer *buffer, size_t offset)
280 {
281  static uint8_t temp[2048];
282  size_t length;
283  int_t ret;
284 
285  //Retrieve the length of the packet
286  length = netBufferGetLength(buffer) - offset;
287 
288  //Copy user data
289  netBufferRead(temp, buffer, offset, length);
290 
291  //STA or AP mode?
292  if(interface == esp32WifiStaInterface)
293  {
294  //Send packet
295  ret = esp_wifi_internal_tx(ESP_IF_WIFI_STA, temp, length);
296  }
297  else
298  {
299  //Send packet
300  ret = esp_wifi_internal_tx(ESP_IF_WIFI_AP, temp, length);
301  }
302 
303  //The transmitter can accept another packet
304  osSetEvent(&interface->nicTxEvent);
305 
306  //Return status code
307  if(ret == ESP_OK)
308  return NO_ERROR;
309  else
310  return ERROR_FAILURE;
311 }
312 
313 
314 /**
315  * @brief Configure MAC address filtering
316  * @param[in] interface Underlying network interface
317  * @return Error code
318  **/
319 
321 {
322  //Not implemented
323  return NO_ERROR;
324 }
325 
326 
327 /**
328  * @brief Station start (event handler)
329  * @param[in] event Event information
330  * @return Error code
331  **/
332 
333 esp_err_t esp32WifiStaStartEvent(system_event_t *event)
334 {
335  //Debug message
336  TRACE_INFO("ESP32: STA start event\r\n");
337 
338  //Successful processing
339  return ESP_OK;
340 }
341 
342 
343 /**
344  * @brief Station stop (event handler)
345  * @param[in] event Event information
346  * @return Error code
347  **/
348 
349 esp_err_t esp32WifiStaStopEvent(system_event_t *event)
350 {
351  //Debug message
352  TRACE_INFO("ESP32: STA stop event\r\n");
353 
354  //Successful processing
355  return ESP_OK;
356 }
357 
358 
359 /**
360  * @brief Station connected to AP (event handler)
361  * @param[in] event Event information
362  * @return Error code
363  **/
364 
365 esp_err_t esp32WifiStaConnectedEvent(system_event_t *event)
366 {
367  esp_err_t ret;
368 
369  //Debug message
370  TRACE_INFO("ESP32: STA connected event\r\n");
371 
372  //Register RX callback
373  ret = esp_wifi_internal_reg_rxcb(ESP_IF_WIFI_STA, esp32WifiStaRxCallback);
374 
375  //Check status code
376  if(ret == ESP_OK)
377  {
378  //Valid STA interface?
379  if(esp32WifiStaInterface != NULL)
380  {
381  //The link is up
382  esp32WifiStaInterface->linkState = TRUE;
383 
384  //Get exclusive access
386  //Process link state change event
387  nicNotifyLinkChange(esp32WifiStaInterface);
388  //Release exclusive access
390  }
391  }
392 
393  //Return status code
394  return ret;
395 }
396 
397 
398 /**
399  * @brief Station disconnected from AP (event handler)
400  * @param[in] event Event information
401  * @return Error code
402  **/
403 
404 esp_err_t esp32WifiStaDisconnectedEvent(system_event_t *event)
405 {
406  esp_err_t ret;
407 
408  //Debug message
409  TRACE_INFO("ESP32: STA disconnected event\r\n");
410 
411  //Unregister RX callback
412  ret = esp_wifi_internal_reg_rxcb(ESP_IF_WIFI_STA, NULL);
413 
414  //Check status code
415  if(ret == ESP_OK)
416  {
417  //Valid STA interface?
418  if(esp32WifiStaInterface != NULL)
419  {
420  //The link is down
421  esp32WifiStaInterface->linkState = FALSE;
422 
423  //Get exclusive access
425  //Process link state change event
426  nicNotifyLinkChange(esp32WifiStaInterface);
427  //Release exclusive access
429  }
430  }
431 
432  //Return status code
433  return ret;
434 }
435 
436 
437 /**
438  * @brief Station got IP from connected AP (event handler)
439  * @param[in] event Event information
440  * @return Error code
441  **/
442 
443 esp_err_t esp32WifiStaGotIpEvent(system_event_t *event)
444 {
445  //Debug message
446  TRACE_INFO("ESP32: STA got IP event\r\n");
447 
448  //Return status code
449  return ESP_OK;
450 }
451 
452 
453 /**
454  * @brief Station lost IP (event handler)
455  * @param[in] event Event information
456  * @return Error code
457  **/
458 
459 esp_err_t esp32WifiStaLostIpEvent(system_event_t *event)
460 {
461  //Debug message
462  TRACE_INFO("ESP32: STA lost IP event\r\n");
463 
464  //Return status code
465  return ESP_OK;
466 }
467 
468 
469 /**
470  * @brief Soft-AP start (event handler)
471  * @param[in] event Event information
472  * @return Error code
473  **/
474 
475 esp_err_t esp32WifiApStartEvent(system_event_t *event)
476 {
477  esp_err_t ret;
478 
479  //Debug message
480  TRACE_INFO("ESP32: AP start event\r\n");
481 
482  //Register RX callback
483  ret = esp_wifi_internal_reg_rxcb(ESP_IF_WIFI_AP, esp32WifiApRxCallback);
484 
485  //Check status code
486  if(ret == ESP_OK)
487  {
488  //Valid AP interface?
489  if(esp32WifiApInterface != NULL)
490  {
491  //The link is up
492  esp32WifiApInterface->linkState = TRUE;
493 
494  //Get exclusive access
496  //Process link state change event
497  nicNotifyLinkChange(esp32WifiApInterface);
498  //Release exclusive access
500  }
501  }
502 
503  //Return status code
504  return ret;
505 }
506 
507 
508 /**
509  * @brief Soft-AP stop (event handler)
510  * @param[in] event Event information
511  * @return Error code
512  **/
513 
514 esp_err_t esp32WifiApStopEvent(system_event_t *event)
515 {
516  esp_err_t ret;
517 
518  //Debug message
519  TRACE_INFO("ESP32: AP stop event\r\n");
520 
521  //Unregister RX callback
522  ret = esp_wifi_internal_reg_rxcb(ESP_IF_WIFI_AP, NULL);
523 
524  //Check status code
525  if(ret == ESP_OK)
526  {
527  //Valid AP interface?
528  if(esp32WifiApInterface != NULL)
529  {
530  //The link is down
531  esp32WifiApInterface->linkState = FALSE;
532 
533  //Get exclusive access
535  //Process link state change event
536  nicNotifyLinkChange(esp32WifiApInterface);
537  //Release exclusive access
539  }
540  }
541 
542  //Return status code
543  return ret;
544 }
545 
546 
547 /**
548  * @brief Process incoming packets (STA interface)
549  * @param[in] buffer Incoming packet
550  * @param[in] length Length of the packet, in bytes
551  * @param[in] eb Pointer to the buffer allocated by the Wi-Fi driver
552  * @return Error code
553  **/
554 
555 esp_err_t esp32WifiStaRxCallback(void *buffer, uint16_t length, void *eb)
556 {
557  //Valid STA interface?
558  if(esp32WifiStaInterface != NULL)
559  {
560  //Get exclusive access
562  //Pass the packet to the upper layer
563  nicProcessPacket(esp32WifiStaInterface, buffer, length);
564  //Release exclusive access
566  }
567 
568  //Release buffer
569  if(eb != NULL)
570  {
571  esp_wifi_internal_free_rx_buffer(eb);
572  }
573 
574  //Successful processing
575  return ESP_OK;
576 }
577 
578 
579 /**
580  * @brief Process incoming packets (AP interface)
581  * @param[in] buffer Incoming packet
582  * @param[in] length Length of the packet, in bytes
583  * @param[in] eb Pointer to the buffer allocated by the Wi-Fi driver
584  * @return Error code
585  **/
586 
587 esp_err_t esp32WifiApRxCallback(void *buffer, uint16_t length, void *eb)
588 {
589  //Valid AP interface?
590  if(esp32WifiApInterface != NULL)
591  {
592  //Get exclusive access
594  //Pass the packet to the upper layer
595  nicProcessPacket(esp32WifiApInterface, buffer, length);
596  //Release exclusive access
598  }
599 
600  //Release buffer
601  if(eb != NULL)
602  {
603  esp_wifi_internal_free_rx_buffer(eb);
604  }
605 
606  //Successful processing
607  return ESP_OK;
608 }
const NicDriver esp32WifiStaDriver
ESP32 Wi-Fi driver (STA mode)
void esp32WifiTick(NetInterface *interface)
ESP32 Wi-Fi timer handler.
void nicNotifyLinkChange(NetInterface *interface)
Process link state change event.
Definition: nic.c:298
const NicDriver esp32WifiApDriver
ESP32 Wi-Fi driver (AP mode)
TCP/IP stack core.
esp_err_t esp32WifiStaRxCallback(void *buffer, uint16_t length, void *eb)
Process incoming packets (STA interface)
Debugging facilities.
size_t netBufferGetLength(const NetBuffer *buffer)
Get the actual length of a multi-part buffer.
Definition: net_mem.c:295
esp_err_t esp32WifiApStopEvent(system_event_t *event)
Soft-AP stop (event handler)
void esp32WifiEnableIrq(NetInterface *interface)
Enable interrupts.
Generic error code.
Definition: error.h:43
error_t esp32WifiUpdateMacAddrFilter(NetInterface *interface)
Configure MAC address filtering.
esp_err_t esp32WifiStaStopEvent(system_event_t *event)
Station stop (event handler)
#define TRUE
Definition: os_port.h:48
void esp32WifiDisableIrq(NetInterface *interface)
Disable interrupts.
signed int int_t
Definition: compiler_port.h:42
error_t esp32WifiSendPacket(NetInterface *interface, const NetBuffer *buffer, size_t offset)
Send a packet.
size_t netBufferRead(void *dest, const NetBuffer *src, size_t srcOffset, size_t length)
Read data from a multi-part buffer.
Definition: net_mem.c:670
NIC driver.
Definition: nic.h:161
Structure describing a buffer that spans multiple chunks.
Definition: net_mem.h:86
esp_err_t esp32WifiStaDisconnectedEvent(system_event_t *event)
Station disconnected from AP (event handler)
const MacAddr MAC_UNSPECIFIED_ADDR
Definition: ethernet.c:53
esp_err_t esp32WifiStaGotIpEvent(system_event_t *event)
Station got IP from connected AP (event handler)
ESP32 Wi-Fi controller.
void macAddrToEui64(const MacAddr *macAddr, Eui64 *interfaceId)
Map a MAC address to the IPv6 modified EUI-64 identifier.
Definition: ethernet.c:1266
#define TRACE_INFO(...)
Definition: debug.h:86
esp_err_t esp32WifiApStartEvent(system_event_t *event)
Soft-AP start (event handler)
#define ETH_MTU
Definition: ethernet.h:82
Ethernet interface.
Definition: nic.h:69
Success.
Definition: error.h:42
error_t esp32WifiInit(NetInterface *interface)
ESP32_WIFI initialization.
void nicProcessPacket(NetInterface *interface, void *packet, size_t length)
Handle a packet received by the network controller.
Definition: nic.c:239
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
esp_err_t esp32WifiStaStartEvent(system_event_t *event)
Station start (event handler)
error_t
Error codes.
Definition: error.h:40
void osReleaseMutex(OsMutex *mutex)
Release ownership of the specified mutex object.
#define NetInterface
Definition: net.h:34
system_event_handler_t default_event_handlers[SYSTEM_EVENT_MAX]
esp_err_t esp32WifiStaLostIpEvent(system_event_t *event)
Station lost IP (event handler)
#define macCompAddr(macAddr1, macAddr2)
Definition: ethernet.h:95
void esp32WifiEventHandler(NetInterface *interface)
ESP32 Wi-Fi event handler.
OsMutex netMutex
Definition: net.c:70
esp_err_t esp32WifiApRxCallback(void *buffer, uint16_t length, void *eb)
Process incoming packets (AP interface)
esp_err_t esp32WifiStaConnectedEvent(system_event_t *event)
Station connected to AP (event handler)
uint8_t length
Definition: dtls_misc.h:140
#define FALSE
Definition: os_port.h:44
void osAcquireMutex(OsMutex *mutex)
Acquire ownership of the specified mutex object.