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

📄 macp.nc

📁 tinyos-2.x.rar
💻 NC
📖 第 1 页 / 共 5 页
字号:
			}
		}
	}
	}
	
return SUCCESS;
}
async event error_t TimerAsync.sfd_fired()
{

return SUCCESS;
}

/*******************Timer BACKOFF PERIOD******************/
async event error_t TimerAsync.backoff_fired()
{
	//slotted CSMA/CA function
	atomic{
	
		if( csma_locate_backoff_boundary == 1 )
		{
			csma_locate_backoff_boundary=0;
			
			//post start_csma_ca_slotted();
			
			//DEFERENCE CHANGE
			if (backoff_deference == 0)
			{
				//normal situation
				delay_backoff_period = (call Random.rand16() & ((uint8_t)(powf(2,BE)) - 1));
				
				if (check_csma_ca_backoff_send_conditions((uint32_t) delay_backoff_period) == 1)
				{
					backoff_deference = 1;
				}
				
			}
			else
			{
				backoff_deference = 0;
			}
			
			csma_delay=1;
		}
		
		
		
		if( csma_cca_backoff_boundary == 1 )
			post perform_csma_ca_slotted();
	}
	//CSMA/CA
	atomic{
		if(csma_delay == 1 )
		{
			if (delay_backoff_period == 0)
			{
				if(csma_slotted == 0)
				{
					post perform_csma_ca_unslotted();
				}
				else
				{
					//CSMA/CA SLOTTED
					csma_delay=0;
					csma_cca_backoff_boundary=1;
				}
			}
			delay_backoff_period--;
		}
	}
	number_backoff++;
return SUCCESS;
}

/*******************T_ackwait**************************/
  event void T_ackwait.fired() {
  
  ////////printfUART("Tfd \n", "");
	
	//call Leds.redToggle();

	if (send_ack_check == 1)
	{
		retransmit_count++;
		
		if (retransmit_count == aMaxFrameRetries || send_indirect_transmission > 0)
		{
				//check the type of data being send
				/*
				if (associating == 1)
				{
					printfUART("af ack\n", "");
					associating=0;
					signal MLME_ASSOCIATE.confirm(0x0000,MAC_NO_ACK);
				}
				*/
				
			atomic{	
				//////////printfUART("TRANSMISSION FAIL\n",""); 
				//stardard procedure, if fail discard the packet
				atomic send_buffer_count --;
				send_buffer_msg_out++;
			
					//failsafe
					if(send_buffer_count > SEND_BUFFER_SIZE)
					{
						
						atomic send_buffer_count =0;
						send_buffer_msg_out=0;
						send_buffer_msg_in=0;
						
					}
					
					
					if (send_buffer_msg_out == SEND_BUFFER_SIZE)
						send_buffer_msg_out=0;
					
					if (send_buffer_count > 0)
						post send_frame_csma();
						
					send_ack_check=0;
					retransmit_count=0;
					ack_sequence_number_check=0;
				
				}
		}
		
		//////////printfUART("RETRY\n",""); 
		//retransmissions
		post send_frame_csma();
	}
    
  }

/*******************T_ResponseWaitTime**************************/
  event void T_ResponseWaitTime.fired() {
  	//command response wait time
	//////////printfUART("T_ResponseWaitTime.fired\n", "");

	if (associating == 1)
	{
		printfUART("af rwt\n", "");
		associating=0;
		signal MLME_ASSOCIATE.confirm(0x0000,MAC_NO_DATA);
		
	}
  
  }

/*******************TDBS Implementation Timers**************************/
async event error_t TimerAsync.before_start_track_beacon_fired()
{
	I_AM_IN_PARENT_CAP = 1;

return SUCCESS;
}



