/*
 * Define custom data type in this file.
 * Author: Jimbo
 */
#ifndef __BMC_TYPE_H__
#define __BMC_TYPE_H__
#include <stdint.h>
//#include "bmc_main.h"
#include "sensor_sdr.h"
#include "sensor_tbl.h"
#include "bmc_conf.h"
#include "Types.h"
#include "time.h"

#define   	IPMI_IFC_NAME_LEN   		60

/*---- Handle to a Queue -----*/
typedef long    HQueue_T;
typedef int (*pCmdHndlr_T) (_NEAR_ INT8U* pReq, INT8U ReqLen, _NEAR_ INT8U* pRes);




typedef int (*pPDK_SensorInitHook_T) (void *pSensorInfo,int BMCInst);
typedef int (*pPDK_MonitorHook_T) (void* pSensorInfo,INT8U *pReadFlags,int BMCInst);
typedef int (*pPDK_MonitorExtHook_T) (void *pSensorInfo, INT8U* pReadFlags,int BMCInst);


/*----------------------
 *  *  * Command Handler map
 *   *   *----------------------*/
typedef struct
{
    INT8U               Cmd;
    INT8U               Privilege;
    pCmdHndlr_T         CmdHndlr;
    INT8U               ReqLen;         /* 0xFF - Any Length */
    INT16U              FFConfig;
    INT16U              IfcSupport;

}  CmdHndlrMap_T;


/***     add in ??? ***/


typedef struct
{
    INT8U			NetFn;
    INT8U			GroupExtnCode;
    const CmdHndlrMap_T*	CmdHndlrMap;

} GroupExtnMsgHndlrTbl_T;

//GroupExtnMsgHndlrTbl_T m_GroupExtnMsgHndlrTbl [10] =		//notice!
//{

//};

#define ECC_CE_THRESHOLD        24
#define ECC_CE_TIMEOUT          (24 * 60 *60) // 24 hours 

#define MAX_PS_NUM              3
#define MAX_CPU_NUM             2
#define MAX_FAN_NUM             8
#define MAX_HDD_NUM             4
#define MAX_DIMM_NUM            2 // DIMM pairs 

typedef struct
{
    INT8U    TempFault;
    INT8U    PSFault  [MAX_PS_NUM  + 1];
    INT8U    FanFault [MAX_FAN_NUM + 1];
    INT8U    CPUFault [MAX_CPU_NUM + 1];
    INT8U    HDDFault [MAX_HDD_NUM + 1];
    INT8U    ECCFault_UE [MAX_CPU_NUM][MAX_DIMM_NUM];
    INT32U   ECCFault_CE [MAX_CPU_NUM][MAX_DIMM_NUM][ECC_CE_THRESHOLD];
    INT8U    NumAlerts;
    INT8U    NumFatal;
} PDK_FaultInfo_T;



typedef struct
{
    INT8U    ChassisID;
    INT8U    BoardRev;
    INT8U    SPBoardRev;
    INT8U    IOBoard;
    INT8U    NMIBtnStatus;
    INT8U    FLctBtnStatus;
    INT8U    BLctBtnStatus;
    PDK_FaultInfo_T FaultInfo;
} PDK_SharedMem_T;
/***     add in ??? ***/



typedef struct
{
    INT8U CmdNum;
    INT8U Status;
}NetFnCmds_T;

typedef struct
{
    INT8U NetFn;
    INT8U GroupExtCode;
    const NetFnCmds_T *NetFunction;
}NETFNTable_T;


typedef struct
{
    	INT8U	        sensorReading;
	INT8U		sensorMonitBusy;
	sensor_tbl_t 	*sensorDev;
	SDRRec_T	*sensorSdr;  	
} SensorInfo_T;


/* GetSDRReq_T */
typedef struct
{
    INT16U  ReservationID;
    INT16U  RecID;
    INT8U   Offset;
    INT8U   Size;

} PACKED  GetSDRReq_T;

/* GetSDRRes_T */
typedef struct
{
    INT8U   CompletionCode;
    INT16U   NextRecID;
//    INT8U   NextRecID_L;
//    INT8U   Data;	
} PACKED  GetSDRRes_T;

/* IPMI Message Header */
typedef struct
{
    INT8U ResAddr;
    INT8U NetFnLUN;
    INT8U ChkSum;
    INT8U ReqAddr;
    INT8U RqSeqLUN;
    INT8U Cmd;
} IPMIMsgHdr_T;


