Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a2ca62d

Browse files
committedJan 7, 2013
Fixed the formatting mess (now all tabs)
1 parent bd4f20e commit a2ca62d

12 files changed

+1255
-1255
lines changed
 

‎libraries/Ethernet/Dhcp.cpp

+418-418
Large diffs are not rendered by default.

‎libraries/Ethernet/Dhcp.h

+34-34
Original file line numberDiff line numberDiff line change
@@ -138,41 +138,41 @@ typedef struct _RIP_MSG_FIXED
138138

139139
class DhcpClass {
140140
private:
141-
uint32_t _dhcpInitialTransactionId;
142-
uint32_t _dhcpTransactionId;
143-
uint8_t _dhcpMacAddr[6];
144-
uint8_t _dhcpLocalIp[4];
145-
uint8_t _dhcpSubnetMask[4];
146-
uint8_t _dhcpGatewayIp[4];
147-
uint8_t _dhcpDhcpServerIp[4];
148-
uint8_t _dhcpDnsServerIp[4];
149-
uint32_t _dhcpLeaseTime;
150-
uint32_t _dhcpT1, _dhcpT2;
151-
signed long _renewInSec;
152-
signed long _rebindInSec;
153-
signed long _lastCheck;
154-
unsigned long _timeout;
155-
unsigned long _responseTimeout;
156-
unsigned long _secTimeout;
157-
uint8_t _dhcp_state;
158-
EthernetUDP _dhcpUdpSocket;
159-
160-
int request_DHCP_lease();
161-
void reset_DHCP_lease();
162-
void presend_DHCP();
163-
void send_DHCP_MESSAGE(uint8_t, uint16_t);
164-
void printByte(char *, uint8_t);
165-
166-
uint8_t parseDHCPResponse(unsigned long responseTimeout, uint32_t& transactionId);
141+
uint32_t _dhcpInitialTransactionId;
142+
uint32_t _dhcpTransactionId;
143+
uint8_t _dhcpMacAddr[6];
144+
uint8_t _dhcpLocalIp[4];
145+
uint8_t _dhcpSubnetMask[4];
146+
uint8_t _dhcpGatewayIp[4];
147+
uint8_t _dhcpDhcpServerIp[4];
148+
uint8_t _dhcpDnsServerIp[4];
149+
uint32_t _dhcpLeaseTime;
150+
uint32_t _dhcpT1, _dhcpT2;
151+
signed long _renewInSec;
152+
signed long _rebindInSec;
153+
signed long _lastCheck;
154+
unsigned long _timeout;
155+
unsigned long _responseTimeout;
156+
unsigned long _secTimeout;
157+
uint8_t _dhcp_state;
158+
EthernetUDP _dhcpUdpSocket;
159+
160+
int request_DHCP_lease();
161+
void reset_DHCP_lease();
162+
void presend_DHCP();
163+
void send_DHCP_MESSAGE(uint8_t, uint16_t);
164+
void printByte(char *, uint8_t);
165+
166+
uint8_t parseDHCPResponse(unsigned long responseTimeout, uint32_t& transactionId);
167167
public:
168-
IPAddress getLocalIp();
169-
IPAddress getSubnetMask();
170-
IPAddress getGatewayIp();
171-
IPAddress getDhcpServerIp();
172-
IPAddress getDnsServerIp();
173-
174-
int beginWithDHCP(uint8_t *, unsigned long timeout = 60000, unsigned long responseTimeout = 4000);
175-
int checkLease();
168+
IPAddress getLocalIp();
169+
IPAddress getSubnetMask();
170+
IPAddress getGatewayIp();
171+
IPAddress getDhcpServerIp();
172+
IPAddress getDnsServerIp();
173+
174+
int beginWithDHCP(uint8_t *, unsigned long timeout = 60000, unsigned long responseTimeout = 4000);
175+
int checkLease();
176176
};
177177

178178
#endif

‎libraries/Ethernet/Dns.cpp

+351-351
Large diffs are not rendered by default.

‎libraries/Ethernet/Dns.h

+23-23
Original file line numberDiff line numberDiff line change
@@ -10,32 +10,32 @@
1010
class DNSClient
1111
{
1212
public:
13-
// ctor
14-
void begin(const IPAddress& aDNSServer);
15-
16-
/** Convert a numeric IP address string into a four-byte IP address.
17-
@param aIPAddrString IP address to convert
18-
@param aResult IPAddress structure to store the returned IP address
19-
@result 1 if aIPAddrString was successfully converted to an IP address,
20-
else error code
21-
*/
22-
int inet_aton(const char *aIPAddrString, IPAddress& aResult);
23-
24-
/** Resolve the given hostname to an IP address.
25-
@param aHostname Name to be resolved
26-
@param aResult IPAddress structure to store the returned IP address
27-
@result 1 if aIPAddrString was successfully converted to an IP address,
28-
else error code
29-
*/
30-
int getHostByName(const char* aHostname, IPAddress& aResult);
13+
// ctor
14+
void begin(const IPAddress& aDNSServer);
15+
16+
/** Convert a numeric IP address string into a four-byte IP address.
17+
@param aIPAddrString IP address to convert
18+
@param aResult IPAddress structure to store the returned IP address
19+
@result 1 if aIPAddrString was successfully converted to an IP address,
20+
else error code
21+
*/
22+
int inet_aton(const char *aIPAddrString, IPAddress& aResult);
23+
24+
/** Resolve the given hostname to an IP address.
25+
@param aHostname Name to be resolved
26+
@param aResult IPAddress structure to store the returned IP address
27+
@result 1 if aIPAddrString was successfully converted to an IP address,
28+
else error code
29+
*/
30+
int getHostByName(const char* aHostname, IPAddress& aResult);
3131

3232
protected:
33-
uint16_t BuildRequest(const char* aName);
34-
uint16_t ProcessResponse(uint16_t aTimeout, IPAddress& aAddress);
33+
uint16_t BuildRequest(const char* aName);
34+
uint16_t ProcessResponse(uint16_t aTimeout, IPAddress& aAddress);
3535

36-
IPAddress iDNSServer;
37-
uint16_t iRequestId;
38-
EthernetUDP iUdp;
36+
IPAddress iDNSServer;
37+
uint16_t iRequestId;
38+
EthernetUDP iUdp;
3939
};
4040

