/**********************************************************************
 *                                                                    *
 * Copyright 1999, 2000, Borst Automation, Fachingen                  *
 * All Rights Reserved                                                * 
 *                                                                    *
 *         File Name: PcComPrt.h                                      *
 *            Device: IBM PC compatible, Windows API32                *
 *         Author(s): Walter Borst (bow), Borst Automation            *
 *                                                                    *
 * Description                                                        *
 * ===========                                                        *
 * See PcComPrt.c                                                     *
 *                                                                    *
 * Revision History                                                   *
 * ================                                                   *
 * date:      name:  version:  description:                           *
 * jan-00     bow    00000     module created                         *
 * jul-00     bow    01000     changes for project li001/00           *
 *                                                                    *
 **********************************************************************/
//Prolog
#ifndef _PcComPrt_h_
#define _PcComPrt_h_

/**********************************************************************
 * Includes                                                           *
 **********************************************************************/
#ifdef __PCCOMPRT_H__
  #define USECOMM        // Need the COMM API
  #define _WINMM_        // Need multi media
  #pragma warning( disable: 4115) 
  //Pragma set because microsoft windows header does not compile
  //without this warning
  #include <windows.h>
  #pragma warning( default: 4115)
  #include <io.h>
  #include <memory.h>
  #include "..\Common\Types.h"
  #include "..\Common\Hart.h"
  #include "..\Protocol\HartLoL.h"

  /**********************************************************************
   * Definitions                                                        *
   **********************************************************************/
   //Pointer to call back function
  #define LP_CALLAPP VOID *

  //For the easy writer
  #define COMPORTINFO   m_strComPortInfo[iComIdx]
  #define PCOMPORTINFO  ((typCOMPORTINFO*) pParam)

  //Superimposed state machine
  #define SISM_IDLE           0
  #define SISM_TRANSMITTING   1
  #define SISM_RECEIVING      2

  //COM1 to COM8
  #define MAXCOMPORT          8

  //Block size
  #define MAXBLOCK          256

  /**********************************************************************
   * Structures                                                         *
   **********************************************************************/

  typedef struct { ULONG    ulTimerStarted;
                   ULONG    ulLastReceiveTime;
                   ULONG    ulTimeOutMargin;
                   ULONG    ulEventTime;
                   ULONG    ulRcvTimeOutMargin;
                   ULONG    ulDownCount;
                   BOOL     bTimeOut;
                   BOOL     bLocked;
                   UINT     uiDrvIdx;               
                 } typTimerControlStruct;

  typedef struct tagCOMPORTINFO
  {
    ULONG                   ulConnected;
    INT                     iComIdx;
    ULONG                   ulLastTransitionTime;
    BYTE                    bySendBuf[256];
    UINT                    uiSendBufLen;
    UINT                    uiCount;
    UINT                    uiState;
    /********************************/
    HANDLE                  hComFileHandle;
    HANDLE                  hComThread;
    ULONG                   ulThreadId;
    OVERLAPPED              osRead;
    OVERLAPPED              osWrite;
    LP_CALLAPP              pAppCallBack;
    UINT                    uiThreadRepeat;
    UINT                    uiThreadEnded;
    UINT                    uiSISMstatus;
  } typCOMPORTINFO;
#endif //ifdef __PCCOMPRT_H__

/**********************************************************************
 * Prototypes                                                         *
 **********************************************************************/
VOID PcComPrtInitialize(VOID);
BOOL OpenComPort( UINT uiComPort,
                  UINT uiBaudrate,
                  UCHR ucDataBits,
                  UCHR ucParity,
                  UCHR ucStopBits
                );
VOID CloseComPort(INT iComPort);
VOID WriteToCom( INT    iComPort,
                 BYTE*  pbBuf,
                 UINT   uiLen
               );
UCHR GetRcvChar(VOID);
UCHR GetRcvErr(VOID);

#ifdef __PCCOMPRT_H__

  static INT ReadCommBlock(LPSTR pParam, LPSTR lpszBlock);
  static DWORD WINAPI CommWatchProc(LPSTR pData);


  /**********************************************************************
   * Makros                                                             *
   **********************************************************************/
  #define COMPORT_START_SEND EscapeCommFunction(PCOMPORTINFO->hComFileHandle,SETRTS); \
	                           EscapeCommFunction(PCOMPORTINFO->hComFileHandle,CLRDTR); \
                             Sleep(30); \
                             ClearCommError(PCOMPORTINFO->hComFileHandle, &dwErrorFlags,&ComStat); \
                             WriteFile( PCOMPORTINFO->hComFileHandle, \
                                        PCOMPORTINFO->bySendBuf, \
                                        PCOMPORTINFO->uiSendBufLen, \
                                        &dwWritten, \
                                        &(PCOMPORTINFO->osWrite) \
                                      )
  #define COMPORT_TRANSMIT_DONE Sleep(20); \
                                EscapeCommFunction(PCOMPORTINFO->hComFileHandle,SETDTR) ; \
                                EscapeCommFunction(PCOMPORTINFO->hComFileHandle,CLRRTS) ; \
                                ReadCommBlock((LPSTR) pParam,(LPSTR) abIn); \
                                PCOMPORTINFO->uiSISMstatus = SISM_IDLE
#endif
//Epilog
#endif

