Z-Statk协调器路由器终端的确定---Simple例程(二)2010-12-2410:09:42分类:嵌入式下面看一下终端结点的不同之处,下面这个图是终端结点的的编译选项,这里我们可以看到没有了编译选项SOFT_START。这里我们重点看一下不同之处。#ifdefined(ZDO_COORDINATOR)&&!defined(SOFT_START)//SetthedefaulttocoodinatordevStartModes_tdevStartMode=MODE_HARD;#elsedevStartModes_tdevStartMode=MODE_JOIN;//Assumejoining//devStartModes_tdevStartMode=MODE_RESUME;//ifalreadydirectlyjoined//toparent.SettomakethedevicedoanOrphanscan.#endif//DeviceLogicalTypeuint8zgDeviceLogicalType=DEVICE_LOGICAL_TYPE;#ifdefined(SOFT_START)#defineDEVICE_LOGICAL_TYPEZG_DEVICETYPE_SOFT#elifdefined(ZDO_COORDINATOR)#defineDEVICE_LOGICAL_TYPEZG_DEVICETYPE_COORDINATOR#elifdefined(RTR_NWK)#defineDEVICE_LOGICAL_TYPEZG_DEVICETYPE_ROUTER#else#defineDEVICE_LOGICAL_TYPEZG_DEVICETYPE_ENDDEVICE#endif最后得出zgDeviceLogicalType=DEVICE_LOGICAL_TYPE=ZG_DEVICETYPE_ENDDEVICE这里主要的不同还是在应用程序中的按键部分,下面是按键的程序,这个是在SimpleSwitch.c中voidzb_HandleKeys(uint8shift,uint8keys){uint8startOptions;uint8logicalType;//Shiftisusedtomakeeachbutton/switchdualpurpose.if(shift){if(keys&HAL_KEY_SW_1){}if(keys&HAL_KEY_SW_2){}if(keys&HAL_KEY_SW_3){}if(keys&HAL_KEY_SW_4){}}else{if(keys&HAL_KEY_SW_1){if(myAppState==APP_INIT){//Intheinitstate,keysareusedtoindicatethelogicalmode.//TheSwitchdeviceisalwaysanend-device在这里当按下S1按键时设置为终端设备logicalType=ZG_DEVICETYPE_ENDDEVICE;zb_WriteConfiguration(ZCD_NV_LOGICAL_TYPE,sizeof(uint8),&logicalType);//Domoreconfigurationifnecessaryandthenrestartdevicewithauto-startbitsetzb_ReadConfiguration(ZCD_NV_STARTUP_OPTION,sizeof(uint8),&startOptions);startOptions=ZCD_STARTOPT_AUTO_START;zb_WriteConfiguration(ZCD_NV_STARTUP_OPTION,sizeof(uint8),&startOptions);zb_SystemReset();}else{//Initiateabindingwithnulldestinationzb_BindDevice(TRUE,TOGGLE_LIGHT_CMD_ID,NULL);}}if(keys&HAL_KEY_SW_2){if(myAppState==APP_INIT){//Intheinitstate,keysareusedtoindicatethelogicalmode.//TheSwitchdeviceisalwaysanend-device按键SW2和SW1的作用是一样的,都是设置为终端设备,这里也和协调器和路由器稍微有点不同logicalType=ZG_DEVICETYPE_ENDDEVICE;zb_WriteConfiguration(ZCD_NV_LOGICAL_TYPE,sizeof(uint8),&logicalType);zb_ReadConfiguration(ZCD_NV_STARTUP_OPTION,sizeof(uint8),&startOptions);startOptions=ZCD_STARTOPT_AUTO_START;zb_WriteConfiguration(ZCD_NV_STARTUP_OPTION,sizeof(uint8),&startOptions);zb_SystemReset();}else{//Sendthecommandtotogglelightzb_SendDataRequest(0xFFFE,TOGGLE_LIGHT_CMD_ID,0,(uint8*)NULL,myAppSeqNumber,0,0);}}if(keys&HAL_KEY_SW_3){//Removeallexistingbindingszb_BindDevice(FALSE,TOGGLE_LIGHT_CMD_ID,NULL);}if(keys&HAL_KEY_SW_4){}}}从上面可以看出KEY1KEY2都是作为终端设备的。而在函数中也并没有改变上面的变量的类型。下面的这个函数,在函数uint8ZDOInitDevice(uint16startDelay)调用的,上面在分析协调器的时,也有看到,这个函数在这里就直接返回,并没有起到什么作用。voidZDAppDetermineDeviceType(void){if(zgDeviceLogicalType==ZG_DEVICETYPE_ENDDEVICE)return;#ifdefined(SOFT_START)if(zgDeviceLogicalType==ZG_DEVICETYPE_COORDINATOR){devStartMode=MODE_HARD;//StartasacoordinatorZDO_Config_Node_Descriptor.LogicalType=NODETYPE_COORDINATOR;}else{if(zgDeviceLogicalType==ZG_DEVICETYPE_ROUTER){softStartAllowCoord=FALSE;//Don'tallowcoordtostartcontinueJoining=TRUE;}devStartMode=MODE_JOIN;//Assumejoining}#endif//SOFT_START}UINT16ZDApp_event_loop(bytetask_id,UINT16events){if(events&ZDO_NETWORK_INIT){//InitializeappsandstartthenetworkdevState=DEV_INIT;ZDO_StartDevice((uint8)ZDO_Config_Node_Descriptor.LogicalType,devStartMode,DEFAULT_BEACON_ORDER,DEFAULT_SUPERFRAME_ORDER);//Returnunprocessedeventsreturn(events^ZDO_NETWORK_INIT);}}在ZDO_StartDevice()函数中可以根据不同的选项来进行操作。voidZDO_StartDevice(bytelogicalType,devStartModes_tstartMode,bytebeaconOrder,bytesuperframeOrder){ZStatus_tret;ret=ZUnsupportedMode;#ifdefined(ZDO_COORDINATOR)if(logicalType==NODETYPE_COORDINATOR){if(startMode==MODE_HARD){devState=DEV_COORD_STARTING;//调用网络的格式化函数形成网络ret=NLME_NetworkFormationRequest(zgConfigPANID,zgDefaultChannelList,zgDefaultStartingScanDuration,beaconOrder,superframeOrder,false);}elseif(startMode==MODE_RESUME){//JuststartthecoordinatordevState=DEV_COORD_STARTING;ret=NLME_StartRouterRequest(beaconOrder,beaconOrder,false);}else{#ifdefined(LCD_SUPPORTED)HalLcdWriteScreen(StartDeviceERR,MODEunknown);#endif}}#endif//!ZDO_COORDINATOR#if!defined(ZDO_COORDINATOR)||defined(SOFT_START)if(logicalType==NODETYPE_ROUTER||logicalType==NODETYPE_DEVICE){if((startMode==MODE_JOIN)||(startMode==MODE_REJOIN)){devState=DEV_NWK_DISC;#ifdefined(MANAGED_SCAN)ZDOManagedScan_Next();ret=NLME_NetworkDiscoveryRequest(managedScanChannelMask,BEACON_ORDER_15_MSEC);#elseret=NLME_NetworkDiscoveryRequest(zgDefaultChannelList,zgDefaultStartingScanDuration);#endif}elseif(startMode==MODE_RESUME){if(logicalType==NODETYPE_ROUTER){ZMacScanCnf_tscanCnf;devState=DEV_NWK_ORPHAN;/*ifrouterandnvramisavailable,fakesuccessfulorphanscan*/scanCnf.hdr.Status=ZSUCCESS;scanCnf.ScanType=ZMAC_ORPHAN_SCAN;scanCnf.UnscannedChannels=0;scanCnf.ResultListSize=0;nwk_ScanJoiningOrphan(&scanCnf);ret=ZSuccess;}else{devState=DEV_NWK_ORPHAN;ret=NLME_OrphanJoinRequest(zgDefaultChannelList,zgDefaultStartingScanDuration);}}else{#ifdefined(LCD_SUPPORTED)HalLcdWriteScreen(StartDeviceERR,MODEunknown);#endif}}#endif//!ZDOCOORDINATOR||SOFT_STARTif(ret!