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



/* This variable carries the header into the object file */
const char xxx_proc_element_up_pr_c [] = "MIL_3_Tfile_Hdr_ 145A 30A op_runsim 7 59052F53 59052F53 1 winxpvm FEKT 0 0 none none 0 0 none 0 0 0 0 0 0 0 0 1e80 8                                                                                                                                                                                                                                                                                                                                                                                                           ";
#include <string.h>



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



/* Header Block */

#define PK_ARRVL (op_intrpt_type () == OPC_INTRPT_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 */
	Objid	                  		ID                                              ;
	int	                    		adresa_1                                        ;
	int	                    		adresa_2                                        ;
	int	                    		adresa_3                                        ;
	int	                    		adresa_4                                        ;
	Objid	                  		ppid                                            ;
	} xxx_proc_element_up_state;

#define ID                      		op_sv_ptr->ID
#define adresa_1                		op_sv_ptr->adresa_1
#define adresa_2                		op_sv_ptr->adresa_2
#define adresa_3                		op_sv_ptr->adresa_3
#define adresa_4                		op_sv_ptr->adresa_4
#define ppid                    		op_sv_ptr->ppid

/* 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	xxx_proc_element_up_state *op_sv_ptr;
#define FIN_PREAMBLE_CODE	\
		op_sv_ptr = ((xxx_proc_element_up_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 xxx_proc_element_up (OP_SIM_CONTEXT_ARG_OPT);
	VosT_Obtype _op_xxx_proc_element_up_init (int * init_block_ptr);
	void _op_xxx_proc_element_up_diag (OP_SIM_CONTEXT_ARG_OPT);
	void _op_xxx_proc_element_up_terminate (OP_SIM_CONTEXT_ARG_OPT);
	VosT_Address _op_xxx_proc_element_up_alloc (VosT_Obtype, int);
	void _op_xxx_proc_element_up_svar (void *, const char *, void **);


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




/* Process model interrupt handling procedure */


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

		{


		FSM_ENTER_NO_VARS ("xxx_proc_element_up")

		FSM_BLOCK_SWITCH
			{
			/*---------------------------------------------------------*/
			/** state (idle) enter executives **/
			FSM_STATE_ENTER_UNFORCED (0, "idle", state0_enter_exec, "xxx_proc_element_up [idle enter execs]")
				FSM_PROFILE_SECTION_IN ("xxx_proc_element_up [idle enter execs]", state0_enter_exec)
				{
				ID = op_id_self();
				ppid = op_topo_parent(ID);
				
				op_ima_obj_attr_get(ppid,"OLT 1 ID",&adresa_1);
				op_ima_obj_attr_get(ppid,"OLT 2 ID",&adresa_2);
				op_ima_obj_attr_get(ppid,"OLT 3 ID",&adresa_3);
				op_ima_obj_attr_get(ppid,"OLT 4 ID",&adresa_4);
				}
				FSM_PROFILE_SECTION_OUT (state0_enter_exec)

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


			/** state (idle) exit executives **/
			FSM_STATE_EXIT_UNFORCED (0, "idle", "xxx_proc_element_up [idle exit execs]")


			/** state (idle) transition processing **/
			FSM_TRANSIT_ONLY ((PK_ARRVL), 1, state1_enter_exec, ;, idle, "PK_ARRVL", "", "idle", "rk_resend", "tr_0", "xxx_proc_element_up [idle -> rk_resend : PK_ARRVL / ]")
				/*---------------------------------------------------------*/



			/** state (rk_resend) enter executives **/
			FSM_STATE_ENTER_FORCED (1, "rk_resend", state1_enter_exec, "xxx_proc_element_up [rk_resend enter execs]")
				FSM_PROFILE_SECTION_IN ("xxx_proc_element_up [rk_resend enter execs]", state1_enter_exec)
				{
				Packet * pk_arrived;
				
				
				
				int destination;
				
				
				pk_arrived = op_pk_get (op_intrpt_strm ());
				//op_pk_nfd_get (pk_arrived,"dest_address", &destination);
				
				op_pk_nfd_get (pk_arrived,"ONU-ID", &destination);
				
				
				if(destination<adresa_2 && destination >=adresa_1){
				
					op_pk_send (pk_arrived, 0);
					
				}
				if(destination<adresa_3 && destination >=adresa_2){
				
					op_pk_send (pk_arrived, 1);
					
				}
				if(destination<adresa_4 && destination >=adresa_3){
				
					op_pk_send (pk_arrived, 2);
					
				}
				if(destination >=adresa_4){
				
					op_pk_send (pk_arrived, 3);
					
				}
				/*
				if(destination=1){
				op_pk_send (pk_arrived, 0);	
				}
				
				else if(destination=2){
				op_pk_send (pk_arrived, 1);	
				}
				
				else if(destination=3){
				op_pk_send (pk_arrived, 2);	
				}
				
				else{
				op_pk_send (pk_arrived, 3);	
				}
				*/
				}
				FSM_PROFILE_SECTION_OUT (state1_enter_exec)

			/** state (rk_resend) exit executives **/
			FSM_STATE_EXIT_FORCED (1, "rk_resend", "xxx_proc_element_up [rk_resend exit execs]")


			/** state (rk_resend) transition processing **/
			FSM_TRANSIT_FORCE (0, state0_enter_exec, ;, "default", "", "rk_resend", "idle", "tr_2", "xxx_proc_element_up [rk_resend -> idle : default / ]")
				/*---------------------------------------------------------*/



			}


		FSM_EXIT (0,"xxx_proc_element_up")
		}
	}




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




void
_op_xxx_proc_element_up_terminate (OP_SIM_CONTEXT_ARG_OPT)
	{

	FIN_MT (_op_xxx_proc_element_up_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_xxx_proc_element_up_svar function. */
#undef ID
#undef adresa_1
#undef adresa_2
#undef adresa_3
#undef adresa_4
#undef ppid

#undef FIN_PREAMBLE_DEC
#undef FIN_PREAMBLE_CODE

#define FIN_PREAMBLE_DEC
#define FIN_PREAMBLE_CODE

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

	obtype = Vos_Define_Object_Prstate ("proc state vars (xxx_proc_element_up)",
		sizeof (xxx_proc_element_up_state));
	*init_block_ptr = 0;

	FRET (obtype)
	}

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

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



void
_op_xxx_proc_element_up_svar (void * gen_ptr, const char * var_name, void ** var_p_ptr)
	{
	xxx_proc_element_up_state		*prs_ptr;

	FIN_MT (_op_xxx_proc_element_up_svar (gen_ptr, var_name, var_p_ptr))

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

	if (strcmp ("ID" , var_name) == 0)
		{
		*var_p_ptr = (void *) (&prs_ptr->ID);
		FOUT
		}
	if (strcmp ("adresa_1" , var_name) == 0)
		{
		*var_p_ptr = (void *) (&prs_ptr->adresa_1);
		FOUT
		}
	if (strcmp ("adresa_2" , var_name) == 0)
		{
		*var_p_ptr = (void *) (&prs_ptr->adresa_2);
		FOUT
		}
	if (strcmp ("adresa_3" , var_name) == 0)
		{
		*var_p_ptr = (void *) (&prs_ptr->adresa_3);
		FOUT
		}
	if (strcmp ("adresa_4" , var_name) == 0)
		{
		*var_p_ptr = (void *) (&prs_ptr->adresa_4);
		FOUT
		}
	if (strcmp ("ppid" , var_name) == 0)
		{
		*var_p_ptr = (void *) (&prs_ptr->ppid);
		FOUT
		}
	*var_p_ptr = (void *)OPC_NIL;

	FOUT
	}

