/********************************************************************************************************
 * @file	zcl_level.c
 *
 * @brief	This is the source file for zcl_level
 *
 * @author	Zigbee Group
 * @date	2019
 *
 * @par     Copyright (c) 2019, Telink Semiconductor (Shanghai) Co., Ltd. ("TELINK")
 *          All rights reserved.
 *
 *          Redistribution and use in source and binary forms, with or without
 *          modification, are permitted provided that the following conditions are met:
 *
 *              1. Redistributions of source code must retain the above copyright
 *              notice, this list of conditions and the following disclaimer.
 *
 *              2. Unless for usage inside a TELINK integrated circuit, redistributions
 *              in binary form must reproduce the above copyright notice, this list of
 *              conditions and the following disclaimer in the documentation and/or other
 *              materials provided with the distribution.
 *
 *              3. Neither the name of TELINK, nor the names of its contributors may be
 *              used to endorse or promote products derived from this software without
 *              specific prior written permission.
 *
 *              4. This software, with or without modification, must only be used with a
 *              TELINK integrated circuit. All other usages are subject to written permission
 *              from TELINK and different commercial license may apply.
 *
 *              5. Licensee shall be solely responsible for any claim to the extent arising out of or
 *              relating to such deletion(s), modification(s) or alteration(s).
 *
 *          THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 *          ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 *          WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 *          DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER BE LIABLE FOR ANY
 *          DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 *          (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 *          LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 *          ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *          (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 *          SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *******************************************************************************************************/
/**********************************************************************
 * INCLUDES
 */
#include "../zcl_include.h"


#ifdef ZCL_LEVEL_CTRL
/**********************************************************************
 * LOCAL CONSTANTS
 */


/**********************************************************************
 * LOCAL TYPES
 */


/**********************************************************************
 * LOCAL VARIABLES
 */


/**********************************************************************
 * LOCAL FUNCTIONS
 */
static void zcl_level_startUpCurrentLevel(u8 endpoint);
static status_t zcl_level_cmdHandler(zclIncoming_t *pInMsg);


_CODE_ZCL_ status_t zcl_level_register(u8 endpoint, u16 manuCode, u8 attrNum, const zclAttrInfo_t attrTbl[], cluster_forAppCb_t cb)
{
	u8 status = ZCL_STA_SUCCESS;

    status = zcl_registerCluster(endpoint, ZCL_CLUSTER_GEN_LEVEL_CONTROL, manuCode, attrNum, attrTbl, zcl_level_cmdHandler, cb);

    if(status == ZCL_STA_SUCCESS){
    	zcl_level_startUpCurrentLevel(endpoint);
    }

    return status;
}

_CODE_ZCL_ static void zcl_level_startUpCurrentLevel(u8 endpoint)
{
	u8 startUpCurrentLevel = 0;
	u8 preLevel = 0;
	u16 attrLen = 0;

	if( (zcl_getAttrVal(endpoint, ZCL_CLUSTER_GEN_LEVEL_CONTROL, ZCL_ATTRID_LEVEL_START_UP_CURRENT_LEVEL, &attrLen, (u8 *)&startUpCurrentLevel) == ZCL_STA_SUCCESS) &&
		(zcl_getAttrVal(endpoint, ZCL_CLUSTER_GEN_LEVEL_CONTROL, ZCL_ATTRID_LEVEL_CURRENT_LEVEL, &attrLen, (u8 *)&preLevel) == ZCL_STA_SUCCESS) ){
			u8 dstLevel = 0;

			if(startUpCurrentLevel == ZCL_START_UP_CURRENT_LEVEL_TO_MIN){
				dstLevel = ZCL_LEVEL_ATTR_MIN_LEVEL;
			}else if(startUpCurrentLevel == ZCL_START_UP_CURRENT_LEVEL_TO_PREVIOUS){
				dstLevel = preLevel;
			}else{
				dstLevel = startUpCurrentLevel;
			}

			if(dstLevel != preLevel){
				zcl_setAttrVal(endpoint, ZCL_CLUSTER_GEN_LEVEL_CONTROL, ZCL_ATTRID_LEVEL_CURRENT_LEVEL, (u8 *)&dstLevel);
			}
	}
}


