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

层(应用层)进行启动进行确认,至此协调器的整个启动过程完成。其中详细代码如下:

? ZDO任务初始化:

void ZDApp_Init( byte task_id ) {

uint8 capabilities;

// Save the task ID

ZDAppTaskID = task_id;

// Initialize the ZDO global device short address storage

ZDAppNwkAddr.addrMode = Addr16Bit;//地址模式为16bit短地址

ZDAppNwkAddr.addr.shortAddr = INVALID_NODE_ADDR;//短地址=0xFFFE (void)NLME_GetExtAddr(); // Load the saveExtAddr pointer,API函数-》得到64bit IEEE地址

// Check for manual \

ZDAppCheckForHoldKey();//用户可以通过S5手动制止该设备自动启动

// 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 );//为设备注册端点0描述符

#if defined( ZDO_USERDESC_RESPONSE ) ZDApp_InitUserDesc();

#endif // ZDO_USERDESC_RESPONSE

// set broadcast address mask to support broadcast filtering

NLME_GetRequest(nwkCapabilityInfo, 0, &capabilities);//获得网络信息库的属性值-网络容量

NLME_SetBroadcastFilter( capabilities );//根据网络容量设置掩码,用于处理有效的广播地址

// Start the device?

if ( devState != DEV_HOLD )//根据上面函数ZDAppCheckForHoldKey()决定 {

ZDOInitDevice( 0 ); } else {

// Blink LED to indicate HOLD_START HalLedBlink ( HAL_LED_4, 0, 50, 500 ); }

ZDApp_RegisterCBs(); } /* ZDO_Init() */

16

? ZDO启动设备:

uint8 ZDOInitDevice( uint16 startDelay ) {

uint8 networkStateNV = ZDO_INITDEV_NEW_NETWORK_STATE;//0x01,初始化该设备新的网络状态

uint16 extendedDelay = 0;

devState = DEV_INIT; // Remove the Hold state

//not connected to anything,无连接

// Initialize leave control logic ZDApp_LeaveCtrlInit();

// Check leave control reset settings

ZDApp_LeaveCtrlStartup( &devState, &startDelay );

// Leave may make the hold state come back if ( devState == DEV_HOLD )

return ( ZDO_INITDEV_LEAVE_NOT_STARTED ); // Don't join - (one time).

#if defined ( NV_RESTORE )//project->Options->C/C++ complier->defined symbols->xNV_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 restored

networkStateNV = ZDApp_ReadNetworkRestoreState(); }

if ( networkStateNV == ZDO_INITDEV_RESTORED_NETWORK_STATE ) {

networkStateNV = ZDApp_RestoreNetworkState(); } else {

// Wipe out the network state in NV NLME_InitNV();

NLME_SetDefaultNV(); } #endif

17

if ( networkStateNV == ZDO_INITDEV_NEW_NETWORK_STATE ) {

ZDAppDetermineDeviceType();//确定该设备的类型

// Only delay if joining network - not restoring network state

extendedDelay = (uint16)((NWK_START_DELAY + startDelay)

+ (osal_rand() & EXTENDED_JOINING_RANDOM_MASK));//加入网络的时延

}

// Initialize device security

ZDApp_SecInit( networkStateNV );//初始化设备的安全属性

// Trigger the network start

ZDApp_NetworkInit( extendedDelay );//开始形成网络。任务:ZDAppTaskID, 事件:ZDO_NETWORK_INIT

return ( networkStateNV ); }

? ZDO初始化网络:

void ZDApp_NetworkInit( uint16 delay ) {

if ( delay ) {

// Wait awhile before starting the device

osal_start_timerEx( ZDAppTaskID, ZDO_NETWORK_INIT, delay ); } else {

osal_set_event( ZDAppTaskID, ZDO_NETWORK_INIT );//ZDO_NETWORK_INIT事件处理函数查看ZDApp_event_loop()

} } ④ 处理ZDO网络初始化事件

UINT16 ZDApp_event_loop( byte task_id, UINT16 events ) {

……

if ( events & ZDO_NETWORK_INIT )//网络初始化事件处理 {

// Initialize apps and start the network devState = DEV_INIT;

ZDO_StartDevice( (uint8)ZDO_Config_Node_Descriptor.LogicalType, devStartMode, DEFAULT_BEACON_ORDER,

18

DEFAULT_SUPERFRAME_ORDER );//设备逻辑类型,启动模式,信标时间,超帧长度

//HalLedSet (HAL_LED_1, HAL_LED_MODE_ON);

// Return unprocessed events

return (events ^ ZDO_NETWORK_INIT); } …… } ⑤ ZDO启动设备

void ZDO_StartDevice( byte logicalType, devStartModes_t startMode, byte beaconOrder, byte superframeOrder )

{

……

#if defined(ZDO_COORDINATOR)

if ( logicalType == NODETYPE_COORDINATOR ) {

if ( startMode == MODE_HARD ) {

devState = DEV_COORD_STARTING;

ret = NLME_NetworkFormationRequest( zgConfigPANID, zgDefaultChannelList, zgDefaultStartingScanDuration, beaconOrder,

superframeOrder, false );//0xFFFF,0x00000800,5,0,0,false

} …… } ⑥ 建立网络确认

void ZDO_NetworkFormationConfirmCB( ZStatus_t Status ) {

#if defined(ZDO_COORDINATOR)

……

osal_set_event( ZDAppTaskID, ZDO_NETWORK_START ); #endif //ZDO_COORDINATOR }

⑦ 处理ZDO网络启动事件

void ZDApp_NetworkStartEvt( void )

{

if ( nwkStatus == ZSuccess ) {

19