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