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.
496 lines
13 KiB
496 lines
13 KiB
//----------------------------------------------------------------------------
|
|
// Laser Guard Ltd.
|
|
// Copyright © 2000-2002. All Rights Reserved.
|
|
// Version 1.0 01/01/2000
|
|
// Last modification: 08/10/2003
|
|
// PROJECT:
|
|
// SUBSYSTEM:
|
|
// FILE: SocketSM.cpp
|
|
// AUTHOR: Sergey Chizhevskiy
|
|
// OVERVIEW
|
|
// ~~~~~~~~
|
|
// Source file of SocketSM.cpp
|
|
//----------------------------------------------------------------------------
|
|
#include "stdafx.h"
|
|
//#include "chicago.h"
|
|
#include "SocketSM.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
|
|
|
|
CSocketSM::CSocketSM()
|
|
{
|
|
m_nType = SOCKET_SM;
|
|
m_hCommPort = NULL;
|
|
m_hEventQuit = NULL;
|
|
m_hEventLoop = NULL;
|
|
m_hEventLoopStop = NULL;
|
|
m_hThread = NULL;
|
|
// m_nCurrentZone = 0;
|
|
m_dwThreadWriteId = 0;
|
|
|
|
LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CSocketSM::CSocketSM") );
|
|
}
|
|
|
|
CSocketSM::~CSocketSM()
|
|
{
|
|
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("CSocketSM::~CSocketSM") );
|
|
}
|
|
|
|
BOOL CSocketSM::Create( CSensorArray *pSensorArray, LPCTSTR pCommName )
|
|
{
|
|
m_pSensorArray = pSensorArray;
|
|
strcpy( m_pCommName, pCommName );
|
|
m_strName = pCommName;
|
|
|
|
if( !CreateCommPort() )
|
|
{
|
|
m_pSensorArray->GetEventQueue()->PostEvent( LGS_EVENT_SOCKET_COMMUNICATION, FALSE, m_nType, m_nNumber, RGB( 0xFF, 0, 0 ), "Can't create sinoptic map line" );
|
|
return m_bInitiated = FALSE;
|
|
}
|
|
else
|
|
m_pSensorArray->GetEventQueue()->PostEvent( LGS_EVENT_SOCKET_COMMUNICATION, TRUE, m_nType, m_nNumber, RGB( 0, 0xFF, 0 ), "Sinoptic map 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, ThreadProcSocketSM, this, 0UL, &m_dwThreadWriteId );
|
|
|
|
// ::SetThreadPriority( m_hThread, THREAD_PRIORITY_BELOW_NORMAL );
|
|
|
|
return m_bInitiated = TRUE;
|
|
}
|
|
|
|
BOOL CSocketSM::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_1200;
|
|
m_DCB.ByteSize = 8;
|
|
m_DCB.Parity = EVENPARITY;
|
|
m_DCB.StopBits = ONESTOPBIT;
|
|
|
|
m_DCB.EvtChar = 0;
|
|
m_DCB.fParity = TRUE;
|
|
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, MESSAGE_SIZE*5, 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 = ( MESSAGE_SIZE * CBR_9600 ) / m_DCB.BaudRate;
|
|
if( !m_dwMaxReadTime )
|
|
m_dwMaxReadTime = 1;
|
|
m_dwMaxWaitTime = m_dwMaxReadTime + MAX_PROCESSING_TIME;
|
|
|
|
::PurgeComm( m_hCommPort, PURGE_TXCLEAR | PURGE_RXCLEAR | PURGE_TXABORT | PURGE_RXABORT );
|
|
|
|
return TRUE;
|
|
}
|
|
|
|
void CSocketSM::CommandLoopStart()
|
|
{
|
|
// m_nCurrentZone = 0;
|
|
::SetEvent( m_hEventLoop );
|
|
}
|
|
|
|
DWORD CSocketSM::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[ MESSAGE_SIZE ];
|
|
|
|
DWORD dwTime = 0;
|
|
DWORD dwLoopCount = 0;
|
|
|
|
WORD wCriticalError;
|
|
|
|
CEventQueue *pEventQueue = m_pSensorArray->GetEventQueue();
|
|
|
|
BYTE bits[8] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
|
|
|
|
BYTE pByteBuffer[90];
|
|
BOOL pBoolBuffer[90];
|
|
|
|
INT pBase[3] = { 1, 31, 61 };
|
|
|
|
LPBYTE pAlarm = pByteBuffer + pBase[0];
|
|
LPBYTE pSecure = pByteBuffer + pBase[1];
|
|
LPBYTE pAccess = pByteBuffer + pBase[2];
|
|
|
|
LPBOOL pAlarmBool = pBoolBuffer + pBase[0];
|
|
LPBOOL pSecureBool = pBoolBuffer + pBase[1];
|
|
LPBOOL pAccessBool = pBoolBuffer + pBase[2];
|
|
|
|
LPBYTE ppByteBuffer[3] = { pAlarm, pSecure, pAccess };
|
|
LPBOOL ppBoolBuffer[3] = { pAlarmBool, pSecureBool, pAccessBool };
|
|
|
|
::FillMemory( pByteBuffer, 90 * sizeof( BYTE ), 0x00 );
|
|
::FillMemory( pBoolBuffer, 90 * sizeof( BOOL ), 0x01 );
|
|
|
|
int nCountByte = m_pSensorArray->GetZoneCount() / 6 + ( ( m_pSensorArray->GetZoneCount() % 6 ) != 0 );
|
|
int nCurrentGroup = 0;
|
|
int nCurrentByte = 0;
|
|
|
|
CString str;
|
|
|
|
BOOL bReason;
|
|
|
|
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 = 0;
|
|
dwTime = dwTempTime + 1000;
|
|
}
|
|
else dwLoopCount++;
|
|
|
|
for( int i = 0; i < m_pSensorArray->GetZoneCount(); i++ )
|
|
{
|
|
int nByte = i / 6;
|
|
int maskOR = 1 << ( i % 6 );
|
|
int maskAND = ~maskOR;
|
|
switch( m_pSensorArray->GetZone( i )->GetState() )
|
|
{
|
|
case CZone::DISABLE:
|
|
if( !( pAccess[ nByte ] & maskOR ) ) pAccessBool[ nByte ] = TRUE;
|
|
if( pAlarm[ nByte ] & maskOR ) pAlarmBool[ nByte ] = TRUE;
|
|
if( pSecure[ nByte ] & maskOR ) pSecureBool[ nByte ] = TRUE;
|
|
pAccess[ nByte ] |= maskOR;
|
|
pAlarm[ nByte ] &= maskAND;
|
|
pSecure[ nByte ] &= maskAND;
|
|
break;
|
|
case CZone::UNKNOWN:
|
|
case CZone::NORMAL:
|
|
if( !( pSecure[ nByte ] & maskOR ) ) pSecureBool[ nByte ] = TRUE;
|
|
if( pAlarm[ nByte ] & maskOR ) pAlarmBool[ nByte ] = TRUE;
|
|
if( pAccess[ nByte ] & maskOR ) pAccessBool[ nByte ] = TRUE;
|
|
pSecure[ nByte ] |= maskOR;
|
|
pAlarm[ nByte ] &= maskAND;
|
|
pAccess[ nByte ] &= maskAND;
|
|
break;
|
|
case CZone::POSTALARM:
|
|
case CZone::TIMEOUT:
|
|
case CZone::ALARM:
|
|
if( !( pAlarm[ nByte ] & maskOR ) ) pAlarmBool[ nByte ] = TRUE;
|
|
if( pSecure[ nByte ] & maskOR ) pSecureBool[ nByte ] = TRUE;
|
|
if( pAccess[ nByte ] & maskOR ) pAccessBool[ nByte ] = TRUE;
|
|
pAlarm[ nByte ] |= maskOR;
|
|
pSecure[ nByte ] &= maskAND;
|
|
pAccess[ nByte ] &= maskAND;
|
|
break;
|
|
}
|
|
}
|
|
|
|
BOOL bFound = FALSE;
|
|
|
|
for( i = 0; i < 3 * nCountByte; i++ )
|
|
{
|
|
nCurrentByte++;
|
|
if( nCurrentByte==nCountByte )
|
|
{
|
|
nCurrentByte = 0;
|
|
nCurrentGroup = (nCurrentGroup+1)%3;
|
|
}
|
|
if( ppBoolBuffer[ nCurrentGroup ][ nCurrentByte ] )
|
|
{
|
|
bFound = TRUE;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if( !bFound )
|
|
{
|
|
::Sleep( 100 );
|
|
|
|
for( i = 0; i < 3 * nCountByte; i++ )
|
|
{
|
|
nCurrentByte++;
|
|
if( nCurrentByte==nCountByte )
|
|
{
|
|
nCurrentByte = 0;
|
|
nCurrentGroup = (nCurrentGroup+1)%3;
|
|
}
|
|
if( ppByteBuffer[ nCurrentGroup ][ nCurrentByte ] )
|
|
break;
|
|
}
|
|
}
|
|
|
|
pBuffer[0] = ( nCurrentByte + pBase[ nCurrentGroup ] ) | 0x80; // add start bit
|
|
pBuffer[1] = ppByteBuffer[ nCurrentGroup ][ nCurrentByte ] | 0x40; // add command bit
|
|
|
|
if( ::ClearCommError( m_hCommPort, &dwErrors, &comstat ) )
|
|
{
|
|
if( dwErrors & (CE_RXOVER|CE_BREAK) )
|
|
{
|
|
if( dwErrors & CE_RXOVER ) ::PurgeComm( m_hCommPort, PURGE_RXCLEAR );
|
|
if( dwErrors & CE_BREAK ) ::ClearCommBreak( m_hCommPort );
|
|
}
|
|
}
|
|
else
|
|
{
|
|
wCriticalError = 0;
|
|
goto RECREATE_COMMPORT;
|
|
}
|
|
|
|
::SetCommMask( m_hCommPort, EV_TXEMPTY );
|
|
|
|
// Asynchronous write data
|
|
if( !::WriteFile( m_hCommPort, pBuffer, 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:
|
|
switch( ::WaitForMultipleObjects( sizeof(hEventsWait)/sizeof(*hEventsWait), hEventsWait, FALSE, m_dwMaxReadTime ) )
|
|
{
|
|
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 != MESSAGE_SIZE )
|
|
{
|
|
wCriticalError = 4;
|
|
goto RECREATE_COMMPORT;
|
|
}
|
|
|
|
// Before reading it needs clear comm errors if they exist
|
|
if( ::ClearCommError( m_hCommPort, &dwErrors, &comstat ) )
|
|
{
|
|
if( dwErrors & (CE_RXOVER|CE_BREAK) )
|
|
{
|
|
if( dwErrors & CE_RXOVER ) ::PurgeComm( m_hCommPort, PURGE_RXCLEAR );
|
|
if( dwErrors & CE_BREAK ) ::ClearCommBreak( m_hCommPort );
|
|
}
|
|
}
|
|
else
|
|
{
|
|
wCriticalError = 5;
|
|
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:
|
|
switch( ::WaitForMultipleObjects( sizeof(hEventsWait)/sizeof(*hEventsWait), hEventsWait, FALSE, m_dwMaxWaitTime ) )
|
|
{
|
|
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 = 6;
|
|
goto RECREATE_COMMPORT;
|
|
}
|
|
}
|
|
|
|
if( !( dwEvtMask & EV_RXCHAR ) )
|
|
bReason = FALSE;
|
|
}
|
|
|
|
if( bReason )
|
|
{
|
|
// Asynchronous read data
|
|
if( !::ReadFile( m_hCommPort, pBuffer, MESSAGE_SIZE, &dwRead, &ovRead ) )
|
|
{
|
|
DWORD dwError = ::GetLastError();
|
|
switch( dwError )
|
|
{
|
|
case ERROR_IO_PENDING:
|
|
switch( ::WaitForMultipleObjects( sizeof(hEventsRead)/sizeof(*hEventsRead), hEventsRead, FALSE, m_dwMaxReadTime ) )
|
|
{
|
|
case WAIT_OBJECT_0 + 0: goto END_THREAD;
|
|
case WAIT_OBJECT_0 + 1:
|
|
case WAIT_TIMEOUT:
|
|
::GetOverlappedResult( m_hCommPort, &ovRead, &dwRead, FALSE );
|
|
break;
|
|
}
|
|
break;
|
|
default:
|
|
wCriticalError = 7;
|
|
goto RECREATE_COMMPORT;
|
|
}
|
|
}
|
|
|
|
::PurgeComm( m_hCommPort, PURGE_RXCLEAR | PURGE_RXABORT );
|
|
}
|
|
else
|
|
dwRead = 0UL;
|
|
|
|
if( dwRead == MESSAGE_SIZE )
|
|
{
|
|
m_nCountSuccess++;
|
|
ppBoolBuffer[ nCurrentGroup ][ nCurrentByte ] = FALSE;
|
|
}
|
|
else
|
|
{
|
|
m_nCountFailure++;
|
|
}
|
|
|
|
continue;
|
|
|
|
RECREATE_COMMPORT:
|
|
if( !CreateCommPort() )
|
|
goto CRITICAL_END_THREAD;
|
|
}
|
|
|
|
CRITICAL_END_THREAD:
|
|
str.Format( "SM 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("<SocketSM> thread: ");
|
|
return 0;
|
|
}
|
|
|
|
void CSocketSM::CommandLoopStop()
|
|
{
|
|
if( ::WaitForSingleObject( m_hEventLoop, 0 ) == WAIT_OBJECT_0 )
|
|
::SetEvent( m_hEventLoopStop );
|
|
}
|