⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nwkp.nc

📁 tinyos-2.x.rar
💻 NC
📖 第 1 页 / 共 4 页
字号:
				route_destination_address[1]=neighbortable[next_hop_index-1].Network_Address;
			}
			//send the data																																	//ack
			call MCPS_DATA.request(SHORT_ADDRESS, panid, srcadd, SHORT_ADDRESS, panid, route_destination_address, (NsduLength + 8), MSDU,1,set_txoptions(1,0,0,0));
		}
		else
		{
			//route up to parent
			atomic{
				route_destination_address[0]=0x00000000;
				route_destination_address[1]=neighbortable[parent].Network_Address;
																																					//ack
				call MCPS_DATA.request(SHORT_ADDRESS, panid, srcadd, SHORT_ADDRESS, panid, route_destination_address, (NsduLength + 8), MSDU,1,set_txoptions_upstream(1,0,0,0,1));
			}
		}
		
	}
	
	return SUCCESS;
}

/*************************************************************/
/*******************NLME IMPLEMENTATION***********************/
/*************************************************************/


/*************************************************************
******************* NLME-RESET********************************
**************************************************************/
 command error_t NLME_RESET.request(){

  call MLME_RESET.request(TRUE);
return SUCCESS;
}
/*************************************************************/
/*******************NLME - START - ROUTER*********************/
/*************************************************************/

//This primitive allows the NHL of a ZigBee Router to initialize or change its superframe configuration.
//p171 and 210
command error_t NLME_START_ROUTER.request(uint8_t BeaconOrder, uint8_t SuperframeOrder, bool BatteryLifeExtension,uint32_t StartTime)
{	
	//printfUART("NLME_START_ROUTER.request\n", "");
	
	device_type = ROUTER;
	
	if(TYPE_DEVICE == ROUTER)
	{
	
		//assign current BO and SO
		beaconorder = BeaconOrder;
		superframeorder = SuperframeOrder;
	
		call MLME_SET.request(MACBEACONORDER, (uint8_t *)&BeaconOrder);
		
		call MLME_SET.request(MACSUPERFRAMEORDER, (uint8_t *)&SuperframeOrder);
		
		//*******************************************************
		//***********SET PAN VARIABLES***************************
		depth=DEVICE_DEPTH;
		
		nwk_IB.nwkAvailableAddresses=AVAILABLEADDRESSES;
		nwk_IB.nwkAddressIncrement= ADDRESSINCREMENT;
		
		nwk_IB.nwkMaxChildren=MAXCHILDREN;	//number of children a device is allowed to have on its current network
		nwk_IB.nwkMaxDepth=MAXDEPTH;	//the depth a device can have
		nwk_IB.nwkMaxRouters=MAXROUTERS;
		
		cskip = Cskip(depth);
		
		cskip_routing = Cskip(depth -1);
		
		nwk_IB.nwkNextAddress = networkaddress + (cskip * nwk_IB.nwkMaxRouters) + nwk_IB.nwkAddressIncrement;
	
		next_child_router_address = networkaddress +((number_child_router-1) * cskip) +1;
	
		
		number_child_router++;
		
		
		printfUART("cskip  %d\n", cskip);
		printfUART("C %d D %d r %d\n", MAXCHILDREN,MAXDEPTH,MAXROUTERS);
	
		//  command error_t request(uint32_t PANId, uint8_t LogicalChannel, uint8_t BeaconOrder, uint8_t SuperframeOrder,bool PANCoordinator,bool BatteryLifeExtension,bool CoordRealignment,bool SecurityEnable);
		call MLME_START.request(panid,LOGICAL_CHANNEL,BeaconOrder, SuperframeOrder, 0, 0,0,0,StartTime);
		
	}
	else
	{
	
		signal NLME_START_ROUTER.confirm(NWK_INVALID_REQUEST);
		
	}
	return SUCCESS;
}


/*************************************************************/
/******************NLME - NETWORK - FORMATION*****************/
/*************************************************************/

