/****************************************************************
 ****************************************************************
 **                                                            **
 **    (C)Copyright 2005-2006, American Megatrends Inc.        **
 **                                                            **
 **            All Rights Reserved.                            **
 **                                                            **
 **        6145-F, Northbelt Parkway, Norcross,                **
 **                                                            **
 **        Georgia - 30071, USA. Phone-(770)-246-8600.         **
 **                                                            **
 ****************************************************************
 ****************************************************************/
/*****************************************************************
 *
 * MsgHndlr.h
 * Message Handler.
 *
 * Author: Govind Kothandapani <govindk@ami.com>
 * 		 : Rama Bisa <ramab@ami.com>
 *       : Basavaraj Astekar <basavaraja@ami.com>
 *       : Bakka Ravinder Reddy <bakkar@ami.com>
 *
 *****************************************************************/
#ifndef MSG_HNDLR_H
#define MSG_HNDLR_H

#include "com_Message.h"
#include "com_IPMIDefs.h"

#pragma pack( 1 )

#define BMC_LUN_00                      0
#define BMC_LUN_01                      1
#define BMC_LUN_10                      2
#define BMC_LUN_11                      3

#define LOOP_BACK_REQ                     0x10

#define LAN_IFC_SUP 0x0001
#define SOL_IFC_SUP 0x0002
#define SERIAL_IFC_SUP 0x0004
#define AMI_DEVICE_SUP 0x0008
#define USER_LOCKED                    1
#define USER_UNLOCKED                0
#define MAX_NETFN                        64

// /*FLASH TYPE*/
// #define YAFU_FLASH   1
// #define TFTP_FLASH   2
// #define HPM_FLASH    3


/*Get the current cmd channel for OBSM*/
//#define GetCurCmdChannel(BMCInst)	g_BMCInfo[BMCInst].Msghndlr.CurChannel

/*-----------------------
 * Command Handler Type
 *----------------------*/
typedef int (*pCmdHndlr_T) (  uint8_t* pReq, uint8_t ReqLen,   uint8_t* pRes);

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

}  CmdHndlrMap_T;

/*-------------------------------
 * Extended Command Handler Type
 *-------------------------------*/
typedef int (*pExCmdHndlr_T) (  uint8_t* pReq, uint32_t ReqLen,   uint8_t* pRes);

/*------------------------------
 * Extended Command Handler map
 *------------------------------*/
typedef struct
{
    uint8_t		Cmd;
    uint8_t		Privilege;
    pExCmdHndlr_T	CmdHndlr;
    uint8_t		ReqLen;		/* 0xFF - Any Length */
    uint16_t		FFConfig;
    uint16_t            IfcSupport;

} ExCmdHndlrMap_T;



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

}  MsgHndlrTbl_T;

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

} GroupExtnMsgHndlrTbl_T;




/*-------------------------------------------------------------
 * Pending Bridged Response Tbl Size
 * 
 * Do not increase beyond 60 as maximum sequence number is 64
 *------------------------------------------------------------*/
#define MAX_PENDING_BRIDGE_RES    64
#define MAX_PENDING_BRIDGE_TBL    2
#define PRIMARY_PB_TBL    0
#define SECONDARY_PB_TBL    1

typedef union
{
    IPMIMsgHdr_T        IPMIMsgHdr;
    IPMIUDSMsg_T        UDSMsgHdr;

}  ResMsgHdr_T;

typedef struct
{
    uint8_t               Used;
    uint8_t               TimeOut;
    uint8_t               SeqNum;           //转发到IPMB总线上的序号，也是PendingTbl的索引。
    uint8_t               ChannelNum;       //记录下发SendMessage命令的通道，Lan
    uint8_t               DstSessionHandle;
    uint8_t               SrcSessionHandle;
    uint32_t              SrcSessionID;
    int                   DestQ;            //记录下发SendMessage命令的接收队列，LanIfcQ
    uint8_t               OriginSrc;
    IPMIMsgHdr_T        ReqMsgHdr;
    ResMsgHdr_T         ResMsgHdr;
    uint8_t             ResDataOk;          //jimbo add, 响应数据准备好了标志。
    int                 UDSSocket;
}    PendingBridgedResTbl_T;

/*------------------------------
 * Pending Sequence Number
 *-----------------------------*/
//#define		NO_IPMB_PROTOCOL_CHANNEL	3
#define		MAX_PENDING_SEQ_NO			50
#define		SEQ_NO_EXPIRATION_TIME		5

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

#define MAX_IPMI_CMD_BLOCKS         3
// typedef struct
// {
//     uint8_t			Channel;		/**< Originator's channel number 		*/
//     HQueue_T		hSrcQ;			/**< Originator Queue 					*/
//     uint8_t			Param;			/**< Parameter							*/
//     uint8_t			Cmd;			/**< Command that needs to be processed */
//     uint8_t			NetFnLUN;		/**< Net function and LUN of command    */
//     uint8_t			Privilege;		/**< Current privilege level			*/
//     uint32_t			SessionID;		/**< Session ID if any					*/
//     uint8_t			RequestSize;						/**< Size of the Request	*/
//     uint8_t			Request	 [MSG_PAYLOAD_SIZE];		/**< Request Data	buffer	*/
//     uint8_t			ResponseSize;						/**< Size of the response	*/
//     uint8_t			Response [MSG_PAYLOAD_SIZE];		/**< Response Data buffer	*/
//       uint8_t*    pRequest;                   /**< Pointer to Request data    */
//       uint8_t*    pResponse;                  /**< Pointer to Response data   */

// }    IPMICmdMsg_T;

