//---------------------------------------------------------------------------- // Laser Guard Ltd. // Copyright © 2000-2002. All Rights Reserved. // Version 1.0 01/01/2000 // Last modification: 08/10/2003 // PROJECT: // SUBSYSTEM: // FILE: PTKS576.cpp // AUTHOR: Sergey Chizhevskiy // OVERVIEW // ~~~~~~~~ // Source file of PTKS576.CPP //---------------------------------------------------------------------------- #include "stdafx.h" #ifdef __BORLANDC__ #include "..\LgLib\Version.h" #include "..\LgLib\Comm\PTKS576.h" #else #include "..\Version.h" #include "PTKS576.h" #endif CProtocolPTZTKS576::CProtocolPTZTKS576() { m_pConnection = NULL; m_nCount = 0; m_pBufferPtr = m_pBuffer; m_bAddress = 0; m_bReadAnyAddress = FALSE; m_bReadOnlyHeader = TRUE; } CProtocolPTZTKS576::~CProtocolPTZTKS576() { Destroy(); } BOOL CProtocolPTZTKS576::Create( CConnection *pConnection ) { Destroy(); m_pConnection = pConnection; m_pBufferPtr = m_pBuffer; m_nCount = 0; return TRUE; } void CProtocolPTZTKS576::Destroy() { m_pConnection = NULL; m_pBufferPtr = m_pBuffer; m_nCount = 0; } void CProtocolPTZTKS576::SetAddress( BYTE bAddress ) { m_bAddress = bAddress; } BOOL CProtocolPTZTKS576::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_bReadOnlyHeader ) { if( m_nCount < 1 ) { // read new data DWORD nCount = 1 - m_nCount; m_nCount += ( m_pConnection->Read( m_pBuffer + m_nCount, nCount, dwTimeout ) ); } } else { if( m_nCount < 2 ) { // read new data DWORD nCount = 2 - m_nCount; m_nCount += ( m_pConnection->Read( m_pBuffer + m_nCount, nCount, dwTimeout ) ); } } if( m_nCount >= 1 ) { // check header tag if( m_pBuffer[0] == 0xA0 ) { if( m_bReadOnlyHeader ) { // everything ok } else { if( m_nCount >= 2 && ( m_pBuffer[1] & 0xF0 ) == 0x80 ) { INT nLength = ( (m_pBuffer[1] & 0x0F) + 1 ) * 6; if( nLength <= MAX_COMMAND_SIZE ) { // everything is ok nReadCount = ( nLength + 1 ) - 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 >= 1 ) { // check header tag if( m_pBuffer[0] == 0xA0 ) { if( m_bReadOnlyHeader ) { // store length (*pCount) = 1; (*ppBuffer) = m_pBufferPtr; // shift pointers m_pBufferPtr += 1; m_nCount -= 1; // at least one command received bResult = TRUE; } else { // current lenght is small if( m_nCount < 2 ) return FALSE; else if( ( m_pBuffer[1] & 0xF0 ) == 0x80 ) { // validate length INT nLength = ( (m_pBuffer[1] & 0x0F) + 1 ) * 6; if( nLength <= MAX_COMMAND_SIZE ) { // check current lenght if( nLength > m_nCount ) return FALSE; INT nFullLength = nLength + 1; // store length (*pCount) = (INT)nFullLength; (*ppBuffer) = m_pBufferPtr; // shift pointers m_pBufferPtr += (nFullLength - 1); m_nCount -= (nFullLength - 1); // 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 CProtocolPTZTKS576::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); }