/*
 ******************************************************************************
 *  Copyright 1993 DeltaLink bv. All Rights Reserved.
 ******************************************************************************
 *
 * DeltaLink FactoryLink Utilities Header file
 *
 * File: fl_utils.h
 *
 */
#ifndef _FLUTILS_INCLUDED /* { */
#define _FLUTILS_INCLUDED 1

#define MINIMUM_LEVEL 1

/* Tag functions */

#define CMPTAG( a, b)       (( (a)->t_type == (b)->t_type) && ( (a)->t_data == (b)->t_data))
#define UNDEF_TAG( a)       { (a)->t_type = 0xFFFF; (a)->t_data = 0xFFFF; }
#define PTAG( a)            (a).t_type, (a).t_data

/* define the signal for accessing the RTDB */

#define FLC_SIG_RTDB_ACCESS 16

#define FLS_NORMAL		0
#define FLS_SIGNAL		1

/*
 * define the start of FactoryLink errors
 */
#define  FL_BASE_ERROR   400

#define  NOCHANGE        0
#define  CHANGED         1

#define  MAX_TMSG        12

/*
 * define the running modes of a FactoryLink task
 */
#define LIC_DEMO          1       /* run in demo license */
#define LIC_LITE          2       /* run in lite license */
#define LIC_FULL          4       /* run in full license */

#define PRT_OPTFILE       8       /* run with option file */
#define PRT_BUTTON        16       /* run with protection key */

/*
 * error codes of the protection mechanism
 */
#define DLE_FL_INIT       -1
#define DLE_FL_ENV        -2
#define DLE_DEMO_INIT     -3

/*
 * define time constants
 */
#define	SECONDS_PER_DAY      (24L*60L*60L)
#define	SECONDS_PER_YEAR     (SECONDS_PER_DAY * 365L)
#define	SECONDS_PER_LEAP     (SECONDS_PER_DAY * 366L)
#define SECONDS_DIFF_SECTIME ((8*SECONDS_PER_YEAR) + (2*SECONDS_PER_LEAP))

/*
 * definition of task specific parameters for the 'fl_status' function 
 */

typedef struct _task_id {

  id_t   id;                       /* FactoryLink identification number of task */
  int    dl_id;                    /* DeltaLink identification number of task */
	int		 station_id;						   /* Unique station id */

	char	 dl_serial[ 10];					 /* serial number returned from the protection */
	char	 dl_version[3];						 /* version number returned from the protection */

  ushort mode;                     /* running mode of the task */
  ushort version;                  /* version of the task */

	ushort e_driven;                 /* task event driven ? */
	int		 signal;                   /* store signal in case event driven */

	KENV   k;                        /* the FactoryLink Kernel environment */

  char   fl_name[ MAX_PROC_NAME];  /* FactoryLink application name */
  char   fl_domain[ 8];            /* FactoryLink application domain */
  char   fl_user[ MAX_USR_NAME];   /* FactoryLink application user name */

  char   name[ MAX_PROC_NAME];     /* task name */
  char   desc[ MAX_PROC_DESC];     /* task description */

  char   log_fname[ MAX_FILE_NAME]; /* name of the logfile */
  FILE   *log_fhandle;

  char   dbg_level;                /* level of debug information */
  char   log_level;                /* level of log information */
  char   exc_level;                /* level is exclusive for debug and logging */

  u32    max_total_tags;           /* maximum number of tags */
  u32    max_io_tags;              /* maximum number of I/O tags */

} TASK_ID;


/* Union used to store TAG Values */

typedef union _TVAL {

  DIG     dig;                          /* digital */
  ANA     ana;                          /* analog */
  LANA    lana;                         /* long analog */
  FLP     flp;                          /* floating point */
  char    msg[ MAX_TMSG];               /* message */

} TVAL;


/*
 * user event function structure 
 */
typedef struct _event_fnc {

  int               ( *u_fnc)( void *, VAL); /* pointer to user function */

  void              *u_data;                 /* pointer to user data */

  struct _event_fnc *next,                   /* pointer to next event function */
                    *prev;                   /* pointer to previous event function */

} EVENT_FNC;

/*
 * global variables used in the FactoryLink utility library
 */
extern EVENT_FNC **e_fnclist;     						/* list containing user function pointers */
extern TAG       *e_taglist;       						/* tag list which creates events */
extern u16			 _nr_events;									/* current number of events */

extern int       ( *null_fnc)( void);					/* pointer to user NULL function */


/*
 * prototypes of all FL utility functions
 */
uint    calc_dimension    							( char *, char *);

void    fl_status         							( TASK_ID *, char *, ANA, ...);
#define fl_error( id)     							( fl_errno( id) + FL_BASE_ERROR )
void    fl_shutdown       							( TASK_ID *, int);

void    fl_task_xlate_load( TASK_ID *);

i16     fl_gettype        							( TAG *);
int     fl_chread         							( TASK_ID *, TAG *, uint, uint *, void *);
int 		fl_dig_set											( TASK_ID *, TAG *);
int 		fl_set_dig											( TASK_ID *, TAG *);
int     fl_read_tval      							( TASK_ID *, TAG *, TVAL *);
int     fl_write_tval     							( TASK_ID *, TAG *, TVAL *);
int     fl_get_mbxmsg     							( TASK_ID *, TAG *, MBXMSG *, uint *);
int 		fl_cmptag												( TAG *, TAG *);
void 		fl_convert											( void *, i16, void *, i16);

int 		fl_add_events 									( TASK_ID *, TAG *, void **, u16, int ( *)( void *, VAL));
int 		fl_register_null_event					( TASK_ID *, int ( *)( void));
int 		fl_clr_events 									( TASK_ID *, TAG *, void **, u16);
int     fl_clr_eventlist  							( TASK_ID *);
int 		fl_wait_event										( TASK_ID *);
int     fl_exec_event                   ( TASK_ID *, TAG *);

int     fl_change_bits_read             ( id_t, TAG FAR *, uint, u16 FAR *);

int     debug_log_init    							( TASK_ID *);
int     debug_log         							( TASK_ID *, char *, int, ...);
void    debug_log_exit    							( TASK_ID *);

void    mirror            							( char *buf, ushort length);
FLP   	sie2ieee      									( u32);
u32   	ieee2sie      									( FLP);

/*
 * prototypes of all FL utility support functions in module fl_func.c
 */
int 				 find_event 				( TAG *, u16 *);

EVENT_FNC 	*ll_goto_begin			( EVENT_FNC *);
EVENT_FNC 	*ll_goto_end				( EVENT_FNC *);
EVENT_FNC 	*ll_insert					( EVENT_FNC *);
EVENT_FNC 	*ll_append					( EVENT_FNC *);
EVENT_FNC 	*ll_delete					( EVENT_FNC *, EVENT_FNC **);
int 				 ll_count						( EVENT_FNC *);

/*
 * prototypes of all DL utility functions
 */
int		dl_init_task          ( TASK_ID *);
int 	dl_task_banner				( TASK_ID *);
int		dl_test_term_flag     ( TASK_ID *);
int		dl_exit_task          ( TASK_ID *, int);
void	dl_sleep							( ulong);
short dl_get_option_bit     ( int);

void 	dl_time2kdt						( KDT *, u32);
u32 	dl_kdt2time						( KDT *);
char *dl_ctime							( KDT *);

int   fl_init_critical      ( TASK_ID *, TASK_ID ** );
int   fl_exit_critical      ( void);
int   fl_signal_critical    ( void);
int   fl_enter_critical     ( int);
int   fl_leave_critical     ( int);

#endif /* _FLUTILS_INCLUDED */
