/****************************************************************************/
/* Copyright (c) 2007 MBARI                                                 */
/* MBARI Proprietary Information. All rights reserved.                      */
/****************************************************************************/
/* Summary  :                                                               */
/* Filename : AtsDriver.h                                                   */
/* Author   :                                                               */
/* Project  : Seafloor Mapping                                              */
/* Version  : 1.0                                                           */
/* Created  : 06/07/2007                                                    */
/* Modified :                                                               */
/* Archived :                                                               */
/****************************************************************************/
/* Modification History:                                                    */
/****************************************************************************/
#ifndef _AtsDriver_H
#define _AtsDriver_H

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/ioctl.h> // IO Control of socket.
#include "Task.h"
#include "Attributes.h"
#include "TailConeIF.h"
#include "NavigationIF.h"
#include "TimeIF.h"
#define DEFAULT_PORT 9009

/*
CLASS 
AtsDriver

DESCRIPTION
Track the drift between the clocks of MVC and the Reson data logging system
(currently the 6046 PLC) by reading the system time from the AuvTimeSync (Ats)
on the PLC and comparing them to the MVC time.
*/

class AtsOutput;
class AtsLog;

class AtsDriver : public Task {

public:
  
  friend class AtsLog;

  /////////////////////////////////////////////////////////
  // overloaded Constructor
  AtsDriver(const short port = DEFAULT_PORT);

  ///////////////////////////////////////////////////////
  //destructor
  virtual  ~AtsDriver();
  int init();
  void setVerbose(const Boolean b) { _verbose = b; }
  void run();


protected:

  AtsOutput* _output;
  AtsLog*    _log;
  Boolean    _verbose;

  struct sockaddr_in  _servaddr;
  int   _atsSocket;
  short _atsPort;
  float _baseline;
  float _lastLoggedDelta;

  //////////////////////////////////////////////////////////////// 
  // initialization function - configure the socket interface to
  // the Ats
  //
  int  setupSocket();
  
  // Calculate the current time difference between system clocks
  //
  int  calcTimeDiff(float *diff);
  void logData();  
};

#endif
