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

📄 nwkp.nc

📁 tinyos-2.x.rar
💻 NC
📖 第 1 页 / 共 4 页
字号:
/*
 * @author IPP HURRAY http://www.hurray.isep.ipp.pt/art-wise
 * @author Andre Cunha
 *
 */
#include <Timer.h>
#include "printfUART.h"




module NWKP {
	

	uses interface Leds;
	
	//MAC interfaces
#ifndef TKN154_MAC
	uses interface MLME_START;
	
	uses interface MLME_GET;
	uses interface MLME_SET;
	
	uses interface MLME_BEACON_NOTIFY;
	uses interface MLME_GTS;
	
	uses interface MLME_ASSOCIATE;
	uses interface MLME_DISASSOCIATE;
	
	uses interface MLME_ORPHAN;
	
	uses interface MLME_SYNC;
	uses interface MLME_SYNC_LOSS;
	
	uses interface MLME_RESET;
	
	uses interface MLME_SCAN;
	
	
	uses interface MCPS_DATA;

#else
	uses interface OPENZB_MLME_START as MLME_START;
	
	uses interface OPENZB_MLME_GET as MLME_GET;
	uses interface OPENZB_MLME_SET as MLME_SET;
	
	uses interface OPENZB_MLME_BEACON_NOTIFY as MLME_BEACON_NOTIFY;
	uses interface OPENZB_MLME_GTS as MLME_GTS;
	
	uses interface OPENZB_MLME_ASSOCIATE as MLME_ASSOCIATE;
	uses interface OPENZB_MLME_DISASSOCIATE as MLME_DISASSOCIATE;
	
	uses interface OPENZB_MLME_ORPHAN as MLME_ORPHAN;
	
	uses interface OPENZB_MLME_SYNC as MLME_SYNC;
	uses interface OPENZB_MLME_SYNC_LOSS as MLME_SYNC_LOSS;
	
	uses interface OPENZB_MLME_RESET as MLME_RESET;
	
	uses interface OPENZB_MLME_SCAN as MLME_SCAN;
	
	
	uses interface OPENZB_MCPS_DATA as MCPS_DATA;


#endif


	uses interface Random;

//provides

	provides interface Init;
	provides interface NLDE_DATA;

	//NLME NWK Management services
    provides interface NLME_NETWORK_FORMATION;
   	provides interface NLME_NETWORK_DISCOVERY;
	provides interface NLME_START_ROUTER;
	provides interface NLME_JOIN;
	provides interface NLME_LEAVE;
	provides interface NLME_SYNC;
 
	/*  
	provides interface NLME_PERMIT_JOINING;
	provides interface NLME_DIRECT_JOIN;*/
	provides interface NLME_RESET;
	
