@ -300,19 +300,25 @@ bool servaModBusManager::dispatchEvent( EVENT_CONTAINER *Evt ){
//setup Schedule
SCADA_TRACK_INFO * tData = m_pDBC - > getTrackSCADA ( Evt - > m_nSourceNodeID ) ;
if ( tData ) {
MODBUS_RTU_INFO * tRTU = m_pDBC - > getModBusRTU_Platform ( tData - > platform_id ) ;
if ( tRTU ) {
MODBUS_ACION_ITEM * tAI = ( MODBUS_ACION_ITEM * ) malloc ( sizeof ( MODBUS_ACION_ITEM ) ) ;
if ( tAI ) {
memset ( tAI , 0 , sizeof ( MODBUS_ACION_ITEM ) ) ;
tAI - > scheduled_on = getMilliseconds ( ) + ( m_sManagerData . automaticAlarmRisingTO * 1000 ) ;
tAI - > module_id = tRTU - > id ;
tAI - > value = ( 1 < < tRTU - > crossbar [ ( ( tData - > side = = ' U ' ) ? MBUS_CROSSBAR_UPPER_TRACK : MBUS_CROSSBAR_BOTTOM_TRACK ) ] ) ;
m_mProtector . lock ( ) ;
m_vActioItems . push_back ( tAI ) ;
m_mProtector . unlock ( ) ;
//MODBUS_RTU_INFO *tRTU = m_pDBC->getModBusRTU_Platform( tData->platform_id );
DBC_UINT64_LIST * tIDS = m_pDBC - > listPlatformModBusRTU ( tData - > platform_id ) ;
if ( tIDS ) {
for ( DBC_UINT64_LIST : : iterator idx = tIDS - > begin ( ) ; idx ! = tIDS - > end ( ) ; idx + + ) {
MODBUS_RTU_INFO * tRTU = m_pDBC - > getModBusRTU ( * idx ) ;
if ( tRTU ) {
MODBUS_ACION_ITEM * tAI = ( MODBUS_ACION_ITEM * ) malloc ( sizeof ( MODBUS_ACION_ITEM ) ) ;
if ( tAI ) {
memset ( tAI , 0 , sizeof ( MODBUS_ACION_ITEM ) ) ;
tAI - > scheduled_on = getMilliseconds ( ) + ( m_sManagerData . automaticAlarmRisingTO * 1000 ) ;
tAI - > module_id = tRTU - > id ;
tAI - > value = ( 1 < < tRTU - > crossbar [ ( ( tData - > side = = ' U ' ) ? MBUS_CROSSBAR_UPPER_TRACK : MBUS_CROSSBAR_BOTTOM_TRACK ) ] ) ;
m_mProtector . lock ( ) ;
m_vActioItems . push_back ( tAI ) ;
m_mProtector . unlock ( ) ;
} ;
free ( tRTU ) ;
} ;
} ;
free ( tRTU ) ;
} ;
free ( tData ) ;
} ;
@ -322,36 +328,42 @@ bool servaModBusManager::dispatchEvent( EVENT_CONTAINER *Evt ){
case EVT_SRV_SESSION_ENDED : {
SCADA_TRACK_INFO * tData = m_pDBC - > getTrackSCADA ( Evt - > m_nSourceNodeID ) ;
if ( tData ) {
MODBUS_RTU_INFO * tRTU = m_pDBC - > getModBusRTU_Platform ( tData - > platform_id ) ;
if ( tRTU ) {
uint8_t bON = ( 1 < < tRTU - > crossbar [ ( ( tData - > side = = ' U ' ) ? MBUS_CROSSBAR_UPPER_TRACK : MBUS_CROSSBAR_BOTTOM_TRACK ) ] ) ;
uint8_t bOFF = ~ ( 1 < < tRTU - > crossbar [ ( ( tData - > side = = ' U ' ) ? MBUS_CROSSBAR_UPPER_TRACK : MBUS_CROSSBAR_BOTTOM_TRACK ) ] ) ;
//Remove Scheduled Signal change
m_mProtector . lock ( ) ;
for ( V_MODBUS_ACION_ITEMS : : iterator idx = m_vActioItems . begin ( ) ; idx ! = m_vActioItems . end ( ) ; ) {
if ( ( * idx ) - > module_id = = tRTU - > id ) {
//check if shceduled signal is current
if ( ( ( ( * idx ) - > value & bON ) = = bON ) | | ( ( ( * idx ) - > mask & bOFF ) = = bOFF ) ) {
free ( ( * idx ) ) ;
m_vActioItems . erase ( idx ) ;
} else idx + + ;
//break;
} else idx + + ;
} ;
m_mProtector . unlock ( ) ;
//MODBUS_RTU_INFO *tRTU = m_pDBC->getModBusRTU_Platform( tData->platform_id );
DBC_UINT64_LIST * tIDS = m_pDBC - > listPlatformModBusRTU ( tData - > platform_id ) ;
if ( tIDS ) {
for ( DBC_UINT64_LIST : : iterator idx = tIDS - > begin ( ) ; idx ! = tIDS - > end ( ) ; idx + + ) {
MODBUS_RTU_INFO * tRTU = m_pDBC - > getModBusRTU ( * idx ) ;
if ( tRTU ) {
uint8_t bON = ( 1 < < tRTU - > crossbar [ ( ( tData - > side = = ' U ' ) ? MBUS_CROSSBAR_UPPER_TRACK : MBUS_CROSSBAR_BOTTOM_TRACK ) ] ) ;
uint8_t bOFF = ~ ( 1 < < tRTU - > crossbar [ ( ( tData - > side = = ' U ' ) ? MBUS_CROSSBAR_UPPER_TRACK : MBUS_CROSSBAR_BOTTOM_TRACK ) ] ) ;
//Remove Scheduled Signal change
m_mProtector . lock ( ) ;
for ( V_MODBUS_ACION_ITEMS : : iterator idx = m_vActioItems . begin ( ) ; idx ! = m_vActioItems . end ( ) ; ) {
if ( ( * idx ) - > module_id = = tRTU - > id ) {
//check if shceduled signal is current
if ( ( ( ( * idx ) - > value & bON ) = = bON ) | | ( ( ( * idx ) - > mask & bOFF ) = = bOFF ) ) {
free ( ( * idx ) ) ;
m_vActioItems . erase ( idx ) ;
} else idx + + ;
//break;
} else idx + + ;
} ;
m_mProtector . unlock ( ) ;
//Mitigate Signal if sent
if ( ( tRTU - > c_OutBits & bON ) = = bON ) {
tRTU - > c_OutBits & = bOFF ;
int rv = modbus_set_rtu ( tRTU - > ip , tRTU - > port , tRTU - > output_base_addr , tRTU - > c_OutBits ) ;
if ( rv < 0 ) {
m_pDBC - > updateModBusRTU ( tRTU - > id , MBUS_STATUS_OFFLINE , 0 , 0 ) ;
} else {
m_pDBC - > updateModBusRTU ( tRTU - > id , MBUS_STATUS_ONLINE , tRTU - > c_InBits , tRTU - > c_OutBits ) ;
//Mitigate Signal if sent
if ( ( tRTU - > c_OutBits & bON ) = = bON ) {
tRTU - > c_OutBits & = bOFF ;
int rv = modbus_set_rtu ( tRTU - > ip , tRTU - > port , tRTU - > output_base_addr , tRTU - > c_OutBits ) ;
if ( rv < 0 ) {
m_pDBC - > updateModBusRTU ( tRTU - > id , MBUS_STATUS_OFFLINE , 0 , 0 ) ;
} else {
m_pDBC - > updateModBusRTU ( tRTU - > id , MBUS_STATUS_ONLINE , tRTU - > c_InBits , tRTU - > c_OutBits ) ;
} ;
} ;
free ( tRTU ) ;
} ;
} ;
free ( tRTU ) ;
} ;
free ( tData ) ;
} ;
@ -363,47 +375,53 @@ bool servaModBusManager::dispatchEvent( EVENT_CONTAINER *Evt ){
SESSION_RATE_DATA * tSRD = ( SESSION_RATE_DATA * ) Evt - > m_pEvtData ;
SCADA_TRACK_INFO * tData = m_pDBC - > getTrackSCADA ( tSRD - > track_id ) ;
if ( tData ) {
MODBUS_RTU_INFO * tRTU = m_pDBC - > getModBusRTU_Platform ( tData - > platform_id ) ;
if ( tRTU ) {
uint8_t bON = ( 1 < < tRTU - > crossbar [ ( ( tData - > side = = ' U ' ) ? MBUS_CROSSBAR_UPPER_TRACK : MBUS_CROSSBAR_BOTTOM_TRACK ) ] ) ;
uint8_t bOFF = ~ ( 1 < < tRTU - > crossbar [ ( ( tData - > side = = ' U ' ) ? MBUS_CROSSBAR_UPPER_TRACK : MBUS_CROSSBAR_BOTTOM_TRACK ) ] ) ;
//Remove Scheduled Signal change
m_mProtector . lock ( ) ;
for ( V_MODBUS_ACION_ITEMS : : iterator idx = m_vActioItems . begin ( ) ; idx ! = m_vActioItems . end ( ) ; ) {
//check if shceduled ActionItem is Assigned to Current RTU
if ( ( * idx ) - > module_id = = tRTU - > id ) {
if ( ( ( ( * idx ) - > value & bON ) = = bON ) | | ( ( ( * idx ) - > mask & bOFF ) = = bOFF ) ) {
free ( ( * idx ) ) ;
m_vActioItems . erase ( idx ) ;
} else idx + + ;
} else idx + + ;
} ;
m_mProtector . unlock ( ) ;
if ( tSRD - > level < m_sManagerData . hazardSignalLevel ) {
//Clear Signal if Set
if ( ( tRTU - > c_OutBits & bON ) = = bON ) {
tRTU - > c_OutBits & = bOFF ;
int rv = modbus_set_rtu ( tRTU - > ip , tRTU - > port , tRTU - > output_base_addr , tRTU - > c_OutBits ) ;
if ( rv < 0 ) {
m_pDBC - > updateModBusRTU ( tRTU - > id , MBUS_STATUS_OFFLINE , 0 , 0 ) ;
} else {
m_pDBC - > updateModBusRTU ( tRTU - > id , MBUS_STATUS_ONLINE , tRTU - > c_InBits , tRTU - > c_OutBits ) ;
//MODBUS_RTU_INFO *tRTU = m_pDBC->getModBusRTU_Platform( tData->platform_id );
DBC_UINT64_LIST * tIDS = m_pDBC - > listPlatformModBusRTU ( tData - > platform_id ) ;
if ( tIDS ) {
for ( DBC_UINT64_LIST : : iterator idx = tIDS - > begin ( ) ; idx ! = tIDS - > end ( ) ; idx + + ) {
MODBUS_RTU_INFO * tRTU = m_pDBC - > getModBusRTU ( * idx ) ;
if ( tRTU ) {
uint8_t bON = ( 1 < < tRTU - > crossbar [ ( ( tData - > side = = ' U ' ) ? MBUS_CROSSBAR_UPPER_TRACK : MBUS_CROSSBAR_BOTTOM_TRACK ) ] ) ;
uint8_t bOFF = ~ ( 1 < < tRTU - > crossbar [ ( ( tData - > side = = ' U ' ) ? MBUS_CROSSBAR_UPPER_TRACK : MBUS_CROSSBAR_BOTTOM_TRACK ) ] ) ;
//Remove Scheduled Signal change
m_mProtector . lock ( ) ;
for ( V_MODBUS_ACION_ITEMS : : iterator idx = m_vActioItems . begin ( ) ; idx ! = m_vActioItems . end ( ) ; ) {
//check if shceduled ActionItem is Assigned to Current RTU
if ( ( * idx ) - > module_id = = tRTU - > id ) {
if ( ( ( ( * idx ) - > value & bON ) = = bON ) | | ( ( ( * idx ) - > mask & bOFF ) = = bOFF ) ) {
free ( ( * idx ) ) ;
m_vActioItems . erase ( idx ) ;
} else idx + + ;
} else idx + + ;
} ;
} ;
} else {
//Set Signal if was Cleared
if ( ( tRTU - > c_OutBits & bON ) = = 0 ) {
tRTU - > c_OutBits | = bON ;
int rv = modbus_set_rtu ( tRTU - > ip , tRTU - > port , tRTU - > output_base_addr , tRTU - > c_OutBits ) ;
if ( rv < 0 ) {
m_pDBC - > updateModBusRTU ( tRTU - > id , MBUS_STATUS_OFFLINE , 0 , 0 ) ;
m_mProtector . unlock ( ) ;
if ( tSRD - > level < m_sManagerData . hazardSignalLevel ) {
//Clear Signal if Set
if ( ( tRTU - > c_OutBits & bON ) = = bON ) {
tRTU - > c_OutBits & = bOFF ;
int rv = modbus_set_rtu ( tRTU - > ip , tRTU - > port , tRTU - > output_base_addr , tRTU - > c_OutBits ) ;
if ( rv < 0 ) {
m_pDBC - > updateModBusRTU ( tRTU - > id , MBUS_STATUS_OFFLINE , 0 , 0 ) ;
} else {
m_pDBC - > updateModBusRTU ( tRTU - > id , MBUS_STATUS_ONLINE , tRTU - > c_InBits , tRTU - > c_OutBits ) ;
} ;
} ;
} else {
m_pDBC - > updateModBusRTU ( tRTU - > id , MBUS_STATUS_ONLINE , tRTU - > c_InBits , tRTU - > c_OutBits ) ;
//Set Signal if was Cleared
if ( ( tRTU - > c_OutBits & bON ) = = 0 ) {
tRTU - > c_OutBits | = bON ;
int rv = modbus_set_rtu ( tRTU - > ip , tRTU - > port , tRTU - > output_base_addr , tRTU - > c_OutBits ) ;
if ( rv < 0 ) {
m_pDBC - > updateModBusRTU ( tRTU - > id , MBUS_STATUS_OFFLINE , 0 , 0 ) ;
} else {
m_pDBC - > updateModBusRTU ( tRTU - > id , MBUS_STATUS_ONLINE , tRTU - > c_InBits , tRTU - > c_OutBits ) ;
} ;
} ;
} ;
free ( tRTU ) ;
} ;
} ;
free ( tRTU ) ;
} ;
free ( tData ) ;
} ;
@ -413,20 +431,26 @@ bool servaModBusManager::dispatchEvent( EVENT_CONTAINER *Evt ){
case EVT_SRV_TEST_MODBUS : {
MODBUS_TEST_DATA * tData = ( MODBUS_TEST_DATA * ) Evt - > m_pEvtData ;
if ( tData ) {
MODBUS_RTU_INFO * tRTU = m_pDBC - > getModBusRTU_Platform ( tData - > platform_id ) ;
if ( tRTU ) {
# ifdef DEBUG_EXTRA
char tB [ 200 ] ;
sprintf ( tB , " %s:%d 0x%02x 0x%02x " , tRTU - > ip , tRTU - > port , tRTU - > output_base_addr , tData - > c_OutBits ) ;
LOG ( tB ) ;
# endif
int rv = modbus_set_rtu ( tRTU - > ip , tRTU - > port , tRTU - > output_base_addr , tData - > c_OutBits ) ;
if ( rv < 0 ) {
m_pDBC - > updateModBusRTU ( tRTU - > id , MBUS_STATUS_OFFLINE , 0 , 0 ) ;
} else {
m_pDBC - > updateModBusRTU ( tRTU - > id , MBUS_STATUS_ONLINE , tRTU - > c_InBits , tData - > c_OutBits ) ;
//MODBUS_RTU_INFO *tRTU = m_pDBC->getModBusRTU_Platform( tData->platform_id );
DBC_UINT64_LIST * tIDS = m_pDBC - > listPlatformModBusRTU ( tData - > platform_id ) ;
if ( tIDS ) {
for ( DBC_UINT64_LIST : : iterator idx = tIDS - > begin ( ) ; idx ! = tIDS - > end ( ) ; idx + + ) {
MODBUS_RTU_INFO * tRTU = m_pDBC - > getModBusRTU ( * idx ) ;
if ( tRTU ) {
# ifdef DEBUG_EXTRA
char tB [ 200 ] ;
sprintf ( tB , " %s:%d 0x%02x 0x%02x " , tRTU - > ip , tRTU - > port , tRTU - > output_base_addr , tData - > c_OutBits ) ;
LOG ( tB ) ;
# endif
int rv = modbus_set_rtu ( tRTU - > ip , tRTU - > port , tRTU - > output_base_addr , tData - > c_OutBits ) ;
if ( rv < 0 ) {
m_pDBC - > updateModBusRTU ( tRTU - > id , MBUS_STATUS_OFFLINE , 0 , 0 ) ;
} else {
m_pDBC - > updateModBusRTU ( tRTU - > id , MBUS_STATUS_ONLINE , tRTU - > c_InBits , tData - > c_OutBits ) ;
} ;
free ( tRTU ) ;
} ;
} ;
free ( tRTU ) ;
} ;
} ;
return false ; // No need Propogate this Evt
@ -436,48 +460,61 @@ bool servaModBusManager::dispatchEvent( EVENT_CONTAINER *Evt ){
if ( m_sManagerData . helthSignalLevel ) {
MODULE_HEALTH_STATUS * tData = ( MODULE_HEALTH_STATUS * ) Evt - > m_pEvtData ;
if ( tData - > status > = m_sManagerData . helthSignalLevel ) {
MODBUS_RTU_INFO * tRTU = m_pDBC - > getModBusRTU_Platform ( tData - > platform_id ) ;
if ( tRTU ) {
uint8_t bON = ( 1 < < tRTU - > crossbar [ MBUS_CROSSBAR_HEALTH ] ) ;
uint8_t bOFF = ~ ( 1 < < tRTU - > crossbar [ MBUS_CROSSBAR_HEALTH ] ) ;
# ifdef DEBUG_EXTRA
char tB [ 200 ] ;
sprintf ( tB , " %s:%d 0x%02x 0x%02x " , tRTU - > ip , tRTU - > port , tRTU - > output_base_addr , tData - > c_OutBits ) ;
LOG ( tB ) ;
# endif
//Set Signal if was Cleared
if ( ( tRTU - > c_OutBits & bON ) = = 0 ) {
tRTU - > c_OutBits | = bON ;
int rv = modbus_set_rtu ( tRTU - > ip , tRTU - > port , tRTU - > output_base_addr , tRTU - > c_OutBits ) ;
if ( rv < 0 ) {
m_pDBC - > updateModBusRTU ( tRTU - > id , MBUS_STATUS_OFFLINE , 0 , 0 ) ;
} else {
m_pDBC - > updateModBusRTU ( tRTU - > id , MBUS_STATUS_ONLINE , tRTU - > c_InBits , tRTU - > c_OutBits ) ;
//MODBUS_RTU_INFO *tRTU = m_pDBC->getModBusRTU_Platform( tData->platform_id );
DBC_UINT64_LIST * tIDS = m_pDBC - > listPlatformModBusRTU ( tData - > platform_id ) ;
if ( tIDS ) {
for ( DBC_UINT64_LIST : : iterator idx = tIDS - > begin ( ) ; idx ! = tIDS - > end ( ) ; idx + + ) {
MODBUS_RTU_INFO * tRTU = m_pDBC - > getModBusRTU ( * idx ) ;
if ( tRTU ) {
uint8_t bON = ( 1 < < tRTU - > crossbar [ MBUS_CROSSBAR_HEALTH ] ) ;
uint8_t bOFF = ~ ( 1 < < tRTU - > crossbar [ MBUS_CROSSBAR_HEALTH ] ) ;
# ifdef DEBUG_EXTRA
char tB [ 200 ] ;
sprintf ( tB , " %s:%d 0x%02x 0x%02x " , tRTU - > ip , tRTU - > port , tRTU - > output_base_addr , tData - > c_OutBits ) ;
LOG ( tB ) ;
# endif
//Set Signal if was Cleared
if ( ( tRTU - > c_OutBits & bON ) = = 0 ) {
tRTU - > c_OutBits | = bON ;
int rv = modbus_set_rtu ( tRTU - > ip , tRTU - > port , tRTU - > output_base_addr , tRTU - > c_OutBits ) ;
if ( rv < 0 ) {
m_pDBC - > updateModBusRTU ( tRTU - > id , MBUS_STATUS_OFFLINE , 0 , 0 ) ;
} else {
m_pDBC - > updateModBusRTU ( tRTU - > id , MBUS_STATUS_ONLINE , tRTU - > c_InBits , tRTU - > c_OutBits ) ;
} ;
} ;
free ( tRTU ) ;
} ;
} ;
free ( tRTU ) ;
delete ( tIDS ) ;
} ;
} else { //Below Level
MODBUS_RTU_INFO * tRTU = m_pDBC - > getModBusRTU_Platform ( tData - > platform_id ) ;
if ( tRTU ) {
uint8_t bON = ( 1 < < tRTU - > crossbar [ MBUS_CROSSBAR_HEALTH ] ) ;
uint8_t bOFF = ~ ( 1 < < tRTU - > crossbar [ MBUS_CROSSBAR_HEALTH ] ) ;
# ifdef DEBUG_EXTRA
char tB [ 200 ] ;
sprintf ( tB , " %s:%d 0x%02x 0x%02x " , tRTU - > ip , tRTU - > port , tRTU - > output_base_addr , tData - > c_OutBits ) ;
LOG ( tB ) ;
# endif
//Mitigate Signal if sent
if ( ( tRTU - > c_OutBits & bON ) = = bON ) {
tRTU - > c_OutBits & = bOFF ;
int rv = modbus_set_rtu ( tRTU - > ip , tRTU - > port , tRTU - > output_base_addr , tRTU - > c_OutBits ) ;
if ( rv < 0 ) {
m_pDBC - > updateModBusRTU ( tRTU - > id , MBUS_STATUS_OFFLINE , 0 , 0 ) ;
} else {
m_pDBC - > updateModBusRTU ( tRTU - > id , MBUS_STATUS_ONLINE , tRTU - > c_InBits , tRTU - > c_OutBits ) ;
//MODBUS_RTU_INFO *tRTU = m_pDBC->getModBusRTU_Platform( tData->platform_id );
DBC_UINT64_LIST * tIDS = m_pDBC - > listPlatformModBusRTU ( tData - > platform_id ) ;
if ( tIDS ) {
for ( DBC_UINT64_LIST : : iterator idx = tIDS - > begin ( ) ; idx ! = tIDS - > end ( ) ; idx + + ) {
MODBUS_RTU_INFO * tRTU = m_pDBC - > getModBusRTU ( * idx ) ;
if ( tRTU ) {
uint8_t bON = ( 1 < < tRTU - > crossbar [ MBUS_CROSSBAR_HEALTH ] ) ;
uint8_t bOFF = ~ ( 1 < < tRTU - > crossbar [ MBUS_CROSSBAR_HEALTH ] ) ;
# ifdef DEBUG_EXTRA
char tB [ 200 ] ;
sprintf ( tB , " %s:%d 0x%02x 0x%02x " , tRTU - > ip , tRTU - > port , tRTU - > output_base_addr , tData - > c_OutBits ) ;
LOG ( tB ) ;
# endif
//Mitigate Signal if sent
if ( ( tRTU - > c_OutBits & bON ) = = bON ) {
tRTU - > c_OutBits & = bOFF ;
int rv = modbus_set_rtu ( tRTU - > ip , tRTU - > port , tRTU - > output_base_addr , tRTU - > c_OutBits ) ;
if ( rv < 0 ) {
m_pDBC - > updateModBusRTU ( tRTU - > id , MBUS_STATUS_OFFLINE , 0 , 0 ) ;
} else {
m_pDBC - > updateModBusRTU ( tRTU - > id , MBUS_STATUS_ONLINE , tRTU - > c_InBits , tRTU - > c_OutBits ) ;
} ;
} ;
free ( tRTU ) ;
} ;
} ;
free ( tRTU ) ;
} ;
} ;
} ;