#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 	"libipmi_sdr.h"
#include 	"libipmi_sensor.h"
#include 	"com_BMCCfg.h"
#include    <stdio.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>

void getAllFanInfo(Webs *wp){
	FanInfo_T fanInfo[MAX_FAN_NUMS] = {0xff};
	uint16_t    wRet = LIBIPMI_E_SUCCESS;
    IPMI20_UDS_SESSION_T    UDSSession;
    int i;
    uint8_t target_addr;

    //将风扇的索引初始化为0xff，读到有效值后会被修改。
    for(i=0;i<MAX_FAN_NUMS;i++)
    {
        fanInfo[i].index = 0xff;
    }
	//Create session
    LIBIPMI_CreateSession(&UDSSession, DEFAULT_TIMEOUT);

    wp->index = TEST_INDEX;
    target_addr = gIPMBAddr[wp->index];
    if(target_addr == 0x20)
    {
    	wRet = LIBIPMI_HL_GetFanInfo( &UDSSession, fanInfo, DEFAULT_TIMEOUT);    	
    }
    else
    {
        wRet = IPMC_GetFanInfo( &UDSSession, target_addr, fanInfo, DEFAULT_TIMEOUT); 
    }

    if(wRet != 0)
    {
        websError(wp, 404, "getAllFanInfo error!");
    }

	//Close session
    LIBIPMI_CloseSession(&UDSSession );

	char *pStr;
    cJSON * root =  cJSON_CreateObject();
    cJSON * pJsonArry=cJSON_CreateArray();   /*创建数组*/
    cJSON * pJsonsub;
    cJSON_AddItemToObject(root, "data", pJsonArry);//根节点下添加
    cJSON_AddStringToObject(root, "msg", "");
    cJSON_AddNumberToObject(root, "code", 200);

    for(i=0;i<MAX_FAN_NUMS;i++)
    {
        if(fanInfo[i].index != 0xff)
        {
        	cJSON_AddItemToArray(pJsonArry,pJsonsub=cJSON_CreateObject()); /* 给创建的数组增加对对象*/
            cJSON_AddNumberToObject(pJsonsub, "index", fanInfo[i].index); 
            cJSON_AddStringToObject(pJsonsub, "name", fanInfo[i].name);
            cJSON_AddNumberToObject(pJsonsub, "mode", fanInfo[i].mode);			//0: 自动， 1：手动
            cJSON_AddNumberToObject(pJsonsub, "level", fanInfo[i].level);
            cJSON_AddNumberToObject(pJsonsub, "speed", fanInfo[i].speed);
        }
    }
    
   
    pStr = cJSON_PrintUnformatted(root);
    websSetStatus(wp, 200);
    websWriteHeaders(wp, -1, 0);
    websWriteEndHeaders(wp); 
    websWrite(wp,"%s", pStr);
    websFlush(wp, 0);
    websDone(wp);
    printf("---> cJSON Str:\n%s\n", pStr);

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

void setFanInfo(Webs *wp){
	uint16_t    wRet = LIBIPMI_E_SUCCESS;
    IPMI20_UDS_SESSION_T    UDSSession;
    int i;
    uint8_t target_addr;

    char *strIndex = websGetVar(wp, "index", NULL);
    char *strMode = websGetVar(wp, "mode", NULL);
    char *strLevel = websGetVar(wp, "level", NULL);

    uint8_t index = atoi(strIndex);
    uint8_t mode = atoi(strMode);
    uint8_t level = atoi(strLevel);

    if(index >= MAX_FAN_NUMS)
    {
    	websError(wp, 404, "Invalid index!");
    	return ;
    }

    if(mode > 1)
    {
		websError(wp, 404, "Invalid mode!");    	
		return;
    }

    if(level > 100)
    	level = 100;

	//Create session
    LIBIPMI_CreateSession(&UDSSession, DEFAULT_TIMEOUT);

    wp->index = TEST_INDEX;
    target_addr = gIPMBAddr[wp->index];
    if(target_addr == 0x20)
    {
	   wRet = LIBIPMI_HL_SetFanInfo( &UDSSession, index, mode, level, DEFAULT_TIMEOUT);
    }
    else
    {
        wRet = IPMC_SetFanInfo( &UDSSession, target_addr, index, mode, level, DEFAULT_TIMEOUT);
    }

	if(wRet != 0)
	{
		websError(wp, 404, "getAllFanInfo error!");
	}

	//Close session
    LIBIPMI_CloseSession(&UDSSession );

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

    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 Str:\n%s\n", pStr);

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