Andrew Boyson / crypto

Dependents:   oldheating gps motorhome heating

tls/tls-session.c

Committer:
andrewboyson
Date:
2019-08-28
Revision:
5:ee5489ee1117
Parent:
2:82268409e83f
Child:
10:e269fd7b9500

File content as of revision 5:ee5489ee1117:

#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>

#include "tls-session.h"
#include "mstimer.h"

#define TLS_MAX_SESSIONS 4

static struct TlsSession sessions[TLS_MAX_SESSIONS];

struct TlsSession* TlsSessionGetOldest() //Never fails so never returns NULL
{
    struct TlsSession* pOldest = 0;
    uint32_t ageOldest = 0;
    for (struct TlsSession* p = sessions; p < sessions + TLS_MAX_SESSIONS; p++)
    {
        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 = p;
        }
    }  
    return pOldest;                            //Otherwise return the oldest
}

struct TlsSession* TlsSessionOrNull(int sessionIndex)
{
    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;
}