esp8266_driver.c
Go to the documentation of this file.
1 /**
2  * @file esp8266_driver.c
3  * @brief ESP8266 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 "espressif/esp_wifi.h"
36 #include "espressif/esp_system.h"
37 #include "lwip/pbuf.h"
38 #include "core/net.h"
40 #include "debug.h"
41 
42 //Underlying network interface
43 static NetInterface *esp8266WifiStaInterface = NULL;
44 static NetInterface *esp8266WifiApInterface = NULL;
45 
46 
47 /**
48  * @brief ESP8266 Wi-Fi driver (STA mode)
49  **/
50 
52 {
54  ETH_MTU,
62  NULL,
63  NULL,
64  NULL,
65  TRUE,
66  TRUE,
67  TRUE,
68  TRUE
69 };
70 
71 
72 /**
73  * @brief ESP8266 Wi-Fi driver (AP mode)
74  **/
75 
77 {
79  ETH_MTU,
87  NULL,
88  NULL,
89  NULL,
90  TRUE,
91  TRUE,
92  TRUE,
93  TRUE
94 };
95 
96 
97 /**
98  * @brief ESP8266_WIFI initialization
99  * @param[in] interface Underlying network interface
100  * @return Error code
101  **/
102 
104 {
105  bool_t ret;
106 
107  //STA or AP mode?
108  if(interface->nicDriver == &esp8266WifiStaDriver)
109  {
110  //Debug message
111  TRACE_INFO("Initializing ESP8266 Wi-Fi (STA mode)...\r\n");
112 
113  //Save underlying network interface (STA mode)
114  esp8266WifiStaInterface = interface;
115 
116  //Optionally set the MAC address
117  if(macCompAddr(&interface->macAddr, &MAC_UNSPECIFIED_ADDR))
118  {
119  //Use the factory preprogrammed station address
120  ret = sdk_wifi_get_macaddr(STATION_IF, interface->macAddr.b);
121 
122  //Check status code
123  if(ret)
124  {
125  //Generate the 64-bit interface identifier
126  macAddrToEui64(&interface->macAddr, &interface->eui64);
127  }
128  }
129  else
130  {
131  //Override the factory preprogrammed address
132  ret = sdk_wifi_set_macaddr(STATION_IF, interface->macAddr.b);
133  }
134  }
135  else
136  {
137  //Debug message
138  TRACE_INFO("Initializing ESP8266 Wi-Fi (AP mode)...\r\n");
139 
140  //Save underlying network interface (AP mode)
141  esp8266WifiApInterface = interface;
142 
143  //Optionally set the MAC address
144  if(macCompAddr(&interface->macAddr, &MAC_UNSPECIFIED_ADDR))
145  {
146  //Use the factory preprogrammed station address
147  ret = sdk_wifi_get_macaddr(SOFTAP_IF, interface->macAddr.b);
148 
149  //Check status code
150  if(ret)
151  {
152  //Generate the 64-bit interface identifier
153  macAddrToEui64(&interface->macAddr, &interface->eui64);
154  }
155  }
156  else
157  {
158  //Override the factory preprogrammed address
159  ret = sdk_wifi_set_macaddr(SOFTAP_IF, interface->macAddr.b);
160  }
161  }
162 
163  //ESP8266 Wi-Fi is now ready to send
164  osSetEvent(&interface->nicTxEvent);
165 
166  //Return status code
167  if(ret)
168  return NO_ERROR;
169  else
170  return ERROR_FAILURE;
171 }
172 
173 
174 /**
175  * @brief ESP8266 Wi-Fi timer handler
176  *
177  * This routine is periodically called by the TCP/IP stack to
178  * handle periodic operations such as polling the link state
179  *
180  * @param[in] interface Underlying network interface
181  **/
182 
184 {
185 }
186 
187 
188 /**
189  * @brief Enable interrupts
190  * @param[in] interface Underlying network interface
191  **/
192 
194 {
195 }
196 
197 
198 /**
199  * @brief Disable interrupts
200  * @param[in] interface Underlying network interface
201  **/
202 
204 {
205 }
206 
207 
208 /**
209  * @brief ESP8266 Wi-Fi event handler
210  * @param[in] interface Underlying network interface
211  **/
212 
214 {
215 }
216 
217 
218 /**
219  * @brief Send a packet
220  * @param[in] interface Underlying network interface
221  * @param[in] buffer Multi-part buffer containing the data to send
222  * @param[in] offset Offset to the first data byte
223  * @return Error code
224  **/
225 
227  const NetBuffer *buffer, size_t offset)
228 {
229  int_t ret;
230  size_t length;
231  struct netif *netif;
232  struct pbuf *p;
233 
234  //STA or AP mode?
235  if(interface == esp8266WifiStaInterface)
236  netif = sdk_system_get_netif(STATION_IF);
237  else
238  netif = sdk_system_get_netif(SOFTAP_IF);
239 
240  //Sanity check
241  if(netif != NULL)
242  {
243  //Retrieve the length of the packet
244  length = netBufferGetLength(buffer) - offset;
245 
246  //Allocate a buffer
247  p = pbuf_alloc(PBUF_RAW_TX, length, PBUF_RAM);
248 
249  //Successful memory allocation?
250  if(p != NULL)
251  {
252  //Copy user data
253  netBufferRead(p->payload, buffer, offset, length);
254 
255  //Send packet
256  ret = sdk_ieee80211_output_pbuf(netif, p);
257 
258  //Release buffer
259  pbuf_free(p);
260  }
261  }
262 
263  //The transmitter can accept another packet
264  osSetEvent(&interface->nicTxEvent);
265 
266  //Return status code
267  if(!ret)
268  return NO_ERROR;
269  else
270  return ERROR_FAILURE;
271 }
272 
273 
274 /**
275  * @brief Configure MAC address filtering
276  * @param[in] interface Underlying network interface
277  * @return Error code
278  **/
279 
281 {
282  //Not implemented
283  return NO_ERROR;
284 }
285 
286 
287 /**
288  * @brief Process link-up event
289  * @param[in] netif Underlying network interface
290  **/
291 
292 void netif_set_up(struct netif *netif)
293 {
294  NetInterface *interface;
295 
296  //Check the interface where the event is occurring
297  if(netif == sdk_system_get_netif(STATION_IF))
298  interface = esp8266WifiStaInterface;
299  else if(netif == sdk_system_get_netif(SOFTAP_IF))
300  interface = esp8266WifiApInterface;
301  else
302  interface = NULL;
303 
304  //Valid interface?
305  if(interface != NULL)
306  {
307  //The link is up
308  interface->linkState = TRUE;
309 
310  //Get exclusive access
312  //Process link state change event
313  nicNotifyLinkChange(interface);
314  //Release exclusive access
316  }
317 }
318 
319 
320 /**
321  * @brief Process link-down event
322  * @param[in] netif Underlying network interface
323  **/
324 
325 void netif_set_down(struct netif *netif)
326 {
327  NetInterface *interface;
328 
329  //Check the interface where the event is occurring
330  if(netif == sdk_system_get_netif(STATION_IF))
331  interface = esp8266WifiStaInterface;
332  else if(netif == sdk_system_get_netif(SOFTAP_IF))
333  interface = esp8266WifiApInterface;
334  else
335  interface = NULL;
336 
337  //Valid interface?
338  if(interface != NULL)
339  {
340  //The link is down
341  interface->linkState = FALSE;
342 
343  //Get exclusive access
345  //Process link state change event
346  nicNotifyLinkChange(interface);
347  //Release exclusive access
349  }
350 }
351 
352 
353 /**
354  * @brief Process incoming packets
355  * @param[in] netif Underlying network interface
356  * @param[in] p Pointer to the buffer allocated by the Wi-Fi driver
357  **/
358 
359 void ethernetif_input(struct netif *netif, struct pbuf *p)
360 {
361  NetInterface *interface;
362 
363  //Check the interface where the event is occurring
364  if(netif == sdk_system_get_netif(STATION_IF))
365  interface = esp8266WifiStaInterface;
366  else if(netif == sdk_system_get_netif(SOFTAP_IF))
367  interface = esp8266WifiApInterface;
368  else
369  interface = NULL;
370 
371  //Valid buffer?
372  if(p != NULL)
373  {
374  //Valid interface?
375  if(interface != NULL)
376  {
377  //Get exclusive access
379  //Pass the packet to the upper layer
380  nicProcessPacket(interface, p->payload, p->len);
381  //Release exclusive access
383  }
384 
385  //Release buffer
386  pbuf_free(p);
387  }
388 }
void nicNotifyLinkChange(NetInterface *interface)
Process link state change notification.
Definition: nic.c:525
void netif_set_up(struct netif *netif)
Process link-up event.
uint8_t length
Definition: dtls_misc.h:149
void ethernetif_input(struct netif *netif, struct pbuf *p)
Process incoming packets.
int bool_t
Definition: compiler_port.h:49
void esp8266WifiEventHandler(NetInterface *interface)
ESP8266 Wi-Fi event handler.
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
uint8_t p
Definition: ndp.h:298
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
#define TRUE
Definition: os_port.h:50
void esp8266WifiDisableIrq(NetInterface *interface)
Disable interrupts.
#define FALSE
Definition: os_port.h:46
error_t
Error codes.
Definition: error.h:42
void esp8266WifiTick(NetInterface *interface)
ESP8266 Wi-Fi timer handler.
void netif_set_down(struct netif *netif)
Process link-down event.
@ ERROR_FAILURE
Generic error code.
Definition: error.h:45
const NicDriver esp8266WifiStaDriver
ESP8266 Wi-Fi driver (STA mode)
#define NetInterface
Definition: net.h:36
OsMutex netMutex
Definition: net.c:75
void esp8266WifiEnableIrq(NetInterface *interface)
Enable interrupts.
#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
const NicDriver esp8266WifiApDriver
ESP8266 Wi-Fi driver (AP mode)
#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
error_t esp8266WifiInit(NetInterface *interface)
ESP8266_WIFI initialization.
error_t esp8266WifiSendPacket(NetInterface *interface, const NetBuffer *buffer, size_t offset)
Send a packet.
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
TCP/IP stack core.
NIC driver.
Definition: nic.h:179
error_t esp8266WifiUpdateMacAddrFilter(NetInterface *interface)
Configure MAC address filtering.
ESP8266 Wi-Fi controller.
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