/*
 * FILE:        FLDEFS.H
 *
 * MODULE:      FactoryLink Definitions Header File
 *
 * PROGRAM:     FactoryLink Multi-Platform version 4.1
 *
 * AUTHOR:      Clay Waldrop, Jr.
 *
 * HISTORY:       DATE   AUTHOR  DESCRIPTION OF ACTION
 *              08/06/90   CW    Finished Multi-Platform version 1.0
 *                               for OS/2, UNIX, and VMS platforms.
 *              09/05/90   CW    Finished Multi-Platform version 2.0
 *                               for OS/2, UNIX, and VMS platforms.
 *		06/03/91   DM    Pulled out from old FLIB.H so we can
 *				 share with KER.H.
 *		08/14/92   CAC	 Wrapped #ifdef __cplusplus code around
 *				 function prototypes for linkage with C++
 *				 objects for CML++.
 *
 *		10/28/92   CLC   Added prototype for fl_count_io, a routine called 
 *                     under FactoryLink Lite to limit I/O points.
 *		05-jan-1993 clc  Removed fl_count_io
 */
#ifndef _FL_DEFS_INCLUDED /* { */
#define	_FL_DEFS_INCLUDED 1

#ifndef FALSE
#define FALSE                   0
#endif
#ifndef TRUE
#define TRUE                    1
#endif
#define OFF                     0
#define ON                      1
#define GOOD                    0
#define ERROR                   (-1)
#define NONE                    0
#define NUL                     '\0'
#ifndef NULL
#define NULL                    0
#endif

#define forever                 while (TRUE)
#define dimof(a)                (sizeof(a)/sizeof((a)[0]))
#define maxof(a,b)              ((a) > (b) ? (a) : (b))
#define minof(a,b)              ((a) < (b) ? (a) : (b))

#define KBYTE                   1024    /* number of bytes in a K-byte */
#define MAX_STATUS              256

#define MAX_MSG			80	/* max nominal size of a message */
#define MAX_PROCS               31      /* max number of concurrent */
                                        /* FactoryLink client processes */

#define MAX_PROC_NAME           32      /* max size of a client process */
                                        /* name (including '\0') */
#define MAX_PROC_DESC           80      /* max size of a client process */
                                        /* description (including '\0') */
#define MAX_FILE_NAME           128     /* max size of a file name */
                                        /* (could be a full path name) */
#define MAX_LINE                80      /* max size of a line */
                                        /* (to print on output device) */
#define MAX_DIR_NAME            64      /* max size of a directory name */
                                        /* (could be a full path name) */
#define MAX_CMD_LINE            128     /* max size of a command line */
                                        /* (to read at start-up time) */
#define	MAX_USR_NAME		16	/* max length of Domain/User name */

#define	MAX_TYPE_NAME		8	/* max length of TAG type */

#define	MAX_TAG_NAME		32	/* maximum length of a TAG name */
					/* without dimensions */
#define	MAX_DIM_LENGTH		16	/* max length of TAG dimensions */

#define	MAX_TFLAGS_LENGTH	 4	/* max length of TAG flags */

#define	ATTR_NAME_LENGTH	32	/* max size of an attribute name */
#define	MAX_ATTR_BIT (sizeof(ATTR)*8)	/* usually 32 */

/* Constants for attribute operations */

#define	DONTREAD		1
#define	DONTWRITE		1
#define	ATTR_SET		0
#define	ATTR_CLR		1
#define	ATTR_TGL		2

/* FactoryLink data types (descriptions returned by FL_GET_TAG_INFO) */

#define FL_UNDEFINED            (-3)    /* tag is undefined, which occurs */
                                        /* when t_type or t_data is 0xFFFF */
#define FL_BAD_DATA             (-2)    /* bad t_data field */
#define FL_BAD_TYPE             (-1)    /* bad t_type field */
#define FL_DIGITAL              0       /* digital (DIG externally) */
#define FL_ANALOG               1       /* analog (ANA) */
#define FL_FLOAT                2       /* floating point (FLP) */
#define FL_MESSAGE              3       /* message (MSG) */
#define FL_LANALOG              4       /* long analog (LANA) */
#define FL_MAILBOX              5       /* mailbox (holds MBXMSG's) */

/* FactoryLink process statuses (reported to RUNMGR) */

#define FLS_INACTIVE            0       /* inactive/not running */
#define FLS_ACTIVE              1       /* active/running */
#define FLS_ERROR               2       /* non-fatal error encountered */
#define FLS_STARTING            3       /* starting/initializing */
#define FLS_STOPPING            4       /* stopping/exiting */

