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