#include "session.h"



INT8U Set_BMCInfo_IPMIConfig_LANIfcSupport(INT8U Value,int BMCInst)
{
    _FAR_ BMCInfo_t* pBMCInfo = &g_BMCInfo;//[BMCInst];
    pBMCInfo->IpmiConfig.LANIfcSupport=Value;
    return 0;
}

INT8U Set_BMCInfo_IPMIConfig_SerialIfcSupport(INT8U Value,int BMCInst)
{
    _FAR_ BMCInfo_t* pBMCInfo = &g_BMCInfo;//[BMCInst];
    pBMCInfo->IpmiConfig.SerialIfcSupport=Value;
    return 0;
}


INT8U IsLANChannel(INT8U Channel, int BMCInst)
{
    _FAR_ BMCInfo_t* pBMCInfo = &g_BMCInfo;//[BMCInst];
    int i=0;

    //for(i=0;i<sizeof(pBMCInfo->LanIfcConfig)/sizeof(LANIFCConfig_T);i++)
        {
            //if(pBMCInfo->LanIfcConfig[i].Chnum == Channel)
               {
                  return 1;
               }
        }
    
       return 0;
}
                                                  
_FAR_ ChannelUserInfo_T* getChUserIdInfo (INT8U userId, _NEAR_ INT8U *Index, _FAR_ ChannelUserInfo_T*   pChUserInfo, int BMCInst)
{
    _FAR_ BMCInfo_t* pBMCInfo = &g_BMCInfo;//[BMCInst];

    if (userId == 0) { return NULL; }

    for(*Index=0; *Index < pBMCInfo->IpmiConfig.MaxChUsers; (*Index)++)
    {
        if ((pChUserInfo->UserId == userId) && ((pChUserInfo->ID == USER_ID )))
        {
            return pChUserInfo;
        }
        pChUserInfo++;
    }
    printf("getChUserIdInfo is NULL\n");
    return NULL;
}

ChannelInfo_T*  getChannelInfo (INT8U ch,int BMCInst)
{

    INT8U Index;
    ChcfgInfo_T *pChannelInfo=NULL;
    _FAR_ BMCInfo_t* pBMCInfo = &g_BMCInfo;//[BMCInst];

    if(IsLANChannel(ch, BMCInst))
    {
        if(pBMCInfo->IpmiConfig.LANIfcSupport != 1)
        {
            return NULL;
        }
    }/*else
   {
        if( PRIMARY_IPMB_CHANNEL == ch && pBMCInfo->IpmiConfig.PrimaryIPMBSupport != 1 )
        {
            return NULL;
        }
        else if (pBMCInfo->IpmiConfig.SecondaryIPMBSupport != 1 && (pBMCInfo->SecondaryIPMBCh != CH_NOT_USED && ch == pBMCInfo->SecondaryIPMBCh))
        {
            return NULL;
        }
        else if (pBMCInfo->IpmiConfig.SerialIfcSupport != 1 && (pBMCInfo->SERIALch != CH_NOT_USED && ch == pBMCInfo->SERIALch))
        {
            return NULL;
        }
        else if (pBMCInfo->IpmiConfig.ICMBIfcSupport != 1 && (pBMCInfo->ICMBCh != CH_NOT_USED && ch == pBMCInfo->ICMBCh))
        {
            return NULL;
        }
        else if (pBMCInfo->IpmiConfig.SMBUSIfcSupport !=1 && (pBMCInfo->SMBUSCh != CH_NOT_USED && ch == pBMCInfo->SMBUSCh))
        {
            return NULL;
        }
        else if (pBMCInfo->IpmiConfig.USBIfcSupport != 1 && (ch == USB_CHANNEL))
        {
            return NULL;
        }
        else if (pBMCInfo->IpmiConfig.SMMIfcSupport != 1 && (pBMCInfo->SMMCh != CH_NOT_USED && ch == pBMCInfo->SMMCh))
        {
            return NULL;
        }
        else if(pBMCInfo->IpmiConfig.SYSIfcSupport !=1 && ch == SYS_IFC_CHANNEL)
        {
            return NULL;
        }
    }
*/
    for(Index=0;Index<MAX_NUM_CHANNELS;Index++)
    {
       // if(pBMCInfo->ChConfig[Index].ChType != 0xff)
        {
            pChannelInfo = &pBMCInfo->ChConfig;//[Index];
            printf("Channel numb is %x %x \n",pChannelInfo->ChannelInfo.ChannelNumber,ch);
            if(pChannelInfo->ChannelInfo.ChannelNumber == ch)
            {
                return (ChannelInfo_T *)&pBMCInfo->ChConfig.ChannelInfo;//[Index].ChannelInfo;
            }
        }
    }
    printf("pChannelInfo->ChannelInfo.ChannelNumber != ch\n");
    return NULL;


}


_FAR_ UserInfo_T*	getUserIdInfo (INT8U UserId, int BMCInst)
{
    _FAR_ UserInfo_T* pUserTable = (UserInfo_T *) GetNVRUsrCfgAddr (NVRH_USERCONFIG, BMCInst);
    _FAR_ BMCInfo_t* pBMCInfo = &g_BMCInfo;//[BMCInst];

    if (UserId == 0) { return NULL; }

    if (UserId <= pBMCInfo->IpmiConfig.MaxUsers)
    {
        return &pUserTable[UserId-1];
    }
    else
    {
        return NULL;  
  }

}


