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

📄 macm.nc.svn-base

📁 802.15.4协议的实现
💻 SVN-BASE
📖 第 1 页 / 共 5 页
字号:
	
	task void pd_data_confirm();
	
	uint8_t pd_data_confirm_status;
	
	void list_mac_pib();
	
	task void send_gts_frame();
	
	//TEST
	task void test_send_data();
	task void create_gts_request();
	
	//END TEST
	
	
  uint8_t msdu_pay[20];
  
  /**
   * Used to initialize this component.
   */
  command result_t StdControl.init() 
  {
    call Phy_control.init();
	call Random.init();

			msdu_pay[0]=0x01;
			msdu_pay[1]=0x01;
			msdu_pay[2]=0x02;
			msdu_pay[3]=0x03;
			msdu_pay[4]=0x04;
			msdu_pay[5]=0x05;
			msdu_pay[6]=0x06;
	

	
	printfUART_init();
	
    return SUCCESS;
  }
  /**
   * Starts the components.
   * @return Always returns SUCCESS.
   */
  command result_t StdControl.start() 
  {
  
	call Leds.redOff();
	call Leds.greenOff();
	call Leds.yellowOff();


atomic{
	//inicialize the mac PIB
	init_MacPIB();
	
	
	init_GTS_db();
	init_GTS_null_db();
	
	//aExtendedAddress0= 0x11111111;
	//aExtendedAddress1= 0x99999999;

	aExtendedAddress0=TOS_LOCAL_ADDRESS;
	aExtendedAddress1=TOS_LOCAL_ADDRESS;
	
	
	init_indirect_trans_buffer();

  //initialization of the beacon interval
		//BI = ((aBaseSuperframeDuration * pow(2,mac_PIB.macBeaconOrder)) * 4.0) / 250.0;
		//SD = ((aBaseSuperframeDuration * pow(2,mac_PIB.macSuperframeOrder)) * 4.0) / 250.0;
		
		//initialization of the async timers values
		//values expressed in symbols
		BI = aBaseSuperframeDuration * pow(2,mac_PIB.macBeaconOrder);
		
		SD = aBaseSuperframeDuration * pow(2,mac_PIB.macSuperframeOrder);
		
		//backoff_period
		b = aUnitBackoffPeriod;
		//backoff_period_boundary
		bb = pow(2,mac_PIB.macSuperframeOrder) * aBaseSlotDuration;
		
		/*
		printfUART("BI: %i \n",BI);
		printfUART("SD: %i \n",SD);
		printfUART("b: %i \n",b);
		printfUART("bb: %i \n",bb);
	*/
		//Other timers, sync timers units expressed in miliseconds
		ackwait_period = (mac_PIB.macAckWaitDuration * 4.0 ) / 250.0;

		response_wait_time = (aResponseWaitTime * 4.0) / 250.0;

		mac_txmpdu_ptr = &mac_txmpdu;
		
		//beacon_frame_ptr = &mac_beacon_txmpdu;
  }
  
	//call Phy_control.start();

	call TimerAsync.set_enable_backoffs(1);	
	call TimerAsync.set_b_symbols(b);
	call TimerAsync.set_bb_symbols(bb);
	call TimerAsync.set_bi_symbols(BI);
	call TimerAsync.set_sd_symbols(SD);
	call TimerAsync.start();

	//TEST
	atomic mac_PIB.macShortAddress=TOS_LOCAL_ADDRESS;
	//END TEST
	
	
	//call PLME_SET_TRX_STATE.request(PHY_RX_ON);
	
	//call T_scan_duration.start(TIMER_REPEAT,500);
	
	//call T_cca.start(TIMER_ONE_SHOT,2000);
	
    return SUCCESS;
  }
  /**
   * Stops the components.
   * @return Always returns SUCCESS.
   */
command result_t StdControl.stop() {
    
	call Phy_control.stop();

	call TimerAsync.stop();
	
	return SUCCESS;																																														
  }