typedef struct
{
    uint8_t    NetFnLUN;
    uint32_t   SessionID;
    int    SrcQ ;
}   KCSBridgeResInfo_T;

#pragma pack( )

/*------------------ Timer Task Table --------------------------*/
typedef void (*pTimerFn_T) (void);
typedef struct
{
    uint8_t           NumSecs;
    pTimerFn_T      TimerFn;

} TimerTaskTbl_T;

/*-----------------------------------------------------------
 * Bridge Status
 *---------------------------------------------------------*/
#define STATUS_OK        0x0
#define STATUS_FAIL      0x1


/* Extern declaration */
extern int GetCmdHndlr(MsgPkt_T* pReq,MsgPkt_T* pRes,CmdHndlrMap_T* pCmdHndlrMap,
                                uint32_t HdrOffset,uint8_t CmdOverride,CmdHndlrMap_T** CmdHndrl );
extern int	  GetMsgHndlrMap (uint8_t NetFn,   CmdHndlrMap_T ** pCmdHndlrMap);

/**
*@fn GroupExtnGetMsgHndlrMap
*@brief Gets the exact command Handler by comparing NetFn
*@param Netfn -NetFunction of the Cmd to execute
*@GroupExtnCode - Group Extension code
*@param pCmdHndlrMap Pointer to the Command Handler
*/
extern int 	  GroupExtnGetMsgHndlrMap (uint8_t NetFn, uint8_t GroupExtnCode, CmdHndlrMap_T ** pCmdHndlrMap);

//extern PendingBridgedResTbl_T	m_PendingBridgedResTbl[MAX_PENDING_BRIDGE_TBL][MAX_PENDING_BRIDGE_RES];
extern uint8_t CalculateCheckSum2 (  uint8_t* Pkt, uint32_t Len);
extern void  SwapIPMIMsgHdr (  IPMIMsgHdr_T* pIPMIMsgReq,   IPMIMsgHdr_T* pIPMIMsgRes);
extern void  SwapUDSIPMIMsg (  MsgPkt_T* pIPMIMsgReq,   MsgPkt_T* pIPMIMsgRes);

/**
* @fn GetIfcSupport
* @brief This function checks the support of Interface before 
*            Interface specific commands are executed
* @param IfcSupt - Interface support variable to be verified
* @param IfcSupport - Gives the Interface presence support
*/
//extern int GetIfcSupport(uint16_t IfcSupt,uint8_t *IfcSupport);

/*------------------------
 * Privilege Levels
 *------------------------*/
#define PRIV_NONE		0x00
#define PRIV_CALLBACK	0x01
#define PRIV_USER		0x02
#define PRIV_OPERATOR	0x03
#define PRIV_ADMIN		0x04
#define PRIV_OEM		0x05
#define PRIV_LOCAL		0x81
#define PRIV_SYS_IFC	0x82

/*------------------------------
* Channel type used for BadPassword
*------------------------------*/
#define LAN_CHANNEL_BADP       0x1
#define SERIAL_CHANNEL_BADP  0x2

/*----------------------------
 * Command Handler Example (app.c)
 *----------------------------*/
/*
#ifdef	APP_DEVICE

 .... Code ....

const CmdHndlrMap_T	g_APP_CmdHndlr [] =
{
	{ CMD_GET_DEV_ID,			PRIV_USER,		GET_DEV_ID,			  0x00 },
	{ CMD_BROADCAST_GET_DEV_ID, PRIV_LOCAL,		BROADCAST_GET_DEV_ID, 0x00 },
	...
	...
	{ 0x00, 					0x00, 			0x00,				  0x00 }
};

#else
const CmdHndlrMap_T	g_APP_CmdHndlr [] = { 0x00, 0x00, 0x00, 0x00 };
#endif	// APP_DEVICE
*/

/*---------------------------
 * app.h
 *---------------------------*/
/*
#ifndef APP_H
#define APP_H
#include "MsgHndlr.h"

extern const CmdHndlrMap_T g_APP_CmdHndlr [];

#endif
*/

/*---------------------------------------------------
 * Parameters passed by through the message handler Q.
 *---------------------------------------------------*/
// #define PARAM_IFC			        0x01
// #define PARAM_TIMER			        0x02
// #define PARAM_EXECUTE_SP	        0x03
// #define PARAM_SENSOR_MONITOR        0x04
// #define PARAM_SENSOR_MONITOR_DONE   0x05
// #define PARAM_IFC_READY				0x06
#define RCVMSGQ_LENGTH              100


#define NO_RESPONSE			        0x10
#define NORMAL_RESPONSE		        0x11
//#define BRIDGING_REQUEST	        0x12


/*----------------------------------------------------
 * Receive Message Queue Names
 *----------------------------------------------------*/
extern   char g_RcvMsgQ [3][RCVMSGQ_LENGTH];

/*------------------------------------------------------------------
 *@fn RespondSendMessage
 *@brief Frames the Response packet when a IPMB destination is
 *       unavialable
 *
 *@param pReq:    Request Message Packet address
 *@param Status   Status of SendIPMBPkt method
 *@param BMCInst: BMC Instance Number
 *
 *@return none
 *-----------------------------------------------------------------*/
extern void RespondSendMessage (   MsgPkt_T* pReq, uint8_t Status);

/**
*@fn ValidateMsgHdr
*@brief Validates the Message header and keeps track of the messages that has been bridged
*@param pReq Request packet of the command to be executed
*@return Returns -1 in case of the response to the bridged message
*            Returns 0 otherwise
*/
extern int  ValidateMsgHdr (  MsgPkt_T* pReq);


#endif  /* MSG_HNDLR_H */

