/****************************************************************
 ****************************************************************
 **                                                            **
 **    (C)Copyright 2005-2006, American Megatrends Inc.        **
 **                                                            **
 **            All Rights Reserved.                            **
 **                                                            **
 **        6145-F, Northbelt Parkway, Norcross,                **
 **                                                            **
 **        Georgia - 30071, USA. Phone-(770)-246-8600.         **
 **                                                            **
 ****************************************************************
 ****************************************************************/
/*****************************************************************
 *
 * Session.h
 *
 *
 * Author: Govind Kothandapani <govindk@ami.com>
 * 		 : Rama Bisa <ramab@ami.com>
 *       : Basavaraj Astekar <basavaraja@ami.com>
 *       : Bakka Ravinder Reddy <bakkar@ami.com>
 *
 *****************************************************************/
#ifndef SESSION_H
#define SESSION_H
#include "com_BmcType.h"
#include "com_IPMI_LANIfc.h"
#include "com_IPMI_RMCP+.h"
#include "com_IPMI_AppDevice.h"
#include "com_IPMIDefs.h"
#include "com_Message.h"
#include "PMConfig.h"
#pragma pack( 1 )

/* Macros */
#define		SESSION_ID_INFO							1
#define		SESSION_HANDLE_INFO						2
#define		SESSION_INDEX_INFO						3
#define		SESSION_CHANNEL_INFO					4
#define		MGD_SYS_SESSION_ID_INFO					5
#define		SESSION_PAYLOAD_INFO					6
#define		SESSION_REMOTE_INFO					7
#define		MAX_INST_SUPPORTED						2
#define		SESSIONLESS_CHANNEL						0x00
#define		SINGLE_SESSION_CHANNEL					0x01
#define		MULTI_SESSION_CHANNEL					0x02
#define		SESSION_BASED_CHANNEL					0x03
#define		HASH_DATA_LENGTH						16
#define		NULL_USER_ID							1

#define UDS_SESSION_COUNT_INFO                      0x00
#define UDS_SESSION_ID_INFO                         0x01
#define UDS_SESSION_HANDLE_INFO                     0x02
#define UDS_SESSION_INDEX_INFO                      0x03
#define UDS_SOCKET_ID_INFO                          0x04
#define UDS_SESSION_PID_INFO                        0x05
#define UDS_ACTIVE_SESSION_INDEX_LIST               0x06

/* UDS Session Table Info */
typedef struct
{
   uint32_t SessionID;
   uint32_t LoggedInTime;
   uint32_t SessionTimeoutValue;
   int UDSSocket;
   uint8_t LoggedInUsername[MAX_USERNAME_LEN];
   uint8_t LoggedInPassword[MAX_PASSWORD_LEN];
   uint8_t LoggedInUserID;
   uint8_t Activated;
   uint8_t LoggedInSessionHandle;
   uint8_t UDSChannelNum;
   uint8_t LoggedInChannel;
   uint8_t LoggedInPrivilege;
   uint8_t AuthenticationMechanism;
   uint32_t ProcessID;
   uint32_t ThreadID;
   uint8_t IPAddr [IP6_ADDR_LEN];
}  UDSSessionTbl_T;

typedef struct
{
    uint16_t SessionCount;
    UDSSessionTbl_T *UDSSessionTbl;
}  UDSSessionTblInfo_T;

/* SessPayloadInfo_T */
typedef struct
{
    uint8_t    Type;
    uint8_t    OemPldIANA [3];
    uint8_t    OemPldID [2];
    uint8_t    Version;
    uint16_t    PortNum;
    uint16_t    ActivatedInst;
    uint8_t    AuxConfig [4];

}    SessPayloadInfo_T;


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

/* SessionTblInfo_T */
typedef	struct
{
    uint16_t			Count;
    SessionInfo_T	*SessionTbl;
}    SessionTblInfo_T;


typedef struct
{
    uint32_t Time;
    uint8_t  Index;
}   OldSessionInfo_T;

typedef enum
{
    KCS_IFC = 0x00,
    IPMB_IFC,
    LAN_IFC,
    SERIAL_IFC,
}IfcType_T;

#pragma pack( )

