// Zmodlib.cpp


#include "zmcore.h"
#include "Zmodlib.h"
#include "error.h"
#include "unused.h"

HANDLE hSerCom;
#define READ_TIMEOUT      500      // milliseconds
#define INQUESIZE		5000
#define	OUTQUESIZE		5000

/*****************************************************************/

int InitCom(void)
{
	
	
	printf ("Serial port successfully configured.\n");
	return 0;
}

/*********************************************************************************/

void CloseCom(void)
{
	return;
}

/*********************************************************************************/
BOOL WriteSerPort(char *lpBuf, DWORD dwToWrite)
{
   OVERLAPPED osWrite = {0};
   DWORD dwWritten;
   BOOL fRes;

   // Create this writes OVERLAPPED structure hEvent.
   osWrite.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
   if (osWrite.hEvent == NULL)
      // Error creating overlapped event handle.
      return FALSE;

   // Issue write.
   if (!WriteFile(hSerCom, lpBuf, dwToWrite, &dwWritten, &osWrite)) {
      if (GetLastError() != ERROR_IO_PENDING) { 
         // WriteFile failed, but it isn't delayed. Report error and abort.
         fRes = FALSE;
      }
      else {
         // Write is pending.
         if (!GetOverlappedResult(hSerCom, &osWrite, &dwWritten, TRUE))
            fRes = FALSE;
         else
            // Write operation completed successfully.
            fRes = TRUE;
      }
   }
   else
      // WriteFile completed immediately.
      fRes = TRUE;

   CloseHandle(osWrite.hEvent);
   return fRes;
}

/**********************************************************************************/

BOOL ReadSerPort(char* lpBuf, DWORD dwToRead, DWORD *dwRead)
{
	DWORD dwRes;
	BOOL fWaitingOnRead = FALSE;
	OVERLAPPED osReader = {0};


	// Create the overlapped event. Must be closed before exiting
	// to avoid a handle leak.
	osReader.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);

	if (osReader.hEvent == NULL)
	{
		// Error creating overlapped event; abort.
		printf("\nError Create Reader Event");
		return 1;
	}

	while(1)
	{
		if (!fWaitingOnRead)
		{
			// Issue read operation.
			if (!ReadFile(hSerCom, lpBuf, dwToRead, dwRead, &osReader))
			{
				if (GetLastError() != ERROR_IO_PENDING)     // read not delayed?
					printf("\nError in GetLastError.");
				else
					fWaitingOnRead = TRUE;
			}
			else
			{    
				// read completed immediately
				CloseHandle(osReader.hEvent);
				return 0;
			}
		}


		if (fWaitingOnRead)
		{
			dwRes = WaitForSingleObject(osReader.hEvent, READ_TIMEOUT);
			switch(dwRes)
			{
				// Read completed.
				case WAIT_OBJECT_0:
					if (!GetOverlappedResult(hSerCom, &osReader, dwRead, FALSE))
					{
						printf("\nError in GetOverlappedResult");
						return 1;
					}
					else
					{
						// Read completed successfully.
						CloseHandle(osReader.hEvent);
						return 0;
						break;
					}

				case WAIT_TIMEOUT:
					// Operation isn't complete yet, so loop back around.
					break;                       

				default:
					// Error in the WaitForSingleObject; abort.
					printf("\nError in WaitForSingleObject.");
					CloseHandle(osReader.hEvent);
					return 1;
					break;
			}
		}
	}

}
/********************************************************/
