Skip to content

Commit c32ab4e

Browse files
Update Ethernet.h
1 parent 78aeaf5 commit c32ab4e

File tree

1 file changed

+103
-56
lines changed

1 file changed

+103
-56
lines changed

src/Ethernet.h

+103-56
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
// these "friend" classes are now defined in the same header file. socket.h
2929
// was removed to avoid possible conflict with the C library header files.
3030

31-
3231
// Configure the maximum number of sockets to support. W5100 chips can have
3332
// up to 4 sockets. W5200 & W5500 can have up to 8 sockets. Several bytes
3433
// of RAM are used for each socket. Reducing the maximum can save RAM, but
@@ -45,21 +44,22 @@
4544
// can really help with UDP protocols like Artnet. In theory larger
4645
// buffers should allow faster TCP over high-latency links, but this
4746
// does not always seem to work in practice (maybe WIZnet bugs?)
48-
//#define ETHERNET_LARGE_BUFFERS
49-
47+
// #define ETHERNET_LARGE_BUFFERS
5048

5149
#include <Arduino.h>
5250
#include "Client.h"
5351
#include "Server.h"
5452
#include "Udp.h"
5553

56-
enum EthernetLinkStatus {
54+
enum EthernetLinkStatus
55+
{
5756
Unknown,
5857
LinkON,
5958
LinkOFF
6059
};
6160

62-
enum EthernetHardwareStatus {
61+
enum EthernetHardwareStatus
62+
{
6363
EthernetNoHardware,
6464
EthernetW5100,
6565
EthernetW5200,
@@ -71,10 +71,12 @@ class EthernetClient;
7171
class EthernetServer;
7272
class DhcpClass;
7373

74-
class EthernetClass {
74+
class EthernetClass
75+
{
7576
private:
7677
static IPAddress _dnsServerAddress;
77-
static DhcpClass* _dhcp;
78+
static DhcpClass *_dhcp;
79+
7880
public:
7981
// Initialise the Ethernet shield to use the provided MAC address and
8082
// gain the rest of the configuration through DHCP.
@@ -108,34 +110,35 @@ class EthernetClass {
108110
friend class EthernetClient;
109111
friend class EthernetServer;
110112
friend class EthernetUDP;
113+
111114
private:
112115
// Opens a socket(TCP or UDP or IP_RAW mode)
113116
static uint8_t socketBegin(uint8_t protocol, uint16_t port);
114-
static uint8_t socketBeginMulticast(uint8_t protocol, IPAddress ip,uint16_t port);
117+
static uint8_t socketBeginMulticast(uint8_t protocol, IPAddress ip, uint16_t port);
115118
static uint8_t socketStatus(uint8_t s);
116119
// Close socket
117120
static void socketClose(uint8_t s);
118121
// Establish TCP connection (Active connection)
119-
static void socketConnect(uint8_t s, uint8_t * addr, uint16_t port);
122+
static void socketConnect(uint8_t s, uint8_t *addr, uint16_t port);
120123
// disconnect the connection
121124
static void socketDisconnect(uint8_t s);
122125
// Establish TCP connection (Passive connection)
123126
static uint8_t socketListen(uint8_t s);
124127
// Send data (TCP)
125-
static uint16_t socketSend(uint8_t s, const uint8_t * buf, uint16_t len);
128+
static uint16_t socketSend(uint8_t s, const uint8_t *buf, uint16_t len);
126129
static uint16_t socketSendAvailable(uint8_t s);
127130
// Receive data (TCP)
128-
static int socketRecv(uint8_t s, uint8_t * buf, int16_t len);
131+
static int socketRecv(uint8_t s, uint8_t *buf, int16_t len);
129132
static uint16_t socketRecvAvailable(uint8_t s);
130133
static uint8_t socketPeek(uint8_t s);
131134
// sets up a UDP datagram, the data for which will be provided by one
132135
// or more calls to bufferData and then finally sent with sendUDP.
133136
// return true if the datagram was successfully set up, or false if there was an error
134-
static bool socketStartUDP(uint8_t s, uint8_t* addr, uint16_t port);
137+
static bool socketStartUDP(uint8_t s, uint8_t *addr, uint16_t port);
135138
// copy up to len bytes of data from buf into a UDP datagram to be
136139
// sent later by sendUDP. Allows datagrams to be built up from a series of bufferData calls.
137140
// return Number of bytes successfully buffered
138-
static uint16_t socketBufferData(uint8_t s, uint16_t offset, const uint8_t* buf, uint16_t len);
141+
static uint16_t socketBufferData(uint8_t s, uint16_t offset, const uint8_t *buf, uint16_t len);
139142
// Send a UDP datagram built up from a sequence of startUDP followed by one or more
140143
// calls to bufferData.
141144
// return true if the datagram was successfully sent, or false if there was an error
@@ -146,25 +149,25 @@ class EthernetClass {
146149

147150
extern EthernetClass Ethernet;
148151

149-
150152
#define UDP_TX_PACKET_MAX_SIZE 24
151153

152-
class EthernetUDP : public UDP {
154+
class EthernetUDP : public UDP
155+
{
153156
private:
154-
uint16_t _port; // local port to listen on
155-
IPAddress _remoteIP; // remote IP address for the incoming packet whilst it's being processed
157+
uint16_t _port; // local port to listen on
158+
IPAddress _remoteIP; // remote IP address for the incoming packet whilst it's being processed
156159
uint16_t _remotePort; // remote port for the incoming packet whilst it's being processed
157-
uint16_t _offset; // offset into the packet being sent
160+
uint16_t _offset; // offset into the packet being sent
158161

159162
protected:
160163
uint8_t sockindex;
161164
uint16_t _remaining; // remaining bytes of incoming packet yet to be processed
162165

163166
public:
164-
EthernetUDP() : sockindex(MAX_SOCK_NUM) {} // Constructor
165-
virtual uint8_t begin(uint16_t); // initialize, start listening on specified port. Returns 1 if successful, 0 if there are no sockets available to use
166-
virtual uint8_t beginMulticast(IPAddress, uint16_t); // initialize, start listening on specified port. Returns 1 if successful, 0 if there are no sockets available to use
167-
virtual void stop(); // Finish with the UDP socket
167+
EthernetUDP() : sockindex(MAX_SOCK_NUM) {} // Constructor
168+
virtual uint8_t begin(uint16_t); // initialize, start listening on specified port. Returns 1 if successful, 0 if there are no sockets available to use
169+
virtual uint8_t beginMulticast(IPAddress, uint16_t); // initialize, start listening on specified port. Returns 1 if successful, 0 if there are no sockets available to use
170+
virtual void stop(); // Finish with the UDP socket
168171

169172
// Sending UDP packets
170173

@@ -193,10 +196,10 @@ class EthernetUDP : public UDP {
193196
virtual int read();
194197
// Read up to len bytes from the current packet and place them into buffer
195198
// Returns the number of bytes read, or 0 if none are available
196-
virtual int read(unsigned char* buffer, size_t len);
199+
virtual int read(unsigned char *buffer, size_t len);
197200
// Read up to len characters from the current packet and place them into buffer
198201
// Returns the number of characters read, or 0 if none are available
199-
virtual int read(char* buffer, size_t len) { return read((unsigned char*)buffer, len); };
202+
virtual int read(char *buffer, size_t len) { return read((unsigned char *)buffer, len); };
200203
// Return the next byte from the current packet without moving on to the next byte
201204
virtual int peek();
202205
virtual void flush(); // Finish reading the current packet
@@ -208,18 +211,58 @@ class EthernetUDP : public UDP {
208211
virtual uint16_t localPort() { return _port; }
209212
};
210213

211-
212-
213-
214-
class EthernetClient : public Client {
214+
class EthernetClient : public Client
215+
{
215216
public:
216-
EthernetClient() : _sockindex(MAX_SOCK_NUM), _timeout(1000) { }
217-
EthernetClient(uint8_t s) : _sockindex(s), _timeout(1000) { }
218-
virtual ~EthernetClient() {};
217+
EthernetClient() : _sockindex(MAX_SOCK_NUM), _timeout(1000) {}
218+
EthernetClient(uint8_t s) : _sockindex(s), _timeout(1000) {}
219+
virtual ~EthernetClient(){};
219220

220221
uint8_t status();
221222
virtual int connect(IPAddress ip, uint16_t port);
222223
virtual int connect(const char *host, uint16_t port);
224+
#ifdef ESP32
225+
int connect(IPAddress ip, uint16_t port, int timeout)
226+
{
227+
//possible implemented return values of return are only 0 (error) or 1 (ok).
228+
if(!connect(ip, port)) {
229+
//indicate error
230+
return 0;
231+
}
232+
setConnectionTimeout(timeout);
233+
return 1;
234+
}
235+
int connect(const char* host, uint16_t port, int timeout)
236+
{
237+
if(!connect(host, port)) {
238+
return 0;
239+
}
240+
setConnectionTimeout(timeout);
241+
return 1;
242+
}
243+
#endif
244+
#ifdef ESP32
245+
int connect(IPAddress ip, uint16_t port, int timeout)
246+
{
247+
// possible implemented return values of return are only 0 (error) or 1 (ok).
248+
if (!connect(ip, port))
249+
{
250+
// indicate error
251+
return 0;
252+
}
253+
setConnectionTimeout(timeout);
254+
return 1;
255+
}
256+
int connect(const char *host, uint16_t port, int timeout)
257+
{
258+
if (!connect(host, port))
259+
{
260+
return 0;
261+
}
262+
setConnectionTimeout(timeout);
263+
return 1;
264+
}
265+
#endif
223266
virtual int availableForWrite(void);
224267
virtual size_t write(uint8_t);
225268
virtual size_t write(const uint8_t *buf, size_t size);
@@ -233,8 +276,8 @@ class EthernetClient : public Client {
233276
virtual operator bool() { return _sockindex < MAX_SOCK_NUM; }
234277
virtual bool operator==(const bool value) { return bool() == value; }
235278
virtual bool operator!=(const bool value) { return bool() != value; }
236-
virtual bool operator==(const EthernetClient&);
237-
virtual bool operator!=(const EthernetClient& rhs) { return !this->operator==(rhs); }
279+
virtual bool operator==(const EthernetClient &);
280+
virtual bool operator!=(const EthernetClient &rhs) { return !this->operator==(rhs); }
238281
uint8_t getSocketNumber() const { return _sockindex; }
239282
virtual uint16_t localPort();
240283
virtual IPAddress remoteIP();
@@ -250,43 +293,50 @@ class EthernetClient : public Client {
250293
uint16_t _timeout;
251294
};
252295

253-
254-
class EthernetServer : public Server {
296+
class EthernetServer : public Server
297+
{
255298
private:
256299
uint16_t _port;
300+
257301
public:
258-
EthernetServer(uint16_t port) : _port(port) { }
302+
EthernetServer(uint16_t port) : _port(port) {}
259303
EthernetClient available();
260304
EthernetClient accept();
261305
virtual void begin();
306+
#ifdef ESP32
307+
void begin(uint16_t port)
308+
{
309+
_port = port;
310+
}
311+
#endif
262312
virtual size_t write(uint8_t);
263313
virtual size_t write(const uint8_t *buf, size_t size);
264314
virtual operator bool();
265315
using Print::write;
266-
//void statusreport();
316+
// void statusreport();
267317

268318
// TODO: make private when socket allocation moves to EthernetClass
269319
static uint16_t server_port[MAX_SOCK_NUM];
270320
};
271321

272-
273-
class DhcpClass {
322+
class DhcpClass
323+
{
274324
private:
275325
uint32_t _dhcpInitialTransactionId;
276326
uint32_t _dhcpTransactionId;
277-
uint8_t _dhcpMacAddr[6];
327+
uint8_t _dhcpMacAddr[6];
278328
#ifdef __arm__
279-
uint8_t _dhcpLocalIp[4] __attribute__((aligned(4)));
280-
uint8_t _dhcpSubnetMask[4] __attribute__((aligned(4)));
281-
uint8_t _dhcpGatewayIp[4] __attribute__((aligned(4)));
282-
uint8_t _dhcpDhcpServerIp[4] __attribute__((aligned(4)));
283-
uint8_t _dhcpDnsServerIp[4] __attribute__((aligned(4)));
329+
uint8_t _dhcpLocalIp[4] __attribute__((aligned(4)));
330+
uint8_t _dhcpSubnetMask[4] __attribute__((aligned(4)));
331+
uint8_t _dhcpGatewayIp[4] __attribute__((aligned(4)));
332+
uint8_t _dhcpDhcpServerIp[4] __attribute__((aligned(4)));
333+
uint8_t _dhcpDnsServerIp[4] __attribute__((aligned(4)));
284334
#else
285-
uint8_t _dhcpLocalIp[4];
286-
uint8_t _dhcpSubnetMask[4];
287-
uint8_t _dhcpGatewayIp[4];
288-
uint8_t _dhcpDhcpServerIp[4];
289-
uint8_t _dhcpDnsServerIp[4];
335+
uint8_t _dhcpLocalIp[4];
336+
uint8_t _dhcpSubnetMask[4];
337+
uint8_t _dhcpGatewayIp[4];
338+
uint8_t _dhcpDhcpServerIp[4];
339+
uint8_t _dhcpDnsServerIp[4];
290340
#endif
291341
uint32_t _dhcpLeaseTime;
292342
uint32_t _dhcpT1, _dhcpT2;
@@ -304,7 +354,8 @@ class DhcpClass {
304354
void send_DHCP_MESSAGE(uint8_t, uint16_t);
305355
void printByte(char *, uint8_t);
306356

307-
uint8_t parseDHCPResponse(unsigned long responseTimeout, uint32_t& transactionId);
357+
uint8_t parseDHCPResponse(unsigned long responseTimeout, uint32_t &transactionId);
358+
308359
public:
309360
IPAddress getLocalIp();
310361
IPAddress getSubnetMask();
@@ -316,8 +367,4 @@ class DhcpClass {
316367
int checkLease();
317368
};
318369

319-
320-
321-
322-
323-
#endif
370+
#endif

0 commit comments

Comments
 (0)