4141
#endif

‎libraries/Ethernet/Ethernet.cpp

+74-74
Original file line numberDiff line numberDiff line change
@@ -4,119 +4,119 @@
44

55
// XXX: don't make assumptions about the value of MAX_SOCK_NUM.
66
uint8_t EthernetClass::_state[MAX_SOCK_NUM] = {
7-
0, 0, 0, 0 };
7+
0, 0, 0, 0 };
88
uint16_t EthernetClass::_server_port[MAX_SOCK_NUM] = {
9-
0, 0, 0, 0 };
9+
0, 0, 0, 0 };
1010

1111
int EthernetClass::begin(uint8_t *mac_address)
1212
{
13-
static DhcpClass s_dhcp;
14-
_dhcp = &s_dhcp;
15-
16-
17-
// Initialise the basic info
18-
W5100.init();
19-
W5100.setMACAddress(mac_address);
20-
W5100.setIPAddress(IPAddress(0,0,0,0).raw_address());
21-
22-
// Now try to get our config info from a DHCP server
23-
int ret = _dhcp->beginWithDHCP(mac_address);
24-
if(ret == 1)
25-
{
26-
// We've successfully found a DHCP server and got our configuration info, so set things
27-
// accordingly
28-
W5100.setIPAddress(_dhcp->getLocalIp().raw_address());
29-
W5100.setGatewayIp(_dhcp->getGatewayIp().raw_address());
30-
W5100.setSubnetMask(_dhcp->getSubnetMask().raw_address());
31-
_dnsServerAddress = _dhcp->getDnsServerIp();
32-
}
33-
34-
return ret;
13+
static DhcpClass s_dhcp;
14+
_dhcp = &s_dhcp;
15+
16+
17+
// Initialise the basic info
18+
W5100.init();
19+
W5100.setMACAddress(mac_address);
20+
W5100.setIPAddress(IPAddress(0,0,0,0).raw_address());
21+
22+
// Now try to get our config info from a DHCP server
23+
int ret = _dhcp->beginWithDHCP(mac_address);
24+
if(ret == 1)
25+
{
26+
// We've successfully found a DHCP server and got our configuration info, so set things
27+
// accordingly
28+
W5100.setIPAddress(_dhcp->getLocalIp().raw_address());
29+
W5100.setGatewayIp(_dhcp->getGatewayIp().raw_address());
30+
W5100.setSubnetMask(_dhcp->getSubnetMask().raw_address());
31+
_dnsServerAddress = _dhcp->getDnsServerIp();
32+
}
33+
34+
return ret;
3535
}
3636

3737
void EthernetClass::begin(uint8_t *mac_address, IPAddress local_ip)
3838
{
39-
// Assume the DNS server will be the machine on the same network as the local IP
40-
// but with last octet being '1'
41-
IPAddress dns_server = local_ip;
42-
dns_server[3] = 1;
43-
begin(mac_address, local_ip, dns_server);
39+
// Assume the DNS server will be the machine on the same network as the local IP
40+
// but with last octet being '1'
41+
IPAddress dns_server = local_ip;
42+
dns_server[3] = 1;
43+
begin(mac_address, local_ip, dns_server);
4444
}
4545

4646
void EthernetClass::begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server)
4747
{
48-
// Assume the gateway will be the machine on the same network as the local IP
49-
// but with last octet being '1'
50-
IPAddress gateway = local_ip;
51-
gateway[3] = 1;
52-
begin(mac_address, local_ip, dns_server, gateway);
48+
// Assume the gateway will be the machine on the same network as the local IP
49+
// but with last octet being '1'
50+
IPAddress gateway = local_ip;
51+
gateway[3] = 1;
52+
begin(mac_address, local_ip, dns_server, gateway);
5353
}
5454

5555
void EthernetClass::begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server, IPAddress gateway)
5656
{
57-
IPAddress subnet(255, 255, 255, 0);
58-
begin(mac_address, local_ip, dns_server, gateway, subnet);
57+
IPAddress subnet(255, 255, 255, 0);
58+
begin(mac_address, local_ip, dns_server, gateway, subnet);
5959
}
6060

6161
void EthernetClass::begin(uint8_t *mac, IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet)
6262
{
63-
W5100.init();
64-
W5100.setMACAddress(mac);
65-
W5100.setIPAddress(local_ip._address);
66-
W5100.setGatewayIp(gateway._address);
67-
W5100.setSubnetMask(subnet._address);
68-
_dnsServerAddress = dns_server;
63+
W5100.init();
64+
W5100.setMACAddress(mac);
65+
W5100.setIPAddress(local_ip._address);
66+
W5100.setGatewayIp(gateway._address);
67+
W5100.setSubnetMask(subnet._address);
68+
_dnsServerAddress = dns_server;
6969
}
7070

