1
8
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
37
38
39
40
41
42
43
44
45
46
47
48
49
50
53
54
55
56
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
82
83
97
98
101
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
131
132
133
134
138
139
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/* ... */
/* ... */
#include <stdio.h>
#include <string.h>
#include "lwip/opt.h"
#include "lwip/pbuf.h"
#include "lwip/ethip6.h"
#include "netif/etharp.h"
#include "esp_compiler.h"
#include "esp_netif.h"
#include "esp_netif_net_stack.h"
#include "lwip/esp_netif_net_stack.h"
#include "lwip/esp_pbuf_ref.h"11 includes
#define IFNAME0 'e'
#define IFNAME1 'n'
/* ... */
static void ethernet_low_level_init(struct netif *netif)
{
netif->hwaddr_len = ETH_HWADDR_LEN;
netif->mtu = 1500;
netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_ETHERNET;
#if ESP_LWIP
#if LWIP_IGMP
netif->flags |= NETIF_FLAG_IGMP;
#endif/* ... */
#endif
#if ESP_IPV6
#if LWIP_IPV6 && LWIP_IPV6_MLD
netif->flags |= NETIF_FLAG_MLD6;
#endif/* ... */
#endif
}{ ... }
/* ... */
static err_t ethernet_low_level_output(struct netif *netif, struct pbuf *p)
{
struct pbuf *q = p;
esp_netif_t *esp_netif = esp_netif_get_handle_from_netif_impl(netif);
esp_err_t ret = ESP_FAIL;
if (!esp_netif) {
LWIP_DEBUGF(NETIF_DEBUG, ("corresponding esp-netif is NULL: netif=%p pbuf=%p len=%d\n", netif, p, p->len));
return ERR_IF;
}{...}
if (q->next == NULL) {
ret = esp_netif_transmit(esp_netif, q->payload, q->len);
}{...} else {
LWIP_DEBUGF(PBUF_DEBUG, ("low_level_output: pbuf is a list, application may has bug"));
q = pbuf_alloc(PBUF_RAW_TX, p->tot_len, PBUF_RAM);
if (q != NULL) {
pbuf_copy(q, p);
}{...} else {
return ERR_MEM;
}{...}
ret = esp_netif_transmit(esp_netif, q->payload, q->len);
pbuf_free(q);
}{...}
if (likely(ret == ESP_OK)) {
return ERR_OK;
}{...}
if (ret == ESP_ERR_NO_MEM) {
return ERR_MEM;
}{...}
return ERR_IF;
}{ ... }
/* ... */
esp_netif_recv_ret_t ethernetif_input(void *h, void *buffer, size_t len, void *l2_buff)
{
struct netif *netif = h;
esp_netif_t *esp_netif = esp_netif_get_handle_from_netif_impl(netif);
struct pbuf *p;
if (unlikely(buffer == NULL || !netif_is_up(netif))) {
if (buffer) {
esp_netif_free_rx_buffer(esp_netif, buffer);
}{...}
return ESP_NETIF_OPTIONAL_RETURN_CODE(ESP_FAIL);
}{...}
p = esp_pbuf_allocate(esp_netif, buffer, len, buffer);
if (p == NULL) {
esp_netif_free_rx_buffer(esp_netif, buffer);
return ESP_NETIF_OPTIONAL_RETURN_CODE(ESP_ERR_NO_MEM);
}{...}
if (unlikely(netif->input(p, netif) != ERR_OK)) {
LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: IP input error\n"));
pbuf_free(p);
return ESP_NETIF_OPTIONAL_RETURN_CODE(ESP_FAIL);
}{...}
return ESP_NETIF_OPTIONAL_RETURN_CODE(ESP_OK);
}{ ... }
/* ... */
err_t ethernetif_init(struct netif *netif)
{
LWIP_ASSERT("netif != NULL", (netif != NULL));
esp_netif_t *esp_netif = netif->state;
#if LWIP_NETIF_HOSTNAME
#if ESP_LWIP
if (esp_netif_get_hostname(esp_netif, &netif->hostname) != ESP_OK) {
netif->hostname = CONFIG_LWIP_LOCAL_HOSTNAME;
}{...}
#else/* ... */
netif->hostname = "lwip";
#endif
/* ... */
#endif
netif->name[0] = IFNAME0;
netif->name[1] = IFNAME1;
#if LWIP_IPV4
netif->output = etharp_output;
#endif
#if LWIP_IPV6
netif->output_ip6 = ethip6_output;
#endif
netif->linkoutput = ethernet_low_level_output;
ethernet_low_level_init(netif);
return ERR_OK;
}{ ... }