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

📄 nwkp.nc

📁 tinyos-2.x.rar
💻 NC
📖 第 1 页 / 共 4 页
字号:
/*************************************************************/
/************************NLME - SYNC**************************/
/*************************************************************/

//This primitive allows the NHL to synchronize or extract data from its ZigBee coordinator or router
//page 186-187
command error_t NLME_SYNC.request(bool Track)
{
//call MLME_SET.request(0x00,0x15);
 // call MLME_SET.request(MACCOORDSHORTADDRESS, 0x0000);
  //   call MLME_SET.request(0x50,0x1234);

  call MLME_SYNC.request(LOGICAL_CHANNEL,1);
	return SUCCESS;
}
  
/*************************************************************/
/*****************        NLME-SET     ********************/
/*************************************************************/
	
command error_t NLME_SET.request(uint8_t NIBAttribute, uint16_t NIBAttributeLength, uint16_t NIBAttributeValue)
{

	atomic{
	
		switch(NIBAttribute)
		{
			case NWKSEQUENCENUMBER :		nwk_IB.nwkSequenceNumber = (uint8_t) NIBAttributeValue;
											//////printfUART("nwk_IB.nwkSequenceNumber: %x\n",nwk_IB.nwkSequenceNumber);
											signal NLME_SET.confirm(NWK_SUCCESS,NIBAttribute);
											break;
											
			case NWKPASSIVEACKTIMEOUT :		nwk_IB.nwkPassiveAckTimeout = (uint8_t) NIBAttributeValue;
											//////printfUART("nwk_IB.nwkPassiveAckTimeout: %x\n",nwk_IB.nwkPassiveAckTimeout);
											signal NLME_SET.confirm(NWK_SUCCESS,NIBAttribute);
											break;
			
			
			case NWKMAXBROADCASTRETRIES :	nwk_IB.nwkMaxBroadcastRetries = (uint8_t) NIBAttributeValue;
											//////printfUART("nwk_IB.nwkMaxBroadcastRetries: %x\n",nwk_IB.nwkMaxBroadcastRetries);
											signal NLME_SET.confirm(NWK_SUCCESS,NIBAttribute);
											break;
											
			case NWKMAXCHILDREN :			nwk_IB.nwkMaxChildren = (uint8_t) NIBAttributeValue;
											//////printfUART("nwk_IB.nwkMaxChildren: %x\n",nwk_IB.nwkMaxChildren);
											signal NLME_SET.confirm(NWK_SUCCESS,NIBAttribute);
											break;
											
			case NWKMAXDEPTH :				nwk_IB.nwkMaxDepth = (uint8_t) NIBAttributeValue;
											//////printfUART("nwk_IB.nwkMaxDepth: %x\n",nwk_IB.nwkMaxDepth);
											signal NLME_SET.confirm(NWK_SUCCESS,NIBAttribute);
											break;
	
			case NWKMAXROUTERS :			nwk_IB.nwkMaxRouters = (uint8_t) NIBAttributeValue;
											//////printfUART("nwk_IB.nwkMaxRouters: %x\n",nwk_IB.nwkMaxRouters);
											signal NLME_SET.confirm(NWK_SUCCESS,NIBAttribute);
											break;
	
			case NWKMETWORKBROADCASTDELIVERYTIME :		nwk_IB.nwkNetworkBroadcastDeliveryTime = (uint8_t) NIBAttributeValue;
														//////printfUART("nwk_IB.nwkNetworkBroadcastDeliveryTime: %x\n",nwk_IB.nwkNetworkBroadcastDeliveryTime);
														signal NLME_SET.confirm(NWK_SUCCESS,NIBAttribute);
														break;
			case NWKREPORTCONSTANTCOST :	nwk_IB.nwkReportConstantCost = (uint8_t) NIBAttributeValue;
											//////printfUART("nwk_IB.nwkReportConstantCost: %x\n",nwk_IB.nwkReportConstantCost);
											signal NLME_SET.confirm(NWK_SUCCESS,NIBAttribute);
											break;
			case NWKROUTEDISCOVERYRETRIESPERMITED :		nwk_IB.nwkRouteDiscoveryRetriesPermitted = (uint8_t) NIBAttributeValue;
														//////printfUART("nwk_IB.nwkRouteDiscoveryRetriesPermitted: %x\n",nwk_IB.nwkRouteDiscoveryRetriesPermitted);
														signal NLME_SET.confirm(NWK_SUCCESS,NIBAttribute);
														break;
	
			case NWKSYMLINK :				nwk_IB.nwkSymLink = (uint8_t) NIBAttributeValue;
											//////printfUART("nwk_IB.nwkSymLink: %x\n",nwk_IB.nwkSymLink);
											signal NLME_SET.confirm(NWK_SUCCESS,NIBAttribute);
											break;
			
			case NWKCAPABILITYINFORMATION :	nwk_IB.nwkCapabilityInformation = (uint8_t) NIBAttributeValue;
											//////printfUART("nwk_IB.nwkCapabilityInformation: %x\n",nwk_IB.nwkCapabilityInformation);
											signal NLME_SET.confirm(NWK_SUCCESS,NIBAttribute);
											break;
											
			case NWKUSETREEADDRALLOC :		nwk_IB.nwkUseTreeAddrAlloc = (uint8_t) NIBAttributeValue;
											//////printfUART("nwk_IB.nwkUseTreeAddrAlloc: %x\n",nwk_IB.nwkUseTreeAddrAlloc);
											signal NLME_SET.confirm(NWK_SUCCESS,NIBAttribute);
											break;
											
			case NWKUSETREEROUTING :		nwk_IB.nwkUseTreeRouting = (uint8_t) NIBAttributeValue;
											//////printfUART("nwk_IB.nwkUseTreeRouting: %x\n",nwk_IB.nwkUseTreeRouting);
											signal NLME_SET.confirm(NWK_SUCCESS,NIBAttribute);
											break;
											
			case NWKNEXTADDRESS :			nwk_IB.nwkNextAddress = NIBAttributeValue;
											//////printfUART("nwk_IB.nwkNextAddress: %x\n",nwk_IB.nwkNextAddress);
											signal NLME_SET.confirm(NWK_SUCCESS,NIBAttribute);
											break;
											
			case NWKAVAILABLEADDRESSES :	nwk_IB.nwkAvailableAddresses = NIBAttributeValue;
											//////printfUART("nwk_IB.nwkAvailableAddresses: %x\n",nwk_IB.nwkAvailableAddresses);
											signal NLME_SET.confirm(NWK_SUCCESS,NIBAttribute);
											break;
											
			case NWKADDRESSINCREMENT :		nwk_IB.nwkAddressIncrement =NIBAttributeValue;
											//////printfUART("nwk_IB.nwkAddressIncrement: %x\n",nwk_IB.nwkAddressIncrement);
											signal NLME_SET.confirm(NWK_SUCCESS,NIBAttribute);
											break;
											
			case NWKTRANSACTIONPERSISTENCETIME :	nwk_IB.nwkTransactionPersistenceTime = (uint8_t) NIBAttributeValue;
													//////printfUART("nwk_IB.nwkTransactionPersistenceTime: %x\n",nwk_IB.nwkTransactionPersistenceTime);
													signal NLME_SET.confirm(NWK_SUCCESS,NIBAttribute);
													break;
											
			default:						signal NLME_SET.confirm(NWK_UNSUPPORTED_ATTRIBUTE,NIBAttribute);
											break;
			
		}
		
	}


	return SUCCESS;
}