7171
int EthernetClass::maintain(){
72-
int rc = DHCP_CHECK_NONE;
73-
if(_dhcp != NULL){
74-
//we have a pointer to dhcp, use it
75-
rc = _dhcp->checkLease();
76-
switch ( rc ){
77-
case DHCP_CHECK_NONE:
78-
//nothing done
79-
break;
80-
case DHCP_CHECK_RENEW_OK:
81-
case DHCP_CHECK_REBIND_OK:
82-
//we might have got a new IP.
83-
W5100.setIPAddress(_dhcp->getLocalIp().raw_address());
84-
W5100.setGatewayIp(_dhcp->getGatewayIp().raw_address());
85-
W5100.setSubnetMask(_dhcp->getSubnetMask().raw_address());
86-
_dnsServerAddress = _dhcp->getDnsServerIp();
87-
break;
88-
default:
89-
//this is actually a error, it will retry though
90-
break;
91-
}
92-
}
93-
return rc;
72+
int rc = DHCP_CHECK_NONE;
73+
if(_dhcp != NULL){
74+
//we have a pointer to dhcp, use it
75+
rc = _dhcp->checkLease();
76+
switch ( rc ){
77+
case DHCP_CHECK_NONE:
78+
//nothing done
79+
break;
80+
case DHCP_CHECK_RENEW_OK:
81+
case DHCP_CHECK_REBIND_OK:
82+
//we might have got a new IP.
83+
W5100.setIPAddress(_dhcp->getLocalIp().raw_address());
84+
W5100.setGatewayIp(_dhcp->getGatewayIp().raw_address());
85+
W5100.setSubnetMask(_dhcp->getSubnetMask().raw_address());
86+
_dnsServerAddress = _dhcp->getDnsServerIp();
87+
break;
88+
default:
89+
//this is actually a error, it will retry though
90+
break;
91+
}
92+
}
93+
return rc;
9494
}
9595

9696
IPAddress EthernetClass::localIP()
9797
{
98-
IPAddress ret;
99-
W5100.getIPAddress(ret.raw_address());
100-
return ret;
98+
IPAddress ret;
99+
W5100.getIPAddress(ret.raw_address());
100+
return ret;
101101
}
102102

103103
IPAddress EthernetClass::subnetMask()
104104
{
105-
IPAddress ret;
106-
W5100.getSubnetMask(ret.raw_address());
107-
return ret;
105+
IPAddress ret;
106+
W5100.getSubnetMask(ret.raw_address());
107+
return ret;
108108
}
109109

110110
IPAddress EthernetClass::gatewayIP()
111111
{
112-
IPAddress ret;
113-
W5100.getGatewayIp(ret.raw_address());
114-
return ret;
112+
IPAddress ret;
113+
W5100.getGatewayIp(ret.raw_address());
114+
return ret;
115115
}
116116

117117
IPAddress EthernetClass::dnsServerIP()
118118
{
119-
return _dnsServerAddress;
119+
return _dnsServerAddress;
120120
}
121121

122122
EthernetClass Ethernet;

‎libraries/Ethernet/Ethernet.h

+19-19
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,28 @@
1212

1313
class EthernetClass {
1414
private:
15-
IPAddress _dnsServerAddress;
16-
DhcpClass* _dhcp;
15+
IPAddress _dnsServerAddress;
16+
DhcpClass* _dhcp;
1717
public:
18-
static uint8_t _state[MAX_SOCK_NUM];
19-
static uint16_t _server_port[MAX_SOCK_NUM];
20-
// Initialise the Ethernet shield to use the provided MAC address and gain the rest of the
21-
// configuration through DHCP.
22-
// Returns 0 if the DHCP configuration failed, and 1 if it succeeded
23-
int begin(uint8_t *mac_address);
24-
void begin(uint8_t *mac_address, IPAddress local_ip);
25-
void begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server);
26-
void begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server, IPAddress gateway);
27-
void begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet);
28-
int maintain();
18+
static uint8_t _state[MAX_SOCK_NUM];
19+
static uint16_t _server_port[MAX_SOCK_NUM];
20+
// Initialise the Ethernet shield to use the provided MAC address and gain the rest of the
21+
// configuration through DHCP.
22+
// Returns 0 if the DHCP configuration failed, and 1 if it succeeded
23+
int begin(uint8_t *mac_address);
24+
void begin(uint8_t *mac_address, IPAddress local_ip);
25+
void begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server);
26+
void begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server, IPAddress gateway);
27+
void begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet);
28+
int maintain();
2929

30-
IPAddress localIP();
31-
IPAddress subnetMask();
32-
IPAddress gatewayIP();
33-
IPAddress dnsServerIP();
30+
IPAddress localIP();
31+
IPAddress subnetMask();
32+
IPAddress gatewayIP();
33+
IPAddress dnsServerIP();
3434

35-
friend class EthernetClient;
36-
friend class EthernetServer;
35+
friend class EthernetClient;
36+
friend class EthernetServer;
3737
};
3838

3939
extern EthernetClass Ethernet;

‎libraries/Ethernet/EthernetClient.cpp

+92-92
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#include "socket.h"
33

44
extern "C" {
5-
#include "string.h"
5+
#include "string.h"
66
}
77

88
#include "Arduino.h"
@@ -21,145 +21,145 @@ EthernetClient::EthernetClient(uint8_t sock) : _sock(sock) {
2121
}
2222

2323
int EthernetClient::connect(const char* host, uint16_t port) {
24-
// Look up the host first
25-
int ret = 0;
26-
DNSClient dns;
27-
IPAddress remote_addr;
24+
// Look up the host first
25+
int ret = 0;
26+
DNSClient dns;
27+
IPAddress remote_addr;
2828

29-
dns.begin(Ethernet.dnsServerIP());
30-
ret = dns.getHostByName(host, remote_addr);
31-
if (ret == 1) {
32-
return connect(remote_addr, port);
33-
} else {
34-
return ret;
35-
}
29+
dns.begin(Ethernet.dnsServerIP());
30+
ret = dns.getHostByName(host, remote_addr);
31+
if (ret == 1) {
32+
return connect(remote_addr, port);
33+
} else {
34+
return ret;
35+
}
3636
}
3737

