This repository has been archived by the owner on Dec 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathlwIPeth.h
293 lines (238 loc) · 8.06 KB
/
lwIPeth.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#ifndef _LWIPETH_H
#define _LWIPETH_H
// TODO:
// remove all Serial.print
// unchain pbufs
#include <netif/ethernet.h>
#include <lwip/init.h>
#include <lwip/netif.h>
#include <lwip/etharp.h>
#include <lwip/dhcp.h>
#ifdef ESP8266
#include <user_interface.h> // wifi_get_macaddr()
#include <Schedule.h>
#endif
#include <SPI.h>
#include <IPAddress.h>
#include <FunctionalInterrupt.h>
#ifndef DEFAULT_MTU
#define DEFAULT_MTU 1500
#endif
template <class RawEthernet>
class LwipEthernet: public RawEthernet {
public:
LwipEthernet (SPIClass& spi = SPI, int8_t cs = SS, int8_t intr = -1):
RawEthernet(spi, cs, intr),
_mtu(DEFAULT_MTU),
_default(false),
_intrPin(intr)
{
memset(&_netif, 0, sizeof(_netif));
}
// start with dhcp client
// default mac-address is inferred from esp8266's STA interface
boolean begin (const uint8_t *macAddress = nullptr, uint16_t mtu = DEFAULT_MTU);
const netif* getNetIf () const { return &_netif; }
#if LWIP_VERSION_MAJOR == 1
IPAddress localIP () const { return IPAddress(_netif.ip_addr.u_addr.ip4.addr); }
IPAddress subnetMask () const { return IPAddress(_netif.netmask.u_addr.ip4.addr); }
IPAddress gatewayIP () const { return IPAddress(_netif.gw.u_addr.ip4.addr); }
#else
IPAddress localIP () const { return IPAddress(ip4_addr_get_u32(&_netif.ip_addr)); }
IPAddress subnetMask () const { return IPAddress(ip4_addr_get_u32(&_netif.netmask)); }
IPAddress gatewayIP () const { return IPAddress(ip4_addr_get_u32(&_netif.gw)); }
#endif
void setDefault ();
bool connected () { return !!ip4_addr_get_u32(&_netif.ip_addr); }
protected:
netif _netif;
uint16_t _mtu;
bool _default;
int8_t _intrPin;
uint8_t _macAddress[6];
err_t start_with_dhclient ();
err_t netif_init ();
void netif_status_callback ();
static err_t netif_init_s (netif* netif);
static err_t linkoutput_s (netif *netif, struct pbuf *p);
static void netif_status_callback_s (netif* netif);
// called on a regular basis or on interrupt
err_t handlePackets ();
};
template <class RawEthernet>
boolean LwipEthernet<RawEthernet>::begin (const uint8_t* macAddress, uint16_t mtu)
{
if (macAddress)
memcpy(_macAddress, macAddress, 6);
else
{
#ifdef ESP8266
// make a new mac-address from the esp's wifi sta one
// I understand this is cheating with an official mac-address
wifi_get_macaddr(STATION_IF, (uint8*)_macAddress);
#else
// https://serverfault.com/questions/40712/what-range-of-mac-addresses-can-i-safely-use-for-my-virtual-machines
// TODO ESP32: get wifi mac address like with esp8266 above
memset(_macAddress, 0, 6);
_macAddress[0] = 0xEE;
#endif
_macAddress[3] += _netif.num;
memcpy(_netif.hwaddr, _macAddress, 6);
}
if (!RawEthernet::begin(_macAddress))
return false;
_mtu = mtu;
switch (start_with_dhclient())
{
case ERR_OK:
break;
case ERR_IF:
return false;
default:
netif_remove(&_netif);
return false;
}
if (_intrPin >= 0)
{
::printf((PGM_P)F("w500-lwIP: Interrupt not implemented yet, enabling transparent polling\r\n"));
// still need to enable interrupt in wiznet driver
_intrPin = -1;
}
if (_intrPin >= 0)
attachInterrupt(_intrPin, [&]() { this->handlePackets(); }, FALLING);
else if (!schedule_recurrent_function_us([&]() { this->handlePackets(); return true; }, 100))
{
netif_remove(&_netif);
return false;
}
return true;
}
template <class RawEthernet>
err_t LwipEthernet<RawEthernet>::start_with_dhclient ()
{
ip4_addr_t ip, mask, gw;
ip4_addr_set_zero(&ip);
ip4_addr_set_zero(&mask);
ip4_addr_set_zero(&gw);
_netif.hwaddr_len = sizeof _macAddress;
memcpy(_netif.hwaddr, _macAddress, sizeof _macAddress);
if (!netif_add(&_netif, &ip, &mask, &gw, this, netif_init_s, ethernet_input))
return ERR_IF;
_netif.flags |= NETIF_FLAG_UP;
return dhcp_start(&_netif);
}
template <class RawEthernet>
err_t LwipEthernet<RawEthernet>::linkoutput_s (netif *netif, struct pbuf *pbuf)
{
LwipEthernet* ths = (LwipEthernet*)netif->state;
#ifdef ESP8266
if (pbuf->len != pbuf->tot_len || pbuf->next)
Serial.println("ERRTOT\r\n");
#endif
uint16_t len = ths->sendFrame((const uint8_t*)pbuf->payload, pbuf->len);
#if defined(ESP8266) && PHY_HAS_CAPTURE
if (phy_capture)
phy_capture(ths->_netif.num, (const char*)pbuf->payload, pbuf->len, /*out*/1, /*success*/len == pbuf->len);
#endif
return len == pbuf->len? ERR_OK: ERR_MEM;
}
template <class RawEthernet>
err_t LwipEthernet<RawEthernet>::netif_init_s (struct netif* netif)
{
return ((LwipEthernet*)netif->state)->netif_init();
}
template <class RawEthernet>
void LwipEthernet<RawEthernet>::netif_status_callback_s (struct netif* netif)
{
((LwipEthernet*)netif->state)->netif_status_callback();
}
template <class RawEthernet>
err_t LwipEthernet<RawEthernet>::netif_init ()
{
_netif.name[0] = 'e';
_netif.name[1] = '0' + _netif.num;
_netif.mtu = _mtu;
_netif.chksum_flags = NETIF_CHECKSUM_ENABLE_ALL;
_netif.flags =
NETIF_FLAG_ETHARP
| NETIF_FLAG_IGMP
| NETIF_FLAG_BROADCAST
| NETIF_FLAG_LINK_UP;
// lwIP's doc: This function typically first resolves the hardware
// address, then sends the packet. For ethernet physical layer, this is
// usually lwIP's etharp_output()
_netif.output = etharp_output;
// lwIP's doc: This function outputs the pbuf as-is on the link medium
// (this must points to the raw ethernet driver, meaning: us)
_netif.linkoutput = linkoutput_s;
_netif.status_callback = netif_status_callback_s;
return ERR_OK;
}
template <class RawEthernet>
void LwipEthernet<RawEthernet>::netif_status_callback ()
{
//XXX is it wise ?
if (_default && connected())
netif_set_default(&_netif);
else if (netif_default == &_netif && !connected())
netif_set_default(nullptr);
}
template <class RawEthernet>
err_t LwipEthernet<RawEthernet>::handlePackets ()
{
int pkt = 0;
while(1)
{
if (++pkt == 10)
// prevent starvation
return ERR_OK;
uint16_t tot_len = RawEthernet::readFrameSize();
if (!tot_len)
return ERR_OK;
// from doc: use PBUF_RAM for TX, PBUF_POOL from RX
// however:
// PBUF_POOL can return chained pbuf (not in one piece)
// and WiznetDriver does not have the proper API to deal with that
// so in the meantime, we use PBUF_RAM instead which is currently
// guarantying to deliver a continuous chunk of memory.
// TODO: tweak the wiznet driver to allow copying partial chunk
// of received data and use PBUF_POOL.
pbuf* pbuf = pbuf_alloc(PBUF_RAW, tot_len, PBUF_RAM);
if (!pbuf || pbuf->len < tot_len)
{
if (pbuf)
pbuf_free(pbuf);
RawEthernet::discardFrame(tot_len);
return ERR_BUF;
}
uint16_t len = RawEthernet::readFrameData((uint8_t*)pbuf->payload, tot_len);
if (len != tot_len)
{
// tot_len is given by readFrameSize()
// and is supposed to be honoured by readFrameData()
// todo: ensure this test is unneeded, remove the print
Serial.println("read error?\r\n");
pbuf_free(pbuf);
return ERR_BUF;
}
err_t err = _netif.input(pbuf, &_netif);
#if defined(ESP8266) && PHY_HAS_CAPTURE
if (phy_capture)
phy_capture(_netif.num, (const char*)pbuf->payload, tot_len, /*out*/0, /*success*/err == ERR_OK);
#endif
if (err != ERR_OK)
{
pbuf_free(pbuf);
return err;
}
// (else) allocated pbuf is now on lwIP's responsibility
}
}
template <class RawEthernet>
void LwipEthernet<RawEthernet>::setDefault ()
{
_default = true;
if (connected())
netif_set_default(&_netif);
}
#endif // _LWIPETH_H