/* 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 <stdio.h>

#include "x10_api.h"


/* This is an example of an error handler */
void new_error_handler(int code,char *arg,struct x10_status_t *status)
{
  fprintf(stderr,"Erreur: %d %s\n",code,arg);
  exit(1);
}


/* This is an example of an event handler */
void function_hook(unsigned char house,int unit,int function,
		   int dim_level,struct x10_status_t *status)
{
  x10_log_event(house,unit,function,status);
  printf("************* Function %d (%d) on unit %c%d *************\n",function,dim_level,house,unit);
}


int main(int argc,char **argv)
{
  struct x10_status_t *status;
  int return_code;

  if(argc != 2)
    {
      fprintf(stderr,"Usage: %s device_name\n",argv[0]);
      exit(-1);
    }

  /* We open the API */
  if(!(status=x10_open(argv[1],new_error_handler,function_hook,10000000)))
    {
      fprintf(stderr,"Could not open the serial port %s !\n",argv[1]);
      exit(1);
    }

   /* We first set  the date */
/*          printf("\n\nSetting the CM11 to current time\n"); */
/*          return_code=x10_set_status_interface(status);             */
/*          printf("x10_set_status_interface return code = %d\n",return_code);             */

    /* We get  the date */
/*         printf("\n\nGetting time from the CM11A ...\n");          */
/*         return_code=x10_get_status_interface(status);          */
/*         printf("x10_get_status_interface return code = %d\n",return_code);          */
/*         printf("Time read from the CM11: %s, %dth day of the current year, at %d:%d:%d\n",          */
/*      	  status->day_of_week,status->julien_days,status->hours,          */
/*      	  status->minutes,status->seconds);          */

   /* And snoop the AC bus */
   printf("\n\nListening the AC bus ...\n"); 
   return_code=x10_listen_to_events(status,5000000);
   printf("x10_listen_to_events return code = %d\n",return_code); 

   x10_close(status);
   exit(0);

     /* We try global commands (affect every module) */
     printf("\n\nNow turning every module OFF ...\n");    
     return_code=x10_send_cmd('A',5,X10_CMD_ALL_UNITS_OFF,0,status);    
     printf("x10_send_cmd return code = %d\n",return_code);    

     printf("\n\nNow turning every light ON ...\n");  
     return_code=x10_send_cmd('A',5,X10_CMD_ALL_LIGHTS_ON,0,status);  
     printf("x10_send_cmd return code = %d\n",return_code);  

     printf("\n\nNow turning every light OFF ...\n");    
     return_code=x10_send_cmd('A',5,X10_CMD_ALL_LIGHTS_OFF,0,status);    
     printf("x10_send_cmd return code = %d\n",return_code);    


     /* The status command */
   printf("\n\nNow asking status of A1 ...\n");
   return_code=x10_send_cmd('A',1,X10_CMD_STATUS_REQUEST,0,status);
   printf("x10_send_cmd return code = %d (module is %d)\n",return_code,status->last_requested_func_code);

   /* The module activation commands */
   printf("\n\nNow turning OFF the A5 module ...\n");  
   return_code=x10_send_cmd('A',5,X10_CMD_OFF,0,status);  
   printf("x10_send_cmd return code = %d\n",return_code);  

   printf("\n\nNow turning ON the A5 module ...\n");  
   return_code=x10_send_cmd('A',5,X10_CMD_ON,0,status);  
   printf("x10_send_cmd return code = %d\n",return_code);  

   printf("\n\nNow dimming  the A5 module ...\n");  
   return_code=x10_send_cmd('A',5,X10_CMD_DIM,20,status);  
   printf("x10_send_cmd return code = %d\n",return_code);  

   printf("\n\nNow brighting the A5 module ...\n");  
   return_code=x10_send_cmd('A',5,X10_CMD_BRIGHT,20,status);  
   printf("x10_send_cmd return code = %d\n",return_code);  

   /* And snoop the AC bus */
   printf("\n\nListening the AC bus ...\n"); 
   return_code=x10_listen_to_events(status,10000000);
   printf("x10_listen_to_events return code = %d\n",return_code); 

   x10_close(status);

   return(0);
}