INT8U CalculateCheckSum (INT8U* Data, INT16U Len)
{
    INT8U   Sum;
    INT16U  i;

    Sum = 0;
    for (i = 0; i < Len; i++)
    {
        Sum += Data [i];
    }
    Data[2]=(INT8U) (0x100 - Sum);
    return (0x100 - Sum);
}

INT8U CalculateCheckSum2 (_FAR_ INT8U* Pkt, INT32U Len)
{
	INT8U	Sum;
	INT32U	i;

	/* Get Checksum 2 */
	Sum = 0;
	for (i = 3; i < Len; i++)
	{
		Sum += Pkt [i];
	}
	Pkt[Len-1]=(INT8U)(0xFF & (0x100 - Sum));
	return (INT8U)(0xFF & (0x100 - Sum));
}

_FAR_ ChannelUserInfo_T* getChUserInfo (_NEAR_ char *userName,  _NEAR_ INT8U* chIndex, _FAR_ ChannelUserInfo_T *pChUserInfo,int BMCInst)
{
    _FAR_ UserInfo_T* pUserTable = (UserInfo_T *) GetNVRUsrCfgAddr (NVRH_USERCONFIG, BMCInst);
    _FAR_ BMCInfo_t* pBMCInfo = &g_BMCInfo;//[BMCInst];
    int userIndex;
    int i=0;
    printf("pBMCInfo->IpmiConfig.MaxUsers is %d:\n",pBMCInfo->IpmiConfig.MaxUsers);
    for(i=0;i<16;i++)
    printf("%#x  ",pUserTable[0].UserName[i]);
    printf("\n");
printf("pUserTable[0].UserId is %d ,pChUserInfo[0].UserId is %d\n",pUserTable[0].UserId,pChUserInfo[0].UserId);
    /* search the table */
    for(userIndex = 0; userIndex < pBMCInfo->IpmiConfig.MaxUsers; userIndex++)
    {
        for(*chIndex = 0; *chIndex < pBMCInfo->IpmiConfig.MaxChUsers; (*chIndex)++)
        {
            if ((0 == _fmemcmp(pUserTable[userIndex].UserName, userName, MAX_USERNAME_LEN)) &&
                /* Commented to return the pointer for disabled user */ 
                /* (1 == pUserTable[userIndex].UserStatus) && */
                (pUserTable[userIndex].UserId == pChUserInfo[*chIndex].UserId))
            return (pChUserInfo + *chIndex);
        }
    }
    printf("ChannelUserInfo_T is NULL\n");
    return NULL;
}


INT8U*
GetNVRUsrCfgAddr(INT32U NVRHandle, int BMCInst) 
{
    _FAR_ BMCInfo_t* pBMCInfo = &g_BMCInfo;//[BMCInst];

    return ((INT8U *)&pBMCInfo->UserInfo[NVRHandle]);
}

_FAR_ SessionInfo_T* getSessionInfo (INT8U Arg, _FAR_ void *Session,int BMCInst)
{
    INT8U				Index;
    INT8U				ActiveSesIndex = 0;
    _FAR_ BMCInfo_t* pBMCInfo = &g_BMCInfo;//[BMCInst];
    _FAR_	SessionTblInfo_T*	pSessionTblInfo = &pBMCInfo->SessionTblInfo;

    for (Index = 0; Index < pBMCInfo->IpmiConfig.MaxSession; Index++)
    {
        if (FALSE == pSessionTblInfo->SessionTbl[Index].Used)
        {
            continue;
        }
        if (TRUE == pSessionTblInfo->SessionTbl[Index].Activated)
        {
            ActiveSesIndex++;
        }

        switch (Arg)
        {
        case SESSION_ID_INFO:
	    printf("pSessionTblInfo->SessionTbl[Index].SessionID is %d,*((INT32U *)Session) is %d\n",pSessionTblInfo->SessionTbl[Index].SessionID,*((INT32U *)Session));
            if(pSessionTblInfo->SessionTbl[Index].SessionID == *((INT32U *)Session) )
            {
                return &pSessionTblInfo->SessionTbl[Index];
            }
            break;

        case SESSION_REMOTE_INFO:
//            if(pSessionTblInfo->SessionTbl[Index].RemConSessionID == *((INT32U *)Session) )
//            {
//                return &pSessionTblInfo->SessionTbl[Index];
//            }
            break;

        case SESSION_HANDLE_INFO:
            if (pSessionTblInfo->SessionTbl[Index].SessionHandle == *((_FAR_ INT8U*)Session) && pSessionTblInfo->SessionTbl[Index].Activated)
            {
                return &pSessionTblInfo->SessionTbl[Index];
            }
            break;

        case SESSION_INDEX_INFO:
            if (ActiveSesIndex == *((_FAR_ INT8U*)Session))
            {
                return &pSessionTblInfo->SessionTbl[Index];
            }
            break;

        case SESSION_CHANNEL_INFO:
            if (pSessionTblInfo->SessionTbl[Index].Channel == *((_FAR_ INT8U*)Session))
            {
                return &pSessionTblInfo->SessionTbl[Index];
            }
            break;

        default:
            return NULL;
        }
    }
    return NULL;
}




