/*****************************************************************************************************************
*	S e r v i c e s . c
*
*
*
*
*****************************************************************************************************************/

#include "typedefine.h"
#include "main.h"
#include "services.h"
#include "hw_functions.h"


/*****************************************************************************************************************
*	F i n d H i g h P r i R e g
*
*
* Finding the highest priority requiremen in requirement array
* Return the value of task without priority
*****************************************************************************************************************/
void FindHighPriReq(void)
{
	tUInt maxPrio = 0;
	tUChar item;
	tUChar highPrioItem = 0;
	
	for (item=0; item<MAX_REQ; item++)	// checking list of requirement
	{
  		if ((reqBuffer[item].requirement) > maxPrio)	// when found the higher priority task
		{
			highPrioItem = item;
			maxPrio = (reqBuffer[item].requirement);
		}
	}
	
	if (maxPrio	== 0)
		appCB.task = REQUIREMENT_NO;	// no requirement is in the list
	else
		appCB.task = highPrioItem; // return the highest priority and first in front requirement
}


/*****************************************************************************************************************
*	E r r o r S e r v i c e
*
*
* The function serviced the error messages
*
*****************************************************************************************************************/
void ErrorService(void)
{
	if( (appCB.error&ERR_FATAL) )				// chyba je kriticka - restart
     {
		ErrorLed();
	 	while(1);
	 }

//	if( (OPER.appcfg & GLOBAL_CFG_IERROR) )
     {
	    AddReq(PRIORITY_SYS_MESSAGE | ADD_MULTIPLE | PROCESS_SCI_STATE_ERROR, (ERROR_CODE_UNITERR|appCB.error), (tUInt *)&(OPER.idAdr));		  
	 }

 	appCB.last_error = appCB.error;
	appCB.error = 0;
	
}


/*****************************************************************************************************************
*	A d d R e q
*
*
* the function added requirement to the list of waiting requirement
*
*****************************************************************************************************************/
void AddReq(tUInt require, tUInt param, tUInt *addr)
{
	tUInt item;
	
	if( (appCB.mode&SYSBUSY_MASK) ) return;		// system busy - discard event
	
/*	if(!(require&ADD_MULTIPLE))	
	 {
		for (item=0; item < MAX_REQ; item++)	// checking list of requirement
		{
			if ((reqBuffer[item].requirement) == (require & REQUIRE_MASK) )
			{
				//appCB.error = ERR_REQ_REPEAT;		// pozadavek uz je ve fronte zadan, ale jeste se nezpracoval
													// je proto lepsi novy pozadavek neprovest
													// a pockat na vykonani pozadavku jiz zadaneho
													// konkretni priklad je rychle krokovani kanalu modulu v menu napr.A													
				return;	// return because one the same requirement was created before
			}
		}
	 }
*/
	for (item=0; item <= MAX_REQ; item++)	// checking list of requirement
	{
		if (!(reqBuffer[item].requirement))	// when found the empty place
			break;			// break the cycle
	}
	
	if (item < MAX_REQ)		// when found some empty place before end of list
	{
		reqBuffer[item].requirement = (require & REQUIRE_MASK);	// fill the requirement to the empty place
		reqBuffer[item].parameter = param;	
		reqBuffer[item].addr[0] = *addr;	
		reqBuffer[item].addr[1] = *(addr+1);	
	}
	else
		appCB.error = ERR_REQ_LIST_FULL | ERR_FATAL;	// list of requirement is full
}


/*****************************************************************************************************************
*	R e m T a s k
*
*
* the function removed the task from the list of tasks
*
*****************************************************************************************************************/
void RemReq(tUInt require)
{
	tUInt item = 0;
	
	while ((reqBuffer[item].requirement) != require)
	{
		item++;
		if (item > MAX_REQ)
		{
			appCB.error = ERR_TASK_NOT_FOUND;
			return;	// return, the task was not found in list
		}
	}
	
	reqBuffer[item].requirement = 0;
	reqBuffer[item].parameter = 0;
}


/*****************************************************************************************************************
*	R e m T a s k
*
*
* the function removed the task from the list of tasks
*
*****************************************************************************************************************/
//void RemReq(tUChar require)
//{
//	reqBuffer[require].requirement = 0;
//	reqBuffer[require].parameter = 0;
//	reqBuffer[require].addr[0] = 0;
//	reqBuffer[require].addr[1] = 0;
//}
