/*
Copyright 2003/2004, Freie Universitaet Berlin. All rights reserved.

These sources were developed at the Freie Universität Berlin, Computer
Systems and Telematics group.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

- Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

- Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
 
- Neither the name of Freie Universitaet Berlin (FUB) nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

This software is provided by FUB and the contributors on an "as is"
basis, without any representations or warranties of any kind, express
or implied including, but not limited to, representations or
warranties of non-infringement, merchantability or fitness for a
particular purpose. In no event shall FUB or contributors be liable
for any direct, indirect, incidental, special, exemplary, or
consequential damages (including, but not limited to, procurement of
substitute goods or services; loss of use, data, or profits; or
business interruption) however caused and on any theory of liability,
whether in contract, strict liability, or tort (including negligence
or otherwise) arising in any way out of the use of this software, even
if advised of the possibility of such damage.

This implementation was developed by the CST group at the FUB.
Contributors: Bjoern Lichtblau

For documentation and questions please use the web site
http://www.http://scatterweb.mi.fu-berlin.de and the mailinglist
scatterweb@lists.spline.inf.fu-berlin.de (subscription via the Website).
Berlin, 2003/2004/2005
*/

#include "ScatterWeb.Event.h"

appconfig_t AppConfig;

void appconfig_load() {
  IO_read( EADR_APPCONFIG, (UINT8*) &AppConfig, sizeof(appconfig_t));
}

void appconfig_save() {
  IO_write( EADR_APPCONFIG, (UINT8*) &AppConfig, sizeof(appconfig_t));
}

void handleEvent(sdata_t* data) {
    if(AppConfig.announceFlags & AF_SERIAL) Comm_print("%e\r\n", data);
    if(AppConfig.announceFlags & AF_RADIO) {
      packet_t p; // = { BROADCAST, 0, SENSOR_PACKET, 0, 0, 0, (UINT8*) event, sizeof(*event) };
      p.to = BROADCAST;
      p.type = SENSOR_PACKET;
      p.header_length = 0;
      p.data = (UINT8*) data;
      p.data_length = sizeof(*data);
      Net_send(&p, NULL);
    }
}

COMMAND(esr, 0) {
  AppConfig.announceFlags |=  AF_RADIO; 
  if(AppConfig.sensorMask & SENSOR_VIBRATION) Data_vibrationOff();
  if(AppConfig.sensorMask & SENSOR_MOVEMENT) Data_movementOff();
  if(AppConfig.sensorMask & SENSOR_BUTTON) Data_buttonOff();
  if(AppConfig.sensorMask & SENSOR_MICROPHONE) Data_microphoneOff();
  if(AppConfig.sensorMask & SENSOR_VOLTAGE) Data_batteryOff();
  AppConfig.sensorMask |= SENSOR_VIBRATION + SENSOR_MOVEMENT + SENSOR_BUTTON + SENSOR_MICROPHONE + SENSOR_VOLTAGE; 
  if(AppConfig.sensorMask & SENSOR_VIBRATION) Data_vibrationOn(1000);
  if(AppConfig.sensorMask & SENSOR_MOVEMENT) Data_movementOn(1500);
  if(AppConfig.sensorMask & SENSOR_BUTTON) Data_buttonOn(100);
  if(AppConfig.sensorMask & SENSOR_MICROPHONE) Data_microphoneOn(500, 600);
  if(AppConfig.sensorMask & SENSOR_VOLTAGE) Data_batteryOn(1000, 2600);
  appconfig_save(); 
}                
  
COMMAND(dsr, 0) { 
  AppConfig.announceFlags = 0x00; 
  if(AppConfig.sensorMask & SENSOR_VIBRATION) Data_vibrationOff();
  if(AppConfig.sensorMask & SENSOR_MOVEMENT) Data_movementOff();
  if(AppConfig.sensorMask & SENSOR_BUTTON) Data_buttonOff();
  if(AppConfig.sensorMask & SENSOR_MICROPHONE) Data_microphoneOff();
  if(AppConfig.sensorMask & SENSOR_TEMPERATURE) Data_temperatureOff();
  if(AppConfig.sensorMask & SENSOR_VOLTAGE) Data_batteryOff();  
  AppConfig.sensorMask = 0x00; 
  appconfig_save();   
}                

