1 /**
2  * @file
3  * Ethernet Interface Skeleton
4  *
5  */
6 
7 /*
8  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without modification,
12  * are permitted provided that the following conditions are met:
13  *
14  * 1. Redistributions of source code must retain the above copyright notice,
15  *    this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright notice,
17  *    this list of conditions and the following disclaimer in the documentation
18  *    and/or other materials provided with the distribution.
19  * 3. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
23  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
25  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
27  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
31  * OF SUCH DAMAGE.
32  *
33  * This file is part of the lwIP TCP/IP stack.
34  *
35  * Author: Adam Dunkels <adam@sics.se>
36  *
37  */
38 
39 #include "lwip/opt.h"
40 
41 #include "lwip/def.h"
42 #include "lwip/mem.h"
43 #include "lwip/pbuf.h"
44 #include "lwip/stats.h"
45 #include "lwip/snmp.h"
46 #include "lwip/ethip6.h"
47 #include "netif/etharp.h"
48 #include "netif/wlanif.h"
49 
50 #include <stdio.h>
51 #include <string.h>
52 
53 #include "esp_netif.h"
54 #include "esp_netif_net_stack.h"
55 #include "esp_compiler.h"
56 
57 #if !ESP_L2_TO_L3_COPY
58 /**
59  * @brief Free resources allocated in L2 layer
60  *
61  * @param buf memory alloc in L2 layer
62  * @note this function is also the callback when invoke pbuf_free
63  */
lwip_netif_wifi_free_rx_buffer(struct netif * netif,void * buf)64 static void lwip_netif_wifi_free_rx_buffer(struct netif *netif, void *buf)
65 {
66     esp_netif_t *esp_netif = esp_netif_get_handle_from_netif_impl(netif);
67     esp_netif_free_rx_buffer(esp_netif, buf);
68 }
69 #endif
70 
71 /**
72  * In this function, the hardware should be initialized.
73  * Called from ethernetif_init().
74  *
75  * @param netif the already initialized lwip network interface structure
76  *        for this ethernetif
77  */
78 static void
low_level_init(struct netif * netif)79 low_level_init(struct netif *netif)
80 {
81   /* set MAC hardware address length */
82   netif->hwaddr_len = ETHARP_HWADDR_LEN;
83 
84   /* set MAC hardware address */
85 
86   /* maximum transfer unit */
87   netif->mtu = 1500;
88 
89   /* device capabilities */
90   /* don't set NETIF_FLAG_ETHARP if this device is not an ethernet one */
91   netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;
92 
93 #if ESP_LWIP
94 #if LWIP_IGMP
95   netif->flags |= NETIF_FLAG_IGMP;
96 #endif
97 #endif
98 
99 #if ESP_IPV6
100 #if LWIP_IPV6 && LWIP_IPV6_MLD
101   netif->flags |= NETIF_FLAG_MLD6;
102 #endif
103 #endif
104 
105 #if !ESP_L2_TO_L3_COPY
106     netif->l2_buffer_free_notify = lwip_netif_wifi_free_rx_buffer;
107 #endif
108 }
109 
110 /**
111  * This function should do the actual transmission of the packet. The packet is
112  * contained in the pbuf that is passed to the function. This pbuf
113  * might be chained.
114  *
115  * @param netif the lwip network interface structure for this ethernetif
116  * @param p the MAC packet to send (e.g. IP packet including MAC addresses and type)
117  * @return ERR_OK if the packet could be sent
118  *         an err_t value if the packet couldn't be sent
119  *
120  * @note Returning ERR_MEM here if a DMA queue of your MAC is full can lead to
121  *       strange results. You might consider waiting for space in the DMA queue
122  *       to become availale since the stack doesn't retry to send a packet
123  *       dropped because of memory failure (except for the TCP timers).
124  */
125 static err_t ESP_IRAM_ATTR
low_level_output(struct netif * netif,struct pbuf * p)126 low_level_output(struct netif *netif, struct pbuf *p)
127 {
128   esp_netif_t *esp_netif = esp_netif_get_handle_from_netif_impl(netif);
129   if (esp_netif == NULL) {
130     return ERR_IF;
131   }
132 
133   struct pbuf *q = p;
134   esp_err_t ret;
135 
136   if(q->next == NULL) {
137     ret = esp_netif_transmit_wrap(esp_netif, q->payload, q->len, q);
138 
139   } else {
140     LWIP_DEBUGF(PBUF_DEBUG, ("low_level_output: pbuf is a list, application may has bug"));
141     q = pbuf_alloc(PBUF_RAW_TX, p->tot_len, PBUF_RAM);
142     if (q != NULL) {
143       q->l2_owner = NULL;
144       pbuf_copy(q, p);
145     } else {
146       return ERR_MEM;
147     }
148     ret = esp_netif_transmit_wrap(esp_netif, q->payload, q->len, q);
149 
150     pbuf_free(q);
151   }
152 
153   if (ret == ESP_OK) {
154     return ERR_OK;
155   } else if (ret == ESP_ERR_NO_MEM) {
156     return ERR_MEM;
157   } else if (ret == ESP_ERR_INVALID_ARG) {
158     return ERR_ARG;
159   } else {
160     return ERR_IF;
161   }
162 }
163 
164 /**
165  * This function should be called when a packet is ready to be read
166  * from the interface. It uses the function low_level_input() that
167  * should handle the actual reception of bytes from the network
168  * interface. Then the type of the received packet is determined and
169  * the appropriate input function is called.
170  *
171  * @param netif the lwip network interface structure for this ethernetif
172  */
173 void ESP_IRAM_ATTR
wlanif_input(void * h,void * buffer,size_t len,void * eb)174 wlanif_input(void *h, void *buffer, size_t len, void* eb)
175 {
176   struct netif * netif = h;
177   esp_netif_t *esp_netif = esp_netif_get_handle_from_netif_impl(netif);
178   struct pbuf *p;
179 
180   if(unlikely(!buffer || !netif_is_up(netif))) {
181     if (eb) {
182       esp_netif_free_rx_buffer(esp_netif, eb);
183     }
184     return;
185   }
186 
187 #if (ESP_L2_TO_L3_COPY == 1)
188   p = pbuf_alloc(PBUF_RAW, len, PBUF_RAM);
189   if (p == NULL) {
190       esp_netif_free_rx_buffer(esp_netif, eb);
191     return;
192   }
193   p->l2_owner = NULL;
194   memcpy(p->payload, buffer, len);
195   esp_netif_free_rx_buffer(esp_netif, eb);
196 #else
197   p = pbuf_alloc(PBUF_RAW, len, PBUF_REF);
198   if (p == NULL){
199     esp_netif_free_rx_buffer(esp_netif, eb);
200     return;
201   }
202   p->payload = buffer;
203   p->l2_owner = netif;
204   p->l2_buf = eb;
205 #endif
206 
207   /* full packet send to tcpip_thread to process */
208   if (unlikely(netif->input(p, netif) != ERR_OK)) {
209     LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: IP input error\n"));
210     pbuf_free(p);
211   }
212 
213 }
214 
215 /**
216  * Should be called at the beginning of the program to set up the
217  * network interface. It calls the function low_level_init() to do the
218  * actual setup of the hardware.
219  *
220  * This function should be passed as a parameter to netif_add().
221  *
222  * @param netif the lwip network interface structure for this ethernetif
223  * @return ERR_OK if the loopif is initialized
224  *         ERR_MEM if private data couldn't be allocated
225  *         any other err_t on error
226  */
227 err_t
wlanif_init(struct netif * netif)228 wlanif_init(struct netif *netif)
229 {
230   LWIP_ASSERT("netif != NULL", (netif != NULL));
231 
232 #if LWIP_NETIF_HOSTNAME
233   /* Initialize interface hostname */
234 
235 #if ESP_LWIP
236   if (esp_netif_get_hostname(esp_netif_get_handle_from_netif_impl(netif), &netif->hostname) != ESP_OK) {
237     netif->hostname = CONFIG_LWIP_LOCAL_HOSTNAME;
238   }
239 #else
240   netif->hostname = "lwip";
241 #endif
242 
243 #endif /* LWIP_NETIF_HOSTNAME */
244 
245   /*
246    * Initialize the snmp variables and counters inside the struct netif.
247    * The last argument should be replaced with your link speed, in units
248    * of bits per second.
249    */
250   NETIF_INIT_SNMP(netif, snmp_ifType_ethernet_csmacd, 100);
251 
252   /* We directly use etharp_output() here to save a function call.
253    * You can instead declare your own function an call etharp_output()
254    * from it if you have to do some checks before sending (e.g. if link
255    * is available...) */
256   netif->output = etharp_output;
257 #if LWIP_IPV6
258   netif->output_ip6 = ethip6_output;
259 #endif /* LWIP_IPV6 */
260   netif->linkoutput = low_level_output;
261 
262   /* initialize the hardware */
263   low_level_init(netif);
264 
265   return ERR_OK;
266 }
267 
wlanif_init_sta(struct netif * netif)268 err_t wlanif_init_sta(struct netif *netif) {
269   netif->name[0] = 's';
270   netif->name[1] = 't';
271   return wlanif_init(netif);
272 }
273 
wlanif_init_ap(struct netif * netif)274 err_t wlanif_init_ap(struct netif *netif) {
275   netif->name[0] = 'a';
276   netif->name[1] = 'p';
277   return wlanif_init(netif);
278 }
279