/*
 ******************************************************************************
 *  Copyright 1993 DeltaLink bv. All Rights Reserved.
 ******************************************************************************
 *
 * DeltaLink Decoder Header file
 *
 * File: decoder.h
 *
 * Contains definitions for the DeltaLink  Decoder Task
 */
#ifndef _DEC_DEFS_INCLUDED /* { */
#define _DEC_DEFS_INCLUDED 1

#ifndef HPUX
#pragma pack(1)
#else
#pragma HP_ALIGN HPUX_WORD PUSH
#endif

/* define the states of a task */

#define TASK_DEAD   0
#define TASK_ALIVE  1

#define swapb( s)  ((( (*( u16 *)s) >> 8) & 0x00FF) | (( (*( u16 *)s) << 8) & 0xFF00))
#define swapw( s)  ((( (*s) >> 16) & 0x0000FFFF) | (( (*s) << 16) & 0xFFFF0000))

/* length of the Decoder Mailbox receive buffer */

#define RX_BUF_SIZE      50000
#define TX_BUF_SIZE      4120   /* Size determined by Siemens Sinec_H1, Max DB size + 16 */

/* define the masks for every functionality in element fncs */

#define RD_MASK   1                   /* READ function */     
#define BW_MASK   2                   /* BLOCK write function */
#define EW_MASK   4                   /* NORMAL exception write function */
#define CW_MASK   8                   /* ENCODED exception write function */
#define UR_MASK   16                  /* RECEIVE function */

#define NO_FNC       0
#define NORMAL_FNC   1
#define EXC_FNC      2

#define TRIGGERED 32

#define RD_Q_MASK 64                  /* activate read after query response */
#define WR_Q_MASK 128                  /* activate write after query response */

#define ERD_MASK  256                  /* Exception Read function */
#define EUR_MASK  512                  /* Exception Unsolicited Receive function */


/* define the different conversions */

#define C_BIT             1           /* BIT conversion */
#define C_BYTE_R          2           /* RIGHT byte conversion */
#define C_BYTE_L          3           /* LEFT byte conversion */
#define C_WORD            4           /* WORD conversion */
#define C_LONG            5           /* LONG conversion */
#define C_IEEE_DOUBLE     6           /* DOUBLE precision float conversion */
#define C_MSG             7           /* message conversion */
#define C_IEEE_FLT        8           /* IEEE single presicion float conversion */
#define C_BCD             10          /* Binary Coded Decimal conversion */
#define C_BYTE            11          /* BYTE conversion */

#define C_SIE_FLT         9           /* SIEMENS float conversion */

#define C_IBG             12          /* Interbus-S Gain for analog in/out */
#define C_IBAU            13          /* Interbus-S Analog Input/Output 12 bits Unsigned */
#define C_IBAS            14          /* Interbus-S Analog Input/Output 12 bits two's complement */

#define C_R_LONG          15          /* reversed long conversion */
#define C_R_FLT           16          /* reversed float conversion */

#define C_VALMET_FLT      17          /* Valmet float conversion */

#define C_BCD2            18          /* Byte Binary Coded Decimal conversion */
#define C_BCD4            19          /* Word Binary Coded Decimal conversion */
#define C_BCD8            20          /* Long Binary Coded Decimal conversion */

#define C_TIME            21          /* Time conversion: C-time <--> SECTIME */
#define C_TIM0            22          /* Time conversion: user defined */
#define C_TIM1            23          /* Time conversion: user defined */
#define C_TIM2            24          /* Time conversion: user defined */
#define C_TIM3            25          /* Time conversion: user defined */
#define C_TIM4            26          /* Time conversion: user defined */
#define C_TIM5            27          /* Time conversion: user defined */
#define C_TIM6            28          /* Time conversion: user defined */
#define C_TIM7            29          /* Time conversion: user defined */
#define C_TIM8            30          /* Time conversion: user defined */
#define C_TIM9            31          /* Time conversion: user defined */

/* absolute or relative addressing */

#define ABSOLUTE          1    
#define RELATIVE          0

/* define functions */
#define RD_FUNCTION       0           /* read functionality */
#define WR_FUNCTION       1           /* write functionality */
#define RCV_FUNCTION      2           /* receive functionality */

/*
 * date/time defines
 */
#define MAX_DATETIME     40           /* max string length for datetime */

/* information of a Tag in the DataSet overlay */

