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

📄 mac01.c

📁 zigbee程序
💻 C
📖 第 1 页 / 共 5 页
字号:
	
/*****************************************************/
/*				Indirect Transmission buffers		 */
/*****************************************************/
	/***************Variables*************************/	
	//indirect transmission buffer
	norace indirect_transmission_element indirect_trans_queue[INDIRECT_BUFFER_SIZE];
	//indirect transmission message counter
	uint8_t indirect_trans_count=0;

	/***************Functions Definition***************/	
	//function used to initialize the indirect transmission buffer
	void init_indirect_trans_buffer();
	//function used to search and send an existing indirect transmission message
	void send_ind_trans_addr(uint32_t DeviceAddress[]);
	//function used to remove an existing indirect transmission message
	result_t remove_indirect_trans(uint8_t handler);
	//function used to increment the transaction persistent time on each message
	//if the transaction time expires the messages are discarded
	void increment_indirect_trans();

		
/*****************************************************/
/*				RECEIVE buffers		                  */
/*****************************************************/  
	/***************Variables*************************/
	//buffering variables
	norace MPDU buffer_msg[RECEIVE_BUFFER_SIZE];
	int current_msg_in=0;
	int current_msg_out=0;
	int buffer_count=0;

	/***************Functions Definition***************/	
	
	task void data_indication();
	
	void indication_cmd(MPDU *pdu, int8_t ppduLinkQuality);
	void indication_ack(MPDU *pdu, int8_t ppduLinkQuality);
	void indication_data(MPDU *pdu, int8_t ppduLinkQuality);
	
/*****************************************************/
/*				RECEPTION AND TRANSMISSION		                  */
/*****************************************************/  
	/***************Variables*************************/
	//buffering for sending
	norace MPDUBuffer send_buffer[SEND_BUFFER_SIZE];
	uint8_t send_buffer_count=0;
	uint8_t send_buffer_msg_in=0;
	uint8_t send_buffer_msg_out=0;
	
	//retransmission information
	uint8_t send_ack_check;//ack requested in the transmitted frame
	uint8_t retransmit_count;//retransmission count
	uint8_t ack_sequence_number_check;//transmission sequence number
	uint8_t send_retransmission;
	uint8_t send_indirect_transmission;

	uint8_t pending_request_data=0;
	
	uint8_t ackwait_period;
	
	uint8_t link_quality;
	
	norace ACK mac_ack;
	ACK *mac_ack_ptr;
	
	uint32_t gts_expiration;

	uint8_t I_AM_IN_CAP=0;
	uint8_t I_AM_IN_CFP=0;
	uint8_t I_AM_IN_IP=0;
	
	/***************Functions Definition***************/	
	task void send_frame_csma();
	
	uint8_t check_csma_ca_send_conditions(uint8_t frame_length,uint8_t frame_control1);

	uint8_t check_gts_send_conditions(uint8_t frame_length);
	
	uint8_t calculate_ifs(uint8_t pk_length);

/*****************************************************/
/*				BEACON MANAGEMENT  		              */
/*****************************************************/ 
	/***************Variables*************************/
	norace MPDU mac_beacon_txmpdu;
	MPDU *mac_beacon_txmpdu_ptr;
	
	uint8_t *send_beacon_frame_ptr;
	uint8_t send_beacon_length;

	/***************Functions Definition***************/	
	/*function to create the beacon*/
	task void create_beacon();
	/*function to process the beacon information*/
	void process_beacon(MPDU *packet,uint8_t ppduLinkQuality);

/***************************DEBUG FUNCTIONS******************************/
/* This function are list functions with the purpose of debug, to use then uncomment the declatarion
on top of this file*/
/*
	
	void list_mac_pib();
	
	void list_gts();
	
	void list_my_gts();
	void list_gts_null();
	
	
	*/
	//list all the handles in the indirect transmission buffer, debug purposes
	void list_indirect_trans_buffer();	
	

	
