/****************************************************************************/
/* Copyright (c) 2000 MBARI                                                 */
/* MBARI Proprietary Information. All rights reserved.                      */
/****************************************************************************/
/* Summary  :                                                               */
/* Filename : AcousticAbort.cc                                              */
/* Author   :                                                               */
/* Project  :                                                               */
/* Version  : 1.0                                                           */
/* Created  : 02/07/2000                                                    */
/* Modified :                                                               */
/* Archived :                                                               */
/****************************************************************************/
/* Modification History:                                                    */
/****************************************************************************/
#include "MissionTimeAttribute.h"
#include "IntegerAttribute.h"
#include "AcousticAbort.h"
#include "Syslog.h"
#include <fcntl.h>
#include <sys/qioctl.h>

AcousticAbort::AcousticAbort()
  : Behavior(AcousticAbortBehaviorName, NonSequential)
{
     Attribute::Input *input;

     attributes.add(new IntegerAttribute("dummyAttribute",
				         "dummyAttribute",
					 &_dummy));

     // Acoustic abort should be default run forever
     input = new Attribute::Input("endTime", NoTimeLimitMnem);
     attributes.parse(input);
     delete input;

     _dev = open("/dev/par1", O_RDONLY);
}


AcousticAbort::~AcousticAbort()
{
}


Boolean AcousticAbort::validInput()
{
  return True;
}


void AcousticAbort::execute()
{
unsigned long flag[2] = {0,0};
unsigned long status;
static int cnt = 0;
unsigned long edge_status = 0;

    if (cnt++%5) return;
    flag[0] = flag[1] = 0;
    status = 0;
    if (qnx_ioctl(_dev, QCTL_DEV_CTL, &flag, 8, &status, 4) < 0) return;
    edge_status = status & EDGE_BITS;

    if (edge_status == EDGE_ABORT) {
      Syslog::write("Received Acoustic Abort, Aborting!");
      abortMission();
      return;
  }

  // (this is new from Ody) if the stack is empty, then pass it through
  // (that is, if all behaviors above are inactive, then be inactive also
  if( isOutputEmpty() ) {
       Syslog::write("AcousticAbort::execute() - Received empty stack, passing it through\n");
       return;
  }

}






