2022-04-01 16:59:22 +00:00
|
|
|
/********************************************************************************************************
|
|
|
|
* @file zb_appCb.c
|
|
|
|
*
|
|
|
|
* @brief This is the source file for zb_appCb
|
|
|
|
*
|
|
|
|
* @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 "bdb.h"
|
|
|
|
#include "ota.h"
|
|
|
|
#include "sampleLight.h"
|
|
|
|
#include "sampleLightCtrl.h"
|
|
|
|
|
|
|
|
/**********************************************************************
|
|
|
|
* LOCAL CONSTANTS
|
|
|
|
*/
|
2022-04-01 17:14:59 +00:00
|
|
|
#define DEBUG_HEART 0
|
2022-04-01 16:59:22 +00:00
|
|
|
|
|
|
|
/**********************************************************************
|
|
|
|
* TYPEDEFS
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**********************************************************************
|
|
|
|
* LOCAL FUNCTIONS
|
|
|
|
*/
|
|
|
|
void zbdemo_bdbInitCb(u8 status, u8 joinedNetwork);
|
|
|
|
void zbdemo_bdbCommissioningCb(u8 status, void *arg);
|
|
|
|
void zbdemo_bdbIdentifyCb(u8 endpoint, u16 srcAddr, u16 identifyTime);
|
|
|
|
|
|
|
|
/**********************************************************************
|
|
|
|
* GLOBAL VARIABLES
|
|
|
|
*/
|
|
|
|
bdb_appCb_t g_zbDemoBdbCb = {zbdemo_bdbInitCb, zbdemo_bdbCommissioningCb, zbdemo_bdbIdentifyCb, NULL};
|
|
|
|
|
|
|
|
#ifdef ZCL_OTA
|
|
|
|
ota_callBack_t sampleLight_otaCb =
|
2022-04-01 17:14:59 +00:00
|
|
|
{
|
|
|
|
sampleLight_otaProcessMsgHandler,
|
2022-04-01 16:59:22 +00:00
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/**********************************************************************
|
|
|
|
* LOCAL VARIABLES
|
|
|
|
*/
|
|
|
|
u32 heartInterval = 0;
|
|
|
|
|
|
|
|
#if DEBUG_HEART
|
|
|
|
ev_timer_event_t *heartTimerEvt = NULL;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/**********************************************************************
|
|
|
|
* FUNCTIONS
|
|
|
|
*/
|
|
|
|
#if DEBUG_HEART
|
2022-04-01 17:14:59 +00:00
|
|
|
static s32 heartTimerCb(void *arg)
|
|
|
|
{
|
|
|
|
if (heartInterval == 0)
|
|
|
|
{
|
2022-04-01 16:59:22 +00:00
|
|
|
heartTimerEvt = NULL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
gpio_toggle(LED_POWER);
|
|
|
|
|
|
|
|
return heartInterval;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-04-01 17:14:59 +00:00
|
|
|
s32 sampleLight_bdbNetworkSteerStart(void *arg)
|
|
|
|
{
|
2022-04-01 16:59:22 +00:00
|
|
|
bdb_networkSteerStart();
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if FIND_AND_BIND_SUPPORT
|
2022-04-01 17:14:59 +00:00
|
|
|
s32 sampleLight_bdbFindAndBindStart(void *arg)
|
|
|
|
{
|
2022-04-01 16:59:22 +00:00
|
|
|
bdb_findAndBindStart(BDB_COMMISSIONING_ROLE_TARGET);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* @fn zbdemo_bdbInitCb
|
|
|
|
*
|
|
|
|
* @brief application callback for bdb initiation
|
|
|
|
*
|
|
|
|
* @param status - the status of bdb init BDB_INIT_STATUS_SUCCESS or BDB_INIT_STATUS_FAILURE
|
|
|
|
*
|
|
|
|
* @param joinedNetwork - 1: node is on a network, 0: node isn't on a network
|
|
|
|
*
|
|
|
|
* @return None
|
|
|
|
*/
|
2022-04-01 17:14:59 +00:00
|
|
|
void zbdemo_bdbInitCb(u8 status, u8 joinedNetwork)
|
|
|
|
{
|
|
|
|
if (status == BDB_INIT_STATUS_SUCCESS)
|
|
|
|
{
|
2022-04-01 16:59:22 +00:00
|
|
|
/*
|
|
|
|
* start bdb commissioning
|
|
|
|
* */
|
2022-04-01 17:14:59 +00:00
|
|
|
if (joinedNetwork)
|
|
|
|
{
|
2022-04-01 16:59:22 +00:00
|
|
|
heartInterval = 1000;
|
|
|
|
|
|
|
|
#ifdef ZCL_OTA
|
|
|
|
ota_queryStart(OTA_PERIODIC_QUERY_INTERVAL);
|
|
|
|
#endif
|
2022-04-01 17:14:59 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-04-01 16:59:22 +00:00
|
|
|
heartInterval = 500;
|
|
|
|
|
2022-04-01 17:14:59 +00:00
|
|
|
#if (!ZBHCI_EN)
|
2022-04-01 16:59:22 +00:00
|
|
|
u16 jitter = 0;
|
2022-04-01 17:14:59 +00:00
|
|
|
do
|
|
|
|
{
|
2022-04-01 16:59:22 +00:00
|
|
|
jitter = zb_random() % 0x0fff;
|
2022-04-01 17:14:59 +00:00
|
|
|
} while (jitter == 0);
|
2022-04-01 16:59:22 +00:00
|
|
|
TL_ZB_TIMER_SCHEDULE(sampleLight_bdbNetworkSteerStart, NULL, jitter);
|
|
|
|
#endif
|
|
|
|
}
|
2022-04-01 17:14:59 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-04-01 16:59:22 +00:00
|
|
|
heartInterval = 200;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if DEBUG_HEART
|
2022-04-01 17:14:59 +00:00
|
|
|
if (heartTimerEvt)
|
|
|
|
{
|
2022-04-01 16:59:22 +00:00
|
|
|
TL_ZB_TIMER_CANCEL(&heartTimerEvt);
|
|
|
|
}
|
|
|
|
heartTimerEvt = TL_ZB_TIMER_SCHEDULE(heartTimerCb, NULL, heartInterval);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* @fn zbdemo_bdbCommissioningCb
|
|
|
|
*
|
|
|
|
* @brief application callback for bdb commissioning
|
|
|
|
*
|
|
|
|
* @param status - the status of bdb commissioning
|
|
|
|
*
|
|
|
|
* @param arg
|
|
|
|
*
|
|
|
|
* @return None
|
|
|
|
*/
|
2022-04-01 17:14:59 +00:00
|
|
|
void zbdemo_bdbCommissioningCb(u8 status, void *arg)
|
|
|
|
{
|
|
|
|
if (status == BDB_COMMISSION_STA_SUCCESS)
|
|
|
|
{
|
2022-04-01 16:59:22 +00:00
|
|
|
heartInterval = 1000;
|
|
|
|
|
|
|
|
#if FIND_AND_BIND_SUPPORT
|
2022-04-01 17:14:59 +00:00
|
|
|
if (!gLightCtx.bdbFindBindFlg)
|
|
|
|
{
|
|
|
|
gLightCtx.bdbFindBindFlg = TRUE;
|
2022-04-01 16:59:22 +00:00
|
|
|
#endif
|
|
|
|
|
2022-04-01 17:14:59 +00:00
|
|
|
light_blink_start(2, 200, 200);
|
2022-04-01 16:59:22 +00:00
|
|
|
|
|
|
|
#ifdef ZCL_OTA
|
2022-04-01 17:14:59 +00:00
|
|
|
ota_queryStart(OTA_PERIODIC_QUERY_INTERVAL);
|
2022-04-01 16:59:22 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if FIND_AND_BIND_SUPPORT
|
2022-04-01 17:14:59 +00:00
|
|
|
// start Finding & Binding
|
|
|
|
TL_ZB_TIMER_SCHEDULE(sampleLight_bdbFindAndBindStart, NULL, 1000);
|
|
|
|
}
|
2022-04-01 16:59:22 +00:00
|
|
|
#endif
|
2022-04-01 17:14:59 +00:00
|
|
|
}
|
|
|
|
else if (status == BDB_COMMISSION_STA_IN_PROGRESS)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
else if (status == BDB_COMMISSION_STA_NOT_AA_CAPABLE)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
else if ((status == BDB_COMMISSION_STA_NO_NETWORK) || (status == BDB_COMMISSION_STA_TCLK_EX_FAILURE))
|
|
|
|
{
|
2022-04-01 16:59:22 +00:00
|
|
|
u16 jitter = 0;
|
2022-04-01 17:14:59 +00:00
|
|
|
do
|
|
|
|
{
|
2022-04-01 16:59:22 +00:00
|
|
|
jitter = zb_random() % 0x0fff;
|
2022-04-01 17:14:59 +00:00
|
|
|
} while (jitter == 0);
|
2022-04-01 16:59:22 +00:00
|
|
|
TL_ZB_TIMER_SCHEDULE(sampleLight_bdbNetworkSteerStart, NULL, jitter);
|
2022-04-01 17:14:59 +00:00
|
|
|
}
|
|
|
|
else if (status == BDB_COMMISSION_STA_TARGET_FAILURE)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
else if (status == BDB_COMMISSION_STA_FORMATION_FAILURE)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
else if (status == BDB_COMMISSION_STA_NO_IDENTIFY_QUERY_RESPONSE)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
else if (status == BDB_COMMISSION_STA_BINDING_TABLE_FULL)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
else if (status == BDB_COMMISSION_STA_NO_SCAN_RESPONSE)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
else if (status == BDB_COMMISSION_STA_NOT_PERMITTED)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
else if (status == BDB_COMMISSION_STA_REJOIN_FAILURE)
|
|
|
|
{
|
2022-04-01 16:59:22 +00:00
|
|
|
zb_rejoinReq(NLME_REJOIN_METHOD_REJOIN, zb_apsChannelMaskGet());
|
2022-04-01 17:14:59 +00:00
|
|
|
}
|
|
|
|
else if (status == BDB_COMMISSION_STA_FORMATION_DONE)
|
|
|
|
{
|
2022-04-01 16:59:22 +00:00
|
|
|
#if ZBHCI_EN
|
|
|
|
|
|
|
|
#else
|
2022-04-01 17:14:59 +00:00
|
|
|
tl_zbMacChannelSet(DEFAULT_CHANNEL); // set default channel
|
2022-04-01 16:59:22 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extern void sampleLight_zclIdentifyCmdHandler(u8 endpoint, u16 srcAddr, u16 identifyTime);
|
2022-04-01 17:14:59 +00:00
|
|
|
void zbdemo_bdbIdentifyCb(u8 endpoint, u16 srcAddr, u16 identifyTime)
|
|
|
|
{
|
2022-04-01 16:59:22 +00:00
|
|
|
#if FIND_AND_BIND_SUPPORT
|
|
|
|
sampleLight_zclIdentifyCmdHandler(endpoint, srcAddr, identifyTime);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef ZCL_OTA
|
|
|
|
void sampleLight_otaProcessMsgHandler(u8 evt, u8 status)
|
|
|
|
{
|
2022-04-01 17:14:59 +00:00
|
|
|
if (evt == OTA_EVT_START)
|
|
|
|
{
|
|
|
|
if (status == ZCL_STA_SUCCESS)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-04-01 16:59:22 +00:00
|
|
|
}
|
2022-04-01 17:14:59 +00:00
|
|
|
}
|
|
|
|
else if (evt == OTA_EVT_COMPLETE)
|
|
|
|
{
|
|
|
|
if (status == ZCL_STA_SUCCESS)
|
|
|
|
{
|
2022-04-01 16:59:22 +00:00
|
|
|
ota_mcuReboot();
|
2022-04-01 17:14:59 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-04-01 16:59:22 +00:00
|
|
|
ota_queryStart(OTA_PERIODIC_QUERY_INTERVAL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-04-01 17:14:59 +00:00
|
|
|
s32 sampleLight_softReset(void *arg)
|
|
|
|
{
|
2022-04-01 16:59:22 +00:00
|
|
|
SYSTEM_RESET();
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* @fn sampleLight_leaveCnfHandler
|
|
|
|
*
|
|
|
|
* @brief Handler for ZDO Leave Confirm message.
|
|
|
|
*
|
|
|
|
* @param pRsp - parameter of leave confirm
|
|
|
|
*
|
|
|
|
* @return None
|
|
|
|
*/
|
|
|
|
void sampleLight_leaveCnfHandler(nlme_leave_cnf_t *pLeaveCnf)
|
|
|
|
{
|
2022-04-01 17:14:59 +00:00
|
|
|
if (pLeaveCnf->status == SUCCESS)
|
|
|
|
{
|
|
|
|
light_blink_start(3, 200, 200);
|
2022-04-01 16:59:22 +00:00
|
|
|
|
2022-04-01 17:14:59 +00:00
|
|
|
// waiting blink over
|
|
|
|
TL_ZB_TIMER_SCHEDULE(sampleLight_softReset, NULL, 2 * 1000);
|
|
|
|
}
|
2022-04-01 16:59:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* @fn sampleLight_leaveIndHandler
|
|
|
|
*
|
|
|
|
* @brief Handler for ZDO leave indication message.
|
|
|
|
*
|
|
|
|
* @param pInd - parameter of leave indication
|
|
|
|
*
|
|
|
|
* @return None
|
|
|
|
*/
|
|
|
|
void sampleLight_leaveIndHandler(nlme_leave_ind_t *pLeaveInd)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-04-01 17:14:59 +00:00
|
|
|
u8 sampleLight_nwkUpdateIndicateHandler(nwkCmd_nwkUpdate_t *pNwkUpdate)
|
|
|
|
{
|
2022-04-01 16:59:22 +00:00
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
|
2022-04-01 17:14:59 +00:00
|
|
|
#endif /* __PROJECT_TL_DIMMABLE_LIGHT__ */
|