zigbee组网过程
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
}
协调器建网
ZDObject.c
voidZDO_StartDevice( bytelogicalType,devStartModes_tstartMode, bytebeaconOrder, bytesuperframeOrder)
{
ZDApp_ChangeState(DEV_COORD_STARTING);
}
OSAL_SampleLight.c
voidosalInitTasks( void )
{
uint8taskID= 0;
ZDApp_Init(taskID++ );
zcl_Init(taskID++ );
zclSampleLight_Init(taskID++ );
}
ZDApp.c
voidZDApp_Init( uint8task_id)
if( (zclSampleLight_NwkState== DEV_ZB_COORD) ||
(zclSampleLight_NwkState== DEV_ROUTER) ||
(zclSampleLight_NwkState== DEV_END_DEVICE) )
{
#ifdefLED
if(IsOffline)
}
ZDApp.c
UINT16ZDApp_event_loop(bytetask_id, UINT16 events )
{
……
if ( events &ZDO_NETWORK_START)//网络启动事件处理
{
ZDApp_NetworkStartEvt();
}
}
ZDApp.c
voidZDApp_NetworkStartEvt( void )
intmain( void )
{
// Initialize the operating system
osal_init_system(); //初始化操作系统
...
}
OSAL.c
uint8osal_init_system(void )
{
// Initialize the system tasks.
osalInitTasks(); //初始化任务
{
osal_set_event(ZDAppTaskID,ZDO_STATE_CHANGE_EVT);
NLME_StartRouterRequest(0, 0, false );
BUILD_UINT16(((ZDO_NetworkDiscoveryCfm_t*)msgPtr)->panIdLSB,
((ZDO_NetworkDiscoveryCfm_t*)msgPtr)->panIdMSB),
((ZDO_NetworkDiscoveryCfm_t*)msgPtr)->logicalChannel,
}
else
{
osal_set_event(ZDAppTaskID,ZDO_NETWORK_INIT);
}
}
ZDApp.c
UINT16ZDApp_event_loop(uint8task_id, UINT16 events )
{
if ( events &ZDO_NETWORK_INIT)//网络初始化事件处理
初始化该设备新的网络状态
#if defined( NV_RESTORE )
networkStateNV=ZDApp_ReadNetworkRestoreState();
//读出存储的网络状态
if(networkStateNV== ZDO_INITDEV_RESTORED_NETWORK_STATE )
{
ZDAppTaskID=task_id;
ZDAppNwkAddr.addrMode= Addr16Bit;//地址模式为16bit短地址
ZDAppNwkAddr.addr.shortAddr= INVALID_NODE_ADDR;//短地址=0xFFFE
(void)NLME_GetExtAddr(); //API函数,用于得到64bit IEEE地址
{
if( (devState== DEV_NWK_JOINING) || ((devState== DEV_NWK_ORPHAN) &&
(ZDO_Config_Node_Descriptor.LogicalType== NODETYPE_ROUTER)) )
{
if(nwkStatus==ZSuccess)
}
当网络层执行NLME_NetworkFormationRequest()建立网络后,将给予ZDO层反馈信息:
ZDApp.c
voidZDO_NetworkFormationConfirmCB(ZStatus_tStatus)
{
osal_set_event(ZDAppTaskID,ZDO_NETWORK_START);
{
while((MSGpkt=(afIncomingMSGPacket_t*)osal_msg_receive(zclSampleLight_TaskID)) )
{
switch(MSGpkt->hdr.event)
{
caseZDO_STATE_CHANGE:
zclSampleLight_NwkState= (devStates_t)(MSGpkt->hdr.status);
{
ZDOInitDevice(0 );
}
ZDApp_RegisterCBs();
//注册ZDO消息,只有注册了的消息才能以ZDO_CB_MSG消息的形式发送给指定的任务
}
ZDApp.c
uint8ZDOInitDevice( uint16startDelay)
{
uint8networkStateNV= ZDO_INITDEV_NEW_NETWORK_STATE;//0x01,
{
ZDApp_SendMsg(ZDAppTaskID,ZDO_NWK_DISC_CNF,sizeof(ZDO_NetworkDiscoveryCfm_t), (byte *)&msg);
}
ZDApp.c
voidZDApp_ProcessOSALMsg(osal_event_hdr_t*msgPtr)
{
networkStateNV=ZDApp_RestoreNetworkState();
runtimeChannel= (uint32) (1L << _NIB.nwkLogicalChannel);
}
devState= DEV_INIT; eType();//确定该设备的类型
{
ZDApp_SetParentAnnceTimer();
}
}
osal_pwrmgr_device(PWRMGR_ALWAYS_ON );
}
}
ZDApp.c
UINT16ZDApp_event_loop(bytetask_id, UINT16 events )
{
if ( events &ZDO_STATE_CHANGE_EVT)//ZDO状态改变事件
}
应用层初始化
zcl_samplelight.c
voidzclSampleLight_Init( bytetask_id)
{
zclSampleLight_TaskID=task_id;
//Thisapp is part of the Home Automation Profile
zclHA_Init( &zclSampleLight_SimpleDesc);//初始化描述符
ZDApp_NetworkInit(extendedDelay);//开始形成网络
}
ZDApp.c
voidZDApp_NetworkInit( uint16 delay )
{
if( delay )
{
osal_start_timerEx(ZDAppTaskID,ZDO_NETWORK_INIT, delay );
ret=NLME_NetworkFormationRequest(zgConfigPANID,zgApsUseExtendedPANID,runtimeChannel,
zgDefaultStartingScanDuration,beaconOrder,
superframeOrder, false );
afRegister( (endPointDesc_t*)&ZDApp_epDesc); //为设备注册端点0描述符
ZDO_Init();//初始化ZDO物件,并且设置设备
if(devState!= DEV_HOLD ) //无HOLD_AUTO_START此项预编译,所以devState= DEV_INIT
………..
afRegister( &ota_SysAppEp);//注册描述符
………..
}
zcl_samplelight.c
uint16zclSampleLight_event_loop(uint8task_id, uint16 events )
{
if( events & SYS_EVENT_MSG )
{
switch(msgPtr->event )
{
caseZDO_NWK_DISC_CNF:
ZDApp_ChangeState(DEV_NWK_JOINING );
NLME_JoinRequest(((ZDO_NetworkDiscoveryCfm_t*)msgPtr)->extendedPANID,
{
ZDO_UpdateNwkStatus(devState);
}
}
ZDObject.c
voidZDO_UpdateNwkStatus(devStates_tstate )
{
ZDAppNwkAddr.addr.shortAddr=NLME_GetShortAddr();
(void)NLME_GetExtAddr(); // Load thesaveExtAddrpointer.
{
devState= DEV_NWK_DISC;
ret=NLME_NetworkDiscoveryRequest(zgDefaultChannelList,zgDefaultStartingScanDuration);
}
当网络层执行加入网络后,将给予ZDO层反馈信息:
ZDApp.c
ZStatus_tZDO_NetworkDiscoveryConfirmCB(byteResultCount,networkDesc_t*NetworkList)
{
IsOffline= false;
osal_stop_timerEx(zclSampleLight_TaskID, SAMPLELIGHT_OFFLINE_EVT);
HAL_TURN_ON_LED1();
}
路由器入网
ZDObject.c
voidZDO_StartDevice( bytelogicalType,devStartModes_tstartMode, bytebeaconOrder, bytesuperframeOrder)
}
ZDApp.c
voidZDApp_ProcessOSALMsg(osal_event_hdr_t*msgPtr)
{
switch(msgPtr->event )
{
……
caseZDO_NWK_JOIN_IND:
ZDApp_ProcessNetworkJoin();
break;
}
}
ZDApp.c
voidZDApp_ProcessNetworkJoin( void )
{
ZDApp_ChangeState(DEV_INIT );
ZDO_StartDevice((uint8)ZDO_Config_Node_Descriptor.LogicalType,devStartMode,
DEFAULT_BEACON_ORDER, DEFAULT_SUPERFRAME_ORDER );
{
if(nwkStatus==ZSuccess)
{
if(devState== DEV_COORD_STARTING )
{
ZDApp_NwkWriteNVRequest();
ZDApp_ChangeState( DEV_ZB_COORD );//ZDO状态改变事件
if(zgChildAgingEnable== TRUE )
ZDO_Config_Node_Descriptor.CapabilityFlags)
}
}
ZDApp.c
voidZDO_JoinConfirmCB( uint16PanId,ZStatus_tStatus )???????????
{
ZDApp_SendMsg(ZDAppTaskID, ZDO_NWK_JOIN_IND,sizeof(osal_event_hdr_t), (byte*)NULL );
协调器建网
ZDObject.c
voidZDO_StartDevice( bytelogicalType,devStartModes_tstartMode, bytebeaconOrder, bytesuperframeOrder)
{
ZDApp_ChangeState(DEV_COORD_STARTING);
}
OSAL_SampleLight.c
voidosalInitTasks( void )
{
uint8taskID= 0;
ZDApp_Init(taskID++ );
zcl_Init(taskID++ );
zclSampleLight_Init(taskID++ );
}
ZDApp.c
voidZDApp_Init( uint8task_id)
if( (zclSampleLight_NwkState== DEV_ZB_COORD) ||
(zclSampleLight_NwkState== DEV_ROUTER) ||
(zclSampleLight_NwkState== DEV_END_DEVICE) )
{
#ifdefLED
if(IsOffline)
}
ZDApp.c
UINT16ZDApp_event_loop(bytetask_id, UINT16 events )
{
……
if ( events &ZDO_NETWORK_START)//网络启动事件处理
{
ZDApp_NetworkStartEvt();
}
}
ZDApp.c
voidZDApp_NetworkStartEvt( void )
intmain( void )
{
// Initialize the operating system
osal_init_system(); //初始化操作系统
...
}
OSAL.c
uint8osal_init_system(void )
{
// Initialize the system tasks.
osalInitTasks(); //初始化任务
{
osal_set_event(ZDAppTaskID,ZDO_STATE_CHANGE_EVT);
NLME_StartRouterRequest(0, 0, false );
BUILD_UINT16(((ZDO_NetworkDiscoveryCfm_t*)msgPtr)->panIdLSB,
((ZDO_NetworkDiscoveryCfm_t*)msgPtr)->panIdMSB),
((ZDO_NetworkDiscoveryCfm_t*)msgPtr)->logicalChannel,
}
else
{
osal_set_event(ZDAppTaskID,ZDO_NETWORK_INIT);
}
}
ZDApp.c
UINT16ZDApp_event_loop(uint8task_id, UINT16 events )
{
if ( events &ZDO_NETWORK_INIT)//网络初始化事件处理
初始化该设备新的网络状态
#if defined( NV_RESTORE )
networkStateNV=ZDApp_ReadNetworkRestoreState();
//读出存储的网络状态
if(networkStateNV== ZDO_INITDEV_RESTORED_NETWORK_STATE )
{
ZDAppTaskID=task_id;
ZDAppNwkAddr.addrMode= Addr16Bit;//地址模式为16bit短地址
ZDAppNwkAddr.addr.shortAddr= INVALID_NODE_ADDR;//短地址=0xFFFE
(void)NLME_GetExtAddr(); //API函数,用于得到64bit IEEE地址
{
if( (devState== DEV_NWK_JOINING) || ((devState== DEV_NWK_ORPHAN) &&
(ZDO_Config_Node_Descriptor.LogicalType== NODETYPE_ROUTER)) )
{
if(nwkStatus==ZSuccess)
}
当网络层执行NLME_NetworkFormationRequest()建立网络后,将给予ZDO层反馈信息:
ZDApp.c
voidZDO_NetworkFormationConfirmCB(ZStatus_tStatus)
{
osal_set_event(ZDAppTaskID,ZDO_NETWORK_START);
{
while((MSGpkt=(afIncomingMSGPacket_t*)osal_msg_receive(zclSampleLight_TaskID)) )
{
switch(MSGpkt->hdr.event)
{
caseZDO_STATE_CHANGE:
zclSampleLight_NwkState= (devStates_t)(MSGpkt->hdr.status);
{
ZDOInitDevice(0 );
}
ZDApp_RegisterCBs();
//注册ZDO消息,只有注册了的消息才能以ZDO_CB_MSG消息的形式发送给指定的任务
}
ZDApp.c
uint8ZDOInitDevice( uint16startDelay)
{
uint8networkStateNV= ZDO_INITDEV_NEW_NETWORK_STATE;//0x01,
{
ZDApp_SendMsg(ZDAppTaskID,ZDO_NWK_DISC_CNF,sizeof(ZDO_NetworkDiscoveryCfm_t), (byte *)&msg);
}
ZDApp.c
voidZDApp_ProcessOSALMsg(osal_event_hdr_t*msgPtr)
{
networkStateNV=ZDApp_RestoreNetworkState();
runtimeChannel= (uint32) (1L << _NIB.nwkLogicalChannel);
}
devState= DEV_INIT; eType();//确定该设备的类型
{
ZDApp_SetParentAnnceTimer();
}
}
osal_pwrmgr_device(PWRMGR_ALWAYS_ON );
}
}
ZDApp.c
UINT16ZDApp_event_loop(bytetask_id, UINT16 events )
{
if ( events &ZDO_STATE_CHANGE_EVT)//ZDO状态改变事件
}
应用层初始化
zcl_samplelight.c
voidzclSampleLight_Init( bytetask_id)
{
zclSampleLight_TaskID=task_id;
//Thisapp is part of the Home Automation Profile
zclHA_Init( &zclSampleLight_SimpleDesc);//初始化描述符
ZDApp_NetworkInit(extendedDelay);//开始形成网络
}
ZDApp.c
voidZDApp_NetworkInit( uint16 delay )
{
if( delay )
{
osal_start_timerEx(ZDAppTaskID,ZDO_NETWORK_INIT, delay );
ret=NLME_NetworkFormationRequest(zgConfigPANID,zgApsUseExtendedPANID,runtimeChannel,
zgDefaultStartingScanDuration,beaconOrder,
superframeOrder, false );
afRegister( (endPointDesc_t*)&ZDApp_epDesc); //为设备注册端点0描述符
ZDO_Init();//初始化ZDO物件,并且设置设备
if(devState!= DEV_HOLD ) //无HOLD_AUTO_START此项预编译,所以devState= DEV_INIT
………..
afRegister( &ota_SysAppEp);//注册描述符
………..
}
zcl_samplelight.c
uint16zclSampleLight_event_loop(uint8task_id, uint16 events )
{
if( events & SYS_EVENT_MSG )
{
switch(msgPtr->event )
{
caseZDO_NWK_DISC_CNF:
ZDApp_ChangeState(DEV_NWK_JOINING );
NLME_JoinRequest(((ZDO_NetworkDiscoveryCfm_t*)msgPtr)->extendedPANID,
{
ZDO_UpdateNwkStatus(devState);
}
}
ZDObject.c
voidZDO_UpdateNwkStatus(devStates_tstate )
{
ZDAppNwkAddr.addr.shortAddr=NLME_GetShortAddr();
(void)NLME_GetExtAddr(); // Load thesaveExtAddrpointer.
{
devState= DEV_NWK_DISC;
ret=NLME_NetworkDiscoveryRequest(zgDefaultChannelList,zgDefaultStartingScanDuration);
}
当网络层执行加入网络后,将给予ZDO层反馈信息:
ZDApp.c
ZStatus_tZDO_NetworkDiscoveryConfirmCB(byteResultCount,networkDesc_t*NetworkList)
{
IsOffline= false;
osal_stop_timerEx(zclSampleLight_TaskID, SAMPLELIGHT_OFFLINE_EVT);
HAL_TURN_ON_LED1();
}
路由器入网
ZDObject.c
voidZDO_StartDevice( bytelogicalType,devStartModes_tstartMode, bytebeaconOrder, bytesuperframeOrder)
}
ZDApp.c
voidZDApp_ProcessOSALMsg(osal_event_hdr_t*msgPtr)
{
switch(msgPtr->event )
{
……
caseZDO_NWK_JOIN_IND:
ZDApp_ProcessNetworkJoin();
break;
}
}
ZDApp.c
voidZDApp_ProcessNetworkJoin( void )
{
ZDApp_ChangeState(DEV_INIT );
ZDO_StartDevice((uint8)ZDO_Config_Node_Descriptor.LogicalType,devStartMode,
DEFAULT_BEACON_ORDER, DEFAULT_SUPERFRAME_ORDER );
{
if(nwkStatus==ZSuccess)
{
if(devState== DEV_COORD_STARTING )
{
ZDApp_NwkWriteNVRequest();
ZDApp_ChangeState( DEV_ZB_COORD );//ZDO状态改变事件
if(zgChildAgingEnable== TRUE )
ZDO_Config_Node_Descriptor.CapabilityFlags)
}
}
ZDApp.c
voidZDO_JoinConfirmCB( uint16PanId,ZStatus_tStatus )???????????
{
ZDApp_SendMsg(ZDAppTaskID, ZDO_NWK_JOIN_IND,sizeof(osal_event_hdr_t), (byte*)NULL );