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

📄 wlan_mac_tmac_nav_rpm_31jan06_ver2.function block

📁 opnet无线网络编程
💻 FUNCTION BLOCK
📖 第 1 页 / 共 5 页
字号:
//#ifndef OPD_NO_DEBUG
//	if ((op_sim_time() > wsn_test_time) && (my_address == wsn_test_address || my_address == wsn_test_address_2) )
//		printf("\n");
//#endif


// WSN State Variable init is run in the exit executive of BSS_Init 
// (after the wlan SV Init run in the INIT State
static void
wsn_sv_init ()
{

//Objid					params_attr_objid;

	/**	1. Initialize the wsn state variables.				**/
	/** 2. Read model attribute values in variables.	    **/
	/** 3. Create global lists								**/
	/** 4. Register statistics handlers						**/

	FIN (wlan_mac_sv_init ());

	// Initialize the wsn variables

	wsn_test_address = 17;     // used to specify node of interest in test printf
	wsn_test_address_2 = 8;
	wsn_test_time = 0;       // used to determine when to start printing the test cases
	
	// used in the variable RPM version
	//wsn_energy_per_node = WSNC_ENERGY_PER_NODE;	// battery_current_capacity (A*hr) * 3 (Volts) * (3600 seconds/hr)
	wsn_energy_per_node = battery_capacity * WSNC_BATTERY_VOLTAGE * 3600;
	
	
	
	/* Obtain the values assigned to the various attributes	*/
	//op_ima_obj_attr_get (my_objid, "Wireless LAN Parameters", &mac_params_comp_attr_objid);
	//params_attr_objid = op_topo_child (mac_params_comp_attr_objid, OPC_OBJTYPE_GENERIC, 0);


	/* Get WSN model attributes.	*/
	// Time in seconds of a frame cycle for T-MAC (500ms standard simulation comparison)
	
	// frame duration indicates the time of a complete frame cycle
	op_ima_obj_attr_get (my_objid, "Frame_Duration", &frame_period);
	// Used in S-MAC:  Duty_Cycle indicates the % active portion of a frame
	op_ima_obj_attr_get (my_objid, "Duty_Cycle", &duty_cycle);
	
	// Used to set RPM levels permitted in architecture
	op_ima_obj_attr_get (my_objid, "NAV_Sleep", &NAV_SLEEP_ALLOWED);
	op_ima_obj_attr_get (my_objid, "LPM1", &LPM1_ALLOWED);
	op_ima_obj_attr_get (my_objid, "LPM2", &LPM2_ALLOWED);
	op_ima_obj_attr_get (my_objid, "LPM3", &LPM3_ALLOWED);
		
	// Used to set RPM levels permitted in architecture
	//NAV_SLEEP_ALLOWED = OPC_TRUE;
	//LPM1_ALLOWED = OPC_TRUE;
	//LPM2_ALLOWED = OPC_TRUE;
	//LPM3_ALLOWED = OPC_TRUE;
	
	sleep = 0;
	intrpt_time_backoff = 0;
	backoff_pending = 0;
	deference_pending = 0;

	state_start = op_sim_time();
	deference_time = 0;
	
	// variables used to generate sleep percentage statistics
	sleep_stat = 0;	
	LPM1_sleep_stat = 0;
	LPM2_sleep_stat = 0;
	LPM3_sleep_stat = 0;
	
	wsn_interrupt_cw = OPC_FALSE;
	wsn_interrupt_backoff = OPC_FALSE;
	wsn_deference_cw = OPC_FALSE;
	wsn_deference_backoff = OPC_FALSE;

	wsn_state_duration = 0;
	
	// in (s) seconds
	lpm1_duration = WSNC_LPM1_DURATION;
	lpm2_duration = WSNC_LPM2_DURATION;	
	lpm3_duration = WSNC_LPM3_DURATION;	

	wsn_energy_per_node = WSNC_ENERGY_PER_NODE;	// battery_current_capacity (A*hr) * 3 (Volts) * (3600 seconds/hr)
	
	energy_rate_receive = WSNC_RECEIVE_CURRENT * WSNC_BATTERY_VOLTAGE;
	energy_rate_transmit = WSNC_TRANSMIT_CURRENT * WSNC_BATTERY_VOLTAGE;
		
	energy_rate_LPM1 = WSNC_LPM1_CURRENT * WSNC_BATTERY_VOLTAGE;	
	energy_rate_LPM2 = WSNC_LPM2_CURRENT * WSNC_BATTERY_VOLTAGE;	
	energy_rate_LPM3 = WSNC_LPM3_CURRENT * WSNC_BATTERY_VOLTAGE;	
		
	energy_total_LPM1_transition = WSNC_LPM1_DURATION * WSNC_LPM1_AVE_TRANSITION_CURRENT * WSNC_BATTERY_VOLTAGE;
	energy_total_LPM2_transition = WSNC_LPM2_DURATION * WSNC_LPM2_AVE_TRANSITION_CURRENT * WSNC_BATTERY_VOLTAGE;
	energy_total_LPM3_transition = WSNC_LPM3_DURATION * WSNC_LPM3_AVE_TRANSITION_CURRENT * WSNC_BATTERY_VOLTAGE;
	
	energy_consum_total_local = 0;
	wsn_throughput_local = 0;
	wsn_energy_per_bit = 0;
	
	op_prg_mt_mutex_lock(wsn_global_energy_pool_mutex, OPC_MT_MUTEX_LOCK_WRITER);
	wsn_global_energy_pool = wsn_global_energy_pool + wsn_energy_per_node;
	op_prg_mt_mutex_unlock(wsn_global_energy_pool_mutex);

	
	wsn_live_nodes = wsn_live_nodes +1;  // global variable, increment with every node startup

	wsn_data_rate = WSNC_DATA_RATE;	//wsn_data_rate = 250000;
	control_data_rate = wsn_data_rate;
	
	wsn_slot_time = WSNC_SLOT_TIME;
	wlan_slot_time_set (WSNC_SLOT_TIME);
	
	wsn_time_per_byte = WSNC_SLOT_TIME * 2; // in (s)
	// interframe spacing settings
	difs_time = WSNC_DIFS_SLOTS * WSNC_SLOT_TIME;
	pifs_time = WSNC_PIFS_SLOTS * WSNC_SLOT_TIME;
	sifs_time = WSNC_SIFS_SLOTS * WSNC_SLOT_TIME;
	eifs_time = sifs_time + TXTIME_CTRL (WLANC_ACK_LENGTH)  + difs_time;
	
	/* Minimum an Max contention window size for selecting backoff slots.	*/
	cw_min = WSNC_CW_MIN;
	cw_max = WSNC_CW_MAX;

	plcp_overhead_control = WLANC_PLCP_OVERHEAD_DSSS_LONG;
	plcp_overhead_data    = WLANC_PLCP_OVERHEAD_DSSS_LONG;

	
	nav_duration_wsn = 0;
	node_dead = OPC_FALSE;

	// ******************* WSN T-MAC parameters **********************
	
		
	// T-MAC Adaptive Timeout period
	wsn_ta_time = 1.5 * (cw_max * WSNC_SLOT_TIME + TXTIME_CTRL(WLANC_RTS_LENGTH) + sifs_time);

	//  Variable to schedule recurring cycle interrupt
	wsn_period_end = frame_period;
	op_intrpt_schedule_self (wsn_period_end, WlanC_Update_Period);
	//op_intrpt_schedule_self (tmac_period , WlanC_Update_Period);
	
	// /* Registering wsn global statistics.*/
	// creates the handles to graph current consumption and sleep cycle
	//sleep_stathandle				= op_stat_reg ("Wireless Lan.Sleep (cycle)",	 OPC_STAT_INDEX_NONE, OPC_STAT_LOCAL);
	//LPM1_sleep_stathandle			= op_stat_reg ("Wireless Lan.LPM1 Sleep (cycle)",OPC_STAT_INDEX_NONE, OPC_STAT_LOCAL);
	//LPM2_sleep_stathandle			= op_stat_reg ("Wireless Lan.LPM2 Sleep (cycle)",OPC_STAT_INDEX_NONE, OPC_STAT_LOCAL);
	//LPM3_sleep_stathandle			= op_stat_reg ("Wireless Lan.LPM3 Sleep (cycle)",OPC_STAT_INDEX_NONE, OPC_STAT_LOCAL);
	energy_stathandle				= op_stat_reg ("Wireless Lan.Current (mA)",		 OPC_STAT_INDEX_NONE, OPC_STAT_LOCAL);

FOUT;
}
	
