#include <string.h>
#include "OemSMMCmds.h"
#include "com_BmcType.h"
#include "main.h"
#include "Storlead.h"
#include "SensorMonitor.h"
#include "Sensor.h"
#include "com_IPMI_SDRRecord.h"
#include "SDR.h"
#include "SELRecord.h"

int  Storlead_GetSysInfo(uint8_t* pReq, uint8_t ReqLen,  uint8_t* pRes)
{
	char Title[] = "系统信息";
	char Text[] = "模块名称:多端口光纤接口模块;FPGA型号:XC7VX690T;简介: ";
	int len = 0,remainLen = 0;
	uint16_t offset = (pReq[1]<<8) | pReq[2];
	int totalLen = 0;
	char *pStr = NULL;
	int reqLen = (pReq[3]<<8) | pReq[4];
	
	switch(pReq[0])
	{
	case 0:	//get Title
		while(Title[totalLen])
		{
			totalLen++;
		}printf("title total len = %#x\n",totalLen);
		pStr = Title;
		break;
	case 1:
		while(Text[totalLen])
		{
			totalLen++;
		}
		printf("text total len = %#x\n", totalLen);
		pStr = Text;
		break;
	default:
		*pRes = CC_PARAM_OUT_OF_RANGE;
		return 1;
	}
	
	*pRes= CC_NORMAL;

	if(offset > totalLen)
	{
		printf("Invalid offset: %#x\n", offset);
		*pRes= CC_PARAM_OUT_OF_RANGE;
		return 1;
	}
	
	if(reqLen == 0)	
	{
		len = totalLen - offset;
	}
	else
	{
		len = (reqLen > (totalLen-offset)) ? (totalLen-offset) : reqLen;
	}
	
	remainLen = totalLen - offset - len;
	*(pRes+1) = (len>>8)&0xff;
	*(pRes+2) = len&0xff;
	*(pRes+3) = (remainLen>>8)&0xff;
	*(pRes+4) = remainLen&0xff;
	memcpy(pRes+5, pStr+offset, len);

	// int i;
	// printf("Example 80h: ");
	// for(i=0;i<5+len;i++)
	// 	printf("%#x ", pRes[i]);
	// printf("\n");

	return 5+len;	
}

