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

#include <string.h>
#include <stdio.h>
#include "IPMDevice.h"
#include "MsgHndlr.h"
#include "Support.h"
#include "com_IPMIDefs.h"
#include "com_IPMI_IPM.h"
#include "Sensor.h"
#include "main.h"
#include "WDT.h"
//#include "IPMIDef.h"
#include "ChassisTimerTask.h"
#include "com_IPMI_App.h"


#if IPM_DEVICE == 1

/*** Local macro definitions ***/
#define ACPI_SET_SYS_PWR_STATE          0x80
#define ACPI_SET_DEV_PWR_STATE          0x80
#define ACPI_SYS_PWR_STATE_MASK         0x7F
#define ACPI_DEV_PWR_STATE_MASK         0x7F
#define ACPI_MAX_SYS_PWR_STATE			16
#define ACPI_MAX_DEV_PWR_STATE			7
#define ACPI_FLAG_SET					1
#define ACPI_FLAG_UNSET					0


/*** Global Variables ***/

extern  WDTTmrMgr_T   g_WDTTmrMgr;



//To get the data across the processes added in Shared memory structure in SharedMem.h
// uint8_t     g_ACPISysPwrState;
// uint8_t     g_ACPIDevPwrState;

/*** Module Variables ***/

#define WORKING_STATE 0x0
#define S1_SLEEPING_STATE  0x1

//notice
static uint8_t  MfgID[]       = {0x12,0x34,0x56};//     /**< Contains Manufacturer ID */
static uint16_t g_ProdID      = 0xaabb;


static void GetFirmwareVersion(unsigned int* Major,unsigned int* Minor,unsigned int* Rev)
{
	*Major = g_BMCInfo.FwMajorVer;
	*Minor = g_BMCInfo.FwMinorVer;
	*Rev	= 0;
}

/*---------------------------------------
 * GetDevID
 *---------------------------------------*/
int
GetDevID ( uint8_t* pReq, uint8_t ReqLen,  uint8_t* pRes)
{
     GetDevIDRes_T*  pGetDevIDRes = ( GetDevIDRes_T*) pRes;
    static unsigned int Major,Minor,Rev;
    static unsigned char MinorBCD;
    static int firsttime = 1;

    if(firsttime == 1)
    {
        //	printf("Calling get firmware version only for first time\n");
        GetFirmwareVersion(&Major,&Minor,&Rev);
        MinorBCD = ((Minor/10)<<4)+(Minor%10);
        firsttime = 0;
    }

    pGetDevIDRes->CompletionCode      = CC_NORMAL;
    pGetDevIDRes->DeviceID            = DEVICE_ID;
    pGetDevIDRes->DevRevision         = IPMI_DEV_REVISION;
    pGetDevIDRes->FirmwareRevision1   = Major ;
    pGetDevIDRes->FirmwareRevision2   = MinorBCD;  //DO NOT CHANGE THIS ASSIGNMENT. INSTEAD SET THE VALUE OF THE VARIABLE Minor IN THE FIRSTTIME LOOP
    pGetDevIDRes->IPMIVersion         = IPMI_VERSION;
    pGetDevIDRes->DevSupport          = DEV_SUPPORT;
    pGetDevIDRes->ProdID              =  (g_ProdID);
    pGetDevIDRes->AuxFirmwareRevision = Rev;
    memcpy (pGetDevIDRes->MfgID, MfgID, sizeof (MfgID));

    return sizeof (GetDevIDRes_T);
}


/*---------------------------------------
 * ColdReset
 *---------------------------------------*/
int
ColdReset ( uint8_t* pReq, uint8_t ReqLen,  uint8_t* pRes)
{
	MsgPkt_T MsgPkt;
	MsgPkt.Param = PARAM_MC;
	MsgPkt.NetFnLUN = NETFN_APP<<2; 
	MsgPkt.Cmd = CMD_COLD_RESET;
	
	PostMsg(gPendActionIfc, &MsgPkt);
    *pRes = CC_NORMAL;
    return sizeof (*pRes);
}


/*---------------------------------------
 * WarmReset
 *---------------------------------------*/
int
WarmReset ( uint8_t* pReq, uint8_t ReqLen,  uint8_t* pRes)
{
	MsgPkt_T MsgPkt;
	MsgPkt.Param = PARAM_MC;
	MsgPkt.NetFnLUN = NETFN_APP<<2; 
	MsgPkt.Cmd = CMD_WARM_RESET;
	
	PostMsg(gPendActionIfc, &MsgPkt);
	*pRes = CC_NORMAL;
	return sizeof (*pRes);
}


/*---------------------------------------
 * GetSelfTestResults
 *---------------------------------------*/
int
GetSelfTestResults ( uint8_t* pReq, uint8_t ReqLen,  uint8_t* pRes)
{
     GetSelfTestRes_T*  pGetSelfTest = ( GetSelfTestRes_T*) pRes;

    pGetSelfTest->CompletionCode  = CC_NORMAL;
    pGetSelfTest->TestResultByte1 = ( 0 == g_BMCInfo.SelfTestByte ) ?
                                     GST_NO_ERROR: GST_CORRUPTED_DEVICES;
    pGetSelfTest->TestResultByte2 = g_BMCInfo.SelfTestByte;

    return sizeof (GetSelfTestRes_T);
}


/*---------------------------------------
 * MfgTestOn
 *---------------------------------------*/
int
MfgTestOn ( uint8_t* pReq, uint8_t ReqLen,  uint8_t* pRes)
{
	printf("MfgTestOn not implement\r\n");

    *pRes = CC_NORMAL;
    return sizeof (*pRes);
}


/*---------------------------------------
 * SetACPIPwrState
 *---------------------------------------*/
int
SetACPIPwrState ( uint8_t* pReq, uint8_t ReqLen,  uint8_t* pRes)
{
    printf("SetACPIPwrState not implement\r\n");

    *pRes = CC_NORMAL;
    return sizeof (*pRes);
}


/*---------------------------------------
 * GetACPIPwrState
 *---------------------------------------*/
int
GetACPIPwrState ( uint8_t* pReq, uint8_t ReqLen,  uint8_t* pRes)
{
    GetACPIPwrStateRes_T*  pGetACPIRes = ( GetACPIPwrStateRes_T*) pRes;

	printf("GetACPIPwrState not implement\r\n");
    pGetACPIRes->CompletionCode  = CC_NORMAL;
    pGetACPIRes->ACPISysPwrState = 0;//BMC_GET_SHARED_MEM (BMCInst)-> m_ACPISysPwrState;
    pGetACPIRes->ACPIDevPwrState = 0;//BMC_GET_SHARED_MEM (BMCInst)->m_ACPIDevPwrState;
  
    return sizeof (GetACPIPwrStateRes_T);
}


/*---------------------------------------
 * GetDevGUID
 *---------------------------------------*/
int
GetDevGUID ( uint8_t* pReq, uint8_t ReqLen,  uint8_t* pRes)
{
    GetDevGUIDRes_T*  pGetDevGUIDRes = ( GetDevGUIDRes_T*) pRes;

    pGetDevGUIDRes->CompletionCode  = CC_NORMAL;
    memcpy (pGetDevGUIDRes->Node, g_BMCInfo.DeviceGUID, 16);
  
    return sizeof (GetDevGUIDRes_T);
}

#endif  /* IPM_DEVICE */
