#include "cbuffer.h"
#include "extra.h"
#include "PhysicalLayer.h"

/****************************************************************************
	Inicializuje buffer (nahrazka vychoziho konstruktoru)

	Vstup: 
		neni

	Výstup:
		neni

*****************************************************************************/
void CBuffer::Init(void)
{
	//Vymazani vsech pozic v bufferu
	for (int i = 0; i < CBUFFER_SLOTS_COUNT; i++)
	{
		this->dataBuff[i].completed = YES;
		clearBuffer(this->dataBuff[i].data,CBUFFER_SLOT_LENGTH);
	}
	this->dataTop = 0;
	this->dataCurrent = 0;

}

/****************************************************************************
	Vlozeni dat do bufferu (vytvoreni paru Prijimac-data). Je provedena kontrola,
	data v aktualnim slotu odeslana. Pokud ne, je vraceno NO

	Vstup: 
		target:		Cil, kam se maji data poslat (wifi/COM)
		input:		vlastni data

	Výstup:
		YES/NO		Bylo v zasobniku misto?

*****************************************************************************/
ANSWER CBuffer::InsertData(OS_CONNECT target, unsigned char * input)
{
	if (this->dataBuff[dataTop].completed == NO)
		return NO;
	this->dataBuff[dataTop].target = target;
	this->dataBuff[dataTop].completed = NO;
	strcopy(input,this->dataBuff[dataTop].data);
	dataTop = (dataTop+1)%CBUFFER_SLOTS_COUNT;
	return YES;
}

/****************************************************************************
	Pokud je odesilaci fronta volna, ulozi data ke zpracovani ovladaci 
	seriovych portu

	Vstup: 
		neni

	Výstup:
		YES/NO		Ovladac aktualne neodesilal data => mohl dostat data k odeslani

*****************************************************************************/
ANSWER CBuffer::SendNextData(void)
{
	if (CheckSerialSendComplete() == NO || this->dataBuff[dataCurrent].completed == YES)
		return NO;
	SendSerialData(this->dataBuff[dataCurrent].target,this->dataBuff[dataCurrent].data);
	this->dataBuff[dataCurrent].completed = YES;
	dataCurrent = (dataCurrent+1)%CBUFFER_SLOTS_COUNT;
	return YES;
}

/****************************************************************************
	Vraci info, jestli existuji neodeslana data

	Vstup: 
		neni

	Výstup:
		YES/NO

*****************************************************************************/

ANSWER CBuffer::DataPending(void)
{
	return this->dataBuff[dataCurrent].completed == NO? YES:NO;
}

/****************************************************************************
	Vraci info, jestli je mozne zaradit data do fronty

	Vstup: 
		neni

	Výstup:
		YES/NO

*****************************************************************************/
ANSWER CBuffer::DataFull(void)
{
	return this->dataBuff[dataTop].completed == NO? YES:NO;	
}

/****************************************************************************
	Konstruktor, ktery se ovsem v CrossWorks nepouziva

	Vstup: 
		neni

	Výstup:
		neni

*****************************************************************************/

CBuffer::CBuffer(void)
{
	Init();
}

/****************************************************************************
	Destruktor.

	Vstup: 
		neni

	Výstup:
		neni

*****************************************************************************/

CBuffer::~CBuffer(void)
{
	return;
}


