Old fashion design for PC-104
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.

168 lines
3.7 KiB

4 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. for( INT i = 0; i < bSubLength; i++ )
  108. bCheckSum += m_pBufferPtr[ i ];
  109. // if checksum ok
  110. if( bCheckSum == m_pBufferPtr[ i ] )
  111. {
  112. // store length
  113. (*pCount) = (INT)bLength;
  114. (*ppBuffer) = m_pBufferPtr;
  115. // at least one command received
  116. bResult = TRUE;
  117. // shift pointers
  118. m_pBufferPtr += bSubLength;
  119. m_nCount -= bSubLength;
  120. }
  121. }
  122. }
  123. // shift pointers
  124. m_pBufferPtr ++;
  125. m_nCount --;
  126. }
  127. return bResult;
  128. }
  129. BOOL CProtocolMigvanPTZ::Write( PBYTE pBuffer, INT nCount, DWORD dwTimeout, BOOL bHost )
  130. {
  131. bHost;
  132. // validate data length
  133. if( nCount > MAX_COMMAND_SIZE )
  134. return FALSE;
  135. // clear receiver
  136. m_nCount = 0;
  137. m_pBufferPtr = m_pBuffer;
  138. // write data
  139. DWORD dwDataSize = nCount;
  140. return (BOOL)(m_pConnection->Write( pBuffer, dwDataSize, dwTimeout ) == dwDataSize);
  141. }