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.

579 lines
27 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. //(*idx)->platform_id
  217. LOG("Have Assigned Procedure!");
  218. };
  219. };
  220. };
  221. //Reset Latch
  222. //rv = modbus_set_rtu_bit( (*idx)->ip, (*idx)->port, 36, 0 );
  223. rv = modbus_set_rtu( (*idx)->ip, (*idx)->port, 40, 0 );
  224. if ( rv == -1 ) {
  225. LOG_ERROR("Error clear latch flag on RTU");
  226. };
  227. };
  228. continue;
  229. };
  230. #ifdef DEBUG_EXTRA
  231. else LOG_ERROR("Read DI Failure");
  232. #endif
  233. }
  234. #ifdef DEBUG_EXTRA
  235. else LOG_ERROR("Read DO Failure");
  236. #endif
  237. (*idx)->status = MBUS_STATUS_OFFLINE;
  238. m_pDBC->updateModBusRTU( (*idx)->id, (*idx)->status, (*idx)->c_InBits, (*idx)->c_OutBits );
  239. };
  240. };
  241. #ifdef DEBUG_EXTRA
  242. LOG("Sync ModBus Modules Done");
  243. #endif
  244. m_bStage = MBUS_STAGE_WORK;
  245. break;
  246. };
  247. case MBUS_STAGE_WORK:{
  248. uint64_t cts = getMilliseconds();
  249. m_mProtector.lock();
  250. if ( m_vActioItems.size() ) {
  251. for( V_MODBUS_ACION_ITEMS::iterator idx = m_vActioItems.begin(); idx != m_vActioItems.end(); ){
  252. if( (*idx)->scheduled_on < cts ) {
  253. MODBUS_RTU_INFO *tRTU = m_pDBC->getModBusRTU( (*idx)->module_id );
  254. if ( tRTU ) {
  255. if( ( tRTU->c_OutBits & (*idx)->value ) == 0 ) {
  256. //Set
  257. tRTU->c_OutBits |= (*idx)->value;
  258. int rv = modbus_set_rtu( tRTU->ip, tRTU->port, tRTU->output_base_addr, tRTU->c_OutBits );
  259. if ( rv < 0 ) {
  260. m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_OFFLINE, 0, 0 );
  261. } else {
  262. m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_ONLINE, tRTU->c_InBits, tRTU->c_OutBits );
  263. };
  264. };
  265. if( ( tRTU->c_OutBits & (*idx)->mask ) != 0 ) {
  266. //Clear
  267. tRTU->c_OutBits &= ~(*idx)->mask;
  268. int rv = modbus_set_rtu( tRTU->ip, tRTU->port, tRTU->output_base_addr, tRTU->c_OutBits );
  269. if ( rv < 0 ) {
  270. m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_OFFLINE, 0, 0 );
  271. } else {
  272. m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_ONLINE, tRTU->c_InBits, tRTU->c_OutBits );
  273. };
  274. };
  275. free(tRTU);
  276. };
  277. free( *idx );
  278. m_vActioItems.erase( idx );
  279. //Make Just One SCADA per loop
  280. //break;
  281. } else idx++;
  282. };
  283. };
  284. m_mProtector.unlock();
  285. break;
  286. };
  287. case MBUS_STAGE_END:{
  288. m_bThreadStatus = TH_ENDED;
  289. break;
  290. };
  291. };
  292. if ( oss != css ) {
  293. //Each Second
  294. oss = css;
  295. m_bStage = MBUS_STAGE_SYNC;
  296. };
  297. SLEEP(DEFAULT_THREAD_SLEEP_TIME);
  298. };
  299. };
  300. bool servaModBusManager::validate(){
  301. MODBUS_MANAGER_DATA * tData = m_pDBC->getModBusManagerData();
  302. if ( tData ) {
  303. if( memcmp( &m_sManagerData, tData, sizeof( MODBUS_MANAGER_DATA ) ) != 0 ){
  304. #ifdef DEBUG
  305. LOG("Settings was changed ... reapplying");
  306. #endif
  307. memcpy( &m_sManagerData, tData, sizeof( MODBUS_MANAGER_DATA ) );
  308. m_bStage = MBUS_STAGE_INIT; // Reload Modules just in case
  309. };
  310. free( tData );
  311. };
  312. return true;
  313. };
  314. bool servaModBusManager::dispatchEvent( EVENT_CONTAINER *Evt ){
  315. if ( Evt == NULL ) return false;
  316. switch ( Evt->m_eType ) {
  317. case EVT_SRV_SESSION_STARTED:{
  318. if( m_sManagerData.automaticAlarmRisingTO ) {
  319. //setup Schedule
  320. SCADA_TRACK_INFO *tData = m_pDBC->getTrackSCADA( Evt->m_nSourceNodeID );
  321. if ( tData ) {
  322. //MODBUS_RTU_INFO *tRTU = m_pDBC->getModBusRTU_Platform( tData->platform_id );
  323. DBC_UINT64_LIST *tIDS = m_pDBC->listPlatformModBusRTU( tData->platform_id );
  324. if ( tIDS ) {
  325. for ( DBC_UINT64_LIST::iterator idx=tIDS->begin(); idx != tIDS->end(); idx++ ) {
  326. MODBUS_RTU_INFO *tRTU = m_pDBC->getModBusRTU( *idx );
  327. if ( tRTU ) {
  328. MODBUS_ACION_ITEM *tAI = (MODBUS_ACION_ITEM *)malloc(sizeof(MODBUS_ACION_ITEM));
  329. if ( tAI ) {
  330. memset( tAI, 0, sizeof(MODBUS_ACION_ITEM) );
  331. tAI->scheduled_on = getMilliseconds() + ( m_sManagerData.automaticAlarmRisingTO * 1000 );
  332. tAI->module_id = tRTU->id;
  333. tAI->value = ( 1 << tRTU->crossbar[((tData->side == 'U')?MBUS_CROSSBAR_UPPER_TRACK:MBUS_CROSSBAR_BOTTOM_TRACK)] );
  334. m_mProtector.lock();
  335. m_vActioItems.push_back( tAI );
  336. m_mProtector.unlock();
  337. };
  338. free( tRTU );
  339. };
  340. };
  341. };
  342. free( tData );
  343. };
  344. };
  345. break;
  346. };
  347. case EVT_SRV_SESSION_ENDED:{
  348. SCADA_TRACK_INFO *tData = m_pDBC->getTrackSCADA( Evt->m_nSourceNodeID );
  349. if ( tData ) {
  350. //MODBUS_RTU_INFO *tRTU = m_pDBC->getModBusRTU_Platform( tData->platform_id );
  351. DBC_UINT64_LIST *tIDS = m_pDBC->listPlatformModBusRTU( tData->platform_id );
  352. if ( tIDS ) {
  353. for ( DBC_UINT64_LIST::iterator idx=tIDS->begin(); idx != tIDS->end(); idx++ ) {
  354. MODBUS_RTU_INFO *tRTU = m_pDBC->getModBusRTU( *idx );
  355. if ( tRTU ) {
  356. uint8_t bON = ( 1 << tRTU->crossbar[((tData->side == 'U')?MBUS_CROSSBAR_UPPER_TRACK:MBUS_CROSSBAR_BOTTOM_TRACK)] );
  357. uint8_t bOFF = ~( 1 << tRTU->crossbar[((tData->side == 'U')?MBUS_CROSSBAR_UPPER_TRACK:MBUS_CROSSBAR_BOTTOM_TRACK)] );
  358. //Remove Scheduled Signal change
  359. m_mProtector.lock();
  360. for ( V_MODBUS_ACION_ITEMS::iterator idx=m_vActioItems.begin(); idx!=m_vActioItems.end(); ){
  361. if( (*idx)->module_id == tRTU->id ) {
  362. //check if shceduled signal is current
  363. if( (((*idx)->value & bON ) == bON ) || (((*idx)->mask & bOFF ) == bOFF ) ){
  364. free((*idx));
  365. m_vActioItems.erase( idx );
  366. } else idx++;
  367. //break;
  368. } else idx++;
  369. };
  370. m_mProtector.unlock();
  371. //Mitigate Signal if sent
  372. if ( (tRTU->c_OutBits & bON ) == bON ) {
  373. tRTU->c_OutBits &= bOFF;
  374. int rv = modbus_set_rtu( tRTU->ip, tRTU->port, tRTU->output_base_addr, tRTU->c_OutBits );
  375. if ( rv < 0 ) {
  376. m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_OFFLINE, 0, 0 );
  377. } else {
  378. m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_ONLINE, tRTU->c_InBits, tRTU->c_OutBits );
  379. };
  380. };
  381. free( tRTU );
  382. };
  383. };
  384. };
  385. free( tData );
  386. };
  387. break;
  388. };
  389. case EVT_SRV_SESSION_RATED:{
  390. if ( Evt->m_pEvtData == NULL ) return false;
  391. if ( m_sManagerData.hazardSignalLevel ) {
  392. SESSION_RATE_DATA *tSRD = (SESSION_RATE_DATA *)Evt->m_pEvtData;
  393. SCADA_TRACK_INFO *tData = m_pDBC->getTrackSCADA( tSRD->track_id );
  394. if ( tData ) {
  395. //MODBUS_RTU_INFO *tRTU = m_pDBC->getModBusRTU_Platform( tData->platform_id );
  396. DBC_UINT64_LIST *tIDS = m_pDBC->listPlatformModBusRTU( tData->platform_id );
  397. if ( tIDS ) {
  398. for ( DBC_UINT64_LIST::iterator idx=tIDS->begin(); idx != tIDS->end(); idx++ ) {
  399. MODBUS_RTU_INFO *tRTU = m_pDBC->getModBusRTU( *idx );
  400. if ( tRTU ) {
  401. uint8_t bON = ( 1 << tRTU->crossbar[((tData->side == 'U')?MBUS_CROSSBAR_UPPER_TRACK:MBUS_CROSSBAR_BOTTOM_TRACK)] );
  402. uint8_t bOFF = ~( 1 << tRTU->crossbar[((tData->side == 'U')?MBUS_CROSSBAR_UPPER_TRACK:MBUS_CROSSBAR_BOTTOM_TRACK)] );
  403. //Remove Scheduled Signal change
  404. m_mProtector.lock();
  405. for ( V_MODBUS_ACION_ITEMS::iterator idx=m_vActioItems.begin(); idx!=m_vActioItems.end(); ){
  406. //check if shceduled ActionItem is Assigned to Current RTU
  407. if( (*idx)->module_id == tRTU->id ) {
  408. if( (((*idx)->value & bON ) == bON ) || (((*idx)->mask & bOFF ) == bOFF ) ){
  409. free((*idx));
  410. m_vActioItems.erase( idx );
  411. } else idx++;
  412. } else idx++;
  413. };
  414. m_mProtector.unlock();
  415. if ( tSRD->level < m_sManagerData.hazardSignalLevel ) {
  416. //Clear Signal if Set
  417. if ( (tRTU->c_OutBits & bON ) == bON ) {
  418. tRTU->c_OutBits &= bOFF;
  419. int rv = modbus_set_rtu( tRTU->ip, tRTU->port, tRTU->output_base_addr, tRTU->c_OutBits );
  420. if ( rv < 0 ) {
  421. m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_OFFLINE, 0, 0 );
  422. } else {
  423. m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_ONLINE, tRTU->c_InBits, tRTU->c_OutBits );
  424. };
  425. };
  426. } else {
  427. //Set Signal if was Cleared
  428. if ( (tRTU->c_OutBits & bON ) == 0 ) {
  429. tRTU->c_OutBits |= bON;
  430. int rv = modbus_set_rtu( tRTU->ip, tRTU->port, tRTU->output_base_addr, tRTU->c_OutBits );
  431. if ( rv < 0 ) {
  432. m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_OFFLINE, 0, 0 );
  433. } else {
  434. m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_ONLINE, tRTU->c_InBits, tRTU->c_OutBits );
  435. };
  436. };
  437. };
  438. free( tRTU );
  439. };
  440. };
  441. };
  442. free( tData );
  443. };
  444. };
  445. break;
  446. };
  447. case EVT_SRV_MODBUS_TEST_P:{
  448. MODBUS_TEST_DATA *tData = (MODBUS_TEST_DATA *)Evt->m_pEvtData;
  449. if ( tData ) {
  450. DBC_UINT64_LIST *tIDS = m_pDBC->listPlatformModBusRTU( tData->id );
  451. if ( tIDS ) {
  452. for ( DBC_UINT64_LIST::iterator idx=tIDS->begin(); idx != tIDS->end(); idx++ ) {
  453. MODBUS_RTU_INFO *tRTU = m_pDBC->getModBusRTU( *idx );
  454. if ( tRTU ) {
  455. #ifdef DEBUG_EXTRA
  456. char tB[200];
  457. sprintf( tB, "%s:%d 0x%02x 0x%02x", tRTU->ip, tRTU->port, tRTU->output_base_addr, tData->c_OutBits );
  458. LOG( tB );
  459. #endif
  460. int rv = modbus_set_rtu( tRTU->ip, tRTU->port, tRTU->output_base_addr, tData->c_OutBits );
  461. if ( rv < 0 ) {
  462. m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_OFFLINE, 0, 0 );
  463. } else {
  464. m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_ONLINE, tRTU->c_InBits, tData->c_OutBits );
  465. };
  466. free( tRTU );
  467. };
  468. };
  469. };
  470. };
  471. return false; // No need Propogate this Evt
  472. };
  473. case EVT_SRV_MODBUS_TEST_M:{
  474. MODBUS_TEST_DATA *tData = (MODBUS_TEST_DATA *)Evt->m_pEvtData;
  475. if ( tData ) {
  476. MODBUS_RTU_INFO *tRTU = m_pDBC->getModBusRTU( tData->id );
  477. if ( tRTU ) {
  478. #ifdef DEBUG_EXTRA
  479. char tB[200];
  480. sprintf( tB, "%s:%d 0x%02x 0x%02x", tRTU->ip, tRTU->port, tRTU->output_base_addr, tData->c_OutBits );
  481. LOG( tB );
  482. #endif
  483. int rv = modbus_set_rtu( tRTU->ip, tRTU->port, tRTU->output_base_addr, tData->c_OutBits );
  484. if ( rv < 0 ) {
  485. m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_OFFLINE, 0, 0 );
  486. } else {
  487. m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_ONLINE, tRTU->c_InBits, tData->c_OutBits );
  488. };
  489. free( tRTU );
  490. };
  491. };
  492. return false; // No need Propogate this Evt
  493. };
  494. case EVT_SRV_MODULE_HEALTH:{
  495. if ( Evt->m_pEvtData == NULL ) return false;
  496. if ( m_sManagerData.helthSignalLevel ) {
  497. MODULE_HEALTH_STATUS *tData = (MODULE_HEALTH_STATUS *)Evt->m_pEvtData;
  498. if ( tData->status >= m_sManagerData.helthSignalLevel ) {
  499. //MODBUS_RTU_INFO *tRTU = m_pDBC->getModBusRTU_Platform( tData->platform_id );
  500. DBC_UINT64_LIST *tIDS = m_pDBC->listPlatformModBusRTU( tData->platform_id );
  501. if ( tIDS ) {
  502. for ( DBC_UINT64_LIST::iterator idx=tIDS->begin(); idx != tIDS->end(); idx++ ) {
  503. MODBUS_RTU_INFO *tRTU = m_pDBC->getModBusRTU( *idx );
  504. if (tRTU) {
  505. uint8_t bON = ( 1 << tRTU->crossbar[MBUS_CROSSBAR_HEALTH] );
  506. uint8_t bOFF = ~( 1 << tRTU->crossbar[MBUS_CROSSBAR_HEALTH] );
  507. #ifdef DEBUG_EXTRA
  508. char tB[200];
  509. sprintf( tB, "%s:%d 0x%02x 0x%02x", tRTU->ip, tRTU->port, tRTU->output_base_addr, tData->c_OutBits );
  510. LOG( tB );
  511. #endif
  512. //Set Signal if was Cleared
  513. if ( (tRTU->c_OutBits & bON ) == 0 ) {
  514. tRTU->c_OutBits |= bON;
  515. int rv = modbus_set_rtu( tRTU->ip, tRTU->port, tRTU->output_base_addr, tRTU->c_OutBits );
  516. if ( rv < 0 ) {
  517. m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_OFFLINE, 0, 0 );
  518. } else {
  519. m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_ONLINE, tRTU->c_InBits, tRTU->c_OutBits );
  520. };
  521. };
  522. free( tRTU );
  523. };
  524. };
  525. delete( tIDS );
  526. };
  527. } else { //Below Level
  528. //MODBUS_RTU_INFO *tRTU = m_pDBC->getModBusRTU_Platform( tData->platform_id );
  529. DBC_UINT64_LIST *tIDS = m_pDBC->listPlatformModBusRTU( tData->platform_id );
  530. if ( tIDS ) {
  531. for ( DBC_UINT64_LIST::iterator idx=tIDS->begin(); idx != tIDS->end(); idx++ ) {
  532. MODBUS_RTU_INFO *tRTU = m_pDBC->getModBusRTU( *idx );
  533. if ( tRTU ) {
  534. uint8_t bON = ( 1 << tRTU->crossbar[MBUS_CROSSBAR_HEALTH] );
  535. uint8_t bOFF = ~( 1 << tRTU->crossbar[MBUS_CROSSBAR_HEALTH] );
  536. #ifdef DEBUG_EXTRA
  537. char tB[200];
  538. sprintf( tB, "%s:%d 0x%02x 0x%02x", tRTU->ip, tRTU->port, tRTU->output_base_addr, tData->c_OutBits );
  539. LOG( tB );
  540. #endif
  541. //Mitigate Signal if sent
  542. if ( (tRTU->c_OutBits & bON ) == bON ) {
  543. tRTU->c_OutBits &= bOFF;
  544. int rv = modbus_set_rtu( tRTU->ip, tRTU->port, tRTU->output_base_addr, tRTU->c_OutBits );
  545. if ( rv < 0 ) {
  546. m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_OFFLINE, 0, 0 );
  547. } else {
  548. m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_ONLINE, tRTU->c_InBits, tRTU->c_OutBits );
  549. };
  550. };
  551. free( tRTU );
  552. };
  553. };
  554. };
  555. };
  556. };
  557. break;
  558. };
  559. };
  560. return true;
  561. };
  562. #undef LOG
  563. #undef LOG_ERROR