/* FactoryLink wait commands (used by FL_WAIT and FL_WAKEUP) */

#define FLC_WAIT_READ           1       /* wait to read database */
#define FLC_WAIT_WRITE          2       /* wait to write database */
#define FLC_WAIT_ACCESS         3       /* wait to read or write */

/* FactoryLink control signals (used by FL_*_SIG and other services) */

#define FLC_SIG_TERMINATED       0      /* process has been terminated */
#define FLC_SIG_TERM_FLAG_SET    1      /* termination flag is set */
#define FLC_SIG_TAG_LIST_CHANGED 2      /* tag list has been changed */
#define FLC_SIG_MESSAGE_RECEIVED 3      /* message has been received */
#define FLC_SIG_NEWCONFIG        15     /* signal to task to prepare for new config */
#define FLC_SIG_HIBER_BEGIN      16     /* signal to RUNMGR to hibernate */
#define FLC_SIG_HIBER_END        17     /* signal to RUNMGR to end hibernate */

/* FactoryLink error numbers (returned by Kernel services) */

#define FLE_INTERNAL            1
#define FLE_OUT_OF_MEMORY       2
#define FLE_OPERATING_SYSTEM    3
#define FLE_NO_FLINK_INIT       4
#define FLE_NO_PROC_INIT        5
#define FLE_BAD_FUNCTION        6
#define FLE_BAD_ARGUMENT        7
#define FLE_BAD_DATA            8
#define FLE_BAD_TAG             9
#define FLE_NULL_POINTER        10
#define FLE_NO_CHANGE           11
#define FLE_PROC_TABLE_FULL     12
#define FLE_BAD_PROC_NAME       13
#define FLE_BAD_USER_NAME       14
#define FLE_BAD_OPTION          15
#define FLE_BAD_CHECKSUM        16
#define FLE_NO_OPTIONS          17
#define FLE_NO_KEY              18
#define FLE_BAD_KEY             19
#define FLE_NO_PORT             20
#define FLE_PORT_BUSY           21
#define FLE_ALREADY_ACTIVE      22
#define FLE_NOT_LOCKED          23
#define FLE_LOCK_FAILED         24
#define FLE_LOCK_EXPIRED        25
#define FLE_WAIT_FAILED         26
#define FLE_TERM_FLAG_SET       27
#define FLE_QSIZE_TOOBIG        28
#define FLE_QSIZE_CHANGED       29
#define FLE_NO_TAG_LIST         30
#define FLE_TAG_LIST_CHANGED    31
#define FLE_WAKEUP_FAILED       32
#define FLE_NO_SIGNALS          33
#define FLE_SIGNALLED           34
#define FLE_NOT_MAILBOX         35
#define FLE_NO_MESSAGES         36
#define FLE_ACCESS_DENIED       37
#define	FLE_ATTR_FULL		38
#define	FLE_INVALID_ATTR	39
#define	FLE_ATTR_NOT_DEF	40
#define	FLE_APP_EXISTS		41
#define	FLE_NO_FLINK_RTDB	42
#define FLE_NO_TASK_BIT		43
#define FLE_NOT_LITE_TASK	44
#define FLE_SEM_OP		45
#define FLE_MBXLIM_EXCEED	46
#define FLE_NOCOMPAT_TYPES	47
#define FLE_NOCHANGE_TYPES	48

#define FLE_FLOAT_NAN           50      /* FLP is not-a-number */
#define FLE_FLOAT_POS_INF       51      /* FLP is positive infinity */
#define FLE_FLOAT_NEG_INF       52      /* FLP is negative infinity */

#define FLE_MV_EQSRCDST         53      /* Source and dest tags equal */
#define FLE_MV_DOMAIN           54      /* Illegal move from src/dst domain */
#define FLE_MV_MBX              55      /* Unable to move mailbox tags */

/*  Tag flag constants */

#define FLOBJ_FLAG_MEMBER  'M'   /* Flag definitions */
#define FLOBJ_FLAG_STRUCT  'S'

#define tg_hasflag(flags,flag)  (strchr(flags,flag)!=NULL)

/* Direction argument defines */
#define FL_FUNC_GET        0
#define FL_FUNC_SET        1

/* --- Typedefs, Structures & Unions --- */

