/************************************************/
/*                                              */
/* File        : dmap.h                         */
/* Description : detector map library           */
/*                                              */
/* Author: Sfiligoi Igor                        */
/*                                              */
/* Created      : 07.04.1997                    */
/* Last modified: 18.08.1997                    */
/*                                              */
/************************************************/

#ifndef DMAP_H
#define DMAP_H

#ifdef __cplusplus
extern "C" {
#endif

/******************************************/
/*          Error constants               */
/*    (DMAP_ERROR_OK means no error )     */
/*    All the functions in this library   */
/*    return an error value               */
/*    (if not specified otherwise)        */
/******************************************/

#define DMAP_ERROR_OK		0
#define DMAP_ERROR_EMPTY       	-1	/* not a real error */
#define DMAP_ERROR_NOTINIT	-2
#define DMAP_ERROR_UNKNOWN	-3

#define DMAP_ERROR_LOAD_LINE	-10
#define DMAP_ERROR_CHECK_LINE	-11

#define DMAP_ERROR_CONSISTENCY	-20
#define DMAP_ERROR_REPEAT	-21

#define DMAP_ERROR_RANGE	-31

/******************************************/
/*           Type constants               */
/******************************************/

/* Detector constants */
#define DMAP_DET_ECAPA		1
#define DMAP_DET_BARREL		2
#define DMAP_DET_ECAPB		3
#define DMAP_DET_CHAMBER       	4
#define DMAP_DET_QCALA       	5
#define DMAP_DET_QCALB       	6

/* Board constants */
#define DMAP_BTYPE_CALTDC	1
#define DMAP_BTYPE_CALADC	2
#define DMAP_BTYPE_CHATDC	3
#define DMAP_BTYPE_QCALTDC	4
#define DMAP_BTYPE_QCALADC	5

/******************************************/
/*                Types                   */
/******************************************/

/* Calorimeter ADC and TDC */
typedef struct
      {
	unsigned char detector;	/* see DMAP_DET_... constants */
	unsigned char module;
	unsigned char plane;
	unsigned char column;
	unsigned char side;
      } DMAP_CAL_EL;

/* Chamber TDC */
typedef struct
      {
	unsigned char layer;
	unsigned short wire;
      } DMAP_CHAMBER_EL;

/* QCAL ADC and TDC */
typedef struct
      {
	unsigned char detector;	/* see DMAP_DET_... constants */
	unsigned char module;
      } DMAP_QCAL_EL;

/* Channel */
typedef union
      {
	DMAP_CAL_EL     cal;
	DMAP_CHAMBER_EL chamber;
	DMAP_QCAL_EL    qcal;
      } DMAP_CHAN_SUB_EL;

typedef struct
      {
	unsigned char    btype;	/* See DMAP_BTYPE_.. constants */
	DMAP_CHAN_SUB_EL data;
      } DMAP_CHAN_EL;

/******************************************/
/*     Initialization functions           */
/******************************************/


/* load the map in memory */
/* should be called before any other function in the library */
int dmap_load(char *filename);


/* free the allocated memory */
/* should be called after the last call */
/* to any of the functions in the library */
int dmap_dispose();

/******************************************/
/*            Get function                */
/******************************************/

int dmap_get(unsigned char chain,	/* IN:  chain   */
	     unsigned char crate,   	/* IN:  crate   */
	     unsigned char slot,    	/* IN:  slot    */
	     unsigned char chan,    	/* IN:  channel */
	     DMAP_CHAN_EL *data);	/* OUT: associated data */


/******************************************/
/*           Find function                */
/******************************************/

int dmap_find(DMAP_CHAN_EL  data,	/* IN:  data to find */
	      unsigned char *chain,	/* OUT: chain   */
	      unsigned char *crate,     /* OUT: crate   */
	      unsigned char *slot,      /* OUT: slot    */
	      unsigned char *chan);     /* OUT: channel */


/******************************************/
/*           Query functions              */
/******************************************/

/* returns the number of chains,
   or DMAP_ERROR_UNKNOWN in case of error */
int dmap_query_nr_chains(void);

/* returns the number of crates in the chain,
   or DMAP_ERROR_UNKNOWN in case of error */
int dmap_query_nr_crates(unsigned char chain);

/* returns the number of slots in the crate,
   or DMAP_ERROR_UNKNOWN in case of error */
int dmap_query_nr_slots(unsigned char chain,
			unsigned char crate);

/* returns the number of channels in the slot, 
   0 if the board is empty, 
   or DMAP_ERROR_UNKNOWN in case of error */
int dmap_query_nr_chans(unsigned char chain,
			unsigned char crate,
			unsigned char slot);

/* returns the type of the board in the slot, 
   DMAP_ERROR_EMPTY if the board is empty, 
   or DMAP_ERROR_UNKNOWN in case of error */
int dmap_query_btype(unsigned char chain,
		     unsigned char crate,
		     unsigned char slot);

#ifdef __cplusplus
}
#endif


#endif /* DMAP_H */