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

📄 macp.nc

📁 tinyos-2.x.rar
💻 NC
📖 第 1 页 / 共 5 页
字号:

event error_t PLME_ED.confirm(uint8_t status,int8_t EnergyLevel){

return SUCCESS;
}

/*******************************************************************************************************/
/*******************************************************************************************************/
/*******************************************************************************************************/
/****************************************PACKET PROCESSING FUNCTIONS************************************/
/*******************************************************************************************************/
/*******************************************************************************************************/
/*******************************************************************************************************/

void process_beacon(MPDU *packet,uint8_t ppduLinkQuality)
{

	/*
	ORGANIZE THE PROCESS BEACON FUNCION AS FOLLOWS.
	1- GET THE BEACON ORDER
	2- GET THE SUPERFRAME ORDER
	3- GET THE FINAL CAP SLOT
	4 - COMPUTE SD, BI, TS, BACKOFF PERIOD IN MILLISECONDS
	
	4- SYNCHRONIZE THE NODE BY DOING THE FOLLOWING
		- SET A TIMER IN MS FOR THE FINAL TIME SLOT (SUPERFRAME DURATION) : IT EXPRIES AFTER SD - TX TIME - PROCESS TIME
		- SET A TIMER IN MS FOR THE GTS IF ANY EXIST IT EXPRIES AFTER GTS_NBR * TIME_SLOT - TX TIME - PROCESS TIME 
	*/
	uint32_t SO_EXPONENT;
	uint32_t BO_EXPONENT;
	int i=0;
	uint16_t gts_descriptor_addr;
	uint8_t data_count;
	
	uint8_t gts_directions;
	uint8_t gts_des_count;
	
	uint8_t gts_ss;
	uint8_t gts_l;
	uint8_t dir;
	uint8_t dir_mask;

	//end gts variables

	//function that processes the received beacon
	beacon_addr_short *beacon_ptr;
	
	PANDescriptor pan_descriptor;

	// beacon_trans_delay = (((packet->length(bytes) * 8.0) / 250.00(bits/s) ) / 0.34(s) (timer_granularity) ) (symbols)

	//pending frames
	uint8_t short_addr_pending=0;
	uint8_t long_addr_pending=0;

	//used in the synchronization
	//uint32_t process_tick_counter; //symbols
	
	//uint32_t becon_trans_delay; //symbols
	//uint32_t start_reset_ct; //number of clock ticks since the start of the beacon interval
	
	//used in the track beacon
	beacon_processed = 1;
	missed_beacons=0;
	
	//initializing pointer to data structure
	beacon_ptr = (beacon_addr_short*) (packet->data);
	
	
		//decrement buffer count
	atomic buffer_count --;
	
	//printfUART("Received Beacon\n","");
	//printfUART("rb panid: %x %x \n",beacon_ptr->source_address,mac_PIB.macCoordShortAddress);
	//////printfUART("My macPANID: %x\n",mac_PIB.macPANId);
	printfUART("rb %x %x\n",beacon_ptr->source_address,mac_PIB.macCoordShortAddress);
	if( beacon_ptr->source_address != mac_PIB.macCoordShortAddress)
	{
		printfUART("nmp \n","");
		return;
	}
	//printfUART("bea %i\n",call TimerAsync.get_current_ticks());
	
	/**********************************************************************************/
	/*					PROCESSING THE SUPERFRAME STRUCTURE							  */
	/**********************************************************************************/
	
	if (PANCoordinator == 0)
	{
		mac_PIB.macBeaconOrder = get_beacon_order(beacon_ptr->superframe_specification);
		mac_PIB.macSuperframeOrder = get_superframe_order(beacon_ptr->superframe_specification);
		
		//mac_PIB.macCoordShortAddress = beacon_ptr->source_address;
		
		//printfUART("BO,SO:%i %i\n",mac_PIB.macBeaconOrder,mac_PIB.macSuperframeOrder);
		
		//mac_PIB.macPANId = beacon_ptr->source_PAN_identifier;
		
		//beacon order check if it changed
		if (mac_PIB.macSuperframeOrder == 0)
		{
			SO_EXPONENT = 1;
		}
		else
		{
			SO_EXPONENT = powf(2,mac_PIB.macSuperframeOrder);
		}
		
		if ( mac_PIB.macBeaconOrder ==0)
		{
			BO_EXPONENT =1;
		}
		else
		{
				BO_EXPONENT = powf(2,mac_PIB.macBeaconOrder);
		}
		BI = aBaseSuperframeDuration * BO_EXPONENT; 
		SD = aBaseSuperframeDuration * SO_EXPONENT; 
		
		//backoff_period
		backoff = aUnitBackoffPeriod;
		time_slot = SD / NUMBER_TIME_SLOTS;
		
		call TimerAsync.set_bi_sd(BI,SD);
	}	
		
	/**********************************************************************************/
	/*							PROCESS GTS CHARACTERISTICS							  */
	/**********************************************************************************/
		allow_gts =1;
	
		//initializing the gts variables
		s_GTSss=0;
		s_GTS_length=0;
		
		r_GTSss=0;
		r_GTS_length=0;
		
		/*
		send_s_GTSss=0;  
		send_r_GTSss=0;	
		send_s_GTS_len=0;
		send_r_GTS_len=0;
		*/
		final_CAP_slot = 15;


		gts_des_count = (packet->data[8] & 0x0f);
		
		data_count = 9;
		
		final_CAP_slot = 15 - gts_des_count;
		
		if (gts_des_count > 0 )
		{
		data_count = 10; //position of the current data count
		//process descriptors
		
			gts_directions = packet->data[9];
			
			//printfUART("gts_directions:%x\n",gts_directions);
			
			for(i=0; i< gts_des_count; i++)
			{	
				gts_descriptor_addr = (uint16_t) packet->data[data_count];
					
				//printfUART("gts_des_addr:%x mac short:%x\n",gts_descriptor_addr,mac_PIB.macShortAddress);
				
				data_count = data_count+2;
				//check if it concerns me
				if (gts_descriptor_addr == mac_PIB.macShortAddress)
				{
					//confirm the gts request
					////////////printfUART("packet->data[data_count]: %x\n",packet->data[data_count]);
					//gts_ss = 15 - get_gts_descriptor_ss(packet->data[data_count]);
					gts_ss = get_gts_descriptor_ss(packet->data[data_count]);
					gts_l = get_gts_descriptor_len(packet->data[data_count]);

					if ( i == 0 )
					{
						dir_mask=1;
					}
					else
					{
					
							dir_mask = powf(2,i);
					}
					////////////printfUART("dir_mask: %x i: %x gts_directions: %x \n",dir_mask,i,gts_directions);
					dir = ( gts_directions & dir_mask);
					if (dir == 0)
					{
						s_GTSss=gts_ss;
						s_GTS_length=gts_l;
					}
					else
					{

						r_GTSss=gts_ss;
						r_GTS_length=gts_l;
					}
					
					//printfUART("PB gts_ss: %i gts_l: %i dir: %i \n",gts_ss,gts_l,dir);
					////////////printfUART("PB send_s_GTSss: %i send_s_GTS_len: %i\n",send_s_GTSss,send_s_GTS_len);
					
					if ( gts_l == 0 )
					{
						allow_gts=0;
					}

					if (gts_confirm == 1 && gts_l != 0)
					{
						//signal ok
						//printfUART("gts confirm \n","");
						gts_confirm =0;
						signal MLME_GTS.confirm(GTS_specification,MAC_SUCCESS);
					}
					else
					{
						//signal not ok
						////////////printfUART("gts not confirm \n","");
						gts_confirm =0;
						signal MLME_GTS.confirm(GTS_specification,MAC_DENIED);
					}
					
				}
				data_count++;	
			}
		}
	
	/**********************************************************************************/
	/*							PROCESS PENDING ADDRESSES INFORMATION				  */
	/**********************************************************************************/	
		//this should pass to the network layer
		
		
		short_addr_pending=get_number_short(packet->data[data_count]);
		long_addr_pending=get_number_extended(packet->data[data_count]);
		
		//////////printfUART("ADD COUNT %i %i\n",short_addr_pending,long_addr_pending);
		
		data_count++;
		
		if(short_addr_pending > 0)
		{
			for(i=0;i < short_addr_pending;i++)
			{
				//////////printfUART("PB %i %i\n",(uint16_t)packet->data[data_count],short_addr_pending);
				
				//if(packet->data[data_count] == (uint8_t)mac_PIB.macShortAddress && packet->data[data_count+1] == (uint8_t)(mac_PIB.macShortAddress >> 8) )
				if((uint16_t)packet->data[data_count] == mac_PIB.macShortAddress)
				{
					
					create_data_request_cmd();
				}
				data_count = data_count + 2;
			}
		}
		if(long_addr_pending > 0)
		{
			for(i=0; i < long_addr_pending;i++)
			{
				if((uint32_t)packet->data[data_count] == aExtendedAddress0 && (uint32_t)packet->data[data_count + 4] == aExtendedAddress1)
				{
					
					data_count = data_count + 8;

				}
			
			}
		}

	/**********************************************************************************/
	/*				BUILD the PAN descriptor of the COORDINATOR						  */
	/**********************************************************************************/
		
		
	   //Beacon NOTIFICATION
	   //BUILD the PAN descriptor of the COORDINATOR
		//assuming that the adress is short
		pan_descriptor.CoordAddrMode = SHORT_ADDRESS;
		pan_descriptor.CoordPANId = 0x0000;//beacon_ptr->source_PAN_identifier;
		pan_descriptor.CoordAddress0=0x00000000;
		pan_descriptor.CoordAddress1=mac_PIB.macCoordShortAddress;
		pan_descriptor.LogicalChannel=current_channel;
		//superframe specification field
		pan_descriptor.SuperframeSpec = beacon_ptr->superframe_specification;
		
		pan_descriptor.GTSPermit=mac_PIB.macGTSPermit;
		pan_descriptor.LinkQuality=0x00;
		pan_descriptor.TimeStamp=0x000000;
		pan_descriptor.SecurityUse=0;
		pan_descriptor.ACLEntry=0x00;
		pan_descriptor.SecurityFailure=0x00;
	   
		//I_AM_IN_CAP = 1;
	   
	/**********************************************************************************/
	/*								SYNCHRONIZING									  */
	/**********************************************************************************/

	//processing time + beacon transmission delay
	
	//removed not used
	//process_tick_counter = call TimerAsync.get_process_frame_tick_counter();
	//removed not used	
	//start_reset_ct = ((1000 * (packet->length * 8.0) / 250)) / 69.54;  //(process_tick_counter - receive_tick_counter);

	if(PANCoordinator == 0)
	{
			I_AM_IN_CAP = 1;
			I_AM_IN_IP = 0;
			
			//call Leds.yellowOn();
			call Leds.led2On();
			
			call Leds.led1On();
			
			if(findabeacon == 1)
			{
				//printfUART("findabeacon\n", "");
				call TimerAsync.set_timers_enable(1);
				findabeacon =0;
			}
			
			//#ifdef PLATFORM_MICAZ
			//number_time_slot = call TimerAsync.reset_start(start_reset_ct+process_tick_counter+52);// //SOBI=3 52 //SOBI=0 15
			//#else
			
			//call TimerAsync.reset();
			
			number_time_slot = call TimerAsync.reset_start(75);   //95 old val sem print
					
			// +process_tick_counter+52 //SOBI=3 52 //SOBI=0 
			//#endif
			on_sync=1;
			
			printfUART("sE\n", "");
	}
	else
	{
		I_AM_IN_PARENT_CAP = 1;
		
		call TimerAsync.set_track_beacon(1);
		
		//#ifdef PLATFORM_MICAZ
		//	call TimerAsync.set_track_beacon_start_ticks(parent_offset,0x00001E00,(start_reset_ct+process_tick_counter+18));
		//#else
			call TimerAsync.set_track_beacon_start_ticks(parent_offset,0x00001E00,4);//(start_reset_ct)
		//#endif
		
		printfUART("sP \n", "");
	}
	
	call Leds.led1Toggle();
	signal MLME_BEACON_NOTIFY.indication((uint8_t)packet->seq_num,pan_descriptor,0, 0, mac_PIB.macBeaconPayloadLenght, packet->data);
		
return;
}