int  Storlead_GetSensorInfo(uint8_t* pReq, uint8_t ReqLen,  uint8_t* pRes)
{
	printf("Storlead_GetSensorInfo\n" );

	StorleadGetSensorInfoRes_T *pGetSensorInfoRes = (StorleadGetSensorInfoRes_T *)pRes;
    uint8_t*   		pValidSensor = NULL;
    uint16_t       	SensorIndex = pReq[0];
    SensorInfo_T    pSensor ;
    SenInfo_T       SensorInfo;
    uint8_t 		SensorIsSigned = FALSE;
    uint16_t 		SensorReading = 0;
    SensorSharedMem_T*    pSMSharedMem;
    SDRRecHdr_T*     	  pSDRRec;
    FullSensorRec_T*      FullSDR;
    CompactSensorRec_T*   CompSDR;
    int i = 0;
    int             totalsize;

    /* Get the Sensor Shared Memory */
    pSMSharedMem = (SensorSharedMem_T*)&g_BMCInfo.SensorSharedMem;

    if(g_BMCInfo.SenConfig.ValidSensorCnt == 0)
    {
        pGetSensorInfoRes->CompletionCode = OEMCC_SENSOR_INFO_EMPTY;
        printf("ValidSensorCnt is 0\n");
        return 1;
    }

    if(SensorIndex >= g_BMCInfo.SenConfig.ValidSensorCnt)
    {
        pGetSensorInfoRes->CompletionCode = CC_PARAM_OUT_OF_RANGE;
        printf("Invalid SenosrIndex %d\n", SensorIndex);
        return 1;
    }

    //pValidSensor = (uint8_t*)(pAMIGetSensorInfoRes+1);
    // for(i = 0; i < g_BMCInfo.SenConfig.ValidSensorCnt; i++)
    // {
        //SensorIndex = g_BMCInfo.SenConfig.ValidSensorList[i];
        pSensor = pSMSharedMem->SensorInfo[SensorIndex];

        /*Copy the SDR Header*/
        memcpy(&pGetSensorInfoRes->SensorInfo.hdr,pSensor.SDRRec,sizeof(SDRRecHdr_T));

        pGetSensorInfoRes->SensorInfo.SensorNumber = pSensor.SensorNumber;
        pGetSensorInfoRes->SensorInfo.SensorTypeCode = pSensor.SensorTypeCode;
        pGetSensorInfoRes->SensorInfo.EventTypeCode = pSensor.EventTypeCode;

        pGetSensorInfoRes->SensorInfo.Units1 = pSensor.Units1;
        pGetSensorInfoRes->SensorInfo.Units2 = pSensor.Units2;
        pGetSensorInfoRes->SensorInfo.Units3 = pSensor.Units3;

        pGetSensorInfoRes->SensorInfo.M_LSB = pSensor.M_LSB;
        pGetSensorInfoRes->SensorInfo.M_MSB_Tolerance = pSensor.M_MSB_Tolerance;
        pGetSensorInfoRes->SensorInfo.B_LSB = pSensor.B_LSB;
        pGetSensorInfoRes->SensorInfo.B_MSB_Accuracy = pSensor.B_MSB_Accuracy;
        pGetSensorInfoRes->SensorInfo.Accuracy_MSB_Exp = pSensor.Accuracy_MSB_Exp;
        pGetSensorInfoRes->SensorInfo.RExp_BExp = pSensor.RExp_BExp;

        pGetSensorInfoRes->SensorInfo.LowerNonCritical = pSensor.LowerNonCritical;
        pGetSensorInfoRes->SensorInfo.LowerCritical = pSensor.LowerCritical;
        pGetSensorInfoRes->SensorInfo.LowerNonRecoverable = pSensor.LowerNonRecoverable;
        pGetSensorInfoRes->SensorInfo.UpperNonCritical = pSensor.UpperNonCritical;
        pGetSensorInfoRes->SensorInfo.UpperCritical = pSensor.UpperCritical;
        pGetSensorInfoRes->SensorInfo.UpperNonRecoverable= pSensor.UpperNonRecoverable;
        pGetSensorInfoRes->SensorInfo.Settable_Readable_ThreshMask= pSensor.SettableThreshMask;

        pGetSensorInfoRes->SensorInfo.Flags = pSensor.EventFlags & 0xe0;
        if((pSensor.EventFlags & BIT5) != 0)
        {
            pGetSensorInfoRes->SensorInfo.SensorReading = 0;
        }

        SensorReading = pSensor.SensorReading;
        pGetSensorInfoRes->SensorInfo.SensorReading = 0;
        SensorIsSigned = ( 0 != (pSensor.InternalFlags & BIT1));

        if (THRESHOLD_SENSOR_CLASS  == pSensor.EventTypeCode)
        {
            pGetSensorInfoRes->SensorInfo.SensorReading = (SensorReading & 0x00FF);
            pGetSensorInfoRes->SensorInfo.ComparisonStatus = 0;
            if((pSensor.DeassertionEventEnablesByte2 & BIT6) == BIT6 )
            {
                if (CompareValues(SensorIsSigned, pGetSensorInfoRes->SensorInfo.SensorReading, pSensor.UpperNonRecoverable) >= 0)
                {
                    pGetSensorInfoRes->SensorInfo.ComparisonStatus |= BIT5;
                }
            }
            if((pSensor.DeassertionEventEnablesByte2 & BIT5) == BIT5 )
            {
                if (CompareValues(SensorIsSigned, pGetSensorInfoRes->SensorInfo.SensorReading, pSensor.UpperCritical) >= 0)
                {
                    pGetSensorInfoRes->SensorInfo.ComparisonStatus |= BIT4;
                }
            }
            if((pSensor.DeassertionEventEnablesByte2 & BIT4) == BIT4 )
            {
                if (CompareValues(SensorIsSigned, pGetSensorInfoRes->SensorInfo.SensorReading, pSensor.UpperNonCritical) >= 0)
                {
                    pGetSensorInfoRes->SensorInfo.ComparisonStatus |= BIT3;
                }
            }
            if((pSensor.AssertionEventEnablesByte2 & BIT6) == BIT6 )
            {
                if (CompareValues(SensorIsSigned, pGetSensorInfoRes->SensorInfo.SensorReading, pSensor.LowerNonRecoverable) <= 0)
                {
                    pGetSensorInfoRes->SensorInfo.ComparisonStatus |= BIT2;
                }
            }
            if((pSensor.AssertionEventEnablesByte2 & BIT5) == BIT5 )
            {
                if (CompareValues(SensorIsSigned, pGetSensorInfoRes->SensorInfo.SensorReading, pSensor.LowerCritical) <= 0)
                {
                    pGetSensorInfoRes->SensorInfo.ComparisonStatus |= BIT1;
                }
            }
            if((pSensor.AssertionEventEnablesByte2 & BIT4) == BIT4 )
            {
                if (CompareValues(SensorIsSigned, pGetSensorInfoRes->SensorInfo.SensorReading, pSensor.LowerNonCritical) <= 0)
                {
                    pGetSensorInfoRes->SensorInfo.ComparisonStatus |= BIT0;
                }
            }

            pGetSensorInfoRes->SensorInfo.ComparisonStatus &= ((pSensor.SettableThreshMask >>  8) & 0xFF);
            pGetSensorInfoRes->SensorInfo.OptionalStatus = 0;
            // For Threshold sensor, [7:6] - reserved. Returned as 1b. Ignore on read.
            pGetSensorInfoRes->SensorInfo.ComparisonStatus |= THRESHOLD_RESERVED_BIT;
        }
        else
        {
            pGetSensorInfoRes->SensorInfo.ComparisonStatus = (((uint8_t) (SensorReading & 0x00FF)) & ((uint8_t) (pSensor.SettableThreshMask & 0x00FF)) );
            pGetSensorInfoRes->SensorInfo.OptionalStatus   = (((uint8_t) (SensorReading >> 8)) & ((uint8_t) (pSensor.SettableThreshMask >> 8)) );
            // For Discrete sensor, [7] - reserved. Returned as 1b. Ignore on read.
            pGetSensorInfoRes->SensorInfo.OptionalStatus  |= DISCRETE_RESERVED_BIT;
        }

        if((pSensor.EventFlags & BIT7) == 0)
        {
            pGetSensorInfoRes->SensorInfo.AssertionEventByte1 = 0;
            pGetSensorInfoRes->SensorInfo.AssertionEventByte2 = 0;
            pGetSensorInfoRes->SensorInfo.DeassertionEventByte1 = 0;
            pGetSensorInfoRes->SensorInfo.DeassertionEventByte2 = 0;
        }
        if((pSensor.SensorCaps & BIT6) == 0)
        {
             pGetSensorInfoRes->SensorInfo.AssertionEventByte1    = (pSensor.AssertionHistoryByte1 & pSensor.AssertionEventEnablesByte1);
             pGetSensorInfoRes->SensorInfo.AssertionEventByte2    = (pSensor.AssertionHistoryByte2 & pSensor.AssertionEventEnablesByte2);
             pGetSensorInfoRes->SensorInfo.DeassertionEventByte1    = (pSensor.DeassertionHistoryByte1 & pSensor.DeassertionEventEnablesByte1);
             pGetSensorInfoRes->SensorInfo.DeassertionEventByte2    = (pSensor.DeassertionHistoryByte2 & pSensor.DeassertionEventEnablesByte2);
        }
        else
        {
             pGetSensorInfoRes->SensorInfo.AssertionEventByte1    = (pSensor.AssertionEventOccuredByte1 & pSensor.AssertionEventEnablesByte1);
             pGetSensorInfoRes->SensorInfo.AssertionEventByte2    = (pSensor.AssertionEventOccuredByte2 & pSensor.AssertionEventEnablesByte2);
             pGetSensorInfoRes->SensorInfo.DeassertionEventByte1    = (pSensor.DeassertionEventOccuredByte1 & pSensor.DeassertionEventEnablesByte1);
             pGetSensorInfoRes->SensorInfo.DeassertionEventByte2    = (pSensor.DeassertionEventOccuredByte2 & pSensor.DeassertionEventEnablesByte2);
        }
        pSDRRec = GetSDRRec(pSensor.SDRRec->ID);
        if(pSensor.SDRRec->Type  == FULL_SDR_REC)       /*Full SDR*/
        {
            FullSDR = (FullSensorRec_T *)pSDRRec;
            pGetSensorInfoRes->SensorInfo.OwnerID = FullSDR->OwnerID;
            pGetSensorInfoRes->SensorInfo.OwnerLUN= FullSDR->OwnerLUN;
            pGetSensorInfoRes->SensorInfo.MaxReading = FullSDR->MaxReading;
            pGetSensorInfoRes->SensorInfo.MinReading = FullSDR->MinReading;
            pGetSensorInfoRes->SensorInfo.Linearization = FullSDR->Linearization;
            memset(pGetSensorInfoRes->SensorInfo.SensorName,0,MAX_ID_STR_LEN);
            strncpy(pGetSensorInfoRes->SensorInfo.SensorName,FullSDR->IDStr, MAX_ID_STR_LEN - (sizeof(FullSensorRec_T) - sizeof(SDRRecHdr_T) - FullSDR->hdr.Len));
        }
        else if(pSensor.SDRRec->Type == COMPACT_SDR_REC)   /*Compact SDR*/
        {
            CompSDR = (CompactSensorRec_T *)pSDRRec;
            pGetSensorInfoRes->SensorInfo.OwnerID = CompSDR->OwnerID;
            pGetSensorInfoRes->SensorInfo.OwnerLUN= CompSDR->OwnerLUN;
            pGetSensorInfoRes->SensorInfo.MaxReading = 0;
            pGetSensorInfoRes->SensorInfo.MinReading = 0;
            pGetSensorInfoRes->SensorInfo.Linearization = 0;
            memset(pGetSensorInfoRes->SensorInfo.SensorName,0,MAX_ID_STR_LEN);
            strncpy(pGetSensorInfoRes->SensorInfo.SensorName,CompSDR->IDStr, MAX_ID_STR_LEN - (sizeof(CompactSensorRec_T) - sizeof(SDRRecHdr_T) - CompSDR->hdr.Len));
        }
        //memcpy(pValidSensor,(uint8_t *)&SensorInfo,sizeof(SenInfo_T));
        //pValidSensor = pValidSensor + sizeof(SenInfo_T);
    //}

    pGetSensorInfoRes->CompletionCode = CC_NORMAL;
    //pAMIGetSensorInfoRes->Noofentries = g_BMCInfo.SenConfig.ValidSensorCnt;

    //totalsize = sizeof(AMIGetSensorInfoRes_T) + (sizeof(SenInfo_T) * 11/*g_BMCInfo.SenConfig.ValidSensorCnt*/);
    // printf("return %d :", totalsize);
    // for(i=0;i<sizeof(AMIGetSensorInfoRes_T) + (sizeof(SenInfo_T) * g_BMCInfo.SenConfig.ValidSensorCnt);i++)
    // 	printf("%#x ", pRes[i]);
    // printf("\n");

    return sizeof(StorleadGetSensorInfoRes_T);
}


