Skip to content

Commit b9dec89

Browse files
2 parents 6d9ce35 + 57c90c6 commit b9dec89

File tree

1 file changed

+76
-55
lines changed

1 file changed

+76
-55
lines changed

src/Ethernet.h

Lines changed: 76 additions & 55 deletions
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,14 +211,12 @@ 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);
@@ -239,6 +240,28 @@ class EthernetClient : public Client {
239240
setConnectionTimeout(timeout);
240241
return 1;
241242
}
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+
}
242265
#endif
243266
virtual int availableForWrite(void);
244267
virtual size_t write(uint8_t);
@@ -253,8 +276,8 @@ class EthernetClient : public Client {
253276
virtual operator bool() { return _sockindex < MAX_SOCK_NUM; }
254277
virtual bool operator==(const bool value) { return bool() == value; }
255278
virtual bool operator!=(const bool value) { return bool() != value; }
256-
virtual bool operator==(const EthernetClient&);
257-
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); }
258281
uint8_t getSocketNumber() const { return _sockindex; }
259282
virtual uint16_t localPort();
260283
virtual IPAddress remoteIP();
@@ -270,12 +293,13 @@ class EthernetClient : public Client {
270293
uint16_t _timeout;
271294
};
272295

273-
274-
class EthernetServer : public Server {
296+
class EthernetServer : public Server
297+
{
275298
private:
276299
uint16_t _port;
300+
277301
public:
278-
EthernetServer(uint16_t port) : _port(port) { }
302+
EthernetServer(uint16_t port) : _port(port) {}
279303
EthernetClient available();
280304
EthernetClient accept();
281305
virtual void begin();
@@ -289,30 +313,30 @@ class EthernetServer : public Server {
289313
virtual size_t write(const uint8_t *buf, size_t size);
290314
virtual operator bool();
291315
using Print::write;
292-
//void statusreport();
316+
// void statusreport();
293317

294318
// TODO: make private when socket allocation moves to EthernetClass
295319
static uint16_t server_port[MAX_SOCK_NUM];
296320
};
297321

298-
299-
class DhcpClass {
322+
class DhcpClass
323+
{
300324
private:
301325
uint32_t _dhcpInitialTransactionId;
302326
uint32_t _dhcpTransactionId;
303-
uint8_t _dhcpMacAddr[6];
327+
uint8_t _dhcpMacAddr[6];
304328
#ifdef __arm__
305-
uint8_t _dhcpLocalIp[4] __attribute__((aligned(4)));
306-
uint8_t _dhcpSubnetMask[4] __attribute__((aligned(4)));
307-
uint8_t _dhcpGatewayIp[4] __attribute__((aligned(4)));
308-
uint8_t _dhcpDhcpServerIp[4] __attribute__((aligned(4)));
309-
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)));
310334
#else
311-
uint8_t _dhcpLocalIp[4];
312-
uint8_t _dhcpSubnetMask[4];
313-
uint8_t _dhcpGatewayIp[4];
314-
uint8_t _dhcpDhcpServerIp[4];
315-
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];
316340
#endif
317341
uint32_t _dhcpLeaseTime;
318342
uint32_t _dhcpT1, _dhcpT2;
@@ -330,7 +354,8 @@ class DhcpClass {
330354
void send_DHCP_MESSAGE(uint8_t, uint16_t);
331355
void printByte(char *, uint8_t);
332356

333-
uint8_t parseDHCPResponse(unsigned long responseTimeout, uint32_t& transactionId);
357+
uint8_t parseDHCPResponse(unsigned long responseTimeout, uint32_t &transactionId);
358+
334359
public:
335360
IPAddress getLocalIp();
336361
IPAddress getSubnetMask();
@@ -342,8 +367,4 @@ class DhcpClass {
342367
int checkLease();
343368
};
344369

345-
346-
347-
348-
349370
#endif

0 commit comments

Comments
 (0)