组网流程图如下:
第一个功能:协调器的组网,终端设备和路由设备发现网络以及加入网络
//第一步:Z-Stack 由 main()函数开始执行,main()函数共做了 2 件事:一是系统初始化,另外一件是开始执行轮转查询式操作系统
......
//第二步,进入 osal_init_system()函数,执行操作系统初始化
//第三步,进入osalInitTasks()函数,执行操作系统任务初始化
//当有按键按下的时候,产生一个系统消息
//第四步,进入ZDApp_init()函数,执行ZDApp层初始化
//The first step
//The third step,执行ZDOInitDevice()函数,执行设备初始化
.......
.......
}
//The fouth step,执行 ZDApp_NetWorkInit()函数
//The fifth step,转到ZDApp_event_loop()函数
}
//The sixth step,执行ZDO_StartDevice()函数,启动设备
......
}
}
......
}
//The seventh step,分两种情况,1.协调器 2.路由器或终端设备
1)协调器
osal_set_event( ZDAppTaskID, ZDO_NETWORK_START ); //发送网络启动事件 到 ZDApp层,接着转到ZDApp_event_loop()函数
......
}
......
}
......
}
2)路由器或终端设备
//The seventh step(终端设备), 当发现有网络存在时,网络层将给予 ZDO 层发现网络反馈信息
{
.......
//把网络发现这个反馈消息,发送到ZDA层,转到 ZDApp_ProcessOSALMsg(),执行
......
case ZDO_NWK_DISC_CNF: // (终端设备),网络发现响应。
//The ninth step,执行 ZDO_JoinConfirmCB()函数
......
}
......
......
在执行 ZDApp_ProcessNetworkJoin ()函数的时候,要分两种情况,一种是终端设备,一种是路由器:
3)终端设备:
......
}
......
}
4)路由器:
......
......
......
UINT16 ZDApp_event_loop( uint8 task_id, UINT16 events )
}
.......
//并在网络中的身份确定后产生的一个事件
......
}
}
//The ninth step,执行ZDO_UpdateNwkStatus()函数,完成网络状态更新
......
//The tenth step,执行zdoSendStateChangeMsg()函数
//ZDO_STATE_CHANGE_EVT事件处理函数。
......
//自此,协调器组网形成(终端设备成功加入网络)
......
}
//第五步,//初始化玩系统任务事件后,正是开始执行操作系统,此时操作系统不断的检测有没有任务事件发生,一旦检测到有事件发生,就转 //到相应的处理函数,进行处理。
第二个功能:设备间的绑定
-----------引用自蓝天白云
/*当我们按下sw2,即JoyStick控杆的右键时,节点发出终端设备绑定请求,因为我们在SerialApp层,注册过了键盘响应事件,所以,当我们按 下右键时,我们会在SerialApp_ProcessEvent()函数里找到对应的键盘相应事件*/
UINT16 SerialApp_ProcessEvent( uint8 task_id, UINT16 events ) //当有事件传递到应用层的时候,执行此处
{
if ( events & SYS_EVENT_MSG ) // 有事件传递过来,故通过这个条件语句
......
case KEY_CHANGE: //键盘触发事件
.......
}
void SerialApp_HandleKeys( uint8 shift, uint8 keys )
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,//输出簇列表
该函数实际调用无线发送函数将绑定请求发送给协调器节点:默认clusterID为End_Device_Bind_req,最后通过AF_DataRequest()发送出去.
fillAndSend( &ZDP_TransID, dstAddr, End_Device_Bind_req, len );
(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;
//解析clusterID这个消息
inMsg.srcAddr.addrMode = Addr16Bit;
inMsg.srcAddr.addr.shortAddr = pData->srcAddr.addr.shortAddr;
inMsg.wasBroadcast = pData->wasBroadcast;
inMsg.clusterID = pData->clusterId; //这个clusterID,在这里指的是,终端设备发送过来的End_Device_Bind_req这个消息
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[]中,查找,看看有没有跟End_Device_Bind_req相匹配的描述符。
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)选择相对应的匹配描述符处理函数,
.......
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 );
第三个功能:实现两个节点间的串口通信
“串口终端1”的数据,如何被“节点 1”所接收,并且发送出去的?
串口数据==>DMA接收==>主循环中通过SerialApp_CallBack 查询==>从 DMA获取并发送到空中。
具体流程如下:
......
......
}
}
节点2 在收到空中的信号后,如何传递给与其相连的串口终端?
......
while ( (MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( SerialApp_TaskID )) )
......
case AF_INCOMING_MSG_CMD: //在这个实验中,使用串口通讯时,触发的事件,从空中捕获到信号。
......
}
}
}
......
}
}
......
if ( events & SERIALAPP_RESP_EVT ) //串口响应事件,表示成功接受来自节点1的数据,
......
节点1,接收到来自节点2的response。
......
while ( (MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( SerialApp_TaskID )) )
......
case AF_INCOMING_MSG_CMD: //在这个实验中,使用串口通讯时,触发的事件,从空中捕获到信号。
......
}
}
}
SERIALAPP_CLUSTERID2代表接收到发送成功的response,取消自动重发,如果不,自动重发。
......
------------------------------------------------------------------------------------------------------------------- 2011/08/07