

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

#include "iec_include.h"


static struct sFrameVFT bufferFrameVFT = {
        BufferFrame_destroy,
        BufferFrame_resetFrame,
        BufferFrame_setNextByte,
        BufferFrame_appendBytes,
        BufferFrame_getMsgSize,
        BufferFrame_getBuffer,
        BufferFrame_getSpaceLeft
};

Frame
BufferFrame_initialize(BufferFrame self, uint8_t* buffer, int startSize)
{
    self->virtualFunctionTable = &bufferFrameVFT;
    self->buffer = buffer;

    self->startSize = startSize;
    self->msgSize = startSize;
    self->isUsed = false;

    return (Frame) self;
}

void
BufferFrame_destroy(Frame super)
{
    BufferFrame self = (BufferFrame) super;

    self->isUsed = false;
}

void
BufferFrame_resetFrame(Frame super)
{
    BufferFrame self = (BufferFrame) super;

    self->msgSize = self->startSize;
}

void
BufferFrame_setNextByte(Frame super, uint8_t byte)
{
    BufferFrame self = (BufferFrame) super;

    self->buffer[self->msgSize++] = byte;
}

void
BufferFrame_appendBytes(Frame super, const uint8_t* bytes, int numberOfBytes)
{
    BufferFrame self = (BufferFrame) super;

    int i;

    uint8_t* target = self->buffer + self->msgSize;

    for (i = 0; i < numberOfBytes; i++)
        target[i] = bytes[i];

    self->msgSize += numberOfBytes;
}

int
BufferFrame_getMsgSize(Frame super)
{
    BufferFrame self = (BufferFrame) super;

    return self->msgSize;
}

uint8_t*
BufferFrame_getBuffer(Frame super)
{
    BufferFrame self = (BufferFrame) super;

    return self->buffer;
}

int
BufferFrame_getSpaceLeft(Frame super)
{
    BufferFrame self = (BufferFrame) super;

    return ((self->startSize) - self->msgSize);
}

bool
BufferFrame_isUsed(BufferFrame self)
{
    return self->isUsed;
}

void
BufferFrame_markAsUsed(BufferFrame self)
{
    self->isUsed = true;
}