/*************************************************************/
/*****************        NLME-GET     ********************/
/*************************************************************/
	
command error_t NLME_GET.request(uint8_t NIBAttribute)
{
	switch(NIBAttribute)
	{
		case NWKSEQUENCENUMBER :		signal NLME_GET.confirm(NWK_SUCCESS,NIBAttribute,0x0001,(uint16_t)&nwk_IB.nwkSequenceNumber);
										break;
									
		case NWKPASSIVEACKTIMEOUT :		signal NLME_GET.confirm(NWK_SUCCESS,NIBAttribute,0x0001,(uint16_t)&nwk_IB.nwkPassiveAckTimeout);
										break;
		
		case NWKMAXBROADCASTRETRIES :	signal NLME_GET.confirm(NWK_SUCCESS,NIBAttribute,0x0001,(uint16_t)&nwk_IB.nwkMaxBroadcastRetries);
										break;

		case NWKMAXCHILDREN :			signal NLME_GET.confirm(NWK_SUCCESS,NIBAttribute,0x0001,(uint16_t)&nwk_IB.nwkMaxChildren);
										break;

		case NWKMAXDEPTH :				signal NLME_GET.confirm(NWK_SUCCESS,NIBAttribute,0x0001,(uint16_t)&nwk_IB.nwkMaxDepth);
										break;

		case NWKMAXROUTERS :			signal NLME_GET.confirm(NWK_SUCCESS,NIBAttribute,0x0001,(uint16_t)&nwk_IB.nwkMaxRouters);
										break;

		case NWKMETWORKBROADCASTDELIVERYTIME :		signal NLME_GET.confirm(NWK_SUCCESS,NIBAttribute,0x0001,(uint16_t)&nwk_IB.nwkNetworkBroadcastDeliveryTime);
													break;

		case NWKREPORTCONSTANTCOST :	signal NLME_GET.confirm(NWK_SUCCESS,NIBAttribute,0x0001,(uint16_t)&nwk_IB.nwkReportConstantCost);
										break;

		case NWKROUTEDISCOVERYRETRIESPERMITED :		signal NLME_GET.confirm(NWK_SUCCESS,NIBAttribute,0x0001,(uint16_t)&nwk_IB.nwkRouteDiscoveryRetriesPermitted);
													break;

		case NWKSYMLINK :				signal NLME_GET.confirm(NWK_SUCCESS,NIBAttribute,0x0001,(uint16_t)&nwk_IB.nwkSymLink);
										break;
		
		case NWKCAPABILITYINFORMATION :	signal NLME_GET.confirm(NWK_SUCCESS,NIBAttribute,0x0001,(uint16_t)&nwk_IB.nwkCapabilityInformation);
										break;
										
		case NWKUSETREEADDRALLOC :		signal NLME_GET.confirm(NWK_SUCCESS,NIBAttribute,0x0001,(uint16_t)&nwk_IB.nwkUseTreeAddrAlloc);
										break;
										
		case NWKUSETREEROUTING :		signal NLME_GET.confirm(NWK_SUCCESS,NIBAttribute,0x0001,(uint16_t)&nwk_IB.nwkUseTreeRouting);
										break;
										
		case NWKNEXTADDRESS :			signal NLME_GET.confirm(NWK_SUCCESS,NIBAttribute,0x0002,(uint16_t)&nwk_IB.nwkNextAddress);
										break;

		case NWKAVAILABLEADDRESSES :	signal NLME_GET.confirm(NWK_SUCCESS,NIBAttribute,0x0002,(uint16_t)&nwk_IB.nwkAvailableAddresses);
										break;
										
		case NWKADDRESSINCREMENT :		signal NLME_GET.confirm(NWK_SUCCESS,NIBAttribute,0x0002,(uint16_t)&nwk_IB.nwkAddressIncrement);
										break;
										
		case NWKTRANSACTIONPERSISTENCETIME :	signal NLME_GET.confirm(NWK_SUCCESS,NIBAttribute,0x0001,(uint16_t)&nwk_IB.nwkTransactionPersistenceTime);
												break;

		default:						signal NLME_GET.confirm(NWK_UNSUPPORTED_ATTRIBUTE,NIBAttribute,0x0000,0x00);
										break;
	}
	
	return SUCCESS;
}