	provides interface NLME_GET;
	provides interface NLME_SET;

  
}
implementation {


	nwkIB nwk_IB;
	
	uint8_t device_type = END_DEVICE;
	
/*****************************************************/
/*************Neighbourtable Variables****************/
/*****************************************************/ 

	//neighbour table array:
	neighbortableentry neighbortable[NEIGHBOUR_TABLE_SIZE];
	//number of neigbourtable entries:
	uint8_t neighbour_count;
	//the index of the parents neighbortable entry
	uint8_t parent;
	

/*****************************************************/
/****************ASSOCIATION Variables********************/
/*****************************************************/ 

	//CURRENT NETWORK ADDRESS
	uint16_t networkaddress=0x0000;

	//COORDINATOR
	//address assignement variables
	uint8_t depth=0;
	uint8_t cskip=0;
	
	uint8_t cskip_routing=0;

	//neighbour table parent index
	uint8_t parent_index;

	//NON COORDINATOR

	//current pan characteristics
	uint16_t panid;
	uint8_t beaconorder;
	uint8_t superframeorder;
	
	//next child router address
	uint16_t next_child_router_address;
	uint8_t number_child_router=0x01;
	uint8_t number_child_end_devices=0x01;
/*****************************************************/
/****************Integer Variables********************/
/*****************************************************/ 

	uint8_t joined=0;
	uint8_t sync_loss=0;
	//uint8_t synchronizing=0;
	uint8_t syncwait;
	
	
	//USED AFTER DE SCAN //GIVE SOME TIME TO THE DEVICE TO SYNC WITH THE PARENT
	uint8_t received_beacon_count=0;
	
	uint8_t go_associate =0;
	
	PANDescriptor pan_des;
	uint32_t coordinator_addr[2];
/******************************************************/
/*********NEIGHBOuRTABLE  MANAGEMENT FUNCTIONS*********/
/******************************************************/ 

	void init_nwkIB();
	
	uint8_t check_neighbortableentry(uint8_t addrmode,uint32_t Extended_Address0,uint32_t Extended_Address1);
	
	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);
	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);	
	
	uint8_t find_suitable_parent();
	
	uint16_t Cskip(uint8_t d);
	
	uint16_t nexthopaddress(uint16_t destinationaddress,uint8_t d);
	
	void list_neighbourtable();



 command error_t Init.init() {
  	
	printfUART_init();//make the possibility to print
		
	init_nwkIB();
	
	nwk_IB.nwkSequenceNumber=call Random.rand16();
  
	return SUCCESS;
 }


 
/*****************************************************************************************************/  
/**************************************MLME-SCAN*******************************************************/
/*****************************************************************************************************/ 
event error_t MLME_SCAN.confirm(uint8_t status,uint8_t ScanType, uint32_t UnscannedChannels, uint8_t ResultListSize, uint8_t EnergyDetectList[], SCAN_PANDescriptor PANDescriptorList[])
{
//FAULT-TOLERANCE

//the device channel scan ends with a scan confirm containing a list of the PANs (beacons received during the scan) 

	int i;
	uint8_t max_lqi=0;
	uint8_t best_pan_index=0;
	
	
	networkdescriptor networkdescriptorlist[1];
	
	
	//call Leds.redOff();
	
	printfUART("4 rec scan\n", "");
	
	//printfUART("MLME_SCAN.confirm %i\n", ScanType);
	
	if (ScanType == ORPHAN_SCAN)
	{
		printfUART("new scan \n", "");
	
		call MLME_SCAN.request(PASSIVE_SCAN,0xFFFFFFFF,7);
		return SUCCESS;
	}
	
	
	
	if(ScanType == ED_SCAN)
	{
		for(i=0;i<ResultListSize;i++)
		{
			printfUART("ED SCAN %i %i\n", (0x0A + i),EnergyDetectList[i]);
		}
		return SUCCESS;
	}
	
	for (i=0; i<ResultListSize;i++)
	{		/*
			printfUART("cord id %i", PANDescriptorList[i].CoordPANId);
			printfUART("CoordAddress %i", PANDescriptorList[i].CoordAddress);
			printfUART("LogicalChannel %i", PANDescriptorList[i].LogicalChannel);
			printfUART("SuperframeSpec %i", PANDescriptorList[i].SuperframeSpec);
			printfUART("lqi %i\n", PANDescriptorList[i].lqi);
			*/
		if(max_lqi < PANDescriptorList[i].lqi)
		{
			max_lqi =PANDescriptorList[i].lqi;
			best_pan_index = i;
		}
	}
	
	printfUART("SELECTED cord id %i", PANDescriptorList[best_pan_index].CoordPANId);
	printfUART("CoordAddress %i", PANDescriptorList[best_pan_index].CoordAddress);
	printfUART("LogicalChannel %i", PANDescriptorList[best_pan_index].LogicalChannel);
	printfUART("SuperframeSpec %i", PANDescriptorList[best_pan_index].SuperframeSpec);
	printfUART("lqi %i\n", PANDescriptorList[best_pan_index].lqi);
	
	
	
	/*
	
	coordinator_addr[0] = 0x00000001;
	
	coordinator_addr[1] = (uint32_t)PANDescriptorList[best_pan_index].CoordAddress;
	
	//pan_des = PANDescriptorList[best_pan_index];

	
	//BUILD the PAN descriptor of the COORDINATOR
	//assuming that the adress is short
	pan_des.CoordAddrMode = SHORT_ADDRESS;
	pan_des.CoordPANId = PANDescriptorList[best_pan_index].CoordAddress;
	pan_des.CoordAddress0=0x00000000;
	pan_des.CoordAddress1=0x00000000;
	pan_des.LogicalChannel=PANDescriptorList[best_pan_index].LogicalChannel;
	//superframe specification field
	pan_des.SuperframeSpec = PANDescriptorList[best_pan_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;
	
	*/

	
	
	printfUART("5 en sync %x \n", PANDescriptorList[best_pan_index].LogicalChannel);
	// the sync enables the TimerAsync events, in order to enable the synchronization with the PAN coordinator
	call MLME_SYNC.request(PANDescriptorList[best_pan_index].LogicalChannel,0);
	
	
	
	//The networkdescriptorlist must contain information about every network that was heard
	
	//make NetworkDescriptorList out of the PanDescriptorList


    printfUART("6 add neigh\n", "");

	networkdescriptorlist[0].PANId=PANDescriptorList[best_pan_index].CoordPANId;
	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;

	add_neighbortableentry(networkdescriptorlist[0].PANId,0x00000000,0x00000000,PANDescriptorList[best_pan_index].CoordAddress,COORDINATOR,NEIGHBOR_IS_PARENT);
	
	signal NLME_NETWORK_DISCOVERY.confirm(1,networkdescriptorlist, NWK_SUCCESS);
	
	
	
	return SUCCESS;
}

