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.

1032 lines
65 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
12 months ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "servaModBusManager.h"
  2. #include "servaKernel.h"
  3. #define LOG( X ) m_pLog->Log( "MODBUS", this, X )
  4. #ifdef DEBUG
  5. #define LOG_ERROR( X ) {\
  6. char _tBfrErrMSG2w34[256];\
  7. sprintf( _tBfrErrMSG2w34, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
  8. m_pLog->Log( "MODBUS", this, _LOG_ERROR, _tBfrErrMSG2w34 );\
  9. }
  10. #else
  11. #define LOG_ERROR( X ) m_pLog->Log( "MODBUS", this, _LOG_ERROR, X );
  12. #endif
  13. servaModBusManager *servaModBusManager::_instance = NULL;
  14. servaModBusManager *servaModBusManager::getInstance(){
  15. if ( _instance == NULL ) {
  16. _instance = new servaModBusManager();
  17. };
  18. return _instance;
  19. };
  20. servaModBusManager::servaModBusManager():basicThread(),basicValidatable(){
  21. m_bStage = MBUS_STAGE_INIT;
  22. m_pDBC = NULL;
  23. m_pLog = basicLogCollection::getInstance();
  24. m_pMaster = servaKernel::getInstance();
  25. if ( m_pMaster == NULL ) return;
  26. m_pDBC = m_pMaster->getDBC();
  27. memset( &m_sManagerData, 0, sizeof( MODBUS_MANAGER_DATA ));
  28. };
  29. servaModBusManager::~servaModBusManager(){
  30. waitThreadEnd( true );
  31. if (m_vRtuUnits.size()) {
  32. for( V_MODBUS_RTU_INFO::iterator idx=m_vRtuUnits.begin(); idx != m_vRtuUnits.end(); idx++ ) {
  33. free( *idx );
  34. };
  35. m_vRtuUnits.clear();
  36. };
  37. };
  38. bool servaModBusManager::create(){
  39. if( m_pMaster == NULL ) return false;
  40. if( m_pDBC == NULL ) return false;
  41. MODBUS_MANAGER_DATA * tData = m_pDBC->getModBusManagerData();
  42. if ( tData ) {
  43. memcpy( &m_sManagerData, tData, sizeof( MODBUS_MANAGER_DATA ) );
  44. free( tData );
  45. };
  46. m_bStage = MBUS_STAGE_INIT;
  47. return init();
  48. };
  49. int servaModBusManager::modbus_get_rtu( CSTR ip, uint16_t port, uint8_t addr, uint8_t &status ){
  50. uint8_t buf[16];
  51. modbus_t * ctx; // Modbus context pointer
  52. int rc; // Return code for error checking
  53. // Create a new Modbus TCP context for a slave device
  54. ctx = modbus_new_tcp( ip, port );
  55. if (ctx == NULL) {
  56. #ifdef DEBUG_EXTRA
  57. LOG( "Unable to create the Modbus context" );
  58. #endif
  59. return -1;
  60. };
  61. // Connect to the Modbus TCP slave device
  62. rc = modbus_connect(ctx);
  63. if (rc == -1) {
  64. #ifdef DEBUG_EXTRA
  65. char tB[256];
  66. sprintf(tB, "Connection failed: %s", modbus_strerror(errno));
  67. LOG( tB );
  68. #endif
  69. modbus_free(ctx);
  70. return -1;
  71. };
  72. rc = modbus_read_bits(ctx, addr, 8, buf ); //0x10
  73. if( rc == -1 ) {
  74. #ifdef DEBUG_EXTRA
  75. char tB[256];
  76. sprintf(tB, "Write register failed: %s", modbus_strerror(errno));
  77. LOG( tB );
  78. #endif
  79. } else {
  80. status = modbus_get_byte_from_bits( buf, 0, 8 );
  81. };
  82. // Close the Modbus TCP connection and free the context
  83. modbus_close(ctx);
  84. modbus_free(ctx);
  85. return rc;
  86. };
  87. int servaModBusManager::modbus_set_rtu( CSTR ip, uint16_t port, uint8_t addr, uint8_t data ){
  88. uint8_t bits[8];
  89. modbus_t * ctx; // Modbus context pointer
  90. int rc; // Return code for error checking
  91. // Create a new Modbus TCP context for a slave device
  92. ctx = modbus_new_tcp( ip, port );
  93. if (ctx == NULL) {
  94. #ifdef DEBUG_EXTRA
  95. LOG( "Unable to create the Modbus context");
  96. #endif
  97. return -1;
  98. };
  99. // Connect to the Modbus TCP slave device
  100. rc = modbus_connect(ctx);
  101. if (rc == -1) {
  102. #ifdef DEBUG_EXTRA
  103. char tB[256];
  104. sprintf(tB, "Connection failed: %s", modbus_strerror(errno));
  105. LOG( tB );
  106. #endif
  107. modbus_free(ctx);
  108. return -1;
  109. };
  110. modbus_set_bits_from_byte( bits, 0, data );
  111. rc = modbus_write_bits(ctx, addr, 8, bits ); //0x10
  112. if (rc == -1) {
  113. #ifdef DEBUG_EXTRA
  114. char tB[256];
  115. sprintf(tB, "Write register failed: %s", modbus_strerror(errno));
  116. LOG( tB );
  117. #endif
  118. };
  119. // Close the Modbus TCP connection and free the context
  120. modbus_close(ctx);
  121. modbus_free(ctx);
  122. return rc;
  123. };
  124. int servaModBusManager::modbus_set_rtu_bit( CSTR ip, uint16_t port, uint8_t addr, uint8_t bit ){
  125. modbus_t * ctx; // Modbus context pointer
  126. int rc; // Return code for error checking
  127. // Create a new Modbus TCP context for a slave device
  128. ctx = modbus_new_tcp( ip, port );
  129. if (ctx == NULL) {
  130. #ifdef DEBUG_EXTRA
  131. LOG( "Unable to create the Modbus context");
  132. #endif
  133. return -1;
  134. };
  135. // Connect to the Modbus TCP slave device
  136. rc = modbus_connect(ctx);
  137. if (rc == -1) {
  138. #ifdef DEBUG_EXTRA
  139. char tB[256];
  140. sprintf(tB, "Connection failed: %s", modbus_strerror(errno));
  141. LOG( tB );
  142. #endif
  143. modbus_free(ctx);
  144. return -1;
  145. };
  146. rc = modbus_write_bit(ctx, addr, bit ); //0x10
  147. if (rc == -1) {
  148. #ifdef DEBUG_EXTRA
  149. char tB[256];
  150. sprintf(tB, "Write register failed: %s", modbus_strerror(errno));
  151. LOG( tB );
  152. #endif
  153. };
  154. // Close the Modbus TCP connection and free the context
  155. modbus_close(ctx);
  156. modbus_free(ctx);
  157. return rc;
  158. };
  159. void servaModBusManager::m_fThreadProc( void ){
  160. m_bThreadStatus = TH_WORKING;
  161. uint32_t oss = time(NULL);
  162. while ( m_bThreadStatus < TH_ENDED ) {
  163. uint32_t css = time(NULL);
  164. switch (m_bStage) {
  165. case MBUS_STAGE_INIT:{
  166. #ifdef DEBUG
  167. LOG("Clear Loadded ModBus Modules");
  168. #endif
  169. if (m_vRtuUnits.size()) {
  170. for( V_MODBUS_RTU_INFO::iterator idx=m_vRtuUnits.begin(); idx != m_vRtuUnits.end(); idx++ ) {
  171. free( *idx );
  172. };
  173. m_vRtuUnits.clear();
  174. };
  175. #ifdef DEBUG
  176. LOG("Load Enabled ModBus Modules");
  177. #endif
  178. DBC_UINT64_LIST *tList = m_pDBC->listModBusRTUs( 0, 'E' );
  179. if ( tList ) {
  180. if ( tList->size() ) {
  181. for ( DBC_UINT64_LIST::iterator idx = tList->begin(); idx != tList->end(); idx++ ) {
  182. MODBUS_RTU_INFO *tData = m_pDBC->getModBusRTU( *idx );
  183. if ( tData ) {
  184. m_vRtuUnits.push_back( tData );
  185. };
  186. };
  187. };
  188. delete(tList);
  189. #ifdef DEBUG
  190. LOG("Load ModBus Modules Done");
  191. #endif
  192. } else LOG("No ModBus Modules Defined");
  193. m_bStage = MBUS_STAGE_SYNC;
  194. break;
  195. };
  196. case MBUS_STAGE_SYNC:{
  197. #ifdef DEBUG_EXTRA
  198. LOG("Sync ModBus Modules");
  199. #endif
  200. if( m_vRtuUnits.size() ){
  201. for( V_MODBUS_RTU_INFO::iterator idx = m_vRtuUnits.begin(); idx!= m_vRtuUnits.end(); idx++ ){
  202. int rv = modbus_get_rtu((*idx)->ip, (*idx)->port, (*idx)->output_base_addr, (*idx)->c_OutBits );
  203. if ( rv != -1 ) {
  204. rv = modbus_get_rtu((*idx)->ip, (*idx)->port, (*idx)->input_base_addr, (*idx)->c_InBits );
  205. if ( rv != -1 ) {
  206. (*idx)->status = MBUS_STATUS_ONLINE;
  207. m_pDBC->updateModBusRTU( (*idx)->id, (*idx)->status, (*idx)->c_InBits, (*idx)->c_OutBits );
  208. //Input Handling
  209. if ( (*idx)->c_InBits ) {
  210. LOG("Have DI Signal!!");
  211. //Testing Bits
  212. for ( uint8_t bi = 0; bi < 4; bi++ ) {
  213. if ( ((*idx)->c_InBits & ( 1 << bi ) ) == ( 1 << bi ) ) {
  214. //Bit is set
  215. if( (*idx)->crossbar[4+bi] ) { // Assigned Action For Bit
  216. // check functionality
  217. switch ( (*idx)->crossbar[4 + bi] ) {
  218. case MBUS_FUNC_ACK_UPPER_TRACK:{
  219. uint64_t cTID = m_pDBC->getTrackIdByPlatform( (*idx)->platform_id, 'U' );
  220. if ( cTID ) {
  221. SESSION_DATA *tSData = servaSessionManager::getInstance()->getActiveSessionByTrack( cTID );
  222. if ( tSData ) {
  223. //Rate Session
  224. m_pDBC->updateSessionStatus( tSData->db_id, 1 );
  225. // Create Event?? or just Disable Scada Signal
  226. DBC_UINT64_LIST *tIDS = m_pDBC->listPlatformModBusRTU( (*idx)->platform_id );
  227. if ( tIDS ) {
  228. for ( DBC_UINT64_LIST::iterator idd=tIDS->begin(); idd != tIDS->end(); idd++ ) {
  229. MODBUS_RTU_INFO *tRTU = m_pDBC->getModBusRTU( *idd );
  230. if ( tRTU ) {
  231. uint8_t CBV = tRTU->crossbar[MBUS_CROSSBAR_UPPER_TRACK];
  232. if ( CBV ) {
  233. CBV --;
  234. uint8_t bON = (1 << CBV);
  235. uint8_t bOFF = ~( 1 << CBV );
  236. //Remove Scheduled Signal change
  237. m_mProtector.lock();
  238. for ( V_MODBUS_ACION_ITEMS::iterator idz=m_vActioItems.begin(); idz!=m_vActioItems.end(); ){
  239. if( (*idz)->module_id == tRTU->id ) {
  240. //check if shceduled signal is current
  241. if( (((*idz)->value & bON ) == bON ) || (((*idz)->mask & bOFF ) == bOFF ) ){
  242. free((*idx));
  243. m_vActioItems.erase( idz );
  244. } else idz++;
  245. //break;
  246. } else idz++;
  247. };
  248. m_mProtector.unlock();
  249. //Mitigate Signal if sent
  250. if ( (tRTU->c_OutBits & bON ) == bON ) {
  251. tRTU->c_OutBits &= bOFF;
  252. int rv = modbus_set_rtu( tRTU->ip, tRTU->port, tRTU->output_base_addr, tRTU->c_OutBits );
  253. if ( rv < 0 ) {
  254. m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_OFFLINE, 0, 0 );
  255. } else {
  256. m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_ONLINE, tRTU->c_InBits, tRTU->c_OutBits );
  257. };
  258. };
  259. };//Crossbar check
  260. free( tRTU );
  261. };
  262. };
  263. delete( tIDS );
  264. };
  265. free( tSData );
  266. };
  267. };
  268. break;
  269. };
  270. case MBUS_FUNC_ACK_BOTTOM_TRACK:{//Bottom Track
  271. uint64_t cTID = m_pDBC->getTrackIdByPlatform( (*idx)->platform_id, 'B' );
  272. if ( cTID ) {
  273. SESSION_DATA *tSData = servaSessionManager::getInstance()->getActiveSessionByTrack( cTID );
  274. if ( tSData ) {
  275. //Rate Session
  276. m_pDBC->updateSessionStatus( tSData->db_id, 1 );
  277. // Create Event?? or just Disable Scada Signal
  278. DBC_UINT64_LIST *tIDS = m_pDBC->listPlatformModBusRTU( (*idx)->platform_id );
  279. if ( tIDS ) {
  280. for ( DBC_UINT64_LIST::iterator idd=tIDS->begin(); idd != tIDS->end(); idd++ ) {
  281. MODBUS_RTU_INFO *tRTU = m_pDBC->getModBusRTU( *idd );
  282. if ( tRTU ) {
  283. uint8_t CBV = tRTU->crossbar[MBUS_CROSSBAR_BOTTOM_TRACK];
  284. if ( CBV ) {
  285. CBV --;
  286. uint8_t bON = (1 << CBV );
  287. uint8_t bOFF = ~( 1 << CBV );
  288. //Remove Scheduled Signal change
  289. m_mProtector.lock();
  290. for ( V_MODBUS_ACION_ITEMS::iterator idz=m_vActioItems.begin(); idz!=m_vActioItems.end(); ){
  291. if( (*idz)->module_id == tRTU->id ) {
  292. //check if shceduled signal is current
  293. if( (((*idz)->value & bON ) == bON ) || (((*idz)->mask & bOFF ) == bOFF ) ){
  294. free((*idx));
  295. m_vActioItems.erase( idz );
  296. } else idz++;
  297. //break;
  298. } else idz++;
  299. };
  300. m_mProtector.unlock();
  301. //Mitigate Signal if sent
  302. if ( (tRTU->c_OutBits & bON ) == bON ) {
  303. tRTU->c_OutBits &= bOFF;
  304. int rv = modbus_set_rtu( tRTU->ip, tRTU->port, tRTU->output_base_addr, tRTU->c_OutBits );
  305. if ( rv < 0 ) {
  306. m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_OFFLINE, 0, 0 );
  307. } else {
  308. m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_ONLINE, tRTU->c_InBits, tRTU->c_OutBits );
  309. };
  310. };
  311. }; // Crossbar check
  312. free( tRTU );
  313. };
  314. };
  315. delete( tIDS );
  316. };
  317. free( tSData );
  318. };
  319. };
  320. break;
  321. };
  322. case MBUS_FUNC_ACK_BOTH_TRACKS:{//Both Tracks
  323. uint64_t cTID = m_pDBC->getTrackIdByPlatform( (*idx)->platform_id, 'U' );
  324. if ( cTID ) {
  325. SESSION_DATA *tSData = servaSessionManager::getInstance()->getActiveSessionByTrack( cTID );
  326. if ( tSData ) {
  327. //Rate Session
  328. m_pDBC->updateSessionStatus( tSData->db_id, 1 );
  329. // Create Event?? or just Disable Scada Signal
  330. DBC_UINT64_LIST *tIDS = m_pDBC->listPlatformModBusRTU( (*idx)->platform_id );
  331. if ( tIDS ) {
  332. for ( DBC_UINT64_LIST::iterator idd=tIDS->begin(); idd != tIDS->end(); idd++ ) {
  333. MODBUS_RTU_INFO *tRTU = m_pDBC->getModBusRTU( *idd );
  334. if ( tRTU ) {
  335. uint8_t CBV = tRTU->crossbar[MBUS_CROSSBAR_UPPER_TRACK];
  336. if ( CBV ) {
  337. CBV --;
  338. uint8_t bON = (1 << CBV );
  339. uint8_t bOFF = ~( 1 << CBV );
  340. //Remove Scheduled Signal change
  341. m_mProtector.lock();
  342. for ( V_MODBUS_ACION_ITEMS::iterator idz=m_vActioItems.begin(); idz!=m_vActioItems.end(); ){
  343. if( (*idz)->module_id == tRTU->id ) {
  344. //check if shceduled signal is current
  345. if( (((*idz)->value & bON ) == bON ) || (((*idz)->mask & bOFF ) == bOFF ) ){
  346. free((*idx));
  347. m_vActioItems.erase( idz );
  348. } else idz++;
  349. //break;
  350. } else idz++;
  351. };
  352. m_mProtector.unlock();
  353. //Mitigate Signal if sent
  354. if ( (tRTU->c_OutBits & bON ) == bON ) {
  355. tRTU->c_OutBits &= bOFF;
  356. int rv = modbus_set_rtu( tRTU->ip, tRTU->port, tRTU->output_base_addr, tRTU->c_OutBits );
  357. if ( rv < 0 ) {
  358. m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_OFFLINE, 0, 0 );
  359. } else {
  360. m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_ONLINE, tRTU->c_InBits, tRTU->c_OutBits );
  361. };
  362. };
  363. }; // Crossbar check
  364. free( tRTU );
  365. };
  366. };
  367. delete( tIDS );
  368. };
  369. free( tSData );
  370. };
  371. };
  372. cTID = m_pDBC->getTrackIdByPlatform( (*idx)->platform_id, 'B' );
  373. if ( cTID ) {
  374. SESSION_DATA *tSData = servaSessionManager::getInstance()->getActiveSessionByTrack( cTID );
  375. if ( tSData ) {
  376. //Rate Session
  377. m_pDBC->updateSessionStatus( tSData->db_id, 1 );
  378. // Create Event?? or just Disable Scada Signal
  379. DBC_UINT64_LIST *tIDS = m_pDBC->listPlatformModBusRTU( (*idx)->platform_id );
  380. if ( tIDS ) {
  381. for ( DBC_UINT64_LIST::iterator idd=tIDS->begin(); idd != tIDS->end(); idd++ ) {
  382. MODBUS_RTU_INFO *tRTU = m_pDBC->getModBusRTU( *idd );
  383. if ( tRTU ) {
  384. uint8_t CBV = tRTU->crossbar[MBUS_CROSSBAR_BOTTOM_TRACK];
  385. if ( CBV ) {
  386. uint8_t bON = (1 << CBV );
  387. uint8_t bOFF = ~( 1 << CBV );
  388. //Remove Scheduled Signal change
  389. m_mProtector.lock();
  390. for ( V_MODBUS_ACION_ITEMS::iterator idz=m_vActioItems.begin(); idz!=m_vActioItems.end(); ){
  391. if( (*idz)->module_id == tRTU->id ) {
  392. //check if shceduled signal is current
  393. if( (((*idz)->value & bON ) == bON ) || (((*idz)->mask & bOFF ) == bOFF ) ){
  394. free((*idx));
  395. m_vActioItems.erase( idz );
  396. } else idz++;
  397. //break;
  398. } else idz++;
  399. };
  400. m_mProtector.unlock();
  401. //Mitigate Signal if sent
  402. if ( (tRTU->c_OutBits & bON ) == bON ) {
  403. tRTU->c_OutBits &= bOFF;
  404. int rv = modbus_set_rtu( tRTU->ip, tRTU->port, tRTU->output_base_addr, tRTU->c_OutBits );
  405. if ( rv < 0 ) {
  406. m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_OFFLINE, 0, 0 );
  407. } else {
  408. m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_ONLINE, tRTU->c_InBits, tRTU->c_OutBits );
  409. };
  410. };
  411. }; // Crossbar check
  412. free( tRTU );
  413. };
  414. };
  415. delete( tIDS );
  416. };
  417. free( tSData );
  418. };
  419. };
  420. break;
  421. };
  422. case MBUS_FUNC_SIG_STROBE_LIGHT:{ // Set TCC AUX signal
  423. DBC_UINT64_LIST *tIDS = m_pDBC->listPlatformModBusRTU( (*idx)->platform_id );
  424. if ( tIDS ) {
  425. for ( DBC_UINT64_LIST::iterator idd=tIDS->begin(); idd != tIDS->end(); idd++ ) {
  426. MODBUS_RTU_INFO *tRTU = m_pDBC->getModBusRTU( *idd );
  427. if ( tRTU ) {
  428. //AUX crossbar record used as additional functionality
  429. uint8_t CBV = tRTU->crossbar[MBUS_CROSSBAR_AUX];
  430. // check if TCC Module
  431. if( tRTU->func == 3 && CBV ) {
  432. // check if was not snoozed by operator
  433. int c_status = m_pDBC->getScadaPostStatus( (*idx)->platform_id, SCADA_POST_TYPE_HELTH_STROBE, (*idx)->func );
  434. if( c_status < 1 ){ // Not Snoozed by Operator
  435. CBV--;
  436. uint8_t bON = (1 << CBV );
  437. uint8_t bOFF = ~( 1 << CBV );
  438. //Remove Scheduled Signal change
  439. m_mProtector.lock();
  440. for ( V_MODBUS_ACION_ITEMS::iterator idz=m_vActioItems.begin(); idz!=m_vActioItems.end(); ){
  441. if( (*idz)->module_id == tRTU->id ) {
  442. //check if shceduled signal is current
  443. if( (((*idz)->value & bON ) == bON ) || (((*idz)->mask & bOFF ) == bOFF ) ){
  444. free((*idx));
  445. m_vActioItems.erase( idz );
  446. } else idz++;
  447. //break;
  448. } else idz++;
  449. };
  450. m_mProtector.unlock();
  451. //Skeep Signal if Allready Set
  452. if ( (tRTU->c_OutBits & bON ) == 0 ) {
  453. tRTU->c_OutBits |= bON;
  454. int rv = modbus_set_rtu( tRTU->ip, tRTU->port, tRTU->output_base_addr, tRTU->c_OutBits );
  455. if ( rv < 0 ) {
  456. tRTU->c_OutBits &= bOFF;
  457. m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_OFFLINE, tRTU->c_InBits, tRTU->c_OutBits );
  458. } else {
  459. m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_ONLINE, tRTU->c_InBits, tRTU->c_OutBits );
  460. m_pDBC->addScadaPost( (*idx)->platform_id, SCADA_POST_TYPE_HELTH_STROBE, (*idx)->func );
  461. };
  462. } else {
  463. m_pDBC->addScadaPost( (*idx)->platform_id, SCADA_POST_TYPE_HELTH_STROBE, (*idx)->func );
  464. };
  465. };
  466. };
  467. free( tRTU );
  468. };
  469. };
  470. delete( tIDS );
  471. };
  472. break;
  473. };
  474. };
  475. };
  476. };
  477. };
  478. //Reset Latch
  479. //rv = modbus_set_rtu_bit( (*idx)->ip, (*idx)->port, 36, 0 );
  480. rv = modbus_set_rtu( (*idx)->ip, (*idx)->port, 40, 0 );
  481. if ( rv == -1 ) {
  482. LOG_ERROR("Error clear latch flag on RTU");
  483. };
  484. };
  485. continue;
  486. };
  487. #ifdef DEBUG_EXTRA
  488. else LOG_ERROR("Read DI Failure");
  489. #endif
  490. }
  491. #ifdef DEBUG_EXTRA
  492. else LOG_ERROR("Read DO Failure");
  493. #endif
  494. (*idx)->status = MBUS_STATUS_OFFLINE;
  495. m_pDBC->updateModBusRTU( (*idx)->id, (*idx)->status, (*idx)->c_InBits, (*idx)->c_OutBits );
  496. };
  497. };
  498. #ifdef DEBUG_EXTRA
  499. LOG("Sync ModBus Modules Done");
  500. #endif
  501. oss = css; // Give 1 Second for STAGE_WORK ??
  502. m_bStage = MBUS_STAGE_WORK;
  503. [[fallthrough]];
  504. };
  505. case MBUS_STAGE_WORK:{
  506. uint64_t cts = getMilliseconds();
  507. m_mProtector.lock();
  508. if ( m_vActioItems.size() ) {
  509. for( V_MODBUS_ACION_ITEMS::iterator idx = m_vActioItems.begin(); idx != m_vActioItems.end(); ){
  510. if( (*idx)->scheduled_on < cts ) {
  511. handleRTU( (*idx)->module_id, (*idx)->value, (*idx)->mask );
  512. free( *idx );
  513. m_vActioItems.erase( idx );
  514. } else idx++;
  515. };
  516. };
  517. m_mProtector.unlock();
  518. break;
  519. };
  520. case MBUS_STAGE_END:{
  521. m_bThreadStatus = TH_ENDED;
  522. break;
  523. };
  524. };
  525. if ( oss != css ) {
  526. //Each Second or as it takes .... Scan All RTUs may take longer!
  527. oss = css;
  528. m_bStage = MBUS_STAGE_SYNC;;
  529. };
  530. SLEEP(DEFAULT_THREAD_SLEEP_TIME);
  531. };
  532. };
  533. bool servaModBusManager::handleRTU( uint64_t rtu_id, uint8_t value, uint8_t mask ){
  534. bool rVal = false;
  535. if ( m_pDBC == NULL ) return rVal;
  536. MODBUS_RTU_INFO *tRTU = m_pDBC->getModBusRTU( rtu_id );
  537. if ( tRTU ) {
  538. if( ( tRTU->c_OutBits & value ) == 0 ) {
  539. //Set
  540. tRTU->c_OutBits |= value;
  541. int rv = modbus_set_rtu( tRTU->ip, tRTU->port, tRTU->output_base_addr, tRTU->c_OutBits );
  542. if ( rv < 0 ) {
  543. // Do we need clear Database values on failure??
  544. //m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_OFFLINE, 0, 0 );
  545. } else {
  546. m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_ONLINE, tRTU->c_InBits, tRTU->c_OutBits );
  547. rVal = true;
  548. };
  549. };
  550. if( ( tRTU->c_OutBits & mask ) != 0 ) {
  551. //Clear
  552. tRTU->c_OutBits &= ~mask;
  553. int rv = modbus_set_rtu( tRTU->ip, tRTU->port, tRTU->output_base_addr, tRTU->c_OutBits );
  554. if ( rv < 0 ) {
  555. // Do we need clear Database values on failure??
  556. //m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_OFFLINE, 0, 0 );
  557. } else {
  558. m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_ONLINE, tRTU->c_InBits, tRTU->c_OutBits );
  559. rVal = true;
  560. };
  561. };
  562. free(tRTU);
  563. };
  564. return rVal;
  565. };
  566. bool servaModBusManager::validate(){
  567. MODBUS_MANAGER_DATA * tData = m_pDBC->getModBusManagerData();
  568. if ( tData ) {
  569. if( memcmp( &m_sManagerData, tData, sizeof( MODBUS_MANAGER_DATA ) ) != 0 ){
  570. #ifdef DEBUG
  571. LOG("Settings was changed ... reapplying");
  572. #endif
  573. memcpy( &m_sManagerData, tData, sizeof( MODBUS_MANAGER_DATA ) );
  574. m_bStage = MBUS_STAGE_INIT; // Reload Modules just in case
  575. };
  576. free( tData );
  577. };
  578. return true;
  579. };
  580. bool servaModBusManager::dispatchEvent( EVENT_CONTAINER *Evt ){
  581. if ( Evt == NULL ) return false;
  582. switch ( Evt->m_eType ) {
  583. case EVT_SRV_SESSION_STARTED:{
  584. LOG("EVT Session Start");
  585. if( m_sManagerData.automaticAlarmRisingTO ) {
  586. //setup Schedule
  587. LOG("Setup Scheduled Signal");
  588. SERVA_SESSION_EVT_DATA *tSData = (SERVA_SESSION_EVT_DATA *)Evt->m_pEvtData;
  589. if( tSData ) {
  590. LOG("Have Session Evt Data");
  591. SCADA_TRACK_INFO *tData = m_pDBC->getTrackSCADA(Evt->m_nSourceNodeID);
  592. if ( tData ) {
  593. LOG("Have Track Info");
  594. //MODBUS_RTU_INFO *tRTU = m_pDBC->getModBusRTU_Platform( tData->platform_id );
  595. uint8_t expected_module_functionality = 0;
  596. if ( tSData->side == 'L' || tSData->side == 'B' ) {
  597. expected_module_functionality |= 1;
  598. };
  599. if ( tSData->side == 'R' || tSData->side == 'B' ) {
  600. expected_module_functionality |= 2;
  601. };
  602. #ifdef DEBUG
  603. if ( expected_module_functionality ) LOG("Expect Module Function");
  604. #endif
  605. DBC_UINT64_LIST *tIDS = m_pDBC->listPlatformModBusRTU(tData->platform_id);
  606. if ( tIDS ) {
  607. for ( DBC_UINT64_LIST::iterator idx=tIDS->begin(); idx != tIDS->end(); idx++ ) {
  608. LOG("Check Module!");
  609. MODBUS_RTU_INFO *tRTU = m_pDBC->getModBusRTU( *idx );
  610. if ( tRTU ) {
  611. uint8_t CBV = tRTU->crossbar[((tData->side == 'U')?MBUS_CROSSBAR_UPPER_TRACK:MBUS_CROSSBAR_BOTTOM_TRACK)];
  612. if( CBV ) { // 0 - disabled functionality
  613. CBV --; // correct DC bits order
  614. // 1 = left 2 = right 3 = tcc
  615. if( ( tRTU->func == 1 ) && ( ( expected_module_functionality & 1 ) == 1 ) ) {
  616. LOG("Using Left Module!");
  617. MODBUS_ACION_ITEM *tAI = (MODBUS_ACION_ITEM *)malloc(sizeof(MODBUS_ACION_ITEM));
  618. if ( tAI ) {
  619. memset( tAI, 0, sizeof(MODBUS_ACION_ITEM) );
  620. tAI->scheduled_on = getMilliseconds() + ( m_sManagerData.automaticAlarmRisingTO * 1000 );
  621. tAI->module_id = tRTU->id;
  622. tAI->value = ( 1 << CBV );
  623. m_mProtector.lock();
  624. m_vActioItems.push_back( tAI );
  625. m_mProtector.unlock();
  626. };
  627. };
  628. if( ( tRTU->func == 2 ) && ( ( expected_module_functionality & 2 ) == 2 ) ) {
  629. LOG("Using Right Module!");
  630. MODBUS_ACION_ITEM *tAI = (MODBUS_ACION_ITEM *)malloc(sizeof(MODBUS_ACION_ITEM));
  631. if ( tAI ) {
  632. memset( tAI, 0, sizeof(MODBUS_ACION_ITEM) );
  633. tAI->scheduled_on = getMilliseconds() + ( m_sManagerData.automaticAlarmRisingTO * 1000 );
  634. tAI->module_id = tRTU->id;
  635. tAI->value = ( 1 << CBV );
  636. m_mProtector.lock();
  637. m_vActioItems.push_back( tAI );
  638. m_mProtector.unlock();
  639. };
  640. };
  641. if( tRTU->func == 3 ) { // TCC does always
  642. LOG("Have TCC Module!");
  643. MODBUS_ACION_ITEM *tAI = (MODBUS_ACION_ITEM *)malloc(sizeof(MODBUS_ACION_ITEM));
  644. if ( tAI ) {
  645. memset( tAI, 0, sizeof(MODBUS_ACION_ITEM) );
  646. tAI->scheduled_on = getMilliseconds() + ( m_sManagerData.automaticAlarmRisingTO * 1000 );
  647. tAI->module_id = tRTU->id;
  648. tAI->value = ( 1 << CBV );
  649. m_mProtector.lock();
  650. m_vActioItems.push_back( tAI );
  651. m_mProtector.unlock();
  652. };
  653. };
  654. }; // Crossbar check
  655. free( tRTU );
  656. };
  657. };
  658. delete( tIDS );
  659. };
  660. free( tData );
  661. };
  662. };
  663. };
  664. break;
  665. };
  666. case EVT_SRV_SESSION_ENDED:{ // Ignore Session End since The SCADA signal must be disabled only by operator
  667. if ( m_sManagerData.supressSignalBySessionEnd ) {
  668. LOG("EVT Session Ended");
  669. SCADA_TRACK_INFO *tData = m_pDBC->getTrackSCADA( Evt->m_nSourceNodeID );
  670. if ( tData ) {
  671. //MODBUS_RTU_INFO *tRTU = m_pDBC->getModBusRTU_Platform( tData->platform_id );
  672. DBC_UINT64_LIST *tIDS = m_pDBC->listPlatformModBusRTU( tData->platform_id );
  673. if ( tIDS ) {
  674. for ( DBC_UINT64_LIST::iterator idx=tIDS->begin(); idx != tIDS->end(); idx++ ) {
  675. MODBUS_RTU_INFO *tRTU = m_pDBC->getModBusRTU( *idx );
  676. if ( tRTU ) {
  677. uint8_t CBV = tRTU->crossbar[((tData->side == 'U')?MBUS_CROSSBAR_UPPER_TRACK:MBUS_CROSSBAR_BOTTOM_TRACK)];
  678. if ( CBV ) {
  679. CBV--;
  680. uint8_t bON = (1 << CBV );
  681. uint8_t bOFF = ~( 1 << CBV );
  682. //Remove Scheduled Signal change
  683. m_mProtector.lock();
  684. for ( V_MODBUS_ACION_ITEMS::iterator idd=m_vActioItems.begin(); idd!=m_vActioItems.end(); ){
  685. if( (*idd)->module_id == tRTU->id ) {
  686. //check if shceduled signal is current
  687. if( (((*idd)->value & bON ) == bON ) || (((*idd)->mask & bOFF ) == bOFF ) ){
  688. free((*idd));
  689. m_vActioItems.erase( idd );
  690. } else idd++;
  691. //break;
  692. } else idd++;
  693. };
  694. m_mProtector.unlock();
  695. //Mitigate Signal if sent
  696. if ( (tRTU->c_OutBits & bON ) == bON ) {
  697. tRTU->c_OutBits &= bOFF;
  698. int rv = modbus_set_rtu( tRTU->ip, tRTU->port, tRTU->output_base_addr, tRTU->c_OutBits );
  699. if ( rv < 0 ) {
  700. m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_OFFLINE, 0, 0 );
  701. } else {
  702. m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_ONLINE, tRTU->c_InBits, tRTU->c_OutBits );
  703. };
  704. };
  705. }; // checking crossbar
  706. free( tRTU );
  707. };
  708. };
  709. delete( tIDS );
  710. };
  711. free( tData );
  712. };
  713. };
  714. break;
  715. };
  716. case EVT_SRV_SESSION_RATED:{
  717. if ( Evt->m_pEvtData == NULL ) return false;
  718. if ( m_sManagerData.hazardSignalLevel ) {
  719. SESSION_RATE_DATA *tSRD = (SESSION_RATE_DATA *)Evt->m_pEvtData;
  720. SCADA_TRACK_INFO *tData = m_pDBC->getTrackSCADA( tSRD->track_id );
  721. if ( tData ) {
  722. //MODBUS_RTU_INFO *tRTU = m_pDBC->getModBusRTU_Platform( tData->platform_id );
  723. DBC_UINT64_LIST *tIDS = m_pDBC->listPlatformModBusRTU( tData->platform_id );
  724. if ( tIDS ) {
  725. for ( DBC_UINT64_LIST::iterator idx=tIDS->begin(); idx != tIDS->end(); idx++ ) {
  726. MODBUS_RTU_INFO *tRTU = m_pDBC->getModBusRTU( *idx );
  727. if ( tRTU ) {
  728. uint8_t CBV = tRTU->crossbar[((tData->side == 'U')?MBUS_CROSSBAR_UPPER_TRACK:MBUS_CROSSBAR_BOTTOM_TRACK)];
  729. if ( CBV ) {
  730. CBV--;
  731. uint8_t bON = (1 << CBV );
  732. uint8_t bOFF = ~( 1 << CBV );
  733. //Remove Scheduled Signal change
  734. m_mProtector.lock();
  735. for ( V_MODBUS_ACION_ITEMS::iterator idd=m_vActioItems.begin(); idd!=m_vActioItems.end(); ){
  736. //check if shceduled ActionItem is Assigned to Current RTU
  737. if( (*idd)->module_id == tRTU->id ) {
  738. if( (((*idd)->value & bON ) == bON ) || (((*idd)->mask & bOFF ) == bOFF ) ){
  739. free((*idd));
  740. m_vActioItems.erase( idd );
  741. } else idd++;
  742. } else idd++;
  743. };
  744. m_mProtector.unlock();
  745. if ( tSRD->level < m_sManagerData.hazardSignalLevel ) {
  746. //Clear Signal if Set
  747. if ( (tRTU->c_OutBits & bON ) == bON ) {
  748. tRTU->c_OutBits &= bOFF;
  749. int rv = modbus_set_rtu( tRTU->ip, tRTU->port, tRTU->output_base_addr, tRTU->c_OutBits );
  750. if ( rv < 0 ) {
  751. m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_OFFLINE, 0, 0 );
  752. } else {
  753. m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_ONLINE, tRTU->c_InBits, tRTU->c_OutBits );
  754. };
  755. };
  756. } else {
  757. //Set Signal if was Cleared
  758. if ( (tRTU->c_OutBits & bON ) == 0 ) {
  759. tRTU->c_OutBits |= bON;
  760. int rv = modbus_set_rtu( tRTU->ip, tRTU->port, tRTU->output_base_addr, tRTU->c_OutBits );
  761. if ( rv < 0 ) {
  762. m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_OFFLINE, 0, 0 );
  763. } else {
  764. m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_ONLINE, tRTU->c_InBits, tRTU->c_OutBits );
  765. };
  766. };
  767. };
  768. };// Crossbar Check
  769. free( tRTU );
  770. };
  771. };
  772. delete(tIDS);
  773. };
  774. free( tData );
  775. };
  776. };
  777. break;
  778. };
  779. case EVT_SRV_MODBUS_TEST_P:{
  780. MODBUS_TEST_DATA *tData = (MODBUS_TEST_DATA *)Evt->m_pEvtData;
  781. if ( tData ) {
  782. DBC_UINT64_LIST *tIDS = m_pDBC->listPlatformModBusRTU( tData->id );
  783. if ( tIDS ) {
  784. for ( DBC_UINT64_LIST::iterator idx=tIDS->begin(); idx != tIDS->end(); idx++ ) {
  785. MODBUS_RTU_INFO *tRTU = m_pDBC->getModBusRTU( *idx );
  786. if ( tRTU ) {
  787. #ifdef DEBUG_EXTRA
  788. char tB[200];
  789. sprintf( tB, "%s:%d 0x%02x 0x%02x", tRTU->ip, tRTU->port, tRTU->output_base_addr, tData->c_OutBits );
  790. LOG( tB );
  791. #endif
  792. int rv = modbus_set_rtu( tRTU->ip, tRTU->port, tRTU->output_base_addr, tData->c_OutBits );
  793. if ( rv < 0 ) {
  794. m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_OFFLINE, 0, 0 );
  795. } else {
  796. m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_ONLINE, tRTU->c_InBits, tData->c_OutBits );
  797. };
  798. free( tRTU );
  799. };
  800. };
  801. };
  802. };
  803. return false; // No need Propogate this Evt
  804. };
  805. case EVT_SRV_MODBUS_TEST_M:{
  806. MODBUS_TEST_DATA *tData = (MODBUS_TEST_DATA *)Evt->m_pEvtData;
  807. if ( tData ) {
  808. MODBUS_RTU_INFO *tRTU = m_pDBC->getModBusRTU( tData->id );
  809. if ( tRTU ) {
  810. #ifdef DEBUG_EXTRA
  811. char tB[200];
  812. sprintf( tB, "%s:%d 0x%02x 0x%02x", tRTU->ip, tRTU->port, tRTU->output_base_addr, tData->c_OutBits );
  813. LOG( tB );
  814. #endif
  815. int rv = modbus_set_rtu( tRTU->ip, tRTU->port, tRTU->output_base_addr, tData->c_OutBits );
  816. if ( rv < 0 ) {
  817. m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_OFFLINE, 0, 0 );
  818. } else {
  819. m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_ONLINE, tRTU->c_InBits, tData->c_OutBits );
  820. };
  821. free( tRTU );
  822. };
  823. };
  824. return false; // No need Propogate this Evt
  825. };
  826. case CMD_KRN_SCADA_SNOOZE_POST_MESSAGE:{
  827. MODBUS_PB_DATA *tData = (MODBUS_PB_DATA *)Evt->m_pEvtData;
  828. if ( tData ) {
  829. MODBUS_PM_DATA *sData = m_pDBC->getScadaPostData( tData->id );
  830. if ( sData ) {
  831. bool mitigate = false;
  832. if ( sData->type == 6 ) {
  833. mitigate = true;
  834. } else {
  835. if ( sData->type >= m_sManagerData.helthSignalLevel ) mitigate = true;
  836. };
  837. if ( mitigate ) {
  838. DBC_UINT64_LIST *tIDS = m_pDBC->listPlatformModBusRTU( sData->platform_id );
  839. if ( tIDS ) {
  840. for ( DBC_UINT64_LIST::iterator idx=tIDS->begin(); idx != tIDS->end(); idx++ ) {
  841. MODBUS_RTU_INFO *tRTU = m_pDBC->getModBusRTU( *idx );
  842. if ( tRTU ) {
  843. uint8_t CBV = tRTU->crossbar[((sData->type == 6)?MBUS_CROSSBAR_AUX:MBUS_CROSSBAR_HEALTH)];
  844. //check only TCC modules
  845. if ( tRTU->func == 3 && CBV ) {
  846. CBV --;
  847. uint8_t bON = (1 << CBV );
  848. uint8_t bOFF = ~( 1 << CBV );
  849. #ifdef DEBUG_EXTRA
  850. char tB[200];
  851. sprintf( tB, "%s:%d 0x%02x 0x%02x", tRTU->ip, tRTU->port, tRTU->output_base_addr, tData->c_OutBits );
  852. LOG( tB );
  853. #endif
  854. //Mitigate Signal if sent
  855. if ( (tRTU->c_OutBits & bON ) == bON ) {
  856. tRTU->c_OutBits &= bOFF;
  857. int rv = modbus_set_rtu( tRTU->ip, tRTU->port, tRTU->output_base_addr, tRTU->c_OutBits );
  858. if ( rv < 0 ) {
  859. m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_OFFLINE, 0, 0 );
  860. } else {
  861. m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_ONLINE, tRTU->c_InBits, tRTU->c_OutBits );
  862. };
  863. };
  864. }; // Crossbar Check
  865. free( tRTU );
  866. };
  867. };
  868. };
  869. };
  870. m_pDBC->updateScadaPostStatus(tData->id, 1);
  871. };
  872. };
  873. return false; // No need Propogate this Evt
  874. };
  875. case EVT_SRV_MODBUS_D_POSTBOX:{
  876. MODBUS_PB_DATA *tData = (MODBUS_PB_DATA *)Evt->m_pEvtData;
  877. if ( tData ) {
  878. MODBUS_PM_DATA *sData = m_pDBC->getScadaPostData( tData->id );
  879. if ( sData ) {
  880. bool mitigate = false;
  881. if ( sData->type == 6 ) {
  882. mitigate = true;
  883. } else {
  884. if ( sData->type >= m_sManagerData.helthSignalLevel ) mitigate = true;
  885. };
  886. if ( mitigate ) {
  887. DBC_UINT64_LIST *tIDS = m_pDBC->listPlatformModBusRTU( sData->platform_id );
  888. if ( tIDS ) {
  889. for ( DBC_UINT64_LIST::iterator idx=tIDS->begin(); idx != tIDS->end(); idx++ ) {
  890. MODBUS_RTU_INFO *tRTU = m_pDBC->getModBusRTU( *idx );
  891. if ( tRTU ) {
  892. uint8_t CBV = tRTU->crossbar[((sData->type == 6)?MBUS_CROSSBAR_AUX:MBUS_CROSSBAR_HEALTH)];
  893. //check only TCC modules
  894. if ( tRTU->func == 3 && CBV ) {
  895. CBV --;
  896. uint8_t bON = (1 << CBV );
  897. uint8_t bOFF = ~( 1 << CBV );
  898. #ifdef DEBUG_EXTRA
  899. char tB[200];
  900. sprintf( tB, "%s:%d 0x%02x 0x%02x", tRTU->ip, tRTU->port, tRTU->output_base_addr, tData->c_OutBits );
  901. LOG( tB );
  902. #endif
  903. //Mitigate Signal if sent
  904. if ( (tRTU->c_OutBits & bON ) == bON ) {
  905. tRTU->c_OutBits &= bOFF;
  906. int rv = modbus_set_rtu( tRTU->ip, tRTU->port, tRTU->output_base_addr, tRTU->c_OutBits );
  907. if ( rv < 0 ) {
  908. m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_OFFLINE, 0, 0 );
  909. } else {
  910. m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_ONLINE, tRTU->c_InBits, tRTU->c_OutBits );
  911. };
  912. };
  913. }; // Crossbar Check
  914. free( tRTU );
  915. };
  916. };
  917. };
  918. };
  919. m_pDBC->deleteScadaPost( tData->id );
  920. };
  921. };
  922. return false; // No need Propogate this Evt
  923. };
  924. case EVT_SRV_MODULE_HEALTH:{
  925. if ( Evt->m_pEvtData == NULL ) return false;
  926. if ( m_sManagerData.helthSignalLevel ) {
  927. MODULE_HEALTH_STATUS *tData = (MODULE_HEALTH_STATUS *)Evt->m_pEvtData;
  928. //checking if Snoozed
  929. int c_status = m_pDBC->getScadaPostStatus( tData->platform_id, (SERVER_SCADA_POST_TYPE) tData->status, tData->module_id );
  930. if( c_status < 1 ){
  931. if( c_status < 0 && tData->status ) { // No Record in MBox
  932. // Add status regardless of
  933. m_pDBC->addScadaPost( tData->platform_id, (SERVER_SCADA_POST_TYPE) tData->status, tData->module_id );
  934. c_status = 0; // Set Enabled Signal Status
  935. };
  936. // clear all abowe Health statuses as not relevant
  937. // do we need a module ID ?? since the zone is whole platform
  938. // If tData->status == 0 then the Health status will be cleared
  939. m_pDBC->deleteScadaPostsAbowe( tData->platform_id, (SERVER_SCADA_POST_TYPE) tData->status );
  940. if( tData->status >= m_sManagerData.helthSignalLevel && c_status == 0 ){
  941. //MODBUS_RTU_INFO *tRTU = m_pDBC->getModBusRTU_Platform( tData->platform_id );
  942. DBC_UINT64_LIST *tIDS = m_pDBC->listPlatformModBusRTU( tData->platform_id );
  943. if ( tIDS ) {
  944. for ( DBC_UINT64_LIST::iterator idx=tIDS->begin(); idx != tIDS->end(); idx++ ) {
  945. MODBUS_RTU_INFO *tRTU = m_pDBC->getModBusRTU( *idx );
  946. if (tRTU) {
  947. uint8_t CBV = tRTU->crossbar[MBUS_CROSSBAR_HEALTH];
  948. //Check Only TCC modules
  949. if ( tRTU->func == 3 && CBV ) {
  950. CBV --;
  951. uint8_t bON = (1 << CBV );
  952. uint8_t bOFF = ~( 1 << CBV );
  953. #ifdef DEBUG_EXTRA
  954. char tB[200];
  955. sprintf( tB, "%s:%d 0x%02x 0x%02x", tRTU->ip, tRTU->port, tRTU->output_base_addr, tData->c_OutBits );
  956. LOG( tB );
  957. #endif
  958. //Set Signal if was Cleared
  959. if ( (tRTU->c_OutBits & bON ) == 0 ) {
  960. tRTU->c_OutBits |= bON;
  961. int rv = modbus_set_rtu( tRTU->ip, tRTU->port, tRTU->output_base_addr, tRTU->c_OutBits );
  962. if ( rv < 0 ) {
  963. m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_OFFLINE, 0, 0 );
  964. } else {
  965. m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_ONLINE, tRTU->c_InBits, tRTU->c_OutBits );
  966. };
  967. };
  968. }; // Crossbar check
  969. free( tRTU );
  970. };
  971. };
  972. delete( tIDS );
  973. };
  974. } else { //Below Level
  975. //MODBUS_RTU_INFO *tRTU = m_pDBC->getModBusRTU_Platform( tData->platform_id );
  976. DBC_UINT64_LIST *tIDS = m_pDBC->listPlatformModBusRTU( tData->platform_id );
  977. if ( tIDS ) {
  978. for ( DBC_UINT64_LIST::iterator idx=tIDS->begin(); idx != tIDS->end(); idx++ ) {
  979. MODBUS_RTU_INFO *tRTU = m_pDBC->getModBusRTU( *idx );
  980. if ( tRTU ) {
  981. uint8_t CBV = tRTU->crossbar[MBUS_CROSSBAR_HEALTH];
  982. //check only TCC modules
  983. if ( tRTU->func == 3 && CBV ) {
  984. CBV --;
  985. uint8_t bON = (1 << CBV );
  986. uint8_t bOFF = ~( 1 << CBV );
  987. #ifdef DEBUG_EXTRA
  988. char tB[200];
  989. sprintf( tB, "%s:%d 0x%02x 0x%02x", tRTU->ip, tRTU->port, tRTU->output_base_addr, tData->c_OutBits );
  990. LOG( tB );
  991. #endif
  992. //Mitigate Signal if sent
  993. if ( (tRTU->c_OutBits & bON ) == bON ) {
  994. tRTU->c_OutBits &= bOFF;
  995. int rv = modbus_set_rtu( tRTU->ip, tRTU->port, tRTU->output_base_addr, tRTU->c_OutBits );
  996. if ( rv < 0 ) {
  997. m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_OFFLINE, 0, 0 );
  998. } else {
  999. m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_ONLINE, tRTU->c_InBits, tRTU->c_OutBits );
  1000. };
  1001. };
  1002. }; // Crossbar Check
  1003. free( tRTU );
  1004. };
  1005. };
  1006. };
  1007. };
  1008. }
  1009. };
  1010. break;
  1011. };
  1012. };
  1013. return true;
  1014. };
  1015. #undef LOG
  1016. #undef LOG_ERROR