/**********************************************************************
 * INCLUDES
 */
#include "ikk-cluster.h"
#include "ikk-debug.h"
#include "../ikk-config.h"
#include "../../sampleLight.h"
#include "zcl.h"

static uint8_t g_tmp_buffer[64] = { 0 };

pOnOffClusterOnOffStatusCallback g_pfOnOffStatusCallback = NULL;


static status_t WriteServerAttribute(u8 endpoint, u16 clusterId,uint8_t attributeID,uint8_t* dataPtr, uint8_t data_type)
{
	zclWriteRec_t  attr_data;
	attr_data.attrID 	= attributeID;
	attr_data.dataType 	= data_type;
	u16 dataLen = zcl_getAttrSize(data_type, dataPtr);
	memcpy(attr_data.attrData,dataPtr,dataLen);

	iKonkeAfSelfPrint("##WriteServerAttribute attr =%x,attr = %x, :\r\n",clusterId,attr_data.attrID);
	iKonkeAfSelfPrint("##DataBuffer len = %d,data=%x-%x:\r\n",dataLen,attr_data.attrData[1],attr_data.attrData[2]);
	iKonkeAfSelfPrintBuffer(attr_data.attrData,attr_data.attrData[0]+1);
	return zcl_attrWrite(endpoint,clusterId,&attr_data,false);
}


static void  Rewrite_Modeid(uint8_t *modeid,uint8_t software,uint8_t hardware)
{
	uint8_t      Modeid_Temp[] = "3AFE101003008611" ;
	uint8_t      temp[6];
	uint8_t      dest_temp[20];
	temp[0]     = software;
	temp[1]     = hardware;
	temp[2]     = modeid[3];
	temp[3]     = modeid[2];
	temp[4]     = modeid[1];
	temp[5]     = modeid[0];
	iKonkeAfSelfPrintBuffer(temp,6);

	for(uint8_t i = 0;i<6;i++)
	{
		uint8_t high_byte = (temp[i]>>4)&0x0f;
		uint8_t low_byte  = temp[i]&0x0f;
		if(  ( high_byte >= 0)&&( high_byte <= 9) )
		{
			dest_temp[2*i] = high_byte +'0';
		}
		else
		{
			dest_temp[2*i] = high_byte -10 +'A';
		}

		if(  (low_byte >= 0)&&(low_byte <= 9) )
		{
			dest_temp[2*i +1] = low_byte +'0';
		}
		else
		{
			dest_temp[2*i +1] = low_byte -10 +'A';
		}
	}
	memcpy(&Modeid_Temp[4],dest_temp,12);
	memmove(&g_tmp_buffer[1],Modeid_Temp,16);										//ÖØÐ´³õÊ¼»¯Öµ
	g_tmp_buffer[0] = 16;
	g_tmp_buffer[0+ 2+16] = '0';

	u8 status = WriteServerAttribute(1, ZCL_CLUSTER_GEN_BASIC, ZCL_ATTRID_BASIC_MODEL_ID ,(uint8_t*)g_tmp_buffer,ZCL_DATA_TYPE_CHAR_STR);
	iKonkeAfSelfPrint("write modeid status = %d :\r\n",status);
	status = WriteServerAttribute(1, ZCL_CLUSTER_GEN_BASIC, ZCL_ATTRID_BASIC_APP_VER ,(uint8_t*)&software,ZCL_DATA_TYPE_UINT8);
	iKonkeAfSelfPrint("write app version status = %d :\r\n",status);
	status = WriteServerAttribute(1, ZCL_CLUSTER_GEN_BASIC, ZCL_ATTRID_BASIC_HW_VER ,(uint8_t*)&hardware,ZCL_DATA_TYPE_UINT8);
	iKonkeAfSelfPrint("write hard version status = %d :\r\n",status);
	iKonkeAfSelfPrint("Finish Rewrite Modeid :\r\n");
	iKonkeAfSelfPrintBuffer(g_tmp_buffer,g_tmp_buffer[0]+1);
}
static void Rewrite_ManufactureName(void)
{
	uint8_t manuName[] = MANUFATURE_NAME;
	g_tmp_buffer[0] = strlen((const char *)manuName);
	strcpy((char*)&g_tmp_buffer[1],(const char*)manuName);
	iKonkeAfSelfPrint("Rewrite_ManufactureName :\r\n");
	iKonkeAfSelfPrintBuffer(g_tmp_buffer,g_tmp_buffer[0]+1);
	u8 status =WriteServerAttribute(1, ZCL_CLUSTER_GEN_BASIC, ZCL_ATTRID_BASIC_MFR_NAME ,(uint8_t*)g_tmp_buffer,ZCL_DATA_TYPE_CHAR_STR);
	iKonkeAfSelfPrint("write mfg name status = %d :\r\n",status);
}
static void Read_ManufactureName(void)
{
	u8 status;
	zclAttrInfo_t *data = zcl_attrRead(1,ZCL_CLUSTER_GEN_BASIC,ZCL_ATTRID_BASIC_MFR_NAME,&status);
	iKonkeAfSelfPrint("Read_ManufactureName :\r\n");
	iKonkeAfSelfPrintBuffer(data->data,zcl_getAttrSize(data->type, data->data));
}
void Update_Local_Attribute_Info(void)
{
	uint32_t modeid = MODEL_ID;
	Rewrite_Modeid((uint8_t *)&modeid,FIRMWARE_VER,HARDWARE_VERSION);
	Rewrite_ManufactureName();
	Read_ManufactureName();


}





























kk_err_t kZclOnOffClusterServerInit(pOnOffClusterOnOffStatusCallback pOnOffStatusCallback )
{
	g_pfOnOffStatusCallback = pOnOffStatusCallback;

	return KET_OK;
}

void ON_OFF_Cmd_Handler(uint8_t endpoint,uint8_t cmd)
{
	zcl_onOffAttr_t *pOnOff = zcl_onoffAttrGet();
	OnOffStatusEnum on_off_ctr;
	if(cmd == ZCL_CMD_ONOFF_ON){
		pOnOff->globalSceneControl = TRUE;

		pOnOff->onOff = ZCL_ONOFF_STATUS_ON;
		on_off_ctr = EOOS_ON;
		if(pOnOff->onTime == 0){
			pOnOff->offWaitTime = 0;
		}
	}else if(cmd == ZCL_CMD_ONOFF_OFF){
		pOnOff->onOff = ZCL_ONOFF_STATUS_OFF;
		pOnOff->onTime = 0;
		on_off_ctr = EOOS_OFF;
	}else{
		if(pOnOff->onOff == ZCL_ONOFF_STATUS_OFF){
			pOnOff->globalSceneControl = TRUE;

			pOnOff->onOff = ZCL_ONOFF_STATUS_ON;
			on_off_ctr = EOOS_ON;
			if(pOnOff->onTime == 0){
				pOnOff->offWaitTime = 0;
			}
		}else{
			pOnOff->onOff = ZCL_ONOFF_STATUS_OFF;
			pOnOff->onTime = 0;
			on_off_ctr = EOOS_OFF;
		}
	}

	g_pfOnOffStatusCallback(endpoint,on_off_ctr);
}

