//---------------------------------------------------------------------------- // Laser Guard Ltd. // Copyright © 2000-2002. All Rights Reserved. // Version 1.0 01/01/2000 // Last modification: 08/10/2003 // PROJECT: // SUBSYSTEM: // FILE: ProtPTZm.cpp // AUTHOR: Sergey Chizhevskiy // OVERVIEW // ~~~~~~~~ // Source file of ProtPTZm.cpp //---------------------------------------------------------------------------- #include "stdafx.h" #ifdef __BORLANDC__ #include "..\LgLib\Version.h" #include "..\LgLib\Comm\ProtPTZm.h" #else #include "..\Version.h" #include "ProtPTZm.h" #endif CProtocolMigvanPTZ::CProtocolMigvanPTZ() { m_pConnection = NULL; m_nCount = 0; m_pBufferPtr = m_pBuffer; m_bAddress = 0; m_bReadAnyAddress = FALSE; } CProtocolMigvanPTZ::~CProtocolMigvanPTZ() { Destroy(); } BOOL CProtocolMigvanPTZ::Create( CConnection *pConnection ) { Destroy(); m_pConnection = pConnection; m_pBufferPtr = m_pBuffer; m_nCount = 0; return TRUE; } void CProtocolMigvanPTZ::Destroy() { m_pConnection = NULL; m_pBufferPtr = m_pBuffer; m_nCount = 0; } BOOL CProtocolMigvanPTZ::Read( PBYTE *ppBuffer, PINT pCount, DWORD dwTimeout, BOOL bHost ) { bHost; BOOL bResult = FALSE; // copy remaining part if( ( m_pBuffer != m_pBufferPtr ) && m_nCount ) memcpy( m_pBuffer, m_pBufferPtr, m_nCount ); DWORD nReadCount = 0; // try to read command header if( m_nCount < HEADER_SIZE ) { // read new data DWORD nCount = HEADER_SIZE - m_nCount; m_nCount += ( m_pConnection->Read( m_pBuffer + m_nCount, nCount, dwTimeout ) ); } if( m_nCount >= HEADER_SIZE ) { // check header tag if( m_pBuffer[ ADDRESS_POS ] == 0 || m_pBuffer[ ADDRESS_POS ] == m_bAddress || m_bReadAnyAddress ) { // validate possibility of lenght BYTE bLength = m_pBuffer[ LENGTH_POS ]; if( bLength <= MAX_DATA_LENGTH ) { nReadCount = bLength - (m_nCount - HEADER_SIZE) + 1; } else nReadCount = MAX_COMMAND_SIZE - m_nCount; } else nReadCount = MAX_COMMAND_SIZE - m_nCount; } else nReadCount = MAX_COMMAND_SIZE - m_nCount; // read new data m_nCount += ( m_pConnection->Read( m_pBuffer + m_nCount, nReadCount, dwTimeout ) ); // initialize pointer m_pBufferPtr = m_pBuffer; // parse input bytes while( m_nCount >= MIN_COMMAND_SIZE ) { // check header tag if( m_bReadAnyAddress || m_pBufferPtr[ ADDRESS_POS ] == m_bAddress || m_pBufferPtr[ ADDRESS_POS ] == 0 ) { // validate possibility of lenght BYTE bLength = m_pBufferPtr[ LENGTH_POS ]; if( bLength <= MAX_DATA_LENGTH ) { // check current lenght if( ( bLength + MIN_COMMAND_SIZE ) > m_nCount ) return FALSE; // length without checksum BYTE bSubLength = bLength + HEADER_SIZE; // test checksum BYTE bCheckSum = 0; for( INT i = 0; i < bSubLength; i++ ) bCheckSum += m_pBufferPtr[ i ]; // if checksum ok if( bCheckSum == m_pBufferPtr[ i ] ) { // store length (*pCount) = (INT)bLength; (*ppBuffer) = m_pBufferPtr; // at least one command received bResult = TRUE; // shift pointers m_pBufferPtr += bSubLength; m_nCount -= bSubLength; } } } // shift pointers m_pBufferPtr ++; m_nCount --; } return bResult; } BOOL CProtocolMigvanPTZ::Write( PBYTE pBuffer, INT nCount, DWORD dwTimeout, BOOL bHost ) { bHost; // validate data length if( nCount > MAX_COMMAND_SIZE ) return FALSE; // clear receiver m_nCount = 0; m_pBufferPtr = m_pBuffer; // write data DWORD dwDataSize = nCount; return (BOOL)(m_pConnection->Write( pBuffer, dwDataSize, dwTimeout ) == dwDataSize); }