static void
wlan_mac_sv_init ()
{
	Objid					mac_params_comp_attr_objid;
	Objid					params_attr_objid;
	Objid					pcf_params_comp_attr_objid;
	Objid					subpcf_params_attr_objid;	
	Objid					chann_objid;
	int						num_chann;
	double					tx_power;
	int						i;
	Objid					statwire_objid;
	int						num_statwires;
	double					threshold;
	double					rx_power_threshold_dbm;
	void*                   temp_ptr;
	int						roaming_cap_flag, cts_to_self_flag;
	Evhandle				invalid_evh = OPC_EV_HANDLE_INVALID;

	/**	1. Initialize state variables.						**/
	/** 2. Read model attribute values in variables.	    **/
	/** 3. Create global lists								**/
	/** 4. Register statistics handlers						**/
	FIN (wlan_mac_sv_init ());

	/* object id of the surrounding processor.				*/
	my_objid = op_id_self ();

	/* Obtain the node's object identifier					*/
	my_node_objid = op_topo_parent (my_objid);

	/* Obtain subnet objid.									*/
	my_subnet_objid = op_topo_parent (my_node_objid);

	/* Obtain the values assigned to the various attributes	*/
	op_ima_obj_attr_get (my_objid, "Wireless LAN Parameters", &mac_params_comp_attr_objid);
	params_attr_objid = op_topo_child (mac_params_comp_attr_objid, OPC_OBJTYPE_GENERIC, 0);

	/* Determine the assigned MAC address.					*/
	op_ima_obj_attr_get (my_objid, "Address", &my_address);

	/* Obtain an address handle for resolving WLAN MAC addresses.				*/
	oms_aa_handle = oms_aa_address_handle_get ("MAC Addresses", "Address");

	/* Obtain the BSS_Id attribute to determine if BSS based network is used	*/ 
	op_ima_obj_attr_get (params_attr_objid, "BSS Identifier", &bss_id);

	/* Register the log handles and related flags.								*/
	config_log_handle	= op_prg_log_handle_create (OpC_Log_Category_Configuration, "Wireless LAN", "MAC Configuration", 128);
	drop_pkt_log_handle	= op_prg_log_handle_create (OpC_Log_Category_Protocol,      "Wireless LAN", "Data packet Drop", 128);
	drop_pkt_entry_log_flag = 0;

	// wsn log
	// creates a log file
	wsn_log = op_prg_log_handle_create (OpC_Log_Category_Protocol, "Wireless LAN", "WSN log",  1000000);

	/* Update the global variable if this is the first node to come up. If not the	*/ 
	/* first node, then check for mismatches. A subnet can be a traditional subnet	*/
	/* (i.e. a subnet with one BSS, this is the existing model) or a  BSS based		*/
	/* subnet where for every node the attribute BSS_Id is set to indicate to which */
	/* BSS a node belongs. If the global is set to traditional subnet and the this	*/
	/* node has its BSS_Id attribute set then log a warning message and recover		*/
	/* by considering the BSS_Id attribute setting as not used. If the global is	*/
	/* set to BSS based subnet and this node is not using its BSS_Id attribute 		*/
	/* then log an error message and stop the simulation.					 		*/
	if (bss_id_type == WlanC_Not_Set)
	{
		if (bss_id ==  WLAN_BSSID_NOT_USED)
		{
			bss_id_type = WlanC_Entire_Subnet ;
		}
		else
		{
			bss_id_type = WlanC_Bss_Divided_Subnet ;
		}
	}

	/* Configuration error checking */
	if (bss_id_type == WlanC_Entire_Subnet && bss_id != WLAN_BSSID_NOT_USED)
	{
		/* Recoverable mismatch, log warning and continue by enforcing			*/
		/* traditional subnet, i.e. force the bss_id variable to not used.		*/

		/* Write the warning message.											*/
		op_prg_log_entry_write (config_log_handle,
			"WARNING:\n"
			" A node with an explicit BSS \n"
			" assignment was found in a pure \n"
			" subnet.\n"
			"ACTION:\n"
			" The BSS identifier is set to\n"
			" the default value.\n"
			"CAUSE:\n"
			" There are some nodes in the\n"
			" network which have their BSS\n"
			" identifiers set to the default\n"
			" while the others have the\n"
			" default setting.\n"
			"SUGGESTION:\n"
			" Ensure that all nodes have the\n"
			" BSS identifier set to the default\n"
			" value or all of them are explicitly\n"
			" assigned.\n");
	}
	else if (bss_id_type == WlanC_Bss_Divided_Subnet && bss_id == WLAN_BSSID_NOT_USED)
	{
		/* Unrecoverable error-- not all BSS IDs have been configured. Cannot 	*/
		/* what the BSS ID should be, hence terminate.							*/

		wlan_mac_error ("BSS ID not set in a node which belongs to a network in which some BSS IDs are set", 
			"Please set a non-default BSS ID on all nodes in the network", OPC_NIL);
	}

	/* Use the subnet ID as the BSS ID if it is set to "Auto Assigned".			*/
	if (bss_id_type == WlanC_Entire_Subnet)
	{
		bss_id = my_subnet_objid;
	}

	/* Get model attributes.	*/
	op_ima_obj_attr_get (params_attr_objid, "Data Rate", &data_tx_rate);
	op_ima_obj_attr_get (params_attr_objid, "Fragmentation Threshold", &frag_threshold);
	op_ima_obj_attr_get (params_attr_objid, "Rts Threshold", &rts_threshold);
	op_ima_obj_attr_get (params_attr_objid, "Short Retry Limit", &short_retry_limit);
	op_ima_obj_attr_get (params_attr_objid, "Long Retry Limit", &long_retry_limit);
	op_ima_obj_attr_get (params_attr_objid, "Access Point Functionality", &ap_flag);	
	op_ima_obj_attr_get (params_attr_objid, "Buffer Size", &hld_max_size);
	op_ima_obj_attr_get (params_attr_objid, "Max Receive Lifetime", &max_receive_lifetime);
	op_ima_obj_attr_get (params_attr_objid, "Large Packet Processing", &accept_large_packets);

	/* Get simulation attributes. */
	op_ima_sim_attr_get (OPC_IMA_TOGGLE, "WLAN Beacon Efficiency Mode", &beacon_eff_mode);

	/* Extract beacon and PCF parameters.										*/
	op_ima_obj_attr_get (params_attr_objid, "PCF Parameters", &pcf_params_comp_attr_objid);
	subpcf_params_attr_objid = op_topo_child (pcf_params_comp_attr_objid, OPC_OBJTYPE_GENERIC, 0);
	op_ima_obj_attr_get (subpcf_params_attr_objid, "PCF Functionality", &pcf_flag);	
	op_ima_obj_attr_get (subpcf_params_attr_objid, "CFP Beacon Multiple", &cfp_prd);	
	op_ima_obj_attr_get (subpcf_params_attr_objid, "CFP Offset", &cfp_offset);	
	op_ima_obj_attr_get (subpcf_params_attr_objid, "CFP Interval", &cfp_length);	
	op_ima_obj_attr_get (subpcf_params_attr_objid, "Max Failed Polls", &max_poll_fails);	
	op_ima_obj_attr_get (subpcf_params_attr_objid, "Beacon Interval", &beacon_int);	

	ap_relay = OPC_TRUE;

	/* Check if there is an active AP controlling the medium during the CFP.*/
	if ((ap_flag == OPC_BOOLINT_ENABLED) && (pcf_flag == OPC_BOOLINT_ENABLED))
		active_pc = OPC_TRUE;
	else
		active_pc = OPC_FALSE;

	/* Load the appropriate physical layer characteristics.					*/	
	op_ima_obj_attr_get (params_attr_objid, "Physical Characteristics", &phy_char_flag);

⌨️ 快捷键说明

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