typedef struct
{
    INT8U   Used;
    INT8U   TimeOut;
    INT8U   SeqNum;
    INT8U   NetFn;
    INT8U   Cmd;
}  PendingSeqNoTbl_T;

/* GetSensorThresholdReq_T */
typedef struct
{
    INT8U   SensorNum;

} PACKED  GetSensorThresholdReq_T;


/* GetSensorThresholdRes_T */
typedef struct
{
    INT8U   CompletionCode;
    INT8U   GetFlags;
    INT8U   LowerNonCritical;
    INT8U   LowerCritical;
    INT8U   LowerNonRecoverable;
    INT8U   UpperNonCritical;
    INT8U   UpperCritical;
    INT8U   UpperNonRecoverable;

} PACKED  GetSensorThresholdRes_T;

/* GetSensorReadingReq_T */
typedef struct
{
    INT8U   SensorNum;

} PACKED  GetSensorReadingReq_T;


/* GetSensorReadingRes_T */
typedef struct
{
    INT8U   CompletionCode;
    INT8U   SensorReading;
    INT8U   Flags;
    INT8U   ComparisonStatus;
    INT8U   OptionalStatus;

} PACKED  GetSensorReadingRes_T;

typedef struct
{
    INT32U 	SessionID;
    INT8U 	Privilege;
    INT16U 	IPMIMsgLen;
    INT8U 	NetFnLUN;
    INT8U 	Cmd;
    INT8U 	ChannelNum;
    INT8U 	AuthFlag;
    INT8U 	UserName[MAX_USER_NAME_LEN];
    INT8U 	IPAddr[IP6_ADDR_LEN];
    INT32U 	ProcessID;
    INT32U 	ThreadID;
}PACKED IPMIUDSMsg_T;

typedef union
{
    IPMIMsgHdr_T        IPMIMsgHdr;
    IPMIUDSMsg_T        UDSMsgHdr;

}PACKED ResMsgHdr_T;

typedef struct
{
    INT8U               Used;
    INT8U               TimeOut;
    INT8U               SeqNum;
    INT8U               ChannelNum;
    INT8U               DstSessionHandle;
    INT8U               SrcSessionHandle;
    INT32U              SrcSessionID;
    INT8U               DestQ[PIPE_NAME_LEN];
    INT8U               OriginSrc;
    IPMIMsgHdr_T        ReqMsgHdr;
    ResMsgHdr_T         ResMsgHdr;
}  PendingBridgedResTbl_T;



/**
 *  * @struct ChannelUserInfo_T
 *   * Structure for user information per channel.
 *   **/
typedef struct
{
    INT32U  ID;
    INT8U   UserId;
    INTU    AccessLimit:4;
    INTU    LinkAuth:1;
    INTU    UserAccessCallback:1;
    INTU    UserAccessCBCPCallback:1;
    INT8U   UserCallbackCapabilities;
    INT8U   CBCPNegOptions;
    INT8U   CallBack1;
    INT8U   CallBack2;
    INT8U   CallBack3;
    INT8U   FailureAttempts;
    INT8U   Lock;
    INT32U LockedTime;
    INT8U  PayloadEnables [MAX_PLD_ENABLES_TYPES];

    INT8U  IPMIMessaging;
    INT8U  ActivatingSOL;

} PACKED  ChannelUserInfo_T;

/**
 *  * @struct UserInfo_T
 *   * Structure for user information.
 *   **/

typedef struct
{
 INT32U  ID;
 INT8U   UserId;
 INT8U   UserName [MAX_USERNAME_LEN];
 INT8U   UserPassword [MAX_PASSWORD_LEN];
 INT8U   MaxPasswordSize;                          /**< Maximum password size                                      */
 INT8U   UserShell;                            /**< user shell type                            */          
 INT8U     UserEMailID [EMAIL_ADDR_SIZE];  /**< Email-ID registered for the user           */
 INT8U   MaxSession;                       /**< max No of session allowed for User.    */
 INT8U   CurrentSession;                   /**< No Current session for the User.       */
 INTU    UserStatus:1;                     /**< User Enabled/Disabled Status.          */
 INTU    FixedUser:1;                      /**< Is this user name fixed                */
 INT8U  EmailFormat[EMAIL_FORMAT_SIZE]; /** Email Alert format                       */
 INT32U  ExtendedPrivilege;                       /** User Enhanced Privilege Flags           */
 INT8U   UserPasswdConfigured;                     /*  Flag to allow default Empty Password  */
} PACKED  UserInfo_T;
//
//
///**
// * @struct DefaultChCfg_T
//  * Structure of channel Information.
//  **/
//