3838
int EthernetClient::connect(IPAddress ip, uint16_t port) {
39-
if (_sock != MAX_SOCK_NUM)
40-
return 0;
39+
if (_sock != MAX_SOCK_NUM)
40+
return 0;
4141

42-
for (int i = 0; i < MAX_SOCK_NUM; i++) {
43-
uint8_t s = W5100.readSnSR(i);
44-
if (s == SnSR::CLOSED || s == SnSR::FIN_WAIT || s == SnSR::CLOSE_WAIT) {
45-
_sock = i;
46-
break;
47-
}
48-
}
42+
for (int i = 0; i < MAX_SOCK_NUM; i++) {
43+
uint8_t s = W5100.readSnSR(i);
44+
if (s == SnSR::CLOSED || s == SnSR::FIN_WAIT || s == SnSR::CLOSE_WAIT) {
45+
_sock = i;
46+
break;
47+
}
48+
}
4949

50-
if (_sock == MAX_SOCK_NUM)
51-
return 0;
50+
if (_sock == MAX_SOCK_NUM)
51+
return 0;
5252

53-
_srcport++;
54-
if (_srcport == 0) _srcport = 1024;
55-
socket(_sock, SnMR::TCP, _srcport, 0);
53+
_srcport++;
54+
if (_srcport == 0) _srcport = 1024;
55+
socket(_sock, SnMR::TCP, _srcport, 0);
5656

57-
if (!::connect(_sock, rawIPAddress(ip), port)) {
58-
_sock = MAX_SOCK_NUM;
59-
return 0;
60-
}
57+
if (!::connect(_sock, rawIPAddress(ip), port)) {
58+
_sock = MAX_SOCK_NUM;
59+
return 0;
60+
}
6161

62-
while (status() != SnSR::ESTABLISHED) {
63-
delay(1);
64-
if (status() == SnSR::CLOSED) {
65-
_sock = MAX_SOCK_NUM;
66-
return 0;
67-
}
68-
}
62+
while (status() != SnSR::ESTABLISHED) {
63+
delay(1);
64+
if (status() == SnSR::CLOSED) {
65+
_sock = MAX_SOCK_NUM;
66+
return 0;
67+
}
68+
}
6969

70-
return 1;
70+
return 1;
7171
}
7272

7373
size_t EthernetClient::write(uint8_t b) {
74-
return write(&b, 1);
74+
return write(&b, 1);
7575
}
7676

7777
size_t EthernetClient::write(const uint8_t *buf, size_t size) {
78-
if (_sock == MAX_SOCK_NUM) {
79-
setWriteError();
80-
return 0;
81-
}
82-
if (!send(_sock, buf, size)) {
83-
setWriteError();
84-
return 0;
85-
}
86-
return size;
78+
if (_sock == MAX_SOCK_NUM) {
79+
setWriteError();
80+
return 0;
81+
}
82+
if (!send(_sock, buf, size)) {
83+
setWriteError();
84+
return 0;
85+
}
86+
return size;
8787
}
8888

8989
int EthernetClient::available() {
90-
if (_sock != MAX_SOCK_NUM)
91-
return W5100.getRXReceivedSize(_sock);
92-
return 0;
90+
if (_sock != MAX_SOCK_NUM)
91+
return W5100.getRXReceivedSize(_sock);
92+
return 0;
9393
}
9494

9595
int EthernetClient::read() {
96-
uint8_t b;
97-
if ( recv(_sock, &b, 1) > 0 )
98-
{
99-
// recv worked
100-
return b;
101-
}
102-
else
103-
{
104-
// No data available
105-
return -1;
106-
}
96+
uint8_t b;
97+
if ( recv(_sock, &b, 1) > 0 )
98+
{
99+
// recv worked
100+
return b;
101+
}
102+
else
103+
{
104+
// No data available
105+
return -1;
106+
}
107107
}
108108

109109
int EthernetClient::read(uint8_t *buf, size_t size) {
110-
return recv(_sock, buf, size);
110+
return recv(_sock, buf, size);
111111
}
112112

113113
int EthernetClient::peek() {
114-
uint8_t b;
115-
// Unlike recv, peek doesn't check to see if there's any data available, so we must
116-
if (!available())
117-
return -1;
118-
::peek(_sock, &b);
119-
return b;
114+
uint8_t b;
115+
// Unlike recv, peek doesn't check to see if there's any data available, so we must
116+
if (!available())
117+
return -1;
118+
::peek(_sock, &b);
119+
return b;
120120
}
121121

122122
void EthernetClient::flush() {
123-
while (available())
124-
read();
123+
while (available())
124+
read();
125125
}
126126

127127
void EthernetClient::stop() {
128-
if (_sock == MAX_SOCK_NUM)
129-
return;
128+
if (_sock == MAX_SOCK_NUM)
129+
return;
130130

131-
// attempt to close the connection gracefully (send a FIN to other side)
132-
disconnect(_sock);
133-
unsigned long start = millis();
131+
// attempt to close the connection gracefully (send a FIN to other side)
132+
disconnect(_sock);
133+
unsigned long start = millis();
134134

135-
// wait a second for the connection to close
136-
while (status() != SnSR::CLOSED && millis() - start < 1000)
137-
delay(1);
135+
// wait a second for the connection to close
136+
while (status() != SnSR::CLOSED && millis() - start < 1000)
137+
delay(1);
138138

139-
// if it hasn't closed, close it forcefully
140-
if (status() != SnSR::CLOSED)
141-
close(_sock);
139+
// if it hasn't closed, close it forcefully
140+
if (status() != SnSR::CLOSED)
141+
close(_sock);
142142

143-
EthernetClass::_server_port[_sock] = 0;
144-
_sock = MAX_SOCK_NUM;
143+
EthernetClass::_server_port[_sock] = 0;
144+
_sock = MAX_SOCK_NUM;
145145
}
146146

