|
|
|
@ -222,61 +222,264 @@ void servaModBusManager::m_fThreadProc( void ){ |
|
|
|
if ( ((*idx)->c_InBits & ( 1 << bi ) ) == ( 1 << bi ) ) { |
|
|
|
//Bit is set
|
|
|
|
if ( (*idx)->crossbar[4+bi] ) { // Assigned Action For Bit
|
|
|
|
uint64_t cTID = 0; |
|
|
|
SCADA_TRACK_INFO tData; |
|
|
|
tData.platform_id = (*idx)->platform_id; |
|
|
|
|
|
|
|
if ( (*idx)->crossbar[4+bi] == 1 || (*idx)->crossbar[4+bi] == 3 ) tData.side = 'U'; |
|
|
|
if ( (*idx)->crossbar[4+bi] == 2 || (*idx)->crossbar[4+bi] == 3 ) tData.side = 'B'; |
|
|
|
|
|
|
|
cTID = m_pDBC->getTrackIdByPlatform((*idx)->platform_id, tData.side ); |
|
|
|
|
|
|
|
if ( cTID ) { |
|
|
|
|
|
|
|
SESSION_DATA *tSData = servaSessionManager::getInstance()->getActiveSessionByTrack( cTID ); |
|
|
|
|
|
|
|
if ( tSData ) { |
|
|
|
//Rate Session
|
|
|
|
m_pDBC->updateSessionStatus( tSData->db_id, 1 ); |
|
|
|
// Create Event?? or just Disable Scada Signal
|
|
|
|
DBC_UINT64_LIST *tIDS = m_pDBC->listPlatformModBusRTU( tData.platform_id ); |
|
|
|
// check functionality
|
|
|
|
switch ( (*idx)->crossbar[4 + bi] ) { |
|
|
|
case 1:{//Upper Track
|
|
|
|
uint64_t cTID = m_pDBC->getTrackIdByPlatform( (*idx)->platform_id, 'U' ); |
|
|
|
if ( cTID ) { |
|
|
|
SESSION_DATA *tSData = servaSessionManager::getInstance()->getActiveSessionByTrack( cTID ); |
|
|
|
if ( tSData ) { |
|
|
|
//Rate Session
|
|
|
|
m_pDBC->updateSessionStatus( tSData->db_id, 1 ); |
|
|
|
// Create Event?? or just Disable Scada Signal
|
|
|
|
DBC_UINT64_LIST *tIDS = m_pDBC->listPlatformModBusRTU( (*idx)->platform_id ); |
|
|
|
if ( tIDS ) { |
|
|
|
for ( DBC_UINT64_LIST::iterator idd=tIDS->begin(); idd != tIDS->end(); idd++ ) { |
|
|
|
MODBUS_RTU_INFO *tRTU = m_pDBC->getModBusRTU( *idd ); |
|
|
|
if ( tRTU ) { |
|
|
|
uint8_t bON = ( 1 << tRTU->crossbar[MBUS_CROSSBAR_UPPER_TRACK] ); |
|
|
|
uint8_t bOFF = ~( 1 << tRTU->crossbar[MBUS_CROSSBAR_UPPER_TRACK] ); |
|
|
|
//Remove Scheduled Signal change
|
|
|
|
m_mProtector.lock(); |
|
|
|
for ( V_MODBUS_ACION_ITEMS::iterator idz=m_vActioItems.begin(); idz!=m_vActioItems.end(); ){ |
|
|
|
if( (*idz)->module_id == tRTU->id ) { |
|
|
|
//check if shceduled signal is current
|
|
|
|
if( (((*idz)->value & bON ) == bON ) || (((*idz)->mask & bOFF ) == bOFF ) ){ |
|
|
|
free((*idx)); |
|
|
|
m_vActioItems.erase( idz ); |
|
|
|
} else idz++; |
|
|
|
//break;
|
|
|
|
} else idz++; |
|
|
|
}; |
|
|
|
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 ); |
|
|
|
}; |
|
|
|
}; |
|
|
|
free( tRTU ); |
|
|
|
}; |
|
|
|
}; |
|
|
|
delete( tIDS ); |
|
|
|
}; |
|
|
|
free( tSData ); |
|
|
|
}; |
|
|
|
}; |
|
|
|
break; |
|
|
|
}; |
|
|
|
case 2:{//Bottom Track
|
|
|
|
uint64_t cTID = m_pDBC->getTrackIdByPlatform( (*idx)->platform_id, 'B' ); |
|
|
|
if ( cTID ) { |
|
|
|
SESSION_DATA *tSData = servaSessionManager::getInstance()->getActiveSessionByTrack( cTID ); |
|
|
|
if ( tSData ) { |
|
|
|
//Rate Session
|
|
|
|
m_pDBC->updateSessionStatus( tSData->db_id, 1 ); |
|
|
|
// Create Event?? or just Disable Scada Signal
|
|
|
|
DBC_UINT64_LIST *tIDS = m_pDBC->listPlatformModBusRTU( (*idx)->platform_id ); |
|
|
|
if ( tIDS ) { |
|
|
|
for ( DBC_UINT64_LIST::iterator idd=tIDS->begin(); idd != tIDS->end(); idd++ ) { |
|
|
|
MODBUS_RTU_INFO *tRTU = m_pDBC->getModBusRTU( *idd ); |
|
|
|
if ( tRTU ) { |
|
|
|
uint8_t bON = ( 1 << tRTU->crossbar[MBUS_CROSSBAR_BOTTOM_TRACK] ); |
|
|
|
uint8_t bOFF = ~( 1 << tRTU->crossbar[MBUS_CROSSBAR_BOTTOM_TRACK] ); |
|
|
|
//Remove Scheduled Signal change
|
|
|
|
m_mProtector.lock(); |
|
|
|
for ( V_MODBUS_ACION_ITEMS::iterator idz=m_vActioItems.begin(); idz!=m_vActioItems.end(); ){ |
|
|
|
if( (*idz)->module_id == tRTU->id ) { |
|
|
|
//check if shceduled signal is current
|
|
|
|
if( (((*idz)->value & bON ) == bON ) || (((*idz)->mask & bOFF ) == bOFF ) ){ |
|
|
|
free((*idx)); |
|
|
|
m_vActioItems.erase( idz ); |
|
|
|
} else idz++; |
|
|
|
//break;
|
|
|
|
} else idz++; |
|
|
|
}; |
|
|
|
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 ); |
|
|
|
}; |
|
|
|
}; |
|
|
|
free( tRTU ); |
|
|
|
}; |
|
|
|
}; |
|
|
|
delete( tIDS ); |
|
|
|
}; |
|
|
|
free( tSData ); |
|
|
|
}; |
|
|
|
}; |
|
|
|
break; |
|
|
|
}; |
|
|
|
case 3:{//Both Tracks
|
|
|
|
uint64_t cTID = m_pDBC->getTrackIdByPlatform( (*idx)->platform_id, 'U' ); |
|
|
|
if ( cTID ) { |
|
|
|
SESSION_DATA *tSData = servaSessionManager::getInstance()->getActiveSessionByTrack( cTID ); |
|
|
|
if ( tSData ) { |
|
|
|
//Rate Session
|
|
|
|
m_pDBC->updateSessionStatus( tSData->db_id, 1 ); |
|
|
|
// Create Event?? or just Disable Scada Signal
|
|
|
|
DBC_UINT64_LIST *tIDS = m_pDBC->listPlatformModBusRTU( (*idx)->platform_id ); |
|
|
|
if ( tIDS ) { |
|
|
|
for ( DBC_UINT64_LIST::iterator idd=tIDS->begin(); idd != tIDS->end(); idd++ ) { |
|
|
|
MODBUS_RTU_INFO *tRTU = m_pDBC->getModBusRTU( *idd ); |
|
|
|
if ( tRTU ) { |
|
|
|
uint8_t bON = ( 1 << tRTU->crossbar[MBUS_CROSSBAR_UPPER_TRACK] ); |
|
|
|
uint8_t bOFF = ~( 1 << tRTU->crossbar[MBUS_CROSSBAR_UPPER_TRACK] ); |
|
|
|
//Remove Scheduled Signal change
|
|
|
|
m_mProtector.lock(); |
|
|
|
for ( V_MODBUS_ACION_ITEMS::iterator idz=m_vActioItems.begin(); idz!=m_vActioItems.end(); ){ |
|
|
|
if( (*idz)->module_id == tRTU->id ) { |
|
|
|
//check if shceduled signal is current
|
|
|
|
if( (((*idz)->value & bON ) == bON ) || (((*idz)->mask & bOFF ) == bOFF ) ){ |
|
|
|
free((*idx)); |
|
|
|
m_vActioItems.erase( idz ); |
|
|
|
} else idz++; |
|
|
|
//break;
|
|
|
|
} else idz++; |
|
|
|
}; |
|
|
|
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 ); |
|
|
|
}; |
|
|
|
}; |
|
|
|
free( tRTU ); |
|
|
|
}; |
|
|
|
}; |
|
|
|
delete( tIDS ); |
|
|
|
}; |
|
|
|
free( tSData ); |
|
|
|
}; |
|
|
|
}; |
|
|
|
cTID = m_pDBC->getTrackIdByPlatform( (*idx)->platform_id, 'B' ); |
|
|
|
if ( cTID ) { |
|
|
|
SESSION_DATA *tSData = servaSessionManager::getInstance()->getActiveSessionByTrack( cTID ); |
|
|
|
if ( tSData ) { |
|
|
|
//Rate Session
|
|
|
|
m_pDBC->updateSessionStatus( tSData->db_id, 1 ); |
|
|
|
// Create Event?? or just Disable Scada Signal
|
|
|
|
DBC_UINT64_LIST *tIDS = m_pDBC->listPlatformModBusRTU( (*idx)->platform_id ); |
|
|
|
if ( tIDS ) { |
|
|
|
for ( DBC_UINT64_LIST::iterator idd=tIDS->begin(); idd != tIDS->end(); idd++ ) { |
|
|
|
MODBUS_RTU_INFO *tRTU = m_pDBC->getModBusRTU( *idd ); |
|
|
|
if ( tRTU ) { |
|
|
|
uint8_t bON = ( 1 << tRTU->crossbar[MBUS_CROSSBAR_BOTTOM_TRACK] ); |
|
|
|
uint8_t bOFF = ~( 1 << tRTU->crossbar[MBUS_CROSSBAR_BOTTOM_TRACK] ); |
|
|
|
//Remove Scheduled Signal change
|
|
|
|
m_mProtector.lock(); |
|
|
|
for ( V_MODBUS_ACION_ITEMS::iterator idz=m_vActioItems.begin(); idz!=m_vActioItems.end(); ){ |
|
|
|
if( (*idz)->module_id == tRTU->id ) { |
|
|
|
//check if shceduled signal is current
|
|
|
|
if( (((*idz)->value & bON ) == bON ) || (((*idz)->mask & bOFF ) == bOFF ) ){ |
|
|
|
free((*idx)); |
|
|
|
m_vActioItems.erase( idz ); |
|
|
|
} else idz++; |
|
|
|
//break;
|
|
|
|
} else idz++; |
|
|
|
}; |
|
|
|
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 ); |
|
|
|
}; |
|
|
|
}; |
|
|
|
free( tRTU ); |
|
|
|
}; |
|
|
|
}; |
|
|
|
delete( tIDS ); |
|
|
|
}; |
|
|
|
free( tSData ); |
|
|
|
}; |
|
|
|
}; |
|
|
|
break; |
|
|
|
}; |
|
|
|
case 4:{ // Set TCC AUX signal
|
|
|
|
// ?? and create Scheduled mitigation of the signal ??
|
|
|
|
DBC_UINT64_LIST *tIDS = m_pDBC->listPlatformModBusRTU( (*idx)->platform_id ); |
|
|
|
if ( tIDS ) { |
|
|
|
for ( DBC_UINT64_LIST::iterator idd=tIDS->begin(); idd != tIDS->end(); idd++ ) { |
|
|
|
MODBUS_RTU_INFO *tRTU = m_pDBC->getModBusRTU( *idd ); |
|
|
|
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 idz=m_vActioItems.begin(); idz!=m_vActioItems.end(); ){ |
|
|
|
if( (*idz)->module_id == tRTU->id ) { |
|
|
|
//check if shceduled signal is current
|
|
|
|
if( (((*idz)->value & bON ) == bON ) || (((*idz)->mask & bOFF ) == bOFF ) ){ |
|
|
|
free((*idx)); |
|
|
|
m_vActioItems.erase( idz ); |
|
|
|
// check if TCC Module
|
|
|
|
if ( tRTU->func == 3 ) { |
|
|
|
uint8_t bON = (1 << tRTU->crossbar[MBUS_CROSSBAR_AUX]); |
|
|
|
uint8_t bOFF = ~( 1 << tRTU->crossbar[MBUS_CROSSBAR_AUX] ); |
|
|
|
//Remove Scheduled Signal change
|
|
|
|
m_mProtector.lock(); |
|
|
|
for ( V_MODBUS_ACION_ITEMS::iterator idz=m_vActioItems.begin(); idz!=m_vActioItems.end(); ){ |
|
|
|
if( (*idz)->module_id == tRTU->id ) { |
|
|
|
//check if shceduled signal is current
|
|
|
|
if( (((*idz)->value & bON ) == bON ) || (((*idz)->mask & bOFF ) == bOFF ) ){ |
|
|
|
free((*idx)); |
|
|
|
m_vActioItems.erase( idz ); |
|
|
|
} else idz++; |
|
|
|
//break;
|
|
|
|
} else idz++; |
|
|
|
//break;
|
|
|
|
} else idz++; |
|
|
|
}; |
|
|
|
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 ); |
|
|
|
}; |
|
|
|
m_mProtector.unlock(); |
|
|
|
//Skeep Signal if Allready Set
|
|
|
|
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 ); |
|
|
|
//Create Action Item Do disable Signal
|
|
|
|
MODBUS_ACION_ITEM *tAI = (MODBUS_ACION_ITEM *)malloc(sizeof(MODBUS_ACION_ITEM)); |
|
|
|
if ( tAI ) { |
|
|
|
memset( tAI, 0, sizeof(MODBUS_ACION_ITEM) ); |
|
|
|
//?? How long
|
|
|
|
tAI->scheduled_on = getMilliseconds() + ( m_sManagerData.automaticAlarmRisingTO * 1000 ); |
|
|
|
tAI->module_id = tRTU->id; |
|
|
|
tAI->value = ( 1 << tRTU->crossbar[MBUS_CROSSBAR_AUX] ); |
|
|
|
m_mProtector.lock(); |
|
|
|
m_vActioItems.push_back( tAI ); |
|
|
|
m_mProtector.unlock(); |
|
|
|
}; |
|
|
|
}; |
|
|
|
} else { |
|
|
|
m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_ONLINE, tRTU->c_InBits, tRTU->c_OutBits ); |
|
|
|
// Only Create New Action Item to Mitigate
|
|
|
|
// That will propogate delay because old Mitigation item was deleted
|
|
|
|
MODBUS_ACION_ITEM *tAI = (MODBUS_ACION_ITEM *)malloc(sizeof(MODBUS_ACION_ITEM)); |
|
|
|
if ( tAI ) { |
|
|
|
memset( tAI, 0, sizeof(MODBUS_ACION_ITEM) ); |
|
|
|
//?? How long
|
|
|
|
tAI->scheduled_on = getMilliseconds() + ( m_sManagerData.automaticAlarmRisingTO * 1000 ); |
|
|
|
tAI->module_id = tRTU->id; |
|
|
|
tAI->value = ( 1 << tRTU->crossbar[MBUS_CROSSBAR_AUX] ); |
|
|
|
m_mProtector.lock(); |
|
|
|
m_vActioItems.push_back( tAI ); |
|
|
|
m_mProtector.unlock(); |
|
|
|
}; |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
free( tRTU ); |
|
|
|
}; |
|
|
|
}; |
|
|
|
delete( tIDS ); |
|
|
|
}; |
|
|
|
free( tSData ); |
|
|
|
break; |
|
|
|
}; |
|
|
|
}; |
|
|
|
}; |
|
|
|
|