ZigBee无线通信网络软件毕业设计 - 图文 下载本文

if ( (startMode == MODE_JOIN) || (startMode == MODE_REJOIN) ) {

devState = DEV_NWK_DISC;

#if defined( MANAGED_SCAN ) ZDOManagedScan_Next();

ret = NLME_NetworkDiscoveryRequest( managedScanChannelMask, BEACON_ORDER_15_MSEC );

#else

ret = NLME_NetworkDiscoveryRequest( zgDefaultChannelList, zgDefaultStartingScanDuration );

#endif } …… else {

#if defined( LCD_SUPPORTED )

HalLcdWriteScreen( \#endif

HalUARTWrite(HAL_UART_PORT_0,\ } }

#endif //!ZDO COORDINATOR || SOFT_START

if ( ret != ZSuccess )

osal_start_timerEx(ZDAppTaskID, ZDO_NETWORK_INIT, NWK_RETRY_DELAY );

} ② 发现网络确认:

ZStatus_t ZDO_NetworkDiscoveryConfirmCB( byte ResultCount,

networkDesc_t *NetworkList ) {

??

ZDApp_SendMsg( ZDAppTaskID, ZDO_NWK_DISC_CNF, sizeof(ZDO_NetworkDiscoveryCfm_t), (byte *)&msg );

return (ZSuccess);

} // ZDO_NetworkDiscoveryConfirmCB

③ 处理ZDO层处理ZDO_NWK_DISC_CNF消息,调用NLME_JoinRequest: void ZDApp_ProcessOSALMsg( osal_event_hdr_t *msgPtr ) { ??

case ZDO_NWK_DISC_CNF:

if (devState != DEV_NWK_DISC)

24

{ }

#if !defined ( ZDO_COORDINATOR ) || defined ( SOFT_START ) #if defined ( MANAGED_SCAN )

else if ( (((ZDO_NetworkDiscoveryCfm_t *)msgPtr)->hdr.status == ZDO_SUCCESS) && (zdoDiscCounter > NUM_DISC_ATTEMPTS) )

#else

else if ( (((ZDO_NetworkDiscoveryCfm_t *)msgPtr)->hdr.status == ZDO_SUCCESS) && (zdoDiscCounter++ > NUM_DISC_ATTEMPTS) )

#endif {

if ( devStartMode == MODE_JOIN ) //加入模式 {

devState = DEV_NWK_JOINING;

ZDApp_NodeProfileSync((ZDO_NetworkDiscoveryCfm_t *)msgPtr);

if ( NLME_JoinRequest( ((ZDO_NetworkDiscoveryCfm_t *)msgPtr)->extendedPANID,

BUILD_UINT16( ((ZDO_NetworkDiscoveryCfm_t

*)msgPtr)->panIdLSB, ((ZDO_NetworkDiscoveryCfm_t *)msgPtr)->panIdMSB ),

((ZDO_NetworkDiscoveryCfm_t *)msgPtr)->logicalChannel, ZDO_Config_Node_Descriptor.CapabilityFlags ) != ZSuccess ) {

ZDApp_NetworkInit( (uint16)(NWK_START_DELAY + ((uint16)(osal_rand()& EXTENDED_JOINING_RANDOM_MASK))) );

} }

else if ( devStartMode == MODE_REJOIN ) //重新加入模式 {

devState = DEV_NWK_REJOIN;

if ( NLME_ReJoinRequest() != ZSuccess ) {

ZDApp_NetworkInit( (uint16)(NWK_START_DELAY + ((uint16)(osal_rand()& EXTENDED_JOINING_RANDOM_MASK))) );

} }

if ( ZDO_Config_Node_Descriptor.CapabilityFlags & CAPINFO_RCVR_ON_IDLE )

{

// The receiver is on, turn network layer polling off.

25

NLME_SetPollRate( 0 );

NLME_SetQueuedPollRate( 0 ); NLME_SetResponseRate( 0 ); } } else {

#if defined ( SOFT_START ) && !defined ( VIRTKEY_SOFT_START ) #if defined ( MANAGED_SCAN ) if ( (softStartAllowCoord)

&& (((ZDO_NetworkDiscoveryCfm_t *)msgPtr)->hdr.status != ZDO_SUCCESS )

&& (zdoDiscCounter > NUM_DISC_ATTEMPTS) ) #else

if ( (softStartAllowCoord)

&& (((ZDO_NetworkDiscoveryCfm_t *)msgPtr)->hdr.status != ZDO_SUCCESS )

&& (zdoDiscCounter++ > NUM_DISC_ATTEMPTS) ) #endif {

ZDO_Config_Node_Descriptor.LogicalType = NODETYPE_COORDINATOR; devStartMode = MODE_HARD; //将启动模式该改变成硬件模式 }

else if ( continueJoining == FALSE ) {

devState = DEV_HOLD;

osal_stop_timerEx( ZDAppTaskID, ZDO_NETWORK_INIT ); break; // Don't init } #endif

#if defined ( MANAGED_SCAN )

ZDApp_NetworkInit( MANAGEDSCAN_DELAY_BETWEEN_SCANS ); #else

if ( continueJoining ) {

ZDApp_NetworkInit( (uint16)(BEACON_REQUEST_DELAY

+ ((uint16)(osal_rand()& BEACON_REQ_DELAY_MASK))) ); } #endif }

#endif // !ZDO_COORDINATOR break; ?? }

26

④ 加入网络确认,发送ZDO_NWK_DISC_CNF消息: void ZDO_JoinConfirmCB( uint16 PanId, ZStatus_t Status ) {

??

// Notify ZDApp

ZDApp_SendMsg( ZDAppTaskID, ZDO_NWK_JOIN_IND, sizeof(osal_event_hdr_t), (byte*)NULL );

}

⑤ ZDApp处理网络加入事件

void ZDApp_ProcessNetworkJoin( void )

{

if ( (devState == DEV_NWK_JOINING) ||

((devState == DEV_NWK_ORPHAN) &&

(ZDO_Config_Node_Descriptor.LogicalType == NODETYPE_ROUTER)) ) {

// Result of a Join attempt by this device. if ( nwkStatus == ZSuccess ) {

osal_set_event( ZDAppTaskID, ZDO_STATE_CHANGE_EVT ); //设置网络状态改变事件

?? } ?? }

(2)节点的绑定

传感节点可以和对应的控制节点进行绑定,具体的光照传感节点能够绑定LED台灯节点,湿度传感节点能够绑定水泵节点,二氧化碳传感节点能够绑定卷帘节点。已经绑定的节点能够进行“自动控制”,也就是通过传感器的值来控制相应节点的动作。

绑定原理说明:

绑定是一种控制两个或者多个设备应用层之间信息流传递的机制。在ZigBee2006发布版本中,它被称为源绑定,所有的设备都可以执行绑定机制。 绑定允许应用程序发送一个数据包而不需要知道目标设备的短地址。应用支持子层(APS)从它的绑定表中确定目标设备的短地址,然后将数据发送给目标应用或者目标组。如果在绑定表中找到的短地址不止一个,协议栈会向所有找到的短地址发送数据。

绑定只能在互为\补充的\设备间被创建。也就是说,当两个设备已经在他们

27