ZIGBee组网流程
第一个功能:协调器的组网,终端设备和路由设备发现网络以及加入网络
//第一步:Z-Stack 由 main()函数开始执行,main()函数共做了 2 件事:一是系统初始化,另外一件是开始执行轮转查询式操作系统
int main( void )
{
.......
// Initialize the operating system
osal_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++ );
#endif
ZDApp_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.