_CODE_ZCL_ status_t zcl_level_move2level(u8 srcEp, epInfo_t *pDstEpInfo, u8 disableDefaultRsp, u8 seqNo, u8 cmdId, moveToLvl_t *pMove2Level)
{
	u8 buf[5];
	u8 *pBuf = buf;

	*pBuf++ = pMove2Level->level;
	*pBuf++ = LO_UINT16(pMove2Level->transitionTime);
	*pBuf++ = HI_UINT16(pMove2Level->transitionTime);
	if(pMove2Level->optPresent){
		*pBuf++ = pMove2Level->optionsMask;
		*pBuf++ = pMove2Level->optionsOverride;
	}

	return zcl_sendCmd(srcEp, pDstEpInfo, ZCL_CLUSTER_GEN_LEVEL_CONTROL, cmdId, TRUE,
					ZCL_FRAME_CLIENT_SERVER_DIR, disableDefaultRsp, 0, seqNo, pBuf - buf, buf);
}

_CODE_ZCL_ status_t zcl_level_move(u8 srcEp, epInfo_t *pDstEpInfo, u8 disableDefaultRsp, u8 seqNo, u8 cmdId, move_t *pMove)
{
	u8 buf[4];
	u8 *pBuf = buf;

	*pBuf++ = pMove->moveMode;
	*pBuf++ = pMove->rate;
	if(pMove->optPresent){
		*pBuf++ = pMove->optionsMask;
		*pBuf++ = pMove->optionsOverride;
	}

	return zcl_sendCmd(srcEp, pDstEpInfo, ZCL_CLUSTER_GEN_LEVEL_CONTROL, cmdId, TRUE,
					ZCL_FRAME_CLIENT_SERVER_DIR, disableDefaultRsp, 0, seqNo, pBuf - buf, buf);
}

_CODE_ZCL_ status_t zcl_level_step(u8 srcEp, epInfo_t *pDstEpInfo, u8 disableDefaultRsp, u8 seqNo, u8 cmdId, step_t *pStep)
{
	u8 buf[6];
	u8 *pBuf = buf;

	*pBuf++ = pStep->stepMode;
	*pBuf++ = pStep->stepSize;
	*pBuf++ = LO_UINT16(pStep->transitionTime);
	*pBuf++ = HI_UINT16(pStep->transitionTime);
	if(pStep->optPresent){
		*pBuf++ = pStep->optionsMask;
		*pBuf++ = pStep->optionsOverride;
	}

	return zcl_sendCmd(srcEp, pDstEpInfo, ZCL_CLUSTER_GEN_LEVEL_CONTROL, cmdId, TRUE,
					ZCL_FRAME_CLIENT_SERVER_DIR, disableDefaultRsp, 0, seqNo, pBuf - buf, buf);
}

_CODE_ZCL_ status_t zcl_level_stop(u8 srcEp, epInfo_t *pDstEpInfo, u8 disableDefaultRsp, u8 seqNo, u8 cmdId, stop_t *pStop)
{
	u8 buf[2];
	u8 *pBuf = buf;

	if(pStop->optPresent){
		*pBuf++ = pStop->optionsMask;
		*pBuf++ = pStop->optionsOverride;
	}

	return zcl_sendCmd(srcEp, pDstEpInfo, ZCL_CLUSTER_GEN_LEVEL_CONTROL, cmdId, TRUE,
					ZCL_FRAME_CLIENT_SERVER_DIR, disableDefaultRsp, 0, seqNo, pBuf - buf, buf);
}