typedef i16     id_t;                   /* FactoryLink ID */
typedef u32     mask_t;                 /* FactoryLink mask */
typedef	u32	ATTR;			/* data attributes bit array */

typedef	struct	
{
	char	name[ATTR_NAME_LENGTH]; /* data attributes name */
	int	len;			/* length of name */
} ATTRNM;

typedef struct _TAG                     /* specifies a database element */
{
        u16     t_type;                 /* index into array of KTYPE's */
        u16     t_data;                 /* index into array of KDATA's */
} TAG;

typedef struct _MSG                     /* message type (variable length) */
{
        char FAR *m_ptr;                /* ptr to beginning of data */
        u16     m_max;                  /* maximum allocated size of data */
        u16     m_len;                  /* actual length of data */
} FLMSG;

#ifndef MSMSG
typedef FLMSG  MSG;
#endif

typedef struct _MBXMSG                  /* client mailbox message */
{
        FLMSG   mm_msg;                 /* actual message to be sent */
                                        /* (refers to message data) */
        TAG     mm_mbx;                 /* source mailbox (usually owned */
                                        /* by sender of this message) */
        u16     mm_type;                /* message type (for translation */
                                        /* to and from canonical form) */
        u16     mm_user1;               /* reserved for sender & receiver */
                                        /* (usually message subtype) */
        u32     mm_user2;               /* reserved for sender & receiver */
                                        /* (usually sequence number) */
	id_t	mm_sendid;		/* task id of sender */
} MBXMSG;

typedef union _VAL                      /* union of above values */
{
        DIG     dig;                    /* digital */
        ANA     ana;                    /* analog */
        LANA    lana;                   /* long analog */
        FLP     flp;                    /* floating point */
        FLMSG   msg;                    /* message */
        MBXMSG  mbxmsg;                 /* mailbox message */
} VAL;

typedef struct _VPTR                    /* virtual memory pointer (v-ptr) */
{
        u16     v_mem;                  /* index into array of KMEM's */
        u16     v_map;                  /* index into array of KMEMMAP's */
} VPTR;


/* Client environment structure - return structure for fl_get_env */

typedef struct _KENV                    /* client environment */
{
	TAG     e_ctrl;                 /* control: start/stop (DIG or ANA) */
	TAG     e_stat;                 /* self-reported status (ANA) */
	TAG     e_msg;                  /* self-reported message (MSG) */
	char    e_adir[MAX_DIR_NAME];   /* application drive & directory */
	char    e_pdir[MAX_DIR_NAME];   /* program drive & directory */
	char    e_cmd[MAX_CMD_LINE];    /* command line */
} KENV;

/* Date & Time report structure */

typedef struct _KDT                     /* OS-independent */
{
	u16     hours;                  /* hours (0-23) */
	u16     minutes;                /* minutes (0-59) */
	u16     seconds;                /* seconds (0-59) */
	u16     hundredths;             /* hundredths of seconds (0-99) */
	u16     day;                    /* day of month (1-31) */
	u16     month;                  /* month (1-12) */
	u16     year;                   /* year (1980-2097) */
	u16     weekday;                /* day of week (0=Sun,...,6=Sat) */
	u16     timezone;               /* time zone (relative to GMT) */
} KDT;

/* --- Kernel Services Prototypes --- */

/* Process Management Services */

