//=========================================================================
// Summary  :
// Filename : ModemSerial.h
// Author   : Andrew Grant
// Project  :
// Revision : 1
// Created  : 2001/01/19
// Modified : 2001/01/19
//=========================================================================
// Description :  Generic Modem Interface
//=========================================================================
#ifndef _MODEMSERIAL_H
#define _MODEMSERIAL_H

#include "ModemFork.h"
#ifdef WIN32
#include "C:\Program Files\Microsoft Visual Studio .NET\Vc7\include\time.h"
#else
#include <time.h>
#endif

#ifndef _MSC_VER
// Under QNX, use the standard SerialDevice.
# include "SerialDevice.h"
#else
// Under WINDOWS, use the WinSerialDevice.h
# include "WinSerialDevice.h"
#endif

#define iSERIAL_MAX_TRANSIT_BUFFER_SIZE 1024

///////////////////////////////////////////////////////
//  Serial Modem class, implementing modem as a serial port connection.

class ModemSerial : public ModemFork {

public:

  ModemSerial(const char* pcDevice, int baudRate = 9600);
  virtual ~ModemSerial();


  ///////////////////////////////////////////////////////////////////
  // reads specified number of bytes from data stream.
  //  if cannot read EXACT number of bytes, returns false, but still takes some data off of port
  //  times out appropriately for baud rate and num bytes
  virtual bool bReadFromDeviceChild(char* buffer, int nBytes);

  ///////////////////////////////////////////////////////////////////
  // reads specified number of bytes from data stream.
  //  returns number of bytes actually read
  //  times out appropriately for baud rate and num bytes
  virtual int iReadFromDeviceChild(char* buffer, int nBytes);

  ///////////////////////////////////////////////////////////////////
  // ModemFork OVERRIDES
  virtual int iReadFromModem(char* buffer, int iMaxLen);
  virtual int iWriteToModem(char* buffer, int iMaxLen);
  virtual int getBytesPerSecondChild();
  virtual bool bHasConnectionChild();
  virtual int  iExpectedRoundTripDuration() const;
  virtual int  iGetMaxTransitBytes() { return iSERIAL_MAX_TRANSIT_BUFFER_SIZE; };
  virtual SerialDevice* getDevice() { return _device; };


 protected:
  ///////////////////////////////////////////////////////////////////
  // searches for start code in data stream.
  //  if successful, next byte is the byte after the start code and returns true
  //  if fails, returns false
  virtual bool bFindEndOfStartCodeChild();

  int checksum(char* buf, int nbytes);

  int         _iBaudRate;
  SerialDevice* _device;
  time_t _tTimeOfLastReceive;
  time_t _tTimeOfLastSend;
  char* _pcStartCode;       // unique data stream start code!
  int _iStartCodeLength;          // length of start code

	FILE * _logFile;

  // Used by bFindEndOfStartCode to handle slow data issues
  char*       _pcLeftovers;       // copy of leftover bytes from last time.
  int         _iLeftoversLength;  // how many of the leftovers are valid bytes?
};


#endif
