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

📄 macm.nc.svn-base

📁 802.15.4协议的实现
💻 SVN-BASE
📖 第 1 页 / 共 5 页
字号:

			}
			
			
			//Destination SHORT - Source SHORT
			if ( destination_address == SHORT_ADDRESS && source_address == SHORT_ADDRESS )	
			{
				dest_short_ptr = (dest_short *) &pdu->data[0];
				source_short_ptr = (source_short *) &pdu->data[DEST_SHORT_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_short_ptr->destination_address != 0xffff && dest_short_ptr->destination_address != mac_PIB.macShortAddress)
				{
					printfUART("data rejected, short 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_short_ptr->destination_PAN_identifier != 0xffff && dest_short_ptr->destination_PAN_identifier != mac_PIB.macPANId )
				{
					printfUART("SH SH data rejected, wrong destination PAN\n", ""); 
					return;
				}
				
				data_len = 8;
				
				
				DstAddr[0] =dest_short_ptr->destination_address;
				
				SrcAddr[0] =source_short_ptr->source_address;
				
				msdu_length = pdu->length - data_len;
				
				for(i=0 ; i < msdu_length; i++)
				{
					payload[i] = pdu->data[data_len +i];
				}
			
				signal MCPS_DATA.indication(source_address, source_short_ptr->source_PAN_identifier, SrcAddr,destination_address, dest_short_ptr->destination_PAN_identifier, DstAddr, msdu_length, payload, ppduLinkQuality, 0,0);  

			}
		}
		
		/*********NO DESTINATION ADDRESS PRESENT ****************/
		
		if ( destination_address == 0 && source_address > 1 )
		{
				
			if (source_address == LONG_ADDRESS)
			{//Source LONG
				source_long_ptr = (source_long *) &pdu->data[0];
				
				//If only source addressing fields are included in a data or MAC command frame, the frame shall be
				//accepted only if the device is a PAN coordinator and the source PAN identifier matches macPANId.
				if ( PANCoordinator==0 || source_long_ptr->source_PAN_identifier != mac_PIB.macPANId )
				{
					printfUART("data rejected, im not pan\n", ""); 
					return;
				}
				
				data_len = 10;
				
				SrcAddr[1] =source_long_ptr->source_address0;
				SrcAddr[0] =source_long_ptr->source_address1;
				
				msdu_length = pdu->length - data_len;
				
				for(i=0 ; i < msdu_length; i++)
				{
					payload[i] = pdu->data[data_len +i];
				}
			
				signal MCPS_DATA.indication(source_address, source_long_ptr->source_PAN_identifier, SrcAddr,destination_address, 0x0000, DstAddr, msdu_length, payload, ppduLinkQuality, 0,0);  

			}
			else
			{//Source SHORT

				source_short_ptr = (source_short *) &pdu->data[0];
				//If only source addressing fields are included in a data or MAC command frame, the frame shall be
				//accepted only if the device is a PAN coordinator and the source PAN identifier matches macPANId.
				if ( PANCoordinator==0 || source_short_ptr->source_PAN_identifier != mac_PIB.macPANId )
				{
					printfUART("data rejected, im not pan\n", ""); 
					return;
				}
				
				data_len = 4;

				
				SrcAddr[0] =source_short_ptr->source_address;
				
				msdu_length = pdu->length - data_len;
				
				for(i=0 ; i < msdu_length; i++)
				{
					payload[i] = pdu->data[data_len +i];
				}
			
				signal MCPS_DATA.indication(source_address, source_short_ptr->source_PAN_identifier, SrcAddr,destination_address, 0x0000, DstAddr, msdu_length, payload, ppduLinkQuality, 0,0);  

			}
		}
		/*********NO SOURCE ADDRESS PRESENT ****************/
		
		if ( destination_address > 1 && source_address == 0 )
		{
			if (destination_address == LONG_ADDRESS)
			{//Destination LONG
				dest_long_ptr = (dest_long *) &pdu->data[0];
				
				//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 )
				{
					printfUART("data rejected, wrong destination PAN\n", ""); 
					return;
				}
				
				data_len = 10;
				
				DstAddr[1] = dest_long_ptr->destination_address0;
				DstAddr[0] =dest_long_ptr->destination_address1;
				
				msdu_length = pdu->length - data_len;
				
				for(i=0 ; i < msdu_length; i++)
				{
					payload[i] = pdu->data[data_len +i];
				}
			
				signal MCPS_DATA.indication(source_address,0x0000, SrcAddr,destination_address, dest_long_ptr->destination_PAN_identifier, DstAddr, msdu_length, payload, ppduLinkQuality, 0,0);  

			}
			else
			{//Destination SHORT
				dest_short_ptr = (dest_short *) &pdu->data[0];
				
				//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_short_ptr->destination_address != 0xffff && dest_short_ptr->destination_address != mac_PIB.macShortAddress)
				{
					printfUART("data rejected, short 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_short_ptr->destination_PAN_identifier != 0xffff && dest_short_ptr->destination_PAN_identifier != mac_PIB.macPANId )
				{
					printfUART("data rejected, wrong destination PAN\n", ""); 
					return;
				}
				
				data_len = 4;
				
				DstAddr[0] =dest_short_ptr->destination_address;
				
				msdu_length = pdu->length - data_len;
				
				for(i=0 ; i < msdu_length; i++)
				{
					payload[i] = pdu->data[data_len +i];
				}
			
				signal MCPS_DATA.indication(source_address,0x0000, SrcAddr,destination_address, dest_short_ptr->destination_PAN_identifier, DstAddr, msdu_length, payload, ppduLinkQuality, 0,0);  

				data_len = 4;
			}
		}
		
	}
	else
	{
	//intra_pan == 1
	
	
	
	}
	
	
return;
}


void indication_cmd(MPDU *pdu, int8_t ppduLinkQuality)
{
	//decrement buffer count
	atomic buffer_count --;
	
	process_command(pdu);
	
return;
}

void indication_ack(MPDU *pdu, int8_t ppduLinkQuality)
{
	//decrement buffer count
	atomic
	{
	buffer_count --;
	process_ack(pdu);
	}
return;
}


/*****************************************************
****************PLME_ED EVENTS***********************
******************************************************/ 
event result_t PLME_CCA.confirm(uint8_t status){
   return SUCCESS;
   }
   
   
event result_t PLME_SET.confirm(uint8_t status, uint8_t PIBAttribute){
   
   return SUCCESS;
   }
 
event result_t PLME_SET_TRX_STATE.confirm(uint8_t status){
   
	if(pending_reset)
	{
	atomic{
		if(status==SUCCESS)
		{ 
			init_MacCon();
			if(SetDefaultPIB) init_MacPIB();
			
			signal MLME_RESET.confirm(MAC_SUCCESS);
		}
		else
		{
			signal MLME_RESET.confirm(MAC_DISABLE_TRX_FAILURE);
		}
		pending_reset=0;
		}
	}
   return SUCCESS;
   }    
   
event result_t PLME_GET.confirm(uint8_t status,uint8_t PIBAttribute, uint8_t PIBAttributeValue){
   
   return SUCCESS;
   }    
 

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

		if ( scanning_channels == 1)
		{
			scanned_values[scan_count] = EnergyLevel;
			scan_count ++;
			call T_scan_duration.start(TIMER_ONE_SHOT,scan_duration);	
		}
		
   return SUCCESS;
   }
  
