From c36e7f8c3918853cb09c18ab1bd59f83d13fd0d5 Mon Sep 17 00:00:00 2001 From: Sebastian Martin Dicke Date: Fri, 16 Sep 2016 10:21:55 +0200 Subject: [PATCH 01/16] Made function arguments const, which are not change in the functions. --- Dhcp.cpp | 6 +++--- Dhcp.h | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Dhcp.cpp b/Dhcp.cpp index d3ad5ce..9d32c37 100644 --- a/Dhcp.cpp +++ b/Dhcp.cpp @@ -9,7 +9,7 @@ #include "Arduino.h" #include "util.h" -int DhcpClass::beginWithDHCP(uint8_t *mac, unsigned long timeout, unsigned long responseTimeout) +int DhcpClass::beginWithDHCP(uint8_t *mac, const unsigned long timeout, const unsigned long responseTimeout) { _dhcpLeaseTime=0; _dhcpT1=0; @@ -132,7 +132,7 @@ void DhcpClass::presend_DHCP() { } -void DhcpClass::send_DHCP_MESSAGE(uint8_t messageType, uint16_t secondsElapsed) +void DhcpClass::send_DHCP_MESSAGE(const uint8_t messageType, const uint16_t secondsElapsed) { uint8_t buffer[32]; memset(buffer, 0, 32); @@ -251,7 +251,7 @@ void DhcpClass::send_DHCP_MESSAGE(uint8_t messageType, uint16_t secondsElapsed) _dhcpUdpSocket.endPacket(); } -uint8_t DhcpClass::parseDHCPResponse(unsigned long responseTimeout, uint32_t& transactionId) +uint8_t DhcpClass::parseDHCPResponse(const unsigned long responseTimeout, uint32_t& transactionId) { uint8_t type = 0; uint8_t opt_len = 0; diff --git a/Dhcp.h b/Dhcp.h index ba5e693..ac65e32 100644 --- a/Dhcp.h +++ b/Dhcp.h @@ -167,10 +167,10 @@ class DhcpClass { int request_DHCP_lease(); void reset_DHCP_lease(); void presend_DHCP(); - void send_DHCP_MESSAGE(uint8_t, uint16_t); + void send_DHCP_MESSAGE(const uint8_t, const uint16_t); void printByte(char *, uint8_t); - uint8_t parseDHCPResponse(unsigned long responseTimeout, uint32_t& transactionId); + uint8_t parseDHCPResponse(const unsigned long responseTimeout, uint32_t& transactionId); public: IPAddress getLocalIp(); IPAddress getSubnetMask(); @@ -178,7 +178,7 @@ class DhcpClass { IPAddress getDhcpServerIp(); IPAddress getDnsServerIp(); - int beginWithDHCP(uint8_t *, unsigned long timeout = 60000, unsigned long responseTimeout = 4000); + int beginWithDHCP(uint8_t *, const unsigned long timeout = 60000, const unsigned long responseTimeout = 4000); int checkLease(); }; From 8902dcf0ebb01daeaedde9c8a92f97fb7b1e223c Mon Sep 17 00:00:00 2001 From: Sebastian Martin Dicke Date: Fri, 16 Sep 2016 10:27:42 +0200 Subject: [PATCH 02/16] Added a destructor. --- Dhcp.cpp | 8 ++++++++ Dhcp.h | 1 + 2 files changed, 9 insertions(+) diff --git a/Dhcp.cpp b/Dhcp.cpp index 9d32c37..585315d 100644 --- a/Dhcp.cpp +++ b/Dhcp.cpp @@ -9,6 +9,14 @@ #include "Arduino.h" #include "util.h" +DhcpClass::~DhcpClass() { + delete[] _dhcpLocalIp; + delete[] _dhcpSubnetMask; + delete[] _dhcpGatewayIp; + delete[] _dhcpDhcpServerIp; + delete[] _dhcpDnsServerIp; +} + int DhcpClass::beginWithDHCP(uint8_t *mac, const unsigned long timeout, const unsigned long responseTimeout) { _dhcpLeaseTime=0; diff --git a/Dhcp.h b/Dhcp.h index ac65e32..35c9d5a 100644 --- a/Dhcp.h +++ b/Dhcp.h @@ -180,6 +180,7 @@ class DhcpClass { int beginWithDHCP(uint8_t *, const unsigned long timeout = 60000, const unsigned long responseTimeout = 4000); int checkLease(); + ~DhcpClass(); }; #endif From 728de72ff954fcd50c07f87637787f6af8f9e115 Mon Sep 17 00:00:00 2001 From: Sebastian Martin Dicke Date: Fri, 16 Sep 2016 10:41:32 +0200 Subject: [PATCH 03/16] Replaced defines by constant expressions to attain type safety. --- Dns.cpp | 66 ++++++++++++++++++++++++++++----------------------------- Dns.h | 2 +- 2 files changed, 34 insertions(+), 34 deletions(-) diff --git a/Dns.cpp b/Dns.cpp index efa0bd4..0291d6d 100644 --- a/Dns.cpp +++ b/Dns.cpp @@ -12,41 +12,41 @@ #include "Arduino.h" -#define SOCKET_NONE 255 +constexpr uint8_t SOCKET_NONE = 255; // Various flags and header field values for a DNS message -#define UDP_HEADER_SIZE 8 -#define DNS_HEADER_SIZE 12 -#define TTL_SIZE 4 -#define QUERY_FLAG (0) -#define RESPONSE_FLAG (1<<15) -#define QUERY_RESPONSE_MASK (1<<15) -#define OPCODE_STANDARD_QUERY (0) -#define OPCODE_INVERSE_QUERY (1<<11) -#define OPCODE_STATUS_REQUEST (2<<11) -#define OPCODE_MASK (15<<11) -#define AUTHORITATIVE_FLAG (1<<10) -#define TRUNCATION_FLAG (1<<9) -#define RECURSION_DESIRED_FLAG (1<<8) -#define RECURSION_AVAILABLE_FLAG (1<<7) -#define RESP_NO_ERROR (0) -#define RESP_FORMAT_ERROR (1) -#define RESP_SERVER_FAILURE (2) -#define RESP_NAME_ERROR (3) -#define RESP_NOT_IMPLEMENTED (4) -#define RESP_REFUSED (5) -#define RESP_MASK (15) -#define TYPE_A (0x0001) -#define CLASS_IN (0x0001) -#define LABEL_COMPRESSION_MASK (0xC0) +constexpr uint8_t UDP_HEADER_SIZE = 8; +constexpr uint8_t DNS_HEADER_SIZE = 12; +constexpr uint8_t TTL_SIZE = 4; +constexpr uint8_t QUERY_FLAG = (0); +constexpr uint8_t RESPONSE_FLAG = (1<<15); +constexpr uint8_t QUERY_RESPONSE_MASK = (1<<15); +constexpr uint8_t OPCODE_STANDARD_QUERY = (0); +constexpr uint8_t OPCODE_INVERSE_QUERY = (1<<11); +constexpr uint8_t OPCODE_STATUS_REQUEST = (2<<11); +constexpr uint8_t OPCODE_MASK = (15<<11); +constexpr uint8_t AUTHORITATIVE_FLAG = (1<<10); +constexpr uint8_t TRUNCATION_FLAG = (1<<9); +constexpr uint8_t RECURSION_DESIRED_FLAG = (1<<8); +constexpr uint8_t RECURSION_AVAILABLE_FLAG = (1<<7); +constexpr uint8_t RESP_NO_ERROR = (0); +constexpr uint8_t RESP_FORMAT_ERROR = (1); +constexpr uint8_t RESP_SERVER_FAILURE = (2); +constexpr uint8_t RESP_NAME_ERROR = (3); +constexpr uint8_t RESP_NOT_IMPLEMENTED = (4); +constexpr uint8_t RESP_REFUSED = (5); +constexpr uint8_t RESP_MASK = (15); +constexpr uint8_t TYPE_A = (0x0001); +constexpr uint8_t CLASS_IN = (0x0001); +constexpr uint8_t LABEL_COMPRESSION_MASK = (0xC0); // Port number that DNS servers listen on -#define DNS_PORT 53 +constexpr uint8_t DNS_PORT = 53; // Possible return codes from ProcessResponse -#define SUCCESS 1 -#define TIMED_OUT -1 -#define INVALID_SERVER -2 -#define TRUNCATED -3 -#define INVALID_RESPONSE -4 +constexpr uint8_t SUCCESS = 1; +constexpr uint8_t TIMED_OUT = -1; +constexpr uint8_t INVALID_SERVER = -2; +constexpr uint8_t TRUNCATED = -3; +constexpr uint8_t INVALID_RESPONSE = -4; void DNSClient::begin(const IPAddress& aDNSServer) { @@ -234,9 +234,9 @@ uint16_t DNSClient::BuildRequest(const char* aName) } -uint16_t DNSClient::ProcessResponse(uint16_t aTimeout, IPAddress& aAddress) +uint16_t DNSClient::ProcessResponse(const uint16_t aTimeout, const IPAddress& aAddress) { - uint32_t startTime = millis(); + const uint32_t startTime = millis(); // Wait for a response packet while(iUdp.parsePacket() <= 0) diff --git a/Dns.h b/Dns.h index c99f5c3..8b9ba67 100644 --- a/Dns.h +++ b/Dns.h @@ -31,7 +31,7 @@ class DNSClient protected: uint16_t BuildRequest(const char* aName); - uint16_t ProcessResponse(uint16_t aTimeout, IPAddress& aAddress); + uint16_t ProcessResponse(const uint16_t aTimeout, const IPAddress& aAddress); IPAddress iDNSServer; uint16_t iRequestId; From b31ecdef23be6f824da393f339c119f698792dac Mon Sep 17 00:00:00 2001 From: Sebastian Martin Dicke Date: Fri, 16 Sep 2016 10:45:38 +0200 Subject: [PATCH 04/16] Moved constants to header file and limited access to private to clean. --- Dns.cpp | 37 ------------------------------------- Dns.h | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 37 deletions(-) diff --git a/Dns.cpp b/Dns.cpp index 0291d6d..4bf9043 100644 --- a/Dns.cpp +++ b/Dns.cpp @@ -11,43 +11,6 @@ //#include #include "Arduino.h" - -constexpr uint8_t SOCKET_NONE = 255; -// Various flags and header field values for a DNS message -constexpr uint8_t UDP_HEADER_SIZE = 8; -constexpr uint8_t DNS_HEADER_SIZE = 12; -constexpr uint8_t TTL_SIZE = 4; -constexpr uint8_t QUERY_FLAG = (0); -constexpr uint8_t RESPONSE_FLAG = (1<<15); -constexpr uint8_t QUERY_RESPONSE_MASK = (1<<15); -constexpr uint8_t OPCODE_STANDARD_QUERY = (0); -constexpr uint8_t OPCODE_INVERSE_QUERY = (1<<11); -constexpr uint8_t OPCODE_STATUS_REQUEST = (2<<11); -constexpr uint8_t OPCODE_MASK = (15<<11); -constexpr uint8_t AUTHORITATIVE_FLAG = (1<<10); -constexpr uint8_t TRUNCATION_FLAG = (1<<9); -constexpr uint8_t RECURSION_DESIRED_FLAG = (1<<8); -constexpr uint8_t RECURSION_AVAILABLE_FLAG = (1<<7); -constexpr uint8_t RESP_NO_ERROR = (0); -constexpr uint8_t RESP_FORMAT_ERROR = (1); -constexpr uint8_t RESP_SERVER_FAILURE = (2); -constexpr uint8_t RESP_NAME_ERROR = (3); -constexpr uint8_t RESP_NOT_IMPLEMENTED = (4); -constexpr uint8_t RESP_REFUSED = (5); -constexpr uint8_t RESP_MASK = (15); -constexpr uint8_t TYPE_A = (0x0001); -constexpr uint8_t CLASS_IN = (0x0001); -constexpr uint8_t LABEL_COMPRESSION_MASK = (0xC0); -// Port number that DNS servers listen on -constexpr uint8_t DNS_PORT = 53; - -// Possible return codes from ProcessResponse -constexpr uint8_t SUCCESS = 1; -constexpr uint8_t TIMED_OUT = -1; -constexpr uint8_t INVALID_SERVER = -2; -constexpr uint8_t TRUNCATED = -3; -constexpr uint8_t INVALID_RESPONSE = -4; - void DNSClient::begin(const IPAddress& aDNSServer) { iDNSServer = aDNSServer; diff --git a/Dns.h b/Dns.h index 8b9ba67..7c1d181 100644 --- a/Dns.h +++ b/Dns.h @@ -36,6 +36,43 @@ class DNSClient IPAddress iDNSServer; uint16_t iRequestId; EthernetUDP iUdp; + + private: + static constexpr uint8_t SOCKET_NONE = 255; + // Various flags and header field values for a DNS message + static constexpr uint8_t UDP_HEADER_SIZE = 8; + static constexpr uint8_t DNS_HEADER_SIZE = 12; + static constexpr uint8_t TTL_SIZE = 4; + static constexpr uint8_t QUERY_FLAG = (0); + static constexpr uint8_t RESPONSE_FLAG = (1<<15); + static constexpr uint8_t QUERY_RESPONSE_MASK = (1<<15); + static constexpr uint8_t OPCODE_STANDARD_QUERY = (0); + static constexpr uint8_t OPCODE_INVERSE_QUERY = (1<<11); + static constexpr uint8_t OPCODE_STATUS_REQUEST = (2<<11); + static constexpr uint8_t OPCODE_MASK = (15<<11); + static constexpr uint8_t AUTHORITATIVE_FLAG = (1<<10); + static constexpr uint8_t TRUNCATION_FLAG = (1<<9); + static constexpr uint8_t RECURSION_DESIRED_FLAG = (1<<8); + static constexpr uint8_t RECURSION_AVAILABLE_FLAG = (1<<7); + static constexpr uint8_t RESP_NO_ERROR = (0); + static constexpr uint8_t RESP_FORMAT_ERROR = (1); + static constexpr uint8_t RESP_SERVER_FAILURE = (2); + static constexpr uint8_t RESP_NAME_ERROR = (3); + static constexpr uint8_t RESP_NOT_IMPLEMENTED = (4); + static constexpr uint8_t RESP_REFUSED = (5); + static constexpr uint8_t RESP_MASK = (15); + static constexpr uint8_t TYPE_A = (0x0001); + static constexpr uint8_t CLASS_IN = (0x0001); + static constexpr uint8_t LABEL_COMPRESSION_MASK = (0xC0); + // Port number that DNS servers listen on + static constexpr uint8_t DNS_PORT = 53; + + // Possible return codes from ProcessResponse + static constexpr uint8_t SUCCESS = 1; + static constexpr uint8_t TIMED_OUT = -1; + static constexpr uint8_t INVALID_SERVER = -2; + static constexpr uint8_t TRUNCATED = -3; + static constexpr uint8_t INVALID_RESPONSE = -4; }; #endif From ff067f1baafc433d790569547531c3c677d56e07 Mon Sep 17 00:00:00 2001 From: Sebastian Martin Dicke Date: Fri, 16 Sep 2016 10:50:47 +0200 Subject: [PATCH 05/16] Replaced some postfix by prefix increments + adding whitespaces to increase arithmetic expressions readabiliy. --- Dns.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/Dns.cpp b/Dns.cpp index 4bf9043..c2019c1 100644 --- a/Dns.cpp +++ b/Dns.cpp @@ -102,12 +102,12 @@ int DNSClient::getHostByName(const char* aHostname, IPAddress& aResult) while ((wait_retries < 3) && (ret == TIMED_OUT)) { ret = ProcessResponse(5000, aResult); - wait_retries++; + ++wait_retries; } } } } - retries++; + ++retries; } // We're done with the socket now @@ -179,7 +179,7 @@ uint16_t DNSClient::BuildRequest(const char* aName) // And then write out the section iUdp.write((uint8_t*)start, end-start); } - start = end+1; + start = end + 1; } // We've got to the end of the question name, so @@ -214,7 +214,7 @@ uint16_t DNSClient::ProcessResponse(const uint16_t aTimeout, const IPAddress& aA //uint8_t header[DNS_HEADER_SIZE]; // Enough space to reuse for the DNS header union { uint8_t byte[DNS_HEADER_SIZE]; // Enough space to reuse for the DNS header - uint16_t word[DNS_HEADER_SIZE/2]; + uint16_t word[DNS_HEADER_SIZE / 2]; } header; // Check that it's a response from the right server and the right port @@ -260,7 +260,7 @@ uint16_t DNSClient::ProcessResponse(const uint16_t aTimeout, const IPAddress& aA } // Skip over any questions - for (uint16_t i =0; i < htons(header.word[2]); i++) + for (uint16_t i = 0; i < htons(header.word[2]); ++i) { // Skip over the name uint8_t len; @@ -279,7 +279,7 @@ uint16_t DNSClient::ProcessResponse(const uint16_t aTimeout, const IPAddress& aA } while (len != 0); // Now jump over the type and class - for (int i =0; i < 4; i++) + for (int i =0; i < 4; ++i) { iUdp.read(); // we don't care about the returned byte } @@ -290,7 +290,7 @@ uint16_t DNSClient::ProcessResponse(const uint16_t aTimeout, const IPAddress& aA // type A answer) and some authority and additional resource records but // we're going to ignore all of them. - for (uint16_t i =0; i < answerCount; i++) + for (uint16_t i = 0; i < answerCount; ++i) { // Skip the name uint8_t len; @@ -333,7 +333,7 @@ uint16_t DNSClient::ProcessResponse(const uint16_t aTimeout, const IPAddress& aA iUdp.read((uint8_t*)&answerClass, sizeof(answerClass)); // Ignore the Time-To-Live as we don't do any caching - for (int i =0; i < TTL_SIZE; i++) + for (int i =0; i < TTL_SIZE; ++i) { iUdp.read(); // we don't care about the returned byte } @@ -357,7 +357,7 @@ uint16_t DNSClient::ProcessResponse(const uint16_t aTimeout, const IPAddress& aA else { // This isn't an answer type we're after, move onto the next one - for (uint16_t i =0; i < htons(header_flags); i++) + for (uint16_t i =0; i < htons(header_flags); ++i) { iUdp.read(); // we don't care about the returned byte } From 60d06cc33ce033ca927f00ae02353f026fb1b508 Mon Sep 17 00:00:00 2001 From: Sebastian Martin Dicke Date: Fri, 16 Sep 2016 11:09:51 +0200 Subject: [PATCH 06/16] Improved variable naming. --- socket.cpp | 202 ++++++++++++++++++++++++++--------------------------- socket.h | 24 +++---- 2 files changed, 113 insertions(+), 113 deletions(-) diff --git a/socket.cpp b/socket.cpp index e074110..b35b80a 100644 --- a/socket.cpp +++ b/socket.cpp @@ -24,10 +24,10 @@ typedef struct { static socketstate_t state[MAX_SOCK_NUM]; -static uint16_t getSnTX_FSR(uint8_t s); -static uint16_t getSnRX_RSR(uint8_t s); -static void send_data_processing(uint8_t s, uint16_t offset, const uint8_t *data, uint16_t len); -static void read_data(uint8_t s, uint16_t src, volatile uint8_t *dst, uint16_t len); +static uint16_t getSnTX_FSR(uint8_t socket); +static uint16_t getSnRX_RSR(uint8_t socket); +static void send_data_processing(uint8_t socket, uint16_t offset, const uint8_t *data, uint16_t length); +static void read_data(uint8_t s, uint16_t source, volatile uint8_t *destination, uint16_t length); @@ -102,10 +102,10 @@ uint8_t socketBegin(uint8_t protocol, uint16_t port) // Return the socket's status // -uint8_t socketStatus(uint8_t s) +uint8_t socketStatus(uint8_t socket) { SPI.beginTransaction(SPI_ETHERNET_SETTINGS); - uint8_t status = W5100.readSnSR(s); + uint8_t status = W5100.readSnSR(socket); SPI.endTransaction(); return status; } @@ -113,24 +113,24 @@ uint8_t socketStatus(uint8_t s) // Immediately close. If a TCP connection is established, the // remote host is left unaware we closed. // -void socketClose(uint8_t s) +void socketClose(uint8_t socket) { SPI.beginTransaction(SPI_ETHERNET_SETTINGS); - W5100.execCmdSn(s, Sock_CLOSE); + W5100.execCmdSn(socket, Sock_CLOSE); SPI.endTransaction(); } // Place the socket in listening (server) mode // -uint8_t socketListen(uint8_t s) +uint8_t socketListen(uint8_t socket) { SPI.beginTransaction(SPI_ETHERNET_SETTINGS); - if (W5100.readSnSR(s) != SnSR::INIT) { + if (W5100.readSnSR(socket) != SnSR::INIT) { SPI.endTransaction(); return 0; } - W5100.execCmdSn(s, Sock_LISTEN); + W5100.execCmdSn(socket, Sock_LISTEN); SPI.endTransaction(); return 1; } @@ -138,13 +138,13 @@ uint8_t socketListen(uint8_t s) // establish a TCP connection in Active (client) mode. // -void socketConnect(uint8_t s, uint8_t * addr, uint16_t port) +void socketConnect(uint8_t socket, uint8_t * address, uint16_t port) { // set destination IP SPI.beginTransaction(SPI_ETHERNET_SETTINGS); - W5100.writeSnDIPR(s, addr); - W5100.writeSnDPORT(s, port); - W5100.execCmdSn(s, Sock_CONNECT); + W5100.writeSnDIPR(socket, address); + W5100.writeSnDPORT(socket, port); + W5100.execCmdSn(socket, Sock_CONNECT); SPI.endTransaction(); } @@ -166,16 +166,16 @@ void socketDisconnect(uint8_t s) /*****************************************/ -static uint16_t getSnRX_RSR(uint8_t s) +static uint16_t getSnRX_RSR(uint8_t socket) { #if 1 uint16_t val, prev; - prev = W5100.readSnRX_RSR(s); + prev = W5100.readSnRX_RSR(socket); while (1) { - val = W5100.readSnRX_RSR(s); + val = W5100.readSnRX_RSR(socket); if (val == prev) { - state[s].RX_RSR = val; + state[socket].RX_RSR = val; return val; } prev = val; @@ -187,75 +187,75 @@ static uint16_t getSnRX_RSR(uint8_t s) #endif } -static void read_data(uint8_t s, uint16_t src, volatile uint8_t *dst, uint16_t len) +static void read_data(uint8_t socket, uint16_t source, volatile uint8_t *destination, uint16_t length) { uint16_t size; uint16_t src_mask; uint16_t src_ptr; //Serial.printf("read_data, len=%d, at:%d\n", len, src); - src_mask = (uint16_t)src & W5100.SMASK; - src_ptr = W5100.RBASE[s] + src_mask; + src_mask = (uint16_t)source & W5100.SMASK; + src_ptr = W5100.RBASE[socket] + src_mask; - if ((src_mask + len) > W5100.SSIZE) { + if ((src_mask + length) > W5100.SSIZE) { size = W5100.SSIZE - src_mask; - W5100.read(src_ptr, (uint8_t *)dst, size); - dst += size; - W5100.read(W5100.RBASE[s], (uint8_t *) dst, len - size); + W5100.read(src_ptr, (uint8_t *)destination, size); + destination += size; + W5100.read(W5100.RBASE[socket], (uint8_t *) destination, length - size); } else { - W5100.read(src_ptr, (uint8_t *) dst, len); + W5100.read(src_ptr, (uint8_t *) destination, length); } } // Receive data. Returns size, or -1 for no data, or 0 if connection closed // -int socketRecv(uint8_t s, uint8_t *buf, int16_t len) +int socketRecv(uint8_t socket, uint8_t *buffer, int16_t length) { // Check how much data is available - int ret = state[s].RX_RSR; + int result = state[socket].RX_RSR; SPI.beginTransaction(SPI_ETHERNET_SETTINGS); - if (ret < len) { - ret = getSnRX_RSR(s); + if (result < length) { + result = getSnRX_RSR(socket); } - if (ret == 0) { + if (result == 0) { // No data available. - uint8_t status = W5100.readSnSR(s); + uint8_t status = W5100.readSnSR(socket); if ( status == SnSR::LISTEN || status == SnSR::CLOSED || status == SnSR::CLOSE_WAIT ) { // The remote end has closed its side of the connection, // so this is the eof state - ret = 0; + result = 0; } else { // The connection is still up, but there's no data waiting to be read - ret = -1; + result = -1; } } else { - if (ret > len) ret = len; // more data available than buffer length - uint16_t ptr = state[s].RX_RD; - read_data(s, ptr, buf, ret); - ptr += len; - state[s].RX_RD = ptr; - state[s].RX_RSR -= ret; - uint16_t inc = state[s].RX_inc + ret; - if (inc >= 80 || state[s].RX_RSR == 0) { - state[s].RX_inc = 0; - W5100.writeSnRX_RD(s, ptr); - W5100.execCmdSn(s, Sock_RECV); + if (result > length) result = length; // more data available than buffer length + uint16_t ptr = state[socket].RX_RD; + read_data(socket, ptr, buffer, result); + ptr += length; + state[socket].RX_RD = ptr; + state[socket].RX_RSR -= result; + uint16_t inc = state[socket].RX_inc + result; + if (inc >= 80 || state[socket].RX_RSR == 0) { + state[socket].RX_inc = 0; + W5100.writeSnRX_RD(socket, ptr); + W5100.execCmdSn(socket, Sock_RECV); //Serial.printf("Sock_RECV cmd, RX_RD=%d\n", state[s].RX_RD); } else { - state[s].RX_inc = inc; + state[socket].RX_inc = inc; } } SPI.endTransaction(); - return ret; + return result; } -uint16_t socketRecvAvailable(uint8_t s) +uint16_t socketRecvAvailable(uint8_t socket) { - uint16_t ret = state[s].RX_RSR; + uint16_t ret = state[socket].RX_RSR; if (ret == 0) { SPI.beginTransaction(SPI_ETHERNET_SETTINGS); - ret = getSnRX_RSR(s); + ret = getSnRX_RSR(socket); SPI.endTransaction(); } //Serial.printf("sock.recvAvailable s=%d, num=%d\n", s, ret); @@ -264,14 +264,14 @@ uint16_t socketRecvAvailable(uint8_t s) // get the first byte in the receive queue (no checking) // -uint8_t socketPeek(uint8_t s) +uint8_t socketPeek(uint8_t socket) { - uint8_t b; + uint8_t result; SPI.beginTransaction(SPI_ETHERNET_SETTINGS); - uint16_t ptr = state[s].RX_RD; - W5100.read((ptr & W5100.SMASK) + W5100.RBASE[s], &b, 1); + uint16_t ptr = state[socket].RX_RD; + W5100.read((ptr & W5100.SMASK) + W5100.RBASE[socket], &result, 1); SPI.endTransaction(); - return b; + return result; } @@ -280,15 +280,15 @@ uint8_t socketPeek(uint8_t s) /* Socket Data Transmit Functions */ /*****************************************/ -static uint16_t getSnTX_FSR(uint8_t s) +static uint16_t getSnTX_FSR(uint8_t socket) { uint16_t val, prev; - prev = W5100.readSnTX_FSR(s); + prev = W5100.readSnTX_FSR(socket); while (1) { - val = W5100.readSnTX_FSR(s); + val = W5100.readSnTX_FSR(socket); if (val == prev) { - state[s].TX_FSR = val; + state[socket].TX_FSR = val; return val; } prev = val; @@ -296,23 +296,23 @@ static uint16_t getSnTX_FSR(uint8_t s) } -static void send_data_processing(uint8_t s, uint16_t data_offset, const uint8_t *data, uint16_t len) +static void send_data_processing(uint8_t socket, uint16_t data_offset, const uint8_t *data, uint16_t length) { - uint16_t ptr = W5100.readSnTX_WR(s); + uint16_t ptr = W5100.readSnTX_WR(socket); ptr += data_offset; uint16_t offset = ptr & W5100.SMASK; - uint16_t dstAddr = offset + W5100.SBASE[s]; + uint16_t dstAddr = offset + W5100.SBASE[socket]; - if (offset + len > W5100.SSIZE) { + if (offset + length > W5100.SSIZE) { // Wrap around circular buffer uint16_t size = W5100.SSIZE - offset; W5100.write(dstAddr, data, size); - W5100.write(W5100.SBASE[s], data + size, len - size); + W5100.write(W5100.SBASE[socket], data + size, length - size); } else { - W5100.write(dstAddr, data, len); + W5100.write(dstAddr, data, length); } - ptr += len; - W5100.writeSnTX_WR(s, ptr); + ptr += length; + W5100.writeSnTX_WR(socket, ptr); } @@ -320,40 +320,40 @@ static void send_data_processing(uint8_t s, uint16_t data_offset, const uint8_t * @brief This function used to send the data in TCP mode * @return 1 for success else 0. */ -uint16_t socketSend(uint8_t s, const uint8_t * buf, uint16_t len) +uint16_t socketSend(uint8_t socket, const uint8_t * buffer, const uint16_t length) { uint8_t status=0; - uint16_t ret=0; + uint16_t result=0; uint16_t freesize=0; - if (len > W5100.SSIZE) { - ret = W5100.SSIZE; // check size not to exceed MAX size. + if (length > W5100.SSIZE) { + result = W5100.SSIZE; // check size not to exceed MAX size. } else { - ret = len; + result = length; } // if freebuf is available, start. do { SPI.beginTransaction(SPI_ETHERNET_SETTINGS); - freesize = getSnTX_FSR(s); - status = W5100.readSnSR(s); + freesize = getSnTX_FSR(socket); + status = W5100.readSnSR(socket); SPI.endTransaction(); if ((status != SnSR::ESTABLISHED) && (status != SnSR::CLOSE_WAIT)) { - ret = 0; + result = 0; break; } yield(); - } while (freesize < ret); + } while (freesize < result); // copy data SPI.beginTransaction(SPI_ETHERNET_SETTINGS); - send_data_processing(s, 0, (uint8_t *)buf, ret); - W5100.execCmdSn(s, Sock_SEND); + send_data_processing(socket, 0, (uint8_t *)buffer, result); + W5100.execCmdSn(socket, Sock_SEND); /* +2008.01 bj */ - while ( (W5100.readSnIR(s) & SnIR::SEND_OK) != SnIR::SEND_OK ) { + while ( (W5100.readSnIR(socket) & SnIR::SEND_OK) != SnIR::SEND_OK ) { /* m2008.01 [bj] : reduce code */ - if ( W5100.readSnSR(s) == SnSR::CLOSED ) { + if ( W5100.readSnSR(socket) == SnSR::CLOSED ) { SPI.endTransaction(); return 0; } @@ -362,51 +362,51 @@ uint16_t socketSend(uint8_t s, const uint8_t * buf, uint16_t len) SPI.beginTransaction(SPI_ETHERNET_SETTINGS); } /* +2008.01 bj */ - W5100.writeSnIR(s, SnIR::SEND_OK); + W5100.writeSnIR(socket, SnIR::SEND_OK); SPI.endTransaction(); - return ret; + return result; } -uint16_t socketBufferData(uint8_t s, uint16_t offset, const uint8_t* buf, uint16_t len) +uint16_t socketBufferData(uint8_t socket, uint16_t offset, const uint8_t* buffer, uint16_t length) { //Serial.printf(" bufferData, offset=%d, len=%d\n", offset, len); - uint16_t ret =0; + uint16_t result =0; SPI.beginTransaction(SPI_ETHERNET_SETTINGS); - uint16_t txfree = getSnTX_FSR(s); - if (len > txfree) { - ret = txfree; // check size not to exceed MAX size. + uint16_t txfree = getSnTX_FSR(socket); + if (length > txfree) { + result = txfree; // check size not to exceed MAX size. } else { - ret = len; + result = length; } - send_data_processing(s, offset, buf, ret); + send_data_processing(socket, offset, buffer, result); SPI.endTransaction(); - return ret; + return result; } -int socketStartUDP(uint8_t s, uint8_t* addr, uint16_t port) +int socketStartUDP(uint8_t socket, uint8_t* address, uint16_t port) { - if ( ((addr[0] == 0x00) && (addr[1] == 0x00) && (addr[2] == 0x00) && (addr[3] == 0x00)) || + if ( ((address[0] == 0x00) && (address[1] == 0x00) && (address[2] == 0x00) && (address[3] == 0x00)) || ((port == 0x00)) ) { return 0; } SPI.beginTransaction(SPI_ETHERNET_SETTINGS); - W5100.writeSnDIPR(s, addr); - W5100.writeSnDPORT(s, port); + W5100.writeSnDIPR(socket, address); + W5100.writeSnDPORT(socket, port); SPI.endTransaction(); return 1; } -int socketSendUDP(uint8_t s) +int socketSendUDP(uint8_t socket) { SPI.beginTransaction(SPI_ETHERNET_SETTINGS); - W5100.execCmdSn(s, Sock_SEND); + W5100.execCmdSn(socket, Sock_SEND); /* +2008.01 bj */ - while ( (W5100.readSnIR(s) & SnIR::SEND_OK) != SnIR::SEND_OK ) { - if (W5100.readSnIR(s) & SnIR::TIMEOUT) { + while ( (W5100.readSnIR(socket) & SnIR::SEND_OK) != SnIR::SEND_OK ) { + if (W5100.readSnIR(socket) & SnIR::TIMEOUT) { /* +2008.01 [bj]: clear interrupt */ - W5100.writeSnIR(s, (SnIR::SEND_OK|SnIR::TIMEOUT)); + W5100.writeSnIR(socket, (SnIR::SEND_OK|SnIR::TIMEOUT)); SPI.endTransaction(); //Serial.printf("sendUDP timeout\n"); return 0; @@ -417,7 +417,7 @@ int socketSendUDP(uint8_t s) } /* +2008.01 bj */ - W5100.writeSnIR(s, SnIR::SEND_OK); + W5100.writeSnIR(socket, SnIR::SEND_OK); SPI.endTransaction(); //Serial.printf("sendUDP ok\n"); diff --git a/socket.h b/socket.h index 8b0ea53..f1713f0 100644 --- a/socket.h +++ b/socket.h @@ -7,40 +7,40 @@ void socketPortRand(uint16_t n); // Opens a socket(TCP or UDP or IP_RAW mode) uint8_t socketBegin(uint8_t protocol, uint16_t port); -uint8_t socketStatus(uint8_t s); +uint8_t socketStatus(uint8_t socket); // Close socket -void socketClose(uint8_t s); +void socketClose(uint8_t socket); // Establish TCP connection (Active connection) -void socketConnect(uint8_t s, uint8_t * addr, uint16_t port); +void socketConnect(uint8_t socket, uint8_t * address, uint16_t port); // disconnect the connection -void socketDisconnect(uint8_t s); +void socketDisconnect(uint8_t socket); // Establish TCP connection (Passive connection) -uint8_t socketListen(uint8_t s); +uint8_t socketListen(uint8_t socket); // Send data (TCP) -uint16_t socketSend(uint8_t s, const uint8_t * buf, uint16_t len); +uint16_t socketSend(uint8_t socket, const uint8_t * buffer, const uint16_t length); // Receive data (TCP) -int socketRecv(uint8_t s, uint8_t * buf, int16_t len); -uint16_t socketRecvAvailable(uint8_t s); -uint8_t socketPeek(uint8_t s); +int socketRecv(uint8_t socket, uint8_t * buffer, int16_t length); +uint16_t socketRecvAvailable(uint8_t socket); +uint8_t socketPeek(uint8_t socket); /* @brief This function sets up a UDP datagram, the data for which will be provided by one or more calls to bufferData and then finally sent with sendUDP. @return 1 if the datagram was successfully set up, or 0 if there was an error */ -int socketStartUDP(uint8_t s, uint8_t* addr, uint16_t port); +int socketStartUDP(uint8_t socket, uint8_t* address, uint16_t port); /* @brief This function copies up to len bytes of data from buf into a UDP datagram to be sent later by sendUDP. Allows datagrams to be built up from a series of bufferData calls. @return Number of bytes successfully buffered */ -uint16_t socketBufferData(uint8_t s, uint16_t offset, const uint8_t* buf, uint16_t len); +uint16_t socketBufferData(uint8_t socket, uint16_t offset, const uint8_t* buffer, uint16_t length); /* @brief Send a UDP datagram built up from a sequence of startUDP followed by one or more calls to bufferData. @return 1 if the datagram was successfully sent, or 0 if there was an error */ -int socketSendUDP(uint8_t s); +int socketSendUDP(uint8_t socket); #endif /* _SOCKET_H_ */ From 04c9a6cad11acee4e962f65310e2b81e2b299513 Mon Sep 17 00:00:00 2001 From: Sebastian Martin Dicke Date: Fri, 16 Sep 2016 11:13:00 +0200 Subject: [PATCH 07/16] Add constant expression to clearify return semantic in error case. --- EthernetUdp.cpp | 6 +++--- EthernetUdp.h | 3 ++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/EthernetUdp.cpp b/EthernetUdp.cpp index d48f965..f2b2077 100644 --- a/EthernetUdp.cpp +++ b/EthernetUdp.cpp @@ -141,7 +141,7 @@ int EthernetUDP::read() } // If we get here, there's no data available - return -1; + return ERROR; } int EthernetUDP::read(unsigned char* buffer, size_t len) @@ -163,7 +163,7 @@ int EthernetUDP::read(unsigned char* buffer, size_t len) } } // If we get here, there's no data available or recv failed - return -1; + return ERROR; } int EthernetUDP::peek() @@ -181,7 +181,7 @@ void EthernetUDP::flush() } /* Start EthernetUDP socket, listening at local port PORT */ -uint8_t EthernetUDP::beginMulticast(IPAddress ip, uint16_t port) +uint8_t EthernetUDP::beginMulticast(IPAddress ip, const uint16_t port) { if (sockindex < MAX_SOCK_NUM) socketClose(sockindex); sockindex = socketBegin(SnMR::UDP | SnMR::MULTI, port); diff --git a/EthernetUdp.h b/EthernetUdp.h index 7d8bb6e..7472a92 100644 --- a/EthernetUdp.h +++ b/EthernetUdp.h @@ -44,6 +44,7 @@ class EthernetUDP : public UDP { private: + constexpr int ERROR = -1; uint16_t _port; // local port to listen on IPAddress _remoteIP; // remote IP address for the incoming packet whilst it's being processed uint16_t _remotePort; // remote port for the incoming packet whilst it's being processed @@ -56,7 +57,7 @@ class EthernetUDP : public UDP { public: EthernetUDP() : sockindex(MAX_SOCK_NUM) {} // Constructor 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 - 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 + virtual uint8_t beginMulticast(IPAddress, const uint16_t); // initialize, start listening on specified port. Returns 1 if successful, 0 if there are no sockets available to use virtual void stop(); // Finish with the UDP socket // Sending UDP packets From c8faef8cb2b8ce6486f082fbe88864d0f51c3c82 Mon Sep 17 00:00:00 2001 From: Sebastian Martin Dicke Date: Fri, 16 Sep 2016 11:17:10 +0200 Subject: [PATCH 08/16] changed --- EthernetUdp.cpp | 4 ++-- EthernetUdp.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/EthernetUdp.cpp b/EthernetUdp.cpp index f2b2077..77a8bc8 100644 --- a/EthernetUdp.cpp +++ b/EthernetUdp.cpp @@ -171,7 +171,7 @@ int EthernetUDP::peek() // Unlike recv, peek doesn't check to see if there's any data available, so we must. // If the user hasn't called parsePacket yet then return nothing otherwise they // may get the UDP header - if (sockindex >= MAX_SOCK_NUM || _remaining == 0) return -1; + if (sockindex >= MAX_SOCK_NUM || _remaining == 0) return ERROR; return socketPeek(sockindex); } @@ -181,7 +181,7 @@ void EthernetUDP::flush() } /* Start EthernetUDP socket, listening at local port PORT */ -uint8_t EthernetUDP::beginMulticast(IPAddress ip, const uint16_t port) +uint8_t EthernetUDP::beginMulticast(IPAddress ip, uint16_t port) { if (sockindex < MAX_SOCK_NUM) socketClose(sockindex); sockindex = socketBegin(SnMR::UDP | SnMR::MULTI, port); diff --git a/EthernetUdp.h b/EthernetUdp.h index 7472a92..1c0c681 100644 --- a/EthernetUdp.h +++ b/EthernetUdp.h @@ -57,7 +57,7 @@ class EthernetUDP : public UDP { public: EthernetUDP() : sockindex(MAX_SOCK_NUM) {} // Constructor 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 - virtual uint8_t beginMulticast(IPAddress, const uint16_t); // initialize, start listening on specified port. Returns 1 if successful, 0 if there are no sockets available to use + 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 virtual void stop(); // Finish with the UDP socket // Sending UDP packets From f6d9c6883f69eabcbc6a424ffe1738a65d739306 Mon Sep 17 00:00:00 2001 From: Sebastian Martin Dicke Date: Fri, 16 Sep 2016 11:17:34 +0200 Subject: [PATCH 09/16] Replaced postfix operator by prefix one. --- EthernetServer.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/EthernetServer.cpp b/EthernetServer.cpp index f0e43d7..7741665 100644 --- a/EthernetServer.cpp +++ b/EthernetServer.cpp @@ -38,7 +38,7 @@ void EthernetServer::accept() EthernetClient EthernetServer::available() { accept(); - for (uint8_t i=0; i < MAX_SOCK_NUM; i++) { + for (uint8_t i=0; i < MAX_SOCK_NUM; ++i) { if (server_port[i] == _port) { uint8_t stat = socketStatus(i); if (stat == SnSR::ESTABLISHED || stat == SnSR::CLOSE_WAIT) { @@ -60,7 +60,7 @@ size_t EthernetServer::write(uint8_t b) size_t EthernetServer::write(const uint8_t *buffer, size_t size) { accept(); - for (uint8_t i=0; i < MAX_SOCK_NUM; i++) { + for (uint8_t i=0; i < MAX_SOCK_NUM; ++i) { if (server_port[i] == _port) { if (socketStatus(i) == SnSR::ESTABLISHED) { socketSend(i, buffer, size); From 3b9e99b70451b6b421c41b422c60f86d0a65c508 Mon Sep 17 00:00:00 2001 From: Sebastian Martin Dicke Date: Fri, 16 Sep 2016 11:22:30 +0200 Subject: [PATCH 10/16] Altered to remove syntax error. --- EthernetClient.h | 6 +++--- EthernetServer.cpp | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/EthernetClient.h b/EthernetClient.h index b46c3f0..f86415e 100644 --- a/EthernetClient.h +++ b/EthernetClient.h @@ -9,8 +9,8 @@ class EthernetClient : public Client { public: - EthernetClient() : sockindex(MAX_SOCK_NUM) { }; - EthernetClient(uint8_t s) : sockindex(s) { }; + EthernetClient() : sockindex(MAX_SOCK_NUM) { } + EthernetClient(uint8_t s) : sockindex(s) { } uint8_t status(); virtual int connect(IPAddress ip, uint16_t port); @@ -28,7 +28,7 @@ class EthernetClient : public Client { virtual bool operator==(const bool value) { return bool() == value; } virtual bool operator!=(const bool value) { return bool() != value; } virtual bool operator==(const EthernetClient&); - virtual bool operator!=(const EthernetClient& rhs) { return !this->operator==(rhs); }; + virtual bool operator!=(const EthernetClient& rhs) { return !this->operator==(rhs); } uint8_t getSocketNumber() const { return sockindex; } friend class EthernetServer; diff --git a/EthernetServer.cpp b/EthernetServer.cpp index 7741665..9a94034 100644 --- a/EthernetServer.cpp +++ b/EthernetServer.cpp @@ -38,7 +38,7 @@ void EthernetServer::accept() EthernetClient EthernetServer::available() { accept(); - for (uint8_t i=0; i < MAX_SOCK_NUM; ++i) { + for (uint8_t i = 0; i < MAX_SOCK_NUM; ++i) { if (server_port[i] == _port) { uint8_t stat = socketStatus(i); if (stat == SnSR::ESTABLISHED || stat == SnSR::CLOSE_WAIT) { From 76c89f6e1dc60f5647ca6760beca2f65adebdaf7 Mon Sep 17 00:00:00 2001 From: Sebastian Martin Dicke Date: Fri, 16 Sep 2016 11:24:24 +0200 Subject: [PATCH 11/16] Addded a constant expression to clearify error return semantic. --- EthernetClient.cpp | 4 ++-- EthernetClient.h | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/EthernetClient.cpp b/EthernetClient.cpp index 86ab26b..7baa496 100644 --- a/EthernetClient.cpp +++ b/EthernetClient.cpp @@ -72,7 +72,7 @@ int EthernetClient::read(uint8_t *buf, size_t size) int EthernetClient::peek() { - if (sockindex >= MAX_SOCK_NUM) return -1; + if (sockindex >= MAX_SOCK_NUM) return ERROR; if (!available()) return -1; return socketPeek(sockindex); } @@ -81,7 +81,7 @@ int EthernetClient::read() { uint8_t b; if (socketRecv(sockindex, &b, 1) > 0) return b; - return -1; + return ERROR; } void EthernetClient::flush() diff --git a/EthernetClient.h b/EthernetClient.h index f86415e..4ed3839 100644 --- a/EthernetClient.h +++ b/EthernetClient.h @@ -37,6 +37,7 @@ class EthernetClient : public Client { private: uint8_t sockindex; + constexpr uint8_t ERROR = -1; }; #endif From c76bf84a8216ddbfb8ae7b2add543ea707f54def Mon Sep 17 00:00:00 2001 From: Sebastian Martin Dicke Date: Fri, 16 Sep 2016 11:26:57 +0200 Subject: [PATCH 12/16] Renamed variables to adjust names to purposes. --- EthernetClient.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/EthernetClient.cpp b/EthernetClient.cpp index 7baa496..55e6c16 100644 --- a/EthernetClient.cpp +++ b/EthernetClient.cpp @@ -79,8 +79,8 @@ int EthernetClient::peek() int EthernetClient::read() { - uint8_t b; - if (socketRecv(sockindex, &b, 1) > 0) return b; + uint8_t result; + if (socketRecv(sockindex, &result, 1) > 0) return result; return ERROR; } From 568d497ec5fe816f524f9329a17afe1db767ac51 Mon Sep 17 00:00:00 2001 From: Sebastian Martin Dicke Date: Fri, 16 Sep 2016 11:29:38 +0200 Subject: [PATCH 13/16] Merged if statements into one to remove doublicated code. --- EthernetClient.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/EthernetClient.cpp b/EthernetClient.cpp index 55e6c16..89d65bb 100644 --- a/EthernetClient.cpp +++ b/EthernetClient.cpp @@ -126,9 +126,9 @@ uint8_t EthernetClient::status() // EthernetServer::available() as the condition in an if-statement. bool EthernetClient::operator==(const EthernetClient& rhs) { - if (sockindex != rhs.sockindex) return false; - if (sockindex >= MAX_SOCK_NUM) return false; - if (rhs.sockindex >= MAX_SOCK_NUM) return false; + if ((sockindex != rhs.sockindex) || (sockindex >= MAX_SOCK_NUM) || (rhs.sockindex >= MAX_SOCK_NUM)) { + return false; + } return true; } From 3b37d416549ffcf4b0e69bc2d97e2b42da6ccc52 Mon Sep 17 00:00:00 2001 From: Sebastian Martin Dicke Date: Fri, 16 Sep 2016 11:32:25 +0200 Subject: [PATCH 14/16] Improved variable naming. --- Ethernet.cpp | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/Ethernet.cpp b/Ethernet.cpp index 06973f2..e03996d 100644 --- a/Ethernet.cpp +++ b/Ethernet.cpp @@ -18,8 +18,8 @@ int EthernetClass::begin(uint8_t *mac, unsigned long timeout, unsigned long resp SPI.endTransaction(); // Now try to get our config info from a DHCP server - int ret = _dhcp->beginWithDHCP(mac, timeout, responseTimeout); - if (ret == 1) { + int result = _dhcp->beginWithDHCP(mac, timeout, responseTimeout); + if (result == 1) { // We've successfully found a DHCP server and got our configuration // info, so set things accordingly SPI.beginTransaction(SPI_ETHERNET_SETTINGS); @@ -30,7 +30,7 @@ int EthernetClass::begin(uint8_t *mac, unsigned long timeout, unsigned long resp _dnsServerAddress = _dhcp->getDnsServerIp(); socketPortRand(micros()); } - return ret; + return result; } void EthernetClass::begin(uint8_t *mac, IPAddress ip) @@ -77,11 +77,11 @@ void EthernetClass::begin(uint8_t *mac, IPAddress ip, IPAddress dns, IPAddress g int EthernetClass::maintain() { - int rc = DHCP_CHECK_NONE; + int result = DHCP_CHECK_NONE; if (_dhcp != NULL) { // we have a pointer to dhcp, use it - rc = _dhcp->checkLease(); - switch (rc) { + result = _dhcp->checkLease(); + switch (result) { case DHCP_CHECK_NONE: //nothing done break; @@ -100,34 +100,34 @@ int EthernetClass::maintain() break; } } - return rc; + return result; } IPAddress EthernetClass::localIP() { - IPAddress ret; + IPAddress address; SPI.beginTransaction(SPI_ETHERNET_SETTINGS); - W5100.getIPAddress(ret.raw_address()); + W5100.getIPAddress(address.raw_address()); SPI.endTransaction(); - return ret; + return address; } IPAddress EthernetClass::subnetMask() { - IPAddress ret; + IPAddress address; SPI.beginTransaction(SPI_ETHERNET_SETTINGS); - W5100.getSubnetMask(ret.raw_address()); + W5100.getSubnetMask(address.raw_address()); SPI.endTransaction(); - return ret; + return address; } IPAddress EthernetClass::gatewayIP() { - IPAddress ret; + IPAddress result; SPI.beginTransaction(SPI_ETHERNET_SETTINGS); - W5100.getGatewayIp(ret.raw_address()); + W5100.getGatewayIp(result.raw_address()); SPI.endTransaction(); - return ret; + return result; } From d386fb53d9e7ca1422a23310919a39b9b20e8037 Mon Sep 17 00:00:00 2001 From: Sebastian Martin Dicke Date: Fri, 16 Sep 2016 11:33:25 +0200 Subject: [PATCH 15/16] Changed all NULLs to nullptrs. --- Ethernet.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Ethernet.cpp b/Ethernet.cpp index e03996d..24dd1ee 100644 --- a/Ethernet.cpp +++ b/Ethernet.cpp @@ -3,7 +3,7 @@ #include "Dhcp.h" IPAddress EthernetClass::_dnsServerAddress; -DhcpClass* EthernetClass::_dhcp = NULL; +DhcpClass* EthernetClass::_dhcp = nullptr; int EthernetClass::begin(uint8_t *mac, unsigned long timeout, unsigned long responseTimeout) { @@ -78,7 +78,7 @@ void EthernetClass::begin(uint8_t *mac, IPAddress ip, IPAddress dns, IPAddress g int EthernetClass::maintain() { int result = DHCP_CHECK_NONE; - if (_dhcp != NULL) { + if (_dhcp != nullptr) { // we have a pointer to dhcp, use it result = _dhcp->checkLease(); switch (result) { From 29926cc7b98200d1e7d2b19ba54dca7256e09dfb Mon Sep 17 00:00:00 2001 From: Sebastian Martin Dicke Date: Fri, 16 Sep 2016 11:37:54 +0200 Subject: [PATCH 16/16] Made unchanged function arguments constant. --- Ethernet.cpp | 10 +++++----- Ethernet.h | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/Ethernet.cpp b/Ethernet.cpp index 24dd1ee..b0d3494 100644 --- a/Ethernet.cpp +++ b/Ethernet.cpp @@ -5,7 +5,7 @@ IPAddress EthernetClass::_dnsServerAddress; DhcpClass* EthernetClass::_dhcp = nullptr; -int EthernetClass::begin(uint8_t *mac, unsigned long timeout, unsigned long responseTimeout) +int EthernetClass::begin(uint8_t *mac, const unsigned long timeout, const unsigned long responseTimeout) { static DhcpClass s_dhcp; _dhcp = &s_dhcp; @@ -33,7 +33,7 @@ int EthernetClass::begin(uint8_t *mac, unsigned long timeout, unsigned long resp return result; } -void EthernetClass::begin(uint8_t *mac, IPAddress ip) +void EthernetClass::begin(uint8_t *mac, const IPAddress ip) { // Assume the DNS server will be the machine on the same network as the local IP // but with last octet being '1' @@ -42,7 +42,7 @@ void EthernetClass::begin(uint8_t *mac, IPAddress ip) begin(mac, ip, dns); } -void EthernetClass::begin(uint8_t *mac, IPAddress ip, IPAddress dns) +void EthernetClass::begin(uint8_t *mac, const IPAddress ip, const IPAddress dns) { // Assume the gateway will be the machine on the same network as the local IP // but with last octet being '1' @@ -51,13 +51,13 @@ void EthernetClass::begin(uint8_t *mac, IPAddress ip, IPAddress dns) begin(mac, ip, dns, gateway); } -void EthernetClass::begin(uint8_t *mac, IPAddress ip, IPAddress dns, IPAddress gateway) +void EthernetClass::begin(uint8_t *mac, const IPAddress ip, const IPAddress dns, const IPAddress gateway) { IPAddress subnet(255, 255, 255, 0); begin(mac, ip, dns, gateway, subnet); } -void EthernetClass::begin(uint8_t *mac, IPAddress ip, IPAddress dns, IPAddress gateway, IPAddress subnet) +void EthernetClass::begin(uint8_t *mac, const IPAddress ip, const IPAddress dns, const IPAddress gateway, const IPAddress subnet) { W5100.init(); SPI.beginTransaction(SPI_ETHERNET_SETTINGS); diff --git a/Ethernet.h b/Ethernet.h index 6274a0e..c10b6a3 100644 --- a/Ethernet.h +++ b/Ethernet.h @@ -17,14 +17,14 @@ class EthernetClass { // Initialise the Ethernet shield to use the provided MAC address and // gain the rest of the configuration through DHCP. // Returns 0 if the DHCP configuration failed, and 1 if it succeeded - static int begin(uint8_t *mac, unsigned long timeout = 60000, unsigned long responseTimeout = 4000); + static int begin(uint8_t *mac, const unsigned long timeout = 60000, const unsigned long responseTimeout = 4000); static int maintain(); // Manaul configuration - static void begin(uint8_t *mac, IPAddress ip); - static void begin(uint8_t *mac, IPAddress ip, IPAddress dns); - static void begin(uint8_t *mac, IPAddress ip, IPAddress dns, IPAddress gateway); - static void begin(uint8_t *mac, IPAddress ip, IPAddress dns, IPAddress gateway, IPAddress subnet); + static void begin(uint8_t *mac, const IPAddress, const ip); + static void begin(uint8_t *mac, const IPAddress ip, const IPAddress dns); + static void begin(uint8_t *mac, const IPAddress ip, const IPAddress dns, IPAddress gateway); + static void begin(uint8_t *mac, const IPAddress ip, const IPAddress dns, const IPAddress gateway, const IPAddress subnet); static IPAddress localIP(); static IPAddress subnetMask();