/*****************************************************************************************************/  
/**************************************MLME-ORPHAN****************************************************/
/*****************************************************************************************************/ 
event error_t MLME_ORPHAN.indication(uint32_t OrphanAddress[1], uint8_t SecurityUse, uint8_t ACLEntry)
{

	
		
		return 0x0000;
	}

/*****************************************************************************************************/  
/**************************************MLME-RESET*****************************************************/
/*****************************************************************************************************/ 
event error_t MLME_RESET.confirm(uint8_t status)
{

  signal NLME_RESET.confirm(status);

  return SUCCESS;
}
/*****************************************************************************************************/  
/**************************************MLME-SYNC-LOSS*************************************************/
/*****************************************************************************************************/ 
event error_t MLME_SYNC_LOSS.indication(uint8_t LossReason)
{
	////printfUART("MLME_SYNC_LOSS.indication\n", ""); 
	sync_loss = 1;
	syncwait=1;
	//signal NLME_SYNC.indication();
/*

printfUART("SL\n","");
		
		call MLME_SCAN.request(ORPHAN_SCAN,0xFFFFFFFF,7);
*/
	return SUCCESS;
}
/*****************************************************************************************************/  
/**************************************MLME-GTS*******************************************************/
/*****************************************************************************************************/ 
event error_t MLME_GTS.confirm(uint8_t GTSCharacteristics, uint8_t status)
{



	return SUCCESS;
}

event error_t MLME_GTS.indication(uint16_t DevAddress, uint8_t GTSCharacteristics, bool SecurityUse, uint8_t ACLEntry)
{



	return SUCCESS;
}

/*****************************************************************************************************/  
/**************************************MLME-BEACON NOTIFY*********************************************/
/*****************************************************************************************************/ 
event error_t MLME_BEACON_NOTIFY.indication(uint8_t BSN,PANDescriptor pan_descriptor, uint8_t PenAddrSpec, uint8_t AddrList, uint8_t sduLength, uint8_t sdu[])
{
	
	uint32_t destinaddress[2];
	printfUART("BN\n", "");

	
	if (go_associate==1)
	{
		received_beacon_count++;
		
		printfUART("bn %i\n", received_beacon_count);
		

⌨️ 快捷键说明

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