/*****************************************************/
/*				TIMERS FIRED       					 */
/*****************************************************/ 

async event result_t TimerAsync.before_bi_fired()
{
	//printfUART("before_bi_fired\n","");
	
	if ( Beacon_enabled_PAN == 1 )
	{
		trx_status = PHY_TX_ON;
		post set_trx();
	}
	else
	{
		trx_status = PHY_RX_ON;
		post set_trx();
	}
	return SUCCESS;
}

/*******************Timer BEACON INTERVAL******************/
async event result_t TimerAsync.bi_fired()
{
atomic{
	//post create_beacon();
	call Leds.yellowOn();
	
	if ( Beacon_enabled_PAN == 1 )
	{
		//trx_status = PHY_TX_ON;
		//post set_trx();
		call Leds.yellowOn();
		call PD_DATA.request(send_beacon_length,send_beacon_frame_ptr);
	}
	
	//trx_status = PHY_RX_ON;
	//post set_trx();
	
	}
	printfUART("Timer BI fired\n",""); 
	
	number_b =0;
	number_bb=0;
	
	/*
	if (Beacon_enabled_PAN == 1)
	{
			
		//post create_beacon();
		atomic{
		
			trx_status = PHY_RX_ON;
			post set_trx();
		}
		
	}
	else
	{
		atomic{
		trx_status = PHY_RX_ON;
		post set_trx();
		}
		//call PLME_SET_TRX_STATE.request(PHY_RX_ON);
	}
	*/
	//(SYNC) checking if the node is trying to syncronize
	//printfUART("The value of missed_beacons is %i\n", missed_beacons)
	
	if(findabeacon == 1 ) 
	{
		missed_beacons++;
		on_sync =0;
	}
	if(missed_beacons == aMaxLostBeacons && findabeacon == 1)
	{		
		
		atomic{
			//signal MLME_SYNC_LOSS.indication(MAC_BEACON_LOSS);
			
			printfUART("beacon loss find %i\n", missed_beacons);
			
			beacon_loss_reason = MAC_BEACON_LOSS;
			post signal_loss();
		
			findabeacon=0;
			missed_beacons =0;
			}
	}
	
	 //CHECK there is the need to wait a small amount of time before checking is the beacon as been processed or not
		//possible solition, if it receives a packet stand by for confirmation it its a beacon
	if (TrackBeacon == 1) 
	{
		if (beacon_processed==1) 
		{
			beacon_processed=0;
		}
		else
		{
			//dealocate all GTS
			//beacon loss
			printfUART("beacon loss track %i\n", missed_beacons);
			on_sync =0;
			
			beacon_loss_reason = MAC_BEACON_LOSS;
			post signal_loss();
		}
	}
	
	//post create_data();

	csma_backoff_counter_inst=0;

	return SUCCESS;
}