//track beacon events timers
async event error_t TimerAsync.start_track_beacon_fired()
{
	I_AM_IN_PARENT_CAP = 1;

	//printfUART("fi\n","");
	
	if (upstream_buffer_count > 0)
		post send_frame_csma_upstream();



return SUCCESS;
}
async event error_t TimerAsync.end_track_beacon_fired()
{

	I_AM_IN_PARENT_CAP = 0;
	
	//printfUART("unfi\n","");
	//call Leds.greenOff();

return SUCCESS;
}





/*****************************************************
****************PD_DATA EVENTS***********************
******************************************************/
  async event error_t PD_DATA.confirm(uint8_t status) {


    return SUCCESS;     
  }
 
/*****************************************************
****************       PD_DATA     ********************
******************************************************/ 

async event error_t PD_DATA.indication(uint8_t psduLenght,uint8_t* psdu, int8_t ppduLinkQuality){
/*
MPDU *packet;

uint8_t destination_address=0;

dest_short *dest_short_ptr;
dest_long *dest_long_ptr;
	
beacon_addr_short *beacon_addr_short_ptr;

*/
atomic{
		//if(I_AM_IN_CAP == 1 || I_AM_IN_CFP == 1 || mac_PIB.macShortAddress==0xffff || scanning_channels ==1 || findabeacon == 1)
		//{
			if (buffer_count > RECEIVE_BUFFER_SIZE)
			{
				//call Leds.redToggle();
				printfUART("full\n","");
			}
			else
			{
		
				/*
				packet = (MPDU*)psdu;
							
				
				switch ((packet->frame_control1 & 0x7))
				{
					case TYPE_BEACON:
									beacon_addr_short_ptr = (beacon_addr_short *) &packet->data[0];
									
									//avoid VERIFY static assignment of coordinator parent
									if (beacon_addr_short_ptr->source_address != mac_PIB.macCoordShortAddress)
									{
											printfUART("pb %x %x\n", beacon_addr_short_ptr->source_address,mac_PIB.macCoordShortAddress);
											return SUCCESS;
									}
									
									
									
									if ( mac_PIB.macShortAddress != 0xffff)
									{
										if ( beacon_addr_short_ptr->source_address != mac_PIB.macCoordShortAddress)
										{
											printfUART("pb %x %x\n", beacon_addr_short_ptr->source_address,mac_PIB.macCoordShortAddress);
											return SUCCESS;
										}
									}
					
					break;
					case TYPE_DATA:
					case TYPE_CMD:
									//VALIDATION OF DESTINATION ADDRESSES - NOT TO OVERLOAD THE PROCESSOR
									destination_address=get_fc2_dest_addr(packet->frame_control2);
				
									if (destination_address > 1)
									{
										switch(destination_address)
										{
											case SHORT_ADDRESS: 
																dest_short_ptr = (dest_short *) &packet->data[0];
																
																if ( dest_short_ptr->destination_address != 0xffff && dest_short_ptr->destination_address != mac_PIB.macShortAddress)
																{
																	printfUART("nsm %x %x\n", dest_short_ptr->destination_address,mac_PIB.macShortAddress); 
																	return SUCCESS;
																}
																//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("wsP %x %x \n", dest_short_ptr->destination_PAN_identifier,mac_PIB.macPANId); 
																	return SUCCESS;
																}
																break;
											
											case LONG_ADDRESS: 
																dest_long_ptr = (dest_long *) &packet->data[0];
																
																if ( dest_long_ptr->destination_address0 !=aExtendedAddress0 && dest_long_ptr->destination_address1 !=aExtendedAddress1 )
																{
																	printfUART("nlm %x %x \n",dest_long_ptr->destination_address0,dest_long_ptr->destination_address1); 
																	return SUCCESS;
																}
																//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("wLP %x %x\n", dest_long_ptr->destination_PAN_identifier,mac_PIB.macPANId); 
																	return SUCCESS;
																}
																
																break;
										}
									}
					

					
									break;
					case TYPE_ACK:
				
									break;
				}
						
				*/
				memcpy(&buffer_msg[current_msg_in],psdu,sizeof(MPDU));

				atomic{
					current_msg_in++;
				
					if ( current_msg_in == RECEIVE_BUFFER_SIZE ) 
						current_msg_in = 0;
		
					buffer_count ++;
				}
				
				link_quality = ppduLinkQuality;
				
				if (scanning_channels ==1)
				{
					//channel scan operation, accepts beacons only
					post data_channel_scan_indication();
				
				}
				else
				{
				//normal operation
					post data_indication();
				}
			}
		//}
		//else
		//{
		//	printfUART("drop\n","");
		//}
}
return SUCCESS;
}



