2022-04-01 16:59:22 +00:00
|
|
|
/********************************************************************************************************
|
|
|
|
* @file zcl_levelCb.c
|
|
|
|
*
|
|
|
|
* @brief This is the source file for zcl_levelCb
|
|
|
|
*
|
|
|
|
* @author Zigbee Group
|
|
|
|
* @date 2021
|
|
|
|
*
|
|
|
|
* @par Copyright (c) 2021, Telink Semiconductor (Shanghai) Co., Ltd. ("TELINK")
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*******************************************************************************************************/
|
|
|
|
|
|
|
|
#if (__PROJECT_TL_DIMMABLE_LIGHT__)
|
|
|
|
/**********************************************************************
|
|
|
|
* INCLUDES
|
|
|
|
*/
|
|
|
|
#include "tl_common.h"
|
|
|
|
#include "zb_api.h"
|
|
|
|
#include "zcl_include.h"
|
|
|
|
#include "sampleLight.h"
|
|
|
|
#include "sampleLightCtrl.h"
|
|
|
|
|
|
|
|
#ifdef ZCL_LEVEL_CTRL
|
|
|
|
|
|
|
|
/**********************************************************************
|
|
|
|
* TYPEDEFS
|
|
|
|
*/
|
2022-04-01 17:14:59 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
2022-04-01 16:59:22 +00:00
|
|
|
s32 stepLevel256;
|
2022-04-01 17:14:59 +00:00
|
|
|
u16 currentLevel256;
|
|
|
|
u8 withOnOff;
|
|
|
|
} zcl_levelInfo_t;
|
2022-04-01 16:59:22 +00:00
|
|
|
|
|
|
|
/**********************************************************************
|
|
|
|
* LOCAL VARIABLES
|
|
|
|
*/
|
|
|
|
static zcl_levelInfo_t levelInfo = {
|
2022-04-01 17:14:59 +00:00
|
|
|
.stepLevel256 = 0,
|
|
|
|
.currentLevel256 = 0,
|
|
|
|
.withOnOff = 0,
|
2022-04-01 16:59:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static ev_timer_event_t *levelTimerEvt = NULL;
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* @fn sampleLight_levelTimerEvtCb
|
|
|
|
*
|
|
|
|
* @brief timer event to process the level command
|
|
|
|
*
|
|
|
|
* @param arg
|
|
|
|
*
|
|
|
|
* @return 0: timer continue on; -1: timer will be canceled
|
|
|
|
*/
|
2022-04-01 17:14:59 +00:00
|
|
|
static s32 sampleLight_levelTimerEvtCb(void *arg)
|
2022-04-01 16:59:22 +00:00
|
|
|
{
|
|
|
|
zcl_levelAttr_t *pLevel = zcl_levelAttrGet();
|
|
|
|
|
2022-04-01 17:14:59 +00:00
|
|
|
if (pLevel->remainingTime)
|
|
|
|
{
|
2022-04-01 16:59:22 +00:00
|
|
|
light_applyUpdate(&pLevel->curLevel, &levelInfo.currentLevel256, &levelInfo.stepLevel256, &pLevel->remainingTime,
|
2022-04-01 17:14:59 +00:00
|
|
|
ZCL_LEVEL_ATTR_MIN_LEVEL, ZCL_LEVEL_ATTR_MAX_LEVEL, FALSE);
|
2022-04-01 16:59:22 +00:00
|
|
|
}
|
|
|
|
|
2022-04-01 17:14:59 +00:00
|
|
|
if (levelInfo.withOnOff)
|
|
|
|
{
|
|
|
|
if (pLevel->curLevel == ZCL_LEVEL_ATTR_MIN_LEVEL)
|
|
|
|
{
|
2022-04-01 16:59:22 +00:00
|
|
|
sampleLight_onoff(ZCL_CMD_ONOFF_OFF);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-01 17:14:59 +00:00
|
|
|
if (pLevel->remainingTime)
|
|
|
|
{
|
2022-04-01 16:59:22 +00:00
|
|
|
return 0;
|
2022-04-01 17:14:59 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-04-01 16:59:22 +00:00
|
|
|
levelTimerEvt = NULL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* @fn sampleLight_LevelTimerStop
|
|
|
|
*
|
|
|
|
* @brief force to stop the level timer
|
|
|
|
*
|
|
|
|
* @param
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
static void sampleLight_LevelTimerStop(void)
|
|
|
|
{
|
2022-04-01 17:14:59 +00:00
|
|
|
if (levelTimerEvt)
|
|
|
|
{
|
2022-04-01 16:59:22 +00:00
|
|
|
TL_ZB_TIMER_CANCEL(&levelTimerEvt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* @fn sampleLight_moveToLevelProcess
|
|
|
|
*
|
|
|
|
* @brief
|
|
|
|
*
|
|
|
|
* @param cmdId
|
|
|
|
* @param cmd
|
|
|
|
*
|
|
|
|
* @return None
|
|
|
|
*/
|
|
|
|
static void sampleLight_moveToLevelProcess(u8 cmdId, moveToLvl_t *cmd)
|
|
|
|
{
|
|
|
|
zcl_levelAttr_t *pLevel = zcl_levelAttrGet();
|
|
|
|
|
2022-04-07 17:22:16 +00:00
|
|
|
pLevel->remainingTime = ((cmd->transitionTime == 0) || (cmd->transitionTime == 0xFFFF)) ? 1 : INTERP_STEPS_FROM_ONE_TENTH(cmd->transitionTime, ZCL_LEVEL_CHANGE_INTERVAL);
|
2022-04-01 16:59:22 +00:00
|
|
|
|
|
|
|
levelInfo.withOnOff = (cmdId == ZCL_CMD_LEVEL_MOVE_TO_LEVEL_WITH_ON_OFF) ? TRUE : FALSE;
|
|
|
|
levelInfo.currentLevel256 = (u16)(pLevel->curLevel) << 8;
|
|
|
|
levelInfo.stepLevel256 = ((s32)(cmd->level - pLevel->curLevel)) << 8;
|
|
|
|
levelInfo.stepLevel256 /= (s32)pLevel->remainingTime;
|
|
|
|
|
|
|
|
light_applyUpdate(&pLevel->curLevel, &levelInfo.currentLevel256, &levelInfo.stepLevel256, &pLevel->remainingTime,
|
2022-04-01 17:14:59 +00:00
|
|
|
ZCL_LEVEL_ATTR_MIN_LEVEL, ZCL_LEVEL_ATTR_MAX_LEVEL, FALSE);
|
2022-04-01 16:59:22 +00:00
|
|
|
|
2022-04-01 17:14:59 +00:00
|
|
|
if (levelInfo.withOnOff)
|
|
|
|
{
|
|
|
|
if (levelInfo.stepLevel256 > 0)
|
|
|
|
{
|
2022-04-01 16:59:22 +00:00
|
|
|
sampleLight_onoff(ZCL_CMD_ONOFF_ON);
|
2022-04-01 17:14:59 +00:00
|
|
|
}
|
|
|
|
else if (pLevel->curLevel == ZCL_LEVEL_ATTR_MIN_LEVEL)
|
|
|
|
{
|
2022-04-01 16:59:22 +00:00
|
|
|
sampleLight_onoff(ZCL_CMD_ONOFF_OFF);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-04 16:08:18 +00:00
|
|
|
sampleLight_LevelTimerStop();
|
2022-04-01 17:14:59 +00:00
|
|
|
if (pLevel->remainingTime)
|
|
|
|
{
|
2022-04-01 16:59:22 +00:00
|
|
|
levelTimerEvt = TL_ZB_TIMER_SCHEDULE(sampleLight_levelTimerEvtCb, NULL, ZCL_LEVEL_CHANGE_INTERVAL);
|
2022-04-01 17:14:59 +00:00
|
|
|
}
|
2022-04-01 16:59:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* @fn sampleLight_moveProcess
|
|
|
|
*
|
|
|
|
* @brief
|
|
|
|
*
|
|
|
|
* @param cmdId
|
|
|
|
* @param cmd
|
|
|
|
*
|
|
|
|
* @return None
|
|
|
|
*/
|
|
|
|
static void sampleLight_moveProcess(u8 cmdId, move_t *cmd)
|
|
|
|
{
|
|
|
|
zcl_levelAttr_t *pLevel = zcl_levelAttrGet();
|
|
|
|
|
|
|
|
levelInfo.withOnOff = (cmdId == ZCL_CMD_LEVEL_MOVE_WITH_ON_OFF) ? TRUE : FALSE;
|
|
|
|
levelInfo.currentLevel256 = (u16)(pLevel->curLevel) << 8;
|
|
|
|
|
|
|
|
u32 rate = (u32)cmd->rate * 100;
|
|
|
|
u8 newLevel;
|
|
|
|
u8 deltaLevel;
|
2022-04-01 17:14:59 +00:00
|
|
|
if (cmd->moveMode == LEVEL_MOVE_UP)
|
|
|
|
{
|
2022-04-01 16:59:22 +00:00
|
|
|
newLevel = ZCL_LEVEL_ATTR_MAX_LEVEL;
|
|
|
|
deltaLevel = ZCL_LEVEL_ATTR_MAX_LEVEL - pLevel->curLevel;
|
2022-04-01 17:14:59 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-04-01 16:59:22 +00:00
|
|
|
newLevel = ZCL_LEVEL_ATTR_MIN_LEVEL;
|
|
|
|
deltaLevel = pLevel->curLevel - ZCL_LEVEL_ATTR_MIN_LEVEL;
|
|
|
|
}
|
|
|
|
pLevel->remainingTime = ((u32)deltaLevel * 1000) / rate;
|
2022-04-01 17:14:59 +00:00
|
|
|
if (pLevel->remainingTime == 0)
|
|
|
|
{
|
2022-04-01 16:59:22 +00:00
|
|
|
pLevel->remainingTime = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
levelInfo.stepLevel256 = ((s32)(newLevel - pLevel->curLevel)) << 8;
|
|
|
|
levelInfo.stepLevel256 /= (s32)pLevel->remainingTime;
|
|
|
|
|
2022-04-01 17:14:59 +00:00
|
|
|
if (cmd->moveMode == LEVEL_MOVE_UP)
|
|
|
|
{
|
|
|
|
if (levelInfo.withOnOff)
|
|
|
|
{
|
2022-04-01 16:59:22 +00:00
|
|
|
sampleLight_onoff(ZCL_CMD_ONOFF_ON);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
light_applyUpdate(&pLevel->curLevel, &levelInfo.currentLevel256, &levelInfo.stepLevel256, &pLevel->remainingTime,
|
2022-04-01 17:14:59 +00:00
|
|
|
ZCL_LEVEL_ATTR_MIN_LEVEL, ZCL_LEVEL_ATTR_MAX_LEVEL, FALSE);
|
2022-04-01 16:59:22 +00:00
|
|
|
|
2022-04-01 17:14:59 +00:00
|
|
|
if (levelInfo.withOnOff)
|
|
|
|
{
|
|
|
|
if (pLevel->curLevel == ZCL_LEVEL_ATTR_MIN_LEVEL)
|
|
|
|
{
|
2022-04-01 16:59:22 +00:00
|
|
|
sampleLight_onoff(ZCL_CMD_ONOFF_OFF);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-04 16:08:18 +00:00
|
|
|
sampleLight_LevelTimerStop();
|
2022-04-01 17:14:59 +00:00
|
|
|
if (pLevel->remainingTime)
|
|
|
|
{
|
2022-04-01 16:59:22 +00:00
|
|
|
levelTimerEvt = TL_ZB_TIMER_SCHEDULE(sampleLight_levelTimerEvtCb, NULL, ZCL_LEVEL_CHANGE_INTERVAL);
|
2022-04-01 17:14:59 +00:00
|
|
|
}
|
2022-04-01 16:59:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* @fn sampleLight_stepProcess
|
|
|
|
*
|
|
|
|
* @brief
|
|
|
|
*
|
|
|
|
* @param cmdId
|
|
|
|
* @param cmd
|
|
|
|
*
|
|
|
|
* @return None
|
|
|
|
*/
|
|
|
|
static void sampleLight_stepProcess(u8 cmdId, step_t *cmd)
|
|
|
|
{
|
|
|
|
zcl_levelAttr_t *pLevel = zcl_levelAttrGet();
|
|
|
|
|
2022-04-07 17:22:16 +00:00
|
|
|
pLevel->remainingTime = ((cmd->transitionTime == 0) || (cmd->transitionTime == 0xFFFF)) ? 1 : INTERP_STEPS_FROM_ONE_TENTH(cmd->transitionTime, ZCL_LEVEL_CHANGE_INTERVAL);
|
2022-04-01 16:59:22 +00:00
|
|
|
|
|
|
|
levelInfo.withOnOff = (cmdId == ZCL_CMD_LEVEL_STEP_WITH_ON_OFF) ? TRUE : FALSE;
|
|
|
|
levelInfo.currentLevel256 = (u16)(pLevel->curLevel) << 8;
|
|
|
|
levelInfo.stepLevel256 = (((s32)cmd->stepSize) << 8) / pLevel->remainingTime;
|
|
|
|
|
2022-04-01 17:14:59 +00:00
|
|
|
if (cmd->stepMode == LEVEL_STEP_UP)
|
|
|
|
{
|
|
|
|
if (levelInfo.withOnOff)
|
|
|
|
{
|
2022-04-01 16:59:22 +00:00
|
|
|
sampleLight_onoff(ZCL_CMD_ONOFF_ON);
|
|
|
|
}
|
2022-04-01 17:14:59 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-04-01 16:59:22 +00:00
|
|
|
levelInfo.stepLevel256 = -levelInfo.stepLevel256;
|
|
|
|
}
|
|
|
|
|
|
|
|
light_applyUpdate(&pLevel->curLevel, &levelInfo.currentLevel256, &levelInfo.stepLevel256, &pLevel->remainingTime,
|
2022-04-01 17:14:59 +00:00
|
|
|
ZCL_LEVEL_ATTR_MIN_LEVEL, ZCL_LEVEL_ATTR_MAX_LEVEL, FALSE);
|
2022-04-01 16:59:22 +00:00
|
|
|
|
2022-04-01 17:14:59 +00:00
|
|
|
if (levelInfo.withOnOff)
|
|
|
|
{
|
|
|
|
if (pLevel->curLevel == ZCL_LEVEL_ATTR_MIN_LEVEL)
|
|
|
|
{
|
2022-04-01 16:59:22 +00:00
|
|
|
sampleLight_onoff(ZCL_CMD_ONOFF_OFF);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-04 16:08:18 +00:00
|
|
|
sampleLight_LevelTimerStop();
|
2022-04-01 17:14:59 +00:00
|
|
|
if (pLevel->remainingTime)
|
|
|
|
{
|
2022-04-01 16:59:22 +00:00
|
|
|
levelTimerEvt = TL_ZB_TIMER_SCHEDULE(sampleLight_levelTimerEvtCb, NULL, ZCL_LEVEL_CHANGE_INTERVAL);
|
2022-04-01 17:14:59 +00:00
|
|
|
}
|
2022-04-01 16:59:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* @fn sampleLight_stopProcess
|
|
|
|
*
|
|
|
|
* @brief
|
|
|
|
*
|
|
|
|
* @param cmdId
|
|
|
|
* @param cmd
|
|
|
|
*
|
|
|
|
* @return None
|
|
|
|
*/
|
|
|
|
static void sampleLight_stopProcess(u8 cmdId, stop_t *cmd)
|
|
|
|
{
|
|
|
|
zcl_levelAttr_t *pLevel = zcl_levelAttrGet();
|
|
|
|
|
|
|
|
sampleLight_LevelTimerStop();
|
|
|
|
pLevel->remainingTime = 0;
|
|
|
|
|
|
|
|
levelInfo.currentLevel256 = ((s32)pLevel->curLevel) << 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* @fn sampleLight_levelCb
|
|
|
|
*
|
|
|
|
* @brief Handler for ZCL LEVEL command. This function will set LEVEL attribute first.
|
|
|
|
*
|
|
|
|
* @param pAddrInfo
|
|
|
|
* @param cmd - level cluster command id
|
|
|
|
* @param cmdPayload
|
|
|
|
*
|
|
|
|
* @return status_t
|
|
|
|
*/
|
|
|
|
status_t sampleLight_levelCb(zclIncomingAddrInfo_t *pAddrInfo, u8 cmdId, void *cmdPayload)
|
|
|
|
{
|
2022-04-01 17:14:59 +00:00
|
|
|
if (pAddrInfo->dstEp == SAMPLE_LIGHT_ENDPOINT)
|
|
|
|
{
|
|
|
|
switch (cmdId)
|
|
|
|
{
|
|
|
|
case ZCL_CMD_LEVEL_MOVE_TO_LEVEL:
|
|
|
|
case ZCL_CMD_LEVEL_MOVE_TO_LEVEL_WITH_ON_OFF:
|
|
|
|
sampleLight_moveToLevelProcess(cmdId, (moveToLvl_t *)cmdPayload);
|
|
|
|
break;
|
|
|
|
case ZCL_CMD_LEVEL_MOVE:
|
|
|
|
case ZCL_CMD_LEVEL_MOVE_WITH_ON_OFF:
|
|
|
|
sampleLight_moveProcess(cmdId, (move_t *)cmdPayload);
|
|
|
|
break;
|
|
|
|
case ZCL_CMD_LEVEL_STEP:
|
|
|
|
case ZCL_CMD_LEVEL_STEP_WITH_ON_OFF:
|
|
|
|
sampleLight_stepProcess(cmdId, (step_t *)cmdPayload);
|
|
|
|
break;
|
|
|
|
case ZCL_CMD_LEVEL_STOP:
|
|
|
|
case ZCL_CMD_LEVEL_STOP_WITH_ON_OFF:
|
|
|
|
sampleLight_stopProcess(cmdId, (stop_t *)cmdPayload);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2022-04-01 16:59:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ZCL_STA_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2022-04-01 17:14:59 +00:00
|
|
|
#endif /* ZCL_LEVEL_CTRL */
|
2022-04-01 16:59:22 +00:00
|
|
|
|
2022-04-01 17:14:59 +00:00
|
|
|
#endif /* __PROJECT_TL_DIMMABLE_LIGHT__ */
|