Fork for fixes

Files at this revision

API Documentation at this revision

Comitter:
ivo_n
Date:
Fri Sep 25 21:21:31 2020 +0000
Parent:
19:58e840279555
Child:
22:a0b1d0e6d237
Commit message:
trying to fix Spurious TCP Dup Acks by clearing sent frames.

Changed in this revision

DhcpClient.cpp Show annotated file Show diff for this revision Revisions of this file
DhcpClient.h Show annotated file Show diff for this revision Revisions of this file
UipEthernet.cpp Show annotated file Show diff for this revision Revisions of this file
UipEthernet.h Show annotated file Show diff for this revision Revisions of this file
--- a/DhcpClient.cpp	Thu Sep 24 21:14:56 2020 +0000
+++ b/DhcpClient.cpp	Fri Sep 25 21:21:31 2020 +0000
@@ -2,7 +2,6 @@
 // Author: Jordan Terrell - blog.jordanterrell.com
 #include <string.h>
 #include <stdlib.h>
-#include "UipEthernet.h"
 #include "DhcpClient.h"
 #include "utility/util.h"
 
@@ -45,78 +44,54 @@
 //return:0 on error, 1 if request is sent and response is received
 int DhcpClient::requestDhcpLease()
 {
-#ifdef UIPETHERNET_DEBUG_UDP
-    printf("requestDhcpLease(): begin\r\n");
-#endif
     uint8_t messageType = 0;
-    Timer           timer;
-
-    timer.reset();
-    timer.start();
 
     // Pick an initial transaction ID
-    srand(time(NULL)+ 1);
+    srand(time(NULL));
     _dhcpTransactionId = (rand() % 2000UL) + 1;
     _dhcpInitialTransactionId = _dhcpTransactionId;
 
     _dhcpUdpSocket.stop();
     if (_dhcpUdpSocket.begin(DHCP_CLIENT_PORT) == 0) {
         // Couldn't get a socket
-#ifdef UIPETHERNET_DEBUG_UDP
-        printf("requestDhcpLease(): Couldn't get a socket\r\n");
-#endif
         return 0;
     }
 
     presendDhcp();
 
     volatile int    result = 0;
+    time_t          startTime = time(NULL);
 
     while (_dhcp_state != STATE_DHCP_LEASED) {
         if (_dhcp_state == STATE_DHCP_START) {
-#ifdef UIPETHERNET_DEBUG_UDP
-            printf("_dhcp_state: STATE_DHCP_START\r\n");
-#endif
             _dhcpTransactionId++;
 
-            sendDhcpMessage(DHCP_DISCOVER, (timer.read_ms() / 1000 + 1));
+            sendDhcpMessage(DHCP_DISCOVER, (time(NULL) - startTime));
             _dhcp_state = STATE_DHCP_DISCOVER;
         }
         else
         if (_dhcp_state == STATE_DHCP_REREQUEST) {
-#ifdef UIPETHERNET_DEBUG_UDP
-            printf("_dhcp_state: STATE_DHCP_REREQUEST\r\n");
-#endif
             _dhcpTransactionId++;
-            sendDhcpMessage(DHCP_REQUEST, (timer.read_ms() / 1000 + 1));
+            sendDhcpMessage(DHCP_REQUEST, (time(NULL) - startTime));
             _dhcp_state = STATE_DHCP_REQUEST;
         }
         else
         if (_dhcp_state == STATE_DHCP_DISCOVER) {
-#ifdef UIPETHERNET_DEBUG_UDP
-            printf("_dhcp_state: STATE_DHCP_DISCOVER\r\n");
-#endif
             uint32_t    respId;
             messageType = parseDhcpResponse(_responseTimeout, respId);
             if (messageType == DHCP_OFFER) {
                 // We'll use the transaction ID that the offer came with,
                 // rather than the one we were up to
                 _dhcpTransactionId = respId;
-                sendDhcpMessage(DHCP_REQUEST, (timer.read_ms() / 1000 + 1));
+                sendDhcpMessage(DHCP_REQUEST, (time(NULL) - startTime));
                 _dhcp_state = STATE_DHCP_REQUEST;
             }
         }
         else
         if (_dhcp_state == STATE_DHCP_REQUEST) {
-#ifdef UIPETHERNET_DEBUG_UDP
-            printf("_dhcp_state: STATE_DHCP_REQUEST\r\n");
-#endif
             uint32_t    respId;
             messageType = parseDhcpResponse(_responseTimeout, respId);
             if (messageType == DHCP_ACK) {
-#ifdef UIPETHERNET_DEBUG_UDP
-                printf("messageType: DHCP_ACK\r\n");
-#endif
                 _dhcp_state = STATE_DHCP_LEASED;
                 result = 1;
 
@@ -140,37 +115,23 @@
                 _rebindInSec = _dhcpT2;
             }
             else
-            if (messageType == DHCP_NAK) {
- #ifdef UIPETHERNET_DEBUG_UDP
-                printf("messageType: DHCP_NAK\r\n");
-#endif
+            if (messageType == DHCP_NAK)
                 _dhcp_state = STATE_DHCP_START;
-            }
         }
 
         if (messageType == 255) {
-#ifdef UIPETHERNET_DEBUG_UDP
-            printf("DHCP_NAK: 255\r\n");
-#endif
             messageType = 0;
             _dhcp_state = STATE_DHCP_START;
         }
 
-        if ((result != 1) && ((timer.read_ms() / 1000) > _timeout)) {
-#ifdef UIPETHERNET_DEBUG_UDP
-            printf("requestDhcpLease(): timeout\r\n");
-#endif
+        if ((result != 1) && ((time(NULL) - startTime) > _timeout))
             break;
-        }
     }
 
     // We're done with the socket now
     _dhcpUdpSocket.stop();
     _dhcpTransactionId++;
 
-#ifdef UIPETHERNET_DEBUG_UDP
-    printf("requestDhcpLease(): %d\r\n", result);
-#endif
     return result;
 }
 