147147
uint8_t EthernetClient::connected() {
148-
if (_sock == MAX_SOCK_NUM) return 0;
149-
150-
uint8_t s = status();
151-
return !(s == SnSR::LISTEN || s == SnSR::CLOSED || s == SnSR::FIN_WAIT ||
152-
(s == SnSR::CLOSE_WAIT && !available()));
148+
if (_sock == MAX_SOCK_NUM) return 0;
149+
150+
uint8_t s = status();
151+
return !(s == SnSR::LISTEN || s == SnSR::CLOSED || s == SnSR::FIN_WAIT ||
152+
(s == SnSR::CLOSE_WAIT && !available()));
153153
}
154154

155155
uint8_t EthernetClient::status() {
156-
if (_sock == MAX_SOCK_NUM) return SnSR::CLOSED;
157-
return W5100.readSnSR(_sock);
156+
if (_sock == MAX_SOCK_NUM) return SnSR::CLOSED;
157+
return W5100.readSnSR(_sock);
158158
}
159159

160160
// the next function allows us to use the client returned by
161161
// EthernetServer::available() as the condition in an if-statement.
162162

163163
EthernetClient::operator bool() {
164-
return _sock != MAX_SOCK_NUM;
164+
return _sock != MAX_SOCK_NUM;
165165
}

‎libraries/Ethernet/EthernetClient.h

+20-20
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,30 @@
88
class EthernetClient : public Client {
99

1010
public:
11-
EthernetClient();
12-
EthernetClient(uint8_t sock);
11+
EthernetClient();
12+
EthernetClient(uint8_t sock);
1313

14-
uint8_t status();
15-
virtual int connect(IPAddress ip, uint16_t port);
16-
virtual int connect(const char *host, uint16_t port);
17-
virtual size_t write(uint8_t);
18-
virtual size_t write(const uint8_t *buf, size_t size);
19-
virtual int available();
20-
virtual int read();
21-
virtual int read(uint8_t *buf, size_t size);
22-
virtual int peek();
23-
virtual void flush();
24-
virtual void stop();
25-
virtual uint8_t connected();
26-
virtual operator bool();
14+
uint8_t status();
15+
virtual int connect(IPAddress ip, uint16_t port);
16+
virtual int connect(const char *host, uint16_t port);
17+
virtual size_t write(uint8_t);
18+
virtual size_t write(const uint8_t *buf, size_t size);
19+
virtual int available();
20+
virtual int read();
21+
virtual int read(uint8_t *buf, size_t size);
22+
virtual int peek();
23+
virtual void flush();
24+
virtual void stop();
25+
virtual uint8_t connected();
26+
virtual operator bool();
2727

28-
friend class EthernetServer;
29-
30-
using Print::write;
28+
friend class EthernetServer;
29+
30+
using Print::write;
3131

3232
private:
33-
static uint16_t _srcport;
34-
uint8_t _sock;
33+
static uint16_t _srcport;
34+
uint8_t _sock;
3535
};
3636

3737
#endif

‎libraries/Ethernet/EthernetServer.cpp

