/****************************************************************************/
/* Copyright (c) 2004 MBARI                                                 */
/* MBARI Proprietary Information. All rights reserved.                      */
/****************************************************************************/
/* Summary  :                                                               */
/* Filename : BenthosModem.h                                                */
/* Author   :                                                               */
/* Project  :                                                               */
/* Version  : 1.0                                                           */
/* Created  : 09/27/2004                                                    */
/* Modified :                                                               */
/* Archived :                                                               */
/****************************************************************************/
/* Modification History:                                                    */
/****************************************************************************/
#ifndef _BENTHOSMODEM_H
#define _BENTHOSMODEM_H

#include "PeriodicTask.h"
#include "SerialDeviceDriver.h"
#include "DeviceIF.h"
#include "Attributes.h"
#include "NavigationIF.h"
#include "LayeredControlIF.h"
#include "FanIF.h"
#include "BenthosModemCommand.h"
#include "BenthosModemOutput.h"
#include "smac.h"

/*
CLASS
BenthosModem

DESCRIPTION
Benthos Modem Device Driver Interface

AUTHOR
Rich Henthorn

*/

class BenthosModem : public PeriodicTask {

  friend class BenthosModemOutput;  
public:

  BenthosModem(SerialDevice *serialDevice, Boolean verbose);

  virtual ~BenthosModem();

  Boolean initialized() { return _initialized; }

  typedef struct {
    int nbytes;
    char data[1024];
    int src, dest; //src and est modem address
    Boolean crcPass; //logical
    float multiPathDelay; //milliseconds
    float snr; //dB, larger is better
    int agc; //hex number indicating gain
    float spd; //relative knots btwn src and dest
    int ccerr; //number of errors corrected by convolutional encoder
  } BenthosModemPacket;

protected:

  SerialDevice *_device;
  Boolean _verbose;
  BenthosModemCommand *_command;

  ////////////////////////////////////////////////////////////////////
  // initialization routine
  Boolean initialize();

  // Override base class callback function
  //
  void periodicCallback();

  int commandMode();
  int onlineMode();
  int setRegister(short reg, short value);
  int power(short powerLevel);
  int dopplerCorrection(short on_off); // 0 = off, otherwise on
  int range(short modemAddress, float *meters);
  void write(char *string);
  int transpond(short freq);
  Boolean readPacket(BenthosModemPacket *packet);
  Boolean dispatchPacket(BenthosModemPacket *packet);
  void sendStatus();

  int _state;
  int _interval;
  NavigationIF *_nav;
  LayeredControlIF *_layeredControl;
  FanIF *_fan;
  Boolean _initialized;
  unsigned char _buffer[1024]; //buffer for incoming messages - THEY NEED TO
                               //BE SHORTER THAN THIS
  int _bytesInBuffer;
  BenthosModemOutput *_output;
  char *BenthosModem::memstr(char *haystack, char *needle, int size);
};

#endif
