/* Process model C form file: attachProcedure.pr.c */
/* Portions of this file copyright 1992-2000 by OPNET, Inc. */



/* This variable carries the header into the object file */
static const char attachProcedure_pr_c [] = "MIL_3_Tfile_Hdr_ 70B 30A opnet 7 3BDC9042 3BDC9042 1 brc9ccase07 rng 0 0 none none 0 0 none 0 0 0 0 0 0                                                                                                                                                                                                                                                                                                                                                                                                                        ";
#include <string.h>



/* 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 */

#define ATTACH_RCV_IN_STRM 0
#define ATTACH_SRC_IN_STRM 1
#define ATTACH_XMT_OUT_STRM 0

#define ATTACH_ACCEPT 0x02


/* transition macros */
#define ATTACH_SRC_ARRIVAL (op_intrpt_type () == \
OPC_INTRPT_STRM && op_intrpt_strm () == ATTACH_SRC_IN_STRM)

#define ATTACH_RCV_ARRIVAL (op_intrpt_type () == \
OPC_INTRPT_STRM && op_intrpt_strm () == ATTACH_RCV_IN_STRM)


/* End of Header Block */


#if !defined (VOSD_NO_FIN)
#undef	BIN
#undef	BOUT
#define	BIN		FIN_LOCAL_FIELD(last_line_passed) = __LINE__ - _block_origin;
#define	BOUT	BIN
#define	BINIT	FIN_LOCAL_FIELD(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 */
	Distribution *	         		imsi_distribution;
	Stathandle	             		attachProcessTime_gsh;
	} attachProcedure_state;

#define pr_state_ptr            		((attachProcedure_state*) SimI_Mod_State_Ptr)
#define imsi_distribution       		pr_state_ptr->imsi_distribution
#define attachProcessTime_gsh   		pr_state_ptr->attachProcessTime_gsh

/* 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	attachProcedure_state *op_sv_ptr = pr_state_ptr;


/* No Function Block */

enum { _block_origin = __LINE__ };

#if defined (__cplusplus)
extern "C" {
#endif
	void attachProcedure (void);
	Compcode attachProcedure_init (void **);
	void attachProcedure_diag (void);
	void attachProcedure_terminate (void);
	void attachProcedure_svar (void *, const char *, char **);
#if defined (__cplusplus)
} /* end of 'extern "C"' */
#endif




/* Process model interrupt handling procedure */


