#include    "goahead.h"
#include    "libipmi_IPM.h"
#include    "com_IPMIDefs.h"
#include    "ResultUtils.h"
#include    "cJSON.h"
#include    "libipmi_storlead_OEM.h"
#include    "libsensor.h"
#include    <stdio.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include "libipmi_ChassisDevice.h"


/*控制远程开关机及复位*/
/* 
	192.168.0.207/action/?pwrAction=on
	192.168.0.207/action/?pwrAction=off
	192.168.0.207/action/?pwrAction=reset
	192.168.0.207/action/?pwrAction=cycle
	192.168.0.207/action/?pwrAction=diagnostic
	192.168.0.207/action/?pwrAction=soft
*/
void chassisPwrCtrl(Webs *wp){

	IPMI20_UDS_SESSION_T    UDSSession;
	char cmd = -1;
	char *pwrAction = NULL;
	int ret;
    uint8_t target_addr;
    uint16_t wRet;
	pwrAction = websGetVar(wp, "pwrAction", NULL);

    //wp->index = 1;

	if(strcmp(pwrAction, "on") == 0)
		cmd = 1;
	else if(strcmp(pwrAction, "off") == 0)
		cmd = 0;
	else if(strcmp(pwrAction, "reset") == 0)
		cmd = 3;
	else if(strcmp(pwrAction, "cycle") == 0)
		cmd = 2;
	else if(strcmp(pwrAction, "diagnostic")==0)
		cmd = 4;
	else if(strcmp(pwrAction, "soft") == 0)
		cmd = 5;

//	printf("Get pwrAction: %s, cmd: %d\n", pwrAction, cmd);

	
    cJSON * root =  cJSON_CreateObject();
    cJSON * data =  cJSON_CreateObject();
    cJSON_AddItemToObject(root, "data", data);//根节点下添加
    cJSON_AddStringToObject(root, "msg", "");
    cJSON_AddNumberToObject(root, "code", 200);
    

	if((cmd >= 0) && (cmd <= 5) )
	{
		//Create session
	    LIBIPMI_CreateSession(&UDSSession, DEFAULT_TIMEOUT);
        target_addr = gIPMBAddr[wp->index];
        if(target_addr == 0x20)
        {
	       wRet = IPMICMD_ChassisControl( &UDSSession, DEFAULT_TIMEOUT, cmd);
        }
        else
        {
            wRet = IPMC_ChassisControl( &UDSSession, target_addr, DEFAULT_TIMEOUT, cmd);
        }
	    //Close session
	    LIBIPMI_CloseSession(&UDSSession );

        if(wRet != 0)
            cJSON_AddStringToObject(data, "status", "fail");
        else    
	       cJSON_AddStringToObject(data, "status", "success");
	}
	else
	{
		printf("Invalid command %d\n", cmd);
		cJSON_AddStringToObject(data, "status", "fail");
	}

    char *pStr;
    pStr = cJSON_PrintUnformatted(root);
    websSetStatus(wp, 200);
    websWriteHeaders(wp, -1, 0);
    websWriteEndHeaders(wp);
    websWrite(wp, "%s", pStr);
    websFlush(wp, 0);	
    websDone(wp);
    //printf("cJSON:%s\n", pStr);

    if(pStr)
        wfree(pStr);
    if(root)
        cJSON_Delete(root);
}


/*控制远程开关机及复位*/
void getChassisStatus(Webs *wp)
{
	chassis_status_T chassisSta;
	IPMI20_UDS_SESSION_T    UDSSession;
    uint8_t target_addr;
    uint16_t wRet;
    char ptr[100];

	//Create session
    LIBIPMI_CreateSession(&UDSSession, DEFAULT_TIMEOUT);
    target_addr = gIPMBAddr[wp->index];
    if(target_addr == 0x20)
    {
        wRet = LIBIPMI_HL_GetChassisStatus(&UDSSession, DEFAULT_TIMEOUT, &chassisSta);
    }
    else
    {
        wRet = IPMC_GetChassisStatus(&UDSSession, target_addr, DEFAULT_TIMEOUT, &chassisSta);
    }
    //Close session
    LIBIPMI_CloseSession(&UDSSession );

    
    websSetStatus(wp, 200);
    websWriteHeaders(wp, -1, 0);
    websWriteEndHeaders(wp);
    if(chassisSta.power_state == 0)
        sprintf(ptr, "{\"data\":{\"powerStatus\":\"off\"},\"msg\":\"\",\"code\":200}");
    else
        sprintf(ptr, "{\"data\":{\"powerStatus\":\"on\"},\"msg\":\"\",\"code\":200}");
    websWrite(wp, "%s", ptr);
    websFlush(wp, 0);	
    websDone(wp);
    //printf("---> cJson:%s\n", ptr);
}