typedef struct
{
    INT32U          ID;
    INTU            ChannelIndex;
    INTU            ChannelNumber:4;
    INTU            ChannelType:7;     /* Channel Type */
    INTU            ChannelMedium:7;
    INTU            ChannelProtocol:5;
    INTU            SessionSupport:2;               /**< session support. */
    INTU            ActiveSession:6;                /**< No of active session in this channel. */
    INT8U           AuthType [MAX_PRIVILEGE_TYPES]; /**< Authentication according to privilege. */
    INT8U           ProtocolVendorId [3];
    INT8U           AuxiliaryInfo [2];
    INTU            ReceiveMsgQ:1;            /**< enable/disable  message to Receive Message Queue. */
    INTU            AccessMode:4;             /**< channel access mode - disabled/perboot/always/shared. */
    INTU            AccessModeSupported:4;    /**< access mode supported by this channel. */
    INTU            Alerting:1;               /**< enable/disable alerting through this channel. */
    INTU            PerMessageAuth:1;         /**< enable/disable per message Auth through this channel. */
    INTU            UserLevelAuth:1;          /**< enable/disable userlevel Auth through this channel. */
    INTU            MaxPrivilege:4;           /**< max privilage level allowed by this channel. */
    INTU            reserved:1;
    INTU            SetUserAccess:1;          /**< set user access command not allowed. */
    INTU            SetChPrivilege:2;
    INTU            SessionLimit:6;
    INTU            LoginStatusNUP:1;
    INTU            LoginStatusNUNP:1;
    INTU            LoginStatusNNU:1;
    INTU            MaxUser:6;
    INTU            NoCurrentUser:6;
    INTU            NoFixedUser:6;
    INTU            ChannelIndexRam:4;
    INTU            ChannelIndexNvRam:4;
  ChannelUserInfo_T ChannelUserInfo [MAX_NUM_CH_USERS_MDS];

} PACKED  ChannelInfo_T;


/*-------------------------
 *  * Message Handler Table
 *   *-------------------------*/
typedef struct
{
    INT8U			NetFn;
    const CmdHndlrMap_T*	CmdHndlrMap;

}  MsgHndlrTbl_T;

/*-----------------------------------
 *  * Type definitions
 *   *-----------------------------------*/
typedef struct
{
    INT32U      Param;                    /* Parameter */
    INT8U       Channel;                  /* Originator's channel number */ //=1
    INT8U       SrcQ [PIPE_NAME_LEN];     /* Originator Queue */
    INT8U       Cmd;                      /* Command that needs to be processed*/
    INT8U       NetFnLUN;                 /* Net function and LUN of command   */// =0
    INT8U       Privilege;                /* Current privilege level */ //=0
    INT32U      SessionID;                /* Session ID if any */
    time_t      ReqTime;                  /* Request Timestamp */ 
    INT16U      ResTimeOut;              /* response timeout in secs */ //=10
    INT16U      SessionType;               /* Session Type */
    INT8U       IPAddr [IP6_ADDR_LEN];    /* IPv6 Address */
    INT16U      UDPPort;                  /* UDP Port Number  */
    INT16U      Socket;                   /* socket handle    */
    INT32U      Size;                     /* Size of the data */
    INT8U       Data [MSG_PAYLOAD_SIZE];  /* Data */
} MsgPkt_T;

typedef struct
{
    INT8S IfcName[IPMI_IFC_NAME_LEN];
    HQueue_T IfcQ;
}PACKED IPMIQueue_T;


typedef struct
{
    INT8U    IPMBBusNum;
    INT8S    IPMBBusName[64];
    int  IPMBSlaveFd;
    int  IPMBMasterFd;
    INT8U	m_IPMBSeqNo;

} IPMB_T;


typedef struct {
	INT8U		pwr_good;	//power good.
	INT8U		S3;
	INT8U		S4;
	INT8U		S5;
}PowerSta_t;

