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

tcp/tcb.c

Committer:
andrewboyson
Date:
2018-11-11
Revision:
79:f50e02fb5c94
Parent:
78:9d8fc88df405
Child:
80:4ef1500fca1d

File content as of revision 79:f50e02fb5c94:

#include <stdint.h>

#include   "log.h"
#include   "tcp.h"
#include   "tcb.h"
#include "clock.h"

#define TCB_COUNT 10

struct tcb tcbs[TCB_COUNT];

uint32_t TcbGetIsn()
{
    static uint32_t isn = 0;
    isn += 100000; //Gives each tcb 100,000 packets and won't repeat before 42,940 tcbs.
    return isn;
}
struct tcb* TcbGetExisting(int ipType, int remArIndex, uint16_t port)
{
    for (int i = 0; i < TCB_COUNT; i++)
    {
        struct tcb* pTcb = tcbs + i;
        if (pTcb->state                    && 
            pTcb->ipType     == ipType     &&
            pTcb->remArIndex == remArIndex &&
            pTcb->remPort    == port) return pTcb;
    }
    return NULL;
}
struct tcb* TcbGetEmpty()
{
    for (int i = 0; i < TCB_COUNT; i++)
    {
        struct tcb* pTcb = tcbs + i;
        if (pTcb->state == TCB_EMPTY) return pTcb;
    }
    return NULL;
}
void TcbGetNext(struct tcb** ppTcb)
{
    if (!*ppTcb) //Initialise if passed NULL
    {
        *ppTcb = tcbs;
        return;
    }
    ++*ppTcb; //Increment
    if (*ppTcb >= tcbs + TCB_COUNT) *ppTcb = tcbs;
}

uint32_t TcbElapsed = 0;

void TcbMain()
{
    if (ClockTicked) TcbElapsed++;
}
void TcbInit()
{
    for (int i = 0; i < TCB_COUNT; i++) tcbs[i].state = TCB_EMPTY;
}