//This primitive allows the NHL to request to start a ZigBee network with itself as the coordinator
//Page 167-169
command error_t NLME_NETWORK_FORMATION.request(uint32_t ScanChannels, uint8_t ScanDuration, uint8_t BeaconOrder, uint8_t SuperframeOrder, uint16_t PANId, bool BatteryLifeExtension)
{

	uint8_t v_temp[6];

call Leds.led0On();
call Leds.led1On();
call Leds.led2On();
   
	v_temp[0] = 0x06;
	
	device_type = COORDINATOR;
	//device_type = ROUTER;
	
	call MLME_SET.request(MACMAXBEACONPAYLOADLENGTH,v_temp);
	
	//protocol ID
	v_temp[0] = 0x00;
	//uint8_t nwk_payload_profile_protocolversion(uint8_t stackprofile,uint8_t nwkcprotocolversion)
	v_temp[1] = nwk_payload_profile_protocolversion(0x00,0x00);
	//uint8_t nwk_payload_capacity(uint8_t routercapacity,uint8_t devicedepth,uint8_t enddevicecapacity)
	v_temp[2] = nwk_payload_capacity(0x01,0x00,0x01);
	
	//TX OFFSET (3 bytes)
	v_temp[3] = 0x56;
	v_temp[4] = 0x34;
	v_temp[5] = 0x12;
	
	
	call MLME_SET.request(MACBEACONPAYLOAD,v_temp);
	

	printfUART("NLME_NETWORK_FORMATION.request\n", "");
	//perform an energydetection scan
	//perform an active scan
	//and select a suitable channel
	//panid must be less than or equal to 0x3fff
	
	//assign current panid
	panid=PANId;
	
	//assign current BO and SO
	beaconorder = BeaconOrder;
	superframeorder = SuperframeOrder;
	
	call MLME_SET.request(MACBEACONORDER, (uint8_t *)&BeaconOrder);

	call MLME_SET.request(MACSUPERFRAMEORDER, (uint8_t *)&SuperframeOrder);
	
	v_temp[0] = (uint8_t)(PANId >> 8);
	v_temp[1] = (uint8_t)PANId;
	
	call MLME_SET.request(MACPANID, v_temp);

	//static assignement of the coordinator address
	networkaddress=0x0000;//Network address of the ZC of a network always 0x0000;
	
	//////printfUART("setting short addr: %i\n", networkaddress);
	
	v_temp[0] = (uint8_t)(networkaddress >> 8);
	v_temp[1] = (uint8_t)(networkaddress);

	
	call MLME_SET.request(MACSHORTADDRESS,v_temp);
	
	
	//*******************************************************
	//***********SET PAN VARIABLES***************************
	//nwk_IB.nwkNextAddress=networkaddress+0x0001;
	depth=DEVICE_DEPTH;
	nwk_IB.nwkAvailableAddresses=AVAILABLEADDRESSES;
	nwk_IB.nwkAddressIncrement= ADDRESSINCREMENT;
	
	nwk_IB.nwkMaxChildren=MAXCHILDREN;	//number of children a device is allowed to have on its current network
	nwk_IB.nwkMaxDepth=MAXDEPTH;	//the depth a device can have
	nwk_IB.nwkMaxRouters=MAXROUTERS;
	
	cskip = Cskip(depth);
	
	cskip_routing = Cskip(depth -1 );
	
	nwk_IB.nwkNextAddress = networkaddress + (cskip * nwk_IB.nwkMaxRouters) + nwk_IB.nwkAddressIncrement;
	
	next_child_router_address = networkaddress +((number_child_router-1) * cskip) +1;
	
	number_child_router++;
	
	printfUART("cskip  %d\n", cskip);
	printfUART("C %d D %d r %d\n", MAXCHILDREN,MAXDEPTH,MAXROUTERS);
	
	
	call MLME_START.request(PANId, LOGICAL_CHANNEL,BeaconOrder ,SuperframeOrder,1,0,0,0,0);
	



    




	return SUCCESS;
}
	
/*************************************************************/
/***************NLME - NETWORK - DISCOVERY *******************/
/*************************************************************/

