#include "protocolDefender.h" #include "../../alg/generic/md5.h" #define LOG( X ) m_pLog->Log( "Defender", this, X ) #define LOGS( X ) basicLogCollection::getInstance()->Log( "Defender", NULL, X ) #define LOG_ERROR( X ) m_pLog->Log( "Defender", this, _LOG_ERROR, X ) CSTR defenderStatusMode[] = { "Operational", "Degraded" }; CSTR defenderFaultCode[] = { "Critical", "Warning", "Info" }; CSTR defenderCategories[] = { DFSL_CATEGORY_ERROR, DFSL_CATEGORY_STATUS, DFSL_CATEGORY_ALARM_2D, DFSL_CATEGORY_ALARM_3D }; CSTR defenderTypeDescription[] = { "Fault Event", "Status Message", "Target Detection Event", "Target Detection Event" }; CSTR defenderTypeSeverity[] = { "very-low", "low", "medium", "high", "very-high" }; /* Global Time Diff Value to correct Timestamps */ static int g_iTimeDiff = 0; /** * number of seconds from 1 Jan. 1601 00:00 to 1 Jan 1970 00:00 UTC */ //#define EPOCH_DIFF 11644473600LL #define UNIX_TimeStamp_2_WINDOWS_FileTime_SHIFT 116444736000000000 #ifdef __linux__ unsigned long long getSHDTimeValue( void ){ unsigned long long tMilliseconds; struct timeval tData; struct timezone tTZ; tMilliseconds = 0; tTZ.tz_minuteswest = 0; tTZ.tz_dsttime = 0; if( gettimeofday( &tData, &tTZ ) == -1 ){ } else { tMilliseconds = tData.tv_sec; tMilliseconds = tMilliseconds *1000; tMilliseconds += tData.tv_usec / 1000; }; #else /* void TimetToFileTime( time_t t, LPFILETIME pft ){ LONGLONG ll = Int32x32To64(t, 10000000) + 116444736000000000; pft->dwLowDateTime = (DWORD) ll; pft->dwHighDateTime = ll >>32; }*/ unsigned __int64 getSHDTimeValue( void ){ unsigned __int64 tMilliseconds; SYSTEMTIME Ztime; GetSystemTime(&Ztime); SystemTimeToFileTime( &Ztime, (FILETIME*) &tMilliseconds ); tMilliseconds -= UNIX_TimeStamp_2_WINDOWS_FileTime_SHIFT; tMilliseconds = tMilliseconds / 10000; #endif tMilliseconds += g_iTimeDiff; return tMilliseconds; }; CSTR getSHDTimeStamp( void ){ CSTR RetVal = NULL; time_t rawtime; uint32_t msec; struct tm * ptm = NULL; time ( &rawtime ); ptm = gmtime ( &rawtime ); msec = getSHTimeValue() % 1000; //2019-03-24T12:32:59.670Z if( ptm != NULL ) { RetVal = (CSTR) malloc( 128 ); if( RetVal!=NULL ){ sprintf( RetVal, "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ", (ptm->tm_year+1900), (ptm->tm_mon+1), ptm->tm_mday, ptm->tm_hour, ptm->tm_min, ptm->tm_sec, msec ); }; }; return RetVal; }; bool correctSHDTime( int diff ){ #ifdef __linux__ struct timeval tData; struct timezone tTZ; tTZ.tz_minuteswest = 0; tTZ.tz_dsttime = 0; if( gettimeofday( &tData, &tTZ ) == -1 ){ return false; } else { tData.tv_sec += diff / 1000; tData.tv_usec += ( diff * 1000 ) % 1000000; }; if( settimeofday( &tData, &tTZ ) == -1 ){ return false; }; #else unsigned __int64 tMilliseconds; SYSTEMTIME Ztime; GetSystemTime(&Ztime); SystemTimeToFileTime( &Ztime, (FILETIME*) &tMilliseconds ); tMilliseconds += diff * 10000; FileTimeToSystemTime( (FILETIME*) &tMilliseconds, &Ztime ); if( 0 == SetSystemTime(&Ztime) ) return false; #endif return true; }; CSTR protocolDefender::buildURL( CSTR templ, int dim, CSTR devID ){ CSTR RetVal; char tpl[256]; RetVal = (CSTR) malloc( 256 ); //sprintf( tpl, "http://%s:%d%s", m_pServerIP, m_nServerPort, templ ); //sprintf( RetVal, tpl, dim, devID ); sprintf( RetVal, DFSL_DEFENDER_DEFAULT_URL, m_pServerIP, m_nServerPort ); return RetVal; }; CSTR protocolDefender::buildUniqID( void ){ CSTR RetVal; RetVal = (CSTR) malloc(64); sprintf( RetVal, "%s", MD5String( getSHDTimeStamp() ) ); return RetVal; }; /* Default Constructor */ protocolDefender::protocolDefender( config_setting_t *Settings ): protocolHTTP( basicSettingsProvider::getInstance()->getSubNode(Settings, "DEFENDER_CONNECTION") ){ m_pLog = basicLogCollection::getInstance(); m_pSettings = basicSettingsProvider::getInstance()->getSubNode(Settings, "DEFENDER_CONNECTION"); m_pScannerName = (CSTR)malloc(OS_SCANNER_NAME_SIZE); if( m_pScannerName == NULL ) return; memset( m_pScannerName, 0 , OS_SCANNER_NAME_SIZE); m_pServerIP = (CSTR)malloc(OS_CEP_ADDRESS_SIZE); if( m_pServerIP == NULL ) return; memset( m_pServerIP, 0 , OS_CEP_ADDRESS_SIZE); m_bCurrentStatus = false; if( Settings != NULL ) { double tVal = 0; config_setting_t * pSetting; if (!config_setting_lookup_float(Settings, "LOCATION_LON", &tVal )){ tVal = 35.0938962; pSetting = config_setting_add(Settings, "LOCATION_LON", CONFIG_TYPE_FLOAT); config_setting_set_float(pSetting, tVal); basicSettingsProvider::getInstance()->compromise(); }; m_fLon = tVal; if (!config_setting_lookup_float(Settings, "LOCATION_LAT", &tVal )){ tVal = 32.904208; pSetting = config_setting_add(Settings, "LOCATION_LAT", CONFIG_TYPE_FLOAT); config_setting_set_float(pSetting, tVal); basicSettingsProvider::getInstance()->compromise(); }; m_fLat = tVal; } else { m_fLon = 35.0938962;// 35.095278 m_fLat = 32.904208; // 32.904167 } if( m_pSettings != NULL ){ config_setting_t *pSetting; if (!config_setting_lookup_string(m_pSettings, "SCANNER_NAME", (const char**)&m_pScannerName)){ strncpy( m_pScannerName, "LRF_2D_001", 64 ); LOG( m_pScannerName ); pSetting = config_setting_add(m_pSettings, "SCANNER_NAME", CONFIG_TYPE_STRING); config_setting_set_string(pSetting, m_pScannerName); basicSettingsProvider::getInstance()->compromise(); }; if (!config_setting_lookup_int(m_pSettings, "SCANNER_DIMENTIONS", &m_nScannerDimentions )){ m_nScannerDimentions = 2; pSetting = config_setting_add(m_pSettings, "SCANNER_DIMENTIONS", CONFIG_TYPE_INT); config_setting_set_int(pSetting, m_nScannerDimentions); basicSettingsProvider::getInstance()->compromise(); }; if (!config_setting_lookup_string(m_pSettings, "CEP_IP_ADDRESS", (const char**)&m_pServerIP)){ strncpy( m_pServerIP, "192.168.200.204", 16 ); LOG( m_pServerIP ); pSetting = config_setting_add(m_pSettings, "CEP_IP_ADDRESS", CONFIG_TYPE_STRING); config_setting_set_string(pSetting, m_pServerIP); basicSettingsProvider::getInstance()->compromise(); }; if (!config_setting_lookup_int(m_pSettings, "CEP_IP_PORT", &m_nServerPort)){ m_nServerPort = 80; pSetting = config_setting_add(m_pSettings, "CEP_IP_PORT", CONFIG_TYPE_INT); config_setting_set_int(pSetting, m_nServerPort); basicSettingsProvider::getInstance()->compromise(); }; basicSettingsProvider::getInstance()->validate(); }; }; /* 1.2 GHz Number of Cores Quad-Core Type Tegra 3 Processor Number T30SL Manufacturer NVIDIA Verify that you have the following installed on your system Ubuntu 16.04 LTS (with GCC 5.4) NVIDIA Display Driver ver. 375 NVIDIA VideoSDK 7.1 cuDNN 6 & TensorRT 2.1 NVIDIA CUDA 8.0 Download the DeepStream 1.0 tar file (below) Extract the package at the location of your choice Follow the usage instructions from the Deepstream_User_guide.pdf in the downloaded package. */ protocolDefender::~protocolDefender(){ if(m_pScannerName!=NULL)free(m_pScannerName); if(m_pServerIP!=NULL)free(m_pServerIP); }; void protocolDefender::setScannerName( CSTR Name ){ sprintf( m_pScannerName, "%s", Name ); }; void protocolDefender::setProtocolType( bool DeviceIs3D ){ if( DeviceIs3D ) { m_nScannerDimentions = 3; } else { m_nScannerDimentions = 2; }; }; CSTR protocolDefender::assembleStatusData( bool StatusOK ){ CSTR RetVal; CSTR TIME_STAMP; CSTR tSRC; CSTR tTARGET; CSTR tID; RetVal = (CSTR) malloc(CLOG); if ( RetVal != NULL ) { TIME_STAMP = getSHDTimeStamp(); if( TIME_STAMP != NULL ) { tTARGET = (CSTR) malloc(128); if( tTARGET!= NULL) { tSRC = (CSTR) malloc(128); if( tSRC ) { tID = (CSTR) malloc(64); if( tID != NULL ) { sprintf( tID, "%s", MD5String( TIME_STAMP ) ); sprintf( tSRC, DFSL_SENSOR_SOURCE_DTO, DFSL_HARDWARE_TYPE, DFRL_PROVIDER_ID, m_pScannerName ); sprintf( tTARGET, DFSL_STATUS_DTO, TIME_STAMP , defenderStatusMode[((StatusOK)?1:0)] ); sprintf( RetVal, PACKAGE_SE_STATUS_DATA_ELEMENT, defenderTypeDescription[DC_STATUS], TIME_STAMP, tSRC, "DFSL_LIDAR_SENSOR_STATUS", DFSL_DATA_TYPE_STATUS, tID, tTARGET ); free( tID ); free( tSRC ); free( tTARGET ); free( TIME_STAMP ); } else { free( tSRC ); free( tTARGET ); free( TIME_STAMP ); free( RetVal ); } } else { free( tTARGET ); free( TIME_STAMP ); free( RetVal ); }; } else { free( TIME_STAMP ); free( RetVal ); }; } else { free( RetVal ); RetVal = NULL; }; }; return RetVal; }; CSTR protocolDefender::assembleFaultData( uint8_t Code, CSTR Desc ){ CSTR RetVal; CSTR TIME_STAMP; CSTR tSRC; CSTR tTARGET; CSTR tID; RetVal = (CSTR) malloc(CLOG); if ( RetVal != NULL ) { TIME_STAMP = getSHDTimeStamp(); if( TIME_STAMP != NULL ) { tTARGET = (CSTR) malloc(128); if( tTARGET!= NULL) { tSRC = (CSTR) malloc(128); if( tSRC ) { tID = (CSTR) malloc(64); if( tID != NULL ) { sprintf( tID, "%s", MD5String( TIME_STAMP ) ); sprintf( tSRC, DFSL_SENSOR_SOURCE_DTO, DFSL_HARDWARE_TYPE, DFRL_PROVIDER_ID, m_pScannerName ); sprintf( tTARGET, DFSL_ERROR_DTO, Code, TIME_STAMP ); sprintf( RetVal, PACKAGE_SE_STATUS_DATA_ELEMENT, Desc, TIME_STAMP, tSRC, "DFSL_LIDARE_SENSOR_FAULT_DATA", DFSL_DATA_TYPE_ERROR, tID, tTARGET ); free( tID ); free( tSRC ); free( tTARGET ); free( TIME_STAMP ); } else { free( tSRC ); free( tTARGET ); free( TIME_STAMP ); free( RetVal ); } } else { free( tTARGET ); free( TIME_STAMP ); free( RetVal ); }; } else { free( TIME_STAMP ); free( RetVal ); }; } else { free( RetVal ); RetVal = NULL; }; }; return RetVal; }; CSTR protocolDefender::assembleTargetData( int targetID, int Range, int Width, int Azimuth, int Elevation ){ CSTR RetVal; CSTR TIME_STAMP; CSTR tSRC; CSTR tTARGET; CSTR tID; RetVal = (CSTR) malloc(CLOG); if ( RetVal != NULL ) { TIME_STAMP = getSHDTimeStamp(); if( TIME_STAMP != NULL ) { tTARGET = (CSTR) malloc(128); if( tTARGET!= NULL) { tSRC = (CSTR) malloc(128); if( tSRC ) { tID = (CSTR) malloc(64); if( tID != NULL ) { sprintf( tID, "%s", MD5String( TIME_STAMP ) ); sprintf( tSRC, DFSL_SENSOR_SOURCE_DTO, DFSL_HARDWARE_TYPE, DFRL_PROVIDER_ID, m_pScannerName ); if( m_nScannerDimentions > 2 ) { sprintf( tTARGET, DFSL_3D_TARGET_DTO, Azimuth, Elevation, Range ); } else { sprintf( tTARGET, DFSL_2D_TARGET_DTO, Azimuth, Range, Width ); }; sprintf( RetVal, PACKAGE_SE_TARGET_DATA_ELEMENT, defenderTypeDescription[DC_ALARM], TIME_STAMP, tSRC, "DFSL_TARGET_DATA", DFSL_DATA_TYPE_TARGET, tID, tTARGET ); free( tID ); free( tSRC ); free( tTARGET ); free( TIME_STAMP ); } else { free( tSRC ); free( tTARGET ); free( TIME_STAMP ); free( RetVal ); } } else { free( tTARGET ); free( TIME_STAMP ); free( RetVal ); }; } else { free( TIME_STAMP ); free( RetVal ); }; } else { free( RetVal ); RetVal = NULL; }; }; return RetVal; }; static uint32_t DEFENDER_MESSAGE_ID = 0; size_t protocolDefender::assembleDefenderJSON( CSTR Buffer, defenderCategoryTypes Type, uint8_t Severity, CSTR Location, CSTR Data ){ char tBuffer[CLOG]; size_t RetVal; CSTR tBuf; RetVal = 0; tBuf = getSHDTimeStamp(); DEFENDER_MESSAGE_ID++; //memset( tBuffer, 0, CLOG ); if( tBuf != NULL ){ //char ID[64]; switch ( Type ) { case DC_STATUS: Severity = 0; break; case DC_ERROR: Severity = 2; break; case DC_ALARM: Severity = 4; break; default: Severity = 2; }; //sprintf( ID, "%s", MD5String( tBuf ) ); if( (m_nScannerDimentions > 2) && ( Type == DC_ALARM ) ) Type = DC_ALARM_3D; sprintf( tBuffer, DFSL_SE_ENVELOPE, //DEFENDER_MESSAGE_ID, defenderCategories[Type], defenderTypeSeverity[Severity], tBuf, tBuf, Location, Data ); RetVal = strlen(tBuffer); memcpy( Buffer, tBuffer, RetVal ); Buffer[RetVal] = (char) 0; free( tBuf ); }; return RetVal; }; bool protocolDefender::buildStatusPackage( unsigned char *Buffer, int &BufferSize, bool Status ){ if( Buffer!= NULL ){ CSTR tBuf; tBuf = assembleStatusData( Status ); #ifdef DEBUG //LOG(tBuf); #endif if( tBuf != NULL ) { char DFSL_LOCATION[256]; sprintf(DFSL_LOCATION, DFSL_LOCATION_DTO, m_fLon, m_fLat ); BufferSize = assembleDefenderJSON( (CSTR)Buffer, DC_STATUS, ((Status)?0:2), DFSL_LOCATION, tBuf ); #ifdef DEBUG LOG((CSTR)Buffer); #endif free( tBuf ); return true; } }; return false; }; bool protocolDefender::buildFaultPackage( unsigned char *Buffer, int &BufferSize, unsigned char Type, CSTR Description ){ if( Buffer!= NULL ){ CSTR tBuf; tBuf = assembleFaultData( Type, Description ); if( tBuf != NULL ){ char DFSL_LOCATION[256]; sprintf(DFSL_LOCATION, DFSL_LOCATION_DTO, m_fLon, m_fLat ); BufferSize = assembleDefenderJSON( (CSTR)Buffer, DC_ERROR, 3, DFSL_LOCATION, tBuf ); free( tBuf ); return true; }; }; return false; }; bool protocolDefender::buildEvent2DPackage( unsigned char *Buffer, int &BufferSize, CSTR msgUID, int targetID, float Angle, float Range, float Width){ if( Buffer!= NULL ){ CSTR tBuf; tBuf = assembleTargetData( targetID, Range, Width, Angle, 0 ); if( tBuf != NULL ){ char DFSL_LOCATION[256]; sprintf(DFSL_LOCATION, DFSL_LOCATION_DTO, m_fLon, m_fLat ); BufferSize = assembleDefenderJSON( (CSTR)Buffer, DC_ALARM, 3, DFSL_LOCATION, tBuf ); free( tBuf ); return true; }; }; return false; }; bool protocolDefender::buildEvent3DPackage( unsigned char *Buffer, int &BufferSize, CSTR msgUID, int targetID, float Angle1, float Angle2, float Range ){ if( Buffer!= NULL ){ CSTR tBuf; tBuf = assembleTargetData( targetID, Range, 0, Angle1, Angle2 ); #ifdef DEBUG //LOG(tBuf); #endif if( tBuf != NULL ){ char DFSL_LOCATION[256]; sprintf(DFSL_LOCATION, DFSL_LOCATION_DTO, m_fLon, m_fLat ); BufferSize = assembleDefenderJSON( (CSTR)Buffer, DC_ALARM, 3, DFSL_LOCATION, tBuf ); #ifdef DEBUG LOG((CSTR)Buffer); #endif free( tBuf ); return true; }; }; return false; }; bool protocolDefender::buildClockSyncPackage( unsigned char *Buffer, int &BufferSize ){ /*if( Buffer!= NULL ){ CSTR tBuf; tBuf = getSHTimeStamp(); if( tBuf != NULL ){ sprintf( ((char *)(Buffer+BufferSize)), PACKAGE_ENVELOPE_TEMPLATE_CLOCKSYNC, tBuf ); BufferSize = strlen( (char *)Buffer ); free( tBuf ); return true; }; };*/ return false; }; bool protocolDefender::buildCallBackPackage( unsigned char *Buffer, int &BufferSize, CSTR CallBackPath ){ /*if( Buffer!= NULL ){ sprintf( ((char *)(Buffer+BufferSize)), PACKAGE_ENVELOPE_TEMPLATE_REG_CALL, CallBackPath ); BufferSize = strlen( (char *)Buffer ); return true; };*/ return false; }; bool protocolDefender::Alarm2D( float Angle, float Range, float Width, int ID ){ bool RetVal= false; char Buffer[1024]; int BufferSize = 0; CSTR URL; CSTR UID; UID = buildUniqID(); if( UID!= NULL ){ URL = buildURL( SERVICE_URL_TEMPLATE_EVENT, m_nScannerDimentions, m_pScannerName ); RetVal = buildEvent2DPackage( ( unsigned char *) Buffer, BufferSize, UID, ID, Angle, Range, Width ); if( URL != NULL ){ RetVal = issuePost( URL, Buffer, BufferSize ); free( URL ); }; free( UID ); }; return RetVal; }; bool protocolDefender::Alarm3D( float Angle1, float Angle2, float Range, int ID ){ bool RetVal= false; char Buffer[1024]; int BufferSize = 0; CSTR URL; CSTR UID; UID = buildUniqID(); if( UID!= NULL ){ URL = buildURL( SERVICE_URL_TEMPLATE_EVENT, m_nScannerDimentions, m_pScannerName ); #ifdef DEBUG LOG(URL); #endif RetVal = buildEvent3DPackage( ( unsigned char *) Buffer, BufferSize, UID, ID, Angle1, Angle2, Range ); if( URL != NULL ){ RetVal = issuePost( URL, Buffer, BufferSize ); free( URL ); }; free( UID ); }; return RetVal; }; bool protocolDefender::VisibilityChanged( bool Worsened ){ bool RetVal= false; char Buffer[1024]; int BufferSize = 0; CSTR URL; URL = buildURL( SERVICE_URL_TEMPLATE_STATUS, m_nScannerDimentions, m_pScannerName ); #ifdef DEBUG LOG(URL); #endif RetVal = buildStatusPackage( ( unsigned char *) Buffer, BufferSize, !Worsened ); if( URL != NULL ){ RetVal = issuePost( URL, Buffer, BufferSize ); free( URL ); }; return RetVal; }; bool protocolDefender::StatusUpdate( void ){ bool RetVal= false; char Buffer[1024]; int BufferSize = 0; CSTR URL; URL = buildURL( SERVICE_URL_TEMPLATE_STATUS, m_nScannerDimentions, m_pScannerName ); #ifdef DEBUG LOG(URL); #endif RetVal = buildStatusPackage( ( unsigned char *) Buffer, BufferSize, m_bCurrentStatus ); if( URL != NULL ){ RetVal = issuePost( URL, Buffer, BufferSize ); free( URL ); }; return RetVal; }; bool protocolDefender::ErrorReporting( int lvl, CSTR Descr ){ bool RetVal= false; char Buffer[1024]; int BufferSize = 0; CSTR URL; URL = buildURL( SERVICE_URL_TEMPLATE_FAULT, m_nScannerDimentions, m_pScannerName ); #ifdef DEBUG LOG(URL); #endif RetVal = buildFaultPackage( ( unsigned char *) Buffer, BufferSize, lvl, Descr ); if( URL != NULL ){ RetVal = issuePost( URL, Buffer, BufferSize ); free( URL ); }; return RetVal; }; bool protocolDefender::StartTimeSynch( void ){ bool RetVal= false; //LOG("TimeSYnc"); /*char Buffer[1024]; int BufferSize = 0; CSTR URL = NULL; URL = buildURL( SERVICE_URL_TEMPLATE_CLOCKSYNC, m_nScannerDimentions, m_pScannerName ); RetVal = buildClockSyncPackage(( unsigned char *) Buffer, BufferSize ); if( URL != NULL ){ RetVal = issuePost( URL, Buffer, BufferSize ); free( URL ); };*/ return RetVal; }; bool protocolDefender::packetHandler( protocolPackage *Package ){ //LOG("PACKET HANDLER"); if( Package==NULL )return false; if( Package->m_nSize == 0 ) return false; if( Package->m_pData == NULL ) return false; if( Package->m_nCode != 200 ) return false; try{ XMLNode node1 = XMLNode::parseString( (const char *)Package->m_pData, "root"); if( node1.isEmpty() ){ char BUBU[128]; sprintf( BUBU, "The package[%d:%d] XML data is not a valid XML data. Cann't find root element. Code[%d]", strlen((CSTR)Package->m_pData), Package->m_nSize, Package->m_nCode ); LOG_ERROR(BUBU); } else { LOG( node1.getName() ); if( strstr(node1.getName(),"ClockSync")!=NULL ){ #ifdef __linux__ unsigned long long t0, t1, t2, t3; #else unsigned __int64 t0, t1, t2, t3; #endif XMLNode node3 = node1.getChildNode("t0", 0); XMLNode node4 = node1.getChildNode("t1", 0); XMLNode node5 = node1.getChildNode("t2", 0); t3 = getSHTimeValue(); #ifdef __linux__ t0 = atoll(node3.getText(0)); t1 = atoll(node4.getText(0)); t2 = atoll(node5.getText(0)); #else t0 = _atoi64(node3.getText(0)); t1 = _atoi64(node4.getText(0)); t2 = _atoi64(node5.getText(0)); #endif int offset = ((t1-t0)+(t2-t3))/2; int latency = (t3-t0)-(t2-t1); int jagajaga = offset+latency; //if( 0 & correctSHTime( jagajaga ) ){ // LOG("Time is Synced"); // g_iTimeDiff = 0; //} else { // LOG_ERROR("Time Sync Unucessfull - Will use DIFF!"); g_iTimeDiff = jagajaga; //}; }; }; }catch( exception e ){ return false; }; return true; }; #undef LOG #undef LOG_ERROR