/* Process model C form file: csma_tx.pr.c */
/* Portions of this file copyright 1992, 1996, 1998 by MIL 3, Inc. */



/* This variable carries the header into the object file */
static const char csma_tx_pr_c [] = "MIL_3_Tfile_Hdr_ 60L 30A opnet 7 38E1175D 38E1175D 1 barley nse 0 0 none none 0 0 none 0 0 0 0 0 0";


/* OPNET system definitions */
#include <opnet.h>

#if defined (__cplusplus)
extern "C" {
#endif
FSM_EXT_DECS
#if defined (__cplusplus)
} /* end of 'extern "C"' */
#endif


/* Header Block */

/* Input stream from ideal generator module */
#define IN_STRM   0

/* Output stream to bus transmitter module */
#define OUT_STRM 0

/* Conditional Macros */
#define PKT_ARVL (op_intrpt_type()== OPC_INTRPT_STRM)

/* Global variable */
extern int subm_pkts;

/*Input statistics indices */
#define CH_BUSY_STAT 0 

/*Conditional Macros */
#define FREE (op_stat_local_read (CH_BUSY_STAT) == 0.0)
#define PKTS_QUEUED (!op_strm_empty (IN_STRM))
#define CH_GOES_FREE (op_intrpt_type() == OPC_INTRPT_STAT)


/* End of Header Block */


#if !defined (VOSD_NO_FIN)
#undef	BIN
#undef	BOUT
#define	BIN		_fstack_local_info.last_line_passed = __LINE__ - _block_origin;
#define	BOUT	BIN
#define	BINIT	_fstack_local_info.last_line_passed = 0; _block_origin = __LINE__;
#else
#define	BINIT
#endif /* #if !defined (VOSD_NO_FIN) */



/* State variable definitions */
typedef struct
	{
	/* Internal state tracking for FSM */
	FSM_SYS_STATE
	/* State Variables */
	int                     		max_packet_count ;
	} csma_tx_state;

#define pr_state_ptr            		((csma_tx_state*) SimI_Mod_State_Ptr)
#define max_packet_count        		pr_state_ptr->max_packet_count

/* This macro definition will define a local variable called	*/
/* "op_sv_ptr" in each function containing a FIN statement.	*/
/* This variable points to the state variable data structure,	*/
/* and can be used from a C debugger to display their values.	*/
#undef FIN_PREAMBLE
#define FIN_PREAMBLE	csma_tx_state *op_sv_ptr = pr_state_ptr;


/* No Function Block */

enum { _block_origin = __LINE__ };

#if defined (__cplusplus)
extern "C" {
#endif
	void csma_tx (void);
	Compcode csma_tx_init (void **);
	void csma_tx_diag (void);
	void csma_tx_terminate (void);
	void csma_tx_svar (void *, const char *, char **);
#if defined (__cplusplus)
} /* end of 'extern "C"' */
#endif




/* Process model interrupt handling procedure */


