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.

155 lines
3.8 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: LgNetSocketMD.cpp
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file of LgNetSocketMD.cpp
  13. //----------------------------------------------------------------------------
  14. #include "stdafx.h"
  15. #include "..\Version.h"
  16. #include "LgNetSocketMD.h"
  17. #include "LgNetDeviceLGS.h"
  18. #include "..\Comm\LGSm.h"
  19. CLgNetSocketMD::CLgNetSocketMD()
  20. {
  21. m_bSpyMode = FALSE;
  22. }
  23. CLgNetSocketMD::~CLgNetSocketMD()
  24. {
  25. Destroy();
  26. // unlink all linked devices
  27. CLgLinkP<CLgNetSocket,CLgNetSocketDevice>::Unlink();
  28. }
  29. BOOL CLgNetSocketMD::Create( LPCTSTR pCommPort, INT nBaudRate, BOOL bSpyMode, BOOL bDemo )
  30. {
  31. Destroy();
  32. if( !bDemo )
  33. {
  34. if( !m_Connection.Create( pCommPort, nBaudRate, 8, NOPARITY, ONESTOPBIT, CProtocolLGS::MAX_COMMAND_SIZE * 2, CProtocolLGS::MAX_COMMAND_SIZE * 2 ) )
  35. return FALSE;
  36. if( !m_Protocol.Create( &m_Connection ) )
  37. return FALSE;
  38. }
  39. m_bSpyMode = bSpyMode;
  40. if( !CLgNetSocket::Create( &m_Protocol ) )
  41. return FALSE;
  42. return TRUE;
  43. }
  44. void CLgNetSocketMD::Destroy()
  45. {
  46. CLgNetSocket::Destroy();
  47. m_Protocol.Destroy();
  48. m_Connection.Destroy();
  49. }
  50. BOOL CLgNetSocketMD::SetBaudrate( DWORD nBaudrate )
  51. {
  52. DCB dcb;
  53. if( !m_Connection.GetCommState( &dcb ) )
  54. return FALSE;
  55. if( dcb.BaudRate == nBaudrate )
  56. return TRUE;
  57. dcb.BaudRate = nBaudrate;
  58. return m_Connection.SetCommState( &dcb );
  59. }
  60. BOOL CLgNetSocketMD::OnTransaction()
  61. {
  62. BOOL bResult = FALSE;
  63. __try
  64. {
  65. bResult = OnTransaction_();
  66. }
  67. __except( EXCEPTION_EXECUTE_HANDLER )
  68. {
  69. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgNetSocketMD::OnTransaction"), _T("Exception 0x%08X"), _exception_code() );
  70. }
  71. return bResult;
  72. }
  73. BOOL CLgNetSocketMD::OnTransaction_()
  74. {
  75. // if not in spy mode
  76. if( !IsSpyMode() )
  77. return CLgNetSocket::OnTransaction();
  78. // process in spy mode
  79. PBYTE pReadBuffer = NULL;
  80. INT nReadCount = 0;
  81. // read host command
  82. if( !m_Protocol.Read( &pReadBuffer, &nReadCount, 100UL, FALSE ) )
  83. {
  84. LGLOG( LOG_LEVEL_REALTIME, LOG_TYPE_WARNING, this, "CLgNetSocketMD::OnTransaction", "read host command failed" );
  85. // accumulate error amount
  86. m_liFailureCount.QuadPart++;
  87. return FALSE;
  88. }
  89. // store address
  90. INT nAddress = (INT)*pReadBuffer;
  91. // found device for this operation
  92. CLgLocker Locker( &CLgLinkP<CLgNetSocket,CLgNetSocketDevice>::GetLock() );
  93. for( INT i = 0; i < CLgLinkP<CLgNetSocket,CLgNetSocketDevice>::GetCount(); i++ )
  94. {
  95. CLgNetDeviceMD *pDevice = static_cast<CLgNetDeviceMD*>( CLgLinkP<CLgNetSocket,CLgNetSocketDevice>::GetAt(i)->GetThis() );
  96. if( pDevice->GetAddress() == nAddress )
  97. {
  98. // perform command reading
  99. if( pDevice->DeviceProc( this, m_pProtocol ) )
  100. m_liSuccessCount.QuadPart++;
  101. else
  102. {
  103. LGLOG( LOG_LEVEL_REALTIME, LOG_TYPE_WARNING, this, "CLgNetSocketMD::OnTransaction", "read device command failed" );
  104. // accumulate error amount
  105. m_liFailureCount.QuadPart++;
  106. }
  107. return FALSE;
  108. }
  109. }
  110. // log information
  111. LGLOG( LOG_LEVEL_REALTIME, LOG_TYPE_WARNING, this, "CLgNetSocketMD::OnTransaction", "can't find device with address %d", nAddress );
  112. return FALSE;
  113. }
  114. BOOL CLgNetSocketMD::Link( CLgLink<CLgNetSocketDevice,CLgNetSocket>* pLink )
  115. {
  116. // update spy mode
  117. if( pLink )
  118. static_cast<CLgNetDeviceMD*>(pLink->GetThis())->SetSpyMode( IsSpyMode() );
  119. return CLgNetSocket::Link( pLink );
  120. }
  121. /*
  122. BOOL CLgNetSocketMD::AttachDevice( CLgNetSocketDevice* pDevice )
  123. {
  124. // update spy mode
  125. if( pDevice )
  126. ((CLgNetDeviceMD*)pDevice)->SetSpyMode( IsSpyMode() );
  127. return CLgNetSocket::AttachDevice( pDevice );
  128. }
  129. */