|
|
#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 ); 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 ) { LOG("Have DI Signal!!"); //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:{ 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] - 1 ) ); uint8_t bOFF = ~( 1 << ( tRTU->crossbar[MBUS_CROSSBAR_UPPER_TRACK] - 1 ) ); //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 MBUS_FUNC_ACK_BOTTOM_TRACK:{//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 MBUS_FUNC_ACK_BOTH_TRACKS:{//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 MBUS_FUNC_SIG_STROBE_LIGHT:{ // 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 ) { // check if TCC Module
if ( tRTU->func == 3 ) { uint8_t bON = (1 << ( tRTU->crossbar[MBUS_CROSSBAR_AUX] - 1 ) ); uint8_t bOFF = ~( 1 << ( tRTU->crossbar[MBUS_CROSSBAR_AUX] - 1 ) ); //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(); //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->mask = (1 << (tRTU->crossbar[MBUS_CROSSBAR_AUX] - 1)); m_mProtector.lock(); m_vActioItems.push_back( tAI ); m_mProtector.unlock(); }; }; } else { // 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->mask = ( 1 << ( tRTU->crossbar[MBUS_CROSSBAR_AUX] - 1 ) ); m_mProtector.lock(); m_vActioItems.push_back( tAI ); m_mProtector.unlock(); }; }; }; free( tRTU ); }; }; delete( tIDS ); }; break; }; }; }; }; }; //Reset Latch
//rv = modbus_set_rtu_bit( (*idx)->ip, (*idx)->port, 36, 0 );
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("Read DO 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 ) { handleRTU( (*idx)->module_id, (*idx)->value, (*idx)->mask ); free( *idx ); m_vActioItems.erase( 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( uint64_t rtu_id, uint8_t value, uint8_t mask ){ bool rVal = false; if ( m_pDBC == NULL ) return rVal; MODBUS_RTU_INFO *tRTU = m_pDBC->getModBusRTU( rtu_id ); if ( tRTU ) { if( ( tRTU->c_OutBits & value ) == 0 ) { //Set
tRTU->c_OutBits |= value; int rv = modbus_set_rtu( tRTU->ip, tRTU->port, tRTU->output_base_addr, tRTU->c_OutBits ); if ( rv < 0 ) { // Do we need clear Database values on failure??
//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; }; }; if( ( tRTU->c_OutBits & mask ) != 0 ) { //Clear
tRTU->c_OutBits &= ~mask; int rv = modbus_set_rtu( tRTU->ip, tRTU->port, tRTU->output_base_addr, tRTU->c_OutBits ); if ( rv < 0 ) { // Do we need clear Database values on failure??
//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; }; }; free(tRTU); }; 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; }; if ( expected_module_functionality ) { LOG("Expect Module Function"); }; 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 ) { // 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 << tRTU->crossbar[((tData->side == 'U')?MBUS_CROSSBAR_UPPER_TRACK:MBUS_CROSSBAR_BOTTOM_TRACK)] ); 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 << tRTU->crossbar[((tData->side == 'U')?MBUS_CROSSBAR_UPPER_TRACK:MBUS_CROSSBAR_BOTTOM_TRACK)] ); 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 << 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 ); }; }; delete( tIDS ); }; free( tData ); }; }; }; break; }; case EVT_SRV_SESSION_ENDED:{ 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++ ) { 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 idd=m_vActioItems.begin(); idd!=m_vActioItems.end(); ){ if( (*idd)->module_id == tRTU->id ) { //check if shceduled signal is current
if( (((*idd)->value & bON ) == bON ) || (((*idd)->mask & bOFF ) == bOFF ) ){ free((*idd)); m_vActioItems.erase( idd ); } else idd++; //break;
} else idd++; }; 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( 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 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 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)); m_vActioItems.erase( idd ); } 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 ); }; }; }; free( tRTU ); }; }; delete(tIDS); }; free( tData ); }; }; break; }; 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 ); }; }; }; }; 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
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 ); }; }; return false; // No need Propogate this Evt
}; case EVT_SRV_MODULE_HEALTH:{ if ( Evt->m_pEvtData == NULL ) return false; 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 );
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 ); }; }; delete( tIDS ); }; } else { //Below Level
//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 ); }; }; }; }; }; break; }; }; return true;};
#undef LOG
#undef LOG_ERROR
|