/*************************************************************************\
* Copyright (c) 2002 The University of Chicago, as Operator of Argonne
* National Laboratory.
* Copyright (c) 2002 The Regents of the University of California, as
* Operator of Los Alamos National Laboratory.
* This file is distributed subject to a Software License Agreement found
* in the file LICENSE that is included with this distribution. 
\*************************************************************************/

/*
 * $Log: not supported by cvs2svn $
 * Revision 1.1  1999/06/10 19:47:17  soliday
 * Moved to oag tree.
 *
 * Revision 1.2  1999/06/08 22:14:22  soliday
 * Removed compiler warnings under Linux.
 *
 * Revision 1.1  1999/04/07 16:10:55  soliday
 * Initial version of the snapshot shared library
 *
 * */

#include <stdio.h>
#include <string.h>
#include <tcl.h>
#include <tk.h>
#include <SDDS.h>
#include <cadef.h>

#define MAX_OPEN_SDDS_FILES 20
#define SDDS_ERROR_MODE (SDDS_VERBOSE_PrintErrors|SDDS_LAST_GetErrorMessages)

#define MAGIC_POLL .000000001

typedef struct DisDat {
  double x;
  double y;
} DISDAT;

typedef union {
  struct dbr_time_string s;
  struct dbr_time_enum   e;
  struct dbr_time_char   c;
  struct dbr_time_short  i;
  struct dbr_time_long   l;
  struct dbr_time_float  f;
  struct dbr_time_double d;
} DATABUF;

typedef union {
  struct dbr_sts_string  s;
  struct dbr_ctrl_enum   e;
  struct dbr_ctrl_char   c;
  struct dbr_ctrl_short  i;
  struct dbr_ctrl_long   l;
  struct dbr_ctrl_float  f;
  struct dbr_ctrl_double d;
} INFOBUF;

typedef struct ViewPort {
  int    base;
  int    size;
  int    precision;
} VIEWPORT;

typedef enum {NewConnection,NeverConnected, LostConnection,
	      ReadAccess,WriteAccess, PendIO, ConnectionOK } 
caState;

typedef union {
  long tclLong;
  double tclDouble;
  char *tclStr;
} TCLDATABUF;

typedef struct MonScript {
  Tcl_DString script;
  int  number;
} MONSCRIPT;

typedef struct Item {
  struct Chandata *pchandata;
  char *tclname;
} ITEM;

typedef struct Chandata{
  chid 		chid;
  evid 		evid;
  DATABUF 	*pdata;
  INFOBUF  	info;
  VIEWPORT	view;
  caState  	castate;
  int	 	countPendIO;
  int     	initSize; 
  int     	dataSize;
  int	 	iType;       /* native ca type of pv               */
  int           tclType;     /* closest tcl type to native ca type */
  int	 	precision; 
  TCLDATABUF    tclValue;
  ITEM		*pitem;
  MONSCRIPT	*pscript;
  int           secondMonEvent;
  int		updateVar;
  Tcl_Interp	*interp;
  int           skip;
} CHANDATA;

typedef struct {
  SDDS_TABLE table;
  char fileName[255]; /* path of opened file */
  char mode;          /* r or w */
  int pageStarted;    /* has user done a startPage */
  int free;           /* this sdds fd is free */
  int currentPage;
} SDDS_FD;

DISDAT vdisdat[4096];

typedef struct {
  ELLNODE nodePad;
  int index;
  ITEM *pitem;
} ITEMNODE;

/***********************/
/* Function Prototypes */
/***********************/
int tclSnapshotInit(Tcl_Interp *interp);
int tclSnapshotOpen(Tcl_Interp *interp, char *fileName, char mode);
int tclSnapshotClose(Tcl_Interp *interp, int fd);
int tclSnapshotWrite(Tcl_Interp *interp, int argc, char *argv[]);
int tclSnapshotReadRequest(Tcl_Interp *interp, int argc, char *argv[]);
int tclSnapshotReadSnapshot(Tcl_Interp *interp, int argc, char *argv[]);
int tclSnapshotLinkPVs(Tcl_Interp *interp, int argc, char *argv[]);
int tclSnapshotGetPVs(Tcl_Interp *interp, int argc, char *argv[]);
int tclSnapshotPutPVs(Tcl_Interp *interp, int argc, char *argv[]);

ITEM *pv_search(char *s);
int caTypeToTclType(int caNativeType);
int howManyBytes(CHANDATA *pchandata);
void updateLink(CHANDATA *pchandata);
ITEM *pv_exists(char *s);
void copyCaTypeToTclType(CHANDATA *pch);
void *copyTclTypeToCaType(CHANDATA *pch);
