For DOS Protected Mode version of sensor core software
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.

169 lines
3.6 KiB

3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. //----------------------------------------------------------------------------
  2. // Laser Guard Ltd.
  3. // Copyright � 2000-2002. All Rights Reserved.
  4. // Version 1.0 01/01/2000
  5. // Last modification: 08/10/2003
  6. // PROJECT:
  7. // SUBSYSTEM:
  8. // FILE: ProtPTZm.cpp
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file of ProtPTZm.cpp
  13. //----------------------------------------------------------------------------
  14. #include "stdafx.h"
  15. #ifdef __BORLANDC__
  16. #include "..\LgLib\Version.h"
  17. #include "..\LgLib\Comm\ProtPTZm.h"
  18. #else
  19. #include "..\Version.h"
  20. #include "ProtPTZm.h"
  21. #endif
  22. CProtocolMigvanPTZ::CProtocolMigvanPTZ()
  23. {
  24. m_pConnection = NULL;
  25. m_nCount = 0;
  26. m_pBufferPtr = m_pBuffer;
  27. m_bAddress = 0;
  28. m_bReadAnyAddress = FALSE;
  29. }
  30. CProtocolMigvanPTZ::~CProtocolMigvanPTZ()
  31. {
  32. Destroy();
  33. }
  34. BOOL CProtocolMigvanPTZ::Create( CConnection *pConnection )
  35. {
  36. Destroy();
  37. m_pConnection = pConnection;
  38. m_pBufferPtr = m_pBuffer;
  39. m_nCount = 0;
  40. return TRUE;
  41. }
  42. void CProtocolMigvanPTZ::Destroy()
  43. {
  44. m_pConnection = NULL;
  45. m_pBufferPtr = m_pBuffer;
  46. m_nCount = 0;
  47. }
  48. BOOL CProtocolMigvanPTZ::Read( PBYTE *ppBuffer, PINT pCount, DWORD dwTimeout, BOOL bHost )
  49. {
  50. bHost;
  51. BOOL bResult = FALSE;
  52. // copy remaining part
  53. if( ( m_pBuffer != m_pBufferPtr ) && m_nCount )
  54. memcpy( m_pBuffer, m_pBufferPtr, m_nCount );
  55. DWORD nReadCount = 0;
  56. // try to read command header
  57. if( m_nCount < HEADER_SIZE )
  58. {
  59. // read new data
  60. DWORD nCount = HEADER_SIZE - m_nCount;
  61. m_nCount += ( m_pConnection->Read( m_pBuffer + m_nCount, nCount, dwTimeout ) );
  62. }
  63. if( m_nCount >= HEADER_SIZE )
  64. {
  65. // check header tag
  66. if( m_pBuffer[ ADDRESS_POS ] == 0 ||
  67. m_pBuffer[ ADDRESS_POS ] == m_bAddress ||
  68. m_bReadAnyAddress )
  69. {
  70. // validate possibility of lenght
  71. BYTE bLength = m_pBuffer[ LENGTH_POS ];
  72. if( bLength <= MAX_DATA_LENGTH )
  73. {
  74. nReadCount = bLength - (m_nCount - HEADER_SIZE) + 1;
  75. }
  76. else
  77. nReadCount = MAX_COMMAND_SIZE - m_nCount;
  78. }
  79. else
  80. nReadCount = MAX_COMMAND_SIZE - m_nCount;
  81. }
  82. else
  83. nReadCount = MAX_COMMAND_SIZE - m_nCount;
  84. // read new data
  85. m_nCount += ( m_pConnection->Read( m_pBuffer + m_nCount, nReadCount, dwTimeout ) );
  86. // initialize pointer
  87. m_pBufferPtr = m_pBuffer;
  88. // parse input bytes
  89. while( m_nCount >= MIN_COMMAND_SIZE )
  90. {
  91. // check header tag
  92. if( m_bReadAnyAddress ||
  93. m_pBufferPtr[ ADDRESS_POS ] == m_bAddress ||
  94. m_pBufferPtr[ ADDRESS_POS ] == 0 )
  95. {
  96. // validate possibility of lenght
  97. BYTE bLength = m_pBufferPtr[ LENGTH_POS ];
  98. if( bLength <= MAX_DATA_LENGTH )
  99. {
  100. // check current lenght
  101. if( ( bLength + MIN_COMMAND_SIZE ) > m_nCount )
  102. return FALSE;
  103. // length without checksum
  104. BYTE bSubLength = bLength + HEADER_SIZE;
  105. // test checksum
  106. BYTE bCheckSum = 0;
  107. INT i;
  108. for( i = 0; i < bSubLength; i++ )
  109. bCheckSum += m_pBufferPtr[ i ];
  110. // if checksum ok
  111. if( bCheckSum == m_pBufferPtr[ i ] )
  112. {
  113. // store length
  114. (*pCount) = (INT)bLength;
  115. (*ppBuffer) = m_pBufferPtr;
  116. // at least one command received
  117. bResult = TRUE;
  118. // shift pointers
  119. m_pBufferPtr += bSubLength;
  120. m_nCount -= bSubLength;
  121. }
  122. }
  123. }
  124. // shift pointers
  125. m_pBufferPtr ++;
  126. m_nCount --;
  127. }
  128. return bResult;
  129. }
  130. BOOL CProtocolMigvanPTZ::Write( PBYTE pBuffer, INT nCount, DWORD dwTimeout, BOOL bHost )
  131. {
  132. bHost;
  133. // validate data length
  134. if( nCount > MAX_COMMAND_SIZE )
  135. return FALSE;
  136. // clear receiver
  137. m_nCount = 0;
  138. m_pBufferPtr = m_pBuffer;
  139. // write data
  140. DWORD dwDataSize = nCount;
  141. return (BOOL)(m_pConnection->Write( pBuffer, dwDataSize, dwTimeout ) == dwDataSize);
  142. }