/* Process model C form file: ggsnProc.pr.c */
/* Portions of this file copyright 1986-2010 by OPNET Technologies, Inc. */


/*
 =========================== NOTE ==========================
 This file is automatically generated from ggsnProc.pr.m
 during a process model compilation.

 Do NOT manually edit this file.
 Manual edits will be lost during the next compilation.
 =========================== NOTE ==========================
*/



/* This variable carries the header into the object file */
const char ggsnProc_pr_c [] = "MIL_3_Tfile_Hdr_ 160A 30A op_runsim_opt 7 5170A544 5170A544 1 esl2-14 skathire 0 0 none none 0 0 none 0 0 0 0 0 0 0 0 2871 6                                                                                                                                                                                                                                                                                                                                                                                                   ";
#include <string.h>



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



/* Header Block */

#include "gtp.h"
#include "msRecord.h"

#define GTP_RCV_IN_STRM 0
#define GTP_XMT_OUT_STRM 0
#define FAST_XMT_OUT_STRM 1
#define SLOW_XMT_OUT_STRM 2


#define CREATE_PDP_CONTEXT_REQUEST 16
#define CREATE_PDP_CONTEXT_RESPONSE 17
#define DELETE_PDP_CONTEXT_REQUEST 20
#define DELETE_PDP_CONTEXT_RESPONSE 21
#define GTP_TPDU 255

/* transition macros */
#define GTP_FROM_SGSN (op_intrpt_type () == \
OPC_INTRPT_STRM && op_intrpt_strm () == GTP_RCV_IN_STRM)


/* End of Header Block */

#if !defined (VOSD_NO_FIN)
#undef	BIN
#undef	BOUT
#define	BIN		FIN_LOCAL_FIELD(_op_last_line_passed) = __LINE__ - _op_block_origin;
#define	BOUT	BIN
#define	BINIT	FIN_LOCAL_FIELD(_op_last_line_passed) = 0; _op_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 */
	char	                   		debugStr[100]                                   ;
	APN	                    		apn[20]                                         ;	/* Gateway in which MS data will be sent to */
	int	                    		connectionStatus[20]                            ;	/* indicates the transmitter index that the user data */
	                        		                                                	/* is sending through                                 */
	Stathandle	             		imsiGoingThroughFastLink_gsh                    ;
	Stathandle	             		imsiGoingThroughSlowLink_gsh                    ;
	} ggsnProc_state;

#define debugStr                		op_sv_ptr->debugStr
#define apn                     		op_sv_ptr->apn
#define connectionStatus        		op_sv_ptr->connectionStatus
#define imsiGoingThroughFastLink_gsh		op_sv_ptr->imsiGoingThroughFastLink_gsh
#define imsiGoingThroughSlowLink_gsh		op_sv_ptr->imsiGoingThroughSlowLink_gsh

/* These macro definitions 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_DEC
#undef FIN_PREAMBLE_CODE
#define FIN_PREAMBLE_DEC	ggsnProc_state *op_sv_ptr;
#define FIN_PREAMBLE_CODE	\
		op_sv_ptr = ((ggsnProc_state *)(OP_SIM_CONTEXT_PTR->_op_mod_state_ptr));


/* No Function Block */

#if !defined (VOSD_NO_FIN)
enum { _op_block_origin = __LINE__ };
#endif

/* Undefine optional tracing in FIN/FOUT/FRET */
/* The FSM has its own tracing code and the other */
/* functions should not have any tracing.		  */
#undef FIN_TRACING
#define FIN_TRACING

#undef FOUTRET_TRACING
#define FOUTRET_TRACING