//This primitive allows the next higher layer to request that the NWK layer discover networks currently operating within the POS.
//p164 and 210
command error_t NLME_NETWORK_DISCOVERY.request(uint32_t ScanChannels, uint8_t Scanduration)
{
	
	//ISSUE an MLME_SCAN.request to find the available networks
	//Temporary descover of the network
	//Channel Scan is not working properly
	//manually assign the network descriptor
	
	
	networkdescriptor networkdescriptorlist[1];

	printfUART("2 lauch passive scan\n", "");
	//The networkdescriptorlist must contain information about every network that was heard
	
	//make NetworkDescriptorList out of the PanDescriptorList
#ifndef TKN154_MAC
    call MLME_SCAN.request(PASSIVE_SCAN,0xFFFFFFFF,7);
#else


	networkdescriptorlist[0].PANId=0x1234;
	networkdescriptorlist[0].LogicalChannel=LOGICAL_CHANNEL;
	networkdescriptorlist[0].StackProfile=0x00;
	networkdescriptorlist[0].ZigBeeVersion=0x01;
	networkdescriptorlist[0].BeaconOrder=7;
	networkdescriptorlist[0].SuperframeOrder=6;
	networkdescriptorlist[0].PermitJoining=1;
	
	//temporary assignement on the neighbout table of the suitable PAN coordinator
	if (DEVICE_DEPTH == 0x01)
		add_neighbortableentry(networkdescriptorlist[0].PANId,D1_PAN_EXT0,D1_PAN_EXT1,D1_PAN_SHORT,COORDINATOR,NEIGHBOR_IS_PARENT);
	if (DEVICE_DEPTH == 0x02)
		add_neighbortableentry(networkdescriptorlist[0].PANId,D2_PAN_EXT0,D2_PAN_EXT1,D2_PAN_SHORT,COORDINATOR,NEIGHBOR_IS_PARENT);
	if (DEVICE_DEPTH == 0x03)
		add_neighbortableentry(networkdescriptorlist[0].PANId,D3_PAN_EXT0,D3_PAN_EXT1,D3_PAN_SHORT,COORDINATOR,NEIGHBOR_IS_PARENT);
	if (DEVICE_DEPTH == 0x04)
		add_neighbortableentry(networkdescriptorlist[0].PANId,D4_PAN_EXT0,D4_PAN_EXT1,D4_PAN_SHORT,COORDINATOR,NEIGHBOR_IS_PARENT);
#endif	
		
	signal NLME_NETWORK_DISCOVERY.confirm(1,networkdescriptorlist, NWK_SUCCESS);

	return SUCCESS;
}

/*************************************************************/
/************************NLME - JOIN**************************/
/*************************************************************/
//This primitive allows the NHL to request to join a network either through association.
//p173 and 210
command error_t NLME_JOIN.request(uint16_t PANId, bool JoinAsRouter, bool RejoinNetwork, uint32_t ScanChannels, uint8_t ScanDuration, uint8_t PowerSource, uint8_t RxOnWhenIdle, uint8_t MACSecurity)
{	

	//Assume we have selected a suitable parent and all previous conditions were true
	uint32_t destinaddress[2];
	
	printfUART("9 find parent\n", "");
	
	//list_neighbourtable();
	
	parent_index = find_suitable_parent();
	
	panid = PANId;
	
	//printfUART("NLME_JOIN %i %i\n", parent_index,panid); 
	
	if(parent_index == 0)
	{
		signal NLME_JOIN.confirm(PANId,NWK_NOT_PERMITTED);
	}
	else
	{
		//assign the true value to parent index
		parent_index = parent_index - 1;
				
		//destinaddress[0]=neighbortable[parent_index].Extended_Address0;
		//destinaddress[1]=neighbortable[parent_index].Extended_Address1;
		//verificar o endere�o do pan coordinator
		destinaddress[0]=0x00000000;
		
		
		destinaddress[1] = neighbortable[parent_index].Network_Address;
#ifdef TKN154_MAC		
		
		if (DEVICE_DEPTH == 0x01)
			destinaddress[1]=D1_PAN_SHORT;
		if (DEVICE_DEPTH == 0x02)
			destinaddress[1]=D2_PAN_SHORT;
		if (DEVICE_DEPTH == 0x03)
			destinaddress[1]=D3_PAN_SHORT;
		if (DEVICE_DEPTH == 0x04)
			destinaddress[1]=D4_PAN_SHORT;
			
#endif			
		printfUART("10 associate to %i\n", destinaddress[1]);	
		//set_capability_information(uint8_t alternate_PAN_coordinator, uint8_t device_type, uint8_t power_source, uint8_t receiver_on_when_idle, uint8_t security, uint8_t allocate_address)
		
		if (JoinAsRouter == 0x01)
			call MLME_ASSOCIATE.request(LOGICAL_CHANNEL,SHORT_ADDRESS,PANId,destinaddress, set_capability_information(JoinAsRouter,0x01,PowerSource,RxOnWhenIdle,MACSecurity,0x01),0);
		else
		{
		
			printfUART("11 go ass\n", "");	
		
			coordinator_addr[0]=0x00000000;
			coordinator_addr[1] = neighbortable[parent_index].Network_Address;
			//BUILD the PAN descriptor of the COORDINATOR
			//assuming that the adress is short
			pan_des.CoordAddrMode = SHORT_ADDRESS;
			pan_des.CoordPANId = panid;
			pan_des.CoordAddress0=0x00000000;
			pan_des.CoordAddress1=(uint32_t)neighbortable[parent_index].Network_Address;
			pan_des.LogicalChannel=neighbortable[parent_index].Logical_Channel;
			//superframe specification field
			//pan_des.SuperframeSpec = neighbortable[parent_index].SuperframeSpec;

			pan_des.GTSPermit=0x01;
			pan_des.LinkQuality=0x00;
			pan_des.TimeStamp=0x000000;
			pan_des.SecurityUse=0;
			pan_des.ACLEntry=0x00;
			pan_des.SecurityFailure=0x00;
	
			received_beacon_count=0;
			go_associate=1;
#ifdef TKN154_MAC

			
			//call MLME_ASSOCIATE.request(LOGICAL_CHANNEL,SHORT_ADDRESS,PANId,destinaddress, set_capability_information(JoinAsRouter,0x00,PowerSource,RxOnWhenIdle,MACSecurity,0x01),0);
#endif
		}
	}
	
	return SUCCESS;
}


