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.

132 lines
3.0 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: ProtocolCCC.cpp
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file of ProtocolCCC.cpp
  13. //----------------------------------------------------------------------------
  14. // ProtocolCCC.cpp: implementation of the CProtocolCCC class.
  15. //
  16. //////////////////////////////////////////////////////////////////////
  17. #include "stdafx.h"
  18. #include "ProtocolCCC.h"
  19. //////////////////////////////////////////////////////////////////////
  20. // Construction/Destruction
  21. //////////////////////////////////////////////////////////////////////
  22. CProtocolCCC::CProtocolCCC()
  23. {
  24. m_pConnection = NULL;
  25. m_nCount = 0;
  26. m_pBufferPtr = m_pBuffer;
  27. }
  28. CProtocolCCC::~CProtocolCCC()
  29. {
  30. Destroy();
  31. }
  32. BOOL CProtocolCCC::Create( CConnection *pConnection )
  33. {
  34. Destroy();
  35. m_pConnection = pConnection;
  36. m_pBufferPtr = m_pBuffer;
  37. m_nCount = 0;
  38. return TRUE;
  39. }
  40. void CProtocolCCC::Destroy()
  41. {
  42. m_pConnection = NULL;
  43. m_pBufferPtr = m_pBuffer;
  44. m_nCount = 0;
  45. }
  46. BOOL CProtocolCCC::Read( PBYTE *ppBuffer, PINT pCount, DWORD dwTimeout, BOOL bHost )
  47. {
  48. BOOL bResult = FALSE;
  49. // copy remaining part
  50. if( m_pBuffer != m_pBufferPtr )
  51. memcpy( m_pBuffer, m_pBufferPtr, m_nCount );
  52. // read data
  53. while( TRUE )
  54. {
  55. if( m_nCount < COMMAND_SIZE )
  56. {
  57. // try to read address byte
  58. m_nCount += m_pConnection->Read( m_pBuffer + m_nCount, COMMAND_SIZE - m_nCount, dwTimeout );
  59. if( m_nCount < COMMAND_SIZE )
  60. {
  61. break;
  62. }
  63. }
  64. // check address tag
  65. if( ( m_pBuffer[ ADDRESS_POS ] & ADDRESS_TAG ) == ADDRESS_TAG &&
  66. ( m_pBuffer[ DATA_POS ] & ADDRESS_TAG ) != ADDRESS_TAG &&
  67. ( m_pBuffer[ DATA_POS ] & DATA_TAG ) == DATA_TAG )
  68. {
  69. break;
  70. }
  71. else
  72. memmove( m_pBuffer, m_pBuffer + 1, --m_nCount );
  73. }
  74. // initialize pointer
  75. m_pBufferPtr = m_pBuffer;
  76. // parse input bytes
  77. while( m_nCount >= COMMAND_SIZE )
  78. {
  79. // check address tag
  80. if( ( m_pBufferPtr[ ADDRESS_POS ] & ADDRESS_TAG ) == ADDRESS_TAG &&
  81. ( m_pBufferPtr[ DATA_POS ] & ADDRESS_TAG ) != ADDRESS_TAG &&
  82. ( m_pBufferPtr[ DATA_POS ] & DATA_TAG ) == DATA_TAG )
  83. {
  84. // store length
  85. *pCount = 2;
  86. *ppBuffer = m_pBufferPtr;
  87. // at least one command received
  88. bResult = TRUE;
  89. // shift pointers
  90. m_pBufferPtr += 2;
  91. m_nCount -= 2;
  92. }
  93. else
  94. {
  95. // shift pointers
  96. m_pBufferPtr ++;
  97. m_nCount --;
  98. }
  99. }
  100. return bResult;
  101. }
  102. BOOL CProtocolCCC::Write( PBYTE pBuffer, INT nCount, DWORD dwTimeout, BOOL bHost )
  103. {
  104. // validate data length
  105. if( nCount > COMMAND_SIZE )
  106. return FALSE;
  107. // clear receiver
  108. m_nCount = 0;
  109. m_pBufferPtr = m_pBuffer;
  110. // make pointer to write buffer
  111. return m_pConnection->Write( pBuffer, nCount, dwTimeout ) == (DWORD)nCount;
  112. }