void
attachProcedure (void)
	{
	int _block_origin = 0;
	FIN (attachProcedure ());
	if (1)
		{
		Packet *pkptr;
		double attachProccessTime;
		double sentTime;


		FSM_ENTER (attachProcedure)

		FSM_BLOCK_SWITCH
			{
			/*---------------------------------------------------------*/
			/** state (init) enter executives **/
			FSM_STATE_ENTER_FORCED (0, state0_enter_exec, "init", "attachProcedure () [init enter execs]")
				{
				// currently supports only 1000 subscribers
				imsi_distribution = op_dist_load ("uniform_int", 0, 999);
				attachProcessTime_gsh = op_stat_reg ("Attach Process Time",
					OPC_STAT_INDEX_NONE, OPC_STAT_GLOBAL);
				
				}


			/** state (init) exit executives **/
			FSM_STATE_EXIT_FORCED (0, state0_exit_exec, "init", "attachProcedure () [init exit execs]")
				{
				}


			/** state (init) transition processing **/
			FSM_TRANSIT_FORCE (1, state1_enter_exec, ;)
				/*---------------------------------------------------------*/



			/** state (idle) enter executives **/
			FSM_STATE_ENTER_UNFORCED (1, state1_enter_exec, "idle", "attachProcedure () [idle enter execs]")
				{
				}


			/** blocking after enter executives of unforced state. **/
			FSM_EXIT (3,attachProcedure)


			/** state (idle) exit executives **/
			FSM_STATE_EXIT_UNFORCED (1, state1_exit_exec, "idle", "attachProcedure () [idle exit execs]")
				{
				}


			/** state (idle) transition processing **/
			FSM_INIT_COND (ATTACH_SRC_ARRIVAL)
			FSM_TEST_COND (ATTACH_RCV_ARRIVAL)
			FSM_DFLT_COND
			FSM_TEST_LOGIC ("idle")

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



			/** state (attachXmt) enter executives **/
			FSM_STATE_ENTER_FORCED (2, state2_enter_exec, "attachXmt", "attachProcedure () [attachXmt enter execs]")
				{
				pkptr = op_pk_get (ATTACH_SRC_IN_STRM);
				sentTime = op_sim_time ();
				// set the attach request sent time
				op_pk_nfd_set (pkptr, "attachReqSentTime",
					sentTime);
				// set the imsi of the MS
				op_pk_nfd_set (pkptr, "imsi",
					(int)op_dist_outcome(imsi_distribution));
				op_pk_send(pkptr, ATTACH_XMT_OUT_STRM);
				}


			/** state (attachXmt) exit executives **/
			FSM_STATE_EXIT_FORCED (2, state2_exit_exec, "attachXmt", "attachProcedure () [attachXmt exit execs]")
				{
				}


			/** state (attachXmt) transition processing **/
			FSM_TRANSIT_FORCE (1, state1_enter_exec, ;)
				/*---------------------------------------------------------*/



			/** state (attachRcv) enter executives **/
			FSM_STATE_ENTER_FORCED (3, state3_enter_exec, "attachRcv", "attachProcedure () [attachRcv enter execs]")
				{
				
				double attachReqSentTime;
				Packet * attachCompletePkPtr;
				int messageType;
				int ptmsi;
				
				pkptr = op_pk_get(ATTACH_RCV_IN_STRM);
				// retrieve the message type of message sent by SGSN
				op_pk_nfd_get (pkptr, "messageType", &messageType);
				
				switch (messageType) {
				    case ATTACH_ACCEPT:
				        op_pk_nfd_get (pkptr, "attachReqSentTime", &attachReqSentTime);
				        attachProccessTime = op_sim_time() - attachReqSentTime;
				        op_stat_write(attachProcessTime_gsh, attachProccessTime);
					
					    // retrieve the allocatedPTMSI from attach accept
					    op_pk_nfd_get(pkptr, "allocatedPTMSI", &ptmsi);
				        // reply SGSN with AttachComplete
					    attachCompletePkPtr = op_pk_create_fmt ("attachComplete");
					    op_pk_nfd_set(attachCompletePkPtr, "ptmsi", ptmsi);
					    op_pk_send(attachCompletePkPtr, ATTACH_XMT_OUT_STRM);
					    break;
					default:
					    break;
					}
				op_pk_destroy(pkptr);
				}


			/** state (attachRcv) exit executives **/
			FSM_STATE_EXIT_FORCED (3, state3_exit_exec, "attachRcv", "attachProcedure () [attachRcv exit execs]")
				{
				}


			/** state (attachRcv) transition processing **/
			FSM_TRANSIT_FORCE (1, state1_enter_exec, ;)
				/*---------------------------------------------------------*/



			}


		FSM_EXIT (0,attachProcedure)
		}
	}

#if defined (__cplusplus)
	extern "C" { 
#endif
	extern VosT_Fun_Status Vos_Catmem_Register (const char * , int , VosT_Void_Null_Proc, VosT_Address *);
	extern VosT_Address Vos_Catmem_Alloc (VosT_Address, size_t);
	extern VosT_Fun_Status Vos_Catmem_Dealloc (VosT_Address);
#if defined (__cplusplus)
	}
#endif


Compcode
attachProcedure_init (void ** gen_state_pptr)
	{
	int _block_origin = 0;
	static VosT_Address	obtype = OPC_NIL;

	FIN (attachProcedure_init (gen_state_pptr))

	if (obtype == OPC_NIL)
		{
		/* Initialize memory management */
		if (Vos_Catmem_Register ("proc state vars (attachProcedure)",
			sizeof (attachProcedure_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 */
		((attachProcedure_state *)(*gen_state_pptr))->current_block = 0;

		FRET (OPC_COMPCODE_SUCCESS)
		}
	}



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




void
attachProcedure_terminate (void)
	{
	int _block_origin = __LINE__;

	FIN (attachProcedure_terminate (void))

	if (1)
		{
		Packet *pkptr;
		double attachProccessTime;
		double sentTime;

		/* 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 attachProcedure_svar function. */
#undef imsi_distribution
#undef attachProcessTime_gsh



void
attachProcedure_svar (void * gen_ptr, const char * var_name, char ** var_p_ptr)
	{
	attachProcedure_state		*prs_ptr;

	FIN (attachProcedure_svar (gen_ptr, var_name, var_p_ptr))

	if (var_name == OPC_NIL)
		{
		*var_p_ptr = (char *)OPC_NIL;
		FOUT;
		}
	prs_ptr = (attachProcedure_state *)gen_ptr;

	if (strcmp ("imsi_distribution" , var_name) == 0)
		{
		*var_p_ptr = (char *) (&prs_ptr->imsi_distribution);
		FOUT;
		}
	if (strcmp ("attachProcessTime_gsh" , var_name) == 0)
		{
		*var_p_ptr = (char *) (&prs_ptr->attachProcessTime_gsh);
		FOUT;
		}
	*var_p_ptr = (char *)OPC_NIL;

	FOUT;
	}