/*******************Timer SUPERFRAME DURATION******************/
async event result_t TimerAsync.sd_fired()
{

	call Leds.yellowOff();
	
	//call Leds.greenToggle();
	printfUART("Timer SD fired\n", ""); 

	number_b=0;
	number_bb=0;

	if ( mac_PIB.macRxOnWhenIdle == 0 && findabeacon == 0)
	{
		atomic{
			trx_status = PHY_TRX_OFF;
			post set_trx();
		}
		//call PLME_SET_TRX_STATE.request(PHY_TRX_OFF);
	}
	else
	{
		atomic{
		trx_status = PHY_RX_ON;
		post set_trx();
		}
		//call PLME_SET_TRX_STATE.request(PHY_RX_ON);
	}
	
	/*
	//TEST
	atomic{
		trx_status = PHY_RX_ON;
		post set_trx();
		}
	//END TEST
	*/
	//if the node is trying to synchronize
	//printfUART("findabeacon: %i\n", findabeacon); 
	if (on_sync == 0 )
	{
		atomic{
			trx_status = PHY_RX_ON;
			post set_trx();
		}
	
	}

	if (Beacon_enabled_PAN == 1)
	{
		//increment the gts_null descriptors
		atomic{
			if (GTS_null_descriptor_count > 0) post increment_gts_null();
		
			//creation of the beacon
			post create_beacon();
		}
	}
	
	return SUCCESS;
}

//TEST
task void create_gts_request()
{
//im a child node
		atomic {
		
		mac_PIB.macShortAddress = TOS_LOCAL_ADDRESS;
			
		//if (r_GTS_length == 0 )
			create_gts_request_cmd(set_gts_characteristics(1, GTS_RX_ONLY, 0));
		}
return;
}


/*******************Timer BEFORE BACKOFF BOUNDARY******************/
async event result_t TimerAsync.before_bb_fired()
{




return SUCCESS;
}
/*******************Timer BACKOFF BOUNDARY******************/
async event result_t TimerAsync.bb_fired()
{
	number_b=0;
	number_bb++;
	/*
	if (number_bb == 5)
	{
		post test_send_data();
	}
	*/
	/*
	if (number_bb == 8 && on_sync == 1 && Beacon_enabled_PAN==0)
	{
		call Leds.greenToggle();

⌨️ 快捷键说明

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