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.
935 lines
49 KiB
935 lines
49 KiB
#include "servaModBusManager.h"
|
|
#include "servaKernel.h"
|
|
|
|
#define LOG( X ) m_pLog->Log( "MODBUS", this, X )
|
|
|
|
#ifdef DEBUG
|
|
#define LOG_ERROR( X ) {\
|
|
char _tBfrErrMSG2w34[256];\
|
|
sprintf( _tBfrErrMSG2w34, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
|
|
m_pLog->Log( "MODBUS", this, _LOG_ERROR, _tBfrErrMSG2w34 );\
|
|
}
|
|
#else
|
|
#define LOG_ERROR( X ) m_pLog->Log( "MODBUS", this, _LOG_ERROR, X );
|
|
#endif
|
|
|
|
servaModBusManager *servaModBusManager::_instance = NULL;
|
|
|
|
servaModBusManager *servaModBusManager::getInstance(){
|
|
if ( _instance == NULL ) {
|
|
_instance = new servaModBusManager();
|
|
};
|
|
return _instance;
|
|
};
|
|
|
|
servaModBusManager::servaModBusManager():basicThread(),basicValidatable(){
|
|
m_bStage = MBUS_STAGE_INIT;
|
|
m_pDBC = NULL;
|
|
m_pLog = basicLogCollection::getInstance();
|
|
m_pMaster = servaKernel::getInstance();
|
|
if ( m_pMaster == NULL ) return;
|
|
m_pDBC = m_pMaster->getDBC();
|
|
memset( &m_sManagerData, 0, sizeof( MODBUS_MANAGER_DATA ));
|
|
};
|
|
|
|
servaModBusManager::~servaModBusManager(){
|
|
waitThreadEnd( true );
|
|
// The kernel deletes this singleton on restart: the next getInstance() must create a new one
|
|
// (it returned the deleted object before)
|
|
if ( _instance == this ) _instance = NULL;
|
|
m_mProtector.lock();
|
|
for ( V_MODBUS_ACION_ITEMS::iterator idx = m_vActioItems.begin(); idx != m_vActioItems.end(); idx++ ) free( *idx );
|
|
m_vActioItems.clear();
|
|
m_mProtector.unlock();
|
|
if (m_vRtuUnits.size()) {
|
|
for( V_MODBUS_RTU_INFO::iterator idx=m_vRtuUnits.begin(); idx != m_vRtuUnits.end(); idx++ ) {
|
|
free( *idx );
|
|
};
|
|
m_vRtuUnits.clear();
|
|
};
|
|
};
|
|
|
|
bool servaModBusManager::create(){
|
|
if( m_pMaster == NULL ) return false;
|
|
if( m_pDBC == NULL ) return false;
|
|
MODBUS_MANAGER_DATA * tData = m_pDBC->getModBusManagerData();
|
|
if ( tData ) {
|
|
memcpy( &m_sManagerData, tData, sizeof( MODBUS_MANAGER_DATA ) );
|
|
free( tData );
|
|
};
|
|
m_bStage = MBUS_STAGE_INIT;
|
|
return init();
|
|
};
|
|
|
|
int servaModBusManager::modbus_get_rtu( CSTR ip, uint16_t port, uint8_t addr, uint8_t &status ){
|
|
uint8_t buf[16];
|
|
modbus_t * ctx; // Modbus context pointer
|
|
int rc; // Return code for error checking
|
|
// Create a new Modbus TCP context for a slave device
|
|
ctx = modbus_new_tcp( ip, port );
|
|
if (ctx == NULL) {
|
|
#ifdef DEBUG_EXTRA
|
|
LOG( "Unable to create the Modbus context" );
|
|
#endif
|
|
return -1;
|
|
};
|
|
// Connect to the Modbus TCP slave device
|
|
rc = modbus_connect(ctx);
|
|
if (rc == -1) {
|
|
#ifdef DEBUG_EXTRA
|
|
char tB[256];
|
|
sprintf(tB, "Connection failed: %s", modbus_strerror(errno));
|
|
LOG( tB );
|
|
#endif
|
|
modbus_free(ctx);
|
|
return -1;
|
|
};
|
|
rc = modbus_read_bits(ctx, addr, 8, buf ); //0x10
|
|
if( rc == -1 ) {
|
|
#ifdef DEBUG_EXTRA
|
|
char tB[256];
|
|
sprintf(tB, "Write register failed: %s", modbus_strerror(errno));
|
|
LOG( tB );
|
|
#endif
|
|
} else {
|
|
status = modbus_get_byte_from_bits( buf, 0, 8 );
|
|
};
|
|
// Close the Modbus TCP connection and free the context
|
|
modbus_close(ctx);
|
|
modbus_free(ctx);
|
|
return rc;
|
|
};
|
|
int servaModBusManager::modbus_set_rtu( CSTR ip, uint16_t port, uint8_t addr, uint8_t data ){
|
|
uint8_t bits[8];
|
|
modbus_t * ctx; // Modbus context pointer
|
|
int rc; // Return code for error checking
|
|
// Create a new Modbus TCP context for a slave device
|
|
ctx = modbus_new_tcp( ip, port );
|
|
if (ctx == NULL) {
|
|
#ifdef DEBUG_EXTRA
|
|
LOG( "Unable to create the Modbus context");
|
|
#endif
|
|
return -1;
|
|
};
|
|
// Connect to the Modbus TCP slave device
|
|
rc = modbus_connect(ctx);
|
|
if (rc == -1) {
|
|
#ifdef DEBUG_EXTRA
|
|
char tB[256];
|
|
sprintf(tB, "Connection failed: %s", modbus_strerror(errno));
|
|
LOG( tB );
|
|
#endif
|
|
modbus_free(ctx);
|
|
return -1;
|
|
};
|
|
modbus_set_bits_from_byte( bits, 0, data );
|
|
rc = modbus_write_bits(ctx, addr, 8, bits ); //0x10
|
|
if (rc == -1) {
|
|
#ifdef DEBUG_EXTRA
|
|
char tB[256];
|
|
sprintf(tB, "Write register failed: %s", modbus_strerror(errno));
|
|
LOG( tB );
|
|
#endif
|
|
};
|
|
// Close the Modbus TCP connection and free the context
|
|
modbus_close(ctx);
|
|
modbus_free(ctx);
|
|
return rc;
|
|
};
|
|
int servaModBusManager::modbus_set_rtu_bit( CSTR ip, uint16_t port, uint8_t addr, uint8_t bit ){
|
|
modbus_t * ctx; // Modbus context pointer
|
|
int rc; // Return code for error checking
|
|
// Create a new Modbus TCP context for a slave device
|
|
ctx = modbus_new_tcp( ip, port );
|
|
if (ctx == NULL) {
|
|
#ifdef DEBUG_EXTRA
|
|
LOG( "Unable to create the Modbus context");
|
|
#endif
|
|
return -1;
|
|
};
|
|
// Connect to the Modbus TCP slave device
|
|
rc = modbus_connect(ctx);
|
|
if (rc == -1) {
|
|
#ifdef DEBUG_EXTRA
|
|
char tB[256];
|
|
sprintf(tB, "Connection failed: %s", modbus_strerror(errno));
|
|
LOG( tB );
|
|
#endif
|
|
modbus_free(ctx);
|
|
return -1;
|
|
};
|
|
rc = modbus_write_bit(ctx, addr, bit ); //0x10
|
|
if (rc == -1) {
|
|
#ifdef DEBUG_EXTRA
|
|
char tB[256];
|
|
sprintf(tB, "Write register failed: %s", modbus_strerror(errno));
|
|
LOG( tB );
|
|
#endif
|
|
};
|
|
// Close the Modbus TCP connection and free the context
|
|
modbus_close(ctx);
|
|
modbus_free(ctx);
|
|
return rc;
|
|
};
|
|
|
|
void servaModBusManager::m_fThreadProc( void ){
|
|
m_bThreadStatus = TH_WORKING;
|
|
uint32_t oss = time(NULL);
|
|
while ( m_bThreadStatus < TH_ENDED ) {
|
|
uint32_t css = time(NULL);
|
|
switch (m_bStage) {
|
|
case MBUS_STAGE_INIT:{
|
|
#ifdef DEBUG
|
|
LOG("Clear Loadded ModBus Modules");
|
|
#endif
|
|
if (m_vRtuUnits.size()) {
|
|
for( V_MODBUS_RTU_INFO::iterator idx=m_vRtuUnits.begin(); idx != m_vRtuUnits.end(); idx++ ) {
|
|
free( *idx );
|
|
};
|
|
m_vRtuUnits.clear();
|
|
};
|
|
#ifdef DEBUG
|
|
LOG("Load Enabled ModBus Modules");
|
|
#endif
|
|
DBC_UINT64_LIST *tList = m_pDBC->listModBusRTUs( 0, 'E' );
|
|
if ( tList ) {
|
|
if ( tList->size() ) {
|
|
for ( DBC_UINT64_LIST::iterator idx = tList->begin(); idx != tList->end(); idx++ ) {
|
|
MODBUS_RTU_INFO *tData = m_pDBC->getModBusRTU( *idx );
|
|
if ( tData ) {
|
|
m_vRtuUnits.push_back( tData );
|
|
};
|
|
};
|
|
};
|
|
delete(tList);
|
|
#ifdef DEBUG
|
|
LOG("Load ModBus Modules Done");
|
|
#endif
|
|
} else LOG("No ModBus Modules Defined");
|
|
m_bStage = MBUS_STAGE_SYNC;
|
|
break;
|
|
};
|
|
case MBUS_STAGE_SYNC:{
|
|
#ifdef DEBUG_EXTRA
|
|
LOG("Sync ModBus Modules");
|
|
#endif
|
|
if( m_vRtuUnits.size() ){
|
|
for( V_MODBUS_RTU_INFO::iterator idx = m_vRtuUnits.begin(); idx!= m_vRtuUnits.end(); idx++ ){
|
|
int rv = modbus_get_rtu((*idx)->ip, (*idx)->port, (*idx)->output_base_addr, (*idx)->c_OutBits );
|
|
if ( rv != -1 ) {
|
|
rv = modbus_get_rtu((*idx)->ip, (*idx)->port, (*idx)->input_base_addr, (*idx)->c_InBits );
|
|
if ( rv != -1 ) {
|
|
(*idx)->status = MBUS_STATUS_ONLINE;
|
|
m_pDBC->updateModBusRTU( (*idx)->id, (*idx)->status, (*idx)->c_InBits, (*idx)->c_OutBits );
|
|
//Input Handling
|
|
if ( (*idx)->c_InBits ) {
|
|
bool had_some_assignments = false;
|
|
//Testing Bits
|
|
for ( uint8_t bi = 0; bi < 4; bi++ ) {
|
|
if ( ((*idx)->c_InBits & ( 1 << bi ) ) == ( 1 << bi ) ) {
|
|
//Bit is set
|
|
if( (*idx)->crossbar[4+bi] ) { // Assigned Action For Bit
|
|
// check functionality
|
|
switch ( (*idx)->crossbar[4 + bi] ) {
|
|
case MBUS_FUNC_ACK_UPPER_TRACK:{
|
|
LOG("In: Upper Track ACK");
|
|
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, SCADA_ACK_SESSION_RATING );
|
|
SESSION_RATE_DATA *tData = (SESSION_RATE_DATA *) malloc( sizeof( SESSION_RATE_DATA ) );
|
|
if ( tData ) {
|
|
tData->level = SCADA_ACK_SESSION_RATING;
|
|
tData->track_id = (*idx)->platform_id;
|
|
EVENT_CONTAINER * tEvt = createBasicEvent( 0, EVT_CLASS_SYSTEM, EVT_SRV_SESSION_RATED, NULL, NULL, tData );
|
|
if ( tEvt ) {
|
|
basicEventHeap::getInstance()->broadcast( tEvt );
|
|
} else {
|
|
free( tData );
|
|
};
|
|
};
|
|
free( tSData );
|
|
};
|
|
};
|
|
break;
|
|
};
|
|
case MBUS_FUNC_ACK_BOTTOM_TRACK:{//Bottom Track
|
|
LOG("In: Bottom Track ACK");
|
|
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, SCADA_ACK_SESSION_RATING );
|
|
SESSION_RATE_DATA *tData = (SESSION_RATE_DATA *) malloc( sizeof( SESSION_RATE_DATA ) );
|
|
if ( tData ) {
|
|
tData->level = SCADA_ACK_SESSION_RATING;
|
|
tData->track_id = (*idx)->platform_id;
|
|
EVENT_CONTAINER * tEvt = createBasicEvent( 0, EVT_CLASS_SYSTEM, EVT_SRV_SESSION_RATED, NULL, NULL, tData );
|
|
if ( tEvt ) {
|
|
basicEventHeap::getInstance()->broadcast( tEvt );
|
|
} else {
|
|
free( tData );
|
|
};
|
|
};
|
|
free( tSData );
|
|
};
|
|
};
|
|
break;
|
|
};
|
|
case MBUS_FUNC_ACK_PLATFORM_ALARMS:{//Both Tracks
|
|
LOG("In: All Tracks ACK");
|
|
bool triggered_by_direct_event = false;
|
|
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, SCADA_ACK_SESSION_RATING );
|
|
SESSION_RATE_DATA *tData = (SESSION_RATE_DATA *) malloc( sizeof( SESSION_RATE_DATA ) );
|
|
if ( tData ) {
|
|
tData->level = SCADA_ACK_SESSION_RATING;
|
|
tData->track_id = (*idx)->platform_id;
|
|
EVENT_CONTAINER * tEvt = createBasicEvent( 0, EVT_CLASS_SYSTEM, EVT_SRV_SESSION_RATED, NULL, NULL, tData );
|
|
if ( tEvt ) {
|
|
basicEventHeap::getInstance()->broadcast( tEvt );
|
|
} else {
|
|
free( tData );
|
|
};
|
|
};
|
|
free( tSData );
|
|
} else {
|
|
// Use Just Clear Signals Event if no Active Session
|
|
EVENT_CONTAINER * tEvt = createBasicEvent( (*idx)->platform_id, EVT_CLASS_SYSTEM, EVT_SRV_MODBUS_CLEAR_S );
|
|
if ( tEvt ) {
|
|
basicEventHeap::getInstance()->broadcast( tEvt );
|
|
triggered_by_direct_event = true;
|
|
};
|
|
};
|
|
};
|
|
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, SCADA_ACK_SESSION_RATING );
|
|
SESSION_RATE_DATA *tData = (SESSION_RATE_DATA *) malloc( sizeof( SESSION_RATE_DATA ) );
|
|
if ( tData ) {
|
|
tData->level = SCADA_ACK_SESSION_RATING;
|
|
tData->track_id = (*idx)->platform_id;
|
|
EVENT_CONTAINER * tEvt = createBasicEvent( 0, EVT_CLASS_SYSTEM, EVT_SRV_SESSION_RATED, NULL, NULL, tData );
|
|
if ( tEvt ) {
|
|
basicEventHeap::getInstance()->broadcast( tEvt );
|
|
} else {
|
|
free( tData );
|
|
};
|
|
};
|
|
free( tSData );
|
|
} else {
|
|
// Use Just Clear Signals Event if no Active Session
|
|
// But no need to do it twice!
|
|
if ( !triggered_by_direct_event ) {
|
|
EVENT_CONTAINER * tEvt = createBasicEvent( (*idx)->platform_id, EVT_CLASS_SYSTEM, EVT_SRV_MODBUS_CLEAR_S );
|
|
if ( tEvt ) {
|
|
basicEventHeap::getInstance()->broadcast( tEvt );
|
|
};
|
|
};
|
|
};
|
|
};
|
|
break;
|
|
};
|
|
case MBUS_FUNC_SIG_STROBE_LIGHT:{ // Set TCC AUX signal
|
|
LOG("In: Strobe");
|
|
MODBUS_POST_DATA *tData =(MODBUS_POST_DATA *) malloc( sizeof( MODBUS_POST_DATA ) );
|
|
if ( tData ) {
|
|
tData->type = SCADA_POST_TYPE_HELTH_STROBE;
|
|
tData->status = 0;
|
|
tData->src = (*idx)->func;
|
|
tData->platform_id = (*idx)->platform_id;
|
|
EVENT_CONTAINER * tEvt = createBasicEvent( 0, EVT_CLASS_SYSTEM, EVT_SRV_MODBUS_STROBE, NULL, NULL, tData );
|
|
if ( tEvt ) {
|
|
basicEventHeap::getInstance()->broadcast( tEvt );
|
|
} else {
|
|
free( tData );
|
|
};
|
|
};
|
|
break;
|
|
};
|
|
case MBUS_FUNC_ACK_ALL_SIGNALS:{
|
|
// Do we need to search an active SESSION in that case ??
|
|
LOG("In: Dismiss Platform");
|
|
EVENT_CONTAINER * tEvt = createBasicEvent( (*idx)->platform_id, EVT_CLASS_SYSTEM, EVT_SRV_MODBUS_CLEAR_P );
|
|
if ( tEvt ) {
|
|
basicEventHeap::getInstance()->broadcast( tEvt );
|
|
};
|
|
break;
|
|
};
|
|
};
|
|
had_some_assignments = true;
|
|
};
|
|
};
|
|
};
|
|
//Reset Latch - Only If the Crossbar has function for that
|
|
if ( had_some_assignments ) {
|
|
rv = modbus_set_rtu((*idx)->ip, (*idx)->port, 40, 0);
|
|
if ( rv == -1 ) {
|
|
LOG_ERROR("Error clear latch flag on RTU");
|
|
};
|
|
};
|
|
};
|
|
continue;
|
|
};
|
|
#ifdef DEBUG_EXTRA
|
|
else LOG_ERROR("Read DI Failure");
|
|
#endif
|
|
}
|
|
#ifdef DEBUG_EXTRA
|
|
else LOG_ERROR("RTU Communication Failure");
|
|
#endif
|
|
(*idx)->status = MBUS_STATUS_OFFLINE;
|
|
m_pDBC->updateModBusRTU( (*idx)->id, (*idx)->status, (*idx)->c_InBits, (*idx)->c_OutBits );
|
|
};
|
|
};
|
|
#ifdef DEBUG_EXTRA
|
|
LOG("Sync ModBus Modules Done");
|
|
#endif
|
|
oss = css; // Give 1 Second for STAGE_WORK ??
|
|
m_bStage = MBUS_STAGE_WORK;
|
|
[[fallthrough]];
|
|
};
|
|
case MBUS_STAGE_WORK:{
|
|
uint64_t cts = getMilliseconds();
|
|
m_mProtector.lock();
|
|
if ( m_vActioItems.size() ) {
|
|
for( V_MODBUS_ACION_ITEMS::iterator idx = m_vActioItems.begin(); idx != m_vActioItems.end(); ){
|
|
if( (*idx)->scheduled_on < cts ) {
|
|
MODBUS_RTU_INFO *tRTU = m_pDBC->getModBusRTU( (*idx)->module_id );
|
|
if ( tRTU ) {
|
|
if( handleRTU( tRTU, (*idx)->value, (*idx)->mask) ) {
|
|
free( *idx );
|
|
idx = m_vActioItems.erase( idx ); // erase() invalidates the iterator
|
|
} else {
|
|
LOG_ERROR("Cannot Issue SCADA Signal!");
|
|
idx++;
|
|
};
|
|
free( tRTU );
|
|
} else {
|
|
LOG_ERROR("Cannot Get SCADA RTU info!");
|
|
idx++;
|
|
};
|
|
} else idx++;
|
|
};
|
|
};
|
|
m_mProtector.unlock();
|
|
break;
|
|
};
|
|
case MBUS_STAGE_END:{
|
|
m_bThreadStatus = TH_ENDED;
|
|
break;
|
|
};
|
|
};
|
|
if ( oss != css ) {
|
|
//Each Second or as it takes .... Scan All RTUs may take longer!
|
|
oss = css;
|
|
m_bStage = MBUS_STAGE_SYNC;;
|
|
};
|
|
SLEEP(DEFAULT_THREAD_SLEEP_TIME);
|
|
};
|
|
};
|
|
|
|
bool servaModBusManager::handleRTU( MODBUS_RTU_INFO *tRTU, uint8_t value, uint8_t mask ){
|
|
bool rVal = false;
|
|
if ( tRTU ) {
|
|
if ( tRTU->status == MBUS_STATUS_ONLINE ) {
|
|
bool act = false;
|
|
if( ( tRTU->c_OutBits & value ) != value ) {
|
|
//Set
|
|
tRTU->c_OutBits |= value;
|
|
act = true;
|
|
};
|
|
if( ( tRTU->c_OutBits & mask ) != 0 ) {
|
|
//Clear
|
|
tRTU->c_OutBits &= ~mask;
|
|
act = true;
|
|
};
|
|
// Apply Changes at once
|
|
if ( act ) {
|
|
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 );
|
|
rVal = true;
|
|
};
|
|
};
|
|
};
|
|
};
|
|
return rVal;
|
|
};
|
|
|
|
bool servaModBusManager::validate(){
|
|
MODBUS_MANAGER_DATA * tData = m_pDBC->getModBusManagerData();
|
|
if ( tData ) {
|
|
if( memcmp( &m_sManagerData, tData, sizeof( MODBUS_MANAGER_DATA ) ) != 0 ){
|
|
#ifdef DEBUG
|
|
LOG("Settings was changed ... reapplying");
|
|
#endif
|
|
memcpy( &m_sManagerData, tData, sizeof( MODBUS_MANAGER_DATA ) );
|
|
m_bStage = MBUS_STAGE_INIT; // Reload Modules just in case
|
|
};
|
|
free( tData );
|
|
};
|
|
return true;
|
|
};
|
|
|
|
bool servaModBusManager::dispatchEvent( EVENT_CONTAINER *Evt ){
|
|
if ( Evt == NULL ) return false;
|
|
switch ( Evt->m_eType ) {
|
|
case EVT_SRV_SESSION_STARTED:{
|
|
LOG("EVT Session Start");
|
|
if( m_sManagerData.automaticAlarmRisingTO ) {
|
|
//setup Schedule
|
|
LOG("Setup Scheduled Signal");
|
|
SERVA_SESSION_EVT_DATA *tSData = (SERVA_SESSION_EVT_DATA *)Evt->m_pEvtData;
|
|
if( tSData ) {
|
|
LOG("Have Session Evt Data");
|
|
SCADA_TRACK_INFO *tData = m_pDBC->getTrackSCADA(Evt->m_nSourceNodeID);
|
|
if ( tData ) {
|
|
LOG("Have Track Info");
|
|
//MODBUS_RTU_INFO *tRTU = m_pDBC->getModBusRTU_Platform( tData->platform_id );
|
|
uint8_t expected_module_functionality = 0;
|
|
if ( tSData->side == 'L' || tSData->side == 'B' ) {
|
|
expected_module_functionality |= 1;
|
|
};
|
|
if ( tSData->side == 'R' || tSData->side == 'B' ) {
|
|
expected_module_functionality |= 2;
|
|
};
|
|
#ifdef DEBUG
|
|
if ( expected_module_functionality ) LOG("Expect Module Function");
|
|
#endif
|
|
DBC_UINT64_LIST *tIDS = m_pDBC->listPlatformModBusRTU(tData->platform_id);
|
|
if ( tIDS ) {
|
|
for ( DBC_UINT64_LIST::iterator idx=tIDS->begin(); idx != tIDS->end(); idx++ ) {
|
|
LOG("Check Module!");
|
|
MODBUS_RTU_INFO *tRTU = m_pDBC->getModBusRTU( *idx );
|
|
if ( tRTU ) {
|
|
uint8_t CBV = tRTU->crossbar[((tData->side == 'U')?MBUS_CROSSBAR_UPPER_TRACK:MBUS_CROSSBAR_BOTTOM_TRACK)];
|
|
if( CBV ) { // 0 - disabled functionality
|
|
CBV --; // correct DC bits order
|
|
// 1 = left 2 = right 3 = tcc
|
|
if( ( tRTU->func == 1 ) && ( ( expected_module_functionality & 1 ) == 1 ) ) {
|
|
LOG("Using Left Module!");
|
|
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 << CBV );
|
|
m_mProtector.lock();
|
|
m_vActioItems.push_back( tAI );
|
|
m_mProtector.unlock();
|
|
};
|
|
};
|
|
if( ( tRTU->func == 2 ) && ( ( expected_module_functionality & 2 ) == 2 ) ) {
|
|
LOG("Using Right Module!");
|
|
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 << CBV );
|
|
m_mProtector.lock();
|
|
m_vActioItems.push_back( tAI );
|
|
m_mProtector.unlock();
|
|
};
|
|
};
|
|
if( tRTU->func == 3 ) { // TCC does always
|
|
LOG("Have TCC Module!");
|
|
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 << CBV );
|
|
m_mProtector.lock();
|
|
m_vActioItems.push_back( tAI );
|
|
m_mProtector.unlock();
|
|
};
|
|
};
|
|
}; // Crossbar check
|
|
free( tRTU );
|
|
};
|
|
};
|
|
delete( tIDS );
|
|
};
|
|
free( tData );
|
|
};
|
|
};
|
|
};
|
|
break;
|
|
};
|
|
case EVT_SRV_SESSION_ENDED:{ // Ignore Session End since The SCADA signal must be disabled only by operator
|
|
if ( m_sManagerData.supressSignalBySessionEnd ) {
|
|
LOG("EVT 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 );
|
|
DBC_UINT64_LIST *tIDS = m_pDBC->listPlatformModBusRTU( tData->platform_id );
|
|
if ( tIDS ) {
|
|
for ( DBC_UINT64_LIST::iterator idx=tIDS->begin(); idx != tIDS->end(); idx++ ) {
|
|
mitigateCoil( *idx, ((tData->side == 'U')?MBUS_CROSSBAR_UPPER_TRACK:MBUS_CROSSBAR_BOTTOM_TRACK ) );
|
|
};
|
|
delete( tIDS );
|
|
};
|
|
free( tData );
|
|
};
|
|
};
|
|
break;
|
|
};
|
|
case EVT_SRV_SESSION_RATED:{
|
|
if ( Evt->m_pEvtData == NULL ) return false;
|
|
if ( m_sManagerData.hazardSignalLevel ) {
|
|
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 );
|
|
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 CBV = tRTU->crossbar[((tData->side == 'U')?MBUS_CROSSBAR_UPPER_TRACK:MBUS_CROSSBAR_BOTTOM_TRACK)];
|
|
if ( CBV ) {
|
|
CBV--;
|
|
uint8_t bON = (1 << CBV );
|
|
uint8_t bOFF = ~( 1 << CBV );
|
|
//Remove Scheduled Signal change
|
|
m_mProtector.lock();
|
|
for ( V_MODBUS_ACION_ITEMS::iterator idd=m_vActioItems.begin(); idd!=m_vActioItems.end(); ){
|
|
//check if shceduled ActionItem is Assigned to Current RTU
|
|
if( (*idd)->module_id == tRTU->id ) {
|
|
if( (((*idd)->value & bON ) == bON ) || (((*idd)->mask & bOFF ) == bOFF ) ){
|
|
free((*idd));
|
|
idd = m_vActioItems.erase( idd ); // erase() invalidates the iterator
|
|
} else idd++;
|
|
} else idd++;
|
|
};
|
|
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 {
|
|
//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 );
|
|
};
|
|
};
|
|
};
|
|
};// Crossbar Check
|
|
free( tRTU );
|
|
};
|
|
};
|
|
delete(tIDS);
|
|
};
|
|
free( tData );
|
|
};
|
|
};
|
|
break;
|
|
};
|
|
// Do we need that functionality ??
|
|
case EVT_SRV_MODBUS_TEST_P:{
|
|
MODBUS_TEST_DATA *tData = (MODBUS_TEST_DATA *)Evt->m_pEvtData;
|
|
if ( tData ) {
|
|
DBC_UINT64_LIST *tIDS = m_pDBC->listPlatformModBusRTU( tData->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 );
|
|
};
|
|
};
|
|
delete( tIDS );
|
|
};
|
|
};
|
|
return false; // No need Propogate this Evt
|
|
};
|
|
case EVT_SRV_MODBUS_TEST_M:{
|
|
MODBUS_TEST_DATA *tData = (MODBUS_TEST_DATA *)Evt->m_pEvtData;
|
|
if ( tData ) {
|
|
MODBUS_RTU_INFO *tRTU = m_pDBC->getModBusRTU( tData->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
|
|
handleRTU( tRTU, tData->c_OutBits, ~tData->c_OutBits );
|
|
free( tRTU );
|
|
};
|
|
};
|
|
return false; // No need Propogate this Evt
|
|
};
|
|
case EVT_SRV_MODBUS_CLEAR_S:{
|
|
//Create ActionItems For all Modules
|
|
LOG("Clear ALARMS Only");
|
|
uint64_t platform_id = Evt->m_nSourceNodeID;
|
|
if ( platform_id ) {
|
|
DBC_UINT64_LIST *tIDS = m_pDBC->listPlatformModBusRTU( platform_id );
|
|
if ( tIDS ) {
|
|
for ( DBC_UINT64_LIST::iterator idx=tIDS->begin(); idx != tIDS->end(); idx++ ) {
|
|
mitigateCoil( *idx, MBUS_CROSSBAR_UPPER_TRACK );
|
|
mitigateCoil( *idx, MBUS_CROSSBAR_BOTTOM_TRACK );
|
|
};
|
|
delete( tIDS );
|
|
};
|
|
};
|
|
return false;// No need Propogate this Evt ??
|
|
};
|
|
case EVT_SRV_MODBUS_CLEAR_P:{
|
|
//Create ActionItems For all Modules
|
|
LOG("Clear ALL SIGNALS and SNOOZE POSTBOXES");
|
|
uint64_t platform_id = Evt->m_nSourceNodeID;
|
|
if ( platform_id ) {
|
|
DBC_UINT64_LIST *tIDS = m_pDBC->listPlatformModBusRTU( platform_id );
|
|
if ( tIDS ) {
|
|
for ( DBC_UINT64_LIST::iterator idx=tIDS->begin(); idx != tIDS->end(); idx++ ) {
|
|
mitigateCoil( *idx, MBUS_CROSSBAR_UPPER_TRACK );
|
|
mitigateCoil( *idx, MBUS_CROSSBAR_BOTTOM_TRACK );
|
|
mitigateCoil( *idx, MBUS_CROSSBAR_HEALTH, 0, (SERVER_SCADA_POST_TYPE) 0 );
|
|
mitigateCoil( *idx, MBUS_CROSSBAR_AUX, 0, (SERVER_SCADA_POST_TYPE) 6 );
|
|
};
|
|
delete( tIDS );
|
|
};
|
|
};
|
|
return false; // No need Propogate this Evt ??
|
|
};
|
|
case EVT_SRV_MODBUS_S_POSTBOX:{
|
|
LOG("Snooze PBOX");
|
|
MODBUS_POST_DATA *sData = (MODBUS_POST_DATA *)Evt->m_pEvtData;
|
|
if ( sData ) {
|
|
bool mitigate = false;
|
|
if ( sData->type == 6 ) {
|
|
mitigate = true;
|
|
} else {
|
|
if ( sData->type >= m_sManagerData.helthSignalLevel ) mitigate = true;
|
|
};
|
|
if ( mitigate ) {
|
|
DBC_UINT64_LIST *tIDS = m_pDBC->listPlatformModBusRTU( sData->platform_id );
|
|
if ( tIDS ) {
|
|
for ( DBC_UINT64_LIST::iterator idx=tIDS->begin(); idx != tIDS->end(); idx++ ) {
|
|
mitigateCoil(*idx, ((sData->type == SCADA_POST_TYPE_HELTH_STROBE) ? MBUS_CROSSBAR_AUX : MBUS_CROSSBAR_HEALTH), sData->src, (SERVER_SCADA_POST_TYPE)sData->type );
|
|
};
|
|
delete( tIDS );
|
|
};
|
|
};
|
|
};
|
|
break;
|
|
};
|
|
case EVT_SRV_MODBUS_D_POSTBOX:{
|
|
LOG("Dismiss PBOX");
|
|
MODBUS_POST_DATA *sData = (MODBUS_POST_DATA *)Evt->m_pEvtData;
|
|
if ( sData ) {
|
|
bool mitigate = false;
|
|
if ( sData->type == 6 ) {
|
|
mitigate = true;
|
|
} else {
|
|
if ( sData->type >= m_sManagerData.helthSignalLevel ) mitigate = true;
|
|
};
|
|
if ( mitigate ) {
|
|
DBC_UINT64_LIST *tIDS = m_pDBC->listPlatformModBusRTU( sData->platform_id );
|
|
if ( tIDS ) {
|
|
for ( DBC_UINT64_LIST::iterator idx=tIDS->begin(); idx != tIDS->end(); idx++ ) {
|
|
mitigateCoil(*idx, ((sData->type == SCADA_POST_TYPE_HELTH_STROBE) ? MBUS_CROSSBAR_AUX : MBUS_CROSSBAR_HEALTH), sData->src, (SERVER_SCADA_POST_TYPE)sData->type );
|
|
};
|
|
delete( tIDS );
|
|
};
|
|
};
|
|
};
|
|
return false; // No need Propogate this Evt
|
|
};
|
|
case EVT_SRV_MODULE_HEALTH:{
|
|
LOG("Module Helth Changed!");
|
|
if ( Evt->m_pEvtData == NULL ) return false;
|
|
if ( m_sManagerData.helthSignalLevel ) {
|
|
MODULE_HEALTH_STATUS *tData = (MODULE_HEALTH_STATUS *)Evt->m_pEvtData;
|
|
DBC_UINT64_LIST *tIDS = m_pDBC->listPlatformModBusRTU( tData->platform_id );
|
|
if ( tIDS ) {
|
|
for ( DBC_UINT64_LIST::iterator idx=tIDS->begin(); idx != tIDS->end(); idx++ ) {
|
|
if( tData->status >= m_sManagerData.helthSignalLevel ){
|
|
activateCoil( *idx, MBUS_CROSSBAR_HEALTH, tData->module_id, (SERVER_SCADA_POST_TYPE) tData->status );
|
|
} else {
|
|
mitigateCoil( *idx, MBUS_CROSSBAR_HEALTH, tData->module_id, (SERVER_SCADA_POST_TYPE) tData->status );
|
|
};
|
|
};
|
|
delete( tIDS );
|
|
};
|
|
};
|
|
break;
|
|
};
|
|
case EVT_SRV_MODBUS_STROBE:{
|
|
LOG("Activate Strobe Signal");
|
|
MODBUS_POST_DATA *tData = (MODBUS_POST_DATA *) Evt->m_pEvtData;
|
|
if ( tData ) {
|
|
DBC_UINT64_LIST *tIDS = m_pDBC->listPlatformModBusRTU( tData->platform_id );
|
|
if ( tIDS ) {
|
|
for ( DBC_UINT64_LIST::iterator idx=tIDS->begin(); idx != tIDS->end(); idx++ ) {
|
|
activateCoil( *idx, MBUS_CROSSBAR_AUX, tData->src, (SERVER_SCADA_POST_TYPE) tData->type );
|
|
};
|
|
delete( tIDS );
|
|
};
|
|
};
|
|
break;
|
|
};
|
|
};
|
|
return true;
|
|
};
|
|
|
|
bool servaModBusManager::validateHealthAction( uint64_t platform_id, bool action ){
|
|
bool retVal = true;
|
|
if( platform_id ) {
|
|
DBC_UINT64_LIST *tIDS = m_pDBC->listScadaPosts( platform_id );
|
|
if ( tIDS ) {
|
|
for ( DBC_UINT64_LIST::iterator idx=tIDS->begin(); idx != tIDS->end(); idx++ ) {
|
|
MODBUS_POST_DATA *tData = m_pDBC->getScadaPostData( *idx );
|
|
if ( tData ) {
|
|
if ( !action ) {
|
|
// Have Some Unresolved Health Status on Platform
|
|
if ( ( tData->type < 6 ) && ( tData->status == 0 ) ) retVal = false;
|
|
};
|
|
free(tData);
|
|
};
|
|
};
|
|
delete( tIDS );
|
|
};
|
|
};
|
|
return retVal;
|
|
};
|
|
|
|
void servaModBusManager::activateCoil( uint64_t rtu_id, uint8_t function_id, uint64_t src, SERVER_SCADA_POST_TYPE pt ){
|
|
if( function_id > MBUS_CROSSBAR_AUX ) return; // No such Function
|
|
MODBUS_RTU_INFO *tRTU = m_pDBC->getModBusRTU( rtu_id );
|
|
if ( tRTU ) {
|
|
uint8_t CBV = tRTU->crossbar[function_id];
|
|
if( CBV ) {
|
|
bool module_is_ok = true;
|
|
if( pt && tRTU->func != 3 && tRTU->func != 0 && CBV ) module_is_ok = false;
|
|
// check if TCC or Default Module
|
|
if( module_is_ok ) {
|
|
int c_status = 0;
|
|
// check if was not snoozed by operator
|
|
if( pt ) c_status = m_pDBC->getScadaPostStatus( tRTU->platform_id, pt, src );
|
|
if ( function_id == MBUS_CROSSBAR_HEALTH ) {
|
|
// clear all abowe Health statuses as not relevant
|
|
// do we need a module ID ?? since the zone is whole platform
|
|
// If tData->status == 0 then the Health status will be cleared
|
|
char tB[128];
|
|
sprintf( tB, "Clearing All Abowe Health level %d on Activate", pt );
|
|
LOG( tB );
|
|
m_pDBC->deleteScadaPostsAbowe( tRTU->platform_id, pt, src );
|
|
};
|
|
|
|
if( c_status < 1) { // Not Snoozed by Operator
|
|
if ( c_status < 0 ) { // Not Founud in MBOX but must have record!
|
|
m_pDBC->addScadaPost( tRTU->platform_id, pt, src );
|
|
};
|
|
CBV--;
|
|
//Actual Setting modbus signal
|
|
uint8_t bON = (1 << CBV );
|
|
uint8_t bOFF = ~( 1 << CBV );
|
|
//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((*idz));
|
|
idz = m_vActioItems.erase( idz ); // erase() invalidates the iterator
|
|
} else idz++;
|
|
//break;
|
|
} else idz++;
|
|
};
|
|
m_mProtector.unlock();
|
|
//Since Activation need to set only SET
|
|
handleRTU( tRTU, bON, 0x00 );
|
|
};
|
|
};
|
|
};
|
|
free( tRTU );
|
|
};
|
|
};
|
|
|
|
void servaModBusManager::mitigateCoil( uint64_t rtu_id, uint8_t function_id, uint64_t src, SERVER_SCADA_POST_TYPE pt ){
|
|
if( function_id > MBUS_CROSSBAR_AUX ) return; // No such Function
|
|
MODBUS_RTU_INFO *tRTU = m_pDBC->getModBusRTU( rtu_id );
|
|
if ( tRTU ) {
|
|
uint8_t CBV = tRTU->crossbar[function_id];
|
|
if ( CBV ) {
|
|
if (function_id == MBUS_CROSSBAR_HEALTH) {
|
|
// clear all abowe Health statuses as not relevant
|
|
// do we need a module ID ?? since the zone is whole platform
|
|
// If tData->status == 0 then the Health status will be cleared
|
|
char tB[128];
|
|
sprintf( tB, "Clearing All Abowe Health level %d for %lld on Mitigate ", pt, src );
|
|
LOG( tB );
|
|
if ( pt ) {
|
|
m_pDBC->deleteScadaPostsAbowe(tRTU->platform_id, (SERVER_SCADA_POST_TYPE) pt, src ); // pt-1 - to remove above and current
|
|
} else {
|
|
m_pDBC->deleteScadaPostsAbowe(tRTU->platform_id, SCADA_POST_TYPE_NA, 0 ); // Clear All Platform Posts!!
|
|
};
|
|
};
|
|
LOG("Validating Health Action");
|
|
if ( validateHealthAction( tRTU->platform_id, false ) ) {
|
|
CBV--;
|
|
//Actual Setting modbus signal
|
|
uint8_t bON = (1 << CBV );
|
|
uint8_t bOFF = ~bON;
|
|
//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((*idz));
|
|
idz = m_vActioItems.erase( idz ); // erase() invalidates the iterator
|
|
} else idz++;
|
|
//break;
|
|
} else idz++;
|
|
};
|
|
m_mProtector.unlock();
|
|
//Since Mitigation need to set only mask
|
|
handleRTU( tRTU, 0x00, bON );
|
|
};
|
|
};
|
|
free( tRTU );
|
|
};
|
|
}
|
|
|
|
#undef LOG
|
|
#undef LOG_ERROR
|
|
|