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.

496 lines
13 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: SocketSM.cpp
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file of SocketSM.cpp
  13. //----------------------------------------------------------------------------
  14. #include "stdafx.h"
  15. //#include "chicago.h"
  16. #include "SocketSM.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. CSocketSM::CSocketSM()
  26. {
  27. m_nType = SOCKET_SM;
  28. m_hCommPort = NULL;
  29. m_hEventQuit = NULL;
  30. m_hEventLoop = NULL;
  31. m_hEventLoopStop = NULL;
  32. m_hThread = NULL;
  33. // m_nCurrentZone = 0;
  34. m_dwThreadWriteId = 0;
  35. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CSocketSM::CSocketSM") );
  36. }
  37. CSocketSM::~CSocketSM()
  38. {
  39. if( m_hThread )
  40. {
  41. ::SetEvent( m_hEventQuit );
  42. ::WaitForSingleObject( m_hThread, INFINITE );
  43. ::CloseHandle( m_hThread );
  44. }
  45. if( m_hEventQuit ) ::CloseHandle( m_hEventQuit );
  46. if( m_hEventLoop ) ::CloseHandle( m_hEventLoop );
  47. if( m_hEventLoopStop ) ::CloseHandle( m_hEventLoopStop );
  48. if( m_hCommPort )
  49. {
  50. ::CloseHandle( m_hCommPort );
  51. }
  52. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CSocketSM::~CSocketSM") );
  53. }
  54. BOOL CSocketSM::Create( CSensorArray *pSensorArray, LPCTSTR pCommName )
  55. {
  56. m_pSensorArray = pSensorArray;
  57. strcpy( m_pCommName, pCommName );
  58. m_strName = pCommName;
  59. if( !CreateCommPort() )
  60. {
  61. m_pSensorArray->GetEventQueue()->PostEvent( LGS_EVENT_SOCKET_COMMUNICATION, FALSE, m_nType, m_nNumber, RGB( 0xFF, 0, 0 ), "Can't create sinoptic map line" );
  62. return m_bInitiated = FALSE;
  63. }
  64. else
  65. m_pSensorArray->GetEventQueue()->PostEvent( LGS_EVENT_SOCKET_COMMUNICATION, TRUE, m_nType, m_nNumber, RGB( 0, 0xFF, 0 ), "Sinoptic map line created" );
  66. m_hEventQuit = ::CreateEvent( NULL, TRUE, FALSE, NULL );
  67. m_hEventLoop = ::CreateEvent( NULL, TRUE, FALSE, NULL );
  68. m_hEventLoopStop = ::CreateEvent( NULL, FALSE, FALSE, NULL );
  69. m_hThread = ::CreateThread( NULL, 0UL, ThreadProcSocketSM, this, 0UL, &m_dwThreadWriteId );
  70. // ::SetThreadPriority( m_hThread, THREAD_PRIORITY_BELOW_NORMAL );
  71. return m_bInitiated = TRUE;
  72. }
  73. BOOL CSocketSM::CreateCommPort()
  74. {
  75. if( m_hCommPort )
  76. ::CloseHandle( m_hCommPort );
  77. m_hCommPort = ::CreateFile( m_pCommName, GENERIC_READ | GENERIC_WRITE,
  78. 0UL, NULL, OPEN_EXISTING,
  79. FILE_FLAG_OVERLAPPED|FILE_FLAG_NO_BUFFERING, NULL );
  80. if( m_hCommPort == INVALID_HANDLE_VALUE )
  81. return FALSE;
  82. ::ZeroMemory( &m_DCB, sizeof(m_DCB) );
  83. m_DCB.DCBlength = sizeof( m_DCB );
  84. ::GetCommState( m_hCommPort, &m_DCB );
  85. m_DCB.BaudRate = CBR_1200;
  86. m_DCB.ByteSize = 8;
  87. m_DCB.Parity = EVENPARITY;
  88. m_DCB.StopBits = ONESTOPBIT;
  89. m_DCB.EvtChar = 0;
  90. m_DCB.fParity = TRUE;
  91. m_DCB.fOutxCtsFlow = FALSE;
  92. m_DCB.fOutxDsrFlow = FALSE;
  93. m_DCB.fDtrControl = DTR_CONTROL_DISABLE;
  94. m_DCB.fDsrSensitivity = FALSE;
  95. m_DCB.fOutX = FALSE;
  96. m_DCB.fInX = FALSE;
  97. m_DCB.fErrorChar = FALSE;
  98. m_DCB.fNull = FALSE;
  99. m_DCB.fRtsControl = RTS_CONTROL_DISABLE;
  100. m_DCB.fAbortOnError = FALSE;
  101. if( !::SetupComm( m_hCommPort, MESSAGE_SIZE*5, MESSAGE_SIZE*5 ) )
  102. return FALSE;
  103. if( !::SetCommState( m_hCommPort, &m_DCB ) )
  104. return FALSE;
  105. COMMTIMEOUTS ct;
  106. ct.ReadIntervalTimeout = 0UL;
  107. ct.ReadTotalTimeoutMultiplier = 0UL;
  108. ct.ReadTotalTimeoutConstant = 0UL;
  109. ct.WriteTotalTimeoutMultiplier = 0UL;
  110. ct.WriteTotalTimeoutConstant = 0UL;
  111. if( !::SetCommTimeouts( m_hCommPort, &ct ) )
  112. return FALSE;
  113. m_dwMaxReadTime = ( MESSAGE_SIZE * CBR_9600 ) / m_DCB.BaudRate;
  114. if( !m_dwMaxReadTime )
  115. m_dwMaxReadTime = 1;
  116. m_dwMaxWaitTime = m_dwMaxReadTime + MAX_PROCESSING_TIME;
  117. ::PurgeComm( m_hCommPort, PURGE_TXCLEAR | PURGE_RXCLEAR | PURGE_TXABORT | PURGE_RXABORT );
  118. return TRUE;
  119. }
  120. void CSocketSM::CommandLoopStart()
  121. {
  122. // m_nCurrentZone = 0;
  123. ::SetEvent( m_hEventLoop );
  124. }
  125. DWORD CSocketSM::ThreadProc()
  126. {
  127. DWORD dwErrors;
  128. COMSTAT comstat;
  129. DWORD dwWritten;
  130. OVERLAPPED ovWrite;
  131. ovWrite.hEvent = ::CreateEvent( NULL, TRUE, FALSE, NULL );
  132. ovWrite.Internal = ovWrite.InternalHigh = ovWrite.Offset = ovWrite.OffsetHigh = 0UL;
  133. DWORD dwEvtMask = 0;
  134. OVERLAPPED ovWait;
  135. ovWait.hEvent = ::CreateEvent( NULL, TRUE, FALSE, NULL );
  136. ovWait.Internal = ovWait.InternalHigh = ovWait.Offset = ovWait.OffsetHigh = 0UL;
  137. DWORD dwRead;
  138. OVERLAPPED ovRead;
  139. ovRead.hEvent = ::CreateEvent( NULL, TRUE, FALSE, NULL );
  140. ovRead.Internal = ovRead.InternalHigh = ovRead.Offset = ovRead.OffsetHigh = 0UL;
  141. HANDLE hEventsCommand[] = { m_hEventQuit, m_hEventLoopStop, m_hEventLoop };
  142. HANDLE hEventsWrite[] = { m_hEventQuit, ovWrite.hEvent };
  143. HANDLE hEventsWait[] = { m_hEventQuit, ovWait.hEvent };
  144. HANDLE hEventsRead[] = { m_hEventQuit, ovRead.hEvent };
  145. BYTE pBuffer[ MESSAGE_SIZE ];
  146. DWORD dwTime = 0;
  147. DWORD dwLoopCount = 0;
  148. WORD wCriticalError;
  149. CEventQueue *pEventQueue = m_pSensorArray->GetEventQueue();
  150. BYTE bits[8] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
  151. BYTE pByteBuffer[90];
  152. BOOL pBoolBuffer[90];
  153. INT pBase[3] = { 1, 31, 61 };
  154. LPBYTE pAlarm = pByteBuffer + pBase[0];
  155. LPBYTE pSecure = pByteBuffer + pBase[1];
  156. LPBYTE pAccess = pByteBuffer + pBase[2];
  157. LPBOOL pAlarmBool = pBoolBuffer + pBase[0];
  158. LPBOOL pSecureBool = pBoolBuffer + pBase[1];
  159. LPBOOL pAccessBool = pBoolBuffer + pBase[2];
  160. LPBYTE ppByteBuffer[3] = { pAlarm, pSecure, pAccess };
  161. LPBOOL ppBoolBuffer[3] = { pAlarmBool, pSecureBool, pAccessBool };
  162. ::FillMemory( pByteBuffer, 90 * sizeof( BYTE ), 0x00 );
  163. ::FillMemory( pBoolBuffer, 90 * sizeof( BOOL ), 0x01 );
  164. int nCountByte = m_pSensorArray->GetZoneCount() / 6 + ( ( m_pSensorArray->GetZoneCount() % 6 ) != 0 );
  165. int nCurrentGroup = 0;
  166. int nCurrentByte = 0;
  167. CString str;
  168. BOOL bReason;
  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 = 0;
  184. dwTime = dwTempTime + 1000;
  185. }
  186. else dwLoopCount++;
  187. for( int i = 0; i < m_pSensorArray->GetZoneCount(); i++ )
  188. {
  189. int nByte = i / 6;
  190. int maskOR = 1 << ( i % 6 );
  191. int maskAND = ~maskOR;
  192. switch( m_pSensorArray->GetZone( i )->GetState() )
  193. {
  194. case CZone::DISABLE:
  195. if( !( pAccess[ nByte ] & maskOR ) ) pAccessBool[ nByte ] = TRUE;
  196. if( pAlarm[ nByte ] & maskOR ) pAlarmBool[ nByte ] = TRUE;
  197. if( pSecure[ nByte ] & maskOR ) pSecureBool[ nByte ] = TRUE;
  198. pAccess[ nByte ] |= maskOR;
  199. pAlarm[ nByte ] &= maskAND;
  200. pSecure[ nByte ] &= maskAND;
  201. break;
  202. case CZone::UNKNOWN:
  203. case CZone::NORMAL:
  204. if( !( pSecure[ nByte ] & maskOR ) ) pSecureBool[ nByte ] = TRUE;
  205. if( pAlarm[ nByte ] & maskOR ) pAlarmBool[ nByte ] = TRUE;
  206. if( pAccess[ nByte ] & maskOR ) pAccessBool[ nByte ] = TRUE;
  207. pSecure[ nByte ] |= maskOR;
  208. pAlarm[ nByte ] &= maskAND;
  209. pAccess[ nByte ] &= maskAND;
  210. break;
  211. case CZone::POSTALARM:
  212. case CZone::TIMEOUT:
  213. case CZone::ALARM:
  214. if( !( pAlarm[ nByte ] & maskOR ) ) pAlarmBool[ nByte ] = TRUE;
  215. if( pSecure[ nByte ] & maskOR ) pSecureBool[ nByte ] = TRUE;
  216. if( pAccess[ nByte ] & maskOR ) pAccessBool[ nByte ] = TRUE;
  217. pAlarm[ nByte ] |= maskOR;
  218. pSecure[ nByte ] &= maskAND;
  219. pAccess[ nByte ] &= maskAND;
  220. break;
  221. }
  222. }
  223. BOOL bFound = FALSE;
  224. for( i = 0; i < 3 * nCountByte; i++ )
  225. {
  226. nCurrentByte++;
  227. if( nCurrentByte==nCountByte )
  228. {
  229. nCurrentByte = 0;
  230. nCurrentGroup = (nCurrentGroup+1)%3;
  231. }
  232. if( ppBoolBuffer[ nCurrentGroup ][ nCurrentByte ] )
  233. {
  234. bFound = TRUE;
  235. break;
  236. }
  237. }
  238. if( !bFound )
  239. {
  240. ::Sleep( 100 );
  241. for( i = 0; i < 3 * nCountByte; i++ )
  242. {
  243. nCurrentByte++;
  244. if( nCurrentByte==nCountByte )
  245. {
  246. nCurrentByte = 0;
  247. nCurrentGroup = (nCurrentGroup+1)%3;
  248. }
  249. if( ppByteBuffer[ nCurrentGroup ][ nCurrentByte ] )
  250. break;
  251. }
  252. }
  253. pBuffer[0] = ( nCurrentByte + pBase[ nCurrentGroup ] ) | 0x80; // add start bit
  254. pBuffer[1] = ppByteBuffer[ nCurrentGroup ][ nCurrentByte ] | 0x40; // add command bit
  255. if( ::ClearCommError( m_hCommPort, &dwErrors, &comstat ) )
  256. {
  257. if( dwErrors & (CE_RXOVER|CE_BREAK) )
  258. {
  259. if( dwErrors & CE_RXOVER ) ::PurgeComm( m_hCommPort, PURGE_RXCLEAR );
  260. if( dwErrors & CE_BREAK ) ::ClearCommBreak( m_hCommPort );
  261. }
  262. }
  263. else
  264. {
  265. wCriticalError = 0;
  266. goto RECREATE_COMMPORT;
  267. }
  268. ::SetCommMask( m_hCommPort, EV_TXEMPTY );
  269. // Asynchronous write data
  270. if( !::WriteFile( m_hCommPort, pBuffer, MESSAGE_SIZE, &dwWritten, &ovWrite ) )
  271. {
  272. DWORD dwError = ::GetLastError();
  273. switch( dwError )
  274. {
  275. case ERROR_IO_PENDING:
  276. switch( ::WaitForMultipleObjects( sizeof(hEventsWrite)/sizeof(*hEventsWrite), hEventsWrite, FALSE, 100 ) )
  277. {
  278. case WAIT_OBJECT_0 + 0: goto END_THREAD;
  279. case WAIT_OBJECT_0 + 1:
  280. case WAIT_TIMEOUT:
  281. ::GetOverlappedResult( m_hCommPort, &ovWrite, &dwWritten, FALSE );
  282. break;
  283. }
  284. break;
  285. default:
  286. wCriticalError = 1;
  287. goto RECREATE_COMMPORT;
  288. }
  289. if( !::WaitCommEvent( m_hCommPort, &dwEvtMask, &ovWait ) )
  290. {
  291. DWORD dwError = ::GetLastError();
  292. switch( dwError )
  293. {
  294. case ERROR_IO_PENDING:
  295. switch( ::WaitForMultipleObjects( sizeof(hEventsWait)/sizeof(*hEventsWait), hEventsWait, FALSE, m_dwMaxReadTime ) )
  296. {
  297. case WAIT_OBJECT_0 + 0: goto END_THREAD;
  298. case WAIT_OBJECT_0 + 1:
  299. case WAIT_TIMEOUT:
  300. ::GetOverlappedResult( m_hCommPort, &ovWait, &dwEvtMask, FALSE );
  301. break;
  302. }
  303. break;
  304. default:
  305. wCriticalError = 2;
  306. goto RECREATE_COMMPORT;
  307. }
  308. }
  309. if( !( dwEvtMask & EV_TXEMPTY ) )
  310. {
  311. wCriticalError = 3;
  312. goto RECREATE_COMMPORT;
  313. }
  314. }
  315. ::PurgeComm( m_hCommPort, PURGE_TXCLEAR | PURGE_TXABORT );
  316. // If write was not successfull
  317. if( dwWritten != MESSAGE_SIZE )
  318. {
  319. wCriticalError = 4;
  320. goto RECREATE_COMMPORT;
  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 RECREATE_COMMPORT;
  335. }
  336. 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. case WAIT_TIMEOUT:
  351. ::GetOverlappedResult( m_hCommPort, &ovWait, &dwEvtMask, FALSE );
  352. break;
  353. }
  354. break;
  355. default:
  356. wCriticalError = 6;
  357. goto RECREATE_COMMPORT;
  358. }
  359. }
  360. if( !( dwEvtMask & EV_RXCHAR ) )
  361. bReason = FALSE;
  362. }
  363. if( bReason )
  364. {
  365. // Asynchronous read data
  366. if( !::ReadFile( m_hCommPort, pBuffer, MESSAGE_SIZE, &dwRead, &ovRead ) )
  367. {
  368. DWORD dwError = ::GetLastError();
  369. switch( dwError )
  370. {
  371. case ERROR_IO_PENDING:
  372. switch( ::WaitForMultipleObjects( sizeof(hEventsRead)/sizeof(*hEventsRead), hEventsRead, FALSE, m_dwMaxReadTime ) )
  373. {
  374. case WAIT_OBJECT_0 + 0: goto END_THREAD;
  375. case WAIT_OBJECT_0 + 1:
  376. case WAIT_TIMEOUT:
  377. ::GetOverlappedResult( m_hCommPort, &ovRead, &dwRead, FALSE );
  378. break;
  379. }
  380. break;
  381. default:
  382. wCriticalError = 7;
  383. goto RECREATE_COMMPORT;
  384. }
  385. }
  386. ::PurgeComm( m_hCommPort, PURGE_RXCLEAR | PURGE_RXABORT );
  387. }
  388. else
  389. dwRead = 0UL;
  390. if( dwRead == MESSAGE_SIZE )
  391. {
  392. m_nCountSuccess++;
  393. ppBoolBuffer[ nCurrentGroup ][ nCurrentByte ] = FALSE;
  394. }
  395. else
  396. {
  397. m_nCountFailure++;
  398. }
  399. continue;
  400. RECREATE_COMMPORT:
  401. if( !CreateCommPort() )
  402. goto CRITICAL_END_THREAD;
  403. }
  404. CRITICAL_END_THREAD:
  405. str.Format( "SM error(%d)", wCriticalError );
  406. pEventQueue->PostEvent( LGS_EVENT_ERROR, 0, m_nNumber, wCriticalError, RGB( 0xFF, 0, 0 ), (LPSTR)(LPCTSTR)str );
  407. END_THREAD:
  408. ::PurgeComm( m_hCommPort, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR );
  409. ::CloseHandle( ovWrite.hEvent );
  410. ::CloseHandle( ovWait.hEvent );
  411. ::CloseHandle( ovRead.hEvent );
  412. TRACE("<SocketSM> thread: ");
  413. return 0;
  414. }
  415. void CSocketSM::CommandLoopStop()
  416. {
  417. if( ::WaitForSingleObject( m_hEventLoop, 0 ) == WAIT_OBJECT_0 )
  418. ::SetEvent( m_hEventLoopStop );
  419. }