/*
* Brief:	Micellaneous Ticks increment every 1 second.
			Record system power on times.
* Author:	Jimbo_Zhang@outlook.com
* Date:		2019-9-16
*/

#include "TimerTask.h"
#include "time.h"
#include <stdint.h>
#include "main.h"
#include <signal.h>

static void ProcessTimerReq (void);

//g_BMCInfo.CurTimerTick increment in port.c/xPortSysTickHandler().
// void *TimerTask( void *pvParameters )
// {	
// 	uint32_t preTimerTick = g_BMCInfo.CurTimerTick;
// 	printf("TimerTask start...\n");
// 	while(1)
// 	{
// 		preTimerTick = g_BMCInfo.CurTimerTick;
// 		g_BMCInfo.SELTimeSecond++;
	
// 		if(g_BMCInfo.SensorSharedMem.SensorTick < 0xffffffff)	
// 			g_BMCInfo.SensorSharedMem.SensorTick++;
		
// 		if(g_BMCInfo.SenConfig.PowerOnTick < 0xffffffff)
// 			g_BMCInfo.SenConfig.PowerOnTick++;
		
// 		if(g_BMCInfo.SenConfig.SysResetTick < 0xffffffff)
// 			g_BMCInfo.SenConfig.SysResetTick++;
		
// 		g_BMCInfo.CurTimerSecond++;
// 		if(g_BMCInfo.PowerGoodFlag && (g_BMCInfo.CurTimerSecond%60 == 0))	// 1minute
// 		{
// 			g_BMCInfo.BootValidMinutes++;
// 			g_BMCInfo.TotalBootValidMinutes++;
// 		}

// 		ProcessTimerReq();

// 		sleep(1);
// 	}
// }

void TimerTask( int signo )	//signal handler
{	
	//printf("TimerTask start...\n");
	switch(signo)
	{
	case SIGALRM:
		//preTimerTick = g_BMCInfo.CurTimerTick;
		//g_BMCInfo.SELTimeSecond++;
	
		if(g_BMCInfo.SensorSharedMem.SensorTick < 0xffffffff)	
			g_BMCInfo.SensorSharedMem.SensorTick++;
		
		if(g_BMCInfo.SenConfig.PowerOnTick < 0xffffffff)
			g_BMCInfo.SenConfig.PowerOnTick++;
		
		if(g_BMCInfo.SenConfig.SysResetTick < 0xffffffff)
			g_BMCInfo.SenConfig.SysResetTick++;
		
		g_BMCInfo.CurTimerSecond++;
		if(g_BMCInfo.PowerGoodFlag && (g_BMCInfo.CurTimerSecond%60 == 0))	// 1minute
		{
			g_BMCInfo.BootValidMinutes++;
			g_BMCInfo.TotalBootValidMinutes++;
		}

		ProcessTimerReq();
		break;
	}
}



/**
*@fn ProcessTimerReq
*@brief Executes Timer task function for every one second
*@return none
*/
static void ProcessTimerReq (void)
{
	int i;
	for (i = 0; i < g_BMCInfo.TimerTaskTblSize; i++)
	{
		if (0 == (g_BMCInfo.CurTimerTick % g_BMCInfo.TimerTaskTbl [i].NumSecs))
		{
			g_BMCInfo.TimerTaskTbl [i].TimerFn ();
		}
	}
	return;
}





