Andrew Boyson / crypto

Dependents:   oldheating gps motorhome heating

Revision:
5:ee5489ee1117
Parent:
2:82268409e83f
Child:
10:e269fd7b9500
--- a/tls/tls-session.c	Tue Aug 20 14:50:48 2019 +0000
+++ b/tls/tls-session.c	Wed Aug 28 07:10:59 2019 +0000
@@ -1,3 +1,4 @@
+#include <stdlib.h>
 #include <stdint.h>
 #include <stdbool.h>
 
@@ -8,30 +9,31 @@
 
 static struct TlsSession sessions[TLS_MAX_SESSIONS];
 
-struct TlsSession* TlsSessionGetOldest()
+struct TlsSession* TlsSessionGetOldest() //Never fails so never returns NULL
 {
     struct TlsSession* pOldest = 0;
     uint32_t ageOldest = 0;
-    for (int i = 0; i < TLS_MAX_SESSIONS; i++)
+    for (struct TlsSession* p = sessions; p < sessions + TLS_MAX_SESSIONS; p++)
     {
-        if (!sessions[i].state) return sessions + i;   //Found an empty slot so just return it
-        uint32_t age = MsTimerCount - sessions[i].lastUsed;
+        if (!p->state) return p;   //Found an empty slot so just return it
+        uint32_t age = MsTimerCount - p->lastUsed;
         if (age >= ageOldest)
         {
             ageOldest = age;
-              pOldest = sessions + i;
+              pOldest = p;
         }
     }  
     return pOldest;                            //Otherwise return the oldest
 }
 
-struct TlsSession* TlsSessionGetFromIndex(int sessionIndex)
+struct TlsSession* TlsSessionOrNull(int sessionIndex)
 {
-    if (sessionIndex <                 0) return 0;
-    if (sessionIndex >= TLS_MAX_SESSIONS) return 0;
+    if (sessionIndex <                 0) return NULL;
+    if (sessionIndex >= TLS_MAX_SESSIONS) return NULL;
     return &sessions[sessionIndex];
 }
 int TlsSessionGetIndex(struct TlsSession* pSession)
 {
+    if (!pSession) return -1;
     return pSession - sessions;
 }
\ No newline at end of file