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:
93:580fc113d9e9
Parent:
74:c3756bfa960e
--- a/eth/ar4.c	Thu Nov 29 16:52:10 2018 +0000
+++ b/eth/ar4.c	Sun Dec 02 18:40:35 2018 +0000
@@ -1,18 +1,18 @@
 #include <stdint.h>
 #include <stdbool.h>
 #include     "log.h"
-#include   "clock.h"
 #include     "net.h"
 #include     "mac.h"
 #include "ip4addr.h"
 #include     "arp.h"
 #include    "http.h"
+#include "mstimer.h"
 
 bool Ar4Trace = false;
 
-#define CACHE_TIMEOUT   3600
-#define FREEZE_TIMEOUT  1800
-#define REPLY_TIMEOUT      2
+#define  CACHE_TIMEOUT_MS  3600 * 1000
+#define FREEZE_TIMEOUT_MS  1800 * 1000
+#define  REPLY_TIMEOUT_MS     2 * 1000
 #define SEND_ATTEMPTS      3
 #define RECORDS_COUNT     20
 
@@ -21,7 +21,6 @@
 #define STATE_SENT  2
 #define STATE_VALID 3
 
-static uint32_t elapsed = 0;
 struct record
 {
     uint32_t elapsed;
@@ -41,18 +40,19 @@
 }
 static int getOldest()
 {
-    int iN = 0;
-    uint32_t tN = 0xFFFFFFFF;
+    int        iOldest = 0;
+    uint32_t ageOldest = 0;
     for (int i = 0; i < RECORDS_COUNT; i++)
     {
         if (!records[i].state) return i; //Found an empty slot so just return it
-        if (records[i].elapsed < tN)
+        uint32_t age = MsTimerCount - records[i].elapsed;
+        if (age >= ageOldest)
         {
-            tN = records[i].elapsed;
-            iN = i;
+            ageOldest = age;
+              iOldest = i;
         }
     }  
-    return iN;                         //Otherwise return the oldest
+    return iOldest;                         //Otherwise return the oldest
 }
 void Ar4MakeRequestForMacFromIp(uint32_t ip)
 {
@@ -64,7 +64,7 @@
     i = getExistingIp(ip);
     if (i > -1)
     {
-        if (elapsed < records[i].elapsed + FREEZE_TIMEOUT) return;
+        if (!MsTimerHasElapsed(records[i].elapsed, FREEZE_TIMEOUT_MS)) return;
         if (Ar4Trace)
         {
             LogTimeF("AR4 Updated request for MAC of ");
@@ -73,7 +73,7 @@
         }
         records[i].state    = STATE_WANT;
         records[i].tries    = 0;
-        records[i].elapsed  = elapsed;
+        records[i].elapsed  = MsTimerCount;
         return;
     }
     
@@ -88,7 +88,7 @@
     records[i].ip       = ip;
     records[i].state    = STATE_WANT;
     records[i].tries    = 0;
-    records[i].elapsed  = elapsed;
+    records[i].elapsed  = MsTimerCount;
     MacClear(records[i].mac);
 }
 int Ar4AddIpRecord(void (*traceback)(void), char* mac, uint32_t ip)
@@ -118,8 +118,8 @@
     i = getExistingIp(ip);
     if (i > -1)
     {
-        records[i].elapsed   = elapsed;
-        records[i].state = STATE_VALID;
+        records[i].elapsed = MsTimerCount;
+        records[i].state   = STATE_VALID;
         MacCopy(records[i].mac, mac);
         return i;
     }
@@ -127,7 +127,7 @@
     //Otherwise find the first empty slot and add the IP, MAC, and date
     i = getOldest();
     records[i].ip       = ip;
-    records[i].elapsed  = elapsed;
+    records[i].elapsed  = MsTimerCount;
     records[i].state = STATE_VALID;
     MacCopy(records[i].mac, mac);
     return i;
@@ -154,7 +154,7 @@
     {
         if (records[i].state)
         {
-            HttpAddF("%4u ", (elapsed - records[i].elapsed) / 60);
+            HttpAddF("%4u ", (MsTimerCount - records[i].elapsed) / 1000 / 60);
             
             int ipLen = Ip4AddressHttp(records[i].ip);
             HttpFillChar(' ', 40 - ipLen);
@@ -172,16 +172,16 @@
 }
 static void clearCache(struct record* pr)
 {
-    if (elapsed > pr->elapsed + CACHE_TIMEOUT) clear(pr);
+    if (MsTimerHasElapsed(pr->elapsed, CACHE_TIMEOUT_MS)) clear(pr);
 }
 static void retry(struct record* pr)
 {
-    if (pr->state == STATE_SENT && elapsed > pr->elapsed + REPLY_TIMEOUT)
+    if (pr->state == STATE_SENT && MsTimerHasElapsed(pr->elapsed, REPLY_TIMEOUT_MS))
     {
         if (pr->tries < SEND_ATTEMPTS)
         {
             pr->state   = STATE_WANT;
-            pr->elapsed = elapsed;
+            pr->elapsed = MsTimerCount;
             pr->tries++;
         }
         else
@@ -205,7 +205,7 @@
             ArpAddressToResolve = pr->ip;
             ArpResolveRequestFlag = true;
             pr->state   = STATE_SENT;
-            pr->elapsed = elapsed;
+            pr->elapsed = MsTimerCount;
             return;
         }
     }
@@ -221,8 +221,6 @@
     clearCache (pr);
     retry      (pr);
     sendRequest(pr);
-    
-    if (ClockTicked) elapsed++;
 }
 void Ar4Init()
 {