#if defined (__cplusplus)
extern "C" {
#endif
	void ggsnProc (OP_SIM_CONTEXT_ARG_OPT);
	VosT_Obtype _op_ggsnProc_init (int * init_block_ptr);
	void _op_ggsnProc_diag (OP_SIM_CONTEXT_ARG_OPT);
	void _op_ggsnProc_terminate (OP_SIM_CONTEXT_ARG_OPT);
	VosT_Address _op_ggsnProc_alloc (VosT_Obtype, int);
	void _op_ggsnProc_svar (void *, const char *, void **);


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




/* Process model interrupt handling procedure */


void
ggsnProc (OP_SIM_CONTEXT_ARG_OPT)
	{
#if !defined (VOSD_NO_FIN)
	int _op_block_origin = 0;
#endif
	FIN_MT (ggsnProc ());

		{
		/* Temporary Variables */
		Packet * pkptr;
		/* End of Temporary Variables */


		FSM_ENTER ("ggsnProc")

		FSM_BLOCK_SWITCH
			{
			/*---------------------------------------------------------*/
			/** state (init) enter executives **/
			FSM_STATE_ENTER_FORCED_NOLABEL (0, "init", "ggsnProc [init enter execs]")
				FSM_PROFILE_SECTION_IN ("ggsnProc [init enter execs]", state0_enter_exec)
				{
				int i;
				
				// initialize the connection status
				// kept by the GGSN Process
				for (i=0; i<20; i++)
					{
					   connectionStatus[i] = -1;
					}
				
				// initialize the statistics
				imsiGoingThroughFastLink_gsh = op_stat_reg ("IMSI Going Through Fast Link",
					OPC_STAT_INDEX_NONE, OPC_STAT_GLOBAL);
				imsiGoingThroughSlowLink_gsh = op_stat_reg ("IMSI Going Through Slow Link",
					OPC_STAT_INDEX_NONE, OPC_STAT_GLOBAL);
				
				}
				FSM_PROFILE_SECTION_OUT (state0_enter_exec)

			/** state (init) exit executives **/
			FSM_STATE_EXIT_FORCED (0, "init", "ggsnProc [init exit execs]")


			/** state (init) transition processing **/
			FSM_TRANSIT_FORCE (1, state1_enter_exec, ;, "default", "", "init", "idle", "tr_0", "ggsnProc [init -> idle : default / ]")
				/*---------------------------------------------------------*/



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

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


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


			/** state (idle) transition processing **/
			FSM_PROFILE_SECTION_IN ("ggsnProc [idle trans conditions]", state1_trans_conds)
			FSM_INIT_COND (GTP_FROM_SGSN)
			FSM_DFLT_COND
			FSM_TEST_LOGIC ("idle")
			FSM_PROFILE_SECTION_OUT (state1_trans_conds)

			FSM_TRANSIT_SWITCH
				{
				FSM_CASE_TRANSIT (0, 2, state2_enter_exec, ;, "GTP_FROM_SGSN", "", "idle", "gtpMsgRcv", "tr_3", "ggsnProc [idle -> gtpMsgRcv : GTP_FROM_SGSN / ]")
				FSM_CASE_TRANSIT (1, 1, state1_enter_exec, ;, "default", "", "idle", "idle", "tr_6", "ggsnProc [idle -> idle : default / ]")
				}
				/*---------------------------------------------------------*/



			/** state (gtpMsgRcv) enter executives **/
			FSM_STATE_ENTER_FORCED (2, "gtpMsgRcv", state2_enter_exec, "ggsnProc [gtpMsgRcv enter execs]")
				FSM_PROFILE_SECTION_IN ("ggsnProc [gtpMsgRcv enter execs]", state2_enter_exec)
				{
				
				Packet * createPDPContextResponsePkPtr;
				Packet * deletePDPContextResponsePkPtr;
				Packet * encPkPtr;
				GTPEndUserAddress *gtpEndUserAddressPtr;
				APN *apnStructPtr;
				TID *tidStructPtr;
				QoS *qosStructPtr;
				int messageType;
				int pdpType, responseCause;
				
				pkptr = op_pk_get(GTP_RCV_IN_STRM);
				// retrieve the message type of message sent by SGSN
				op_pk_nfd_get (pkptr, "messageType", &messageType);
				
				// retrieve the TID from the incoming message
				// and set it in the corresponding response
				op_pk_nfd_access(pkptr, "TID", &tidStructPtr);
				
				switch (messageType) {
				    case CREATE_PDP_CONTEXT_REQUEST:
					
					    // retrieve the QoS from CreatePDPContextRequest
					    op_pk_nfd_access(pkptr, "qos", &qosStructPtr);
					    
					    // retrieve the end user address from CreatePDPContextRequest
					    op_pk_nfd_access(pkptr, "endUserAddress", &gtpEndUserAddressPtr);
					    pdpType = gtpEndUserAddressPtr->pdpType; 
					    //sprintf (debugStr, "Create PDP Context Request Recv. PDP Type %d", pdpType);
				        //op_prg_odb_print_major ("Debug message for GGSN", debugStr, OPC_NIL);
					
					    // retrieve the apn from CreatePDPContextRequest
					    //op_pk_nfd_access(pkptr, "apn", &apnStructPtr);
					    //strncpy(apn[tidStructPtr->imsi].apnNetworkID, apnStructPtr->apnNetworkID, 
						//         strlen(apnStructPtr->apnNetworkID)+1);	   
					
				        // reply SGSN with Create PDP Context Response
					    createPDPContextResponsePkPtr = op_pk_create_fmt ("CreatePDPContextResponse");
					    // set TID
						op_pk_nfd_set(createPDPContextResponsePkPtr, "TID", tidStructPtr,
				                 op_prg_mem_copy_create, op_prg_mem_free, sizeof (TID));
						
						// GGSN only supports requested QoS where the mean bit rate
						// is less than or equal to 8
						if (qosStructPtr->meanBitRate <= 8)
							{		
				            // determine the connection status based on the requested QoS
							if (qosStructPtr->meanBitRate == 8)
								{
								    connectionStatus[tidStructPtr->imsi] = FAST_XMT_OUT_STRM;
								}
						    else
								{
								    connectionStatus[tidStructPtr->imsi] = SLOW_XMT_OUT_STRM;
								}
							// set negotiated QoS (whici is equal to requested QoS)
						    op_pk_nfd_set(createPDPContextResponsePkPtr, "qos", qosStructPtr,
				                op_prg_mem_copy_create, op_prg_mem_free, sizeof (QoS));
						    // set the cause value (128 = request accepted)
						    responseCause = 128;
							}
						else
							{
				            // set the cause value (200 = service not supported)
							responseCause = 200;
							}
						op_pk_nfd_set(createPDPContextResponsePkPtr, "cause", 
				            responseCause);
					    op_pk_send(createPDPContextResponsePkPtr, GTP_XMT_OUT_STRM);
					    break;
					
					case DELETE_PDP_CONTEXT_REQUEST:	   
						
						// change the connection status back to inactive
						connectionStatus[tidStructPtr->imsi] = -1;
						
				        // reply SGSN with Delete PDP Context Response
					    deletePDPContextResponsePkPtr = op_pk_create_fmt ("DeletePDPContextResponse");
					    op_pk_nfd_set(deletePDPContextResponsePkPtr, "TID", tidStructPtr,
				                 op_prg_mem_copy_create, op_prg_mem_free, sizeof (TID));
					    op_pk_send(deletePDPContextResponsePkPtr, GTP_XMT_OUT_STRM);
					    break;
					
					case GTP_TPDU:
					
				        if (connectionStatus[tidStructPtr->imsi] != -1)
							{
						    // retrieve the encapsulated IP data packet
						    op_pk_nfd_get (pkptr, "data", &encPkPtr);
						    op_pk_send(encPkPtr, connectionStatus[tidStructPtr->imsi]);
				
							if (connectionStatus[tidStructPtr->imsi] == FAST_XMT_OUT_STRM)
								{
							    op_stat_write(imsiGoingThroughFastLink_gsh, tidStructPtr->imsi);
								}
							else if (connectionStatus[tidStructPtr->imsi] == SLOW_XMT_OUT_STRM)
								{
				                op_stat_write(imsiGoingThroughSlowLink_gsh, tidStructPtr->imsi);
								}
				
							/* 
							if (strcmp(apn[tidStructPtr->imsi].apnNetworkID, "abc.com") == 0)
							{
				                // send to abc.com gateway
							    op_pk_send(encPkPtr, FAST_XMT_OUT_STRM);
							}
						    else if (strcmp(apn[tidStructPtr->imsi].apnNetworkID, "msn.com") == 0)
							{
							   // send to msn.com gateway
							   op_pk_send(encPkPtr, SLOW_XMT_OUT_STRM);
							}
							*/
							}
					
					default:
					    break;
					}
				op_pk_destroy(pkptr);
				}
				FSM_PROFILE_SECTION_OUT (state2_enter_exec)

			/** state (gtpMsgRcv) exit executives **/
			FSM_STATE_EXIT_FORCED (2, "gtpMsgRcv", "ggsnProc [gtpMsgRcv exit execs]")


			/** state (gtpMsgRcv) transition processing **/
			FSM_TRANSIT_FORCE (1, state1_enter_exec, ;, "default", "", "gtpMsgRcv", "idle", "tr_4", "ggsnProc [gtpMsgRcv -> idle : default / ]")
				/*---------------------------------------------------------*/



			}


		FSM_EXIT (0,"ggsnProc")
		}
	}




void
_op_ggsnProc_diag (OP_SIM_CONTEXT_ARG_OPT)
	{
	/* No Diagnostic Block */
	}




void
_op_ggsnProc_terminate (OP_SIM_CONTEXT_ARG_OPT)
	{

	FIN_MT (_op_ggsnProc_terminate ())


	/* No Termination Block */

	Vos_Poolmem_Dealloc (op_sv_ptr);

	FOUT
	}


/* Undefine shortcuts to state variables to avoid */
/* syntax error in direct access to fields of */
/* local variable prs_ptr in _op_ggsnProc_svar function. */
#undef debugStr
#undef apn
#undef connectionStatus
#undef imsiGoingThroughFastLink_gsh
#undef imsiGoingThroughSlowLink_gsh

#undef FIN_PREAMBLE_DEC
#undef FIN_PREAMBLE_CODE

#define FIN_PREAMBLE_DEC
#define FIN_PREAMBLE_CODE

VosT_Obtype
_op_ggsnProc_init (int * init_block_ptr)
	{
	VosT_Obtype obtype = OPC_NIL;
	FIN_MT (_op_ggsnProc_init (init_block_ptr))

	obtype = Vos_Define_Object_Prstate ("proc state vars (ggsnProc)",
		sizeof (ggsnProc_state));
	*init_block_ptr = 0;

	FRET (obtype)
	}

VosT_Address
_op_ggsnProc_alloc (VosT_Obtype obtype, int init_block)
	{
#if !defined (VOSD_NO_FIN)
	int _op_block_origin = 0;
#endif
	ggsnProc_state * ptr;
	FIN_MT (_op_ggsnProc_alloc (obtype))

	ptr = (ggsnProc_state *)Vos_Alloc_Object (obtype);
	if (ptr != OPC_NIL)
		{
		ptr->_op_current_block = init_block;
#if defined (OPD_ALLOW_ODB)
		ptr->_op_current_state = "ggsnProc [init enter execs]";
#endif
		}
	FRET ((VosT_Address)ptr)
	}



void
_op_ggsnProc_svar (void * gen_ptr, const char * var_name, void ** var_p_ptr)
	{
	ggsnProc_state		*prs_ptr;

	FIN_MT (_op_ggsnProc_svar (gen_ptr, var_name, var_p_ptr))

	if (var_name == OPC_NIL)
		{
		*var_p_ptr = (void *)OPC_NIL;
		FOUT
		}
	prs_ptr = (ggsnProc_state *)gen_ptr;

	if (strcmp ("debugStr" , var_name) == 0)
		{
		*var_p_ptr = (void *) (prs_ptr->debugStr);
		FOUT
		}
	if (strcmp ("apn" , var_name) == 0)
		{
		*var_p_ptr = (void *) (prs_ptr->apn);
		FOUT
		}
	if (strcmp ("connectionStatus" , var_name) == 0)
		{
		*var_p_ptr = (void *) (prs_ptr->connectionStatus);
		FOUT
		}
	if (strcmp ("imsiGoingThroughFastLink_gsh" , var_name) == 0)
		{
		*var_p_ptr = (void *) (&prs_ptr->imsiGoingThroughFastLink_gsh);
		FOUT
		}
	if (strcmp ("imsiGoingThroughSlowLink_gsh" , var_name) == 0)
		{
		*var_p_ptr = (void *) (&prs_ptr->imsiGoingThroughSlowLink_gsh);
		FOUT
		}
	*var_p_ptr = (void *)OPC_NIL;

	FOUT
	}