/*
*@fn AMIGetSELEntires
*@param This function retrieves the SEL entries
*@return Returns CC_NORMAL
*/
#define MAX_FULL_SEL_ENTRIES 900    //18*50=900， 50个SEL Entry， 每个18字节

int Storlead_GetSELEntires(uint8_t *pReq, uint8_t ReqLen, uint8_t *pRes)
{
    AMIGetSELEntriesReq_T *pAMIGetSelEntriesReq = (AMIGetSELEntriesReq_T *)pReq;
    AMIGetSELEntriesRes_T *pAMIGetSelEntiresRes = (AMIGetSELEntriesRes_T *)pRes;
    SELRepository_T *m_sel = NULL;
    uint16_t NumRecords = 0;

    
     m_sel = (SELRepository_T*)g_BMCInfo.pSEL;
     NumRecords = m_sel->NumRecords;

    if(((NumRecords - pAMIGetSelEntriesReq->Noofentretrieved) * sizeof(SELRec_T)) < MAX_FULL_SEL_ENTRIES)
    {
        pAMIGetSelEntiresRes->Status = FULL_SEL_ENTRIES;
        pAMIGetSelEntiresRes->Noofentries = NumRecords - pAMIGetSelEntriesReq->Noofentretrieved;
    }
    else if(NumRecords == SEL_EMPTY_REPOSITORY)
    {
        pAMIGetSelEntiresRes->CompletionCode = OEMCC_SEL_EMPTY_REPOSITORY;
        return sizeof(*pRes);
    }
    else if (NumRecords < pAMIGetSelEntriesReq->Noofentretrieved)
    {
        pAMIGetSelEntiresRes->CompletionCode = OEMCC_SEL_CLEARED;
        return sizeof(*pRes);
    }
    else
    {
        pAMIGetSelEntiresRes->Status = PARTIAL_SEL_ENTRIES;
        pAMIGetSelEntiresRes->Noofentries = ((MAX_FULL_SEL_ENTRIES - sizeof(AMIGetSELEntriesRes_T))/sizeof(SELRec_T));
    }


   
    memcpy((uint8_t*)(pAMIGetSelEntiresRes + 1),(uint8_t*)&m_sel->SELRecord[pAMIGetSelEntriesReq->Noofentretrieved],
              sizeof(SELRec_T)*pAMIGetSelEntiresRes->Noofentries);
  

    pAMIGetSelEntiresRes->LastRecID = m_sel->LastRecID;

    pAMIGetSelEntiresRes->CompletionCode = CC_NORMAL;

    return sizeof(AMIGetSELEntriesRes_T) + (sizeof(SELRec_T) * pAMIGetSelEntiresRes->Noofentries);
}

