28
28
// these "friend" classes are now defined in the same header file. socket.h
29
29
// was removed to avoid possible conflict with the C library header files.
30
30
31
-
32
31
// Configure the maximum number of sockets to support. W5100 chips can have
33
32
// up to 4 sockets. W5200 & W5500 can have up to 8 sockets. Several bytes
34
33
// of RAM are used for each socket. Reducing the maximum can save RAM, but
45
44
// can really help with UDP protocols like Artnet. In theory larger
46
45
// buffers should allow faster TCP over high-latency links, but this
47
46
// does not always seem to work in practice (maybe WIZnet bugs?)
48
- // #define ETHERNET_LARGE_BUFFERS
49
-
47
+ // #define ETHERNET_LARGE_BUFFERS
50
48
51
49
#include < Arduino.h>
52
50
#include " Client.h"
53
51
#include " Server.h"
54
52
#include " Udp.h"
55
53
56
- enum EthernetLinkStatus {
54
+ enum EthernetLinkStatus
55
+ {
57
56
Unknown,
58
57
LinkON,
59
58
LinkOFF
60
59
};
61
60
62
- enum EthernetHardwareStatus {
61
+ enum EthernetHardwareStatus
62
+ {
63
63
EthernetNoHardware,
64
64
EthernetW5100,
65
65
EthernetW5200,
@@ -71,10 +71,12 @@ class EthernetClient;
71
71
class EthernetServer ;
72
72
class DhcpClass ;
73
73
74
- class EthernetClass {
74
+ class EthernetClass
75
+ {
75
76
private:
76
77
static IPAddress _dnsServerAddress;
77
- static DhcpClass* _dhcp;
78
+ static DhcpClass *_dhcp;
79
+
78
80
public:
79
81
// Initialise the Ethernet shield to use the provided MAC address and
80
82
// gain the rest of the configuration through DHCP.
@@ -108,34 +110,35 @@ class EthernetClass {
108
110
friend class EthernetClient ;
109
111
friend class EthernetServer ;
110
112
friend class EthernetUDP ;
113
+
111
114
private:
112
115
// Opens a socket(TCP or UDP or IP_RAW mode)
113
116
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);
115
118
static uint8_t socketStatus (uint8_t s);
116
119
// Close socket
117
120
static void socketClose (uint8_t s);
118
121
// 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);
120
123
// disconnect the connection
121
124
static void socketDisconnect (uint8_t s);
122
125
// Establish TCP connection (Passive connection)
123
126
static uint8_t socketListen (uint8_t s);
124
127
// 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);
126
129
static uint16_t socketSendAvailable (uint8_t s);
127
130
// 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);
129
132
static uint16_t socketRecvAvailable (uint8_t s);
130
133
static uint8_t socketPeek (uint8_t s);
131
134
// sets up a UDP datagram, the data for which will be provided by one
132
135
// or more calls to bufferData and then finally sent with sendUDP.
133
136
// 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);
135
138
// copy up to len bytes of data from buf into a UDP datagram to be
136
139
// sent later by sendUDP. Allows datagrams to be built up from a series of bufferData calls.
137
140
// 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);
139
142
// Send a UDP datagram built up from a sequence of startUDP followed by one or more
140
143
// calls to bufferData.
141
144
// return true if the datagram was successfully sent, or false if there was an error
@@ -146,25 +149,25 @@ class EthernetClass {
146
149
147
150
extern EthernetClass Ethernet;
148
151
149
-
150
152
#define UDP_TX_PACKET_MAX_SIZE 24
151
153
152
- class EthernetUDP : public UDP {
154
+ class EthernetUDP : public UDP
155
+ {
153
156
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
156
159
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
158
161
159
162
protected:
160
163
uint8_t sockindex;
161
164
uint16_t _remaining; // remaining bytes of incoming packet yet to be processed
162
165
163
166
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
168
171
169
172
// Sending UDP packets
170
173
@@ -193,10 +196,10 @@ class EthernetUDP : public UDP {
193
196
virtual int read ();
194
197
// Read up to len bytes from the current packet and place them into buffer
195
198
// 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);
197
200
// Read up to len characters from the current packet and place them into buffer
198
201
// 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); };
200
203
// Return the next byte from the current packet without moving on to the next byte
201
204
virtual int peek ();
202
205
virtual void flush (); // Finish reading the current packet
@@ -208,14 +211,12 @@ class EthernetUDP : public UDP {
208
211
virtual uint16_t localPort () { return _port; }
209
212
};
210
213
211
-
212
-
213
-
214
- class EthernetClient : public Client {
214
+ class EthernetClient : public Client
215
+ {
215
216
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 (){};
219
220
220
221
uint8_t status ();
221
222
virtual int connect (IPAddress ip, uint16_t port);
@@ -239,6 +240,28 @@ class EthernetClient : public Client {
239
240
setConnectionTimeout (timeout);
240
241
return 1 ;
241
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
+ }
242
265
#endif
243
266
virtual int availableForWrite (void );
244
267
virtual size_t write (uint8_t );
@@ -253,8 +276,8 @@ class EthernetClient : public Client {
253
276
virtual operator bool () { return _sockindex < MAX_SOCK_NUM; }
254
277
virtual bool operator ==(const bool value) { return bool () == value; }
255
278
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); }
258
281
uint8_t getSocketNumber () const { return _sockindex; }
259
282
virtual uint16_t localPort ();
260
283
virtual IPAddress remoteIP ();
@@ -270,12 +293,13 @@ class EthernetClient : public Client {
270
293
uint16_t _timeout;
271
294
};
272
295
273
-
274
- class EthernetServer : public Server {
296
+ class EthernetServer : public Server
297
+ {
275
298
private:
276
299
uint16_t _port;
300
+
277
301
public:
278
- EthernetServer (uint16_t port) : _port(port) { }
302
+ EthernetServer (uint16_t port) : _port(port) {}
279
303
EthernetClient available ();
280
304
EthernetClient accept ();
281
305
virtual void begin ();
@@ -289,30 +313,30 @@ class EthernetServer : public Server {
289
313
virtual size_t write (const uint8_t *buf, size_t size);
290
314
virtual operator bool ();
291
315
using Print::write;
292
- // void statusreport();
316
+ // void statusreport();
293
317
294
318
// TODO: make private when socket allocation moves to EthernetClass
295
319
static uint16_t server_port[MAX_SOCK_NUM];
296
320
};
297
321
298
-
299
- class DhcpClass {
322
+ class DhcpClass
323
+ {
300
324
private:
301
325
uint32_t _dhcpInitialTransactionId;
302
326
uint32_t _dhcpTransactionId;
303
- uint8_t _dhcpMacAddr[6 ];
327
+ uint8_t _dhcpMacAddr[6 ];
304
328
#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 )));
310
334
#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 ];
316
340
#endif
317
341
uint32_t _dhcpLeaseTime;
318
342
uint32_t _dhcpT1, _dhcpT2;
@@ -330,7 +354,8 @@ class DhcpClass {
330
354
void send_DHCP_MESSAGE (uint8_t , uint16_t );
331
355
void printByte (char *, uint8_t );
332
356
333
- uint8_t parseDHCPResponse (unsigned long responseTimeout, uint32_t & transactionId);
357
+ uint8_t parseDHCPResponse (unsigned long responseTimeout, uint32_t &transactionId);
358
+
334
359
public:
335
360
IPAddress getLocalIp ();
336
361
IPAddress getSubnetMask ();
@@ -342,8 +367,4 @@ class DhcpClass {
342
367
int checkLease ();
343
368
};
344
369
345
-
346
-
347
-
348
-
349
370
#endif
0 commit comments