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.

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