该函数的代码如下;
void zb_BindDevice ( uint8 create, uint16 commandId, uint8 *pDestination )
{
zAddrType_t destination;
uint8 ret = ZB_ALREADY_IN_PROGRESS;
if ( create ) //如果是TRUE,也就是意味着是建立绑定
{
if (sapi_bindInProgress == 0xffff)//如果在这个命令上没有创建绑定
{
if ( pDestination ) //已知扩展地址的绑定,即:*pDestination为非NULL
{
destination.addrMode = Addr64Bit;
osal_cpyExtAddr( destination.addr.extAddr, pDestination );//复制扩展地址
//调用APS绑定请求函数
ret = APSME_BindRequest( sapi_epDesc.endPoint, //源EP
commandId, //命令ID
&destination, //目的设备地址模式
api_epDesc.endPoint );//目的EP
if ( ret == ZSuccess )//绑定成功
{
// Find nwk addr 发现网络地址,得到被绑定的设备的短地址
ZDP_NwkAddrReq(pDestination, ZDP_ADDR_REQTYPE_SINGLE, 0, 0 );
osal_start_timerEx( ZDAppTaskID, ZDO_NWK_UPDATE_NV, 250 );
}
}
else //未知扩展地址的绑定 *pDestination为NULL
{
ret = ZB_INVALID_PARAMETER;
destination.addrMode = Addr16Bit; //设置16位短地址
destination.addr.shortAddr = NWK_BROADCAST_SHORTADDR;//广播模式
//比较输出簇commandId是否和本终端输出簇列表中有匹配项,成功匹配返回TRUE
if(ZDO_AnyClusterMatches(1,&commandId,
sapi_epDesc.simpleDesc->AppNumOutClusters,
sapi_epDesc.simpleDesc->pAppOutClusterList ) )
{
// Try to match with a device in the allow bind mode 匹配一个在允许绑定模式下的设备
ret = ZDP_MatchDescReq( &destination, NWK_BROADCAST_SHORTADDR,
sapi_epDesc.simpleDesc->AppProfId, 1, &commandId, 0, (cId_t *)NULL, 0 );
}
//如果commandId是输入簇,则查找本地输入簇列表中是否有匹配项
else if( ZDO_AnyClusterMatches(1,&commandId, sapi_epDesc.simpleDesc->AppNumInClusters,
sapi_epDesc.simpleDesc->pAppInClusterList ) )
{ //匹配一个在允许绑定模式的设备
ret = ZDP_MatchDescReq( &destination, NWK_BROADCAST_SHORTADDR,
sapi_epDesc.simpleDesc->AppProfId, 0, (cId_t *)NULL, 1, &commandId, 0 );
}
if ( ret == ZB_SUCCESS )//如果匹配成功
{
// Set a timer to make sure bind completes 设置一个时间,确保绑定完成
osal_start_timerEx(sapi_TaskID, ZB_BIND_TIMER, AIB_MaxBindingTime);
sapi_bindInProgress = commandId; //允许基于命令的绑定过程
return; // dont send cback event
}
}
}
SAPI_SendCback( SAPICB_BIND_CNF, ret, commandId );
}
else //create=FALSE 解除绑定
{
// Remove local bindings for the commandId
BindingEntry_t *pBind;
// Loop through bindings an remove any that match the cluster
while ( pBind = bindFind( sapi_epDesc.simpleDesc->EndPoint, commandId, 0 ) )
{
bindRemoveEntry(pBind); //完成从绑定表格中移除绑定条目
}
osal_start_timerEx( ZDAppTaskID, ZDO_NWK_UPDATE_NV, 250 );
}
return;