#include <stdio.h>
#include "sensor_thread.h"
#include "bmc_main.h"
#include "bmc_conf.h"
#include <stdint.h>

int sensor_init(void);
int MonitorTSensors(uint8_t sensorNum);
int MonitorNonTSensors(uint8_t sensorNum);

/*
 * Sensor monitor thread.
 * Author: Jimbo
 */
void *sensor_main(void *args)
{
	uint8_t i;
	volatile uint32_t sensorTick_old = 0;
	printf("start sensor manage thread.\n");
	sensor_init();
	

	
	while(1)
	{
//		printf("%d, %d\n",sensorTick_old, g_BMCInfo.sensorTick);
		if(sensorTick_old != g_BMCInfo.sensorTick)
		{
			sensorTick_old = g_BMCInfo.sensorTick;
			for(i=0;i<MAX_SENSOR_NUM;i++)
			{ 
			/*printf("i=%d,eventType:%d, powerOnTick:%d, powerondelay:%d, resetTick:%d, resetDelay:%d,sensorTick:%d, interval:%d\n",
				i,
				g_BMCInfo.sensorInfo[i].sensorSdr->full_sensor_rec.EventTypeCode,
				g_BMCInfo.powerOnTick, g_BMCInfo.sensorInfo[i].sensorDev->poweron_delay,
				g_BMCInfo.sysResetTick, g_BMCInfo.sensorInfo[i].sensorDev->reset_delay,
				g_BMCInfo.sensorTick, g_BMCInfo.sensorInfo[i].sensorDev->monitor_interval);*/
				//monitor threshold sensor
				if(g_BMCInfo.sensorInfo[i].sensorSdr->full_sensor_rec.EventTypeCode == THRESHOLD_SENSOR_CLASS)
				{
					//printf("g_BMCInfo.sensorInfo[i].sensorSdr->full_sensor_rec.EventTypeCode is %x\n",g_BMCInfo.sensorInfo[i].sensorSdr->full_sensor_rec.EventTypeCode);
					if ((0 == (g_BMCInfo.sensorTick % g_BMCInfo.sensorInfo[i].sensorDev->monitor_interval)) &&
					(g_BMCInfo.powerOnTick >= g_BMCInfo.sensorInfo[i].sensorDev->poweron_delay) &&
					(g_BMCInfo.sysResetTick >= g_BMCInfo.sensorInfo[i].sensorDev->reset_delay))
					{ 
						/* Monitor threshold sensor */
						MonitorTSensors (i); 	
					}
				}
				//monitor none threshold sensor
				else
				{
					//printf("g_BMCInfo.sensorInfo[i].sensorSdr->full_sensor_rec.EventTypeCode is %x\n",g_BMCInfo.sensorInfo[i].sensorSdr->full_sensor_rec.EventTypeCode);
					if ((0 == (g_BMCInfo.sensorTick%g_BMCInfo.sensorInfo[i].sensorDev->monitor_interval)) &&
					(g_BMCInfo.powerOnTick >= g_BMCInfo.sensorInfo[i].sensorDev->poweron_delay) &&
					(g_BMCInfo.sysResetTick >= g_BMCInfo.sensorInfo[i].sensorDev->reset_delay))
					{ 
						/* Monitor not threshold sensor */
						MonitorNonTSensors (i); 	
					}
				}
			}
		}
		else
		{
			usleep(100000);
		}
	}
}

int sensor_init(void)
{
	uint8_t i;
	for(i=0;i<MAX_SENSOR_NUM;i++)
	{
		g_BMCInfo.sensorInfo[i].sensorReading = 0;
		g_BMCInfo.sensorInfo[i].sensorMonitBusy = 0;
		g_BMCInfo.sensorInfo[i].sensorDev = getSensorDev(i);
		g_BMCInfo.sensorInfo[i].sensorSdr = getSensorSdr(i);
	}
	return 0;
}

int MonitorTSensors(uint8_t sensorNum)
{
	hal_t	hal_info = {0};
	g_BMCInfo.sensorInfo[sensorNum].sensorMonitBusy = 1;

	hal_info.pbyte = &g_BMCInfo.sensorInfo[sensorNum].sensorReading;
	g_BMCInfo.sensorInfo[sensorNum].sensorDev->device_read(&hal_info);

//	printf("sensor:%d %s, value:%d\n", sensorNum, g_BMCInfo.sensorInfo[sensorNum].sensorSdr->full_sensor_rec.IDStr, g_BMCInfo.sensorInfo[sensorNum].sensorReading);

	g_BMCInfo.sensorInfo[sensorNum].sensorMonitBusy = 0;
	return 0;
}



int MonitorNonTSensors(uint8_t sensorNum)
{
	hal_t	hal_info = {0};
	g_BMCInfo.sensorInfo[sensorNum].sensorMonitBusy = 1;

	hal_info.pbyte = &g_BMCInfo.sensorInfo[sensorNum].sensorReading;
	g_BMCInfo.sensorInfo[sensorNum].sensorDev->device_read(&hal_info);

//	printf("sensor:%d %s, value:%d\n", sensorNum, g_BMCInfo.sensorInfo[sensorNum].sensorSdr->full_sensor_rec.IDStr, g_BMCInfo.sensorInfo[sensorNum].sensorReading);

	g_BMCInfo.sensorInfo[sensorNum].sensorMonitBusy = 0;
	return 0;
}