/*************************************************************/
/************************NLME - LEAVE*************************/
/*************************************************************/

//This primitive allows the NHL to request that it or another device leaves the network
//page 181-183
command error_t NLME_LEAVE.request(uint32_t DeviceAddress[],bool RemoveChildren, bool MACSecurityEnable)
{ 	
	uint32_t devaddr[2];
	////printfUART("NLME_LEAVE.request\n", ""); 
	if (DeviceAddress == 0)//child asked to leave
	{	
		if(RemoveChildren == 0)//implemented like it is always 0
		{	
			//send leave request command frame: RemoveChildren subfield=0 of the command option field of the command frame payload
			//call MCPS_DATA.request(uint8_t SrcAddrMode, uint16_t SrcPANId, uint8_t SrcAddr[], uint8_t DstAddrMode, uint16_t DestPANId, uint8_t DstAddr[], uint8_t msduLength, uint8_t msdu[],uint8_t msduHandle, uint8_t TxOptions);
			devaddr[0]=neighbortable[parent].Extended_Address0;
			devaddr[1]=neighbortable[parent].Extended_Address1;
			call MLME_DISASSOCIATE.request(devaddr,0x02,0);
		}
		else
		{
			//send leave request command frame: RemoveChildren subfield=1
			//try to remove the children, call NLME_LEAVE.request(uint32_t DeviceAddress[]=address of child,bool RemoveChildren, bool MACSecurityEnable)
			//call MCPS_DATA.request(uint8_t SrcAddrMode, uint16_t SrcPANId, uint8_t SrcAddr[], uint8_t DstAddrMode, uint16_t DestPANId, uint8_t DstAddr[], uint8_t msduLength, uint8_t msdu[],uint8_t msduHandle, uint8_t TxOptions);
		}
	}
	else//parent forced a child to leave
	{
		//if(check_neighbortableentry(DeviceAddress[0], DeviceAddress[1]) == 0)
		//{
		//	signal NLME_LEAVE.confirm(DeviceAddress,NWK_UNKNOWN_DEVICE);
		//	//call MCPS_DATA.request(uint8_t SrcAddrMode, uint16_t SrcPANId, uint8_t SrcAddr[], uint8_t DstAddrMode, uint16_t DestPANId, uint8_t DstAddr[], uint8_t msduLength, uint8_t msdu[],uint8_t msduHandle, uint8_t TxOptions);
		//}
		
	}
	
	
	return SUCCESS;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -