/****************************************************************************
	Ridici vrstva odesilani pres seriovy port. Jedna se o spravce fronty
	dat k odeslani. 

*****************************************************************************/
#include "Serial.h"
#include "cbuffer.h"
#include "PhysicalLayer.h"
#include "stdafx.h"
#include "OS.h"



CBuffer serialSendBuffer;	//Kruhovy odesilaci buffer

/****************************************************************************
	Funkce vlozi data do kruhoveho bufferu a pripravi je k odeslani. Zaroven 
	je zvysena priorita ridici smycky teto aplikace aby byla data odeslana co 
	nejrychleji.

	Vstup: 
		target:		cilovy port
		message:	data k odeslani

	Vystup
		ANSWER:		YES/NO

*****************************************************************************/
ANSWER SendSerialMessage(OS_CONNECT target, unsigned char * message)
{
	SetTaskPrioriry(DRV_SERIAL,MOST);						//Zvyseni priority
	return serialSendBuffer.InsertData(target, message);	//Vlozeni dat do bufferu
}

/****************************************************************************
	Ridici smycka aplikace

	Vstup: 
		neni

	Vystup
		neni

*****************************************************************************/
void DrvSerial(void)
{
	if (CheckSerialSendComplete() == NO)					//Pokud nejsou posledni data odeslana
	{
		return;												//Ukonci se
	}
	if (serialSendBuffer.DataPending() == NO)				//pokud necekaji data na odeslani
	{
		SetTaskPrioriry(DRV_SERIAL,NORMAL);					//prepni prioritu na NORMAL
		return;
	}
	SetTaskPrioriry(DRV_SERIAL,MOST);						//prepni prioritu na maximum
	if (serialSendBuffer.SendNextData() == NO)				//predej data ovladaci serioveho portu
	{
		DebugOut("Could not send serial data");
	}
	
}