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,18 +211,58 @@ 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);
222
223
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
223
266
virtual int availableForWrite (void );
224
267
virtual size_t write (uint8_t );
225
268
virtual size_t write (const uint8_t *buf, size_t size);
@@ -233,8 +276,8 @@ class EthernetClient : public Client {
233
276
virtual operator bool () { return _sockindex < MAX_SOCK_NUM; }
234
277
virtual bool operator ==(const bool value) { return bool () == value; }
235
278
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); }
238
281
uint8_t getSocketNumber () const { return _sockindex; }
239
282
virtual uint16_t localPort ();
240
283
virtual IPAddress remoteIP ();
@@ -250,43 +293,50 @@ class EthernetClient : public Client {
250
293
uint16_t _timeout;
251
294
};
252
295
253
-
254
- class EthernetServer : public Server {
296
+ class EthernetServer : public Server
297
+ {
255
298
private:
256
299
uint16_t _port;
300
+
257
301
public:
258
- EthernetServer (uint16_t port) : _port(port) { }
302
+ EthernetServer (uint16_t port) : _port(port) {}
259
303
EthernetClient available ();
260
304
EthernetClient accept ();
261
305
virtual void begin ();
306
+ #ifdef ESP32
307
+ void begin (uint16_t port)
308
+ {
309
+ _port = port;
310
+ }
311
+ #endif
262
312
virtual size_t write (uint8_t );
263
313
virtual size_t write (const uint8_t *buf, size_t size);
264
314
virtual operator bool ();
265
315
using Print::write;
266
- // void statusreport();
316
+ // void statusreport();
267
317
268
318
// TODO: make private when socket allocation moves to EthernetClass
269
319
static uint16_t server_port[MAX_SOCK_NUM];
270
320
};
271
321
272
-
273
- class DhcpClass {
322
+ class DhcpClass
323
+ {
274
324
private:
275
325
uint32_t _dhcpInitialTransactionId;
276
326
uint32_t _dhcpTransactionId;
277
- uint8_t _dhcpMacAddr[6 ];
327
+ uint8_t _dhcpMacAddr[6 ];
278
328
#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 )));
284
334
#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 ];
290
340
#endif
291
341
uint32_t _dhcpLeaseTime;
292
342
uint32_t _dhcpT1, _dhcpT2;
@@ -304,7 +354,8 @@ class DhcpClass {
304
354
void send_DHCP_MESSAGE (uint8_t , uint16_t );
305
355
void printByte (char *, uint8_t );
306
356
307
- uint8_t parseDHCPResponse (unsigned long responseTimeout, uint32_t & transactionId);
357
+ uint8_t parseDHCPResponse (unsigned long responseTimeout, uint32_t &transactionId);
358
+
308
359
public:
309
360
IPAddress getLocalIp ();
310
361
IPAddress getSubnetMask ();
@@ -316,8 +367,4 @@ class DhcpClass {
316
367
int checkLease ();
317
368
};
318
369
319
-
320
-
321
-
322
-
323
- #endif
370
+ #endif
0 commit comments