This repository has been archived by the owner on Jan 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
EthernetServer.cpp
320 lines (255 loc) · 7.75 KB
/
EthernetServer.cpp
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/****************************************************************************************************************************
EthernetServer.cpp
EthernetWebServer is a library for the Ethernet shields to run WebServer
Based on and modified from ESP8266 https://github.com/esp8266/Arduino/releases
Built by Khoi Hoang https://github.com/khoih-prog/EthernetWebServer
Licensed under MIT license
Version: 1.0.9
Copyright 2018 Paul Stoffregen
Permission is hereby granted, free of charge, to any person obtaining a copy of this
software and associated documentation files (the "Software"), to deal in the Software
without restriction, including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Version Modified By Date Comments
------- ----------- ---------- -----------
1.0.0 K Hoang 13/02/2020 Initial coding for Arduino Mega, Teensy, etc to support Ethernetx libraries
1.0.1 K Hoang 20/02/2020 Add support to lambda functions
1.0.2 K Hoang 20/02/2020 Add support to UIPEthernet library for ENC28J60
1.0.3 K Hoang 23/02/2020 Add support to SAM DUE / SAMD21 boards
1.0.4 K Hoang 16/04/2020 Add support to SAMD51 boards
1.0.5 K Hoang 24/04/2020 Add support to nRF52 boards, such as AdaFruit Feather nRF52832, nRF52840 Express, BlueFruit Sense,
Itsy-Bitsy nRF52840 Express, Metro nRF52840 Express, NINA_B30_ublox, etc.
More Custom Ethernet libraries supported such as Ethernet2, Ethernet3, EthernetLarge
1.0.6 K Hoang 27/04/2020 Add support to ESP32/ESP8266 boards
1.0.7 K Hoang 30/04/2020 Add ENC28J60 support to ESP32/ESP8266 boards
1.0.8 K Hoang 12/05/2020 Fix W5x00 support for ESP8266 boards.
1.0.9 K Hoang 15/05/2020 Add EthernetWrapper.h for easier W5x00 support as well as more Ethernet libs in the future.
*****************************************************************************************************************************/
#include <Arduino.h>
#include "EthernetLarge.h"
#include "utility/w5100.h"
uint16_t EthernetServer::server_port[MAX_SOCK_NUM];
void EthernetServer::begin()
{
uint8_t sockindex = Ethernet.socketBegin(SnMR::TCP, _port);
if (sockindex < MAX_SOCK_NUM)
{
if (Ethernet.socketListen(sockindex))
{
server_port[sockindex] = _port;
}
else
{
Ethernet.socketDisconnect(sockindex);
}
}
}
EthernetClient EthernetServer::available()
{
bool listening = false;
uint8_t sockindex = MAX_SOCK_NUM;
uint8_t chip, maxindex = MAX_SOCK_NUM;
chip = W5100.getChip();
if (!chip)
return EthernetClient(MAX_SOCK_NUM);
#if MAX_SOCK_NUM > 4
if (chip == 51)
maxindex = 4; // W5100 chip never supports more than 4 sockets
#endif
for (uint8_t i = 0; i < maxindex; i++)
{
if (server_port[i] == _port)
{
uint8_t stat = Ethernet.socketStatus(i);
if (stat == SnSR::ESTABLISHED || stat == SnSR::CLOSE_WAIT)
{
if (Ethernet.socketRecvAvailable(i) > 0)
{
sockindex = i;
}
else
{
// remote host closed connection, our end still open
if (stat == SnSR::CLOSE_WAIT)
{
Ethernet.socketDisconnect(i);
// status becomes LAST_ACK for short time
}
}
}
else if (stat == SnSR::LISTEN)
{
listening = true;
}
else if (stat == SnSR::CLOSED)
{
server_port[i] = 0;
}
}
}
if (!listening)
{
begin();
}
return EthernetClient(sockindex);
}
EthernetClient EthernetServer::accept()
{
bool listening = false;
uint8_t sockindex = MAX_SOCK_NUM;
uint8_t chip, maxindex = MAX_SOCK_NUM;
chip = W5100.getChip();
if (!chip)
return EthernetClient(MAX_SOCK_NUM);
#if MAX_SOCK_NUM > 4
if (chip == 51)
maxindex = 4; // W5100 chip never supports more than 4 sockets
#endif
for (uint8_t i = 0; i < maxindex; i++)
{
if (server_port[i] == _port)
{
uint8_t stat = Ethernet.socketStatus(i);
if (sockindex == MAX_SOCK_NUM && (stat == SnSR::ESTABLISHED || stat == SnSR::CLOSE_WAIT))
{
// Return the connected client even if no data received.
// Some protocols like FTP expect the server to send the
// first data.
sockindex = i;
server_port[i] = 0; // only return the client once
}
else if (stat == SnSR::LISTEN)
{
listening = true;
}
else if (stat == SnSR::CLOSED)
{
server_port[i] = 0;
}
}
}
if (!listening)
begin();
return EthernetClient(sockindex);
}
EthernetServer::operator bool()
{
uint8_t maxindex = MAX_SOCK_NUM;
#if MAX_SOCK_NUM > 4
if (W5100.getChip() == 51)
maxindex = 4; // W5100 chip never supports more than 4 sockets
#endif
for (uint8_t i = 0; i < maxindex; i++)
{
if (server_port[i] == _port)
{
if (Ethernet.socketStatus(i) == SnSR::LISTEN)
{
return true; // server is listening for incoming clients
}
}
}
return false;
}
#if 0
void EthernetServer::statusreport()
{
Serial.printf("EthernetServer, port=%d\n", _port);
for (uint8_t i = 0; i < MAX_SOCK_NUM; i++)
{
uint16_t port = server_port[i];
uint8_t stat = Ethernet.socketStatus(i);
const char *name;
switch (stat)
{
case 0x00:
name = "CLOSED";
break;
case 0x13:
name = "INIT";
break;
case 0x14:
name = "LISTEN";
break;
case 0x15:
name = "SYNSENT";
break;
case 0x16:
name = "SYNRECV";
break;
case 0x17:
name = "ESTABLISHED";
break;
case 0x18:
name = "FIN_WAIT";
break;
case 0x1A:
name = "CLOSING";
break;
case 0x1B:
name = "TIME_WAIT";
break;
case 0x1C:
name = "CLOSE_WAIT";
break;
case 0x1D:
name = "LAST_ACK";
break;
case 0x22:
name = "UDP";
break;
case 0x32:
name = "IPRAW";
break;
case 0x42:
name = "MACRAW";
break;
case 0x5F:
name = "PPPOE";
break;
default:
name = "???";
}
int avail = Ethernet.socketRecvAvailable(i);
Serial.printf(" %d: port=%d, status=%s (0x%02X), avail=%d\n",
i, port, name, stat, avail);
}
}
#endif
size_t EthernetServer::write(uint8_t b)
{
return write(&b, 1);
}
size_t EthernetServer::write(const uint8_t *buffer, size_t size)
{
uint8_t chip, maxindex = MAX_SOCK_NUM;
chip = W5100.getChip();
if (!chip)
return 0;
#if MAX_SOCK_NUM > 4
if (chip == 51)
maxindex = 4; // W5100 chip never supports more than 4 sockets
#endif
available();
for (uint8_t i = 0; i < maxindex; i++)
{
if (server_port[i] == _port)
{
if (Ethernet.socketStatus(i) == SnSR::ESTABLISHED)
{
Ethernet.socketSend(i, buffer, size);
}
}
}
return size;
}