/****************************************************************
 ****************************************************************
 **                                                            **
 **    (C)Copyright 2005-2006, American Megatrends Inc.        **
 **                                                            **
 **            All Rights Reserved.                            **
 **                                                            **
 **        6145-F, Northbelt Parkway, Norcross,                **
 **                                                            **
 **        Georgia - 30071, USA. Phone-(770)-246-8600.         **
 **                                                            **
 ****************************************************************
 ****************************************************************/
/*****************************************************************
 *
 * SensorMonitor.h
 * Sensor Monitor 
 *
 * Author: Rama Rao Bisa <ramab@ami.com> 
 * 
 *****************************************************************/
#ifndef SENSOR_MONITOR_H
#define SENSOR_MONITOR_H

#include "com_IPMI_SDRRecord.h"


/*** Soft Processor Control functions   ***/    
#define READ_CTRL_FN                                0x01
#define LWR_NON_CRITICAL_GOING_LOW_CTRL_FN          0x02
#define LWR_CRITICAL_GOING_LOW_CTRL_FN              0x03
#define LWR_NON_RECOVERABLE_GOING_LOW_CTRL_FN       0x04
#define UPPER_NON_CRITICAL_GOING_HIGH_CTRL_FN       0x05
#define UPPER_CRITICAL_GOING_HIGH_CTRL_FN           0x06
#define UPPER_NON_RECOVERABLE_GOING_HIGH_CTRL_FN    0x07

#define LWR_NON_CRITICAL_GOING_HIGH_CTRL_FN         0x82
#define LWR_CRITICAL_GOING_HIGH_CTRL_FN             0x83
#define LWR_NON_RECOVERABLE_GOING_HIGH_CTRL_FN      0x84
#define UPPER_NON_CRITICAL_GOING_LOW_CTRL_FN        0x85
#define UPPER_CRITICAL_GOING_LOW_CTRL_FN            0x86
#define UPPER_NON_RECOVERABLE_GOING_LOW_CTRL_FN     0x87

#define GET_NORMAL_VALUE                            0xFE
#define INIT_CTRL_FN                                0xFF

/*** Sensor Event   definitions ***/
#define SENSOR_STATUS_NORMAL                    0x00
#define SENSOR_STATUS_WARNING_HIGH              0x01
#define SENSOR_STATUS_WARNING_LOW               0x02
#define SENSOR_STATUS_CRITICAL_HIGH             0x03
#define SENSOR_STATUS_CRITICAL_LOW              0x04
#define SENSOR_STATUS_NONRECOVERABLE_HIGH       0x05
#define SENSOR_STATUS_NONRECOVERABLE_LOW        0x06
#define SENSOR_STATUS_FATAL                     0x08

#define MAX_SENSORS_ALLOWED                     20
#ifdef SKIPPER
#define MAX_SENSORS                             0x13
#define MAX_FANS                                0x08
#define MAX_VOLTS                               0x08
#define MAX_TEMPS                               0x01
#else
#define MAX_SENSORS                             0x11
#define MAX_FANS                                0x07
#define MAX_VOLTS                               0x08
#define MAX_TEMPS                               0x00
#endif

//#define MAX_SENSOR_NUMBERS	    		(64)	//RAM limit

/*---------------------------------------------------
 * Parameters passed to the sensor monitor handler Q.
 *---------------------------------------------------*/
#define PARAM_REARM_ALL_SENSORS     0x01
#define PARAM_REARM_SENSOR          0x02
#define PARAM_HANDLE_IRQ            0x03
#define PARAM_SENSOR_SCAN           0x04


//typedef int (*pPDK_MonitorHook_T) (void* pSensorInfo,uint8_t *pReadFlags );
//typedef int (*pPDK_SensorInitHook_T) (void *pSensorInfo );
//typedef int (*pPDK_MonitorExtHook_T) (void *pSensorInfo, uint8_t* pReadFlags );
//typedef int (*pPDK_PreEventLogHook_T) (void *pSensorInfo, uint8_t* pEventData, uint8_t* pReadFlags );


///* Hook Type supported */
//typedef enum
//{
//    HT_INIT_SENSOR,
//    HT_PRE_MONITOR,
//    HT_POST_MONITOR

//} HookType_e;

//	
//#define MAX_PRE_MONITOR_HOOKS        3
//#define MAX_POST_MONITOR_HOOKS       3
//#define MAX_POST_MONITOR_EXT_HOOKS   3
//#define MAX_INIT_SENSOR_HOOKS		 3
//#define MAX_PRE_EVENT_LOG_HOOKS      3


