/* Copyright 1999 by Herve Regad-Pellagru, E-mail: regad@micronet.fr

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */

#include <time.h>
#include <windows.h>
#include <sys/types.h>

/* X10 module command codes */

#define X10_CMD_ALL_UNITS_OFF			0
#define X10_CMD_ALL_LIGHTS_ON			1
#define X10_CMD_ON				2
#define X10_CMD_OFF				3
#define X10_CMD_DIM				4
#define X10_CMD_BRIGHT				5
#define X10_CMD_ALL_LIGHTS_OFF			6
#define X10_CMD_EXTENDED_CODE		7
#define X10_CMD_HAIL_REQUEST			8
#define X10_CMD_HAIL_AKNOWLEDGE		9
#define X10_CMD_PRE_SET_DIM1			10
#define X10_CMD_PRE_SET_DIM2			11
#define X10_CMD_EXTENDED_DATA_TRANSFER	12
#define X10_CMD_STATUS_ON			13
#define X10_CMD_STATUS_OFF			14
#define X10_CMD_STATUS_REQUEST	       	15
#define X10_CMD_DUMMY				16


/* Those are the objects status, as noted in the x10_object_t structure */
#define X10_OBJECT_UNKNOWN		0
#define X10_OBJECT_ON			1
#define X10_OBJECT_OFF			2
#define X10_OBJECT_DIM_BRIGHT	       	3

#ifdef  __cplusplus
extern "C" {
#endif

/* contains the state of an X10 module and the time this state was obtained */
struct x10_object_t {
  unsigned char status;
  time_t timestamp;
};

/* This is the handle of the API. It contains all of its state */
struct x10_status_t {
  /* win descriptor of the serial port */
  HANDLE fdinout;
  /* timeout (microseconds) after which an x10_read() will return if no data is
     read */
  int timeout_chat;
  /* fd_set for select(2) in x10_read() */
  fd_set serial_set;

  /* house+unit+func last seen */
  unsigned char last_house_code;
  int last_unit_code;
  int last_func_code;

  /* same as above but used for the X10_CMD_STATUS_REQUEST for it not to
     collide with the above */
  unsigned char last_requested_house_code;
  int last_requested_unit_code;
  int last_requested_func_code;
  
  /* handlers for error condition and for every events (X10 commands 
     snooped on the AC bus */
  void (*error)(int code,char *arg,struct x10_status_t *status);
  void (*function_hook) (unsigned char house_code,
			 int unit_code,
			 int func_code,int dim_level,
			 struct x10_status_t *status);

  /* time data to load to the CM11A (x10_set_status_interface()) and being downloaded
     (x10_get_status_interface()) */
  short int battery_life;
  short int revision;
  short int seconds;
  short int minutes;
  short int hours;
  short int julien_days;
  char *day_of_week;

  void* param;

  /* known status of every x10 modules, used in x10_log_event() */
  struct x10_object_t objects[16][16];
};


struct x10_status_t *x10_open(char *,
			      void (*)(int,char *,struct x10_status_t *),
			      void (*) (unsigned char,int,int,int,struct x10_status_t *),
			      int, void*);
void x10_close(struct x10_status_t *);
int x10_get_status_interface(struct x10_status_t *);
int x10_set_status_interface(struct x10_status_t *);
int x10_send_cmd(char,int,int,int,struct x10_status_t *);
int x10_listen_to_events(struct x10_status_t *,int);
void x10_log_event(char,int,int,struct x10_status_t *);
void x10_default_error(int,char *,struct x10_status_t *);

#ifdef  __cplusplus
}
#endif