/*-----------------------------------------------------------------------*
  CLASS: WatchdogServer

  DESCRIPTION: Server for WDIII watchdog system.

  AUTHOR: Aaron Marsh

  $Id: WatchdogServer.cc,v 1.4 2000/09/15 19:50:54 jrieffel Exp $
 *-----------------------------------------------------------------------*/

#include <process.h>

#include "WatchdogServer.h"
#include "WatchdogOutput.h"
//#include "IntegerAttribute.h"


#define BAUD 9600
#define PARITY "NONE"
#define STOP_BITS 1
#define DATA_BITS 8
#define READ_TIMEOUT 750
//static char *PREFIX = "#WD,MVC;";


WatchdogServer::WatchdogServer(SerialParameters *serialParams,
			       const char *configFileName) : WatchdogIF_SK()
  //,  _attributes("watchdogServer")
{
  _serialParams = serialParams;
  _output = new WatchdogOutput(SharedData::ReadWrite);
  _command = new WatchdogCommand(MessageQueue::Write);
  //  _device = device;
  //_attributes.add(new IntegerAttribute("keepalive_time_hh", "Deadman Timer Hours", &_wd_time.hh, 100.));
  
  //_attributes.add(new IntegerAttribute("keepalive_time_mm", "Deadman Timer Minutes", &_wd_time.mm, 100.));
  
  //_attributes.add(new IntegerAttribute("keepalive_time_ss", "Deadman Timer Seconds", &_wd_time.ss, 100.));
  
  //AttributeParser::parse(configFileName, &_attributes);
  
  // now that they're parsed, write them to the shared memory;
  // they can be changed later via the deadman on/off commands
  //_output->data.deadman_time_hh = _wd_time.hh;
  //_output->data.deadman_time_mm = _wd_time.mm;
  //_output->data.deadman_time_ss = _wd_time.ss;
  
  //  _output->write();

  //  _log = new WatchdogServerLog( this, DataLog::AsciiFormat );
}


WatchdogServer::~WatchdogServer()
{
     delete _output;
}

void WatchdogServer::name(DeviceIF::Name name)
{
  strcpy(name, "Watchdog");
}


void WatchdogServer::serialNumber(DeviceIF::Name number)
{
  strcpy(number, "XXX");
}


DeviceIF::Status WatchdogServer::initialize()
{
  // Dummy implementation for now
  return DeviceIF::Ok;
}


DeviceIF::Status WatchdogServer::powerOn()
{
  // Dummy implementation for now
  return DeviceIF::Ok;
}


DeviceIF::Status WatchdogServer::powerOff()
{
  // Dummy implementation for now
  return DeviceIF::Ok;
}


DeviceIF::Status WatchdogServer::status()
{
  // Dummy implementation for now
  return DeviceIF::Ok;
}


DeviceIF::Status WatchdogServer::dataLoggingOn()
{
  // Dummy implementation for now
  return DeviceIF::Ok;
}


DeviceIF::Status WatchdogServer::dataLoggingOff()
{
  // Dummy implementation for now
  return DeviceIF::Ok;
}


DeviceIF::Status WatchdogServer::keepAlive(WatchdogIF::WD_Time_Struct *_wd_time)
{
   // keepAlive functionality is depricated for now
     // that is, it is handled exclusively by the periodic callback
  // resident within the driver-side.
  return DeviceIF::Ok;
}

DeviceIF::Status WatchdogServer::keepAlive(short time_in_minutes)
{
   // keepAlive functionality is depricated for now
     // that is, it is handled exclusively by the periodic callback
  // resident within the driver-side.
  return DeviceIF::Ok;
}

DeviceIF::Status WatchdogServer::Deadman_On(WatchdogIF::WD_Time_Struct *_wd_time)
{
    // we'll send the command here, and let the driver
  // set the deadman_on flag
  // after it sends the deadman_on command
  //  _output->data.deadman_on = 1;
  WatchdogCommand::Command cmd(WatchdogCommand::DeadmanOn);
    _command->write( &cmd );
  _output->data.deadman_time_hh = _wd_time->hh;
  _output->data.deadman_time_mm = _wd_time->mm;
  _output->data.deadman_time_ss = _wd_time->ss;
  
  _output->write();
  

  return DeviceIF::Ok;
}

DeviceIF::Status WatchdogServer::Deadman_On(short time_in_minutes)
{
  // we'll send the command here, and let the driver
  // set the deadman_on flag
  // after it sends the deadman_on command
  //  _output->data.deadman_on = 1;
  WatchdogCommand::Command cmd(WatchdogCommand::DeadmanOn);
    _command->write( &cmd );

  _output->data.deadman_time_hh = 0;
  _output->data.deadman_time_mm = time_in_minutes;
  _output->data.deadman_time_ss = 0;

  _output->write();


  return DeviceIF::Ok;
}

// the default of 5 minutes
DeviceIF::Status WatchdogServer::Deadman_On()
{
  // we'll send the command here, and let the driver
  // set the deadman_on flag
  // after it sends the deadman_on command
  //  _output->data.deadman_on = 1;
  WatchdogCommand::Command cmd(WatchdogCommand::DeadmanOn);
    _command->write( &cmd );

  _output->data.deadman_time_hh = 0;
  _output->data.deadman_time_mm = 5;
  _output->data.deadman_time_ss = 0;

  _output->write();


  return DeviceIF::Ok;
}


DeviceIF::Status WatchdogServer::Deadman_Off()
{
  // we'll send the command here, and let the driver
  // set the deadman_on flag
  // after it sends the deadman_on command
  //  _output->data.deadman_on = 0;
  WatchdogCommand::Command cmd(WatchdogCommand::DeadmanOff);
  _command->write( &cmd );

  _output->write();
  return DeviceIF::Ok;
}


DeviceIF::Status WatchdogServer::raiseAntenna()
{
  // yeah, send a command via the message queue or something
  WatchdogCommand::Command cmd(WatchdogCommand::RaiseAntenna);
  _command->write( &cmd );
   
   return DeviceIF::Ok;
}


DeviceIF::Status WatchdogServer::lowerAntenna()
{
  WatchdogCommand::Command cmd( WatchdogCommand::LowerAntenna);
 _command->write( &cmd );

  return DeviceIF::Ok;
}

//functions below are un-implimented for now
// thank goodness!

DeviceIF::Status WatchdogServer::resetMVC( )
{
  
  return DeviceIF::Ok;
}

DeviceIF::Status WatchdogServer::rebootMVC()
{
  return DeviceIF::Ok;
}

DeviceIF::Status WatchdogServer::sleepMVC()
{
  return DeviceIF::Ok;
}

int WatchdogServer::spawnAuxTasks()
{
  char *program = "watchdogDriver";
  char errorBuf[256];
  char buf[256];
 
  pid_t pid;
 
  switch ((pid = fork())) {
     
  case 0:
    // In child
    // Execute server program
    execlp(program, program, "-serial", _serialParams->string(), 0);

    sprintf(errorBuf, 
	    "Task::spawnServer() - execlp() of \"%s\" failed", program);

    perror(errorBuf);
 
    break;

  case -1:
    perror("Task::spawnServer() - fork() failed");
    return -1;
  }

  return 0;
}