/* Sensor Monitor Event Flags */

#define SET_SM_INIT_DONE(FLAGS)			(FLAGS = FLAGS & 0xFE)
#define SET_SM_REINIT(FLAGS)			(FLAGS = FLAGS | 0x01)
#define SET_SM_UNABLE_TO_READ(FLAGS)		(FLAGS = FLAGS | 0x20)
#define SET_SM_CLEAR_UNABLE_TO_READ(FLAGS)	(FLAGS = FLAGS & 0xDF)
#define SET_SM_ENABLE_SCAN(FLAGS)		(FLAGS = FLAGS | 0x40)
#define SET_SM_DISABLE_SCAN(FLAGS)		(FLAGS = FLAGS & 0xBF)
#define SET_SM_ENABLE_EVT(FLAGS)		(FLAGS = FLAGS | 0x80)
#define SET_SM_DISABLE_EVT(FLAGS)		(FLAGS = FLAGS & 0x7F)


#pragma pack (1)

//typedef struct 
//{ 
//    uint8_t  MonitorInterval;
////    uint8_t  MonitorState ;
////    uint16_t NormalValue;     		
//    uint8_t  PowerOnDelay; //Delay after chassis power on in seconds. 0 - don't delay
//    uint8_t  SysResetDelay ; // Delay after chassis reset    in seconds. 0 - don't delay
//} SensorProperties_T; 


//typedef struct
//{
//    uint8_t		SensorNum;   	/* Sensor Numbner */	
//    uint8_t       	HookType;       /* Type of the hook */ 
//    pPDK_MonitorHook_T	pHook;          /* Hook */ 

//} PDKSensorHook_T; 
/**
 * @struct SensorInfo_T
 * @brief Sensor Information.
**/
typedef struct
{

//    uint8_t               IsSensorPresent;		//nessary  
    uint8_t               SensorNumber;
    uint8_t               SensorReading;
    uint16_t              SettableThreshMask;
    uint16_t              Err;
    uint16_t              PreviousState;
    uint16_t              RecordID;
    uint8_t               SensorTypeCode;
    uint8_t               EventTypeCode;
    uint8_t               SensorInit;			//SDR sensor initialization

    // These values are copied from the SDR.
    uint8_t           Units1;
    uint8_t           Units2;
    uint8_t           Units3;

    // Caution !!! Order of these members has to maintained
    // Some of the command implementation needs the order to be maintained 

    // the following set are used in Get Sensor Reading Factors command
    uint8_t   		M_LSB;
    uint8_t   		M_MSB_Tolerance;
    uint8_t   		B_LSB;
    uint8_t   		B_MSB_Accuracy;
    uint8_t   		Accuracy_MSB_Exp;
    uint8_t   		RExp_BExp;
    uint16_t        sensor_send_packed[12];		//nessary??

    // the following set are used in Get/Set Sensor Threshold commands
    uint8_t               LowerNonCritical;
    uint8_t               LowerCritical;
    uint8_t               LowerNonRecoverable;
    uint8_t               UpperNonCritical;
    uint8_t               UpperCritical;
    uint8_t               UpperNonRecoverable;
    
    // the following set are used in Get/Set Sensor Hysteresis commands
    uint8_t               PosHysteresis;
    uint8_t               NegHysteresis;


    // the following set are used in Get/Set Event Enables commands
    uint8_t   		EventFlags;
                            /* Event Flags description           */
                            /* Bit 0 -  Initialization Done      */
                            /* Bit 1 -  Update in Progress       */
                            /* Bit 2 -  reserved                 */
                            /* Bit 3 -  reserved                 */
                            /* Bit 4 -  reserved                 */
                            /* Bit 5 -  Unable to read           */
                            /* Bit 6 -  Sensor Scanning disabled */
                            /* Bit 7 -  Event Message Disabled   */
    uint8_t               AssertionEventEnablesByte1;
    uint8_t               AssertionEventEnablesByte2;
    uint8_t               DeassertionEventEnablesByte1;
    uint8_t               DeassertionEventEnablesByte2;


    uint8_t               EventLevel;
    uint8_t               HealthLevel;    // Added to provide correct Health state
    uint8_t               AssertionEventOccuredByte1;
    uint8_t               AssertionEventOccuredByte2;
    uint8_t               DeassertionEventOccuredByte1;
    uint8_t               DeassertionEventOccuredByte2;

    uint8_t               SensorMonitorInterval;  // added to effect different sensor monitor intervals.
    uint8_t               SensorState;				//wether monitor on standby
    uint8_t               SensorReadType;
    uint8_t               PowerOnDelay; //Delay after chassis power on in seconds. 0 - don't delay
    uint8_t               SysResetDelay ; // Delay after chassis reset    in seconds. 0 - don't delay

    /* Settable Sensor Fields */
    uint8_t   Operation;		
    uint8_t   EvtData1;
    uint8_t   EvtData2;
    uint8_t   EvtData3;


    uint8_t		  ComparisonStatus;
    uint8_t       SensorCaps;              // JM 062306 - Added to support manual re-arm sensors
    uint8_t       AssertionHistoryByte1;   // JM 062306 - Added to support manual re-arm sensors
    uint8_t       AssertionHistoryByte2;   // JM 062306 - Added to support manual re-arm sensors
    uint8_t       DeassertionHistoryByte1; // JM 062306 - Added to support manual re-arm sensors
    uint8_t       DeassertionHistoryByte2; // JM 062306 - Added to support manual re-arm sensors

    /* Sensor Monitor Internal Flags 
     * This flags overrides any IPMI SDR settings.
     * 
     * 
     * */  	
    uint8_t   		InternalFlags;   
                            /* Bit 0 -  Enable/Disable scanning - overrides SDR scanning bit      */
                            /* Bit 1 -  reserved                 */
                            /* Bit 2 -  reserved                 */
                            /* Bit 3 -  reserved                 */
                            /* Bit 4 -  reserved                 */
                            /* Bit 5 -  reserved                 */
                            /* Bit 6 -  reserved                 */
                            /* Bit 7 -  reserved                 */
    uint8_t 	      OEMField;	

//	pPDK_MonitorHook_T pPreMonitor;
//	pPDK_MonitorHook_T pPostMonitor;
//    pPDK_SensorInitHook_T pInitSensor [MAX_INIT_SENSOR_HOOKS];
//    uint8_t				InitSensorHookCnt;
//    pPDK_MonitorExtHook_T  	pPostMontiorExt [MAX_POST_MONITOR_EXT_HOOKS];
//    uint8_t					PostMonitorExtHookCnt;
//    pPDK_PreEventLogHook_T 	pPreEventLog [MAX_PRE_EVENT_LOG_HOOKS];
//    uint8_t					PreEventLogHookCnt;

   /* The offset for the sensor is added for OPMA Support */
   uint8_t SenReadingOffset;
   uint8_t SensorOwnerLun;

   uint8_t	IsDCMITempsensor;
   uint8_t	EntityID;
   uint8_t	EntiryInstance;

   SDRRecHdr_T* SDRRec;

} SensorInfo_T;