+58-58
Original file line numberDiff line numberDiff line change
@@ -10,82 +10,82 @@ extern "C" {
1010

1111
EthernetServer::EthernetServer(uint16_t port)
1212
{
13-
_port = port;
13+
_port = port;
1414
}
1515

1616
void EthernetServer::begin()
1717
{
18-
for (int sock = 0; sock < MAX_SOCK_NUM; sock++) {
19-
EthernetClient client(sock);
20-
if (client.status() == SnSR::CLOSED) {
21-
socket(sock, SnMR::TCP, _port, 0);
22-
listen(sock);
23-
EthernetClass::_server_port[sock] = _port;
24-
break;
25-
}
26-
}
18+
for (int sock = 0; sock < MAX_SOCK_NUM; sock++) {
19+
EthernetClient client(sock);
20+
if (client.status() == SnSR::CLOSED) {
21+
socket(sock, SnMR::TCP, _port, 0);
22+
listen(sock);
23+
EthernetClass::_server_port[sock] = _port;
24+
break;
25+
}
26+
}
2727
}
2828

2929
void EthernetServer::accept()
3030
{
31-
int listening = 0;
32-
33-
for (int sock = 0; sock < MAX_SOCK_NUM; sock++) {
34-
EthernetClient client(sock);
35-
36-
if (EthernetClass::_server_port[sock] == _port) {
37-
if (client.status() == SnSR::LISTEN) {
38-
listening = 1;
39-
}
40-
else if (client.status() == SnSR::CLOSE_WAIT && !client.available()) {
41-
client.stop();
42-
}
43-
}
44-
}
45-
46-
if (!listening) {
47-
begin();
48-
}
31+
int listening = 0;
32+
33+
for (int sock = 0; sock < MAX_SOCK_NUM; sock++) {
34+
EthernetClient client(sock);
35+
36+
if (EthernetClass::_server_port[sock] == _port) {
37+
if (client.status() == SnSR::LISTEN) {
38+
listening = 1;
39+
}
40+
else if (client.status() == SnSR::CLOSE_WAIT && !client.available()) {
41+
client.stop();
42+
}
43+
}
44+
}
45+
46+
if (!listening) {
47+
begin();
48+
}
4949
}
5050

5151
EthernetClient EthernetServer::available()
5252
{
53-
accept();
54-
55-
for (int sock = 0; sock < MAX_SOCK_NUM; sock++) {
56-
EthernetClient client(sock);
57-
if (EthernetClass::_server_port[sock] == _port &&
58-
(client.status() == SnSR::ESTABLISHED ||
59-
client.status() == SnSR::CLOSE_WAIT)) {
60-
if (client.available()) {
61-
// XXX: don't always pick the lowest numbered socket.
62-
return client;
63-
}
64-
}
65-
}
66-
67-
return EthernetClient(MAX_SOCK_NUM);
53+
accept();
54+
55+
for (int sock = 0; sock < MAX_SOCK_NUM; sock++) {
56+
EthernetClient client(sock);
57+
if (EthernetClass::_server_port[sock] == _port &&
58+
(client.status() == SnSR::ESTABLISHED ||
59+
client.status() == SnSR::CLOSE_WAIT)) {
60+
if (client.available()) {
61+
// XXX: don't always pick the lowest numbered socket.
62+
return client;
63+
}
64+
}
65+
}
66+
67+
return EthernetClient(MAX_SOCK_NUM);
6868
}
6969

7070
size_t EthernetServer::write(uint8_t b)
7171
{
72-
return write(&b, 1);
72+
return write(&b, 1);
7373
}
7474

7575
size_t EthernetServer::write(const uint8_t *buffer, size_t size)
7676
{
77-
size_t n = 0;
78-
79-
accept();
80-
81-
for (int sock = 0; sock < MAX_SOCK_NUM; sock++) {
82-
EthernetClient client(sock);
83-
84-
if (EthernetClass::_server_port[sock] == _port &&
85-
client.status() == SnSR::ESTABLISHED) {
86-
n += client.write(buffer, size);
87-
}
88-
}
89-
90-
return n;
77+
size_t n = 0;
78+
79+
accept();
80+
81+
for (int sock = 0; sock < MAX_SOCK_NUM; sock++) {
82+
EthernetClient client(sock);
83+
84+
if (EthernetClass::_server_port[sock] == _port &&
85+
client.status() == SnSR::ESTABLISHED) {
86+
n += client.write(buffer, size);
87+
}
88+
}
89+
90+
return n;
9191
}

‎libraries/Ethernet/EthernetServer.h

+8-8
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ class EthernetClient;
88
class EthernetServer :
99
public Server {
1010
private:
11-
uint16_t _port;
12-
void accept();
11+
uint16_t _port;
12+
void accept();
1313
public:
14-
EthernetServer(uint16_t);
15-
EthernetClient available();
16-
virtual void begin();
17-
virtual size_t write(uint8_t);
18-
virtual size_t write(const uint8_t *buf, size_t size);
19-
using Print::write;
14+
EthernetServer(uint16_t);
15+
EthernetClient available();
16+
virtual void begin();
17+
virtual size_t write(uint8_t);
18+
virtual size_t write(const uint8_t *buf, size_t size);
19+
using Print::write;
2020
};
2121

2222
#endif

‎libraries/Ethernet/EthernetUdp.cpp

+112-112
Original file line numberDiff line numberDiff line change
@@ -37,182 +37,182 @@ EthernetUDP::EthernetUDP() : _sock(MAX_SOCK_NUM) {}
3737

3838
/* Start EthernetUDP socket, listening at local port PORT */
3939
uint8_t EthernetUDP::begin(uint16_t port) {
40-
if (_sock != MAX_SOCK_NUM)
41-
return 0;
40+
if (_sock != MAX_SOCK_NUM)
41+
return 0;
4242

43-
for (int i = 0; i < MAX_SOCK_NUM; i++) {
44-
uint8_t s = W5100.readSnSR(i);
45-
if (s == SnSR::CLOSED || s == SnSR::FIN_WAIT) {
46-
_sock = i;
47-
break;
48-
}
49-
}
43+
for (int i = 0; i < MAX_SOCK_NUM; i++) {
44+
uint8_t s = W5100.readSnSR(i);
45+
if (s == SnSR::CLOSED || s == SnSR::FIN_WAIT) {
46+
_sock = i;
47+
break;
48+
}
49+
}
5050

51-
if (_sock == MAX_SOCK_NUM)
52-
return 0;
51+
if (_sock == MAX_SOCK_NUM)
52+
return 0;
5353

54-
_port = port;
55-
_remaining = 0;
56-
socket(_sock, SnMR::UDP, _port, 0);
54+
_port = port;
55+
_remaining = 0;
56+
socket(_sock, SnMR::UDP, _port, 0);
5757

58-
return 1;
58+
return 1;
5959
}
6060

6161
/* return number of bytes available in the current packet,
6262
will return zero if parsePacket hasn't been called yet */
6363
int EthernetUDP::available() {
64-
return _remaining;
64+
return _remaining;
6565
}
6666

6767
/* Release any resources being used by this EthernetUDP instance */
6868
void EthernetUDP::stop()
6969
{
70-
if (_sock == MAX_SOCK_NUM)
71-
return;
70+
if (_sock == MAX_SOCK_NUM)
71+
return;
7272

73-
close(_sock);
73+
close(_sock);
7474

75-
EthernetClass::_server_port[_sock] = 0;
76-
_sock = MAX_SOCK_NUM;
75+
EthernetClass::_server_port[_sock] = 0;
76+
_sock = MAX_SOCK_NUM;
7777
}
7878

7979
int EthernetUDP::beginPacket(const char *host, uint16_t port)
8080
{
81-
// Look up the host first
82-
int ret = 0;
83-
DNSClient dns;
84-
IPAddress remote_addr;
85-
86-
dns.begin(Ethernet.dnsServerIP());
87-
ret = dns.getHostByName(host, remote_addr);
88-
if (ret == 1) {
89-
return beginPacket(remote_addr, port);
90-
} else {
91-
return ret;
92-
}
81+
// Look up the host first
82+
int ret = 0;
83+
DNSClient dns;
84+
IPAddress remote_addr;
85+
86+
dns.begin(Ethernet.dnsServerIP());
87+
ret = dns.getHostByName(host, remote_addr);
88+
if (ret == 1) {
89+
return beginPacket(remote_addr, port);
90+
} else {
91+
return ret;
92+
}
9393
}
9494

9595
int EthernetUDP::beginPacket(IPAddress ip, uint16_t port)
9696
{
97-
_offset = 0;
98-
return startUDP(_sock, rawIPAddress(ip), port);
97+
_offset = 0;
98+
return startUDP(_sock, rawIPAddress(ip), port);
9999
}
100100

101101
int EthernetUDP::endPacket()
102102
{
103-
return sendUDP(_sock);
103+
return sendUDP(_sock);
104104
}
105105

106106
size_t EthernetUDP::write(uint8_t byte)
107107
{
108-
return write(&byte, 1);
108+
return write(&byte, 1);
109109
}
110110

111111
size_t EthernetUDP::write(const uint8_t *buffer, size_t size)
112112
{
113-
uint16_t bytes_written = bufferData(_sock, _offset, buffer, size);
114-
_offset += bytes_written;
115-
return bytes_written;
113+
uint16_t bytes_written = bufferData(_sock, _offset, buffer, size);
114+
_offset += bytes_written;
115+
return bytes_written;
116116
}
117117

118118
int EthernetUDP::parsePacket()
119119
{
120-
// discard any remaining bytes in the last packet
121-
flush();
122-
123-
if (W5100.getRXReceivedSize(_sock) > 0)
124-
{
125-
//HACK - hand-parse the UDP packet using TCP recv method
126-
uint8_t tmpBuf[8];
127-
int ret =0;
128-
//read 8 header bytes and get IP and port from it
129-
ret = recv(_sock,tmpBuf,8);
130-
if (ret > 0)
131-
{
132-
_remoteIP = tmpBuf;
133-
_remotePort = tmpBuf[4];
134-
_remotePort = (_remotePort << 8) + tmpBuf[5];
135-
_remaining = tmpBuf[6];
136-
_remaining = (_remaining << 8) + tmpBuf[7];
137-
138-
// When we get here, any remaining bytes are the data
139-
ret = _remaining;
140-
}
141-
return ret;
142-
}
143-
// There aren't any packets available
144-
return 0;
120+
// discard any remaining bytes in the last packet
121+
flush();
122+
123+
if (W5100.getRXReceivedSize(_sock) > 0)
124+
{
125+
//HACK - hand-parse the UDP packet using TCP recv method
126+
uint8_t tmpBuf[8];
127+
int ret =0;
128+
//read 8 header bytes and get IP and port from it
129+
ret = recv(_sock,tmpBuf,8);
130+
if (ret > 0)
131+
{
132+
_remoteIP = tmpBuf;
133+
_remotePort = tmpBuf[4];
134+
_remotePort = (_remotePort << 8) + tmpBuf[5];
135+
_remaining = tmpBuf[6];
136+
_remaining = (_remaining << 8) + tmpBuf[7];
137+
138+
// When we get here, any remaining bytes are the data
139+
ret = _remaining;
140+
}
141+
return ret;
142+
}
143+
// There aren't any packets available
144+
return 0;
145145
}
146146

147147
int EthernetUDP::read()
148148
{
149-
uint8_t byte;
149+
uint8_t byte;
150150

151-
if ((_remaining > 0) && (recv(_sock, &byte, 1) > 0))
152-
{
153-
// We read things without any problems
154-
_remaining--;
155-
return byte;
156-
}
151+
if ((_remaining > 0) && (recv(_sock, &byte, 1) > 0))
152+
{
153+
// We read things without any problems
154+
_remaining--;
155+
return byte;
156+
}
157157

158-
// If we get here, there's no data available
159-
return -1;
158+
// If we get here, there's no data available
159+
return -1;
160160
}
161161

162162
int EthernetUDP::read(unsigned char* buffer, size_t len)
163163
{
164164

165-
if (_remaining > 0)
166-
{
165+
if (_remaining > 0)
166+
{
167167

168-
int got;
168+
int got;
169169

170-
if (_remaining <= len)
171-
{
172-
// data should fit in the buffer
173-
got = recv(_sock, buffer, _remaining);
174-
}
175-
else
176-
{
177-
// too much data for the buffer,
178-
// grab as much as will fit
179-
got = recv(_sock, buffer, len);
180-
}
170+
if (_remaining <= len)
171+
{
172+
// data should fit in the buffer
173+
got = recv(_sock, buffer, _remaining);
174+
}
175+
else
176+
{
177+
// too much data for the buffer,
178+
// grab as much as will fit
179+
got = recv(_sock, buffer, len);
180+
}
181181

182-
if (got > 0)
183-
{
184-
_remaining -= got;
185-
return got;
186-
}
182+
if (got > 0)
183+
{
184+
_remaining -= got;
185+
return got;
186+
}
187187

188-
}
188+
}
189189

190-
// If we get here, there's no data available or recv failed
191-
return -1;
190+
// If we get here, there's no data available or recv failed
191+
return -1;
192192

193193
}
194194

195195
int EthernetUDP::peek()
196196
{
197-
uint8_t b;
198-
// Unlike recv, peek doesn't check to see if there's any data available, so we must.
199-
// If the user hasn't called parsePacket yet then return nothing otherwise they
200-
// may get the UDP header
201-
if (!_remaining)
202-
return -1;
203-
::peek(_sock, &b);
204-
return b;
197+
uint8_t b;
198+
// Unlike recv, peek doesn't check to see if there's any data available, so we must.
199+
// If the user hasn't called parsePacket yet then return nothing otherwise they
200+
// may get the UDP header
201+
if (!_remaining)
202+
return -1;
203+
::peek(_sock, &b);
204+
return b;
205205
}
206206

207207
void EthernetUDP::flush()
208208
{
209-
// could this fail (loop endlessly) if _remaining > 0 and recv in read fails?
210-
// should only occur if recv fails after telling us the data is there, lets
211-
// hope the w5100 always behaves :)
212-
213-
while (_remaining)
214-
{
215-
read();
216-
}
209+
// could this fail (loop endlessly) if _remaining > 0 and recv in read fails?
210+
// should only occur if recv fails after telling us the data is there, lets
211+
// hope the w5100 always behaves :)
212+
213+
while (_remaining)
214+
{
215+
read();
216+
}
217217
}
218218

‎libraries/Ethernet/EthernetUdp.h

+46-46
Original file line numberDiff line numberDiff line change
@@ -43,57 +43,57 @@
4343

4444
class EthernetUDP : public UDP {
4545
private:
46-
uint8_t _sock; // socket ID for Wiz5100
47-
uint16_t _port; // local port to listen on
48-
IPAddress _remoteIP; // remote IP address for the incoming packet whilst it's being processed
49-
uint16_t _remotePort; // remote port for the incoming packet whilst it's being processed
50-
uint16_t _offset; // offset into the packet being sent
51-
uint16_t _remaining; // remaining bytes of incoming packet yet to be processed
46+
uint8_t _sock; // socket ID for Wiz5100
47+
uint16_t _port; // local port to listen on
48+
IPAddress _remoteIP; // remote IP address for the incoming packet whilst it's being processed
49+
uint16_t _remotePort; // remote port for the incoming packet whilst it's being processed
50+
uint16_t _offset; // offset into the packet being sent
51+
uint16_t _remaining; // remaining bytes of incoming packet yet to be processed
5252

5353
public:
54-
EthernetUDP(); // Constructor
55-
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
56-
virtual void stop(); // Finish with the UDP socket
54+
EthernetUDP(); // Constructor
55+
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
56+
virtual void stop(); // Finish with the UDP socket
5757

58-
// Sending UDP packets
59-
60-
// Start building up a packet to send to the remote host specific in ip and port
61-
// Returns 1 if successful, 0 if there was a problem with the supplied IP address or port
62-
virtual int beginPacket(IPAddress ip, uint16_t port);
63-
// Start building up a packet to send to the remote host specific in host and port
64-
// Returns 1 if successful, 0 if there was a problem resolving the hostname or port
65-
virtual int beginPacket(const char *host, uint16_t port);
66-
// Finish off this packet and send it
67-
// Returns 1 if the packet was sent successfully, 0 if there was an error
68-
virtual int endPacket();
69-
// Write a single byte into the packet
70-
virtual size_t write(uint8_t);
71-
// Write size bytes from buffer into the packet
72-
virtual size_t write(const uint8_t *buffer, size_t size);
73-
74-
using Print::write;
58+
// Sending UDP packets
7559

76-
// Start processing the next available incoming packet
77-
// Returns the size of the packet in bytes, or 0 if no packets are available
78-
virtual int parsePacket();
79-
// Number of bytes remaining in the current packet
80-
virtual int available();
81-
// Read a single byte from the current packet
82-
virtual int read();
83-
// Read up to len bytes from the current packet and place them into buffer
84-
// Returns the number of bytes read, or 0 if none are available
85-
virtual int read(unsigned char* buffer, size_t len);
86-
// Read up to len characters from the current packet and place them into buffer
87-
// Returns the number of characters read, or 0 if none are available
88-
virtual int read(char* buffer, size_t len) { return read((unsigned char*)buffer, len); };
89-
// Return the next byte from the current packet without moving on to the next byte
90-
virtual int peek();
91-
virtual void flush(); // Finish reading the current packet
60+
// Start building up a packet to send to the remote host specific in ip and port
61+
// Returns 1 if successful, 0 if there was a problem with the supplied IP address or port
62+
virtual int beginPacket(IPAddress ip, uint16_t port);
63+
// Start building up a packet to send to the remote host specific in host and port
64+
// Returns 1 if successful, 0 if there was a problem resolving the hostname or port
65+
virtual int beginPacket(const char *host, uint16_t port);
66+
// Finish off this packet and send it
67+
// Returns 1 if the packet was sent successfully, 0 if there was an error
68+
virtual int endPacket();
69+
// Write a single byte into the packet
70+
virtual size_t write(uint8_t);
71+
// Write size bytes from buffer into the packet
72+
virtual size_t write(const uint8_t *buffer, size_t size);
9273

93-
// Return the IP address of the host who sent the current incoming packet
94-
virtual IPAddress remoteIP() { return _remoteIP; };
95-
// Return the port of the host who sent the current incoming packet
96-
virtual uint16_t remotePort() { return _remotePort; };
74+
using Print::write;
75+
76+
// Start processing the next available incoming packet
77+
// Returns the size of the packet in bytes, or 0 if no packets are available
78+
virtual int parsePacket();
79+
// Number of bytes remaining in the current packet
80+
virtual int available();
81+
// Read a single byte from the current packet
82+
virtual int read();
83+
// Read up to len bytes from the current packet and place them into buffer
84+
// Returns the number of bytes read, or 0 if none are available
85+
virtual int read(unsigned char* buffer, size_t len);
86+
// Read up to len characters from the current packet and place them into buffer
87+
// Returns the number of characters read, or 0 if none are available
88+
virtual int read(char* buffer, size_t len) { return read((unsigned char*)buffer, len); };
89+
// Return the next byte from the current packet without moving on to the next byte
90+
virtual int peek();
91+
virtual void flush(); // Finish reading the current packet
92+
93+
// Return the IP address of the host who sent the current incoming packet
94+
virtual IPAddress remoteIP() { return _remoteIP; };
95+
// Return the port of the host who sent the current incoming packet
96+
virtual uint16_t remotePort() { return _remotePort; };
9797
};
9898

9999
#endif

0 commit comments

Comments
 (0)
Please sign in to comment.