int Storlead_AuthorVerify(uint8_t *pReq, uint8_t ReqLen, uint8_t *pRes)
{
    AuthorVerify_T *pAuthorVerify = (AuthorVerify_T *)pReq;
    int i;

    printf("Storlead_AuthorVerify, username: %s, password: %s\n", pAuthorVerify->name, pAuthorVerify->password);
    for(i=0;i<MAX_USER_NUM;i++)
    {
        if(strcmp(g_BMCInfo.UserInfoTbl[i].UserName, pAuthorVerify->name) == 0)
        {
            if(strcmp(g_BMCInfo.UserInfoTbl[i].UserPassword, pAuthorVerify->password)==0)
            {
                *pRes = CC_NORMAL;
                return 1;
            }
            else
            {
                printf("Invalid password!\n");
                *pRes = CC_PASSWORD_TEST_FAILED;
                return 1;
            }
        }
    }

    printf("Invalid username!\n");
    *pRes = CC_PASSWORD_TEST_FAILED;
    return 1;
}

int Storlead_GetSensorHistory(uint8_t *pReq, uint8_t ReqLen, uint8_t *pRes)
{
    uint8_t *pSensorHistory = pRes+3;
    uint8_t  sensorNum = pReq[0];
    uint16_t offset = (pReq[1]<<8) | pReq[2];
    uint16_t len = (pReq[3]<<8) | pReq[4];
    uint16_t total_len = 480;
    *pRes= CC_NORMAL;
    
    if(offset >= total_len)
    {
        *pRes = CC_PARAM_OUT_OF_RANGE;
        return 1;
    }
    
    if(getSensorHistory(sensorNum, pSensorHistory) != 0)
    {
        *pRes = CC_PARAM_OUT_OF_RANGE;
        return 1;
    };
    
    
    if(len > (total_len - offset))
        len = total_len - offset;
    printf("len = %d, offset = %d, total_len = %d\n", len, offset, total_len);
    
    *(pRes+1) = (len>>8)&0xff;
    *(pRes+2) = len&0xff;
    
    return len+3;   
}