typedef struct
{
    INT8U SendMsgTimeout;//
    INT8U SessionTimeOut;//
    INT8U SOLSessionTimeOut;//
    INT8U PrimaryIPMBBusNumber;
    INT8U SecondaryIPMBBusNumber;
    INT8U ChassisTimerInterval;
    INT8U EEPROMBusNum;//
    INT8U AlterPreConfiguredEntries;//
    INT8U ChassisInterruptSupport ;//
    INT8U InterruptSensorHandling;//
    INT8U EventsForRearm ;//
    char *pSOLPort;//
    char *pSerialPort;//
    INT8U MaxSession;//
    INT8U SDRAllocationSize;//
    INT8U SELAllocationSize;//
    INT8U BMCSlaveAddr;
    INT8U MaxUsers;//
    INT8U MaxChUsers;//
    INT8U PrimaryIPMBSupport ;
    INT8U SMMIfcSupport ;//
    INT8U SerialIfcSupport ;//
    INT8U SerialTerminalSupport;//
    INT8U LANIfcSupport ;//
    INT8U KCS1IfcSupport ;//
    INT8U KCS2IfcSupport ;//
    INT8U KCS3IfcSuppport ;//
    INT8U SYSIfcSupport;//
    INT8U SMBUSIfcSupport;//
    INT8U ICMBIfcSupport ;//
    INT8U SecondaryIPMBSupport ;
    INT8U MaxLanChannel ;//
    INT8U KCSSMMChannel;//
    INT8U USBIfcSupport ;//
    INT8U SOLIfcSupport ;//
    INT8U APMLSupport;//
    INT8U DCMISupport ;//
    INT8U HPMSupport;//
    INT8U OPMASupport;//
    INT8U GrpExtnSupport ;//
    INT8U CardInFlashMode ;//
    INT8U VLANIfcSupport;//
    INT8U UDSIfcSupport;//
    INT8U LinkDownResilentSupport;//
    INT8U RearmSetSensorThreshold;//
    INT8U APMLBusNumber;//
    INT8U PrimaryIPMBAddr;
    INT8U SecondaryIPMBAddr;
    INT8U BTIfcSupport;//
    INT8U ChassisTimerSupport;//
    INT8U IPMIFirewallSupport;//
    INT8U InternalPwrGoodMonitoring;//
} IPMIConfig_T;


typedef struct
{
    INT8U ChType;
    ChannelInfo_T ChannelInfo;
} PACKED ChcfgInfo_T;


/*	structure to  keep track the information about a session.	*/
/* SessionInfo_T */
typedef struct
{
    INT8U			Used; /* Flag to indicate the slot used or not */
    INT32U			SessionID;
    INT8U			Activated;
    INT8U			Channel;
    INT8U			AuthType;
    INT8U			Privilege;
    INT8U			MaxPrivilege;
    INT32U			InboundSeq;
    INT32U			OutboundSeq;
    INT32U			TimeOutValue;
    INT8U			Password[MAX_PASSWORD_LEN];
    INT8U			UserId;
    INT8U			SessionHandle;
    INT8U			ChallengeString[CHALLENGE_STR_LEN];
//    LANRMCPPkt_T	LANRMCPPkt;
    SOCKET			hSocket;
    BOOL			SerialModemMode;
    INT32U 			Time;
#if (IPMI20_SUPPORT == 1)
    INT8U				Lookup;
    INT32U				RemConSessionID;
    INT8U				RemConRandomNo [16];
    INT8U				MgdSysRandomNo [16];
    INT8U				AuthAlgorithm;
    INT8U				IntegrityAlgorithm;
    INT8U				ConfidentialityAlgorithm;
//    INT8U				Key1 [MAX_HASH_KEY_SIZE];
//    INT8U				Key2 [MAX_HASH_KEY_SIZE];
//    SessPayloadInfo_T	SessPyldInfo [MAX_PYLDS_SUPPORT];
#endif
    INT32U                   InitialInboundSeq;
    INT32U                   InboundTrac[SIXTEEN_COUNT_WINDOW_LEN];
    INT16U                   InboundRecv;
    INT8U 			IsLoopBack;
    INT8U                    Linkstat;
    INT8U 			EventFlag;
    INT8U                       UserName[MAX_USER_NAME_LEN];
} SessionInfo_T;


/* SDRRepositoryInfo_T */
typedef struct
{
    INT8U   CompletionCode;     /* Completion Code */
    INT8U   Version;            /* Version of the SDR 0x51 */
    INT16U  RecCt;              /* Record Count */
    INT16U  FreeSpace;          /* Free space LSB first */
    INT32U  AddTimeStamp;       /* Most recent addition timestamp */
    INT32U  EraseTimeStamp;     /* Most recent erase timestamp */
    INT8U   OpSupport;          /* Operation Support */

} PACKED  SDRRepositoryInfo_T;


/* SessionTblInfo_T */
typedef	struct
{
    INT16U			Count;
    SessionInfo_T	*SessionTbl;
} PACKED  SessionTblInfo_T;

typedef struct
{
    INT16U  ReservationID;
    INT16U  RecordDataOffset[MAX_SENSOR_NUM];
    INT8U   SDR[MAX_SENSOR_NUM][6];
} SDR_T;