void process_gts_request(MPDU *pdu)
{
		error_t status;
		cmd_gts_request *mac_gts_request;
		
		mac_gts_request= (cmd_gts_request*) &pdu->data;
		
atomic{		
		if ( get_characteristic_type(mac_gts_request->gts_characteristics) == 1)
		{
		//allocation
	
	//process the gts request
		status = add_gts_entry(get_gts_length(mac_gts_request->gts_characteristics),get_gts_direction(mac_gts_request->gts_characteristics),mac_gts_request->source_address);
		
		}
		else
		{
		//dealocation
	
		status = remove_gts_entry(mac_gts_request->source_address);
		}
		
		signal MLME_GTS.indication(mac_gts_request->source_address, mac_gts_request->gts_characteristics, 0, 0);
		
		}

return;
}
/****************DATA indication functions******************/

void indication_data(MPDU *pdu, int8_t ppduLinkQuality)
{
	uint8_t data_len;
	
	uint8_t payload[80];
	uint8_t msdu_length=0;
	
	//int i;
	
	uint32_t SrcAddr[2];
	uint32_t DstAddr[2];
	
	
	//frame control variables
	uint8_t source_address=0;
	uint8_t destination_address=0;


	dest_short *dest_short_ptr;
	dest_long *dest_long_ptr;
	
	source_short *source_short_ptr;
	source_long *source_long_ptr;

	//implement the intra PAN data messages
	//intra_pan_source_short *intra_pan_source_short_ptr;
	//intra_pan_source_long *intra_pan_source_long_ptr;
	
	
	source_address=get_fc2_source_addr(pdu->frame_control2);
	destination_address=get_fc2_dest_addr(pdu->frame_control2);
	
	//decrement buffer count
	atomic buffer_count --;
	
	SrcAddr[0]=0x00000000;
	SrcAddr[1]=0x00000000;
	DstAddr[0]=0x00000000;
	DstAddr[1]=0x00000000;


	//printfUART("id %i %i \n",source_address,destination_address);


	if ( get_fc1_intra_pan(pdu->frame_control1)== 0 )
	{
	//INTRA PAN
		if (destination_address > 1 && source_address > 1)
		{
			// Destination LONG - Source LONG	
			if (destination_address == LONG_ADDRESS && source_address == LONG_ADDRESS)
			{
				dest_long_ptr = (dest_long *) &pdu->data[0];
				source_long_ptr = (source_long *) &pdu->data[DEST_LONG_LEN];
				
				//If a short destination address is included in the frame, it shall match either macShortAddress or the
				//broadcast address (0 x ffff). Otherwise, if an extended destination address is included in the frame, it
				//shall match aExtendedAddress.
				if ( dest_long_ptr->destination_address0 !=aExtendedAddress0 && dest_long_ptr->destination_address1 !=aExtendedAddress1 )
				{
					//////////printfUART("data rejected, ext destination not for me\n", ""); 
					return;
				}
				//If a destination PAN identifier is included in the frame, it shall match macPANId or shall be the
				//broadcast PAN identifier (0 x ffff).
				if(dest_long_ptr->destination_PAN_identifier != 0xffff && dest_long_ptr->destination_PAN_identifier != mac_PIB.macPANId )
				{

⌨️ 快捷键说明

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