/*****************************************************
****************PD_DATA EVENTS***********************
******************************************************/
  async event result_t PD_DATA.confirm(uint8_t status) {
		//printfUART("PD_DATA.confirm\n",""); 
		
		atomic pd_data_confirm_status = status;
		
		post pd_data_confirm();

		//call PLME_SET_TRX_STATE.request(PHY_TRX_OFF);
	
    return SUCCESS;     
  }
 
 task void pd_data_confirm()
 {
	atomic{
			if ( indirect_trans_status_pending == 1 )
			{
				printfUART("indirect removed dest_addres0: %y dest_addres1: %y\n",indirect_trans_queue[indirect_trans_pending_handler].dest_addres0,indirect_trans_queue[indirect_trans_pending_handler].dest_addres1); 
				indirect_trans_status_pending =0;
				indirect_trans_queue[indirect_trans_pending_handler].handler=0x00;
				indirect_trans_queue[indirect_trans_pending_handler].dest_addres0 = 0x00000000;
				indirect_trans_queue[indirect_trans_pending_handler].dest_addres1 = 0x00000000;
				//need to know if the COMM STATUS is send here
				//signal MLME_COMM_STATUS.indication(uint16_t PANId,uint8_t SrcAddrMode, uint32_t SrcAddr[], uint8_t DstAddrMode, uint32_t DstAddr[], uint8_t status);
				signal MLME_COMM_STATUS.indication(mac_PIB.macPANId,0x00, 0x00, 0x00, 0x00,pd_data_confirm_status);
			}
				trx_status = PHY_RX_ON;
				call PLME_SET_TRX_STATE.request(PHY_RX_ON);
		}
 return;
 }
/*****************************************************
****************       PD_DATA     ********************
******************************************************/ 

async event result_t PD_DATA.indication(uint8_t psduLenght,uint8_t* psdu, int8_t ppduLinkQuality){

	call Leds.redToggle();
	
	printfUART("dr %i\n", number_bb);

atomic{
	receive_tick_counter = call TimerAsync.get_current_ticks();
	
	atomic memcpy(&buffer_msg[current_msg_in],psdu,sizeof(MPDU));
	
	post message_in();
	
	link_quality = ppduLinkQuality;
	
	post data_indication();
}
return SUCCESS;
}


task void message_in()
{
	atomic{
		current_msg_in++;
		if ( current_msg_in == buffer_size ) current_msg_in = 0;
		buffer_count ++;
		}
		//printfUART("in: %i\n", current_msg_in);
return;
}

task void message_out()
{
	atomic{
		current_msg_out++;
		if ( current_msg_out == buffer_size )	current_msg_out = 0;
		//buffer_count --;
		}
		//printfUART("out: %i\n", current_msg_out);
return;
}


task void data_indication()
{
	//check all the conditions for a receiver packet
   //pag 155
    //verify the problem
	uint8_t link_qual;
	
	atomic link_qual = link_quality;
	
   	if ( first == 1 )
	{
		atomic buffer_count--;
		first =0;
		post message_out();
		return;
	}
	
	//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--;
		post message_out();
		return;
    }
	
	//while performing channel scan disable the packet reception
	if ( scanning_channels == 1)
	{	
		atomic buffer_count--;
		post message_out();
		return;
	}	

	switch( (buffer_msg[current_msg_out].frame_control & 0x7) )
	{
		
			case TYPE_DATA: //printfUART("Received data\n","");
							indication_data(&buffer_msg[current_msg_out],link_qual);
							break;
							
			case TYPE_ACK: printfUART("Received ack\n","");
							ack_received = 1;
							indication_ack(&buffer_msg[current_msg_out],link_qual);
							break;
							
			case TYPE_CMD:  printfUART("Received cmd\n","");
							indication_cmd(&buffer_msg[current_msg_out],link_qual);
							break;
			
			case TYPE_BEACON:
							//printfUART("Received Beacon\n","");
							indication_beacon(&buffer_msg[current_msg_out],link_qual);
							break;
			default: 
						atomic buffer_count--;
						printfUART("Invalid frame type\n","");
						//return SUCCESS;
						break;
	}
	
	post message_out();
	
	return;
}