COMMAND(raf, 0) { 
 String_write(term_reply, "%b [%s%s]", AppConfig.announceFlags,
                          ((AppConfig.announceFlags & AF_SERIAL) ? "SERIAL " : ""),
                          ((AppConfig.announceFlags & AF_RADIO) ? "RADIO " : "")
  );
}                 

COMMAND(saf, 0) { 
  AppConfig.announceFlags = String_fromHexToByte( &str[4]);
  appconfig_save(); 
  term_raf(str);
} 

COMMAND(rsm, 0) { 
  String_write(term_reply, "%b [%s%s%s%s%s%s%s]", AppConfig.sensorMask,
                          ((AppConfig.sensorMask & SENSOR_VIBRATION) ? "VIB " : ""),
                          ((AppConfig.sensorMask & SENSOR_MOVEMENT) ? "PIR " : ""),
                          ((AppConfig.sensorMask & SENSOR_BUTTON) ? "BUTTON " : ""),
                          ((AppConfig.sensorMask & SENSOR_MICROPHONE) ? "MIC " : ""),
                          ((AppConfig.sensorMask & SENSOR_RC5) ? "RC5 " : ""),
                          ((AppConfig.sensorMask & SENSOR_TEMPERATURE) ? "TEMP " : ""), 
                          ((AppConfig.sensorMask & SENSOR_VOLTAGE) ? "BAT " : "")
                          
  ); 
}                    

COMMAND(ssm, 0) { 
  if(AppConfig.sensorMask & SENSOR_VIBRATION) Data_vibrationOff();
  if(AppConfig.sensorMask & SENSOR_MOVEMENT) Data_movementOff();
  if(AppConfig.sensorMask & SENSOR_BUTTON) Data_buttonOff();
  if(AppConfig.sensorMask & SENSOR_MICROPHONE) Data_microphoneOff();
  if(AppConfig.sensorMask & SENSOR_RC5) Data_RC5Disable();
  if(AppConfig.sensorMask & SENSOR_TEMPERATURE) Data_temperatureOff(); 
  if(AppConfig.sensorMask & SENSOR_VOLTAGE) Data_batteryOff();  
  AppConfig.sensorMask = String_fromHexToByte(&str[4]); appconfig_save(); 
  if(AppConfig.sensorMask & SENSOR_VIBRATION) Data_vibrationOn(1000);
  if(AppConfig.sensorMask & SENSOR_MOVEMENT) Data_movementOn(1500);
  if(AppConfig.sensorMask & SENSOR_BUTTON) Data_buttonOn(100);
  if(AppConfig.sensorMask & SENSOR_MICROPHONE) Data_microphoneOn(500, 600);
  if(AppConfig.sensorMask & SENSOR_RC5) Data_RC5Enable();
  if(AppConfig.sensorMask & SENSOR_VOLTAGE) Data_batteryOn(1000, 2100);  
  if(AppConfig.sensorMask & SENSOR_TEMPERATURE) Data_temperatureOn(6144, 0x0080, 0x0080, 20); // Every 25 s or if changed more than 1°C.
  term_rsm(str); // answer is like rsm
} 

bool Event_init() {
  appconfig_load();
  Comm_print("| AppConfig: \r\n", AppConfig.announceFlags, AppConfig.sensorMask);
  term_raf(NULL); Comm_print("| announceFlags: %s\r\n", term_reply);
  term_rsm(NULL); Comm_print("| sensorMask: %s\r\n", term_reply);
  System_registerCallback(C_SENSOR, handleEvent);  
  if(AppConfig.sensorMask & SENSOR_VIBRATION) Data_vibrationOn(1000);
  if(AppConfig.sensorMask & SENSOR_MOVEMENT) Data_movementOn(1500);
  if(AppConfig.sensorMask & SENSOR_BUTTON) Data_buttonOn(100);
  if(AppConfig.sensorMask & SENSOR_MICROPHONE) Data_microphoneOn(500, 600);
  if(AppConfig.sensorMask & SENSOR_RC5) Data_RC5Enable();
  if(AppConfig.sensorMask & SENSOR_VOLTAGE) Data_batteryOn(1000, 2100);
  if(AppConfig.sensorMask & SENSOR_TEMPERATURE) Data_temperatureOn(6144, 0x0080, 0x0080, 20); // Every 25 s or if changed more than 1°C.
  return True;  
}
