/****************************************************************************/
/* Copyright (c) 2012 MBARI                                                 */
/* MBARI Proprietary Information. All rights reserved.                      */
/****************************************************************************/
/* Summary  :                                                               */
/* Filename : DeltaT.h                                                      */
/* Author   :                                                               */
/* Project  :                                                               */
/* Version  : 1.0                                                           */
/* Created  : 09/17/2012                                                    */
/* Modified :                                                               */
/* Archived :                                                               */
/****************************************************************************/
/* Modification History:                                                    */
/****************************************************************************/
#ifndef _DELTAT_H
#define _DELTAT_H

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "TimeP.h"
#include "PeriodicTask.h"
#include "DeviceIF.h"
#include "FastCatIF.h"
#include "DeltaTInput.h"
#include "DeltaTOutput.h"
#include "DeltaTLog.h"
#include "DeltaTMessage.h"

#define DeltaTTaskName "deltaTdriver"
#define DT_83P  1
#define DT_83B  2
#define DT_83Z  3



/*
CLASS 
DeltaT

DESCRIPTION
Imagenex DeltaT Device Driver Interface

AUTHOR
Henthorn

*/

class DeltaT {

  friend class DeltaTLog;

public:
  
  /////////////////////////////////////////////////////////
  // Constructor
  DeltaT(char *name, unsigned int port);

  /////////////////////////////////////////////////////////
  // overloaded Constructor
  //DeltaT();

  ///////////////////////////////////////////////////////
  //destructor
  virtual  ~DeltaT();

  //////////////////////////////////////////////////////////////// 
  // initialization function
  virtual DeviceIF::Status initialize();

  // Start logging
  //
  virtual void start();

  // Stop logging
  //
  virtual void stop();

  ////////////////////////////////////////////////////////////////////
  // run
  virtual void run();

  virtual int get_sockfd() { return _sockfd; }

  // Request 83P output
  //
  virtual Boolean output83p();
  virtual Boolean output83p(Boolean on_off);

  int  messageHandler();

protected:
  unsigned int _port;
  int _sockfd;
  char *_name;
  struct sockaddr_in _client_addr;
  struct sockaddr_in _server_addr;
  struct timeval     _server_tv;

  Boolean _xmit;
  Boolean _enabled;
  Boolean _output83p;
  Boolean _auto_sos;
  FastCatIF *_ctd;

  long _sos;
  char _send_buf[256];
  unsigned char _receive_buf[2500];
  unsigned int  _offset;  // Used to point into _receive_buf
  DeltaTLog *_log;
  int _fd83p;

  DeltaTInput  *_input;
  DeltaTOutput *_output;
  DeltaTMessage *_msgQ;

  int _sector_size;

  unsigned long _last_ping_number;
  
  ////////////////////////////////////////////////////////////////////
  // DeltaT beam forming app comms
  int  a2month(const char* month); // converts "JAN", "FEB", ... to 0, 1, ...
  int  processPacket(int nbytes_in_record);
  void process83PData();
  void initExternalControlSwitches();
  void sendExternalControlSwitches();
  int initialize_socket(int port);
  void attach_ctd_server(int wait_time);

  ////////////////////////////////////////////////////////////////////
  // DeltaTIF interface functions
  // Also included in config file
  //
  void set_idt_logging(Boolean on);
  void set_mvc_logging(Boolean on);
  void set_averaging(long avg);
  void set_beam_width(long width);
  void set_sector_size(long size);
  void set_nbeams(long nb);
  void set_sos(long sos);
  void set_range(long range);
  void set_gain(long gain);
  void set_gain_eq(Boolean on);

  // DeltaT configuration settings
  //
  void set_profile_min_range(long range);
  void set_xducer_orientation(Boolean up);
  void set_tilt_angle(long degrees);
  void set_trigger_enable(Boolean enable);
  void set_trigger_edge(Boolean positive);
  void set_trigger_delay(long usec);
  void set_profile_point_filter(int ppf);

  int  create83P();
  int  write83P(int nbytes);

};

#endif
