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.

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