//---------------------------------------------------------------------------- // Laser Guard Ltd. // Copyright © 2000-2002. All Rights Reserved. // Version 1.0 01/01/2000 // Last modification: 08/10/2003 // PROJECT: // SUBSYSTEM: // FILE: PTZPG3AD.cpp // AUTHOR: Sergey Chizhevskiy // OVERVIEW // ~~~~~~~~ // Source file of PTZPG3AD.cpp //---------------------------------------------------------------------------- #include "stdafx.h" #ifdef __BORLANDC__ #include "..\LgLib\Comm\PTZPG3AD.h" #else #include "PTZPG3AD.h" #endif CProtocolPTZPG3AD::CProtocolPTZPG3AD() { m_pConnection = NULL; m_nCount = 0; m_pBufferPtr = m_pBuffer; m_pAddress[0] = m_pAddress[1] = 0; m_bReadAnyAddress = FALSE; } CProtocolPTZPG3AD::~CProtocolPTZPG3AD() { Destroy(); } BOOL CProtocolPTZPG3AD::Create( CConnection *pConnection ) { Destroy(); m_pConnection = pConnection; m_pBufferPtr = m_pBuffer; m_nCount = 0; return TRUE; } void CProtocolPTZPG3AD::Destroy() { m_pConnection = NULL; m_pBufferPtr = m_pBuffer; m_nCount = 0; } void CProtocolPTZPG3AD::SetAddress( WORD bAddress ) { m_pAddress[0] = PTZ_PHILIPS_G3_AUTODOME::To14bit( bAddress, 0 ); m_pAddress[1] = PTZ_PHILIPS_G3_AUTODOME::To14bit( bAddress, 1 ); } BOOL CProtocolPTZPG3AD::Read( PBYTE *ppBuffer, PINT pCount, DWORD dwTimeout, BOOL bHost ) { bHost; BOOL bResult = FALSE; while( TRUE ) { // assume without read repeating BOOL bRepeat = 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[ LENGTH_POS ] & HEADER_TAG ) ) { // validate possibility of lenght WORD bLength = ( m_pBuffer[LENGTH_POS] & 0x7F ) + 1; if( bLength <= MAX_COMMAND_SIZE ) { if( m_bReadAnyAddress || ( m_pBuffer[ ADDRESS_POS_0 ] == m_pAddress[0] && m_pBuffer[ ADDRESS_POS_1 ] == m_pAddress[1] ) ) { nReadCount = bLength - m_nCount; } else nReadCount = MAX_COMMAND_SIZE - m_nCount; } 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_pBuffer[ LENGTH_POS ] & HEADER_TAG ) { // validate possibility of lenght WORD bLength = ( m_pBuffer[LENGTH_POS] & 0x7F ) + 1; if( bLength <= MAX_COMMAND_SIZE ) { // check current lenght if( bLength > m_nCount ) return FALSE; // check address if( m_bReadAnyAddress || ( m_pBuffer[ ADDRESS_POS_0 ] == m_pAddress[0] && m_pBuffer[ ADDRESS_POS_1 ] == m_pAddress[1] ) ) { // length without checksum BYTE bSubLength = bLength - 1; // test checksum BYTE bCheckSum = m_pBufferPtr[ 0 ]; for( INT i = 1; i < bSubLength; i++ ) bCheckSum += m_pBufferPtr[ i ]; bCheckSum &= 0x7F; // if checksum is ok if( bCheckSum == m_pBufferPtr[ i ] ) { // store length (*pCount) = (INT)bLength; (*ppBuffer) = m_pBufferPtr; // shift pointers m_pBufferPtr += bSubLength; m_nCount -= bSubLength; // at least one command received bResult = TRUE; } } } } // shift pointers m_pBufferPtr ++; m_nCount --; } // if not need to repeat reading if( !bRepeat ) break; } return bResult; } BOOL CProtocolPTZPG3AD::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); }