void
csma_tx (void)
	{
	int _block_origin = 0;
	FIN (csma_tx ());
	if (1)
		{
		/*Output packet */
		Packet* out_pkt;


		FSM_ENTER (csma_tx)

		FSM_BLOCK_SWITCH
			{
			/*---------------------------------------------------------*/
			/** state (idle) enter executives **/
			FSM_STATE_ENTER_UNFORCED (0, state0_enter_exec, "idle", "csma_tx () [idle enter execs]")
				{
				/* Get the maximum packet count,*/ 
				/* set at simulation run-time */
				op_ima_sim_attr_get (OPC_IMA_INTEGER,
				"max packet count", &max_packet_count);
				
				}


			/** blocking after enter executives of unforced state. **/
			FSM_EXIT (1,csma_tx)


			/** state (idle) exit executives **/
			FSM_STATE_EXIT_UNFORCED (0, state0_exit_exec, "idle", "csma_tx () [idle exit execs]")
				{
				}


			/** state (idle) transition processing **/
			FSM_INIT_COND (PKT_ARVL && FREE)
			FSM_TEST_COND (PKT_ARVL && !FREE)
			FSM_DFLT_COND
			FSM_TEST_LOGIC ("idle")

			FSM_TRANSIT_SWITCH
				{
				FSM_CASE_TRANSIT (0, 1, state1_enter_exec, ;)
				FSM_CASE_TRANSIT (1, 2, state2_enter_exec, ;)
				FSM_CASE_TRANSIT (2, 0, state0_enter_exec, ;)
				}
				/*---------------------------------------------------------*/



			/** state (tx_pkt) enter executives **/
			FSM_STATE_ENTER_FORCED (1, state1_enter_exec, "tx_pkt", "csma_tx () [tx_pkt enter execs]")
				{
				/* A packet has arrived for transmission. Aquire      */
				/* the packet from the input stream, send the packet, */
				/* and update the global submitted packet counter     */
				out_pkt = op_pk_get(IN_STRM);
				op_pk_send (out_pkt, OUT_STRM);
				++subm_pkts;
				
				/* Compare the total number of packets submitted with   */
				/* the maximum of set  for this simulation run, If equal*/
				/* end the simulation run                               */
				
				if (subm_pkts == max_packet_count)
				    {op_sim_end ("Simulation ended when max packet count reached.",
				                 "", "", "");
				     }
				}


			/** state (tx_pkt) exit executives **/
			FSM_STATE_EXIT_FORCED (1, state1_exit_exec, "tx_pkt", "csma_tx () [tx_pkt exit execs]")
				{
				}


			/** state (tx_pkt) transition processing **/
			FSM_INIT_COND (PKTS_QUEUED && FREE)
			FSM_TEST_COND (PKTS_QUEUED && !FREE)
			FSM_DFLT_COND
			FSM_TEST_LOGIC ("tx_pkt")

			FSM_TRANSIT_SWITCH
				{
				FSM_CASE_TRANSIT (0, 1, state1_enter_exec, ;)
				FSM_CASE_TRANSIT (1, 2, state2_enter_exec, ;)
				FSM_CASE_TRANSIT (2, 0, state0_enter_exec, ;)
				}
				/*---------------------------------------------------------*/



			/** state (wt_free) enter executives **/
			FSM_STATE_ENTER_UNFORCED (2, state2_enter_exec, "wt_free", "csma_tx () [wt_free enter execs]")
				{
				}


			/** blocking after enter executives of unforced state. **/
			FSM_EXIT (5,csma_tx)


			/** state (wt_free) exit executives **/
			FSM_STATE_EXIT_UNFORCED (2, state2_exit_exec, "wt_free", "csma_tx () [wt_free exit execs]")
				{
				}


			/** state (wt_free) transition processing **/
			FSM_INIT_COND (CH_GOES_FREE)
			FSM_DFLT_COND
			FSM_TEST_LOGIC ("wt_free")

			FSM_TRANSIT_SWITCH
				{
				FSM_CASE_TRANSIT (0, 1, state1_enter_exec, ;)
				FSM_CASE_TRANSIT (1, 2, state2_enter_exec, ;)
				}
				/*---------------------------------------------------------*/



			}


		FSM_EXIT (0,csma_tx)
		}
	}


Compcode
csma_tx_init (void ** gen_state_pptr)
	{
	int _block_origin = 0;
	static VosT_Cm_Obtype	obtype = OPC_NIL;

	extern void				Vos_Vnop (void);

	FIN (csma_tx_init (gen_state_pptr))

	if (obtype == OPC_NIL)
		{
		/* Initialize memory management */
		if (Vos_Catmem_Register ("proc state vars (csma_tx)",
			sizeof (csma_tx_state), Vos_Vnop, &obtype) == VOSC_FAILURE)
			{
			FRET (OPC_COMPCODE_FAILURE)
			}
		}

	*gen_state_pptr = Vos_Catmem_Alloc (obtype, 1);
	if (*gen_state_pptr == OPC_NIL)
		{
		FRET (OPC_COMPCODE_FAILURE)
		}
	else
		{
		/* Initialize FSM handling */
		((csma_tx_state *)(*gen_state_pptr))->current_block = 0;

		FRET (OPC_COMPCODE_SUCCESS)
		}
	}



void
csma_tx_diag (void)
	{
	/* No Diagnostic Block */
	}




void
csma_tx_terminate (void)
	{
	int _block_origin = __LINE__;

	FIN (csma_tx_terminate (void))

	if (1)
		{
		/*Output packet */
		Packet* out_pkt;

		/* No Termination Block */

		}
	Vos_Catmem_Dealloc (pr_state_ptr);

	FOUT;
	}


/* Undefine shortcuts to state variables to avoid */
/* syntax error in direct access to fields of */
/* local variable prs_ptr in csma_tx_svar function. */
#undef max_packet_count



void
csma_tx_svar (void * gen_ptr, const char * var_name, char ** var_p_ptr)
	{
	csma_tx_state		*prs_ptr;

	FIN (csma_tx_svar (gen_ptr, var_name, var_p_ptr))

	*var_p_ptr = (char *)VOS_NIL;
	prs_ptr = (csma_tx_state *)gen_ptr;

	if (Vos_String_Equal ("max_packet_count" , var_name))
		{
		*var_p_ptr = (char *) (&prs_ptr->max_packet_count);
		goto func_return;
		}

func_return:

	FOUT;
	}