/***************************END DEBUG FUNCTIONS******************************/



  /**
   * Used to initialize this component.
   */
  command result_t StdControl.init() 
  {
    call Phy_control.init();
	call Random.init();

	
	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();
	
	init_gts_slot_list();
	
	init_available_gts_index();
	
	aExtendedAddress0=TOS_LOCAL_ADDRESS;
	aExtendedAddress1=TOS_LOCAL_ADDRESS;
	
	init_indirect_trans_buffer();


	}
	
	//beacon
	mac_beacon_txmpdu_ptr = &mac_beacon_txmpdu;
		
	//ack
	mac_ack_ptr = &mac_ack;
	
	//Other timers, sync timers units expressed in miliseconds
	ackwait_period = ((mac_PIB.macAckWaitDuration * 4.0 ) / 250.0) * 3;

	response_wait_time = ((aResponseWaitTime * 4.0) / 250.0) * 2;
	
	atomic{
	
		//initialization of the async timers values
		//values expressed in symbols
		
		//#ifdef PLATFORM_MICAZ
			BI = aBaseSuperframeDuration * pow(2,mac_PIB.macBeaconOrder);
			SD = aBaseSuperframeDuration * pow(2,mac_PIB.macSuperframeOrder);
		//#else //telosb
		//	BI = aBaseSuperframeDuration * powf(2,mac_PIB.macBeaconOrder);
		//	SD = aBaseSuperframeDuration * powf(2,mac_PIB.macSuperframeOrder);
		//#endif
		
		//backoff_period
		backoff = aUnitBackoffPeriod;
		//backoff_period_boundary
		
		time_slot = SD / NUMBER_TIME_SLOTS;
					
		call TimerAsync.set_enable_backoffs(1);	
		call TimerAsync.set_backoff_symbols(backoff);
		
		call TimerAsync.set_bi_sd(BI,SD);
		
		call TimerAsync.start();
	}
	
	
    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",""); 

		trx_status = PHY_RX_ON;
		call PLME_SET_TRX_STATE.request(PHY_RX_ON);

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

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

		
		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
			{
		
				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();
				}
			}
		}

return SUCCESS;
}


task void data_channel_scan_indication()
{
	uint8_t link_qual;
	
	beacon_addr_short *beacon_ptr;
	
	//printfUART("data_channel_scan_indication\n","");

	atomic link_qual = link_quality;

	atomic buffer_count--;

	switch(scan_type)
	{
		case ED_SCAN: 
						if (scanned_values[current_scanning-1] < link_qual)
								scanned_values[current_scanning-1] = link_qual;
						break;
		
		case ACTIVE_SCAN:break;
					
		case PASSIVE_SCAN: 
							switch( (buffer_msg[current_msg_out].frame_control1 & 0x7) )
							{
								case TYPE_BEACON:
								////////////printfUART("Received Beacon\n","");
								beacon_ptr = (beacon_addr_short*) (&buffer_msg[current_msg_out].data);

								//Beacon NOTIFICATION
								//BUILD the PAN descriptor of the COORDINATOR
								//assuming that the adress is short
								scan_pans[current_scanning-1].CoordPANId = beacon_ptr->destination_PAN_identifier;
								scan_pans[current_scanning-1].CoordAddress=beacon_ptr->source_address;
								scan_pans[current_scanning-1].LogicalChannel=current_channel;
								//superframe specification field
								scan_pans[current_scanning-1].SuperframeSpec = beacon_ptr->superframe_specification;
								
								if (scan_pans[current_scanning-1].lqi < link_qual)
								scan_pans[current_scanning-1].lqi = link_qual;
							
								break;
							default: break;
							//atomic buffer_count--;
							////////////printfUART("Invalid frame type\n","");

							}
							break;
		case ORPHAN_SCAN: break;
		
	}
	atomic{
			current_msg_out++;
		if ( current_msg_out == RECEIVE_BUFFER_SIZE )	
			current_msg_out = 0;
		}
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;

	////printfUART("data_indication\n","");
	
	//call Leds.greenOn();
	
	////////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);

⌨️ 快捷键说明

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