int Storlead_GetFanInfo(uint8_t *pReq, uint8_t ReqLen, uint8_t *pRes)
{
    *pRes = CC_NORMAL;
    memcpy(pRes+1, (uint8_t*)gFanInfo, FAN_NUMBERS*sizeof(FanInfo_T));
    return FAN_NUMBERS*sizeof(FanInfo_T) + 1;   
}

int Storlead_SetFanInfo(uint8_t *pReq, uint8_t ReqLen, uint8_t *pRes)
{
    uint8_t index = pReq[0];
    uint8_t mode = pReq[1];         //0: 自动， 1：手动
    uint8_t level = pReq[2];

    *pRes = CC_NORMAL;
    if(index >= FAN_NUMBERS)
    {
        printf("Invalid index %d\n", index);
        *pRes = CC_PARAM_OUT_OF_RANGE;
        return 1;
    }

    if(mode > 1)
    {
        printf("Invalid mode %d\n", mode);
        *pRes = CC_PARAM_OUT_OF_RANGE;
        return 1;
    }

    if(level > 100)
        level = 100;

    gFanInfo[index].mode = mode;

    if(mode == 1)
    {
        gFanInfo[index].level = level;
    }

    return 1;
}

/*
    Req[0]: ChassisManageEn
*/
int Storlead_getAllBladeStatus(uint8_t *pReq, uint8_t ReqLen, uint8_t *pRes)
{
    int i, index;
    uint8_t mReq[100] = {0};
    MsgPkt_T    ReqPkt;
    MsgPkt_T    ResPkt;
    pRes[0] = CC_NORMAL;

    //if(pReq[0] == 0)
    if(g_BMCInfo.isChMC == 0)
    {
        for(i=0;i<BLADE_NUMBERS;i++)
        {
            gBladeStatus[i].healthStatus = 0;
            sprintf(gBladeStatus[i].bladeName, "---");
            gBladeStatus[i].slotID = 0;
            gBladeStatus[i].pwrStatus = 0;
        }

        //当前模块的信息
        if(g_BMCInfo.IndexInChassis < BLADE_NUMBERS)
        {
            gBladeStatus[g_BMCInfo.IndexInChassis].healthStatus = 1;
            memcpy(gBladeStatus[g_BMCInfo.IndexInChassis].bladeName, g_BMCInfo.BladeName, 31);
            gBladeStatus[g_BMCInfo.IndexInChassis].slotID = g_BMCInfo.SlotID;
            gBladeStatus[g_BMCInfo.IndexInChassis].pwrStatus = g_BMCInfo.PowerGoodFlag;
        }
    }
    else
    {
        for(index=0;index<BLADE_NUMBERS;index++)
        {
            if(gChassisIPMBAddr[index] == 0x20) //chMC
            {
                gBladeStatus[index].healthStatus = 1;
                memcpy(gBladeStatus[index].bladeName, g_BMCInfo.BladeName, 31);
                gBladeStatus[index].slotID = g_BMCInfo.SlotID;
                gBladeStatus[index].pwrStatus = g_BMCInfo.PowerGoodFlag;
                continue;
            }

            ReqPkt.NetFnLUN  = NETFNLUN_IPMI_STORLEAD;
            ReqPkt.Cmd       = CMD_GET_BLADE_INFO;
            ReqPkt.Size      = 0;

            if(0 == API_BridgeInternal(&ReqPkt, &ResPkt, gChassisIPMBAddr[index], PRIMARY_IPMB_CHANNEL))
            {
                printf("RECV: ");
                for(i=0;i<ResPkt.Size;i++)
                    printf("%x ", ResPkt.Data[i]);
                printf("\n");

                if(ResPkt.Data[0] == 0)
                {
                    gBladeStatus[index].healthStatus = ResPkt.Data[1];
                    memcpy(gBladeStatus[index].bladeName, &ResPkt.Data[2], 31);
                    gBladeStatus[index].slotID = ResPkt.Data[33];
                    gBladeStatus[index].pwrStatus = ResPkt.Data[34];
                }
                else
                {
                    gBladeStatus[index].healthStatus = 0;
                    sprintf(gBladeStatus[index].bladeName, "---");
                    gBladeStatus[index].slotID = 0;
                    gBladeStatus[index].pwrStatus = 0;
                }
            }
            else
            {
                gBladeStatus[index].healthStatus = 0;
                sprintf(gBladeStatus[index].bladeName, "---");
                gBladeStatus[index].slotID = 0;
                gBladeStatus[index].pwrStatus = 0;
            }

            
        }

    }

    memcpy(pRes+1, gBladeStatus, (BLADE_NUMBERS*sizeof(BladeStatus_T)));
    return 1+(BLADE_NUMBERS*sizeof(BladeStatus_T));
}