/**
 * @struct SensorSharedMem_T
 * @brief Shared memory for Sensor Information.
**/

typedef struct
{
    SensorInfo_T        SensorInfo [MAX_SENSOR_NUMBERS + 1]; /* Holds all sensor informations */
	uint32_t			SensorTick;
    uint8_t       		GlobalSensorScanningEnable;

} SensorSharedMem_T;




#pragma pack ()

///*---------------------------------------
// * InitInternalSensors
// *---------------------------------------*/
//extern int InitInternalSensors (SensorInfo_T *pSensorInfo);


///**
// * @var g_SensorValues 
// * @brief Global array that holds current sensor values.
//**/
//extern uint8_t  g_SensorValues [];


///*
// * @fn InitPowerOnTick
// * @brief Initialize the power on tick counter, 
// *        should be invoked when ever power on occurs.
// */
//extern void InitPowerOnTick ();

///*
// * @fn InitSysResetTick
// * @brief Initialize the System Reset tick counter, 
// *        should be invoked when ever power reset occurs.
// */
//extern void InitSysResetTick ();
///**
// * @brief Initialize Sensor monitoring.
// * @return 0 if success, -1 if error.
//**/
//extern int InitSensorMonitor (void);

/**
 * @brief Sensor monitoring task.
**/
extern void *SensorMonitorTask (void*);

///**
// * @brief Returns the current sensor reading
//**/
//extern uint16_t SM_GetSensorReading (uint8_t SenNum, uint16_t *pSensorReading);


SensorInfo_T* getSensorInfo(uint8_t sensorNum);
int SensorAverage(uint8_t SensorNum, uint8_t* pSensorReading);
int get_sensor_reading (uint8_t sensor_num, uint8_t *preading);
#endif /* SENSOR_MONITOR_H */