typedef struct {
	INT8U	 	slotID;
	INT32U		sensorTick;	//plus 1 every seconds.
	INT32U		powerOnTick;	//count time in seconds after bmc power on.
	INT32U		sysResetTick;	//count time in seconds after cpu reset.
	PowerSta_t	powerSta;
	SensorInfo_T 	sensorInfo[MAX_SENSOR_NUM+1];
	UserInfo_T UserInfo[MAX_USER_CFG_MDS];

	//IPMB
	INT8U 		SERIALch ;
	SDR_T 		SDRConfig;
	SessionTblInfo_T SessionTblInfo;
	INT8U		PrimaryIPMBCh;
	INT8U		SecondaryIPMBCh;
	IPMB_T		IPMBConfig;
	INT8U           IPMBAddr;
	INT8U  		SendMsgSeqNum;
	ChcfgInfo_T 	ChConfig;	
	INT8U           MsgHndlrTblSize;
	MsgHndlrTbl_T 	MsgHndlrTbl[15];
	IPMIConfig_T 	IpmiConfig;
	
} BMCInfo_t;


/* ReserveSDRRepositoryRes_T */
typedef struct
{
    INT8U   CompletionCode;
    INT8U   ReservationID_H;
    INT8U   ReservationID_L;
//    INT8U   Data;

} PACKED  ReserveSDRRepositoryRes_T;

typedef struct
{
    INT8S Ifcname[16];
    INT8U Index;
    INT8U Enabled;
}PACKED IfcName_T;


// IfcName_T Ifcnametable [MAX_CHANNEL] = 
//{
//        {"eth0", 0, 0},   
//        {{0}, 0, 0},
//        {{0}, 0, 0},
//        {{0}, 0, 0},
//};




typedef union{
 uint8_t Data[28];
 struct{
 /** RMCP Header **/
  uint8_t Version;
  uint8_t Reserved1;
  uint8_t RMCPSqNum;
  uint8_t Class;
 /** ASF Message **/
  uint8_t ASF_IANA[4];
  uint8_t MessageType;
  uint8_t MessageTag;
  uint8_t Reserved2;
  uint8_t DataLen;
  uint8_t OEM_IANA[4];
  uint8_t Oem_def[4];
  uint8_t SupportEntities;
  uint8_t SupportInteractions;
  uint8_t Reserved3[6];
 } AST_T; 

} ASF_Pong_Message;


typedef union{
 uint8_t Data[128];
 struct{
 /** RMCP Header **/
  uint8_t Version;
  uint8_t Reserved1;
  uint8_t RMCPSqNum;
  uint8_t Class;
 /** IPMI Message **/
  uint8_t MessageType;
  uint8_t Sequence_Number[4];
  uint8_t Session_ID[4];
  uint8_t MessageLength;
  uint8_t MessageData[114];
}AST_T; 

} IPMI_Message;



typedef struct
{
    INT8U	ChannelNum;
    INT8U	Privilege;

} PDK_ChannelInfo_T;

/*   add in     */

#define MAX_PDK_FRU_SUPPORTED 10

#define MAX_NETFN                        64

#define VALID_LUN                       0x01

#define MAX_SENSOR_NUMBERS	    		(256 + (256 * VALID_LUN))

#define MAX_ID_STR_FILE_NAME 128


typedef void (*pTimerFn_T) (int BMCInst);



typedef struct
{
    INT8U   DeviceID;       /* FRU Device ID                                */
    INT8U   IsInternalFRU;  /* Flag to hide FRU					*/
    INT8U   Type;           /* FRU type NVR/EEPROM/....                     */
    INT16U  Size;           /* FRU Device size                              */
    INT8U   AccessType;     /* FRU device access type BYTE/WORD             */
    INT8S   NVRFile[MAX_ID_STR_FILE_NAME];        /* FRU file name if FRU is NVR                  */
    INT8U   BusNumber;      /* Bus number if FRU is EEPROM                  */
    INT8U   SlaveAddr;      /* FRU device slave address if FRU is EEPROM    */
    INT8U   DeviceType;     /* FRU device type if FRU is EEPROM             */
    INT16U  Offset;         /* FRU start offset in a physical FRU           */
    INT8S   FRUName[MAX_ID_STR_LEN]; /* FRU Name                 */
} FRUInfo_T;

typedef struct
{
    INT8U           NumSecs;
    pTimerFn_T      TimerFn;

} TimerTaskTbl_T;



#endif /* __BMC_TYPE_H__ */
