99 Version: 1.0.9
1010
1111 Copyright 2018 Paul Stoffregen
12-
12+
1313 Permission is hereby granted, free of charge, to any person obtaining a copy of this
1414 software and associated documentation files (the "Software"), to deal in the Software
1515 without restriction, including without limitation the rights to use, copy, modify,
1616 merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
1717 permit persons to whom the Software is furnished to do so, subject to the following
1818 conditions:
19-
19+
2020 The above copyright notice and this permission notice shall be included in all
2121 copies or substantial portions of the Software.
22-
22+
2323 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
2424 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
2525 PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
3434 1.0.2 K Hoang 20/02/2020 Add support to UIPEthernet library for ENC28J60
3535 1.0.3 K Hoang 23/02/2020 Add support to SAM DUE / SAMD21 boards
3636 1.0.4 K Hoang 16/04/2020 Add support to SAMD51 boards
37- 1.0.5 K Hoang 24/04/2020 Add support to nRF52 boards, such as AdaFruit Feather nRF52832, nRF52840 Express, BlueFruit Sense,
38- Itsy-Bitsy nRF52840 Express, Metro nRF52840 Express, NINA_B30_ublox, etc.
37+ 1.0.5 K Hoang 24/04/2020 Add support to nRF52 boards, such as AdaFruit Feather nRF52832, nRF52840 Express, BlueFruit Sense,
38+ Itsy-Bitsy nRF52840 Express, Metro nRF52840 Express, NINA_B30_ublox, etc.
3939 More Custom Ethernet libraries supported such as Ethernet2, Ethernet3, EthernetLarge
40- 1.0.6 K Hoang 27/04/2020 Add support to ESP32/ESP8266 boards
41- 1.0.7 K Hoang 30/04/2020 Add ENC28J60 support to ESP32/ESP8266 boards
40+ 1.0.6 K Hoang 27/04/2020 Add support to ESP32/ESP8266 boards
41+ 1.0.7 K Hoang 30/04/2020 Add ENC28J60 support to ESP32/ESP8266 boards
4242 1.0.8 K Hoang 12/05/2020 Fix W5x00 support for ESP8266 boards.
4343 1.0.9 K Hoang 15/05/2020 Add EthernetWrapper.h for easier W5x00 support as well as more Ethernet libs in the future.
4444 *****************************************************************************************************************************/
@@ -52,193 +52,199 @@ uint16_t EthernetServer::server_port[MAX_SOCK_NUM];
5252
5353void EthernetServer::begin ()
5454{
55- uint8_t sockindex = Ethernet.socketBegin (SnMR::TCP, _port);
56- if (sockindex < MAX_SOCK_NUM)
57- {
58- if (Ethernet.socketListen (sockindex))
59- {
60- server_port[sockindex] = _port;
61- }
62- else
63- {
64- Ethernet.socketDisconnect (sockindex);
65- }
66- }
55+ uint8_t sockindex = Ethernet.socketBegin (SnMR::TCP, _port);
56+
57+ if (sockindex < MAX_SOCK_NUM)
58+ {
59+ if (Ethernet.socketListen (sockindex))
60+ {
61+ server_port[sockindex] = _port;
62+ }
63+ else
64+ {
65+ Ethernet.socketDisconnect (sockindex);
66+ }
67+ }
6768}
6869
6970EthernetClient EthernetServer::available ()
7071{
71- bool listening = false ;
72- uint8_t sockindex = MAX_SOCK_NUM;
73- uint8_t chip, maxindex= MAX_SOCK_NUM;
74-
75- chip = W5100.getChip ();
76-
77- if (!chip)
78- return EthernetClient (MAX_SOCK_NUM);
79-
80- // KH, set W5100 to max 2 sockets to increase buffer size
81- if (chip == 51 )
82- {
72+ bool listening = false ;
73+ uint8_t sockindex = MAX_SOCK_NUM;
74+ uint8_t chip, maxindex = MAX_SOCK_NUM;
75+
76+ chip = W5100.getChip ();
77+
78+ if (!chip)
79+ return EthernetClient (MAX_SOCK_NUM);
80+
81+ // KH, set W5100 to max 2 sockets to increase buffer size
82+ if (chip == 51 )
83+ {
8384#ifdef ETHERNET_LARGE_BUFFERS
84- maxindex = 2 ; // W5100 chip never supports more than 4 sockets
85- #else
86- maxindex = 4 ; // W5100 chip never supports more than 4 sockets. Original
87- #endif
88- }
89-
90- for (uint8_t i=0 ; i < maxindex; i++)
91- {
92- if (server_port[i] == _port)
93- {
94- uint8_t stat = Ethernet.socketStatus (i);
95- if (stat == SnSR::ESTABLISHED || stat == SnSR::CLOSE_WAIT)
96- {
97- if (Ethernet.socketRecvAvailable (i) > 0 )
98- {
99- sockindex = i;
100- }
101- else
102- {
103- // remote host closed connection, our end still open
104- if (stat == SnSR::CLOSE_WAIT)
105- {
106- Ethernet.socketDisconnect (i);
107- // status becomes LAST_ACK for short time
108- }
109- }
110- }
111- else if (stat == SnSR::LISTEN)
112- {
113- listening = true ;
114- }
115- else if (stat == SnSR::CLOSED)
116- {
117- server_port[i] = 0 ;
118- }
119- }
120- }
121-
122- if (!listening)
123- {
124- begin ();
125- }
126-
127- return EthernetClient (sockindex);
85+ maxindex = 2 ; // W5100 chip never supports more than 4 sockets
86+ #else
87+ maxindex = 4 ; // W5100 chip never supports more than 4 sockets. Original
88+ #endif
89+ }
90+
91+ for (uint8_t i = 0 ; i < maxindex; i++)
92+ {
93+ if (server_port[i] == _port)
94+ {
95+ uint8_t stat = Ethernet.socketStatus (i);
96+
97+ if (stat == SnSR::ESTABLISHED || stat == SnSR::CLOSE_WAIT)
98+ {
99+ if (Ethernet.socketRecvAvailable (i) > 0 )
100+ {
101+ sockindex = i;
102+ }
103+ else
104+ {
105+ // remote host closed connection, our end still open
106+ if (stat == SnSR::CLOSE_WAIT)
107+ {
108+ Ethernet.socketDisconnect (i);
109+ // status becomes LAST_ACK for short time
110+ }
111+ }
112+ }
113+ else if (stat == SnSR::LISTEN)
114+ {
115+ listening = true ;
116+ }
117+ else if (stat == SnSR::CLOSED)
118+ {
119+ server_port[i] = 0 ;
120+ }
121+ }
122+ }
123+
124+ if (!listening)
125+ {
126+ begin ();
127+ }
128+
129+ return EthernetClient (sockindex);
128130}
129131
130132EthernetClient EthernetServer::accept ()
131133{
132- bool listening = false ;
133- uint8_t sockindex = MAX_SOCK_NUM;
134- uint8_t chip, maxindex=MAX_SOCK_NUM;
134+ bool listening = false ;
135+ uint8_t sockindex = MAX_SOCK_NUM;
136+ uint8_t chip, maxindex = MAX_SOCK_NUM;
137+
138+ chip = W5100.getChip ();
135139
136- chip = W5100.getChip ();
137-
138- if (!chip)
139- return EthernetClient (MAX_SOCK_NUM);
140+ if (!chip)
141+ return EthernetClient (MAX_SOCK_NUM);
140142
141143 // KH, set W5100 to max 2 sockets to increase buffer size
142- if (chip == 51 )
143- {
144+ if (chip == 51 )
145+ {
144146#ifdef ETHERNET_LARGE_BUFFERS
145- maxindex = 2 ; // W5100 chip never supports more than 4 sockets
146- #else
147- maxindex = 4 ; // W5100 chip never supports more than 4 sockets. Original
148- #endif
149- }
150-
151- for (uint8_t i= 0 ; i < maxindex; i++)
152- {
153- if (server_port[i] == _port)
154- {
155- uint8_t stat = Ethernet.socketStatus (i);
156-
157- if (sockindex == MAX_SOCK_NUM && (stat == SnSR::ESTABLISHED || stat == SnSR::CLOSE_WAIT))
158- {
159- // Return the connected client even if no data received.
160- // Some protocols like FTP expect the server to send the
161- // first data.
162- sockindex = i;
163- server_port[i] = 0 ; // only return the client once
164- }
165- else if (stat == SnSR::LISTEN)
147+ maxindex = 2 ; // W5100 chip never supports more than 4 sockets
148+ #else
149+ maxindex = 4 ; // W5100 chip never supports more than 4 sockets. Original
150+ #endif
151+ }
152+
153+ for (uint8_t i = 0 ; i < maxindex; i++)
154+ {
155+ if (server_port[i] == _port)
156+ {
157+ uint8_t stat = Ethernet.socketStatus (i);
158+
159+ if (sockindex == MAX_SOCK_NUM && (stat == SnSR::ESTABLISHED || stat == SnSR::CLOSE_WAIT))
160+ {
161+ // Return the connected client even if no data received.
162+ // Some protocols like FTP expect the server to send the
163+ // first data.
164+ sockindex = i;
165+ server_port[i] = 0 ; // only return the client once
166+ }
167+ else if (stat == SnSR::LISTEN)
166168 {
167- listening = true ;
168- }
169- else if (stat == SnSR::CLOSED)
170- {
171- server_port[i] = 0 ;
172- }
173- }
174- }
175-
176- if (!listening)
177- begin ();
178-
179- return EthernetClient (sockindex);
169+ listening = true ;
170+ }
171+ else if (stat == SnSR::CLOSED)
172+ {
173+ server_port[i] = 0 ;
174+ }
175+ }
176+ }
177+
178+ if (!listening)
179+ begin ();
180+
181+ return EthernetClient (sockindex);
180182}
181183
182184EthernetServer::operator bool ()
183185{
184- uint8_t maxindex= MAX_SOCK_NUM;
186+ uint8_t maxindex = MAX_SOCK_NUM;
185187
186188 // KH, set W5100 to max 2 sockets to increase buffer size
187- if (W5100.getChip () == 51 )
188- {
189+ if (W5100.getChip () == 51 )
190+ {
189191#ifdef ETHERNET_LARGE_BUFFERS
190- maxindex = 2 ; // W5100 chip never supports more than 4 sockets
191- #else
192- maxindex = 4 ; // W5100 chip never supports more than 4 sockets. Original
193- #endif
194- }
195-
196- for (uint8_t i=0 ; i < maxindex; i++)
197- {
198- if (server_port[i] == _port)
199- {
200- if (Ethernet.socketStatus (i) == SnSR::LISTEN)
201- {
202- return true ; // server is listening for incoming clients
203- }
204- }
205- }
206- return false ;
192+ maxindex = 2 ; // W5100 chip never supports more than 4 sockets
193+ #else
194+ maxindex = 4 ; // W5100 chip never supports more than 4 sockets. Original
195+ #endif
196+ }
197+
198+ for (uint8_t i = 0 ; i < maxindex; i++)
199+ {
200+ if (server_port[i] == _port)
201+ {
202+ if (Ethernet.socketStatus (i) == SnSR::LISTEN)
203+ {
204+ return true ; // server is listening for incoming clients
205+ }
206+ }
207+ }
208+
209+ return false ;
207210}
208211
209212size_t EthernetServer::write (uint8_t b)
210213{
211- return write (&b, 1 );
214+ return write (&b, 1 );
212215}
213216
214217size_t EthernetServer::write (const uint8_t *buffer, size_t size)
215218{
216- uint8_t chip, maxindex=MAX_SOCK_NUM;
219+ uint8_t chip, maxindex = MAX_SOCK_NUM;
220+
221+ chip = W5100.getChip ();
217222
218- chip = W5100. getChip ();
219- if (!chip) return 0 ;
223+ if (!chip)
224+ return 0 ;
220225
221226 // KH, set W5100 to max 2 sockets to increase buffer size
222- if (chip == 51 )
223- {
227+ if (chip == 51 )
228+ {
224229#ifdef ETHERNET_LARGE_BUFFERS
225- maxindex = 2 ; // W5100 chip never supports more than 4 sockets
226- #else
227- maxindex = 4 ; // W5100 chip never supports more than 4 sockets. Original
228- #endif
229- }
230-
231- available ();
232-
233- for (uint8_t i=0 ; i < maxindex; i++)
234- {
235- if (server_port[i] == _port)
236- {
237- if (Ethernet.socketStatus (i) == SnSR::ESTABLISHED)
238- {
239- Ethernet.socketSend (i, buffer, size);
240- }
241- }
242- }
243- return size;
230+ maxindex = 2 ; // W5100 chip never supports more than 4 sockets
231+ #else
232+ maxindex = 4 ; // W5100 chip never supports more than 4 sockets. Original
233+ #endif
234+ }
235+
236+ available ();
237+
238+ for (uint8_t i = 0 ; i < maxindex; i++)
239+ {
240+ if (server_port[i] == _port)
241+ {
242+ if (Ethernet.socketStatus (i) == SnSR::ESTABLISHED)
243+ {
244+ Ethernet.socketSend (i, buffer, size);
245+ }
246+ }
247+ }
248+
249+ return size;
244250}
0 commit comments