ZDO_STATE_CHANGE 只有当设备角色产生改变后才会触发,才干原告诉COOR你的收集中有一个设备变了角色。zigbee收集启动流程 ---- 和谐器main()->osal_init_system()->osalInitTasks()->ZDApp_Init()进入ZDApp_Init()函数:void ZDApp_Init( uint8 task_id ){ZDAppTaskID = task_id;// Initialize the ZDO global device short address storageZDAppNwkAddr.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 ){ZDOInitDevice( 0 );}else{// Blink LED to indicate HOLD_STARTHalLedBlink ( HAL_LED_4, 0, 50, 500 );}ZDApp_RegisterCBs();}和谐器不编译HOLD_AUTO_START,也不手工设置SW_1,初始化devState = DEV_INIT(拜见基本成绩阐明3).因此直接在ZDApp_Init()中进入ZDOInitDevice( 0 )开启设备.uint8 ZDOInitDevice( uint16 startDelay ){uint8 networkStateNV = ZDO_INITDEV_NEW_NETWORK_STATE;uint16 extendedDelay = 0;if ( devState == DEV_HOLD ){// Initialize the RAM items table, in case an NV item has been updated.zgInitItems( FALSE );}ZDConfig_InitDescriptors();//devtag.071807.todo - fix this temporary solution_NIB.CapabilityFlags = ZDO_Config_Node_Descriptor.CapabilityFlags;devState = DEV_INIT; // Remove the Hold state// Initialize leave control logic 函数读取NV项目ZCD_NV_LEAVE_CTRL的值,ZDApp_LeaveCtrl指向这个值ZDApp_LeaveCtrlInit();// Check leave control reset settingsZDApp_LeaveCtrlStartup( &devState, &startDelay );// Leave may make the hold state come back以上两个函数设置了对设备分开时的把持,假若有延时则延时,不则把设备状况设为DEV_HOLDif ( devState == DEV_HOLD ){// Set the NV startup option to force a "new" join.zgWriteStartupOptions( ZG_STARTUP_SET, ZCD_STARTOPT_DEFAULT_NETWORK_STATE );// Notify the applicationsosal_set_event( ZDAppTaskID, ZDO_STATE_CHANGE_EVT );return ( ZDO_INITDEV_LEAVE_NOT_STARTED ); // Don't join - (one time).}#if defined ( NV_RESTORE )// Get Keypad directly to see if a reset nv is needed.// Hold down the SW_BYPASS_NV key (defined in OnBoard.h)// while booting to skip past NV Restore.if ( HalKeyRead() == SW_BYPASS_NV )networkStateNV = ZDO_INITDEV_NEW_NETWORK_STATE;else{// Determine if NV should be restorednetworkStateNV = ZDApp_ReadNetworkRestoreState();}if ( networkStateNV == ZDO_INITDEV_RESTORED_NETWORK_STATE ){networkStateNV = ZDApp_RestoreNetworkState();}else{// Wipe out the network state in NVNLME_InitNV();NLME_SetDefaultNV();// clear NWK key valuesZDSecMgrClearNVKeyValues();}#endifif ( networkStateNV == ZDO_INITDEV_NEW_NETWORK_STATE ){ZDAppDetermineDeviceType();// Only delay if joining network - not restoring network stateextendedDelay = (uint16)((NWK_START_DELAY + startDelay)+ (osal_rand() & EXTENDED_JOINING_RANDOM_MASK));}// Initialize the security for type of deviceZDApp_SecInit( networkStateNV );// Trigger the network startZDApp_NetworkInit( extendedDelay );// set broadcast address mask to support broadcast filteringNLME_SetBroadcastFilter( ZDO_Config_Node_Descriptor.CapabilityFlags );return ( networkStateNV );}注:(1)自启动形式下SampleApp的终端跟路由器总体流程基本分歧、(2)以SampleApp为例,ZDO_StartDevice()函数的两个重要参数比较:终端:ZDO_Config_Node_Descriptor.LogicalType = NODETYPE_DEVICEdevStartMode = MODE_JOIN路由器:ZDO_Config_Node_Descriptor.LogicalType = NODETYPE_ROUTERdevStartMode = MODE_JOIN和谐器:ZDO_Config_Node_Descriptor.LogicalType = NODETYPE_COORDINATORdevStartMode = MODE_HARD。