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.

825 lines
22 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: SocketECS.cpp
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file of SocketECS.cpp
  13. //----------------------------------------------------------------------------
  14. // SocketECS.cpp: implementation of the CSocketECS class.
  15. //
  16. //////////////////////////////////////////////////////////////////////
  17. #include "stdafx.h"
  18. //#include "chicago.h"
  19. #include "SocketECS.h"
  20. #include "..\..\LgLib\Version.h"
  21. #ifdef _DEBUG
  22. #undef THIS_FILE
  23. static char THIS_FILE[]=__FILE__;
  24. #define new DEBUG_NEW
  25. #endif
  26. #include "SensorArray.h"
  27. #include "SensorSPRU.h"
  28. //////////////////////////////////////////////////////////////////////
  29. // Construction/Destruction
  30. //////////////////////////////////////////////////////////////////////
  31. CSocketECS::CSocketECS()
  32. {
  33. m_nType = SOCKET_ECS;
  34. m_hCommPort = NULL;
  35. m_hEventQuit = NULL;
  36. m_hEventLoop = NULL;
  37. m_hEventLoopStop = NULL;
  38. m_hEventCommand = NULL;
  39. m_hThread = NULL;
  40. m_dwThreadWriteId = 0;
  41. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CSocketECS::CSocketECS") );
  42. }
  43. CSocketECS::~CSocketECS()
  44. {
  45. if( m_hThread )
  46. {
  47. ::SetEvent( m_hEventQuit );
  48. ::WaitForSingleObject( m_hThread, INFINITE );
  49. ::CloseHandle( m_hThread );
  50. }
  51. if( m_hEventQuit ) ::CloseHandle( m_hEventQuit );
  52. if( m_hEventLoop ) ::CloseHandle( m_hEventLoop );
  53. if( m_hEventLoopStop ) ::CloseHandle( m_hEventLoopStop );
  54. if( m_hEventCommand ) ::CloseHandle( m_hEventCommand );
  55. if( m_hCommPort )
  56. {
  57. ::CloseHandle( m_hCommPort );
  58. }
  59. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CSocketECS::~CSocketECS") );
  60. }
  61. void CSocketECS::CommandLoopStart()
  62. {
  63. ::SetEvent( m_hEventLoop );
  64. }
  65. void CSocketECS::CommandLoopStop()
  66. {
  67. if( ::WaitForSingleObject( m_hEventLoop, 0 ) == WAIT_OBJECT_0 )
  68. ::SetEvent( m_hEventLoopStop );
  69. }
  70. BOOL CSocketECS::Create(CSensorArray *pSensorArray, LPCTSTR pCommName)
  71. {
  72. m_pSensorArray = pSensorArray;
  73. if( !CreateCommPort( pCommName ) )
  74. {
  75. m_pSensorArray->GetEventQueue()->PostEvent( LGS_EVENT_SOCKET_COMMUNICATION, FALSE, m_nType, m_nNumber, RGB( 0xFF, 0, 0 ), "Can't create ECS line" );
  76. return m_bInitiated = FALSE;
  77. }
  78. else
  79. m_pSensorArray->GetEventQueue()->PostEvent( LGS_EVENT_SOCKET_COMMUNICATION, TRUE, m_nType, m_nNumber, RGB( 0, 0xFF, 0 ), "ECS line created" );
  80. return m_bInitiated = TRUE;
  81. }
  82. BOOL CSocketECS::CreateCommPort(LPCTSTR pCommName)
  83. {
  84. m_strName = pCommName;
  85. m_hCommPort = ::CreateFile( pCommName, GENERIC_READ | GENERIC_WRITE,
  86. 0UL, NULL, OPEN_EXISTING,
  87. FILE_FLAG_OVERLAPPED|FILE_FLAG_NO_BUFFERING, NULL );
  88. if( m_hCommPort == INVALID_HANDLE_VALUE )
  89. return FALSE;
  90. DCB dcb;
  91. ::ZeroMemory(&dcb, sizeof(dcb));
  92. dcb.DCBlength = sizeof( dcb );
  93. ::GetCommState( m_hCommPort, &dcb );
  94. dcb.BaudRate = CBR_1200;
  95. dcb.ByteSize = 8;
  96. dcb.Parity = EVENPARITY;
  97. dcb.StopBits = ONESTOPBIT;
  98. dcb.EvtChar = 0;
  99. dcb.fParity = TRUE;
  100. dcb.fAbortOnError = FALSE;
  101. m_dwMaxReadTime = ( MESSAGE_SIZE * CBR_9600 ) / dcb.BaudRate;
  102. if( !m_dwMaxReadTime )
  103. m_dwMaxReadTime = 1;
  104. m_dwMaxWaitTime = m_dwMaxReadTime + MAX_PROCESSING_TIME;
  105. if( !::SetupComm( m_hCommPort, MESSAGE_SIZE*5, MESSAGE_SIZE*5 ) )
  106. return FALSE;
  107. if( !::SetCommState( m_hCommPort, &dcb ) )
  108. return FALSE;
  109. COMMTIMEOUTS ct;
  110. ct.ReadIntervalTimeout = 0UL;
  111. ct.ReadTotalTimeoutMultiplier = 0UL;
  112. ct.ReadTotalTimeoutConstant = 0UL;
  113. ct.WriteTotalTimeoutMultiplier = 0UL;
  114. ct.WriteTotalTimeoutConstant = 0UL;
  115. if( !::SetCommTimeouts( m_hCommPort, &ct ) )
  116. return FALSE;
  117. m_hEventQuit = ::CreateEvent( NULL, TRUE, FALSE, NULL );
  118. m_hEventLoop = ::CreateEvent( NULL, TRUE, FALSE, NULL );
  119. m_hEventLoopStop = ::CreateEvent( NULL, FALSE, FALSE, NULL );
  120. m_hEventCommand = ::CreateEvent( NULL, FALSE, FALSE, NULL );
  121. m_hThread = ::CreateThread( NULL, 0UL, ThreadProcSocketECS, this, 0UL, &m_dwThreadWriteId );
  122. // ::SetThreadPriority( m_hThread, THREAD_PRIORITY_BELOW_NORMAL );
  123. return TRUE;
  124. }
  125. BOOL CSocketECS::PostEvent( EVENT& event )
  126. {
  127. BOOL bResult = CSocket::PostEvent( event );
  128. ::SetEvent( m_hEventCommand );
  129. return bResult;
  130. }
  131. DWORD CSocketECS::ThreadProc()
  132. {
  133. DWORD dwErrors;
  134. COMSTAT comstat;
  135. DWORD dwWritten;
  136. OVERLAPPED ovWrite;
  137. ovWrite.hEvent = ::CreateEvent( NULL, TRUE, FALSE, NULL );
  138. ovWrite.Internal = ovWrite.InternalHigh = ovWrite.Offset = ovWrite.OffsetHigh = 0UL;
  139. DWORD dwEvtMask = 0;
  140. OVERLAPPED ovWait;
  141. ovWait.hEvent = ::CreateEvent( NULL, TRUE, FALSE, NULL );
  142. ovWait.Internal = ovWait.InternalHigh = ovWait.Offset = ovWait.OffsetHigh = 0UL;
  143. DWORD dwRead;
  144. OVERLAPPED ovRead;
  145. ovRead.hEvent = ::CreateEvent( NULL, TRUE, FALSE, NULL );
  146. ovRead.Internal = ovRead.InternalHigh = ovRead.Offset = ovRead.OffsetHigh = 0UL;
  147. HANDLE hEventsCommand[] = { m_hEventQuit, m_hEventLoopStop, m_hEventLoop };
  148. HANDLE hEventsWrite[] = { m_hEventQuit, ovWrite.hEvent };
  149. HANDLE hEventsWait[] = { m_hEventQuit, ovWait.hEvent };
  150. HANDLE hEventsRead[] = { m_hEventQuit, ovRead.hEvent };
  151. BYTE pBuffer[ MESSAGE_SIZE ];
  152. DWORD dwTime = 0;
  153. DWORD dwLoopCount = 0;
  154. WORD wCriticalError;
  155. CEventQueue *pEventQueue = m_pSensorArray->GetEventQueue();
  156. CString str;
  157. /*
  158. HANDLE hEventTimer = ::CreateWaitableTimer( NULL, FALSE, NULL );
  159. LARGE_INTEGER li;
  160. ::GetSystemTimeAsFileTime( (LPFILETIME)&li );
  161. ::SetWaitableTimer( hEventTimer, &li, 30000, NULL, NULL, FALSE );
  162. HANDLE hEvents[] = { m_hEventQuit, hEventTimer, m_hEventCommand };
  163. */
  164. HANDLE hEvents[] = { m_hEventQuit, m_hEventCommand };
  165. EVENT event;
  166. DWORD nBufferCount;
  167. BOOL bWaitResponse = FALSE;
  168. BOOL bTransmitAlarm = FALSE;
  169. BOOL bTransmitHeart = FALSE;
  170. BYTE pAlarmBuffer[ 6 ] = { 0xFE, 0, 0, 0, 0, 0xFF };
  171. BYTE pHeartBuffer[ 3 ] = { 0xFE, 0xEE, 0xFF };
  172. LARGE_INTEGER nextHeartTime, currentTime;
  173. ::GetSystemTimeAsFileTime( (LPFILETIME)&nextHeartTime );
  174. while( TRUE )
  175. {
  176. // Wait for command or exit
  177. switch( ::WaitForMultipleObjects( sizeof(hEventsCommand)/sizeof(*hEventsCommand), hEventsCommand, FALSE, INFINITE ) )
  178. {
  179. case WAIT_OBJECT_0+0: goto END_THREAD;
  180. case WAIT_OBJECT_0+1: ::ResetEvent( m_hEventLoop ); continue;
  181. case WAIT_OBJECT_0+2: break;
  182. }
  183. ::GetSystemTimeAsFileTime( (LPFILETIME)&currentTime );
  184. DWORD dwTime = 0;
  185. __int64 diff = nextHeartTime.QuadPart - currentTime.QuadPart;
  186. if( diff > 0I64 )
  187. dwTime = (DWORD)( diff / 10000I64 );
  188. switch( ::WaitForMultipleObjects( 2, hEvents, FALSE, dwTime ) )
  189. {
  190. case WAIT_OBJECT_0+0: goto END_THREAD;
  191. case WAIT_OBJECT_0+1:
  192. ::GetSystemTimeAsFileTime( (LPFILETIME)&currentTime );
  193. if( nextHeartTime.QuadPart > currentTime.QuadPart )
  194. {
  195. if( !bWaitResponse )
  196. {
  197. if( m_EventQueue.GetEvent( event ) )
  198. {
  199. CSensorSPRU *pSensor = (CSensorSPRU*)m_pSensorArray->GetSensor( event.data16 );
  200. pAlarmBuffer[1] = pSensor->GetSocket();
  201. pAlarmBuffer[2] = pSensor->GetAddress();
  202. switch( event.id )
  203. {
  204. case LGS_EVENT_ALARM_SHOW: pAlarmBuffer[3] = 0x02; break;
  205. case LGS_EVENT_ALARM_HIDE: pAlarmBuffer[3] = 0x00; break;
  206. case LGS_EVENT_TIMEOUT_APPEAR: pAlarmBuffer[3] = 0x01; break;
  207. case LGS_EVENT_TIMEOUT_DISAPPEAR: pAlarmBuffer[3] = 0x03; break;
  208. }
  209. pAlarmBuffer[4] = (BYTE)event.data32;
  210. bWaitResponse = TRUE;
  211. bTransmitAlarm = TRUE;
  212. ::SetEvent( m_hEventCommand );
  213. }
  214. }
  215. break;
  216. }
  217. case WAIT_TIMEOUT:
  218. ::GetSystemTimeAsFileTime( (LPFILETIME)&currentTime );
  219. nextHeartTime.QuadPart = currentTime.QuadPart + 300000000I64;
  220. bTransmitHeart = TRUE;
  221. break;
  222. }
  223. if( bTransmitAlarm )
  224. {
  225. nBufferCount = 6;
  226. ::CopyMemory( pBuffer, pAlarmBuffer, nBufferCount );
  227. }
  228. else
  229. if( bTransmitHeart )
  230. {
  231. nBufferCount = 3;
  232. ::CopyMemory( pBuffer, pHeartBuffer, nBufferCount );
  233. }
  234. else
  235. nBufferCount = 0;
  236. // calculate performance
  237. DWORD dwTempTime = ::GetTickCount();
  238. if( dwTempTime > dwTime )
  239. {
  240. m_nPerformance = dwLoopCount;
  241. dwLoopCount = 0;
  242. dwTime = dwTempTime + 1000;
  243. }
  244. else dwLoopCount++;
  245. if( nBufferCount )
  246. {
  247. // Before writting it needs clear comm errors if they exist
  248. if( ::ClearCommError( m_hCommPort, &dwErrors, &comstat ) )
  249. {
  250. if( dwErrors & (CE_RXOVER|CE_BREAK) )
  251. {
  252. if( dwErrors & CE_RXOVER ) ::PurgeComm( m_hCommPort, PURGE_RXCLEAR );
  253. if( dwErrors & CE_BREAK ) ::ClearCommBreak( m_hCommPort );
  254. }
  255. }
  256. else
  257. {
  258. wCriticalError = 0;
  259. goto CRITICAL_END_THREAD;
  260. }
  261. ::SetCommMask( m_hCommPort, EV_TXEMPTY );
  262. // Asynchronous write data
  263. if( !::WriteFile( m_hCommPort, pBuffer, nBufferCount, &dwWritten, &ovWrite ) )
  264. {
  265. DWORD dwError = ::GetLastError();
  266. switch( dwError )
  267. {
  268. case ERROR_IO_PENDING:
  269. switch( ::WaitForMultipleObjects( sizeof(hEventsWrite)/sizeof(*hEventsWrite), hEventsWrite, FALSE, 100 ) )
  270. {
  271. case WAIT_OBJECT_0 + 0: goto END_THREAD;
  272. case WAIT_OBJECT_0 + 1:
  273. ::GetOverlappedResult( m_hCommPort, &ovWrite, &dwWritten, FALSE );
  274. break;
  275. }
  276. break;
  277. default:
  278. wCriticalError = 1;
  279. goto CRITICAL_END_THREAD;
  280. }
  281. if( !::WaitCommEvent( m_hCommPort, &dwEvtMask, &ovWait ) )
  282. {
  283. DWORD dwError = ::GetLastError();
  284. switch( dwError )
  285. {
  286. case ERROR_IO_PENDING:
  287. switch( ::WaitForMultipleObjects( sizeof(hEventsWait)/sizeof(*hEventsWait), hEventsWait, FALSE, m_dwMaxReadTime ) )
  288. {
  289. case WAIT_OBJECT_0 + 0: goto END_THREAD;
  290. case WAIT_OBJECT_0 + 1:
  291. ::GetOverlappedResult( m_hCommPort, &ovWait, &dwEvtMask, FALSE );
  292. break;
  293. }
  294. break;
  295. default:
  296. wCriticalError = 2;
  297. goto CRITICAL_END_THREAD;
  298. }
  299. }
  300. if( !( dwEvtMask & EV_TXEMPTY ) )
  301. {
  302. wCriticalError = 3;
  303. goto CRITICAL_END_THREAD;
  304. }
  305. }
  306. ::PurgeComm( m_hCommPort, PURGE_TXCLEAR | PURGE_TXABORT );
  307. // If write was not successfull
  308. if( dwWritten != nBufferCount )
  309. {
  310. wCriticalError = 4;
  311. goto CRITICAL_END_THREAD;
  312. }
  313. else
  314. {
  315. bTransmitAlarm = FALSE;
  316. bTransmitHeart = FALSE;
  317. m_nCountSuccess++;
  318. }
  319. }
  320. if( bWaitResponse )
  321. {
  322. // Before reading it needs clear comm errors if they exist
  323. if( ::ClearCommError( m_hCommPort, &dwErrors, &comstat ) )
  324. {
  325. if( dwErrors & (CE_RXOVER|CE_BREAK) )
  326. {
  327. if( dwErrors & CE_RXOVER ) ::PurgeComm( m_hCommPort, PURGE_RXCLEAR );
  328. if( dwErrors & CE_BREAK ) ::ClearCommBreak( m_hCommPort );
  329. }
  330. }
  331. else
  332. {
  333. wCriticalError = 5;
  334. goto CRITICAL_END_THREAD;
  335. }
  336. BOOL bReason = TRUE;
  337. if( !comstat.cbInQue ) // input buffer empty
  338. {
  339. ::SetCommMask( m_hCommPort, EV_RXCHAR );
  340. if( !::WaitCommEvent( m_hCommPort, &dwEvtMask, &ovWait ) )
  341. {
  342. DWORD dwError = ::GetLastError();
  343. switch( dwError )
  344. {
  345. case ERROR_IO_PENDING:
  346. switch( ::WaitForMultipleObjects( sizeof(hEventsWait)/sizeof(*hEventsWait), hEventsWait, FALSE, m_dwMaxWaitTime ) )
  347. {
  348. case WAIT_OBJECT_0 + 0: goto END_THREAD;
  349. case WAIT_OBJECT_0 + 1:
  350. ::GetOverlappedResult( m_hCommPort, &ovWait, &dwEvtMask, FALSE );
  351. break;
  352. }
  353. break;
  354. default:
  355. wCriticalError = 6;
  356. goto CRITICAL_END_THREAD;
  357. }
  358. }
  359. if( !( dwEvtMask & EV_RXCHAR ) )
  360. bReason = FALSE;
  361. }
  362. if( bReason )
  363. {
  364. // Asynchronous read data
  365. if( !::ReadFile( m_hCommPort, pBuffer, 1, &dwRead, &ovRead ) )
  366. {
  367. DWORD dwError = ::GetLastError();
  368. switch( dwError )
  369. {
  370. case ERROR_IO_PENDING:
  371. switch( ::WaitForMultipleObjects( sizeof(hEventsRead)/sizeof(*hEventsRead), hEventsRead, FALSE, m_dwMaxReadTime ) )
  372. {
  373. case WAIT_OBJECT_0 + 0: goto END_THREAD;
  374. case WAIT_OBJECT_0 + 1:
  375. ::GetOverlappedResult( m_hCommPort, &ovRead, &dwRead, FALSE );
  376. break;
  377. case WAIT_TIMEOUT:
  378. break;
  379. }
  380. break;
  381. default:
  382. wCriticalError = 7;
  383. goto CRITICAL_END_THREAD;
  384. }
  385. }
  386. ::PurgeComm( m_hCommPort, PURGE_RXCLEAR | PURGE_RXABORT );
  387. }
  388. else
  389. dwRead = 0UL;
  390. if( dwRead == 1 ) // reseived response from ECS
  391. {
  392. switch( *pBuffer )
  393. {
  394. case 0x0F:
  395. m_nCountSuccess++;
  396. bWaitResponse = FALSE;
  397. break;
  398. case 0xFF:
  399. m_nCountSuccess++; // need retransmit
  400. bTransmitAlarm = TRUE;
  401. break;
  402. default:
  403. m_nCountFailure++; // undefined situation
  404. bWaitResponse = FALSE;
  405. break;
  406. }
  407. }
  408. ::SetEvent( m_hEventCommand );
  409. }
  410. }
  411. CRITICAL_END_THREAD:
  412. str.Format( "ECS error(%d)", wCriticalError );
  413. pEventQueue->PostEvent( LGS_EVENT_ERROR, 0, m_nNumber, wCriticalError, RGB( 0xFF, 0, 0 ), (LPSTR)(LPCTSTR)str );
  414. END_THREAD:
  415. ::PurgeComm( m_hCommPort, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR );
  416. /*
  417. ::CancelWaitableTimer( hEventTimer );
  418. ::CloseHandle( hEventTimer );
  419. */
  420. ::CloseHandle( ovWrite.hEvent );
  421. ::CloseHandle( ovWait.hEvent );
  422. ::CloseHandle( ovRead.hEvent );
  423. TRACE("<SocketECS> thread: ");
  424. return 0;
  425. }
  426. /*
  427. DWORD CSocketECS::ThreadProc()
  428. {
  429. DWORD dwErrors;
  430. COMSTAT comstat;
  431. DWORD dwWritten;
  432. OVERLAPPED ovWrite;
  433. ovWrite.hEvent = ::CreateEvent( NULL, TRUE, FALSE, NULL );
  434. ovWrite.Internal = ovWrite.InternalHigh = ovWrite.Offset = ovWrite.OffsetHigh = 0UL;
  435. DWORD dwEvtMask = 0;
  436. OVERLAPPED ovWait;
  437. ovWait.hEvent = ::CreateEvent( NULL, TRUE, FALSE, NULL );
  438. ovWait.Internal = ovWait.InternalHigh = ovWait.Offset = ovWait.OffsetHigh = 0UL;
  439. DWORD dwRead;
  440. OVERLAPPED ovRead;
  441. ovRead.hEvent = ::CreateEvent( NULL, TRUE, FALSE, NULL );
  442. ovRead.Internal = ovRead.InternalHigh = ovRead.Offset = ovRead.OffsetHigh = 0UL;
  443. HANDLE hEventsCommand[] = { m_hEventQuit, m_hEventLoop };
  444. HANDLE hEventsWrite[] = { m_hEventQuit, ovWrite.hEvent };
  445. HANDLE hEventsWait[] = { m_hEventQuit, ovWait.hEvent };
  446. HANDLE hEventsRead[] = { m_hEventQuit, ovRead.hEvent };
  447. BYTE pBuffer[ MESSAGE_SIZE ];
  448. DWORD dwTime = 0;
  449. DWORD dwLoopCount = 0;
  450. WORD wCriticalError;
  451. CEventQueue *pEventQueue = m_pSensorArray->GetEventQueue();
  452. CString str;
  453. HANDLE hEventTimer = ::CreateWaitableTimer( NULL, FALSE, NULL );
  454. LARGE_INTEGER li;
  455. ::GetSystemTimeAsFileTime( (LPFILETIME)&li );
  456. ::SetWaitableTimer( hEventTimer, &li, 30000, NULL, NULL, FALSE );
  457. HANDLE hEvents[] = { m_hEventQuit, hEventTimer, m_hEventCommand };
  458. EVENT event;
  459. DWORD nBufferCount;
  460. BOOL bWaitResponse = FALSE;
  461. BOOL bTransmitAlarm = FALSE;
  462. BOOL bTransmitHeart = FALSE;
  463. BYTE pAlarmBuffer[ 6 ] = { 0xFE, 0, 0, 0, 0, 0xFF };
  464. BYTE pHeartBuffer[ 3 ] = { 0xFE, 0xEE, 0xFF };
  465. while( TRUE )
  466. {
  467. // Wait for command or exit
  468. switch( ::WaitForMultipleObjects( sizeof(hEventsCommand)/sizeof(*hEventsCommand), hEventsCommand, FALSE, INFINITE ) )
  469. {
  470. case WAIT_OBJECT_0+0: goto END_THREAD;
  471. case WAIT_OBJECT_0+1: break;
  472. }
  473. switch( ::WaitForMultipleObjects( 3, hEvents, FALSE, INFINITE ) )
  474. {
  475. case WAIT_OBJECT_0+0: goto END_THREAD;
  476. case WAIT_OBJECT_0+1:
  477. bTransmitHeart = TRUE;
  478. break;
  479. case WAIT_OBJECT_0+2:
  480. if( !bWaitResponse )
  481. {
  482. if( m_ECSQueue.GetEvent( event ) )
  483. {
  484. CSensorDTR *pSensor = (CSensorDTR*)m_pSensorArray->GetSensor( event.data16 );
  485. pAlarmBuffer[1] = pSensor->GetSocket();
  486. pAlarmBuffer[2] = pSensor->GetAddress();
  487. switch( event.event )
  488. {
  489. case LGS_EVENT_ALARM_SHOW: pAlarmBuffer[3] = 0x02; break;
  490. case LGS_EVENT_ALARM_HIDE: pAlarmBuffer[3] = 0x00; break;
  491. case LGS_EVENT_TIMEOUT_APPEAR: pAlarmBuffer[3] = 0x01; break;
  492. case LGS_EVENT_TIMEOUT_DISAPPEAR: pAlarmBuffer[3] = 0x03; break;
  493. }
  494. pAlarmBuffer[4] = (BYTE)event.data32;
  495. bWaitResponse = TRUE;
  496. bTransmitAlarm = TRUE;
  497. ::SetEvent( m_hEventCommand );
  498. }
  499. }
  500. break;
  501. }
  502. if( bTransmitAlarm )
  503. {
  504. nBufferCount = 6;
  505. ::CopyMemory( pBuffer, pAlarmBuffer, nBufferCount );
  506. }
  507. else
  508. if( bTransmitHeart )
  509. {
  510. nBufferCount = 3;
  511. ::CopyMemory( pBuffer, pHeartBuffer, nBufferCount );
  512. }
  513. else
  514. nBufferCount = 0;
  515. // calculate performance
  516. DWORD dwTempTime = ::GetTickCount();
  517. if( dwTempTime > dwTime )
  518. {
  519. m_nPerformance = dwLoopCount;
  520. dwLoopCount = 0;
  521. dwTime = dwTempTime + 1000;
  522. }
  523. else dwLoopCount++;
  524. if( nBufferCount )
  525. {
  526. // Before writting it needs clear comm errors if they exist
  527. if( ::ClearCommError( m_hCommPort, &dwErrors, &comstat ) )
  528. {
  529. if( dwErrors & (CE_RXOVER|CE_BREAK) )
  530. {
  531. if( dwErrors & CE_RXOVER ) ::PurgeComm( m_hCommPort, PURGE_RXCLEAR );
  532. if( dwErrors & CE_BREAK ) ::ClearCommBreak( m_hCommPort );
  533. }
  534. }
  535. else
  536. {
  537. wCriticalError = 0;
  538. goto CRITICAL_END_THREAD;
  539. }
  540. ::SetCommMask( m_hCommPort, EV_TXEMPTY );
  541. // Asynchronous write data
  542. if( !::WriteFile( m_hCommPort, pBuffer, nBufferCount, &dwWritten, &ovWrite ) )
  543. {
  544. DWORD dwError = ::GetLastError();
  545. switch( dwError )
  546. {
  547. case ERROR_IO_PENDING:
  548. switch( ::WaitForMultipleObjects( sizeof(hEventsWrite)/sizeof(*hEventsWrite), hEventsWrite, FALSE, 100 ) )
  549. {
  550. case WAIT_OBJECT_0 + 0: goto END_THREAD;
  551. case WAIT_OBJECT_0 + 1:
  552. ::GetOverlappedResult( m_hCommPort, &ovWrite, &dwWritten, FALSE );
  553. break;
  554. }
  555. break;
  556. default:
  557. wCriticalError = 1;
  558. goto CRITICAL_END_THREAD;
  559. }
  560. if( !::WaitCommEvent( m_hCommPort, &dwEvtMask, &ovWait ) )
  561. {
  562. DWORD dwError = ::GetLastError();
  563. switch( dwError )
  564. {
  565. case ERROR_IO_PENDING:
  566. switch( ::WaitForMultipleObjects( sizeof(hEventsWait)/sizeof(*hEventsWait), hEventsWait, FALSE, m_dwMaxReadTime ) )
  567. {
  568. case WAIT_OBJECT_0 + 0: goto END_THREAD;
  569. case WAIT_OBJECT_0 + 1:
  570. ::GetOverlappedResult( m_hCommPort, &ovWait, &dwEvtMask, FALSE );
  571. break;
  572. }
  573. break;
  574. default:
  575. wCriticalError = 2;
  576. goto CRITICAL_END_THREAD;
  577. }
  578. }
  579. if( !( dwEvtMask & EV_TXEMPTY ) )
  580. {
  581. wCriticalError = 3;
  582. goto CRITICAL_END_THREAD;
  583. }
  584. }
  585. ::PurgeComm( m_hCommPort, PURGE_TXCLEAR | PURGE_TXABORT );
  586. // If write was not successfull
  587. if( dwWritten != nBufferCount )
  588. {
  589. wCriticalError = 4;
  590. goto CRITICAL_END_THREAD;
  591. }
  592. else
  593. {
  594. bTransmitAlarm = FALSE;
  595. bTransmitHeart = FALSE;
  596. m_nCountSuccess++;
  597. }
  598. }
  599. if( bWaitResponse )
  600. {
  601. // Before reading it needs clear comm errors if they exist
  602. if( ::ClearCommError( m_hCommPort, &dwErrors, &comstat ) )
  603. {
  604. if( dwErrors & (CE_RXOVER|CE_BREAK) )
  605. {
  606. if( dwErrors & CE_RXOVER ) ::PurgeComm( m_hCommPort, PURGE_RXCLEAR );
  607. if( dwErrors & CE_BREAK ) ::ClearCommBreak( m_hCommPort );
  608. }
  609. }
  610. else
  611. {
  612. wCriticalError = 5;
  613. goto CRITICAL_END_THREAD;
  614. }
  615. BOOL bReason = TRUE;
  616. if( !comstat.cbInQue ) // input buffer empty
  617. {
  618. ::SetCommMask( m_hCommPort, EV_RXCHAR );
  619. if( !::WaitCommEvent( m_hCommPort, &dwEvtMask, &ovWait ) )
  620. {
  621. DWORD dwError = ::GetLastError();
  622. switch( dwError )
  623. {
  624. case ERROR_IO_PENDING:
  625. switch( ::WaitForMultipleObjects( sizeof(hEventsWait)/sizeof(*hEventsWait), hEventsWait, FALSE, m_dwMaxWaitTime ) )
  626. {
  627. case WAIT_OBJECT_0 + 0: goto END_THREAD;
  628. case WAIT_OBJECT_0 + 1:
  629. ::GetOverlappedResult( m_hCommPort, &ovWait, &dwEvtMask, FALSE );
  630. break;
  631. }
  632. break;
  633. default:
  634. wCriticalError = 6;
  635. goto CRITICAL_END_THREAD;
  636. }
  637. }
  638. if( !( dwEvtMask & EV_RXCHAR ) )
  639. bReason = FALSE;
  640. }
  641. if( bReason )
  642. {
  643. // Asynchronous read data
  644. if( !::ReadFile( m_hCommPort, pBuffer, 1, &dwRead, &ovRead ) )
  645. {
  646. DWORD dwError = ::GetLastError();
  647. switch( dwError )
  648. {
  649. case ERROR_IO_PENDING:
  650. switch( ::WaitForMultipleObjects( sizeof(hEventsRead)/sizeof(*hEventsRead), hEventsRead, FALSE, m_dwMaxReadTime ) )
  651. {
  652. case WAIT_OBJECT_0 + 0: goto END_THREAD;
  653. case WAIT_OBJECT_0 + 1:
  654. ::GetOverlappedResult( m_hCommPort, &ovRead, &dwRead, FALSE );
  655. break;
  656. case WAIT_TIMEOUT:
  657. break;
  658. }
  659. break;
  660. default:
  661. wCriticalError = 7;
  662. goto CRITICAL_END_THREAD;
  663. }
  664. }
  665. ::PurgeComm( m_hCommPort, PURGE_RXCLEAR | PURGE_RXABORT );
  666. }
  667. else
  668. dwRead = 0UL;
  669. if( dwRead == 1 ) // reseived response from ECS
  670. {
  671. switch( *pBuffer )
  672. {
  673. case 0x0F:
  674. m_nCountSuccess++;
  675. bWaitResponse = FALSE;
  676. break;
  677. case 0xFF:
  678. m_nCountSuccess++; // need retransmit
  679. bTransmitAlarm = TRUE;
  680. break;
  681. default:
  682. m_nCountFailure++; // undefined situation
  683. bWaitResponse = FALSE;
  684. break;
  685. }
  686. }
  687. ::SetEvent( m_hEventCommand );
  688. }
  689. }
  690. CRITICAL_END_THREAD:
  691. str.Format( "socket critical error (%d)", wCriticalError );
  692. pEventQueue->PostEvent( LGS_EVENT_SOCKET_CRITICAL_ERROR, 0, m_nNumber, wCriticalError, RGB( 0xFF, 0, 0 ), (LPSTR)(LPCTSTR)str );
  693. END_THREAD:
  694. ::PurgeComm( m_hCommPort, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR );
  695. ::CancelWaitableTimer( hEventTimer );
  696. ::CloseHandle( hEventTimer );
  697. ::CloseHandle( ovWrite.hEvent );
  698. ::CloseHandle( ovWait.hEvent );
  699. ::CloseHandle( ovRead.hEvent );
  700. return 0;
  701. }
  702. */