/*************************************************************/
/**************neighbor table management functions************/
/*************************************************************/

//check if a specific neighbourtable Entry is present
//Return 0:Entry is not present
//Return i:Entry is present and return the index of the entry + 1
	uint8_t check_neighbortableentry(uint8_t addrmode,uint32_t Address0,uint32_t Address1)
	{	
		
		int i=0;
		
		
		//printfUART("neighbourtable check c %i\n", neighbour_count);
	
		if (neighbour_count == 0)
		{
			//printfUART("no neib\n", "");
			return 0;
		}
		
		if(addrmode == SHORT_ADDRESS)
		{	
			for(i=0; i < neighbour_count; i++)
			{	
				///printfUART("compare %i %i\n", neighbortable[i].Network_Address, test);
				
				if(neighbortable[i].Network_Address == (uint16_t) Address0)
				{
					//printfUART("already present \n", "" );
					return i+1;
				}
			}
			return 0;
		}
		else
		{
			for(i=0; i<neighbour_count; i++)
			{	
				////printfUART("compare %x %x\n", neighbortable[i].Extended_Address0, Address0);
				if( (neighbortable[i].Extended_Address0 == Address0) && (neighbortable[i].Extended_Address1 == Address1))
				{
					//printfUART("already present \n", "" );
					return i+1;
				}
			}
			return 0;
		}
		
		return 0;
	}
	
	//function user to find a parent in the neighbour table
	//Return 0:no parent is present
	//Return i:parent is present and return the index of the entry + 1
	uint8_t find_suitable_parent()
	{
		int i =0;
		for (i=0; i<neighbour_count; i++)
		{
			if(neighbortable[i].Relationship == NEIGHBOR_IS_PARENT)
			{
				return i+1;
			}
		}
		return 0;
	}


	//add a neighbortable entry	
	void add_neighbortableentry	(uint16_t PAN_Id,uint32_t Extended_Address0,uint32_t Extended_Address1,uint32_t Network_Address,uint8_t Device_Type,uint8_t Relationship)
	{
	
	atomic{
		neighbortable[neighbour_count].PAN_Id=PAN_Id;
		neighbortable[neighbour_count].Extended_Address0=Extended_Address0;
		neighbortable[neighbour_count].Extended_Address1=Extended_Address1;
		neighbortable[neighbour_count].Network_Address=Network_Address;
		neighbortable[neighbour_count].Device_Type=Device_Type;
		neighbortable[neighbour_count].Relationship=Relationship;
		
		neighbour_count++;
		}
		
	}


	
	//update a neighbourtable entry
	void update_neighbortableentry(uint16_t PAN_Id,uint32_t Extended_Address0,uint32_t Extended_Address1,uint32_t Network_Address,uint8_t Device_Type,uint8_t Relationship)
	{
		//search entry with correct extended address
		uint8_t nrofEntry=0;
		nrofEntry = check_neighbortableentry(0x01,Extended_Address0,Extended_Address1) - 1;
		
		//update every attribute
		neighbortable[nrofEntry].PAN_Id=PAN_Id;
		neighbortable[nrofEntry].Extended_Address0=Extended_Address0;
		neighbortable[nrofEntry].Extended_Address1=Extended_Address1;
		neighbortable[nrofEntry].Network_Address=Network_Address;
		neighbortable[nrofEntry].Device_Type=Device_Type;
		neighbortable[nrofEntry].Relationship=Relationship;
		
		////printfUART("updated neighbourtable entry\n", "");
	}


	void list_neighbourtable()
	{
		int i;
		////printfUART("N List Count: %u\n", neighbour_count);
	
		for(i=0;i<neighbour_count;i++)
		{
			printfUART("Panid %x", neighbortable[i].PAN_Id);
			printfUART("Extaddr0 %x", neighbortable[i].Extended_Address0);
			printfUART("Extaddr1 %x", neighbortable[i].Extended_Address1);
			printfUART("nwkaddr %u", neighbortable[i].Network_Address);
			printfUART("devtype %u", neighbortable[i].Device_Type);
			printfUART("relation %u", neighbortable[i].Relationship);
			printfUART("depth %u\n", neighbortable[i].Depth);		
		}
	}
