// Copyright (c) 2007 Bernhard Fischer
// Licensed under GPL
// See the file COPYING distributed with this package.
// $Id: rrd_reader.h 197 2009-01-15 12:32:43Z reutner-fischer $
//

#define RRD_READONLY    (1<<0)
#define RRD_READWRITE   (1<<1)
#define RRD_CREAT       (1<<2)
#define RRD_READAHEAD   (1<<3)
#define RRD_COPY        (1<<4)

#ifndef DNAN
#define DNAN		set_to_DNAN()
#endif
#ifndef DINF
#define DINF		set_to_DINF()
#endif

double set_to_DNAN (void);
double set_to_DINF (void);


typedef union unival
{
  unsigned long u_cnt;
  rrd_value_t u_val;
} unival;

typedef struct stat_head_t
{

  /* Data Base Identification Section ** */
  char cookie[4];		/* RRD */
  char version[5];		/* version of the format */
  double float_cookie;		/* is it the correct double
				 * representation ?  */

  /* Data Base Structure Definition **** */
  unsigned long ds_cnt;		/* how many different ds provide
				 * input to the rrd */
  unsigned long rra_cnt;	/* how many rras will be maintained
				 * in the rrd */
  unsigned long pdp_step;	/* pdp interval in seconds */

  unival par[10];		/* global parameters ... unused
				   at the moment */
} stat_head_t;

enum dst_en
{ DST_COUNTER = 0,		/* data source types available */
  DST_ABSOLUTE,
  DST_GAUGE,
  DST_DERIVE,
  DST_CDEF
};
enum dst_en dst_conv (char *string);
/* The magic number here is one less than DS_NAM_SIZE */
#define DS_NAM_FMT    "%19[a-zA-Z0-9_-]"
#define DS_NAM_SIZE   20

#define DST_FMT    "%19[A-Z]"
#define DST_SIZE   20

typedef struct ds_def_t
{
  char ds_nam[DS_NAM_SIZE];	/* Name of the data source (null terminated) */
  char dst[DST_SIZE];		/* Type of data source (null terminated) */
  unival par[10];		/* index of this array see ds_param_en */
} ds_def_t;

#define MAX_RRA_PAR_EN 10

enum cf_en
{ CF_AVERAGE = 0,		/* data consolidation functions */
  CF_MINIMUM,
  CF_MAXIMUM,
  CF_LAST,
  CF_HWPREDICT,
  /* An array of predictions using the seasonal
   * Holt-Winters algorithm. Requires an RRA of type
   * CF_SEASONAL for this data source. */
  CF_SEASONAL,
  /* An array of seasonal effects. Requires an RRA of
   * type CF_HWPREDICT for this data source. */
  CF_DEVPREDICT,
  /* An array of deviation predictions based upon
   * smoothed seasonal deviations. Requires an RRA of
   * type CF_DEVSEASONAL for this data source. */
  CF_DEVSEASONAL,
  /* An array of smoothed seasonal deviations. Requires
   * an RRA of type CF_HWPREDICT for this data source.
   */
  CF_FAILURES
};

enum cf_en cf_conv (char *string);
#define CF_NAM_FMT    "%19[A-Z]"
#define CF_NAM_SIZE   20

typedef struct rra_def_t
{
  char cf_nam[CF_NAM_SIZE];	/* consolidation function (null term) */
  unsigned long row_cnt;	/* number of entries in the store */
  unsigned long pdp_cnt;	/* how many primary data points are
				 * required for a consolidated data
				 * point?*/
  unival par[MAX_RRA_PAR_EN];	/* index see rra_param_en */

} rra_def_t;

typedef struct live_head_t
{
  time_t last_up;		/* when was rrd last updated */
  long last_up_usec;		/* micro seconds part of the
				   update timestamp. Always >= 0 */
} live_head_t;

#define LAST_DS_LEN 30		/* DO NOT CHANGE THIS ... */
typedef struct pdp_prep_t
{
  char last_ds[LAST_DS_LEN];	/* the last reading from the data
				 * source.  this is stored in ASCII
				 * to cater for very large counters
				 * we might encounter in connection
				 * with SNMP. */
  unival scratch[10];		/* contents according to pdp_par_en */
} pdp_prep_t;

#define MAX_CDP_PAR_EN 10
typedef struct cdp_prep_t
{
  unival scratch[MAX_CDP_PAR_EN];

  /* contents according to cdp_par_en *
   * init state should be NAN */

} cdp_prep_t;

typedef struct rra_ptr_t
{
  unsigned long cur_row;	/* current row in the rra */
} rra_ptr_t;

typedef struct rrd_t
{
  stat_head_t *stat_head;	/* the static header */
  ds_def_t *ds_def;		/* list of data source definitions */
  rra_def_t *rra_def;		/* list of round robin archive def */
  live_head_t *live_head;	/* rrd v >= 3 last_up with us */
  time_t *legacy_last_up;	/* rrd v < 3 last_up time */
  pdp_prep_t *pdp_prep;		/* pdp data prep area */
  cdp_prep_t *cdp_prep;		/* cdp prep area */
  rra_ptr_t *rra_ptr;		/* list of rra pointers */
  rrd_value_t *rrd_value;	/* list of rrd values */
} rrd_t;



rrd_file_t *rrd_open (const char *file_name, rrd_t * rrd, int rdwr);
int rrd_close (rrd_file_t * rrd_file);
void rrd_free (rrd_t * rrd);
ssize_t rrd_read (rrd_file_t * rrd_file, void *buf, size_t count);
ssize_t rrd_write (rrd_file_t * rrd_file, const void *buf, size_t count);
void rrd_flush (rrd_file_t * rrd_file);
off_t rrd_seek (rrd_file_t * rrd_file, off_t off, int whence);
off_t rrd_tell (rrd_file_t * rrd_file);