_CODE_ZCL_ static status_t zcl_level_clientCmdHandler(zclIncoming_t *pInMsg)
{
	u8 status = ZCL_STA_SUCCESS;
	apsdeDataInd_t *pApsdeInd = (apsdeDataInd_t*)pInMsg->msg;
	u8 *pData = pInMsg->pData;

	zcl_level_cmdPayload_t cmdPayload;
	memset((u8 *)&cmdPayload, 0, sizeof(zcl_level_cmdPayload_t));

	switch(pInMsg->hdr.cmd)
	{
		case ZCL_CMD_LEVEL_MOVE_TO_LEVEL:
		case ZCL_CMD_LEVEL_MOVE_TO_LEVEL_WITH_ON_OFF:
			cmdPayload.moveToLevel.level = *pData++;
			cmdPayload.moveToLevel.transitionTime = BUILD_U16(pData[0], pData[1]);
			pData += 2;
			if(pInMsg->dataLen == 5){
				cmdPayload.moveToLevel.optionsMask = *pData++;
				cmdPayload.moveToLevel.optionsOverride = *pData++;
				cmdPayload.moveToLevel.optPresent = 1;
			}
			break;
		case ZCL_CMD_LEVEL_MOVE:
		case ZCL_CMD_LEVEL_MOVE_WITH_ON_OFF:
			cmdPayload.move.moveMode = *pData++;
			cmdPayload.move.rate = *pData++;
			if(pInMsg->dataLen == 4){
				cmdPayload.move.optionsMask = *pData++;
				cmdPayload.move.optionsOverride = *pData++;
				cmdPayload.move.optPresent = 1;
			}
			break;
		case ZCL_CMD_LEVEL_STEP:
		case ZCL_CMD_LEVEL_STEP_WITH_ON_OFF:
			cmdPayload.step.stepMode = *pData++;
			cmdPayload.step.stepSize = *pData++;
			cmdPayload.step.transitionTime = BUILD_U16(pData[0], pData[1]);
			pData += 2;
			if(pInMsg->dataLen == 6){
				cmdPayload.step.optionsMask = *pData++;
				cmdPayload.step.optionsOverride = *pData++;
				cmdPayload.step.optPresent = 1;
			}
			break;
		case ZCL_CMD_LEVEL_STOP:
		case ZCL_CMD_LEVEL_STOP_WITH_ON_OFF:
			if(pInMsg->dataLen == 2){
				cmdPayload.stop.optionsMask = *pData++;
				cmdPayload.stop.optionsOverride = *pData++;
				cmdPayload.stop.optPresent = 1;
			}
			break;
		default:
			status = ZCL_STA_UNSUP_CLUSTER_COMMAND;
			break;
	}

	if(status == ZCL_STA_SUCCESS){
		if(pInMsg->clusterAppCb){
			zclIncomingAddrInfo_t addrInfo;
			addrInfo.dirCluster = pInMsg->hdr.frmCtrl.bf.dir;
			addrInfo.profileId = pApsdeInd->indInfo.profile_id;
			addrInfo.srcAddr = pApsdeInd->indInfo.src_short_addr;
			addrInfo.dstAddr = pApsdeInd->indInfo.dst_addr;
			addrInfo.srcEp = pApsdeInd->indInfo.src_ep;
			addrInfo.dstEp = pApsdeInd->indInfo.dst_ep;

			pInMsg->clusterAppCb(&addrInfo, pInMsg->hdr.cmd, &cmdPayload);

#ifdef ZCL_SCENE
			u16 attrLen = 0;
			u8 sceneValid = 0;
			if(zcl_getAttrVal(pInMsg->msg->indInfo.dst_ep, ZCL_CLUSTER_GEN_SCENES, ZCL_ATTRID_SCENE_SCENE_VALID, &attrLen, (u8 *)&sceneValid) == ZCL_STA_SUCCESS){
				sceneValid = 0;
				zcl_setAttrVal(pInMsg->msg->indInfo.dst_ep, ZCL_CLUSTER_GEN_SCENES, ZCL_ATTRID_SCENE_SCENE_VALID, (u8 *)&sceneValid);
			}
#endif
		}
	}

	return status;
}

_CODE_ZCL_ static status_t zcl_level_cmdHandler(zclIncoming_t *pInMsg)
{
	if(pInMsg->hdr.frmCtrl.bf.dir == ZCL_FRAME_CLIENT_SERVER_DIR){
		return zcl_level_clientCmdHandler(pInMsg);
	}else{
		return ZCL_STA_UNSUP_CLUSTER_COMMAND;
	}
}

#endif  /* ZCL_LEVEL_CTRL */