@@ -221,17 +182,17 @@
     // yiaddr: already zeroed
     // siaddr: already zeroed
     // giaddr: already zeroed
-    //put data in ENC28J60 transmit buffer
+    //put data in W5100 transmit buffer
     _dhcpUdpSocket.write(buffer, 28);
 
     memset(buffer, 0, 32);                      // clear local buffer
     memcpy(buffer, _dhcpMacAddr, 6);            // chaddr
-    //put data in ENC28J60 transmit buffer
+    //put data in W5100 transmit buffer
     _dhcpUdpSocket.write(buffer, 16);
 
     memset(buffer, 0, 32);                      // clear local buffer
     // leave zeroed out for sname && file
-    // put in ENC28J60 transmit buffer x 6 (192 bytes)
+    // put in W5100 transmit buffer x 6 (192 bytes)
     for (int i = 0; i < 6; i++) {
         _dhcpUdpSocket.write(buffer, 32);
     }
@@ -261,7 +222,7 @@
     printByte((char*) &(buffer[26]), _dhcpMacAddr[4]);
     printByte((char*) &(buffer[28]), _dhcpMacAddr[5]);
 
-    //put data in ENC28J60 transmit buffer
+    //put data in W5100 transmit buffer
     _dhcpUdpSocket.write(buffer, 30);
 
     if (messageType == DHCP_REQUEST) {
@@ -281,22 +242,23 @@
         buffer[10] = _dhcpDhcpServerIp[2];
         buffer[11] = _dhcpDhcpServerIp[3];
 
-        //put data in ENC28J60 transmit buffer
+        //put data in W5100 transmit buffer
         _dhcpUdpSocket.write(buffer, 12);
     }
 
     buffer[0] = dhcpParamRequest;
-    buffer[1] = 0x06;
+    buffer[1] = 0x07;
     buffer[2] = subnetMask;
     buffer[3] = routersOnSubnet;
     buffer[4] = dns;
     buffer[5] = domainName;
     buffer[6] = dhcpT1value;
     buffer[7] = dhcpT2value;
-    buffer[8] = endOption;
+    buffer[8] = userMqtt;
+    buffer[9] = endOption;
 
-    //put data in ENC28J60 transmit buffer
-    _dhcpUdpSocket.write(buffer, 9);
+    //put data in W5100 transmit buffer
+    _dhcpUdpSocket.write(buffer, 10);
 
     _dhcpUdpSocket.endPacket();
 }