int Storlead_GetChassisInfo(uint8_t *pReq, uint8_t ReqLen, uint8_t *pRes)
{
    /*
    typedef struct{
    uint8_t slotNum; //total slot numbers of chassis.
    uint8_t supportChassisManageFn;
    uint8_t chassisManageFnEnable;
    uint8_t curIndex;
} OemChassisInfo_T;
*/
    pRes[0] = CC_NORMAL;
    pRes[1] = BLADE_NUMBERS;
    pRes[2] = g_BMCInfo.isChMC;
    pRes[3] = 0;//g_BMCInfo.BladeManageEn;  //机箱管理使能，该字段废弃了。
    pRes[4] = g_BMCInfo.IndexInChassis;
    return 5;
}


int Storlead_RestoreFactorSettings(uint8_t *pReq, uint8_t ReqLen, uint8_t *pRes)
{
    int i;
    pRes[0] = CC_NORMAL;
    //擦除Flash   IpmiConfig
    sf_sector_erase(5, CONFIG_FLASH_START);

    return 1;
}

int Storlead_GetLanInfo(uint8_t *pReq, uint8_t ReqLen, uint8_t *pRes)
{
    pRes[0] = CC_NORMAL;
    uint8_t index = pReq[0];
    if(index > 0)
    {
        pRes[0] = CC_PARAM_OUT_OF_RANGE;
        return 1;
    }

    //Update LanInfo
    char ip_buf[16];
    getip(ip_buf);
    g_BMCInfo.LanInfo[index].IPAddr[0] = ip2dec(ip_buf, 0);
    g_BMCInfo.LanInfo[index].IPAddr[1] = ip2dec(ip_buf, 1);
    g_BMCInfo.LanInfo[index].IPAddr[2] = ip2dec(ip_buf, 2);
    g_BMCInfo.LanInfo[index].IPAddr[3] = ip2dec(ip_buf, 3);

    char mask_buf[16];
    getnetmask(mask_buf);
    g_BMCInfo.LanInfo[index].NetMask[0] = ip2dec(mask_buf, 0);
    g_BMCInfo.LanInfo[index].NetMask[1] = ip2dec(mask_buf, 1);
    g_BMCInfo.LanInfo[index].NetMask[2] = ip2dec(mask_buf, 2);
    g_BMCInfo.LanInfo[index].NetMask[3] = ip2dec(mask_buf, 3);

    char broadcast_buf[16];
    getbroadcast(broadcast_buf);
    g_BMCInfo.LanInfo[index].BroadCast[0] = ip2dec(broadcast_buf, 0);
    g_BMCInfo.LanInfo[index].BroadCast[1] = ip2dec(broadcast_buf, 1);
    g_BMCInfo.LanInfo[index].BroadCast[2] = ip2dec(broadcast_buf, 2);
    g_BMCInfo.LanInfo[index].BroadCast[3] = ip2dec(broadcast_buf, 3);

    char mac_buf[18];
    getmac(mac_buf);
    g_BMCInfo.LanInfo[index].MACAddr[0] = mac2hex(mac_buf, 0);
    g_BMCInfo.LanInfo[index].MACAddr[1] = mac2hex(mac_buf, 1);
    g_BMCInfo.LanInfo[index].MACAddr[2] = mac2hex(mac_buf, 2);
    g_BMCInfo.LanInfo[index].MACAddr[3] = mac2hex(mac_buf, 3);
    g_BMCInfo.LanInfo[index].MACAddr[4] = mac2hex(mac_buf, 4);
    g_BMCInfo.LanInfo[index].MACAddr[5] = mac2hex(mac_buf, 5);

    //TODO: get default GetWay

    memcpy(pRes+1, &g_BMCInfo.LanInfo[index], sizeof(LanInfo_T));

    return 1+sizeof(LanInfo_T);
}