/*************************************************************/
/*****************Address Assignment functions****************/
/*************************************************************/

//calculate the size of the address sub-block for a router at depth d
uint16_t Cskip(uint8_t d)
{	
	uint8_t skip;
	uint8_t power;
	uint8_t x=1;
	uint8_t Cm = nwk_IB.nwkMaxChildren;
	uint8_t Rm = nwk_IB.nwkMaxRouters;
	uint8_t Lm = nwk_IB.nwkMaxDepth;
	/*////printfUART("nwk_IB.nwkMaxChildren: %i\n",  nwk_IB.nwkMaxChildren);
	////printfUART("nwk_IB.nwkMaxRouters: %i\n", nwk_IB.nwkMaxRouters);
	////printfUART("nwk_IB.nwkMaxDepth: %i\n", nwk_IB.nwkMaxDepth);*/
	if (Rm == 1)
	{
		skip = 1 + Cm * (Lm - d - 1);
	}
	else
	{	
		int i;
		
		power=(Lm - d - 1);
		for(i=0;i<power;i++)
		{
			x=x*Rm;
		}
		skip = (1 + Cm - Rm - (Cm * x))/(1-Rm);
	}
	////printfUART("Cksip function calculated: %i\n", skip);
	return skip;
}

//Calculate the nexthopaddress of the appropriate child if route down is required
uint16_t nexthopaddress(uint16_t destinationaddress,uint8_t d)
{
	uint16_t next_hop;

	next_hop=(networkaddress + 1 + ((destinationaddress-(networkaddress+1))/cskip)* cskip);
	
	////printfUART("Nexthop address calculated: %i\n", next_hop);
	return next_hop;
}

/*************************************************************/
/*****************Initialization functions********************/
/*************************************************************/
	//initialization of the nwk IB
	void init_nwkIB()
	{	
		//nwk IB default values p 204-206
		nwk_IB.nwkPassiveAckTimeout=0x03;
		nwk_IB.nwkMaxBroadcastRetries=0x03;
		nwk_IB.nwkMaxChildren=0x07;	//number of children a device is allowed to have on its current network
		nwk_IB.nwkMaxDepth=0x05;	//the depth a device can have
		nwk_IB.nwkMaxRouters=0x02;	//number of routers anyone device is allowed to have on its current network
		//neighbortableentry nwkNeighborTable[];//null set
		nwk_IB.nwkNetworkBroadcastDeliveryTime=( nwk_IB.nwkPassiveAckTimeout * nwk_IB.nwkMaxBroadcastRetries );
		nwk_IB.nwkReportConstantCost=0x00;
		nwk_IB.nwkRouteDiscoveryRetriesPermitted=nwkcDiscoveryRetryLimit;
	//set? nwkRouteTable;//Null set
		nwk_IB.nwkSymLink=0;
		nwk_IB.nwkCapabilityInformation=0x00;
		nwk_IB.nwkUseTreeAddrAlloc=1;
		nwk_IB.nwkUseTreeRouting=1;
		nwk_IB.nwkNextAddress=0x0000;
		nwk_IB.nwkAvailableAddresses=0x0000;
		nwk_IB.nwkAddressIncrement=0x0001;
		nwk_IB.nwkTransactionPersistenceTime=0x01f4;
	}


  
}

⌨️ 快捷键说明

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