@@ -311,19 +273,17 @@
 {
     volatile uint8_t    type = 0;
     uint8_t             opt_len = 0;
-    Timer               timer;
 
-    timer.start();
+    unsigned long       startTime = time(NULL);
 
     while (_dhcpUdpSocket.parsePacket() <= 0) {
-        if (timer.read() > responseTimeout) {
+        if ((time(NULL) - startTime) > responseTimeout) {
             return 255;
         }
-#if MBED_MAJOR_VERSION == 2
-        wait_ms(50);
-#else
-        thread_sleep_for(50);
-#endif
+      
+//        wait_ms(50);                // wait_ms is depecated
+//        ThisThread::sleep_for(50);  // sleep_for is not IRQ safe
+          wait_us(50000);             // wait_us is IRQ safe and not deprecated
     }
 
     // start reading in the packet
@@ -418,6 +378,15 @@
                     _renewInSec = _dhcpLeaseTime;
                     break;
 
+                case userMqtt:
+                    opt_len = _dhcpUdpSocket.read();
+                    _dhcpUdpSocket.read(_dhcpUserMqttIP, 4);
+                    for (int i = 0; i < opt_len - 4; i++) {
+                        _dhcpUdpSocket.read();
+                    }
+                    printf("userMqtt\n\r");
+                    break;
+
                 default:
                     opt_len = _dhcpUdpSocket.read();
 
@@ -555,6 +524,17 @@
  * @param
  * @retval
  */
+IpAddress DhcpClient::getMqttServerIp()
+{
+    return IpAddress(_dhcpUserMqttIP);
+}
+
+/**
+ * @brief
+ * @note
+ * @param
+ * @retval
+ */
 void DhcpClient::printByte(char* buf, uint8_t n)
 {
     char*   str = &buf[1];
--- a/DhcpClient.h	Thu Sep 24 21:14:56 2020 +0000
+++ b/DhcpClient.h	Fri Sep 25 21:21:31 2020 +0000
@@ -122,6 +122,7 @@
     dhcpT2value             = 59,
     dhcpClassIdentifier     = 60,
     dhcpClientIdentifier    = 61,
+    userMqtt                = 224,
     endOption               = 255
 };
 
@@ -154,6 +155,7 @@
     uint8_t     _dhcpDnsServerIp[4];
     uint32_t    _dhcpLeaseTime;
     uint32_t    _dhcpT1, _dhcpT2;
+    uint8_t     _dhcpUserMqttIP[4];
     time_t      _renewInSec;
     time_t      _rebindInSec;
     time_t      _lastCheck;
@@ -177,6 +179,7 @@
     IpAddress   getGatewayIp();
     IpAddress   getDhcpServerIp();
     IpAddress   getDnsServerIp();
+    IpAddress   getMqttServerIp();
 
     int         begin(uint8_t* mac, unsigned long timeout = 60, unsigned long responseTimeout = 4);
     int         checkLease();
--- a/UipEthernet.cpp	Thu Sep 24 21:14:56 2020 +0000
+++ b/UipEthernet.cpp	Fri Sep 25 21:21:31 2020 +0000
@@ -380,6 +380,18 @@
  * @param
  * @retval
  */
+const char* UipEthernet::get_mqttFromDhcp()
+{
+  static char buf[16];
+  return dhcpClient.getMqttServerIp().toString(buf);
+}
+
+/**
+ * @brief
+ * @note
+ * @param
+ * @retval
+ */
 void UipEthernet::tick()
 {
     if (inPacket == NOBLOCK) {
--- a/UipEthernet.h	Thu Sep 24 21:14:56 2020 +0000
+++ b/UipEthernet.h	Fri Sep 25 21:21:31 2020 +0000
@@ -69,6 +69,7 @@
     void              get_netmask(SocketAddress* addr);
     const char*       get_gateway();
     void              get_gateway(SocketAddress* addr);
+    const char*       get_mqttFromDhcp();
     static uint16_t   chksum(uint16_t sum, const uint8_t* data, uint16_t len);
     static uint16_t   ipchksum();
     bool              stoip4(const char *ip4addr, size_t len, void *dest);