int Storlead_SetLanInfo(uint8_t *pReq, uint8_t ReqLen, uint8_t *pRes)
{
    MsgPkt_T m_MsgPkt;
    uint8_t index = pReq[0];
    LanInfo_T *pLanInfo = (LanInfo_T*)&pReq[1];

    if(index > 0)   //只有一张网卡，index只能是0
    {
        pRes[0] = CC_PARAM_OUT_OF_RANGE;
        return 1;
    }

    memcpy(&g_BMCInfo.LanInfo[index], pLanInfo, sizeof(LanInfo_T));

    m_MsgPkt.Param      = PARAM_LAN;
    m_MsgPkt.NetFnLUN   = NETFNLUN_IPMI_STORLEAD;
    m_MsgPkt.Cmd        = CMD_SET_LAN_INFO;
    m_MsgPkt.Size       = sizeof(LanInfo_T);
    memcpy(m_MsgPkt.Data, pLanInfo, sizeof(LanInfo_T));
    PostMsg(gPendActionIfc, &m_MsgPkt);
    
    pRes[0] = CC_NORMAL;
    return 1;
}

/*
    res[0]: completeCode
    res[1]: healthStatus;   //0: 未知（灰色） 1：健康（绿色） 2：警告（黄色）3：错误（红色）
    res[2-22]: bladeName[31];  //10个汉字或30个字符
    res[23]: slotID;         
    res[24]: pwrStatus;      //0：未上电（灰色）     1：已上电（绿色）
*/
int Storlead_GetBladeInfo(uint8_t *pReq, uint8_t ReqLen, uint8_t *pRes)
{
    pRes[0] = CC_NORMAL;
    pRes[1] = g_BMCInfo.BladeHealthSta;
    memcpy(&pRes[2], g_BMCInfo.BladeName, 31);
    pRes[33] = g_BMCInfo.SlotID;
    pRes[34] = g_BMCInfo.PowerGoodFlag;

    return 35;
}