You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
783 lines
21 KiB
783 lines
21 KiB
//----------------------------------------------------------------------------
|
|
// Laser Guard Ltd.
|
|
// Copyright © 2000-2002. All Rights Reserved.
|
|
// Version 1.0 01/01/2000
|
|
// Last modification: 08/10/2003
|
|
// PROJECT:
|
|
// SUBSYSTEM:
|
|
// FILE: SocketBackup.cpp
|
|
// AUTHOR: Sergey Chizhevskiy
|
|
// OVERVIEW
|
|
// ~~~~~~~~
|
|
// Source file of SocketBackup.cpp
|
|
//----------------------------------------------------------------------------
|
|
#include "stdafx.h"
|
|
//#include "chicago.h"
|
|
#include "SocketBackup.h"
|
|
#include "SensorArray.h"
|
|
#include "SensorSPRU.h"
|
|
#include "..\..\LgLib\Version.h"
|
|
|
|
#ifdef _DEBUG
|
|
#undef THIS_FILE
|
|
static char THIS_FILE[]=__FILE__;
|
|
#define new DEBUG_NEW
|
|
#endif
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
// Construction/Destruction
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
CSocketBackup::CSocketBackup()
|
|
{
|
|
m_nType = SOCKET_BACKUP;
|
|
m_hCommPort = NULL;
|
|
m_hEventQuit = NULL;
|
|
m_hEventLoop = NULL;
|
|
m_hEventLoopStop = NULL;
|
|
m_hThread = NULL;
|
|
m_dwThreadWriteId = 0;
|
|
m_nMode = SERVER;
|
|
m_nCurrentSensor = 0;
|
|
|
|
LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CSocketBackup::CSocketBackup") );
|
|
}
|
|
|
|
CSocketBackup::~CSocketBackup()
|
|
{
|
|
if( m_hThread )
|
|
{
|
|
::SetEvent( m_hEventQuit );
|
|
::WaitForSingleObject( m_hThread, INFINITE );
|
|
::CloseHandle( m_hThread );
|
|
}
|
|
|
|
if( m_hEventQuit ) ::CloseHandle( m_hEventQuit );
|
|
if( m_hEventLoop ) ::CloseHandle( m_hEventLoop );
|
|
if( m_hEventLoopStop ) ::CloseHandle( m_hEventLoopStop );
|
|
|
|
if( m_hCommPort )
|
|
{
|
|
::CloseHandle( m_hCommPort );
|
|
}
|
|
|
|
LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CSocketBackup::~CSocketBackup") );
|
|
}
|
|
|
|
BOOL CSocketBackup::Create( CSensorArray *pSensorArray, LPCTSTR pCommName )
|
|
{
|
|
m_pSensorArray = pSensorArray;
|
|
|
|
m_strName = pCommName;
|
|
|
|
strcpy( m_pCommName, pCommName );
|
|
|
|
if( !CreateCommPort() )
|
|
{
|
|
m_pSensorArray->GetEventQueue()->PostEvent( LGS_EVENT_SOCKET_COMMUNICATION, FALSE, m_nType, m_nNumber, RGB( 0xFF, 0, 0 ), "Can't create backup line" );
|
|
return m_bInitiated = FALSE;
|
|
}
|
|
else
|
|
m_pSensorArray->GetEventQueue()->PostEvent( LGS_EVENT_SOCKET_COMMUNICATION, TRUE, m_nType, m_nNumber, RGB( 0, 0xFF, 0 ), "Backup line created" );
|
|
|
|
m_hEventQuit = ::CreateEvent( NULL, TRUE, FALSE, NULL );
|
|
m_hEventLoop = ::CreateEvent( NULL, TRUE, FALSE, NULL );
|
|
m_hEventLoopStop = ::CreateEvent( NULL, FALSE, FALSE, NULL );
|
|
|
|
m_hThread = ::CreateThread( NULL, 0UL, ThreadProcSocketBackup, this, 0UL, &m_dwThreadWriteId );
|
|
|
|
// ::SetThreadPriority( m_hThread, THREAD_PRIORITY_BELOW_NORMAL );
|
|
|
|
return m_bInitiated = TRUE;
|
|
}
|
|
|
|
BOOL CSocketBackup::CreateCommPort()
|
|
{
|
|
if( m_hCommPort )
|
|
::CloseHandle( m_hCommPort );
|
|
|
|
m_hCommPort = ::CreateFile( m_pCommName, GENERIC_READ | GENERIC_WRITE,
|
|
0UL, NULL, OPEN_EXISTING,
|
|
FILE_FLAG_OVERLAPPED|FILE_FLAG_NO_BUFFERING, NULL );
|
|
|
|
if( m_hCommPort == INVALID_HANDLE_VALUE )
|
|
return FALSE;
|
|
|
|
::ZeroMemory( &m_DCB, sizeof(m_DCB) );
|
|
m_DCB.DCBlength = sizeof( m_DCB );
|
|
::GetCommState( m_hCommPort, &m_DCB );
|
|
|
|
m_DCB.BaudRate = CBR_9600;
|
|
m_DCB.ByteSize = 8;
|
|
m_DCB.Parity = NOPARITY;
|
|
m_DCB.StopBits = ONESTOPBIT;
|
|
m_DCB.EvtChar = 0;
|
|
m_DCB.fParity = FALSE;
|
|
m_DCB.fOutxCtsFlow = FALSE;
|
|
m_DCB.fOutxDsrFlow = FALSE;
|
|
m_DCB.fDtrControl = DTR_CONTROL_DISABLE;
|
|
m_DCB.fDsrSensitivity = FALSE;
|
|
m_DCB.fOutX = FALSE;
|
|
m_DCB.fInX = FALSE;
|
|
m_DCB.fErrorChar = FALSE;
|
|
m_DCB.fNull = FALSE;
|
|
m_DCB.fRtsControl = RTS_CONTROL_DISABLE;
|
|
m_DCB.fAbortOnError = FALSE;
|
|
|
|
if( !::SetupComm( m_hCommPort, WRITE_MESSAGE_SIZE*5, WRITE_MESSAGE_SIZE*5 ) )
|
|
return FALSE;
|
|
|
|
if( !::SetCommState( m_hCommPort, &m_DCB ) )
|
|
return FALSE;
|
|
|
|
COMMTIMEOUTS ct;
|
|
ct.ReadIntervalTimeout = 0UL;
|
|
ct.ReadTotalTimeoutMultiplier = 0UL;
|
|
ct.ReadTotalTimeoutConstant = 0UL;
|
|
ct.WriteTotalTimeoutMultiplier = 0UL;
|
|
ct.WriteTotalTimeoutConstant = 0UL;
|
|
|
|
if( !::SetCommTimeouts( m_hCommPort, &ct ) )
|
|
return FALSE;
|
|
|
|
m_dwMaxReadTime[0] = ( READ_MESSAGE_SIZE * CBR_9600 ) / m_DCB.BaudRate;
|
|
if( !m_dwMaxReadTime[0] ) m_dwMaxReadTime[0] = 1;
|
|
m_dwMaxWaitTime[0] = m_dwMaxReadTime[0] + MAX_PROCESSING_TIME;
|
|
|
|
m_dwMaxReadTime[1] = ( WRITE_MESSAGE_SIZE * CBR_9600 ) / m_DCB.BaudRate;
|
|
if( !m_dwMaxReadTime[1] ) m_dwMaxReadTime[1] = 1;
|
|
m_dwMaxWaitTime[1] = m_dwMaxReadTime[1] + MAX_PROCESSING_TIME;
|
|
|
|
::PurgeComm( m_hCommPort, PURGE_TXCLEAR | PURGE_RXCLEAR | PURGE_TXABORT | PURGE_RXABORT );
|
|
|
|
return TRUE;
|
|
}
|
|
|
|
void CSocketBackup::CommandLoopStart()
|
|
{
|
|
::SetEvent( m_hEventLoop );
|
|
}
|
|
|
|
void CSocketBackup::CommandLoopStop()
|
|
{
|
|
if( ::WaitForSingleObject( m_hEventLoop, 0 ) == WAIT_OBJECT_0 )
|
|
::SetEvent( m_hEventLoopStop );
|
|
}
|
|
|
|
#define MAX_COMM_DELAY 20000000I64
|
|
|
|
DWORD CSocketBackup::ThreadProc()
|
|
{
|
|
DWORD dwErrors;
|
|
COMSTAT comstat;
|
|
|
|
DWORD dwWritten;
|
|
OVERLAPPED ovWrite;
|
|
ovWrite.hEvent = ::CreateEvent( NULL, TRUE, FALSE, NULL );
|
|
ovWrite.Internal = ovWrite.InternalHigh = ovWrite.Offset = ovWrite.OffsetHigh = 0UL;
|
|
|
|
DWORD dwEvtMask = 0;
|
|
OVERLAPPED ovWait;
|
|
ovWait.hEvent = ::CreateEvent( NULL, TRUE, FALSE, NULL );
|
|
ovWait.Internal = ovWait.InternalHigh = ovWait.Offset = ovWait.OffsetHigh = 0UL;
|
|
|
|
DWORD dwRead;
|
|
OVERLAPPED ovRead;
|
|
ovRead.hEvent = ::CreateEvent( NULL, TRUE, FALSE, NULL );
|
|
ovRead.Internal = ovRead.InternalHigh = ovRead.Offset = ovRead.OffsetHigh = 0UL;
|
|
|
|
HANDLE hEventsCommand[] = { m_hEventQuit, m_hEventLoopStop, m_hEventLoop };
|
|
HANDLE hEventsWrite[] = { m_hEventQuit, ovWrite.hEvent };
|
|
HANDLE hEventsWait[] = { m_hEventQuit, ovWait.hEvent };
|
|
HANDLE hEventsRead[] = { m_hEventQuit, ovRead.hEvent };
|
|
|
|
BYTE pBuffer[ WRITE_MESSAGE_SIZE ];
|
|
INT nBufferCount = 0;
|
|
|
|
DWORD dwTime = 0;
|
|
DWORD dwLoopCount = 0;
|
|
|
|
WORD wCriticalError;
|
|
|
|
CEventQueue *pEventQueue = m_pSensorArray->GetEventQueue();
|
|
|
|
BOOL bCatchStartBit = FALSE;
|
|
|
|
__int64 liLastCommunicate = 0I64;
|
|
|
|
enum { COMM_UNKNOWN, COMM_PREPARED, COMM_ESTABLISHED, COMM_ABSENT };
|
|
int nCommunicate = COMM_UNKNOWN;
|
|
|
|
CString str;
|
|
|
|
BOOL bReason = TRUE;
|
|
BOOL bSendReply = FALSE;
|
|
|
|
while( TRUE )
|
|
{
|
|
// Wait for command or exit
|
|
switch( ::WaitForMultipleObjects( sizeof(hEventsCommand)/sizeof(*hEventsCommand), hEventsCommand, FALSE, INFINITE ) )
|
|
{
|
|
case WAIT_OBJECT_0+0: goto END_THREAD;
|
|
case WAIT_OBJECT_0+1: ::ResetEvent( m_hEventLoop ); continue;
|
|
case WAIT_OBJECT_0+2: break;
|
|
}
|
|
|
|
// calculate performance
|
|
DWORD dwTempTime = ::GetTickCount();
|
|
if( dwTempTime > dwTime )
|
|
{
|
|
m_nPerformance = dwLoopCount;
|
|
dwLoopCount = 0UL;
|
|
dwTime = dwTempTime + 1000UL;
|
|
}
|
|
|
|
if( nCommunicate==COMM_UNKNOWN )
|
|
{
|
|
::GetSystemTimeAsFileTime( (LPFILETIME)&liLastCommunicate );
|
|
liLastCommunicate += MAX_COMM_DELAY;
|
|
nCommunicate = COMM_PREPARED;
|
|
}
|
|
|
|
CSensorSPRU *pSensor;
|
|
// first stage - write data
|
|
switch( m_nMode )
|
|
{
|
|
case SERVER:
|
|
|
|
m_nCurrentSensor = (m_nCurrentSensor + 1) % m_pSensorArray->GetSensorCount();
|
|
|
|
pSensor = (CSensorSPRU*)m_pSensorArray->GetSensor(m_nCurrentSensor);
|
|
|
|
pBuffer[0] = ((BYTE)m_nCurrentSensor) | 0x80; // add start bit
|
|
pBuffer[1] = ((BYTE)(m_nCurrentSensor>>7)) & 0x7F;
|
|
pBuffer[2] = pSensor->GetState() & 0x7F;
|
|
pBuffer[3] = pSensor->GetBitState() & 0x7F;
|
|
pBuffer[4] = pSensor->GetBitAlarm() & 0x7F;
|
|
pBuffer[5] = pSensor->GetBitEnable() & 0x7F;
|
|
|
|
if( ::ClearCommError( m_hCommPort, &dwErrors, &comstat ) )
|
|
{
|
|
if( dwErrors & CE_RXOVER ) ::PurgeComm( m_hCommPort, PURGE_RXCLEAR );
|
|
if( dwErrors & CE_BREAK ) ::ClearCommBreak( m_hCommPort );
|
|
if( dwErrors & ( CE_FRAME | CE_IOE | CE_OVERRUN | CE_RXPARITY | CE_TXFULL ) )
|
|
{
|
|
wCriticalError = 0;
|
|
goto RECREATE_COMMPORT;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
wCriticalError = 0;
|
|
goto RECREATE_COMMPORT;
|
|
}
|
|
|
|
::SetCommMask( m_hCommPort, EV_TXEMPTY );
|
|
|
|
// Asynchronous write data
|
|
if( !::WriteFile( m_hCommPort, pBuffer, WRITE_MESSAGE_SIZE, &dwWritten, &ovWrite ) )
|
|
{
|
|
DWORD dwError = ::GetLastError();
|
|
switch( dwError )
|
|
{
|
|
case ERROR_IO_PENDING:
|
|
switch( ::WaitForMultipleObjects( sizeof(hEventsWrite)/sizeof(*hEventsWrite), hEventsWrite, FALSE, 100 ) )
|
|
{
|
|
case WAIT_OBJECT_0 + 0: goto END_THREAD;
|
|
case WAIT_OBJECT_0 + 1:
|
|
case WAIT_TIMEOUT:
|
|
::GetOverlappedResult( m_hCommPort, &ovWrite, &dwWritten, FALSE );
|
|
break;
|
|
}
|
|
break;
|
|
default:
|
|
wCriticalError = 1;
|
|
goto RECREATE_COMMPORT;
|
|
}
|
|
|
|
if( !::WaitCommEvent( m_hCommPort, &dwEvtMask, &ovWait ) )
|
|
{
|
|
DWORD dwError = ::GetLastError();
|
|
switch( dwError )
|
|
{
|
|
case ERROR_IO_PENDING:
|
|
case ERROR_IO_INCOMPLETE:
|
|
switch( ::WaitForMultipleObjects( sizeof(hEventsWait)/sizeof(*hEventsWait), hEventsWait, FALSE, 1000 ) )
|
|
{
|
|
case WAIT_OBJECT_0 + 0: goto END_THREAD;
|
|
case WAIT_OBJECT_0 + 1:
|
|
case WAIT_TIMEOUT:
|
|
::GetOverlappedResult( m_hCommPort, &ovWait, &dwEvtMask, FALSE );
|
|
break;
|
|
}
|
|
break;
|
|
default:
|
|
wCriticalError = 2;
|
|
goto RECREATE_COMMPORT;
|
|
}
|
|
}
|
|
|
|
if( !( dwEvtMask & EV_TXEMPTY ) )
|
|
{
|
|
wCriticalError = 3;
|
|
goto RECREATE_COMMPORT;
|
|
}
|
|
}
|
|
|
|
::PurgeComm( m_hCommPort, PURGE_TXCLEAR | PURGE_TXABORT );
|
|
|
|
// If write was not successfull
|
|
if( dwWritten != WRITE_MESSAGE_SIZE )
|
|
{
|
|
wCriticalError = 4;
|
|
goto RECREATE_COMMPORT;
|
|
}
|
|
else
|
|
m_nCountSuccess++;
|
|
|
|
break;
|
|
|
|
case BACKUP:
|
|
|
|
if( bSendReply )
|
|
{
|
|
pBuffer[0] = 0xFF; // heart byte
|
|
|
|
if( ::ClearCommError( m_hCommPort, &dwErrors, &comstat ) )
|
|
{
|
|
if( dwErrors & CE_RXOVER ) ::PurgeComm( m_hCommPort, PURGE_RXCLEAR );
|
|
if( dwErrors & CE_BREAK ) ::ClearCommBreak( m_hCommPort );
|
|
if( dwErrors & ( CE_FRAME | CE_IOE | CE_OVERRUN | CE_RXPARITY | CE_TXFULL ) )
|
|
{
|
|
wCriticalError = 5;
|
|
goto RECREATE_COMMPORT;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
wCriticalError = 5;
|
|
goto RECREATE_COMMPORT;
|
|
}
|
|
|
|
::SetCommMask( m_hCommPort, EV_TXEMPTY );
|
|
|
|
// Asynchronous write data
|
|
if( !::WriteFile( m_hCommPort, pBuffer, READ_MESSAGE_SIZE, &dwWritten, &ovWrite ) )
|
|
{
|
|
DWORD dwError = ::GetLastError();
|
|
switch( dwError )
|
|
{
|
|
case ERROR_IO_PENDING:
|
|
switch( ::WaitForMultipleObjects( sizeof(hEventsWrite)/sizeof(*hEventsWrite), hEventsWrite, FALSE, 100 ) )
|
|
{
|
|
case WAIT_OBJECT_0 + 0: goto END_THREAD;
|
|
case WAIT_OBJECT_0 + 1:
|
|
case WAIT_TIMEOUT:
|
|
::GetOverlappedResult( m_hCommPort, &ovWrite, &dwWritten, FALSE );
|
|
break;
|
|
}
|
|
break;
|
|
default:
|
|
wCriticalError = 6;
|
|
goto RECREATE_COMMPORT;
|
|
}
|
|
|
|
if( !::WaitCommEvent( m_hCommPort, &dwEvtMask, &ovWait ) )
|
|
{
|
|
DWORD dwError = ::GetLastError();
|
|
switch( dwError )
|
|
{
|
|
case ERROR_IO_PENDING:
|
|
switch( ::WaitForMultipleObjects( sizeof(hEventsWait)/sizeof(*hEventsWait), hEventsWait, FALSE, 100 ) )
|
|
{
|
|
case WAIT_OBJECT_0 + 0: goto END_THREAD;
|
|
case WAIT_OBJECT_0 + 1:
|
|
::GetOverlappedResult( m_hCommPort, &ovWait, &dwEvtMask, FALSE );
|
|
break;
|
|
}
|
|
break;
|
|
default:
|
|
wCriticalError = 7;
|
|
goto RECREATE_COMMPORT;
|
|
}
|
|
}
|
|
|
|
if( !( dwEvtMask & EV_TXEMPTY ) )
|
|
{
|
|
wCriticalError = 8;
|
|
goto RECREATE_COMMPORT;
|
|
}
|
|
}
|
|
|
|
::PurgeComm( m_hCommPort, PURGE_TXCLEAR | PURGE_TXABORT );
|
|
|
|
// If write was not successfull
|
|
if( dwWritten != READ_MESSAGE_SIZE )
|
|
{
|
|
wCriticalError = 9;
|
|
goto RECREATE_COMMPORT;
|
|
}
|
|
|
|
bSendReply = FALSE;
|
|
}
|
|
|
|
break;
|
|
|
|
case MOBILE_MAP:
|
|
break;
|
|
}
|
|
|
|
BYTE byte;
|
|
|
|
bReason = TRUE;
|
|
|
|
// second stage - read data
|
|
switch( m_nMode )
|
|
{
|
|
case SERVER:
|
|
|
|
if( ::ClearCommError( m_hCommPort, &dwErrors, &comstat ) )
|
|
{
|
|
if( dwErrors & CE_RXOVER ) ::PurgeComm( m_hCommPort, PURGE_RXCLEAR );
|
|
if( dwErrors & CE_BREAK ) ::ClearCommBreak( m_hCommPort );
|
|
if( dwErrors & CE_TXFULL ) ::PurgeComm( m_hCommPort, PURGE_TXCLEAR );
|
|
if( dwErrors & ( CE_FRAME | CE_IOE | CE_OVERRUN | CE_RXPARITY ) )
|
|
{
|
|
wCriticalError = 11;
|
|
goto RECREATE_COMMPORT;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
wCriticalError = 11;
|
|
goto RECREATE_COMMPORT;
|
|
}
|
|
|
|
bReason = TRUE;
|
|
|
|
if( !comstat.cbInQue ) // input buffer empty
|
|
{
|
|
::SetCommMask( m_hCommPort, EV_RXCHAR );
|
|
|
|
if( !::WaitCommEvent( m_hCommPort, &dwEvtMask, &ovWait ) )
|
|
{
|
|
DWORD dwError = ::GetLastError();
|
|
switch( dwError )
|
|
{
|
|
case ERROR_IO_PENDING:
|
|
case ERROR_IO_INCOMPLETE:
|
|
|
|
switch( ::WaitForMultipleObjects( sizeof(hEventsWait)/sizeof(*hEventsWait), hEventsWait, FALSE, m_dwMaxWaitTime[0] ) )
|
|
{
|
|
case WAIT_OBJECT_0 + 0: goto END_THREAD;
|
|
case WAIT_OBJECT_0 + 1:
|
|
case WAIT_TIMEOUT:
|
|
::GetOverlappedResult( m_hCommPort, &ovWait, &dwEvtMask, FALSE );
|
|
break;
|
|
}
|
|
break;
|
|
default:
|
|
wCriticalError = 10;
|
|
goto RECREATE_COMMPORT;
|
|
}
|
|
}
|
|
|
|
if( !( dwEvtMask & EV_RXCHAR ) )
|
|
bReason = FALSE;
|
|
|
|
::SetCommMask( m_hCommPort, 0 );
|
|
}
|
|
|
|
if( bReason )
|
|
{
|
|
// Before reading it needs clear comm errors if they exist
|
|
if( ::ClearCommError( m_hCommPort, &dwErrors, &comstat ) )
|
|
{
|
|
if( dwErrors & CE_RXOVER ) ::PurgeComm( m_hCommPort, PURGE_RXCLEAR );
|
|
if( dwErrors & CE_BREAK ) ::ClearCommBreak( m_hCommPort );
|
|
if( dwErrors & ( CE_FRAME | CE_IOE | CE_OVERRUN | CE_RXPARITY | CE_TXFULL ) )
|
|
{
|
|
wCriticalError = 12;
|
|
goto RECREATE_COMMPORT;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
wCriticalError = 12;
|
|
goto RECREATE_COMMPORT;
|
|
}
|
|
|
|
// Asynchronous read data
|
|
if( !::ReadFile( m_hCommPort, &byte, READ_MESSAGE_SIZE, &dwRead, &ovRead ) )
|
|
{
|
|
DWORD dwError = ::GetLastError();
|
|
switch( dwError )
|
|
{
|
|
case ERROR_IO_PENDING:
|
|
switch( ::WaitForMultipleObjects( sizeof(hEventsRead)/sizeof(*hEventsRead), hEventsRead, FALSE, m_dwMaxReadTime[0] ) )
|
|
{
|
|
case WAIT_OBJECT_0 + 0: goto END_THREAD;
|
|
case WAIT_TIMEOUT:
|
|
case WAIT_OBJECT_0 + 1:
|
|
::GetOverlappedResult( m_hCommPort, &ovRead, &dwRead, FALSE );
|
|
break;
|
|
}
|
|
break;
|
|
default:
|
|
wCriticalError = 13;
|
|
goto RECREATE_COMMPORT;
|
|
}
|
|
}
|
|
|
|
::PurgeComm( m_hCommPort, PURGE_RXABORT );
|
|
}
|
|
else dwRead = 0UL;
|
|
|
|
if( dwRead == READ_MESSAGE_SIZE )
|
|
{
|
|
::GetSystemTimeAsFileTime( (LPFILETIME)&liLastCommunicate );
|
|
liLastCommunicate += MAX_COMM_DELAY;
|
|
if( nCommunicate != COMM_ESTABLISHED )
|
|
{
|
|
pEventQueue->PostEvent( LGS_EVENT_MM_COMMUNICATE, TRUE, 0, 0, RGB(0,0xFF,0), "established communication with backup" );
|
|
nCommunicate = COMM_ESTABLISHED;
|
|
}
|
|
m_nCountSuccess++;
|
|
}
|
|
else
|
|
{
|
|
m_nCountFailure++;
|
|
if( nCommunicate != COMM_ABSENT )
|
|
{
|
|
LARGE_INTEGER li;
|
|
::GetSystemTimeAsFileTime( (LPFILETIME)&li );
|
|
if( liLastCommunicate < li.QuadPart )
|
|
{
|
|
pEventQueue->PostEvent( LGS_EVENT_MM_COMMUNICATE, FALSE, 0, 0, RGB(0xFF,0,0), "no communication with backup" );
|
|
nCommunicate = COMM_ABSENT;
|
|
}
|
|
}
|
|
}
|
|
|
|
dwLoopCount++;
|
|
|
|
break;
|
|
|
|
case BACKUP:
|
|
case MOBILE_MAP:
|
|
|
|
bReason = TRUE;
|
|
|
|
if( ::ClearCommError( m_hCommPort, &dwErrors, &comstat ) )
|
|
{
|
|
if( dwErrors & CE_RXOVER ) ::PurgeComm( m_hCommPort, PURGE_RXCLEAR );
|
|
if( dwErrors & CE_BREAK ) ::ClearCommBreak( m_hCommPort );
|
|
if( dwErrors & ( CE_FRAME | CE_IOE | CE_OVERRUN | CE_RXPARITY | CE_TXFULL ) )
|
|
{
|
|
wCriticalError = 0;
|
|
goto RECREATE_COMMPORT;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
wCriticalError = 0;
|
|
goto RECREATE_COMMPORT;
|
|
}
|
|
|
|
if( !comstat.cbInQue ) // input buffer empty
|
|
{
|
|
::SetCommMask( m_hCommPort, EV_RXCHAR );
|
|
|
|
if( !::WaitCommEvent( m_hCommPort, &dwEvtMask, &ovWait ) )
|
|
{
|
|
DWORD dwError = ::GetLastError();
|
|
switch( dwError )
|
|
{
|
|
case ERROR_IO_PENDING:
|
|
case ERROR_IO_INCOMPLETE:
|
|
switch( ::WaitForMultipleObjects( sizeof(hEventsWait)/sizeof(*hEventsWait), hEventsWait, FALSE, m_dwMaxWaitTime[1] ) )
|
|
{
|
|
case WAIT_OBJECT_0 + 0: goto END_THREAD;
|
|
case WAIT_OBJECT_0 + 1:
|
|
case WAIT_TIMEOUT:
|
|
::GetOverlappedResult( m_hCommPort, &ovWait, &dwEvtMask, FALSE );
|
|
break;
|
|
}
|
|
break;
|
|
default:
|
|
wCriticalError = 14;
|
|
goto RECREATE_COMMPORT;
|
|
}
|
|
}
|
|
|
|
if( !( dwEvtMask & EV_RXCHAR ) )
|
|
bReason = FALSE;
|
|
|
|
::SetCommMask( m_hCommPort, 0 );
|
|
}
|
|
|
|
if( bReason )
|
|
{
|
|
// Before reading it needs clear comm errors if they exist
|
|
if( ::ClearCommError( m_hCommPort, &dwErrors, &comstat ) )
|
|
{
|
|
if( dwErrors & CE_RXOVER ) ::PurgeComm( m_hCommPort, PURGE_RXCLEAR );
|
|
if( dwErrors & CE_BREAK ) ::ClearCommBreak( m_hCommPort );
|
|
if( dwErrors & (CE_FRAME|CE_IOE|CE_RXPARITY|CE_OVERRUN) )
|
|
{
|
|
wCriticalError = 15;
|
|
goto RECREATE_COMMPORT;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
wCriticalError = 16;
|
|
goto RECREATE_COMMPORT;
|
|
}
|
|
|
|
// Asynchronous read data
|
|
if( !::ReadFile( m_hCommPort, &byte, READ_MESSAGE_SIZE, &dwRead, &ovRead ) )
|
|
{
|
|
DWORD dwError = ::GetLastError();
|
|
switch( dwError )
|
|
{
|
|
case ERROR_IO_PENDING:
|
|
switch( ::WaitForMultipleObjects( sizeof(hEventsRead)/sizeof(*hEventsRead), hEventsRead, FALSE, m_dwMaxReadTime[1] ) )
|
|
{
|
|
case WAIT_OBJECT_0 + 0: goto END_THREAD;
|
|
case WAIT_TIMEOUT:
|
|
case WAIT_OBJECT_0 + 1:
|
|
::GetOverlappedResult( m_hCommPort, &ovRead, &dwRead, FALSE );
|
|
break;
|
|
}
|
|
break;
|
|
default:
|
|
wCriticalError = 17;
|
|
goto RECREATE_COMMPORT;
|
|
}
|
|
}
|
|
::PurgeComm( m_hCommPort, PURGE_RXABORT );
|
|
}
|
|
else dwRead = 0UL;
|
|
|
|
if( dwRead == READ_MESSAGE_SIZE )
|
|
{
|
|
if( byte&0x80 ) // catch start byte
|
|
{
|
|
if( nBufferCount )
|
|
m_nCountFailure++;
|
|
nBufferCount = 0;
|
|
pBuffer[ nBufferCount++ ] = byte;
|
|
bCatchStartBit = TRUE;
|
|
}
|
|
else
|
|
{
|
|
pBuffer[ nBufferCount++ ] = byte;
|
|
if( nBufferCount == WRITE_MESSAGE_SIZE )
|
|
{
|
|
if( bCatchStartBit )
|
|
{
|
|
int nIndex = ( pBuffer[0] & 0x7F ) | ( ( (int)pBuffer[1] & 0x7F ) << 7 );
|
|
if( nIndex < m_pSensorArray->GetSensorCount() )
|
|
{
|
|
CSensorSPRU *pSensor = (CSensorSPRU*)m_pSensorArray->GetSensor( nIndex );
|
|
|
|
BYTE bStateOld = pSensor->GetState();
|
|
BYTE bBitStateOld = pSensor->GetBitState();
|
|
BYTE bBitAlarmOld = pSensor->GetBitAlarm();
|
|
BYTE bBitEnableOld = pSensor->GetBitEnable();
|
|
|
|
BYTE bState = pBuffer[2] & 0x7F;
|
|
BYTE bBitState = pBuffer[3] & 0x7F;
|
|
BYTE bBitAlarm = pBuffer[4] & 0x7F;
|
|
BYTE bBitEnable = pBuffer[5] & 0x7F;
|
|
|
|
if( bState != bStateOld || bBitState != bBitStateOld ||
|
|
bBitAlarm != bBitAlarmOld || bBitEnable != bBitEnableOld )
|
|
{
|
|
pSensor->SetState( pBuffer[2] & 0x7F );
|
|
pSensor->SetBitState( pBuffer[3] & 0x7F );
|
|
pSensor->SetBitAlarm( pBuffer[4] & 0x7F );
|
|
pSensor->SetBitEnable( pBuffer[5] & 0x7F );
|
|
pSensor->GetZone()->UpdateState();
|
|
pEventQueue->PostEvent( LGS_EVENT_MM_UPDATE, 0, nIndex, (DWORD)pSensor, 0, NULL );
|
|
}
|
|
}
|
|
dwLoopCount++;
|
|
m_nCountSuccess++;
|
|
::GetSystemTimeAsFileTime( (LPFILETIME)&liLastCommunicate );
|
|
liLastCommunicate += MAX_COMM_DELAY;
|
|
if( nCommunicate != COMM_ESTABLISHED )
|
|
{
|
|
pEventQueue->PostEvent( LGS_EVENT_MM_COMMUNICATE, TRUE, 0, 0, RGB(0,0xFF,0), "established communication with host" );
|
|
nCommunicate = COMM_ESTABLISHED;
|
|
}
|
|
}
|
|
else m_nCountFailure++;
|
|
bCatchStartBit = FALSE;
|
|
nBufferCount = 0;
|
|
bSendReply = TRUE;
|
|
}
|
|
}
|
|
}
|
|
|
|
if( nCommunicate != COMM_ABSENT )
|
|
{
|
|
LARGE_INTEGER li;
|
|
::GetSystemTimeAsFileTime( (LPFILETIME)&li );
|
|
if( liLastCommunicate < li.QuadPart )
|
|
{
|
|
for( int z = 0; z < m_pSensorArray->GetZoneCount(); z++ )
|
|
{
|
|
for( int s = 0; s < m_pSensorArray->GetZone( z )->GetCount(); s++ )
|
|
{
|
|
if( m_pSensorArray->GetZone( z )->GetSensor( s )->GetState() != CVisual::DISABLE )
|
|
m_pSensorArray->GetZone( z )->GetSensor( s )->SetState( CVisual::TIMEOUT );
|
|
}
|
|
m_pSensorArray->GetZone( z )->UpdateState();
|
|
}
|
|
pEventQueue->PostEvent( LGS_EVENT_MM_COMMUNICATE, FALSE, 0, 0, RGB(0xFF,0,0), "no communication with host" );
|
|
nCommunicate = COMM_ABSENT;
|
|
}
|
|
}
|
|
|
|
break;
|
|
}
|
|
|
|
continue;
|
|
|
|
RECREATE_COMMPORT:
|
|
if( !CreateCommPort() )
|
|
goto CRITICAL_END_THREAD;
|
|
}
|
|
|
|
CRITICAL_END_THREAD:
|
|
str.Format( "BACKUP error(%d)", wCriticalError );
|
|
pEventQueue->PostEvent( LGS_EVENT_ERROR, 0, m_nNumber, wCriticalError, RGB( 0xFF, 0, 0 ), (LPSTR)(LPCTSTR)str );
|
|
|
|
END_THREAD:
|
|
::PurgeComm( m_hCommPort, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR );
|
|
|
|
::CloseHandle( ovWrite.hEvent );
|
|
::CloseHandle( ovWait.hEvent );
|
|
::CloseHandle( ovRead.hEvent );
|
|
|
|
TRACE("<SocketBackup> thread: ");
|
|
|
|
return 0;
|
|
}
|
|
|
|
void CSocketBackup::SetServer()
|
|
{
|
|
m_nMode = SERVER;
|
|
}
|
|
|
|
void CSocketBackup::SetBackup()
|
|
{
|
|
m_nMode = BACKUP;
|
|
}
|
|
|
|
void CSocketBackup::SetMobileMap()
|
|
{
|
|
m_nMode = MOBILE_MAP;
|
|
}
|