A stack which works with or without an Mbed os library. Provides IPv4 or IPv6 with a full 1500 byte buffer.

Dependents:   oldheating gps motorhome heating

Revision:
37:793b39683406
Parent:
36:900e24b27bfb
Child:
43:bc028d5a6424
--- a/udp/dhcp.cpp	Mon Sep 25 07:09:32 2017 +0000
+++ b/udp/dhcp.cpp	Wed Oct 04 07:51:02 2017 +0000
@@ -1,12 +1,13 @@
-#include "mbed.h"
-#include  "log.h"
-#include  "net.h"
-#include  "eth.h"
-#include  "ip4.h"
-#include  "mac.h"
-#include  "udp.h"
+#include   "mbed.h"
+#include    "log.h"
+#include    "net.h"
+#include "action.h"
+#include    "eth.h"
+#include    "ip4.h"
+#include    "mac.h"
+#include    "udp.h"
 
-#define DEBUG false
+bool DhcpTrace = false;
 
 #define REQUEST   1
 #define REPLY     2
@@ -136,7 +137,7 @@
             case 58:              DhcpRenewalT1   = readOption32(&p);   break;  //T1
             case 59:              DhcpRenewalT2   = readOption32(&p);   break;  //T2
             default:
-                if (DEBUG) LogTimeF("Ignoring option %d\r\n", *p);
+                if (DhcpTrace) LogTimeF("Ignoring option %d\r\n", *p);
                 len = *++p;
                 p += len;
                 return;
@@ -165,16 +166,16 @@
     switch (code)
     {
         case DHCPDISCOVER:
-            if (DEBUG) LogTimeF("DHCP -> discover");
+            if (DhcpTrace) LogTimeF("DHCP -> discover");
             break;
         case DHCPREQUEST:
-            if (DEBUG) LogTimeF("DHCP -> request");
+            if (DhcpTrace) LogTimeF("DHCP -> request");
             break;
         default:
             LogTimeF("DHCP -> unknown message %d", code);
             break;
     }
-    if (DEBUG)
+    if (DhcpTrace)
     {
         char text[20];
         Ip4AddressToString(srvIp, sizeof(text), text);
@@ -218,7 +219,7 @@
     delayTime = 0;
     return HEADER_LENGTH + p - pOptions;
 }
-int DhcpHandleResponse(int* pSize, void* pPacket)
+int DhcpHandleResponse(void (*traceback)(void), int* pSize, void* pPacket)
 {
     struct header* pHeader = (header*)pPacket;
 
@@ -250,18 +251,18 @@
     {
         case DHCPOFFER:
             Ip4AddressToString(yiaddr, sizeof(text), text);
-            if (DEBUG) LogTimeF("DHCP <- offer ip %s\r\n", text);
+            if (DhcpTrace) LogTimeF("DHCP <- offer ip %s\r\n", text);
             *pSize =sendRequest(pPacket, DHCPREQUEST, siaddr, yiaddr);
             return BROADCAST;
         case DHCPACK:
             Ip4AddressToString(yiaddr, sizeof(text), text);
-            if (DEBUG) LogTimeF("DHCP <- ack ip %s\r\n", text);
+            if (DhcpTrace) LogTimeF("DHCP <- ack ip %s\r\n", text);
             DhcpLocalIp = yiaddr;
             DhcpElapsedTime = 0;
             break;
         case DHCPNAK:
             Ip4AddressToString(yiaddr, sizeof(text), text);
-            if (DEBUG) LogTimeF("DHCP <- nack ip %s\r\n", text);
+            if (DhcpTrace) LogTimeF("DHCP <- nack ip %s\r\n", text);
             break;
         default:
             LogTimeF("DHCP <- unknown message %d\r\n", dhcpMessageType);
@@ -277,19 +278,19 @@
     if (DhcpLocalIp && DhcpElapsedTime < (DhcpLeaseTime >> 1)) return 0; //Do nothing if have address and within T1
     
     *pSize = 0;
-    
+    int dest = DO_NOTHING;
     if (DhcpLocalIp && DhcpElapsedTime <  DhcpLeaseTime)
     {
         *pSize = sendRequest(pPacket, DHCPREQUEST, DhcpServer, DhcpLocalIp); //if within T2 then send request to the server - not broadcast
-        return UNICAST_DHCP;
+        dest = UNICAST_DHCP;
     }
     else
     {
-        if (DEBUG) LogTimeF("DHCP lease has expired\r\n");
+        if (DhcpTrace) LogTimeF("DHCP lease has expired\r\n");
         DhcpLocalIp = 0;
         DhcpServer = 0;
         *pSize = sendRequest(pPacket, DHCPDISCOVER, 0, 0); //If outside T2 then start from scratch to do a full DHCP
-        return BROADCAST;
+        dest = BROADCAST;
     }
-    
+    return ActionMakeFromDestAndTrace(dest, DhcpTrace);
 }