mbed OS5

Fork of UIPEthernet by Zoltan Hudak

Revision:
8:4acb22344932
Parent:
7:1bc7e6120801
--- a/Dhcp.cpp	Tue Apr 26 18:37:14 2016 +0000
+++ b/Dhcp.cpp	Fri Jun 30 19:51:28 2017 +0000
@@ -52,7 +52,7 @@
     _dhcpInitialTransactionId = _dhcpTransactionId;
 
     _dhcpUdpSocket.stop();
-    if(_dhcpUdpSocket.begin(DHCP_CLIENT_PORT) == 0) {
+    if (_dhcpUdpSocket.begin(DHCP_CLIENT_PORT) == 0) {
 
         // Couldn't get a socket
         return 0;
@@ -60,28 +60,28 @@
 
     presend_DHCP();
 
-    int             result = 0;
+    volatile int    result = 0;
 
     unsigned long   startTime = millis();
 
-    while(_dhcp_state != STATE_DHCP_LEASED) {
-        if(_dhcp_state == STATE_DHCP_START) {
+    while (_dhcp_state != STATE_DHCP_LEASED) {
+        if (_dhcp_state == STATE_DHCP_START) {
             _dhcpTransactionId++;
 
             send_DHCP_MESSAGE(DHCP_DISCOVER, ((millis() - startTime) / 1000));
             _dhcp_state = STATE_DHCP_DISCOVER;
         }
         else
-        if(_dhcp_state == STATE_DHCP_REREQUEST) {
+        if (_dhcp_state == STATE_DHCP_REREQUEST) {
             _dhcpTransactionId++;
             send_DHCP_MESSAGE(DHCP_REQUEST, ((millis() - startTime) / 1000));
             _dhcp_state = STATE_DHCP_REQUEST;
         }
         else
-        if(_dhcp_state == STATE_DHCP_DISCOVER) {
+        if (_dhcp_state == STATE_DHCP_DISCOVER) {
             uint32_t    respId;
             messageType = parseDHCPResponse(_responseTimeout, respId);
-            if(messageType == DHCP_OFFER) {
+            if (messageType == DHCP_OFFER) {
 
                 // We'll use the transaction ID that the offer came with,
                 // rather than the one we were up to
@@ -91,26 +91,26 @@
             }
         }
         else
-        if(_dhcp_state == STATE_DHCP_REQUEST) {
+        if (_dhcp_state == STATE_DHCP_REQUEST) {
             uint32_t    respId;
             messageType = parseDHCPResponse(_responseTimeout, respId);
-            if(messageType == DHCP_ACK) {
+            if (messageType == DHCP_ACK) {
                 _dhcp_state = STATE_DHCP_LEASED;
                 result = 1;
 
                 //use default lease time if we didn't get it
-                if(_dhcpLeaseTime == 0) {
+                if (_dhcpLeaseTime == 0) {
                     _dhcpLeaseTime = DEFAULT_LEASE;
                 }
 
                 //calculate T1 & T2 if we didn't get it
-                if(_dhcpT1 == 0) {
+                if (_dhcpT1 == 0) {
 
                     //T1 should be 50% of _dhcpLeaseTime
                     _dhcpT1 = _dhcpLeaseTime >> 1;
                 }
 
-                if(_dhcpT2 == 0) {
+                if (_dhcpT2 == 0) {
 
                     //T2 should be 87.5% (7/8ths) of _dhcpLeaseTime
                     _dhcpT2 = _dhcpT1 << 1;
@@ -120,16 +120,16 @@
                 _rebindInSec = _dhcpT2;
             }
             else
-            if(messageType == DHCP_NAK)
+            if (messageType == DHCP_NAK)
                 _dhcp_state = STATE_DHCP_START;
         }
 
-        if(messageType == 255) {
+        if (messageType == 255) {
             messageType = 0;
             _dhcp_state = STATE_DHCP_START;
         }
 
-        if(result != 1 && ((millis() - startTime) > _timeout))
+        if ((result != 1) && ((millis() - startTime) > _timeout))
             break;
     }
 
@@ -161,7 +161,7 @@
 
     IPAddress   dest_addr(255, 255, 255, 255);  // Broadcast address
 
-    if(-1 == _dhcpUdpSocket.beginPacket(dest_addr, DHCP_SERVER_PORT)) {
+    if (-1 == _dhcpUdpSocket.beginPacket(dest_addr, DHCP_SERVER_PORT)) {
 
         // FIXME Need to return errors
         return;
@@ -201,7 +201,7 @@
 
     // leave zeroed out for sname && file
     // put in W5100 transmit buffer x 6 (192 bytes)
-    for(int i = 0; i < 6; i++) {
+    for (int i = 0; i < 6; i++) {
         _dhcpUdpSocket.write(buffer, 32);
     }
 
@@ -234,7 +234,7 @@
     //put data in W5100 transmit buffer
     _dhcpUdpSocket.write(buffer, 30);
 
-    if(messageType == DHCP_REQUEST) {
+    if (messageType == DHCP_REQUEST) {
         buffer[0] = dhcpRequestedIPaddr;
         buffer[1] = 0x04;
         buffer[2] = _dhcpLocalIp[0];
@@ -244,6 +244,7 @@
 
         buffer[6] = dhcpServerIdentifier;
         buffer[7] = 0x04;
+
         //buffer[8] = _dhcpDhcpServerIp[0];
         buffer[8] = _dhcpLocalIp[0];
         buffer[9] = _dhcpDhcpServerIp[1];
@@ -277,13 +278,13 @@
  * @retval
  */
 uint8_t DhcpClass::parseDHCPResponse(unsigned long responseTimeout, uint32_t& transactionId) {
-    uint8_t         type = 0;
-    uint8_t         opt_len = 0;
+    volatile uint8_t    type = 0;
+    uint8_t             opt_len = 0;
 
-    unsigned long   startTime = millis();
+    unsigned long       startTime = millis();
 
-    while(_dhcpUdpSocket.parsePacket() <= 0) {
-        if((millis() - startTime) > responseTimeout) {
+    while (_dhcpUdpSocket.parsePacket() <= 0) {
+        if ((millis() - startTime) > responseTimeout) {
             return 255;
         }
 
@@ -294,7 +295,7 @@
     RIP_MSG_FIXED   fixedMsg;
     _dhcpUdpSocket.read((uint8_t*) &fixedMsg, sizeof(RIP_MSG_FIXED));
 
-    if(fixedMsg.op == DHCP_BOOTREPLY && _dhcpUdpSocket.remotePort() == DHCP_SERVER_PORT) {
+    if (fixedMsg.op == DHCP_BOOTREPLY && _dhcpUdpSocket.remotePort() == DHCP_SERVER_PORT) {
         transactionId = ntohl(fixedMsg.xid);
         if
         (
@@ -313,85 +314,85 @@
         // Skip to the option part
         // Doing this a byte at a time so we don't have to put a big buffer
         // on the stack (as we don't have lots of memory lying around)
-        for(int i = 0; i < (240 - (int)sizeof(RIP_MSG_FIXED)); i++) {
+        for (int i = 0; i < (240 - (int)sizeof(RIP_MSG_FIXED)); i++) {
             _dhcpUdpSocket.read();  // we don't care about the returned byte
         }
 
-        while(_dhcpUdpSocket.available() > 0) {
-            switch(_dhcpUdpSocket.read()) {
-            case endOption:
-                break;
+        while (_dhcpUdpSocket.available() > 0) {
+            switch (_dhcpUdpSocket.read()) {
+                case endOption:
+                    break;
 
-            case padOption:
-                break;
+                case padOption:
+                    break;
+
+                case dhcpMessageType:
+                    opt_len = _dhcpUdpSocket.read();
+                    type = _dhcpUdpSocket.read();
+                    break;
 
-            case dhcpMessageType:
-                opt_len = _dhcpUdpSocket.read();
-                type = _dhcpUdpSocket.read();
-                break;
+                case subnetMask:
+                    opt_len = _dhcpUdpSocket.read();
+                    _dhcpUdpSocket.read(_dhcpSubnetMask, 4);
+                    break;
 
-            case subnetMask:
-                opt_len = _dhcpUdpSocket.read();
-                _dhcpUdpSocket.read(_dhcpSubnetMask, 4);
-                break;
+                case routersOnSubnet:
+                    opt_len = _dhcpUdpSocket.read();
+                    _dhcpUdpSocket.read(_dhcpGatewayIp, 4);
+                    for (int i = 0; i < opt_len - 4; i++) {
+                        _dhcpUdpSocket.read();
+                    }
+                    break;
+
+                case dns:
+                    opt_len = _dhcpUdpSocket.read();
+                    _dhcpUdpSocket.read(_dhcpDnsServerIp, 4);
+                    for (int i = 0; i < opt_len - 4; i++) {
+                        _dhcpUdpSocket.read();
+                    }
+                    break;
 
-            case routersOnSubnet:
-                opt_len = _dhcpUdpSocket.read();
-                _dhcpUdpSocket.read(_dhcpGatewayIp, 4);
-                for(int i = 0; i < opt_len - 4; i++) {
-                    _dhcpUdpSocket.read();
-                }
-                break;
+                case dhcpServerIdentifier:
+                    opt_len = _dhcpUdpSocket.read();
+                    if (*((uint32_t*)_dhcpDhcpServerIp) == 0 || IPAddress(_dhcpDhcpServerIp) == _dhcpUdpSocket.remoteIP()) {
+                        _dhcpUdpSocket.read(_dhcpDhcpServerIp, sizeof(_dhcpDhcpServerIp));
+                    }
+                    else {
+
+                        // Skip over the rest of this option
+                        while (opt_len--) {
+                            _dhcpUdpSocket.read();
+                        }
+                    }
+                    break;
 
-            case dns:
-                opt_len = _dhcpUdpSocket.read();
-                _dhcpUdpSocket.read(_dhcpDnsServerIp, 4);
-                for(int i = 0; i < opt_len - 4; i++) {
-                    _dhcpUdpSocket.read();
-                }
-                break;
+                case dhcpT1value:
+                    opt_len = _dhcpUdpSocket.read();
+                    _dhcpUdpSocket.read((uint8_t*) &_dhcpT1, sizeof(_dhcpT1));
+                    _dhcpT1 = ntohl(_dhcpT1);
+                    break;
 
-            case dhcpServerIdentifier:
-                opt_len = _dhcpUdpSocket.read();
-                if(*((uint32_t*)_dhcpDhcpServerIp) == 0 || IPAddress(_dhcpDhcpServerIp) == _dhcpUdpSocket.remoteIP()) {
-                    _dhcpUdpSocket.read(_dhcpDhcpServerIp, sizeof(_dhcpDhcpServerIp));
-                }
-                else {
+                case dhcpT2value:
+                    opt_len = _dhcpUdpSocket.read();
+                    _dhcpUdpSocket.read((uint8_t*) &_dhcpT2, sizeof(_dhcpT2));
+                    _dhcpT2 = ntohl(_dhcpT2);
+                    break;
+
+                case dhcpIPaddrLeaseTime:
+                    opt_len = _dhcpUdpSocket.read();
+                    _dhcpUdpSocket.read((uint8_t*) &_dhcpLeaseTime, sizeof(_dhcpLeaseTime));
+                    _dhcpLeaseTime = ntohl(_dhcpLeaseTime);
+                    _renewInSec = _dhcpLeaseTime;
+                    break;
+
+                default:
+                    opt_len = _dhcpUdpSocket.read();
 
                     // Skip over the rest of this option
-                    while(opt_len--) {
+                    while (opt_len--) {
                         _dhcpUdpSocket.read();
                     }
-                }
-                break;
-
-            case dhcpT1value:
-                opt_len = _dhcpUdpSocket.read();
-                _dhcpUdpSocket.read((uint8_t*) &_dhcpT1, sizeof(_dhcpT1));
-                _dhcpT1 = ntohl(_dhcpT1);
-                break;
-
-            case dhcpT2value:
-                opt_len = _dhcpUdpSocket.read();
-                _dhcpUdpSocket.read((uint8_t*) &_dhcpT2, sizeof(_dhcpT2));
-                _dhcpT2 = ntohl(_dhcpT2);
-                break;
-
-            case dhcpIPaddrLeaseTime:
-                opt_len = _dhcpUdpSocket.read();
-                _dhcpUdpSocket.read((uint8_t*) &_dhcpLeaseTime, sizeof(_dhcpLeaseTime));
-                _dhcpLeaseTime = ntohl(_dhcpLeaseTime);
-                _renewInSec = _dhcpLeaseTime;
-                break;
-
-            default:
-                opt_len = _dhcpUdpSocket.read();
-
-                // Skip over the rest of this option
-                while(opt_len--) {
-                    _dhcpUdpSocket.read();
-                }
-                break;
+                    break;
             }
         }
     }
@@ -415,15 +416,15 @@
     //this uses a signed / unsigned trick to deal with millis overflow
     unsigned long   now = millis();
     signed long     snow = (long)now;
-    int             rc = DHCP_CHECK_NONE;
-    if(_lastCheck != 0) {
+    volatile int    rc = DHCP_CHECK_NONE;
+    if (_lastCheck != 0) {
         signed long factor;
         //calc how many ms past the timeout we are
 
         factor = snow - (long)_secTimeout;
 
         //if on or passed the timeout, reduce the counters
-        if(factor >= 0) {
+        if (factor >= 0) {
 
             //next timeout should be now plus 1000 ms minus parts of second in factor
             _secTimeout = snow + 1000 - factor % 1000;
@@ -435,25 +436,25 @@
             //if we can assume that the cycle time (factor) is fairly constant
             //and if the remainder is less than cycle time * 2
             //do it early instead of late
-            if(_renewInSec < factor * 2)
+            if (_renewInSec < factor * 2)
                 _renewInSec = 0;
             else
                 _renewInSec -= factor;
 
-            if(_rebindInSec < factor * 2)
+            if (_rebindInSec < factor * 2)
                 _rebindInSec = 0;
             else
                 _rebindInSec -= factor;
         }
 
         //if we have a lease but should renew, do it
-        if(_dhcp_state == STATE_DHCP_LEASED && _renewInSec <= 0) {
+        if (_dhcp_state == STATE_DHCP_LEASED && _renewInSec <= 0) {
             _dhcp_state = STATE_DHCP_REREQUEST;
             rc = 1 + request_DHCP_lease();
         }
 
         //if we have a lease or is renewing but should bind, do it
-        if((_dhcp_state == STATE_DHCP_LEASED || _dhcp_state == STATE_DHCP_START) && _rebindInSec <= 0) {
+        if ((_dhcp_state == STATE_DHCP_LEASED || _dhcp_state == STATE_DHCP_START) && _rebindInSec <= 0) {
 
             //this should basically restart completely
             _dhcp_state = STATE_DHCP_START;
@@ -528,12 +529,11 @@
 void DhcpClass::printByte(char* buf, uint8_t n) {
     char*   str = &buf[1];
     buf[0] = '0';
-    do
-    {
+    do {
         unsigned long   m = n;
         n /= 16;
 
         char    c = m - 16 * n;
         *str-- = c < 10 ? c + '0' : c + 'A' - 10;
-    } while(n);
+    } while (n);
 }