【转】ZIGBee组网流程

2019-07-20 17:41发布

第一个功能:协调器的组网,终端设备和路由设备发现网络以及加入网络
//第一步: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 step
void 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 );
    }
  }
......
}

友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
15条回答
Snow7
1楼-- · 2019-07-22 06:35
楼主组网成功了?牛人啊
angerbird
2楼-- · 2019-07-22 07:49
 精彩回答 2  元偷偷看……
smilingangel
3楼-- · 2019-07-22 08:12
组网流程是需要注意的,特涅是组网协议规范的

一周热门 更多>