第一个功能:协调器的组网,终端设备和路由设备发现网络以及加入网络
//第一步:Z-Stack 由 main()函数开始执行,main()函数共做了 2 件事:一是系统初始化,另外一件是开始执行轮转查询式操作系统
int main( void ) { .......// Initialize the operating systemosal_init_system(); //第二步,操作系统初始化
......osal_start_system(); //初始化完系统任务事件后,正式开始执行操作系统 ......}
//第二步,进入 osal_init_system()函数,执行操作系统初始化uint8 osal_init_system( void ) //初始化操作系统,其中最重要的是,初始化操作系统的任务{// Initialize the Memory Allocation System osal_mem_init();// Initialize the message queue osal_qHead = NULL;// Initialize the timers osalTimerInit();// Initialize the Power Management System osal_pwrmgr_init();// Initialize the system tasks.osalInitTasks(); //第三步,执行操作系统任务初始化函数// Setup efficient search for the first free block of heap. osal_mem_kick(); return ( SUCCESS );}
//第三步,进入osalInitTasks()函数,执行操作系统任务初始化void osalInitTasks( void ) //第三步,初始化操作系统任务{ uint8 taskID = 0; tasksEvents = (uint16 *)osal_mem_alloc( sizeof( uint16 ) * tasksCnt); osal_memset( tasksEvents, 0, (sizeof( uint16 ) * tasksCnt));//任务优先级由高向低依次排列,高优先级对应 taskID 的值反而小 macTaskInit( taskID++ ); //不需要用户考虑 nwk_init( taskID++ ); //不需要用户考虑 Hal_Init( taskID++ ); //硬件抽象层初始化,需要我们考虑#if defined( MT_TASK ) MT_TaskInit( taskID++ );#endif APS_Init( taskID++ ); //不需要用户考虑#if defined ( ZIGBEE_FRAGMENTATION ) APSF_Init( taskID++ );#endifZDApp_Init( taskID++ ); //第四步,ZDApp层,初始化 ,执行ZDApp_init函数后,如果是协调器将建立网络,如果是终端设备将加入网络。#if defined ( ZIGBEE_FREQ_AGILITY ) || defined ( ZIGBEE_PANID_CONFLICT ) ZDNwkMgr_Init( taskID++ );#endif SerialApp_Init( taskID ); //应用层SerialApp层初始化,需要用户考虑 在此处设置了一个按键触发事件,
//当有按键按下的时候,产生一个系统消息} //第四步,进入ZDApp_init()函数,执行ZDApp层初始化
//The first stepvoid ZDApp_Init( uint8 task_id ) //The first step,ZDApp层初始化。{// Save the task ID ZDAppTaskID = task_id;// Initialize the ZDO global device short address storage ZDAppNwkAddr.addrMode = Addr16Bit; ZDAppNwkAddr.addr.shortAddr = INVALID_NODE_ADDR; (void)NLME_GetExtAddr(); // Load the saveExtAddr pointer.// Check for manual "Hold Auto Start" ZDAppCheckForHoldKey();// Initialize ZDO items and setup the device - type of device to create. ZDO_Init();// Register the endpoint description with the AF // This task doesn't have a Simple description, but we still need // to register the endpoint. afRegister( (endPointDesc_t *)&ZDApp_epDesc );#if defined( ZDO_USERDESC_RESPONSE ) ZDApp_InitUserDesc();#endif // ZDO_USERDESC_RESPONSE// Start the device? if ( devState != DEV_HOLD ) //devState 初值为DEV_INIT , 所以在初始化ZDA层时,就执行该条件语句 {ZDOInitDevice( 0 ); //The second step, 接着转到ZDOInitDevice()函数,执行The third step; } else {// Blink LED to indicate HOLD_START HalLedBlink ( HAL_LED_4, 0, 50, 500 ); } ZDApp_RegisterCBs();}//The third step,执行ZDOInitDevice()函数,执行设备初始化uint8 ZDOInitDevice( uint16 startDelay ) //The third step, ZDO层初始化设备,{
.......// Trigger the network startZDApp_NetworkInit( extendedDelay ); //网络初始化,跳到相应的函数里头,执行The fourth step
.......
}//The fouth step,执行 ZDApp_NetWorkInit()函数void ZDApp_NetworkInit( uint16 delay ) //The fourth step,网络初始化{ if ( delay ) {// Wait awhile before starting the device osal_start_timerEx( ZDAppTaskID, ZDO_NETWORK_INIT, delay ); //发送ZDO_NETWORK_INIT(网络初始化)消息到 ZDApp层,转到 //ZDApp层,执行The fifth step , ZDApp_event_loop() 函数 } else { osal_set_event( ZDAppTaskID, ZDO_NETWORK_INIT ); }}
//The fifth step,转到ZDApp_event_loop()函数UINT16 ZDApp_event_loop( uint8 task_id, UINT16 events ){if ( events & ZDO_NETWORK_INIT ) //The fivth step,网络初始化事件处理 {// Initialize apps and start the network devState = DEV_INIT;//设备逻辑类型,启动模式,信标时间,超帧长度,接着转到The sixth step,去启动设备,接着执行The sixth step,转到ZDO_StartDevice()ZDO_StartDevice( (uint8)ZDO_Config_Node_Descriptor.LogicalType, devStartMode, DEFAULT_BEACON_ORDER, DEFAULT_SUPERFRAME_ORDER );// Return unprocessed events return (events ^ ZDO_NETWORK_INIT); }
}
//The sixth step,执行ZDO_StartDevice()函数,启动设备void ZDO_StartDevice( byte logicalType, devStartModes_t startMode, byte beaconOrder, byte superframeOrder ) //The sixth step{
......if ( ZG_BUILD_COORDINATOR_TYPE && logicalType == NODETYPE_COORDINATOR ) //当设备作为协调器时,执行这个条件语句。 { if ( startMode == MODE_HARD ) { devState = DEV_COORD_STARTING; //向网络层发送网络形成请求。当网络层执行 NLME_NetworkFormationRequest()建立网络后,将给予 ZDO层反馈信息。 // 接着转到The seventh step,去执行ZDApp层的 ZDO_NetworkFormationConfirmCB()函数 ret = NLME_NetworkFormationRequest( zgConfigPANID, zgApsUseExtendedPANID, zgDefaultChannelList, zgDefaultStartingScanDuration, beaconOrder, superframeOrder, false ); }if ( ZG_BUILD_JOINING_TYPE && (logicalType == NODETYPE_ROUTER || logicalType == NODETYPE_DEVICE) ) //当为终端设备或路由时 { if ( (startMode == MODE_JOIN) || (startMode == MODE_REJOIN) ) { devState = DEV_NWK_DISC;// zgDefaultChannelList与协调器形成网络的通道号匹配。 网络发现请求。 // 继而转到ZDO_NetworkDiscoveryConfirmCB()函数 ret = NLME_NetworkDiscoveryRequest( zgDefaultChannelList, zgDefaultStartingScanDuration );
}
}
......
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
-----------引用自蓝天白云
UINT16 SerialApp_ProcessEvent( uint8 task_id, UINT16 events ) //当有事件传递到应用层的时候,执行此处
{
if ( events & SYS_EVENT_MSG ) // 有事件传递过来,故通过这个条件语句 {
......
case KEY_CHANGE: //键盘触发事件 SerialApp_HandleKeys( ((keyChange_t *)MSGpkt)->state, ((keyChange_t *)MSGpkt)->keys ); //接着跳到相应的按键处理函数去执行 break;
.......
}}ZDO终端设备绑定请求:设备能告诉协调器他们想建立绑定表格报告。该协调器将使协调并在这两个设备上创建绑定表格条目。在这里是以SerialApp例子为例。
void SerialApp_HandleKeys( uint8 shift, uint8 keys ){....... if ( keys & HAL_KEY_SW_2 ) // Joystick right { HalLedSet ( HAL_LED_4, HAL_LED_MODE_OFF );//终端设备绑定请求 // Initiate an End Device Bind Request for the mandatory endpoint dstAddr.addrMode = Addr16Bit; dstAddr.addr.shortAddr = 0x0000; // Coordinator 地址ZDP_EndDeviceBindReq( &dstAddr, NLME_GetShortAddr(), //终端设备绑定请求 SerialApp_epDesc.endPoint, SERIALAPP_PROFID, SERIALAPP_MAX_CLUSTERS, (cId_t *)SerialApp_ClusterList, SERIALAPP_MAX_CLUSTERS, (cId_t *)SerialApp_ClusterList, FALSE ); }...... if ( keys & HAL_KEY_SW_4 ) { HalLedSet ( HAL_LED_4, HAL_LED_MODE_OFF ); // Initiate a Match Description Request (Service Discovery) dstAddr.addrMode = AddrBroadcast; //广播地址 dstAddr.addr.shortAddr = NWK_BROADCAST_SHORTADDR;ZDP_MatchDescReq( &dstAddr, NWK_BROADCAST_SHORTADDR, //描述符匹配请求 这也是两不同匹配方式,使用的按键不同 SERIALAPP_PROFID, SERIALAPP_MAX_CLUSTERS, (cId_t *)SerialApp_ClusterList, SERIALAPP_MAX_CLUSTERS, (cId_t *)SerialApp_ClusterList, FALSE ); } }}说明:从上面可以看到,SW2是发送终端设备绑定请求方式,SW4是发送描述符匹配请求方式。如果按下SW2的话,使用终端设备绑定请求方式,这里是要通过终端告诉协调器他们想要建立绑定表格,协调器将协调这两个请求的设备,在两个设备上建立绑定表格条目。(1)终端设备向协调器发送终端设备绑定请求 调用ZDP_EndDeviceBindReq()函数发送绑定请求。ZDP_EndDeviceBindReq( &dstAddr, //目的地址设为0x0000; NLME_GetShortAddr(), SerialApp_epDesc.endPoint, //EP号 SERIALAPP_PROFID,//Profile ID SERIALAPP_MAX_CLUSTERS, //输入簇的数目 (cId_t *)SerialApp_ClusterList, //输入簇列表 SERIALAPP_MAX_CLUSTERS, //输出簇数目 (cId_t *)SerialApp_ClusterList,//输出簇列表 FALSE );
该函数实际调用无线发送函数将绑定请求发送给协调器节点:默认clusterID为End_Device_Bind_req,最后通过AF_DataRequest()发送出去.fillAndSend( &ZDP_TransID, dstAddr, End_Device_Bind_req, len );最后通过AF_DataRequest()发送出去,这里的&afAddr,是目的地址; &ZDApp_epDesc ,是端口号; clusterID,是簇号; len+1,是数据的长度;//ZDP_TmpBuf-1,是数据的内容; transSeq,是数据的顺序号; ZDP_TxOptions,是发射的一个选项 ; AF_DEFAULT_RADIUS,是一个默认的半径(跳数)。AF_DataRequest( &afAddr, &ZDApp_epDesc, clusterID, (uint16)(len+1), (uint8*)(ZDP_TmpBuf-1), transSeq, ZDP_TxOptions, AF_DEFAULT_RADIUS );(2) 协调器收到终端设备绑定请求End_Device_Bind_req这个信息会传送到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 eventsreturn (events ^ SYS_EVENT_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 ); break;................................}在ZDP_IncomingData( (afIncomingMSGPacket_t *)msgPtr );函数中void ZDP_IncomingData( afIncomingMSGPacket_t *pData ){ uint8 x = 0; 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 );#if defined( MT_ZDO_FUNC ) MT_ZdoRsp( &inMsg );#endif while ( zdpMsgProcs[x].clusterID != 0xFFFF ) { if ( zdpMsgProcs[x].clusterID == inMsg.clusterID ) //在zdpMsgProcs[]中,查找,看看有没有跟End_Device_Bind_req相匹配的描述符。 { zdpMsgProcs[x].pFn( &inMsg ); return; } x++; } // 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();面的函数。进行注册了终端绑定请求信息。void ZDApp_RegisterCBs( void ){#if defined ( ZDO_IEEEADDR_REQUEST ) || defined ( REFLECTOR ) ZDO_RegisterForZDOMsg( ZDAppTaskID, IEEE_addr_rsp );#endif#if defined ( ZDO_NWKADDR_REQUEST ) || defined ( REFLECTOR ) ZDO_RegisterForZDOMsg( ZDAppTaskID, NWK_addr_rsp );#endif#if defined ( ZDO_COORDINATOR ) ZDO_RegisterForZDOMsg( ZDAppTaskID, Bind_rsp ); ZDO_RegisterForZDOMsg( ZDAppTaskID, Unbind_rsp ); ZDO_RegisterForZDOMsg( ZDAppTaskID, End_Device_Bind_req );#endif#if defined ( REFLECTOR ) ZDO_RegisterForZDOMsg( ZDAppTaskID, Bind_req ); ZDO_RegisterForZDOMsg( ZDAppTaskID, Unbind_req );#endif}
....... case 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 ); } break;#endif }}下面是ZDO_MatchEndDeviceBind()函数的源代码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; } } else //分配空间不成功 { status = ZDP_NO_ENTRY; sendRsp = TRUE; } if ( !sendRsp ) //分配空间成功 ,复制数据结构成功 {// Set into the correct state 设置正确的设备状态 matchED->state = ZDMATCH_WAIT_REQ;// Setup the timeout 设置计时时间APS_SetEndDeviceBindTimeout(AIB_MaxBindingTime, ZDO_EndDeviceBindMatchTimeoutCB ); } } else //接收到的不是第一个绑定请求 { 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 //对ed1的输出簇ID与ed2的输入簇ID进行比较,如果有符合的则会返回,相匹配的簇的数目 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 //内存空间分配不成功 { // Allocation error, stop status = ZDP_NO_ENTRY; sendRsp = TRUE; } }// Make a source match for ed2 以ed2为源 //对ed2的终端匹配请求和ed1的簇列表相比较,返回相相匹配的簇的数目 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 )) ); } else { // Allocation error, stop status = ZDP_NO_ENTRY; sendRsp = TRUE; } }
一周热门 更多>