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



/* This variable carries the header into the object file */
const char PC_olt_prijem_pr_c [] = "MIL_3_Tfile_Hdr_ 145A 30A modeler 7 58E88F6A 58E88F6A 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                                              ;
	Objid	                  		ppid                                            ;
	int	                    		Port                                            ;
	int	                    		rezim                                           ;
	int	                    		destination                                     ;
	double	                 		pozadovany_grant                                ;
	} PC_olt_prijem_state;

#define ID                      		op_sv_ptr->ID
#define ppid                    		op_sv_ptr->ppid
#define Port                    		op_sv_ptr->Port
#define rezim                   		op_sv_ptr->rezim
#define destination             		op_sv_ptr->destination
#define pozadovany_grant        		op_sv_ptr->pozadovany_grant

/* 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	PC_olt_prijem_state *op_sv_ptr;
#define FIN_PREAMBLE_CODE	\
		op_sv_ptr = ((PC_olt_prijem_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 PC_olt_prijem (OP_SIM_CONTEXT_ARG_OPT);
	VosT_Obtype _op_PC_olt_prijem_init (int * init_block_ptr);
	void _op_PC_olt_prijem_diag (OP_SIM_CONTEXT_ARG_OPT);
	void _op_PC_olt_prijem_terminate (OP_SIM_CONTEXT_ARG_OPT);
	VosT_Address _op_PC_olt_prijem_alloc (VosT_Obtype, int);
	void _op_PC_olt_prijem_svar (void *, const char *, void **);


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




/* Process model interrupt handling procedure */


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

		{


		FSM_ENTER_NO_VARS ("PC_olt_prijem")

		FSM_BLOCK_SWITCH
			{
			/*---------------------------------------------------------*/
			/** state (st_0) enter executives **/
			FSM_STATE_ENTER_UNFORCED (0, "st_0", state0_enter_exec, "PC_olt_prijem [st_0 enter execs]")
				FSM_PROFILE_SECTION_IN ("PC_olt_prijem [st_0 enter execs]", state0_enter_exec)
				{
				ID = op_id_self();
				ppid = op_topo_parent(ID);
				
				op_ima_obj_attr_get(ppid,"Mode",&rezim);
				op_ima_obj_attr_get(ppid,"Basic ONU-ID",&Port);
				
				}
				FSM_PROFILE_SECTION_OUT (state0_enter_exec)

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


			/** state (st_0) exit executives **/
			FSM_STATE_EXIT_UNFORCED (0, "st_0", "PC_olt_prijem [st_0 exit execs]")


			/** state (st_0) transition processing **/
			FSM_TRANSIT_ONLY ((PK_ARRVL), 1, state1_enter_exec, ;, st_0, "PK_ARRVL", "", "st_0", "st_1", "tr_0", "PC_olt_prijem [st_0 -> st_1 : PK_ARRVL / ]")
				/*---------------------------------------------------------*/



			/** state (st_1) enter executives **/
			FSM_STATE_ENTER_FORCED (1, "st_1", state1_enter_exec, "PC_olt_prijem [st_1 enter execs]")
				FSM_PROFILE_SECTION_IN ("PC_olt_prijem [st_1 enter execs]", state1_enter_exec)
				{
				Packet * pk_arrived;
				pk_arrived = op_pk_get (op_intrpt_strm ());
				
				if(rezim==1)
				{
					
				
				
				op_pk_nfd_get (pk_arrived,"ONU-ID", &destination);
				op_pk_nfd_get (pk_arrived,"BufOcc", &pozadovany_grant);
				
				printf("Prijaty pozadavek: %.9f\n",pozadovany_grant);
				
				if(destination==Port){
				op_ima_obj_attr_set(ppid,"ONU 1",pozadovany_grant);}
				if(destination==Port+1){
				op_ima_obj_attr_set(ppid,"ONU 2",pozadovany_grant);}
				if(destination==Port+2){
				op_ima_obj_attr_set(ppid,"ONU 3",pozadovany_grant);}
				if(destination==Port+3){
				op_ima_obj_attr_set(ppid,"ONU 4",pozadovany_grant);}
				if(destination==Port+4){
				op_ima_obj_attr_set(ppid,"ONU 5",pozadovany_grant);}
				if(destination==Port+5){
				op_ima_obj_attr_set(ppid,"ONU 6",pozadovany_grant);}
				if(destination==Port+6){
				op_ima_obj_attr_set(ppid,"ONU 7",pozadovany_grant);}
				if(destination==Port+7){
				op_ima_obj_attr_set(ppid,"ONU 8",pozadovany_grant);}
				
				op_pk_send (pk_arrived, 0);
				
				}
				
				}
				FSM_PROFILE_SECTION_OUT (state1_enter_exec)

			/** state (st_1) exit executives **/
			FSM_STATE_EXIT_FORCED (1, "st_1", "PC_olt_prijem [st_1 exit execs]")


			/** state (st_1) transition processing **/
			FSM_TRANSIT_FORCE (0, state0_enter_exec, ;, "default", "", "st_1", "st_0", "tr_1", "PC_olt_prijem [st_1 -> st_0 : default / ]")
				/*---------------------------------------------------------*/



			}


		FSM_EXIT (0,"PC_olt_prijem")
		}
	}




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




void
_op_PC_olt_prijem_terminate (OP_SIM_CONTEXT_ARG_OPT)
	{

	FIN_MT (_op_PC_olt_prijem_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_PC_olt_prijem_svar function. */
#undef ID
#undef ppid
#undef Port
#undef rezim
#undef destination
#undef pozadovany_grant

#undef FIN_PREAMBLE_DEC
#undef FIN_PREAMBLE_CODE

#define FIN_PREAMBLE_DEC
#define FIN_PREAMBLE_CODE

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

	obtype = Vos_Define_Object_Prstate ("proc state vars (PC_olt_prijem)",
		sizeof (PC_olt_prijem_state));
	*init_block_ptr = 0;

	FRET (obtype)
	}

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

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



void
_op_PC_olt_prijem_svar (void * gen_ptr, const char * var_name, void ** var_p_ptr)
	{
	PC_olt_prijem_state		*prs_ptr;

	FIN_MT (_op_PC_olt_prijem_svar (gen_ptr, var_name, var_p_ptr))

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

	if (strcmp ("ID" , var_name) == 0)
		{
		*var_p_ptr = (void *) (&prs_ptr->ID);
		FOUT
		}
	if (strcmp ("ppid" , var_name) == 0)
		{
		*var_p_ptr = (void *) (&prs_ptr->ppid);
		FOUT
		}
	if (strcmp ("Port" , var_name) == 0)
		{
		*var_p_ptr = (void *) (&prs_ptr->Port);
		FOUT
		}
	if (strcmp ("rezim" , var_name) == 0)
		{
		*var_p_ptr = (void *) (&prs_ptr->rezim);
		FOUT
		}
	if (strcmp ("destination" , var_name) == 0)
		{
		*var_p_ptr = (void *) (&prs_ptr->destination);
		FOUT
		}
	if (strcmp ("pozadovany_grant" , var_name) == 0)
		{
		*var_p_ptr = (void *) (&prs_ptr->pozadovany_grant);
		FOUT
		}
	*var_p_ptr = (void *)OPC_NIL;

	FOUT
	}