#ifdef __cplusplus
#ifdef ZORTECH
extern "Pascal" {	/* Type-safe linkage for CML++ */
#else
extern "C" {
#endif
#endif

id_t    FLENTRY fl_proc_init(char FAR *name, char FAR *desc);
id_t    FLENTRY fl_proc_init_app(char FAR *task, char FAR *desc,
                                 char FAR *name, char FAR *domain, 
                                 char FAR *user);
int     FLENTRY fl_set_term_flag(id_t id, char FAR *name);
int     FLENTRY fl_set_term_flag_ex(id_t id, char *dom, char* user, char *name);
int     FLENTRY fl_set_app_term_flag(id_t id, id_t wid);
int     FLENTRY fl_test_term_flag(id_t id);
int     FLENTRY fl_proc_exit(id_t id);
id_t	FLENTRY fl_name_to_id(id_t id, char FAR *name);
id_t    FLENTRY fl_name_to_id_ex(id_t id, char *dom, char* user, char *name);
int     FLENTRY fl_id_to_name(id_t id, char FAR *name);

/* Database Access Services */

int     FLENTRY fl_read(id_t id, TAG FAR *tp, uint n, void FAR *vp);
int     FLENTRY fl_write(id_t id, TAG FAR *tp, uint n, void FAR *vp);
int     FLENTRY fl_write_app(id_t id, id_t wid, TAG FAR *tp, uint n, void FAR *vp);
int     FLENTRY fl_forced_write(id_t id, TAG FAR *tp, uint n, void FAR *vp);
int     FLENTRY fl_change_wait(id_t id, TAG FAR *tp, uint n, uint FAR *ip,
                               void FAR *vp);
int     FLENTRY fl_change_read(id_t id, TAG FAR *tp, uint n, uint FAR *ip,
                               void FAR *vp);
int     FLENTRY fl_change_read_all(id_t id, TAG FAR *tp, uint n, u16 FAR *ch,
                               void FAR *vp);
int     FLENTRY fl_test_chng(id_t id, TAG FAR *tp, uint n, u16 *changed);
int     FLENTRY fl_test_app_chng(id_t id, id_t wid, TAG FAR *tp, uint n, u16 *changed);
int     FLENTRY fl_set_chng(id_t id, TAG FAR *tp, uint n);
int     FLENTRY fl_clear_chng(id_t id, TAG FAR *tp, uint n);
int     FLENTRY fl_set_sync(id_t id, TAG FAR *tp, uint n);
int     FLENTRY fl_clear_sync(id_t id, TAG FAR *tp, uint n);
int     FLENTRY fl_set_wait(id_t id, TAG FAR *tp, uint n);
int     FLENTRY fl_clear_wait(id_t id, TAG FAR *tp, uint n);
int     FLENTRY fl_write_no_overwrite(id_t id, TAG FAR *tp, uint n, 
                               void FAR *vp, char *no_overwrite);
int     FLENTRY fl_read_no_clear(id_t id, TAG FAR *tp, uint n, void FAR *vp);
int     FLENTRY fl_clear_all_chng(id_t id, TAG FAR *tp, uint n);

/* Database Access with attributes */

int	FLENTRY fl_read_attr(id_t id, TAG FAR *tp, uint n,
				void FAR *vp, ATTR FAR *attr);
int	FLENTRY fl_write_attr(id_t id, id_t wid, TAG FAR *tp, uint n,
				void FAR *vp, ATTR FAR *attr, ATTR FAR *amask);
int	FLENTRY fl_forced_write_attr(id_t id, TAG FAR *tp, uint n,
				void FAR *vp, ATTR FAR *attr, ATTR FAR *amask);
int     FLENTRY fl_change_wait_attr(id_t id, TAG FAR *tp, uint n, uint FAR *ip,
                               void FAR *vp, ATTR FAR *attr);
int     FLENTRY fl_change_read_attr(id_t id, TAG FAR *tp, uint n, uint FAR *ip,
                               void FAR *vp, ATTR FAR *attr);
int	FLENTRY fl_modify_attr(id_t id, TAG FAR *tp, uint n, ATTR FAR *ap, uint op);
int	FLENTRY fl_query_attr(id_t id, TAG FAR *tp, uint n, ATTR FAR *ap);
int	FLENTRY fl_define_attr(id_t id, uint n, ATTRNM *names, ATTR FAR *ap);
int	FLENTRY fl_get_attr_name(id_t id, uint n, ATTRNM *names, ATTR FAR *ap);
int	FLENTRY fl_get_attr_bit(id_t id, uint n, ATTRNM *names, ATTR FAR *ap);
int	FLENTRY fl_set_change_mask(id_t id, ATTR FAR *ap);

/* Database Access via Tag List Services */

int     FLENTRY fl_set_tag_list(id_t id, char FAR *name, TAG FAR *tp, uint n);
int     FLENTRY fl_get_tag_list(id_t id, TAG FAR *tp, uint n, uint FAR *np);
int     FLENTRY fl_change_wait_tag_list(id_t id, TAG FAR *tp, uint FAR *ip,
                                        void FAR *vp);
int     FLENTRY fl_change_read_tag_list(id_t id, TAG FAR *tp, uint FAR *ip,
                                        void FAR *vp);

/* Queueing Services */

int     FLENTRY fl_attach_queue(id_t id, TAG FAR *tp, uint n, u16 FAR *nelem);
int     FLENTRY fl_detach_queue(id_t id, TAG FAR *tp, uint n);

/* Mailbox Services */

int     FLENTRY fl_count_mbx(id_t id, TAG mbx, uint FAR *np);
int     FLENTRY fl_query_mbx(id_t id, TAG mbx, MBXMSG FAR *mmp, uint i,
                             uint n, uint FAR *np);
int     FLENTRY fl_read_mbx(id_t id, TAG mbx, MBXMSG FAR *mmp, uint i);
int     FLENTRY fl_read_app_mbx(id_t id, id_t rid, TAG mbx, MBXMSG FAR *mmp, uint i);
int     FLENTRY fl_write_mbx(id_t id, TAG mbx, MBXMSG FAR *mmp);
int     FLENTRY fl_write_app_mbx(id_t id, id_t wid, TAG mbx, MBXMSG FAR *mmp);
int     FLENTRY fl_set_owner_mbx(id_t id, TAG mbx, uint onoff);
int	FLENTRY fl_limit_mbx(id_t id, u16 dir, TAG *mbxp, uint nmbx, u16 FAR *klimp);
int	FLENTRY fl_limit_app_mbx(id_t id, id_t wid, u16 dir, TAG *mbxp, uint nmbx, u16 FAR *klimp);
int	FLENTRY fl_limit_mbx_default(u16 dir, u16 FAR *klimp);

/* Memory Management Services */

int     FLENTRY fl_alloc_mem(id_t id, u32 size, VPTR FAR *vptrp);
int     FLENTRY fl_access_mem(id_t id, VPTR vptr, void FAR * FAR *pptrp,
                              u32 FAR *sizep);
int     FLENTRY fl_free_mem(id_t id, VPTR vptr);

/* Signal Services */

int	FLENTRY fl_send_sig(id_t id, char FAR *name, int sig);
int	FLENTRY fl_send_sig_ex(id_t id, char *dom, char* user, char *name, int sig);
int	FLENTRY fl_send_app_sig(id_t id, id_t wid, FAST int sig);
int	FLENTRY fl_recv_sig(id_t id);
int	FLENTRY fl_hold_sig(id_t id, int sig, int hold);

/* Environment Access Services */

int	FLENTRY fl_get_ctrl_tag(id_t id, TAG FAR *tag);
int	FLENTRY fl_get_stat_tag(id_t id, TAG FAR *tag);
int	FLENTRY fl_get_msg_tag(id_t id, TAG FAR *tag);
int	FLENTRY fl_get_pgm_dir(id_t id, char FAR *dir);
int	FLENTRY fl_get_app_dir(id_t id, char FAR *dir);
int	FLENTRY fl_get_cmd_line(id_t id, char FAR *line);
int	FLENTRY fl_get_env(id_t id, KENV FAR *env);

/* Miscellaneous Services: Platform-Independent */

int	FLENTRY fl_lock(id_t id);
int	FLENTRY fl_lock_nowait(id_t id);
int	FLENTRY fl_unlock(id_t id);
int	FLENTRY fl_wait(id_t id, int req);
int	FLENTRY fl_wakeup(id_t id, mask_t FAR *mask, int req);
int	FLENTRY fl_wakeup_proc(id_t id, char FAR *name, int req);
int	FLENTRY fl_errno(id_t id);
uint	FLENTRY fl_get_version(void);
int	FLENTRY fl_get_tag_info(TAG FAR *tp, uint n, i16 FAR *dp, u16 FAR *op);
char*	FLENTRY fl_get_serial(void);

/* VAL conversion functions */
extern	int	FLENTRY fl_valint(VAL *v, int type);
extern	long	FLENTRY fl_vallong(VAL *v, int type);
extern	double	FLENTRY fl_valfloat(VAL *v, int type);
extern	char	* FLENTRY fl_valstring(VAL *v, int type);

#ifdef __cplusplus
}	/* Type-safe linkage for CML++ */
#endif

/* Miscellaneous Services: Platform-Dependent */

#ifdef __cplusplus
extern "C" {	/* Type-safe linkage for CML++ */
#endif

char FAR *fl_get_title(void);
char FAR *fl_get_copyrt(void);

#ifdef __cplusplus
}			/* end extern "C" */
#ifdef ZORTECH
extern "Pascal" {	/* Type-safe linkage for CML++ */
#else
extern "C" {
#endif
#endif

int	FLENTRY	fl_get_tick(u32 FAR *tickp, KDT FAR *datetimep);
char FAR * FLENTRY fl_getvar(char FAR *name, char FAR *buf, int len);

#ifdef __cplusplus
}	/* Type-safe linkage for CML++ */
#endif

#endif /* _FL_DEFS_INCLUDED } */
