全部博文(200)
分类:
2013-01-07 14:59:10
HalLedSet ( HAL_LED_4, HAL_LED_MODE_OFF );
// Initiate an End Device Bind Request for the mandatory endpoint
dstAddr.addr.shortAddr = 0x0000; // Coordinator
ZDP_EndDeviceBindReq( &dstAddr, NLME_GetShortAddr(),
SerialApp_epDesc.endPoint,
SERIALAPP_MAX_CLUSTERS,
SERIALAPP_MAX_CLUSTERS,
HalLedSet ( HAL_LED_4, HAL_LED_MODE_OFF );
// Initiate a Match Description Request (Service Discovery)
dstAddr.addr.shortAddr = NWK_BROADCAST_SHORTADDR;
ZDP_MatchDescReq( &dstAddr, NWK_BROADCAST_SHORTADDR,
ZDP_EndDeviceBindReq( &dstAddr, //目的地址设为0x0000;
SERIALAPP_PROFID,//Profile ID
(cId_t *)SerialApp_ClusterList, //输入簇列表
(cId_t *)SerialApp_ClusterList,//输出簇列表
fillAndSend( &ZDP_TransID, dstAddr, End_Device_Bind_req, len );
AF_DataRequest( &afAddr, &ZDApp_epDesc, clusterID,
(uint16)(len+1), (uint8*)(ZDP_TmpBuf-1),
transSeq, ZDP_TxOptions, AF_DEFAULT_RADIUS );
这个信息会传送到ZDO层,在ZDO层的事件处理函数中,调用ZDApp_ProcessOSALMsg( (osal_event_hdr_t *)msg_ptr );
UINT16 ZDApp_event_loop( byte task_id, UINT16 events )
uint8 *msg_ptr;
if ( events & SYS_EVENT_MSG )
while ( (msg_ptr = osal_msg_receive( ZDAppTaskID )) )
ZDApp_ProcessOSALMsg( (osal_event_hdr_t *)msg_ptr );
// Release the memory
osal_msg_deallocate( msg_ptr );
// Return unprocessed events
void ZDApp_ProcessOSALMsg( osal_event_hdr_t *msgPtr )
// Data Confirmation message fields
byte sentEP; // This should always be 0
byte sentStatus;
afDataConfirm_t *afDataConfirm;
switch ( msgPtr->event )
case AF_INCOMING_MSG_CMD:
ZDP_IncomingData( (afIncomingMSGPacket_t *)msgPtr );
在ZDP_IncomingData( (afIncomingMSGPacket_t *)msgPtr );函数中
void ZDP_IncomingData( afIncomingMSGPacket_t *pData )
uint8 handled;
zdoIncomingMsg_t inMsg;
inMsg.srcAddr.addrMode = Addr16Bit;
inMsg.srcAddr.addr.shortAddr = pData->srcAddr.addr.shortAddr;
inMsg.wasBroadcast = pData->wasBroadcast;
inMsg.clusterID = pData->clusterId;
inMsg.SecurityUse = pData->SecurityUse;
inMsg.asduLen = pData->cmd.DataLength-1;
inMsg.asdu = pData->cmd.Data+1;
inMsg.TransSeq = pData->cmd.Data[0];
handled = ZDO_SendMsgCBs( &inMsg );
MT_ZdoRsp( &inMsg );
while ( zdpMsgProcs[x].clusterID != 0xFFFF )
if ( zdpMsgProcs[x].clusterID == inMsg.clusterID )
zdpMsgProcs[x].pFn( &inMsg );
// Handle unhandled messages
if ( !handled )
ZDApp_InMsgCB( &inMsg );
因为ZDO信息处理表zdpMsgProcs[
]没有对应的End_Device_Bind_req簇,因此没有调用ZDO信息处理表中的处理函数,但是前面的ZDO_SendMsgCBs()会把这个终端设备绑定请求发送到登记过这个ZDO信息的任务中去。那这个登记注册的程序在哪里呢?
对于协调器来说,由于在void ZDApp_Init( byte task_id )函数中调用了ZDApp_RegisterCBs();面的函数。进行注册了终端绑定请求信息。
#if defined ( ZDO_IEEEADDR_REQUEST ) || defined ( REFLECTOR )
ZDO_RegisterForZDOMsg( ZDAppTaskID, IEEE_addr_rsp );
#if defined ( ZDO_NWKADDR_REQUEST ) || defined ( REFLECTOR )
ZDO_RegisterForZDOMsg( ZDAppTaskID, NWK_addr_rsp );
ZDO_RegisterForZDOMsg( ZDAppTaskID, Bind_rsp );
ZDO_RegisterForZDOMsg( ZDAppTaskID, Unbind_rsp );
ZDO_RegisterForZDOMsg( ZDAppTaskID, End_Device_Bind_req );
ZDO_RegisterForZDOMsg( ZDAppTaskID, Bind_req );
ZDO_RegisterForZDOMsg( ZDAppTaskID, Unbind_req );
UINT16 ZDApp_event_loop( byte task_id, UINT16 events )
uint8 *msg_ptr;
if ( events & SYS_EVENT_MSG )
while ( (msg_ptr = osal_msg_receive( ZDAppTaskID )) )
ZDApp_ProcessOSALMsg( (osal_event_hdr_t *)msg_ptr );
// Release the memory
osal_msg_deallocate( msg_ptr );
// Return unprocessed events
在这里调用函数ZDApp_ProcessOSALMsg( (osal_event_hdr_t *)msg_ptr );在这个函数中我们可以看到对ZDO_CB_MSG事件的处理
void ZDApp_ProcessOSALMsg( osal_event_hdr_t *msgPtr )
// Data Confirmation message fields
byte sentEP; // This should always be 0
byte sentStatus;
afDataConfirm_t *afDataConfirm;
switch ( msgPtr->event )
// Incoming ZDO Message
case AF_INCOMING_MSG_CMD:
ZDP_IncomingData( (afIncomingMSGPacket_t *)msgPtr );
case ZDO_CB_MSG:
ZDApp_ProcessMsgCBs( (zdoIncomingMsg_t *)msgPtr );
调用ZDApp_ProcessMsgCBs()函数。在这个函数中根据ClusterID(这里是 End_Device_Bind_req)选择相对应的匹配描述符处理函数,
switch ( inMsg->clusterID )
#if defined ( ZDO_NWKADDR_REQUEST ) || defined ( ZDO_IEEEADDR_REQUEST ) || defined ( REFLECTOR )
case NWK_addr_rsp:
case IEEE_addr_rsp:
ZDO_NwkIEEEAddrResp_t *pAddrRsp;
pAddrRsp = ZDO_ParseAddrRsp( inMsg );
if ( pAddrRsp )
if ( pAddrRsp->status == ZSuccess )
ZDO_UpdateAddrManager( pAddrRsp->nwkAddr, pAddrRsp->extAddr );
osal_mem_free( pAddrRsp );
case Bind_req:
ZDO_BindUnbindReq_t bindReq;
ZDO_ParseBindUnbindReq( inMsg, &bindReq );
ZDO_ProcessBindUnbindReq( inMsg, &bindReq );
case Bind_rsp:
if ( matchED )
ZDMatchSendState(
(uint8)((inMsg->clusterID == Bind_rsp) ? ZDMATCH_REASON_BIND_RSP : ZDMATCH_REASON_UNBIND_RSP),
ZDO_ParseBindRsp(inMsg), inMsg->TransSeq );
ZDEndDeviceBind_t bindReq;
ZDO_ParseEndDeviceBindReq( inMsg, &bindReq );//解析绑定请求信息
ZDO_MatchEndDeviceBind( &bindReq );
// Freeing the cluster lists - if allocated.
if ( bindReq.numInClusters )
osal_mem_free( bindReq.inClusters );
if ( bindReq.numOutClusters )
osal_mem_free( bindReq.outClusters );
void ZDO_MatchEndDeviceBind( ZDEndDeviceBind_t *bindReq )
zAddrType_t dstAddr;
uint8 sendRsp = FALSE;
uint8 status;
// Is this the first request? 接收到的是第一个绑定请求
if ( matchED == NULL )
// Create match info structure 创建匹配信息结构体
matchED = (ZDMatchEndDeviceBind_t *)osal_mem_alloc( sizeof ( ZDMatchEndDeviceBind_t ) ); //分配空间
if ( matchED )
// Clear the structure 先进行清除操作
osal_memset( (uint8 *)matchED, 0, sizeof ( ZDMatchEndDeviceBind_t ) );
// Copy the first request's information 复制第一个请求信息
if ( !ZDO_CopyMatchInfo( &(matchED->ed1), bindReq ) ) //复制不成功后
status = ZDP_NO_ENTRY;
sendRsp = TRUE;
status = ZDP_NO_ENTRY;
sendRsp = TRUE;
if ( !sendRsp ) //分配空间成功 ,复制数据结构成功
// Set into the correct state 设置正确的设备状态
matchED->state = ZDMATCH_WAIT_REQ;
ZDO_EndDeviceBindMatchTimeoutCB );
matchED->state = ZDMATCH_SENDING_BINDS; //状态为绑定中
// Copy the 2nd request's information 拷贝第2个请求信息结构
if ( !ZDO_CopyMatchInfo( &(matchED->ed2), bindReq ) ) //拷贝不成功
status = ZDP_NO_ENTRY;
sendRsp = TRUE;
// Make a source match for ed1
matchED->ed1numMatched = ZDO_CompareClusterLists(
matchED->ed1.numOutClusters, matchED->ed1.outClusters,
matchED->ed2.numInClusters, matchED->ed2.inClusters, ZDOBuildBuf );
if ( matchED->ed1numMatched ) //如果有返回ed1相匹配的簇
// Save the match list 申请空间保存相匹配的簇列表
matchED->ed1Matched= osal_mem_alloc( (short)(matchED->ed1numMatched * sizeof ( uint16 )) );
if ( matchED->ed1Matched ) //分配成功
osal_memcpy(matchED->ed1Matched,ZDOBuildBuf, (matchED->ed1numMatched * sizeof ( uint16 )) );
else //内存空间分配不成功
status = ZDP_NO_ENTRY;
sendRsp = TRUE;
// Make a source match for ed2 以ed2为源
matchED->ed2numMatched = ZDO_CompareClusterLists(
matchED->ed2.numOutClusters, matchED->ed2.outClusters,
matchED->ed1.numInClusters, matchED->ed1.inClusters, ZDOBuildBuf );
if ( matchED->ed2numMatched )//如果匹配成功
// Save the match list 保存匹配的簇列表
matchED->ed2Matched = osal_mem_alloc( (short)(matchED->ed2numMatched * sizeof ( uint16 )) );
if ( matchED->ed2Matched )
osal_memcpy( matchED->ed2Matched, ZDOBuildBuf, (matchED->ed2numMatched * sizeof ( uint16 )) );
// Allocation error, stop
status = ZDP_NO_ENTRY;
sendRsp = TRUE;
if ( (sendRsp == FALSE) && (matchED->ed1numMatched || matchED->ed2numMatched) )
// Do the first unbind/bind state 发送响应信息给两个设备
ZDMatchSendState( ZDMATCH_REASON_START, ZDP_SUCCESS, 0 );
status = ZDP_NO_MATCH;
sendRsp = TRUE;
if ( sendRsp ) //如果没有相匹配的或匹配不成功
// send response to this requester 发送匹配请求响应
dstAddr.addrMode = Addr16Bit; //设置目的地址是16位的短地址
dstAddr.addr.shortAddr = bindReq->srcAddr;
//发送绑定终端响应函数status = ZDP_NO_MATCH;
ZDP_EndDeviceBindRsp( bindReq->TransSeq, &dstAddr, status, bindReq->SecurityUse );
if ( matchED->state == ZDMATCH_SENDING_BINDS )
// send response to first requester
dstAddr.addrMode = Addr16Bit;
dstAddr.addr.shortAddr = matchED->ed1.srcAddr;
ZDP_EndDeviceBindRsp( matchED->ed1.TransSeq, &dstAddr, status, matchED->ed1.SecurityUse );
// Process ended - release memory used
ZDO_RemoveMatchMemory();
uint8 ZDMatchSendState( uint8 reason, uint8 status, uint8 TransSeq )
// Send the response messages to requesting devices
// send response to first requester 发送响应信息给第一个请求终端,
dstAddr.addr.shortAddr = matchED->ed1.srcAddr;
ZDP_EndDeviceBindRsp( matchED->ed1.TransSeq, &dstAddr, rspStatus, matchED->ed1.SecurityUse );
// send response to second requester 发送响应信息给第二请求终端
if ( matchED->state == ZDMATCH_SENDING_BINDS )
dstAddr.addr.shortAddr = matchED->ed2.srcAddr;
ZDP_EndDeviceBindRsp( matchED->ed2.TransSeq, &dstAddr, rspStatus, matchED->ed2.SecurityUse );
// Process ended - release memory used
ZDO_RemoveMatchMemory();
return ( TRUE );
由于终端节点在 SerialApp.c 中层注册过 End_Device_Bind_rsp 消息,因此当接收到协调器节点发来的绑定响应消息将交由 SerialApp 任务事件处理函数处理:
UINT16 SerialApp_ProcessEvent( uint8 task_id, UINT16 events )
if ( events & SYS_EVENT_MSG )
afIncomingMSGPacket_t *MSGpkt;
while ( (MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive(
SerialApp_TaskID )) )
switch ( MSGpkt->hdr.event )
SerialApp_ProcessZDOMsgs( (zdoIncomingMsg_t *)MSGpkt );
break;
static void SerialApp_ProcessZDOMsgs( zdoIncomingMsg_t *inMsg )
switch ( inMsg->clusterID )
case End_Device_Bind_rsp:
if ( ZDO_ParseBindRsp( inMsg ) == ZSuccess )
// Light LED
HalLedSet( HAL_LED_4, HAL_LED_MODE_ON );
// Flash LED to show failure
HalLedSet ( HAL_LED_4, HAL_LED_MODE_FLASH );
“终端设备绑定请求”这一命名有误导的嫌疑。这一请求不仅仅适用于终端设备,而且适用于对希望在协调器上绑定的两个设备中匹配的簇实施绑定。一旦这个函数被调用,将假设REFLECTOR这一编译选项在所有希望使用这一服务的节点中都已经打开。具体操作如下:
(1) (Bind Req) Device 1 --> Coordinator <--- Device 2 (Bind Req)
协调器首先找出包含在绑定请求中的簇,然后对比每一设备的IEEE地址,如果簇可以匹配,而且这几个设备没有已经存在的绑定表,那他将发送一个绑定应答给每一个设备。
(2) Device 1 <--- NWK Addr Req ------ Coordinator ------- NWK addr Req ----> Device 2
(3) Device 1 ----> NWK Addr Rsp ---> Coordinator <---- NWK addr Rsp <--- Device 2
(4) Device 1 <----- Bind Rsp <----- Coordinator -----> Bind Rsp ----> Device 2
在《Z-Stack编程指南--绑定》一章中有下面的一段对终端绑定请求的描述。
该绑定机制在一个选定的设备上使用一个按键动作或类似动作在一个定制的超时周期内进行的绑定