// /*--------------------------
//  * Extern Declarations
//  *--------------------------*/
extern		 void				SessionTimerTask (void);
// extern  ChannelInfo_T*  getChannelInfo (uint8_t ch );
// extern _FAR_ ChannelUserInfo_T* getChUserIdInfo (uint8_t userId, _NEAR_ uint8_t *index, _FAR_ ChannelUserInfo_T* pChUserInfo, int BMCInst);
extern SessionInfo_T*		getSessionInfo (uint8_t Arg, void *Session);
// extern _FAR_ ChannelUserInfo_T* getChUserPrivInfo (_NEAR_ char *userName, _NEAR_ uint8_t Role, _NEAR_ uint8_t* chIndex, _FAR_ ChannelUserInfo_T *pChUserInfo, int BMCInst);
// extern _FAR_ ChannelUserInfo_T*	getChUserInfo (_NEAR_ char *userName, _NEAR_ uint8_t* chIndex, _FAR_ ChannelUserInfo_T *pChUserInfo, int BMCInst);
extern UserInfo_T*		getUserIdInfo (uint8_t userId);
extern  UserInfo_T* getUserNameInfo (uint8_t *UserName);
extern uint8_t	CheckForDuplicateUsers (uint8_t* UserName);
// extern _FAR_ ChannelUserInfo_T*	AddChUser (_FAR_ ChannelUserInfo_T*	pChUserInfo, _NEAR_ uint8_t*	Index );
// extern _FAR_ ChannelInfo_T* GetNVRChConfigs(ChannelInfo_T *pChannelInfo, int BMCInst);
// extern _FAR_ ChannelUserInfo_T* GetNVRChUserConfigs(ChannelInfo_T *pChannelInfo );
// extern uint8_t disableUser (uint8_t UserId, int BMCInst);
extern uint8_t GetNumOfActiveSessions (void);
extern uint8_t GetNumOfUsedSessions (void);
extern void  DeleteSession (SessionInfo_T*	pSessionInfo );
extern void  AddSession (SessionInfo_T* pSessionInfo );
extern uint8_t CleanSession(void);
// extern _FAR_ uint16_t	getPayloadActiveInst (uint8_t PayloadType, int BMCInst);
// extern _FAR_ uint32_t	getPayloadInstInfo (uint8_t PayloadType, uint16_t PayloadInst );
extern void	PasswordViolation (void);
extern void UDSSessionTimerTask (void);
extern int AddUDSSession (UDSSessionTbl_T* pUDSSessionInfo);
extern int DeleteUDSSession(UDSSessionTbl_T*  pUDSSessionInfo );
extern UDSSessionTbl_T* GetUDSSessionInfo (uint8_t Type,void *Data );
// /*---------------------------------------------------
//  * @fn UpdateGetMsgTime
//  * @brief Updates the Current Uptime and timeout value
//  *        for an IPMI Message
//  * 
//  * @param pReq       : IPMI Message Packet
//  * @param ResTimeOut : Timeout Macro string
//  * @param BMCInst    : BMC Instance
//  * 
//  * @return none
//  *----------------------------------------------------*/

// extern void UpdateGetMsgTime (MsgPkt_T* pReq,IfcType_T IfcType, int BMCInst);

// /*---------------------------------------------------
//  * @fn IsMsgTimedOut
//  * @brief Checks if the time taken to process 
//  *        the IPMI Command expires
//  * 
//  * @param pReq     : IPMI Message Packet
//  * 
//  * @return 1 if timed out
//  *         0 if error or timeout not set 
//  *----------------------------------------------------*/

// extern BOOL IsMsgTimedOut (MsgPkt_T* pReq);

// /*-----------------------------------------------------
//  * @fn IsResponseMatch
//  * @brief Checks if the Response Message corresponds to 
//  *        the Request Message
//  * 
//  * @param pReq    : IPMI Request Message Packet
//  * @param pRes    : IPMI Response Message Packet
//  * 
//  * @return  1 if match
//  *          0 if mismatch
//  *----------------------------------------------------*/

// extern BOOL IsResponseMatch (MsgPkt_T* pReq, MsgPkt_T* pRes);

/*-------------------------------------------------------
 * @fn FillIPMIResFailure
 * @brief Frames the Response packet when the time taken 
 *        to process an IPMI Command expires
 * 
 * @param pReq    : Request Message Packet
 *        pRes    : Response Message Packet
 *        BMCInst : BMC Instance Number
 * 
 * @return  none
 *------------------------------------------------------*/
extern void FillIPMIResFailure ( MsgPkt_T* pReq, MsgPkt_T* pRes);

#endif	/* SESSION_H */
