/************************************************************************/
/* Copyright 1993 - 1997  MBARI                                         */
/************************************************************************/
/* $Header: recorder.h,v 1.0 93/09/30 09:51:28 hebo Exp $               */
/* Summary  : Type Definitions for Flight Recorder Function of New ROV  */
/* Filename : recorder.h                                                */
/* Author   : Bob Herlien (rah)                                         */
/* Project  : Tiburon                                                   */
/* $Revision: 1.0 $                                                     */
/* Created  : 05/07/93                                                  */
/************************************************************************/
/* Modification History:                                                */
/* $Log:        recorder.h,v $
 * Revision 1.0  93/09/30  09:51:28  09:51:28  hebo (Bob Herlien)
 * Initial revision
 *
*/
/* 07may93 rah, created                                                 */
/************************************************************************/
/* Note - To avoid compiler errors, you need to include the following   */
/*        files AHEAD of this include file:                             */
/*        #include <mbari/types.h>                                      */
/*        #include <semLib.h> (VxWorks)                                 */
/*        #include <time.h> (Unix) or <systime.h> (VxWorks)             */
/*        #include <datamgr.h> (Data Manager)                           */
/*        #include <lstLib.h> (VxWorks)                                 */
/************************************************************************/

#ifndef INCrecorderh
#define INCrecorderh    1

#define RECORD_PRIO     99
#define RECORD_STKSIZE  8000
#define RECORD_CFGFILE  "/usr/tiburon/logs/record.cfg"
#define RECORD_DFLT_FILE "/usr/tiburon/logs/record"
#define RCD_FILENAME_SIZE  256

#define RECBUFLEN       1024            /* Size of string buffer        */
#define DFLT_FNUM_MAX   32              /* Default max number of files  */
#define DFLT_FLUSHTIME  (15 * sysClkRateGet())
                                        /* Default time for file flush*/
#define DFLT_WRAPTIME   (60 * sysClkRateGet())
                                        /* Default time to ping-pong    */

#ifndef MIN
#define MIN(a,b)        (((a)<(b))?(a):(b))
#endif

#define DM_NAME(item)    (((DM_Entry *)item)->dme_name->dmn_name)
#define DM_TYPE(item)    (((DM_Entry *)item)->dme_type)
#define DM_ELEMENT(item) (((DM_Entry *)item)->dme_compound)
#define DM_NTYPES(item)  (((DM_Entry *)item)->dme_ncompounds)

typedef struct                                  /****************************/
{                                               /* Struct for recorder group*/
    NODE        grp_link;                       /* Linked list entry        */
    DM_Group    grp_num;                        /* DM_Group number          */
    DWord       grp_changes;                    /* Accumulated changes      */
} RecGroup;                                     /****************************/

#define RGtoNODE(p)     ((NODE *)p)
#define NODEtoRG(p)     ((RecGroup *)p)
#define NODE_NULL       ((NODE *)0)
#define RG_NULL         ((RecGroup *)NULL)

typedef struct                                  /****************************/
{                                               /* Struct for one rec item  */
    NODE        rec_link;                       /* Linked list entry        */
    DM_Item     rec_item;                       /* DM Item ID               */
    char        *rec_data;                      /* Item data                */
    DM_Time     rec_time;                       /* Time of DM Item data     */
    DmItemSts   rec_sts;                        /* Item status struct       */
    Nat32       rec_period;                     /* Ticks per sample         */
    Nat32       rec_tick;                       /* VxWorks tick for next read*/
    Nat32       rec_arrayIndex;                 /* Item index for array     */
    RecGroup    *rec_groupPtr;                  /* Ptr to group it's in     */
    DWord       rec_groupBit;                   /* Group bit for this item  */
    DWord       rec_flags;                      /* Flags                    */
    MBool       rec_isArray;                    /* TRUE if part of array    */
} RecItem;                                      /****************************/

#define RItoNODE(p)     ((NODE *)p)
#define NODEtoRI(p)     ((RecItem *)p)
#define RI_NULL         ((RecItem *)NULL)

/* Flags        */
#define REC_DATA        1                       /* Record item data     */
#define REC_NAME        2                       /* Record item name     */
#define REC_DATE        4                       /* Record date          */
#define REC_TIME        8                       /* Record time          */
#define REC_TIME_GMT    0x10                    /* Display time/date in GMT*/
#define REC_ALWAYS      0x20                    /* Log even if no change*/
#define DFLT_FLAGS      (REC_DATA | REC_NAME | REC_TIME)

typedef struct                                  /****************************/
{                                               /* Info for one recorder task*/
    NODE        recLink;                        /* For linked list          */
    Pid         recPid;                         /* Pid of recording task    */
    LIST        recItemList;                    /* List of items to record  */
    LIST        recGroupList;                   /* List of DM Groups        */
    SEM_ID      recSem;                         /* Semaphore for consumers  */
    FILE        *fp;                            /* Ptr to FILE to log to    */
    char        *fileName;                      /* Log File name            */
    char        *cfgFileName;                   /* Cfg File name            */
    Nat32       fileNum;                        /* nn for filename.nn[ab]   */
    MBool       filePingPong;                   /* Indicates 'a' or 'b'     */
    MBool       recStop;                        /* TRUE to stop record task */
    Nat32       flushtime;                      /* File flush time          */
    Nat32       wraptime;                       /* File wrap time           */
    Int32       maxFilePairs;                   /* Max number of file pairs */
    char        recbuff[RECBUFLEN];             /* Buffer                   */
} RecStruct;                                    /****************************/

#define RStoNODE(p)     ((NODE *)p)
#define NODEtoRS(p)     ((RecStruct *)p)
#define RS_NULL         ((RecStruct *)NULL)


#ifdef __cplusplus
extern "C" {
#endif

#if (defined(__STDC__) || defined(__cplusplus))

Int32   recordInit( Void );
Pid     recordStart( char *cfgfile, Nat32 delay );
Void    recordStop( Int32 pid );
Void    recordShow( Int32 all );

#endif  /* __STDC__ */

#ifdef __cplusplus
}
#endif

#endif  /* INCrecorderh */
