/*
* Brief:	Handle all chassis function in this file. 
			Such as: Fan control, update power_good status, update chassis health status.
* Author:	Jimbo_Zhang@outlook.com
* Date:		2019-9-18
*/
#include <stdio.h>
#include <stdint.h>
#include <sys/prctl.h>
#include <semaphore.h>
#include <pthread.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>

#include "main.h"
#include "ChassisTimerTask.h"
#include "ChassisDevice.h"
#include "com_IPMI_Chassis.h"
#include "com_IPMI_LANConfig.h"
#include "com_IPMI_DeviceConfig.h"
#include "SensorMonitor.h"
#include "time.h"

#include "com_IPMI_App.h"
#include "hal_interface_api.h"

void *IdentifyTask(void *param);
//int gFd_ChassisPwrHndlrQue;

void *ChassisTimerTask(void* pArg)
{
	MsgPkt_T	ReqPkt;
	MsgPkt_T	ResPkt;
	uint32_t	preChassisSeconds;
	uint32_t	preFanSeconds;
	uint32_t	preIdentifySeconds = 0;
	
	char  		cmdStr[100] = {0};
	SetLanConfigReq_T  *pSetLanCfg;
	int i;
	prctl(PR_SET_NAME,__FUNCTION__,0,0,0);

	printf("ChassisTimerTask Started... \n");

	preChassisSeconds	=	g_BMCInfo.CurTimerSecond;
	preFanSeconds	=	g_BMCInfo.CurTimerSecond;

	//sleep(5);
	while(1)
	{
		//Chassis timer interval
		if((g_BMCInfo.CurTimerSecond != preChassisSeconds) 
			&&((g_BMCInfo.CurTimerSecond - preChassisSeconds) >= g_BMCInfo.IpmiConfig.ChassisTimerInterval))
		{
			//log("Chassis interval\r\n");
			preChassisSeconds	=	g_BMCInfo.CurTimerSecond;
			//TODO:
			//g_BMCInfo.PowerGoodFlag = 1;
			g_BMCInfo.IpmiConfig.ChassisPowerState.PowerState	=	g_BMCInfo.PowerGoodFlag;

			// ReqPkt.NetFnLUN 	= 0x2e<<2;
			// ReqPkt.Cmd 			= 0x01;
			// ReqPkt.Size 		= 0;
			// if(0 == API_BridgeInternal(&ReqPkt, &ResPkt, 0x40, 6))
			// {
			// 	printf("---> Access 0x40 successful: ");
			// }
			// else
			// {
			// 	printf("---> Access 0x40 failed: ");
			// }
			// for(i=0;i<ResPkt.Size;i++)
			// 	printf("%x ", ResPkt.Data[i]);
			// printf("\n");
		}
		
		// //Fan control
		// if((g_BMCInfo.CurTimerSecond != preFanSeconds)
		// 	&& (g_BMCInfo.CurTimerSecond % g_BMCInfo.IpmiConfig.FanControlInterval) == 0)
		// {
		// 	preFanSeconds	=	g_BMCInfo.CurTimerSecond;
		// 	PDK_FanControl();
		// }
		
		//Chassis Identify
		if(g_BMCInfo.ChassisIdentify == TRUE)		//新开个线程实现，默认灯闪烁10s，参考IPMI协议
		{
			if((g_BMCInfo.ChassisIdentifyTimeout != 0) || (g_BMCInfo.ChassisIdentifyForce != 0))
			{
				if(0 != pthread_create(NULL,NULL,IdentifyTask,NULL))
			    {
			        printf("%s: Create MsgHndlrTask thread failed!\n", __FUNCTION__);
			    }
			}
			g_BMCInfo.ChassisIdentify = FALSE;
		}
		
		usleep(100);
	}
}



void *IdentifyTask(void *param)
{
	uint8_t		IdentifyTickCnt = 0;
	GPIO_PinState OldPinState;
	printf("Start Chassis Identify, Time %d, force %d\n", g_BMCInfo.ChassisIdentifyTimeout, g_BMCInfo.ChassisIdentifyForce);
			
	OldPinState = stm32_gpio_read(IDENTIFY_PORT, IDENTIFY_PIN);

	while(g_BMCInfo.ChassisIdentifyForce || g_BMCInfo.ChassisIdentifyTimeout)
	{
		stm32_gpio_write(IDENTIFY_PORT, IDENTIFY_PIN, GPIO_PIN_SET);
		usleep(250000);
		stm32_gpio_write(IDENTIFY_PORT, IDENTIFY_PIN, GPIO_PIN_RESET);
		usleep(250000);
		stm32_gpio_write(IDENTIFY_PORT, IDENTIFY_PIN, GPIO_PIN_SET);
		usleep(250000);
		stm32_gpio_write(IDENTIFY_PORT, IDENTIFY_PIN, GPIO_PIN_RESET);
		usleep(250000);
		if(g_BMCInfo.ChassisIdentifyTimeout>0)
		{
			g_BMCInfo.ChassisIdentifyTimeout--;
		}
	}

	//恢复LED灯的状态
	stm32_gpio_write(IDENTIFY_PORT, IDENTIFY_PIN, OldPinState);

	printf("Chassis Identify finish.\n");
			
	pthread_exit(0);
}