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.

215 lines
4.9 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: ProtLGSm.cpp
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file of ProtLGSm.cpp
  13. //----------------------------------------------------------------------------
  14. #include "StdAfx.h"
  15. #ifdef __BORLANDC__
  16. #include "..\LgLib\Version.h"
  17. #include "..\LgLib\Comm\ProtLGSm.h"
  18. #else
  19. #include "..\Version.h"
  20. #include "ProtLGSm.h"
  21. #endif
  22. CProtocolLGS::CProtocolLGS()
  23. {
  24. m_pConnection = NULL;
  25. m_nCount = 0;
  26. m_pBufferPtr = m_pBuffer;
  27. }
  28. CProtocolLGS::~CProtocolLGS()
  29. {
  30. Destroy();
  31. }
  32. BOOL CProtocolLGS::Create( CConnection *pConnection )
  33. {
  34. Destroy();
  35. m_pConnection = pConnection;
  36. m_pBufferPtr = m_pBuffer;
  37. m_nCount = 0;
  38. printf( "Connection %p\n", pConnection )
  39. return TRUE;
  40. }
  41. void CProtocolLGS::Destroy()
  42. {
  43. m_pConnection = NULL;
  44. m_pBufferPtr = m_pBuffer;
  45. m_nCount = 0;
  46. }
  47. BOOL CProtocolLGS::Read( PBYTE *ppBuffer, PINT pCount, DWORD dwTimeout, BOOL bHost )
  48. {
  49. BOOL bResult = FALSE;
  50. #ifdef _MSC_VER
  51. DWORD dwStartTime = ::GetTickCount();
  52. #endif
  53. while( TRUE )
  54. {
  55. // assume without read repeating
  56. BOOL bRepeat = FALSE;
  57. // copy remaining part
  58. if( m_pBuffer != m_pBufferPtr )
  59. memcpy( m_pBuffer, m_pBufferPtr, m_nCount );
  60. // read data
  61. while( TRUE )
  62. {
  63. if( m_nCount < HEADER_SIZE )
  64. {
  65. // try to read command header
  66. m_nCount += m_pConnection->Read( m_pBuffer + m_nCount, HEADER_SIZE - m_nCount, dwTimeout );
  67. if( m_nCount < HEADER_SIZE )
  68. {
  69. break;
  70. }
  71. }
  72. // check header tag
  73. if( m_pBuffer[ HEADER_POS ] == HEADER_TAG )
  74. {
  75. // validate possibility of lenght
  76. BYTE bLength = ( m_pBuffer[ LENGTH_POS ] & LENGTH_MASK );
  77. if( bLength <= MAX_DATA_LENGTH )
  78. {
  79. // read remaining part
  80. DWORD nReadCount = bLength - (m_nCount - HEADER_SIZE) + 1;
  81. m_nCount += m_pConnection->Read( m_pBuffer + m_nCount, nReadCount, dwTimeout );
  82. break;
  83. }
  84. else
  85. memmove( m_pBuffer, m_pBuffer + 1, --m_nCount );
  86. }
  87. else
  88. memmove( m_pBuffer, m_pBuffer + 1, --m_nCount );
  89. #ifdef _MSC_VER
  90. if( ::GetTickCount() - dwStartTime > dwTimeout )
  91. break;
  92. #endif
  93. }
  94. // initialize pointer
  95. m_pBufferPtr = m_pBuffer;
  96. // parse input bytes
  97. while( m_nCount >= MIN_COMMAND_SIZE )
  98. {
  99. // check header tag
  100. if( m_pBufferPtr[ HEADER_POS ] == HEADER_TAG )
  101. {
  102. // validate possibility of lenght
  103. BYTE bLength = ( m_pBufferPtr[ LENGTH_POS ] & LENGTH_MASK );
  104. if( bLength <= MAX_DATA_LENGTH )
  105. {
  106. // check current lenght
  107. if( ( bLength + MIN_COMMAND_SIZE ) > m_nCount )
  108. return FALSE;
  109. // length without checksum
  110. INT bSubLength = bLength + HEADER_SIZE;
  111. // test checksum
  112. BYTE bCheckSum = 0;
  113. for( INT i = 0; i < bSubLength; i++ )
  114. bCheckSum += m_pBufferPtr[ i ];
  115. // if checksum ok
  116. if( bCheckSum == m_pBufferPtr[ i ] )
  117. {
  118. BOOL bFromHost = ( ( m_pBufferPtr[ LENGTH_POS ] & HOST_TAG ) == HOST_TAG );
  119. // if right direction
  120. if( (bFromHost && !bHost) || (bHost && !bFromHost) )
  121. {
  122. // store length
  123. *pCount = bLength;
  124. *ppBuffer = m_pBufferPtr + DATA_POS;
  125. // at least one command received
  126. bResult = TRUE;
  127. }
  128. else
  129. {
  130. // probably received echo - repeat reading
  131. bRepeat = TRUE;
  132. }
  133. // shift pointers
  134. m_pBufferPtr += bSubLength;
  135. m_nCount -= bSubLength;
  136. }
  137. }
  138. }
  139. // shift pointers
  140. m_pBufferPtr ++;
  141. m_nCount --;
  142. }
  143. // if not need to repeat reading
  144. if( !bRepeat )
  145. break;
  146. }
  147. return bResult;
  148. }
  149. BOOL CProtocolLGS::Write( PBYTE pBuffer, INT nCount, DWORD dwTimeout, BOOL bHost )
  150. {
  151. // validate data length
  152. if( nCount > MAX_DATA_LENGTH )
  153. return FALSE;
  154. // clear receiver
  155. m_nCount = 0;
  156. m_pBufferPtr = m_pBuffer;
  157. // make pointer to write buffer
  158. PBYTE pBufferPtr = m_pBuffer;
  159. #ifdef LGS_PROTOCOL_TRANSMIT_DELAY
  160. // add small delay during transmition
  161. for( INT j = 0; j < LGS_PROTOCOL_TRANSMIT_DELAY; j++ )
  162. *pBufferPtr++ = 0;
  163. #endif
  164. // write header tag
  165. BYTE bCheckSum = (*pBufferPtr++ = HEADER_TAG);
  166. // write data length
  167. bCheckSum += (*pBufferPtr++ = ( nCount | ( bHost ? HOST_TAG : 0 ) ) );
  168. // write data
  169. for( INT i = 0; i < nCount; i++ )
  170. bCheckSum += (*pBufferPtr++ = *pBuffer++);
  171. // write check sum
  172. *pBufferPtr = bCheckSum;
  173. // write data
  174. #ifdef LGS_PROTOCOL_TRANSMIT_DELAY
  175. DWORD dwBytesToWrite = nCount + MIN_COMMAND_SIZE + LGS_PROTOCOL_TRANSMIT_DELAY;
  176. #else
  177. DWORD dwBytesToWrite = nCount + MIN_COMMAND_SIZE;
  178. #endif
  179. return m_pConnection->Write( m_pBuffer, dwBytesToWrite, dwTimeout ) == dwBytesToWrite;
  180. }