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

📄 wlan_mac_smac_nav_rpm_3mar06_ver1.function block

📁 opnet网络仿真
💻 FUNCTION BLOCK
📖 第 1 页 / 共 5 页
字号:
		tx_end_time = current_time + total_pk_size / wsn_data_rate;
		op_stat_write_t (data_traffic_sent_handle_inbits, 0.0, tx_end_time);
		op_stat_write_t (data_traffic_sent_handle, 0.0, tx_end_time);

		/* We can be sending this data message as a response to a CTS message	*/
		/* we received. Therefore reset the "frame respond to send" variable.	*/
		fresp_to_send = WlanC_None;

		/* If there is nothing in the higher layer data queue and fragmentation buffer	*/
		/* then disable the data frame flag which will indicate to the station to wait	*/
		/* for the higher layer packet.													*/
		if (op_prg_list_size (hld_list_ptr) == 0 && op_sar_buf_size (fragmentation_buffer_ptr) == 0)
			wlan_flags->data_frame_to_send = OPC_FALSE;			
	}

	else if	(frame_type == WlanC_Ack)			
	{	
		if ((pcf_flag == OPC_BOOLINT_ENABLED) || (wlan_flags->pcf_active == OPC_TRUE))
			last_frametx_type = frame_type;

		/* Preparing acknowledgement frame in response to the data frame	*/
		/* received from the remote stations.								*/

		/* Since an ACK is a control response frame, adjust its				*/
		/* transmission rate based on the data rate of the data frame we	*/
		/* are ACKing, if operating in an 11a or all-11g BSS. Otherwise use	*/
		tx_data_rate = control_data_rate;

		/* Creating ACK packet format type.									*/
		wlan_transmit_frame_ptr = op_pk_create_fmt ("wsn_control");

		/* Setting ack frame fields.										*/
		pk_chstruct_ptr = wlan_mac_pk_chstruct_create ();

		type = WlanC_Ack;   		
		pk_chstruct_ptr->retry = duplicate_entry;

		/* If there are more fragments to transmit then broadcast the remaining duration for which	*/
		/* the station will be using the channel.													*/
		if ((wlan_flags->duration_zero == OPC_FALSE) || (pcf_flag == OPC_BOOLINT_DISABLED))
			duration = nav_duration - (current_time + TXTIME_CTRL (WLANC_ACK_LENGTH));
		else 
			duration = 0;

		pk_chstruct_ptr->duration = duration;

		/* Destination station address.	*/
		pk_chstruct_ptr->rx_addr = remote_sta_addr;
		
		/* Setting Ack type.	*/
		op_pk_nfd_set_int32 (wlan_transmit_frame_ptr, "Type", type);

		/* Setting the accept field to true, meaning the frame is a good frame.	*/
		op_pk_nfd_set_int32 (wlan_transmit_frame_ptr, "Accept", OPC_TRUE);

		op_pk_nfd_set (wlan_transmit_frame_ptr, "Wlan Header", pk_chstruct_ptr, wlan_mac_pk_chstruct_copy, 
			wlan_mac_pk_chstruct_destroy, sizeof (WlanT_Control_Header_Fields));

		/* Place the transmission data rate and physical layer			*/
		/* technology information into the packet.						*/
		wlan_frame_tx_phy_info_set (wlan_transmit_frame_ptr, tx_data_rate);

		/* Since no frame is expected, the expected frame type field */
		/* to nil.                                                   */
		expected_frame_type = WlanC_None;	

		/* Once Ack is transmitted in response to Data frame then set the frame		*/
		/* response indicator to none frame as the response is already generated	*/
		fresp_to_send = WlanC_None;			

		/* Printing out information to ODB.	*/
		if (wlan_trace_active == OPC_TRUE)
		{
			op_prg_odb_print_major ("Ack is being transmitted for data packet received", OPC_NIL);
		}

		#ifndef OPD_NO_DEBUG
			printf("\n");
			printf("The ACK length in FB is %d \n",  (OpT_Packet_Size) (PLCP_OVERHEAD_CTRL (WLANC_ACK_LENGTH) * control_data_rate + WLANC_ACK_LENGTH));
			printf("The Total packet duration is %e \n",((double)(PLCP_OVERHEAD_CTRL (WLANC_ACK_LENGTH) * control_data_rate + WLANC_ACK_LENGTH))/(double)WSNC_DATA_RATE);
		#endif
						
		// mib modified code	
		op_pk_total_size_set (wlan_transmit_frame_ptr, (OpT_Packet_Size) (PLCP_OVERHEAD_CTRL (WLANC_ACK_LENGTH) * control_data_rate + WLANC_ACK_LENGTH));

		/* Update the control traffic sent statistics.					*/
		total_pk_size = (double) op_pk_total_size_get (wlan_transmit_frame_ptr);
		op_stat_write (ctrl_traffic_sent_handle_inbits, total_pk_size);
		op_stat_write (ctrl_traffic_sent_handle, 		1.0);

		/* Write a value of 0 for the end of transmission.				*/
		tx_end_time = current_time + total_pk_size / tx_data_rate;
		op_stat_write_t (ctrl_traffic_sent_handle_inbits, 0.0, tx_end_time);
		op_stat_write_t (ctrl_traffic_sent_handle, 		  0.0, tx_end_time);
	}

	else if (frame_type == WlanC_Rts)
	{		
		/* Creating Rts packet format type.									*/
		wlan_transmit_frame_ptr = op_pk_create_fmt ("wsn_control");

		/* Initializing RTS frame fields.									*/
		pk_chstruct_ptr = wlan_mac_pk_chstruct_create ();

		/* Type of frame */
		type = WlanC_Rts;   						

		/* if in the infrastructure BSS network then the immediate recipient for the transmitting	*/
		/* station will always be an Access point. Otherwise the frame is directly sent to the 		*/
		/* final destination.																		*/
		if ((bss_flag == OPC_TRUE) && (ap_flag == OPC_BOOLINT_DISABLED))
		{
			/* If Infrastructure BSS then the immediate destination will be Access point, which 	*/
			/* then forward the frame to the appropriate destination.								*/
			pk_chstruct_ptr->rx_addr = ap_mac_address;
		}
		else
		{
			/* Otherwise set the final destination address.	*/				   
			pk_chstruct_ptr->rx_addr = destination_addr;
		}

		/* Source station address.	*/
		pk_chstruct_ptr->tx_addr = my_address;

		/* Setting the Rts frame type.	*/
		op_pk_nfd_set_int32 (wlan_transmit_frame_ptr, "Type", type);

		/* Setting the accept field to true, meaning the frame is a good frame.	*/
		op_pk_nfd_set_int32 (wlan_transmit_frame_ptr, "Accept", OPC_TRUE);

		/* Setting the variable which keeps track of the last transmitted frame that needs response.	*/
		last_frametx_type = WlanC_Rts;

		/* Determining the size of the first data fragment or frame that need */
		/* to be transmitted following the RTS transmission.				  */				
		if (num_fragments > 1)
		{
			/* If there are more than one fragment to transmit then the */
			/* data segment of the first data frame will be the size of */
			/* fragmentation threshold. The total packet size will be   */
			/* data plus the overhead (which is 224 bits).				*/
			tx_datapacket_size = frag_threshold * 8 + WLANC_MSDU_HEADER_SIZE;
		}
		else
			/* If there is one data frame to transmit then the          */
			/* data segment of the first data frame will be the size of */
			/* the remainder computed earlier. The total packet size    */
			/* will be data plus the overhead (which is 224 bits).		*/
		{
			tx_datapacket_size = remainder_size + WLANC_MSDU_HEADER_SIZE;
		}

		/* Station is reserving channel bandwidth by using RTS frame, so    */
		/* in RTS the station will broadcast the duration it needs to send  */ 		 		
		/* one data frame and receive ACK for it. The total duration is the */
		/* the time required to transmit one data frame, plus one CTS frame */
		/* plus one ACK frame, and plus three SIFS intervals. While			*/
		/* computing the duration, call the two macros at different lines	*/
		/* to assure to use the correct value of the state variables within	*/
		/* the macros.						*/
		duration =  TXTIME_CTRL (WLANC_CTS_LENGTH) + TXTIME_CTRL (WLANC_ACK_LENGTH);
		duration += TXTIME_DATA (tx_datapacket_size) + 3 * sifs_time;            		
		pk_chstruct_ptr->duration = duration;
		
		// MIB SMAC Changes 9 Dec
		nav_duration_wsn = pk_chstruct_ptr->duration + current_time ;
		
		// transmission is the RTS duration field (SIFS-CTS-SIFS-DATA-SIFS-ACK) + the RTS transmission time
				
		// SMAC RTS Sleep Extension for data transmitter to remain awake past end of active cycle to finish transmission
		if((nav_duration_wsn + TXTIME_CTRL (WLANC_RTS_LENGTH))>= smac_sleep_time )
				{
				printf("Node %d sending RTS, nav duration end time = %e, original smac_sleep time = %e\n", my_address, nav_duration_wsn + TXTIME_CTRL (WLANC_RTS_LENGTH) + slot_time, smac_sleep_time);
				if ((op_ev_pending (sleep_Cyclic_evh) == OPC_TRUE))
					{						

						op_ev_cancel (sleep_Cyclic_evh);
						// add an additional slot time to the duration to ensure that the node receives the ACK before transitioning to sleep
						sleep_Cyclic_evh = op_intrpt_schedule_self ( (nav_duration_wsn + TXTIME_CTRL (WLANC_RTS_LENGTH) + slot_time), WlanC_Sleep_Schedule_Cyclic);
					
						#ifndef OPD_NO_DEBUG
							if ((op_sim_time() > wsn_test_time) && (my_address == wsn_test_address || my_address == wsn_test_address_2) )
							printf("Sending Long RTS: Cancelling & rescheduling node %d cyclic sleep time to %e\n", my_address, nav_duration_wsn+TXTIME_CTRL (WLANC_RTS_LENGTH)+slot_time);
						#endif
					}
				}
		
		// mib duration diagnostic printf code
		//printf("Setting duration: CTS = %e, ACK = %e, \nDATA = %e, 3SIFS = %e, Duration = %e\n", TXTIME_CTRL (WLANC_CTS_LENGTH),TXTIME_CTRL (WLANC_ACK_LENGTH),
		//		TXTIME_DATA (tx_datapacket_size),3 * sifs_time, duration);
		
		
		/* Setting RTS frame fields.										*/
		op_pk_nfd_set (wlan_transmit_frame_ptr, "Wlan Header", pk_chstruct_ptr, 
			wlan_mac_pk_chstruct_copy, wlan_mac_pk_chstruct_destroy, sizeof (WlanT_Control_Header_Fields));				

		/* Place the transmission data rate and physical layer technology	*/
		/* information into the packet.										*/
		wlan_frame_tx_phy_info_set (wlan_transmit_frame_ptr, tx_data_rate);

		op_pk_total_size_set (wlan_transmit_frame_ptr, (OpT_Packet_Size) (PLCP_OVERHEAD_CTRL (WLANC_RTS_LENGTH) * control_data_rate + WLANC_RTS_LENGTH));
	
		/* Station update of its own nav_duration.							*/
		if (cfp_ap_medium_control == OPC_FALSE)
			nav_duration = current_time + duration + (double) (op_pk_total_size_get (wlan_transmit_frame_ptr)) / control_data_rate; 								 						

		/* CTS is expected in response to RTS.								*/						
		expected_frame_type = WlanC_Cts;

		/* Printing out information to ODB.									*/
		if (wlan_trace_active == OPC_TRUE)
		{
			sprintf (msg_string, "RTS is being transmitted for data packet " OPC_PACKET_ID_FMT ".", pkt_in_service);
			op_prg_odb_print_major (msg_string, OPC_NIL);
		}


		/* Update the control traffic sent statistics.						*/
		total_pk_size = (double) op_pk_total_size_get (wlan_transmit_frame_ptr);
		op_stat_write (ctrl_traffic_sent_handle_inbits, total_pk_size);
		op_stat_write (ctrl_traffic_sent_handle, 		1.0);

		/* Write a value of 0 for the end of transmission.					*/
		tx_end_time = current_time + total_pk_size / control_data_rate;
		op_stat_write_t (ctrl_traffic_sent_handle_inbits, 0.0, tx_end_time);
		op_stat_write_t (ctrl_traffic_sent_handle, 		  0.0, tx_end_time);
	}


	else if (frame_type == WlanC_Cts)
	{
		/* Preparing CTS frame in response to the received RTS frame.		*/

		if ((pcf_flag == OPC_BOOLINT_ENABLED) || (wlan_flags->pcf_active == OPC_TRUE))
			last_frametx_type = frame_type;

		/* Since an CTS is a control response frame, adjust its				*/
		/* transmission rate based on the data rate of the RTS frame we		*/
		/* are replying, if operating in an 11a or all-11g BSS. Otherwise	*/
		/* use 1 Mbps, the mandatory PHY rate of 802.11/11b.				*/
		if (phy_type == WlanC_11a_PHY || (phy_type == WlanC_11g_PHY && wlan_flags->non_erp_present == OPC_FALSE))
			tx_data_rate = rcvd_frame_drate;
		else
			tx_data_rate = control_data_rate;

		/* Creating CTS packet format type.									*/
		wlan_transmit_frame_ptr = op_pk_create_fmt ("wsn_control");

		/* Adjust the packet size if necessary to model the PLCP overhead	*/
		/* accurately, which is physical layer technology dependent. The	*/
		/* default value is set for infra-red technology.					*/
		if (phy_char_flag != WlanC_Infra_Red)
			op_pk_bulk_size_set (wlan_transmit_frame_ptr, 
			(OpT_Packet_Size) (PLCP_OVERHEAD_CTRL (WLANC_CTS_LENGTH) * tx_data_rate - WLANC_DEFAULT_PLCP_OVERHEAD));

		/* Initializing CTS frame fields.									*/
		pk_chstruct_ptr = wlan_mac_pk_chstruct_create ();

		/* Type of frame.													*/
		type = WlanC_Cts;

		/* Destination station address.										*/
		pk_chstruct_ptr->rx_addr = remote_sta_addr;

		/* Station is reserving channel bandwidth by using RTS frame, so    */
		/* in RTS the station will broadcast the duration it needs to send  */ 		 		
		/* one data frame and receive ACK for it. Just subtract the			*/
		/* transmission of the CTS frame from updated NAV. Already waited	*/
		/* SIFS is subtracted within "current_time".						*/
		duration = nav_duration - (TXTIME_CTRL_DR (WLANC_CTS_LENGTH, tx_data_rate) + current_time);
		pk_chstruct_ptr->duration = duration;

		/* Setting CTS frame type.											*/
		op_pk_nfd_set_int32 (wlan_transmit_frame_ptr, "Type", type);

		/* Initialize the "Accept" field.									*/
		op_pk_nfd_set_int32 (wlan_transmit_frame_ptr, "Accept", OPC_TRUE);

		/* Setting CTS frame fields.										*/
		op_pk_nfd_set (wlan_transmit_frame_ptr, "Wlan Header", pk_chstruct_ptr, wlan_mac_pk_chstruct_copy, 
			wlan_mac_pk_chstruct_destroy, sizeof (WlanT_Control_Header_Fields));

		/* Place the transmission data rate and physical layer technology	*/
		/* information into the packet.										*/
		wlan_frame_tx_phy_info_set (wlan_transmit_frame_ptr, tx_data_rate);

		/* Once CTS is transmitted in response to RTS then set the frame	*/
		/* response indicator to none frame as the response is already		*/
		/* generated.														*/
		fresp_to_send = WlanC_None;										

		/* No frame is expected once CTS is transmitted.					*/
		expected_frame_type = WlanC_None;	

		/* Printing out information to ODB.									*/
		if (wlan_trace_active == OPC_TRUE)
		{
			op_prg_odb_print_major ("CT

⌨️ 快捷键说明

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