typedef struct {

  MCIDS   *ds;                        /* pointer to the dataset of this tag */

  u16     t_type;                     /* type of this tag */

  ushort  address;                    /* address in the received data */
  ushort  bit;                        /* bit number in data */
  ushort  conv;                       /* conversion to perform on data */

  ushort  operation;                  /* operation for encoded write */

  char    *m_ptr;                     /* pointer to buffer if type is message */
  u16     m_len;                      /* length of message buffer */

  ushort  base;                       /* base address for tagarray, otherwise == address */
  char    fix;                        /* set this flag if address has to be fixed */    

} INFO;

/* 
 * additional dataset information whci will be linked to the MCI dataset
 */
typedef struct {

  ushort  nr_msg;                         /* max number of messages in send mailbox */

  ushort  ct_start;                       /* first address of an element */
  ushort  ct_len;                         /* total range of addresses specified in CT */

  uint    nr_tags;                        /* number of overlay tags */
  TAG     *tags;                          /* overlay Tags of DataSet */
  void    *val;                           /* buffers to hold the values */
  void    *old_val;                       /* buffers to hold the old values */
  i16     *t_type;                        /* type of FactoryLink element */
  u16     *offset;                        /* size of FactoryLink data type in bytes */
  INFO    *info;                          /* array of info for overlay Tags */

  ushort abs;                             /* absolute or relative addressing */
  ushort fnc;                             /* functions of DataSet */
  ushort start_check;                     /* start flag for checking */
  char   continuous;                      /* continous read */

  TAG    status;                          /* status tag of this dataset */

  TAG    ready[ 3];                       /* completion triggers */
  TAG    sync[ 3];                        /* sync tags */

  int    read_disable;                    /* boolean for disabling read functionality */
  int    write_disable;                   /* boolean for disabling write functionality */
  int    rcv_disable;                     /* boolean for disabling unsolicited receive */

  int    read_sync;                       /* sync. boolean for read function */
  int    write_sync;                      /* sync. boolean for write function */
  int    rcv_sync;                        /* sync. boolean for recv function */

} DS_INFO;

/* 
 * Time strucuture for internal use
 */
typedef struct {

  short size;                             /* size of the expected strucuture */
  short bcd;                              /* mark for BCD conversion */
                       
  short hr;                               /* index */
  short mi;                               /* index */
  short sc;                               /* index */

  short yp;                               /* index */
  short yr;                               /* index */
  short year;                             /* index */
  short mn;                               /* index */
  short dy;                               /* index */
  short dow;                              /* index */

  ushort  dow_offset;
  ushort  dy_offset;
  ushort  mn_offset;

} DEC_TIME;

/* 
 * Time Format Structure 
 */
typedef struct _dec_tmfmt
{
  int  year;                              /* Position four digit year */
  int  yr;                                /* Position two digit year */
  int  mon;                               /* Position alpha month */
  int  mo;                                /* Position two digit month */
  int  dy;                                /* Position two digit day of month */
  int  dow;                               /* Position three digit day of week */
  int  hr;                                /* Position military hour 0..23 */
  int  ah;                                /* Position hour 1..12 */
  int  mi;                                /* Position two digit minute */
  int  sc;                                /* Position tow digit second */
  int  ap;                                /* Position am/pm indicator */
  char fmt[ MAX_DATETIME];                /* String to put it in */
  char amon[ 12][4];                      /* List of alpha months */
  char adow[ 7][4];                       /* List of alpha days */
  char aap[ 2][3];                        /* List of alpha am/pm */

} DEC_TMFMT;


/*
 * global variables used througout the decoder task
 */
extern  TASK_ID       Task;                       /* Id for this task */
extern  MSG           Rx_buf;                     /* buffer for receiving with MCI */

extern  MCISYSTEM 	  Mcisys;							/* system parameters of the mci library */

extern  TAG           *Tags;
extern  u16           *Chbits;


extern  int           fl_sizes[ 5];

extern  DEC_TIME      dec_time[ 10];

void  ctload         ( void);
int   mci_event      ( MCIDS *);
int   block_read     ( void *, VAL);
int   block_write    ( void *, VAL);
int   ex_write       ( void *, VAL);
int   read_disable   ( void *, VAL);
int   write_disable  ( void *, VAL);
int   rcv_disable    ( void *, VAL);
int   read_sync      ( void *, VAL);
int   write_sync     ( void *, VAL);
int   rcv_sync       ( void *, VAL);
VAL   *tag_type_xlate( void *, u16, VAL *, u16);

void  ds_completion  ( MCIDS *, int, u16);

void  tmfmt_load     ( DEC_TMFMT *, char *);
char  *fmt_time      ( char *, DEC_TMFMT *, LANA);
LANA  get_fmt_time   ( char *, DEC_TMFMT *);

#ifndef HPUX
#pragma pack()
#else
#pragma HP_ALIGN POP
#endif

#endif /* _DEC_DEFS_INCLUDED */