task void data_indication()
{
	//check all the conditions for a receiver packet
    //pag 155
 
	uint8_t link_qual;
	
	atomic link_qual = link_quality;

	//printfUART("data_indication\n","");
	////////printfUART("buf  %i %i\n",buffer_count,indirect_trans_count);

	//Although the receiver of the device is enabled during the channel assessment portion of this algorithm, the
	//device shall discard any frames received during this time.
	////////////printfUART("performing_csma_ca: %i\n",performing_csma_ca);
	if (performing_csma_ca == 1)
	{	
		////////////printfUART("REJ CSMA\n","");
		atomic{ 
			buffer_count--;
		
			current_msg_out++;
		if ( current_msg_out == RECEIVE_BUFFER_SIZE )	
			current_msg_out = 0;
			}
		
		return;
    }
	
	//while performing channel scan disable the packet reception
	if ( scanning_channels == 1)
	{	
		atomic{ 
			buffer_count--;
	
			current_msg_out++;
		if ( current_msg_out == RECEIVE_BUFFER_SIZE )	
			current_msg_out = 0;
			}
		return;
	}	
atomic{

    ////printfUART("data ind %x %x %i\n",buffer_msg[current_msg_out].frame_control1,buffer_msg[current_msg_out].frame_control2,(buffer_msg[current_msg_out].frame_control2 & 0x7));
	
	//check the frame type of the received packet
	switch( (buffer_msg[current_msg_out].frame_control1 & 0x7) )
	{
		
			case TYPE_DATA: //printfUART("rd %i\n",buffer_msg[current_msg_out].seq_num);
							indication_data(&buffer_msg[current_msg_out],link_qual);
							break;
							
			case TYPE_ACK: //printfUART("ra\n","");
							//ack_received = 1;
							indication_ack(&buffer_msg[current_msg_out],link_qual);
							
							break;
							
			case TYPE_CMD:  //printfUART("rc\n","");
							indication_cmd(&buffer_msg[current_msg_out],link_qual);
							break;
			
			case TYPE_BEACON:
							
							//printfUART("rb %i\n",buffer_msg[current_msg_out].seq_num);
							if (mac_PIB.macShortAddress == 0x0000) 
							{
								buffer_count--;
							}
							else
							{
								process_beacon(&buffer_msg[current_msg_out],link_qual);
								
							}

							break;
			default: 
						atomic buffer_count--;
						////printfUART("Invalid frame type\n","");

						break;
	}
	atomic{
			current_msg_out++;
		if ( current_msg_out == RECEIVE_BUFFER_SIZE )	
			current_msg_out = 0;
		}
	}
	return;
}



/*****************************************************
****************PLME_ED EVENTS***********************
******************************************************/ 
event error_t PLME_CCA.confirm(uint8_t status){
return SUCCESS;
}
   
   
event error_t PLME_SET.confirm(uint8_t status, uint8_t PIBAttribute){   
return SUCCESS;
}
 
async event error_t PLME_SET_TRX_STATE.confirm(uint8_t status){

return SUCCESS;
}    
   
event error_t PLME_GET.confirm(uint8_t status,uint8_t PIBAttribute, uint8_t PIBAttributeValue){
   
return SUCCESS;
}    

⌨️ 快捷键说明

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