/****************DATA indication functions******************/

void indication_beacon(MPDU *pdu, int8_t ppduLinkQuality)
{
	beacon_addr_short *beacon_reception_ptr;
	
	//check if the frame indicates that is a beacon frame the source pan shall mach the macPANid unless macPANid is equal to 0xffff
	//if panid equal to 0xffff the it shall be accepted
	//printfUART("panid: %i\n",mac_PIB.macPANId);
	beacon_reception_ptr = (beacon_addr_short *) pdu->data;
	
	
	//decrement buffer count
	atomic buffer_count --;
	
	//printfUART("Received Beacon\n","");
	//printfUART("Source panid: %i\n",beacon_reception_ptr->source_PAN_identifier);
	//printfUART("My macPANID: %i\n",mac_PIB.macPANId);
	
	if (mac_PIB.macPANId == 0xffff)
	{
		//printfUART("Im not associated\n","");
		//return SUCCESS;
	}
	else
	{
		
		if( beacon_reception_ptr->source_PAN_identifier != mac_PIB.macPANId && beacon_reception_ptr->source_PAN_identifier != 0xffff)
		{
			printfUART("Not my PAN\n","");
			return;
		}
		
	}
	
	process_beacon(pdu);
	
return;
}


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;

	//CHECK IMPLEMENT
	//intra_pan_source_short *intra_pan_source_short_ptr;
	//intra_pan_source_long *intra_pan_source_long_ptr;
	
	
	source_address=get_frame_control_source_addr(pdu->frame_control);
	destination_address=get_frame_control_dest_addr(pdu->frame_control);
	
	//decrement buffer count
	atomic buffer_count --;
	
	SrcAddr[0]=0x00000000;
	SrcAddr[1]=0x00000000;
	DstAddr[0]=0x00000000;
	DstAddr[1]=0x00000000;


	if ( get_intra_pan(pdu->frame_control)== 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 )
				{
					printfUART("data rejected, wrong destination PAN\n", ""); 
					return;
				}
				data_len = 20;
				
				
				DstAddr[1] = dest_long_ptr->destination_address0;
				DstAddr[0] =dest_long_ptr->destination_address1;
				
				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, dest_long_ptr->destination_PAN_identifier, DstAddr, msdu_length, payload, ppduLinkQuality, 0,0);  
				
			}
			
			// Destination SHORT - Source LONG
			if ( destination_address == SHORT_ADDRESS && source_address == LONG_ADDRESS )
			{
				dest_short_ptr = (dest_short *) &pdu->data[0];
				source_long_ptr = (source_long *) &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("data rejected, wrong destination PAN\n", ""); 
					return;
				}
				
				data_len = 14;
				
				DstAddr[0] =dest_short_ptr->destination_address;
				
				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, dest_short_ptr->destination_PAN_identifier, DstAddr, msdu_length, payload, ppduLinkQuality, 0,0);  

			}
			// Destination LONG - Source SHORT
			if ( destination_address == LONG_ADDRESS && source_address == SHORT_ADDRESS )
			{
				dest_long_ptr = (dest_long *) &pdu->data[0];
				source_short_ptr = (source_short *) &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 )
				{
					printfUART("data rejected, wrong destination PAN\n", ""); 
					return;
				}
				
				data_len = 14;
				
				DstAddr[1] = dest_long_ptr->destination_address0;
				DstAddr[0] =dest_long_ptr->destination_address1;
				
				
				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_long_ptr->destination_PAN_identifier, DstAddr, msdu_length, payload, ppduLinkQuality, 0,0);  

⌨️ 快捷键说明

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